Set the outs to inconsistent at the end of transform_irg().
[libfirm] / ir / opt / tailrec.c
index 93d9083..7db1fea 100644 (file)
 # include "config.h"
 #endif
 
-#ifdef HAVE_STRING_H
 #include <string.h>
-#endif
-
 #include <assert.h>
-#include "tailrec.h"
+
+#include "iroptimize.h"
+#include "scalar_replace.h"
 #include "array.h"
 #include "irprog_t.h"
 #include "irgwalk.h"
@@ -44,8 +43,6 @@
 #include "ircons.h"
 #include "irflag.h"
 #include "trouts.h"
-#include "return.h"
-#include "scalar_replace.h"
 #include "irouts.h"
 #include "irhooks.h"
 #include "xmalloc.h"
@@ -331,13 +328,14 @@ int opt_tail_rec_irg(ir_graph *irg) {
                if (! is_Call(call))
                        continue;
 
+               /* the call must be in the same block as the return */
+               if (get_nodes_block(call) != get_nodes_block(ret))
+                       continue;
+
                /* check if it's a recursive call */
                call_ptr = get_Call_ptr(call);
 
-               if (get_irn_op(call_ptr) != op_SymConst)
-                       continue;
-
-               if (get_SymConst_kind(call_ptr) != symconst_addr_ent)
+               if (! is_SymConst(call_ptr) || get_SymConst_kind(call_ptr) != symconst_addr_ent)
                        continue;
 
                ent = get_SymConst_entity(call_ptr);
@@ -358,9 +356,9 @@ int opt_tail_rec_irg(ir_graph *irg) {
                        continue;
 
                /*
-               * Check, that the types match. At least in C
-               * this might fail.
-               */
+                * Check, that the types match. At least in C
+                * this might fail.
+                */
                mtd_type  = get_entity_type(ent);
                call_type = get_Call_type(call);
 
@@ -375,7 +373,7 @@ int opt_tail_rec_irg(ir_graph *irg) {
                        dump_type(mtd_type);
                        dump_type(call_type);
 #endif
-                       return 0;
+                       continue;
                }
 
                /* here, we have found a call */
@@ -416,6 +414,8 @@ void opt_tail_recursion(void) {
        for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
                irg = get_irp_irg(i);
 
+               current_ir_graph = irg;
+
                if (opt_tail_rec_irg(irg))
                        ++n_opt_applications;
        }