removed makro calls, dokumentation
[libfirm] / ir / ir / ircgcons.c
index 0abe066..4e6f2cd 100644 (file)
@@ -1,13 +1,19 @@
-/* -------------------------------------------------------------------
- * $Id$
- * -------------------------------------------------------------------
- * Auf- und Abbau der interprozeduralen Darstellung (Explizite
- * interprozedurale Abhängigkeiten).
- *
- * Erstellt: Hubert Schmid, 09.06.2002
- * ---------------------------------------------------------------- */
-
-
+/*
+ * Project:     libFIRM
+ * File name:   ir/ir/ircgcons.c
+ * Purpose:     Construction and removal of interprocedural representation
+ *              (explicit interprocedural dependencies).
+ * Author:      Hubert Schmid
+ * Modified by:
+ * Created:     09.06.2002
+ * CVS-ID:      $Id$
+ * Copyright:   (c) 2002-2003 Universität Karlsruhe
+ * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ */
+
+
+#include <string.h>
+#include <stdbool.h>
 #include "ircgcons.h"
 
 #include "array.h"
@@ -16,7 +22,6 @@
 #include "irgmod.h"
 #include "irgwalk.h"
 #include "irflag.h"
-#include "misc.h"
 
 
 /* Datenstruktur für jede Methode */
@@ -65,17 +70,17 @@ static void caller_init(int arr_length, entity ** free_methods) {
 }
 
 
-static void clear_link(ir_node * node, void * env) {
+static INLINE void clear_link(ir_node * node, void * env) {
   set_irn_link(node, NULL);
 }
 
-
-static inline ir_node * tail(ir_node * node) {
+/*
+static INLINE ir_node * tail(ir_node * node) {
   ir_node * link;
   for (; (link = get_irn_link(node)); node = link) ;
   return node;
 }
-
+*/
 
 /* Call-Operationen an die "link"-Liste von "call_tail" anhängen (und
  * "call_tail" aktualisieren), Proj-Operationen in die Liste ihrer Definition
@@ -124,6 +129,7 @@ static void collect_phicallproj(void) {
     ir_graph * irg = get_irp_irg(i);
     ir_node * start = get_irg_start(irg);
     ir_node * end = get_irg_end(irg);
+    current_ir_graph = irg;
     assert(irg && start);
     /* Die speziellen Parameter der Start-Operation extra verlinken,
      * auch wenn sie nicht im intraprozeduralen Graphen erreichbar
@@ -131,7 +137,7 @@ static void collect_phicallproj(void) {
     link(start, get_irg_frame(irg));
     link(start, get_irg_globals(irg));
     /* walk */
-    irg_walk_graph(irg, clear_link, (irg_walk_func) collect_phicallproj_walker, &end);
+    irg_walk_graph(irg, clear_link, (irg_walk_func *) collect_phicallproj_walker, &end);
   }
 }
 
@@ -251,7 +257,7 @@ static void prepare_irg_end(ir_graph * irg, irg_data_t * data) {
     }
   }
   if (n_ret > 0) {
-    int n_res = get_method_n_res(get_entity_type(get_irg_ent(irg)));
+    int n_res = get_method_n_ress(get_entity_type(get_irg_ent(irg)));
     ir_node ** in = NEW_ARR_F(ir_node *, n_ret);
     /* block */
     for (i = n_ret - 1; i >= 0; --i) {
@@ -280,8 +286,10 @@ static void prepare_irg_end(ir_graph * irg, irg_data_t * data) {
        if (!mode && get_irn_mode(in[i]) != mode_T)
          mode = get_irn_mode(in[i]);
       }
-      assert(mode); /* @@@ else we must create a Bad node */
-      data->res[j] = new_Phi(n_ret, in, mode);
+      if (mode)
+       data->res[j] = new_Phi(n_ret, in, mode);
+      else  /* All preds are Bad */
+       data->res[j] = new_Bad();
     }
     DEL_ARR_F(in);
   }
@@ -387,7 +395,7 @@ static void construct_start(entity * caller, entity * callee,
   ir_node * start = get_irg_start(irg), * filter;
 
   assert(irg);
-  assert(get_entity_peculiarity(callee) == existent); /* Else data is not initalized. */
+  assert(get_entity_peculiarity(callee) == peculiarity_existent); /* Else data is not initalized. */
   assert((0 <= data->count) &&
         (data->count < get_Block_cg_n_cfgpreds(get_nodes_Block(start))));
   set_Block_cg_cfgpred(get_nodes_Block(start), data->count, exec);
@@ -490,6 +498,32 @@ static ir_node * get_except(ir_node * call) {
   return NULL;
 }
 
+/* Returns true if control flow operation exc is predecessor of end
+   block in irg.  Works also for Return nodes, not only exceptions. */
+static bool exc_branches_to_end(ir_graph *irg, ir_node *exc) {
+  int i;
+  ir_node *end = get_irg_end_block(irg);
+  for (i = get_Block_n_cfgpreds(end) -1; i >= 0; --i)
+    if (get_Block_cfgpred(end, i) == exc) return true;
+  return false;
+}
+
+/* Returns true if only caller of irg is "Unknown". */
+static bool is_outermost_graph(ir_graph *irg) {
+  irg_data_t * data = get_entity_link(get_irg_ent(irg));
+  if (data->count) {
+    return false;
+  } else if (data->open) {
+    /* Die Methode wird nur von "der" unbekannten Aufrufstelle
+     * aufgerufen. Darstellung wird für diese Methode nicht
+     * geändert. */
+  } else {
+    /* Methode kann nicht aufgerufen werden. Die Darstellung wird
+     * für diese Methode nicht geändert. Das kann nicht vorkommen,
+     * wenn zuvor "gc_irgs()" aufgerufen wurde. */
+  }
+  return true;
+}
 
 /* Grundblock der Call-Operation aufteilen. CallBegin- und Filter-Operationen
  * einfügen. Die Steuer- und Datenflussabhängigkeiten von den aufgerufenen
@@ -522,7 +556,8 @@ static void construct_call(ir_node * call) {
   /* Operationen verschieben */
   move_phis(post_block, pre_block);
   move_nodes(post_block, pre_block, call);
-  /* @@@ GL Wer setzt die Laenge des PostBlock cgfpred array auf 1? */
+  /* @@@ GL Wer setzt die Laenge des PostBlock cgfpred array auf 1?
+     GL: na, dieser Befehl... generiert neuen array. */
   set_irn_in(post_block, 1, &jmp);
 
   /* Wiederverwendete Daten initialisieren. */
@@ -532,6 +567,12 @@ static void construct_call(ir_node * call) {
     data[i] = get_entity_link(callees[i]);
   }
 
+  /*
+   * Set flag to suppress verifying placement on proper irg:
+   * optimization can return block on other irg.
+   */
+  set_interprocedural_view(1);
+
   /* Die interprozeduralen Steuerflussvorgänger des post_block
    * bestimmen. */
   for (i = 0; i < n_callees; ++i) {
@@ -546,17 +587,45 @@ static void construct_call(ir_node * call) {
       in[i] = new_Unknown();
     }
   }
+  set_interprocedural_view(0);
+
   set_Block_cg_cfgpred_arr(post_block, n_callees, in);
 
   /* Die interprozeduralen Steuerflussvorgänger des except_block
    * bestimmen. */
   if ((proj = get_except(call)) != NULL) {
-    except_block = create_Block(1, &proj);
-    set_nodes_Block(proj, except_block);
-    exchange(proj, new_Break());
-    set_irg_current_block(current_ir_graph, pre_block);
-    set_irn_n(except_block, 0, new_Proj(call, mode_X, 1));
-    set_irg_current_block(current_ir_graph, post_block);
+    int preds = 0;
+    bool exc_to_end = false;
+#if 1
+    if (exc_branches_to_end(current_ir_graph, proj)) {
+      /* The Call aborts the procedure if it returns with an exception.
+        If this is an outermost procedure, the normal handling of exceptions
+        will generate a Break that goes to the end block.  This is illegal
+        Frim. So directly branch to the end block with all exceptions. */
+      exc_to_end = true;
+      if (is_outermost_graph(current_ir_graph)) {
+       except_block = get_irg_end_block(current_ir_graph);
+      } else {
+       irg_data_t * data = get_entity_link(get_irg_ent(current_ir_graph));
+       except_block = get_nodes_block(data->except);
+      }
+    } else
+#endif
+      {
+      except_block = create_Block(1, &proj);
+      set_nodes_Block(proj, except_block);
+      exchange(proj, new_Break());
+      set_irg_current_block(current_ir_graph, pre_block);
+      set_irn_n(except_block, 0, new_Proj(call, mode_X, 1));
+      set_irg_current_block(current_ir_graph, post_block);
+    }
+
+    /*
+     * Set flag to suppress verifying placement on proper irg:
+     * optimization can return block on other irg.
+     */
+    set_interprocedural_view(1);
+
     for (i = 0; i < n_callees; ++i) {
       entity * callee = get_Call_callee(call, i);
       if (data[i]) { /* explicit */
@@ -570,8 +639,25 @@ static void construct_call(ir_node * call) {
        in[i] = new_Unknown();
       }
     }
-    set_Block_cg_cfgpred_arr(except_block, n_callees, in);
+
+    preds = n_callees;
+    if (exc_to_end) {
+      /* append all existing preds of the end block to new in array.
+       * Normal access routine guarantees that with first visit we
+       * get the normal preds, and from then on the _cg_ preds.
+       * (interporcedural view is set!)
+       * Do not add the exc pred of end we are replacing! */
+      for (i = get_Block_n_cfgpreds(except_block)-1; i >= 0; --i) {
+       ir_node *pred = get_Block_cfgpred(except_block, i);
+       if (pred != proj) {
+         ARR_APP1(ir_node *, in, pred);
+         preds++;
+       }
+      }
+    }
+    set_Block_cg_cfgpred_arr(except_block, preds, in);
   }
+  set_interprocedural_view(0);
 
   /* Diesen Vorgänger in den Start-Blöcken der aufgerufenen Methoden
    * eintragen. */
@@ -696,6 +782,9 @@ static void destruct_walker(ir_node * node, void * env) {
     exchange(node, new_Jmp());
   } else if (get_irn_op(node) == op_Call) {
     remove_Call_callee_arr(node);
+  } else if (get_irn_op(node) == op_Proj) {
+    // some ProjX end up in strage blocks.
+    set_nodes_block(node, get_nodes_block(get_Proj_pred(node)));
   }
 }