fixed complicated code, commented out one optimization that generates unwanted Bad...
[libfirm] / ir / ana / cgana.c
index bd63d53..71e5889 100644 (file)
 #include "cgana.h"
 #include "rta.h"
 
-
-#include "eset.h"
-#include "pmap.h"
-#include "array.h"
-#include "irprog.h"
+#include "irnode_t.h"
+#include "irmode_t.h"
+#include "irprog_t.h"
 #include "irgwalk.h"
 #include "ircons.h"
 #include "irgmod.h"
-#include "irnode_t.h"
-#include "irflag_t.h"
 
+#include "irflag_t.h"
 #include "dbginfo_t.h"
 
+#include "eset.h"
+#include "pmap.h"
+#include "array.h"
+
+#include "irdump.h"
+
 /* Eindeutige Adresse zur Markierung von besuchten Knoten und zur
  * Darstellung der unbekannten Methode. */
 static void * MARK = &MARK;
@@ -79,9 +82,9 @@ static entity *get_inherited_methods_implementation(entity *inh_meth) {
   entity *impl_meth = NULL;
   ir_node *addr = get_atomic_ent_value(inh_meth);
   assert(addr && "constant entity without value");
-
-  if (get_irn_op(addr) == op_Const) {
-    impl_meth = tarval_to_entity(get_Const_tarval(addr));
+  if ((get_irn_op(addr) == op_SymConst) &&
+            (get_SymConst_kind(addr) == symconst_addr_ent)) {
+    impl_meth = get_SymConst_entity(addr);
   } else {
     assert(0 && "Complex constant values not supported -- address of method should be straight constant!");
   }
@@ -140,7 +143,7 @@ static void collect_impls(entity *method, eset *set, int *size, bool *open) {
       }
     }
   }
-  /** recursive descend **/
+  /** recursive descent **/
   for (i = get_entity_n_overwrittenby(method) - 1; i >= 0; --i)
     collect_impls(get_entity_overwrittenby(method, i), set, size, open);
 }
@@ -205,10 +208,10 @@ static entity ** get_impl_methods(entity * method) {
 
 /** Analyse address computations.
  *
- *  - If the node is a SymConst replace it by Const(ent) if possible.
+ *  - If the node is a SymConst(name) replace it by SymConst(ent) if possible.
  *  - If the node is a Sel:
  *    * If the pointer to the Sel comes directly from an Alloc node
- *      replace the Sel by a Const(ent).
+ *      replace the Sel by a SymConst(ent).
  *
  *
  *  @param node The node to analyze
@@ -216,48 +219,43 @@ static entity ** get_impl_methods(entity * method) {
  */
 static void sel_methods_walker(ir_node * node, pmap * ldname_map) {
 
+  /* SymConst(name)-Operation durch SymConst(ent)-Operation ersetzen. */
   if (get_irn_op(node) == op_SymConst) {
-    /* Wenn möglich SymConst-Operation durch Const-Operation
-     * ersetzen. */
-    if (get_SymConst_kind(node) == linkage_ptr_info) {
-      pmap_entry *entry = pmap_find(ldname_map, (void *) get_SymConst_ptrinfo(node));
+    if (get_SymConst_kind(node) == symconst_addr_name) {
+      pmap_entry * entry = pmap_find(ldname_map, (void *) get_SymConst_name(node));
       if (entry != NULL) { /* Method is declared in the compiled code */
-        entity *ent = entry->value;
-        if (get_opt_normalize() && (get_entity_visibility(ent) != visibility_external_allocated)) {
-         /* Meth. is defined */
-          ir_node *new_node;
-          assert(get_entity_irg(ent));
-          set_irg_current_block(current_ir_graph, get_nodes_Block(node));
-          new_node = new_d_Const(get_irn_dbg_info(node),
-                                 mode_P_mach, new_tarval_from_entity(ent, mode_P_mach));       DBG_OPT_NORMALIZE;
-          exchange(node, new_node);
-        }
+       entity * ent = entry->value;
+       if (get_opt_normalize() && (get_entity_visibility(ent) != visibility_external_allocated)) { /* Meth. is defined */
+         set_irg_current_block(current_ir_graph, get_nodes_block(node));
+         ir_node *new_node = copy_const_value(get_atomic_ent_value(ent));
+                                                                                    DBG_OPT_NORMALIZE;
+         assert(get_entity_irg(ent));
+         DDMN(new_node);
+         exchange(node, new_node);
+       }
       }
     }
-  } else if (get_irn_op(node) == op_Sel &&
-             is_method_type(get_entity_type(get_Sel_entity(node)))) {
+  }
+
+  else if (get_irn_op(node) == op_Sel &&
+          is_method_type(get_entity_type(get_Sel_entity(node)))) {
     entity * ent = get_Sel_entity(node);
+
+    /* Sel from Alloc: replace by constant */
     if (get_opt_optimize() && get_opt_dyn_meth_dispatch() &&
         (get_irn_op(skip_Proj(get_Sel_ptr(node))) == op_Alloc)) {
       ir_node *new_node;
       entity *called_ent;
       /* We know which method will be called, no dispatch necessary. */
-#if 0
-      assert(get_entity_peculiarity(ent) != peculiarity_description);
-      set_irg_current_block(current_ir_graph, get_nodes_Block(node));
-      /* @@@ Is this correct?? Alloc could reference a subtype of the owner
-         of Sel that overwrites the method referenced in Sel. */
-      /* @@@ Yes, this is wrong. GL, 10.3.04 */
-      new_node = copy_const_value(get_atomic_ent_value(ent));              DBG_OPT_POLY_ALLOC;
-#else
       called_ent = resolve_ent_polymorphy(get_Alloc_type(skip_Proj(get_Sel_ptr(node))), ent);
-      set_irg_current_block(current_ir_graph, get_nodes_Block(node));
+      set_irg_current_block(current_ir_graph, get_nodes_block(node));
       /* called_ent may not be description: has no Address/Const to Call! */
       assert(get_entity_peculiarity(called_ent) != peculiarity_description);
       new_node = copy_const_value(get_atomic_ent_value(called_ent));       DBG_OPT_POLY_ALLOC;
-#endif
       exchange (node, new_node);
-    } else {
+    }
+
+    else {
       assert(get_entity_peculiarity(ent) != peculiarity_inherited);
       if (!eset_contains(entities, ent)) {
         /* Entity noch nicht behandelt. Alle (intern oder extern)
@@ -273,15 +271,11 @@ static void sel_methods_walker(ir_node * node, pmap * ldname_map) {
         /* Gib eine Warnung aus wenn die Entitaet eine Beschreibung ist
            fuer die es keine Implementierung gibt. */
         if (get_entity_peculiarity(ent) == peculiarity_description) {
-          /* @@@ GL Methode um Fehler anzuzeigen aufrufen! */
-          printf("WARNING: Calling method description %s\n  in method %s\n  of class %s\n  which has "
-                 "no implementation!\n", get_entity_name(ent),
-                 get_entity_name(get_irg_ent(current_ir_graph)),
-                 get_type_name(get_entity_owner(get_irg_ent(current_ir_graph))));
-          printf("This happens when compiling a Java Interface that's never really used, i.e., "
-                 "no class implementing the interface is ever used.\n");
+         /* This is possible:  We call a method in a dead part of the program. */
         } else {
-          exchange(node, new_Bad());
+         DDMN(node);
+         assert(0);  /* Why should this happen ??? */
+          //exchange(node, new_Bad());
         }
       } else {
         entity ** arr = get_entity_link(ent);
@@ -289,7 +283,7 @@ static void sel_methods_walker(ir_node * node, pmap * ldname_map) {
 #if 0
         int i;
         printf("\nCall site "); DDMN(node);
-        printf("  in "); DDME(get_irg_ent(current_ir_graph));
+        printf("  in "); DDME(get_irg_entity(current_ir_graph));
         printf("  can call:\n");
         for (i = 0; i < ARR_LEN(arr); i++) {
           printf("   - "); DDME(arr[i]);
@@ -301,12 +295,13 @@ static void sel_methods_walker(ir_node * node, pmap * ldname_map) {
         if (get_opt_optimize() && get_opt_dyn_meth_dispatch() &&
             (ARR_LEN(arr) == 1 && arr[0] != NULL)) {
           ir_node *new_node;
-          /* Die Sel-Operation kann immer nur einen Wert auf eine
+          /* Die Sel-Operation kann immer nur _einen_ Wert auf eine
            * interne Methode zurückgeben. Wir können daher die
            * Sel-Operation durch eine Const- bzw. SymConst-Operation
            * ersetzen. */
-          set_irg_current_block(current_ir_graph, get_nodes_Block(node));
-          /* assert(get_entity_peculiarity(tarval_to_entity(get_Const_tarval(get_atomic_ent_value(arr[0])))) == peculiarity_existent); */
+          set_irg_current_block(current_ir_graph, get_nodes_block(node));
+          assert(get_entity_peculiarity(get_SymConst_entity(get_atomic_ent_value(arr[0]))) ==
+                peculiarity_existent);
           new_node = copy_const_value(get_atomic_ent_value(arr[0]));         DBG_OPT_POLY;
           exchange (node, new_node);
         }
@@ -325,7 +320,7 @@ static void sel_methods_init(void) {
   assert(entities == NULL);
   entities = eset_create();
   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
-    entity * ent = get_irg_ent(get_irp_irg(i));
+    entity * ent = get_irg_entity(get_irp_irg(i));
     /* Nur extern sichtbare Methode können überhaupt mit SymConst
      * aufgerufen werden. */
     if (get_entity_visibility(ent) != visibility_local) {
@@ -451,23 +446,21 @@ static void callee_ana_node(ir_node * node, eset * methods) {
 
   switch (get_irn_opcode(node)) {
   case iro_SymConst:
-    /* externe Methode (wegen fix_symconst!) */
-    eset_insert(methods, MARK); /* free method -> unknown */
-    break;
-
-  case iro_Const: {
-    /* interne Methode */
-    entity * ent = tarval_to_entity(get_Const_tarval(node));
-    assert(ent && is_method_type(get_entity_type(ent)));
-    if (get_entity_visibility(ent) != visibility_external_allocated) {
-      assert(get_entity_irg(ent));
-      eset_insert(methods, ent);
+    if (get_SymConst_kind(node) == symconst_addr_ent) {
+      entity * ent = get_SymConst_entity(node);
+      assert(ent && is_method_type(get_entity_type(ent)));
+      if (get_entity_visibility(ent) != visibility_external_allocated) {
+       assert(get_entity_irg(ent));
+       eset_insert(methods, ent);
+      } else {
+       eset_insert(methods, MARK); /* free method -> unknown */
+      }
     } else {
+      assert(get_SymConst_kind(node) == symconst_addr_name);
+      /* externe Methode (wegen fix_symconst!) */
       eset_insert(methods, MARK); /* free method -> unknown */
     }
     break;
-  }
-
   case iro_Sel:
     /* polymorphe Methode */
     for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
@@ -519,32 +512,34 @@ static void callee_walker(ir_node * call, void * env) {
     eset * methods = eset_create();
     entity * ent;
     entity ** arr = NEW_ARR_F(entity *, 0);
-    callee_ana_node(skip_nop(get_Call_ptr(call)), methods);
+    callee_ana_node(skip_Id(get_Call_ptr(call)), methods);
     if (eset_contains(methods, MARK)) { /* unknown method */
       ARR_APP1(entity *, arr, NULL);
     }
     for (ent = eset_first(methods); ent; ent = eset_next(methods)) {
       if (ent != MARK) {
-    ARR_APP1(entity *, arr, ent);
+       ARR_APP1(entity *, arr, ent);
       }
     }
-    if (ARR_LEN(arr) == 0) {
+#if 0  /* This generates Bad nodes when we don't want it.
+         Call it with a check for valid cgana information in local_optimize. */
+    if (ARR_LEN(arr) == 0 && get_opt_optimize() && get_opt_dyn_meth_dispatch()) {
       /* Kann vorkommen, wenn der Vorgänger beispielsweise eine
        * Sel-Operation war, die keine Methoden zurückgeben
        * konnte. Wir ersetzen die Call-Operation ebenfalls durch
        * eine Bad-Operation. Die Verlinkung muss wiederhergestellt
        * werden! */
-      /* exchange(call, new_Bad());  invalid firm */
-
       ir_node *mem = get_Call_mem(call);
       turn_into_tuple (call, 5 /* pn_Call_max */);
       set_Tuple_pred(call, pn_Call_M_regular       , mem);
       set_Tuple_pred(call, pn_Call_T_result        , new_Bad());
       set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
       set_Tuple_pred(call, pn_Call_X_except        , new_Bad());  /* new_Jmp() ?? new_Raise() ?? */
-      set_Tuple_pred(call, pn_Call_M_except        , mem);
+      set_Tuple_pred(call, pn_Call_M_except        , new_Bad());
 
-    } else {
+    } else
+#endif
+    {
       set_Call_callee_arr(call, ARR_LEN(arr), arr);
     }
     DEL_ARR_F(arr);
@@ -553,6 +548,15 @@ static void callee_walker(ir_node * call, void * env) {
 }
 
 
+static void remove_Tuples(ir_node * proj, void * env) {
+  ir_node *new;
+  if (get_irn_opcode(proj) != iro_Proj) return;
+
+  new = skip_Tuple(proj);
+  if (new != proj) exchange(proj, new);
+}
+
+
 /* Bestimmt für jede Call-Operation die Menge der aufrufbaren Methode
  * und speichert das Ergebnis in der Call-Operation. (siehe
  * "set_Call_callee"). "sel_methods" wird für den Aufbau benötigt und
@@ -561,9 +565,10 @@ static void callee_ana(void) {
   int i;
   /* Alle Graphen analysieren. */
   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
-    irg_walk_graph(get_irp_irg(i), callee_walker, NULL, NULL);
+    irg_walk_graph(get_irp_irg(i), callee_walker, remove_Tuples, NULL);
     set_irg_callee_info_state(get_irp_irg(i), irg_callee_info_consistent);
   }
+  set_irp_callee_info_state(irg_callee_info_consistent);
 }
 
 
@@ -635,18 +640,17 @@ static void free_mark(ir_node * node, eset * set) {
     break;
   }
   case iro_SymConst:
-    /* nothing: SymConst points to extern method */
-    break;
-  case iro_Const: {
-    tarval * val = get_Const_tarval(node);
-    if (tarval_is_entity(val)) { /* filter null pointer */
-      entity * ent = tarval_to_entity(val);
+    if (get_SymConst_kind(node) == symconst_addr_ent) {
+      entity * ent = get_SymConst_entity(node);
       if (is_method_type(get_entity_type(ent))) {
         eset_insert(set, ent);
       }
+    } else {
+      assert(get_SymConst_kind(node) == symconst_addr_name);
+      /* nothing: SymConst points to extern method */
     }
     break;
-  }
+
   case iro_Phi:
     for (i = get_Phi_n_preds(node) - 1; i >= 0; --i) {
       free_mark(get_Phi_pred(node, i), set);
@@ -715,27 +719,41 @@ static void free_ana_walker(ir_node * node, eset * set) {
  * SymConst-Operationen müssen in passende Const-Operationen
  * umgewandelt worden sein, d.h. SymConst-Operationen verweisen immer
  * auf eine echt externe Methode.  */
-static entity ** get_free_methods(void) {
+static entity ** get_free_methods(int whole)
+{
   eset * set = eset_create();
   int i;
   entity ** arr = NEW_ARR_F(entity *, 0);
   entity * ent;
 
+  if (! whole) {
+    for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
+      ir_graph * irg = get_irp_irg(i);
+      entity * ent = get_irg_entity(irg);
+      /* insert "external visible" methods. */
+      if (get_entity_visibility(ent) != visibility_local) {
+        eset_insert(set, ent);
+      }
+      /* Finde alle Methoden die in dieser Methode extern sichtbar werden,
+         z.B. da die Adresse einer Methode abgespeichert wird. */
+      irg_walk_graph(irg, NULL, (irg_walk_func *) free_ana_walker, set);
+    }
+  }
+
+  /* insert sticky methods, too */
   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
     ir_graph * irg = get_irp_irg(i);
-    entity * ent = get_irg_ent(irg);
+    entity * ent = get_irg_entity(irg);
     /* insert "external visible" methods. */
-    if (get_entity_visibility(ent) != visibility_local) {
+    if (get_entity_stickyness (ent) == stickyness_sticky) {
       eset_insert(set, ent);
     }
-    /* Finde alle Methoden die in dieser Methode extern sichtbar werden,
-       z.B. da die Adresse einer Methode abgespeichert wird. */
-    irg_walk_graph(irg, NULL, (irg_walk_func *) free_ana_walker, set);
   }
+
   /* Hauptprogramm ist auch dann frei, wenn es nicht "external
    * visible" ist. */
   if (get_irp_main_irg()) {
-    eset_insert(set, get_irg_ent(get_irp_main_irg()));
+    eset_insert(set, get_irg_entity(get_irp_main_irg()));
   }
   /* Wandle Menge in Feld um.  Effizienter. */
   for (ent = eset_first(set); ent; ent = eset_next(set)) {
@@ -746,12 +764,12 @@ static entity ** get_free_methods(void) {
   return arr;
 }
 
-void cgana(int *length, entity ***free_methods) {
+void cgana(int *length, entity ***free_methods, int whole) {
   entity ** free_meths;
   int i;
 
   sel_methods_init();
-  free_meths = get_free_methods();
+  free_meths = get_free_methods(whole);
   callee_ana();
   sel_methods_dispose();
 
@@ -763,13 +781,34 @@ void cgana(int *length, entity ***free_methods) {
   DEL_ARR_F(free_meths);
 }
 
+
+
+static void destruct_walker(ir_node * node, void * env) {
+  if (get_irn_op(node) == op_Call) {
+    remove_Call_callee_arr(node);
+  }
+}
+
+void free_callee_info(ir_graph *irg) {
+  irg_walk_graph(irg, destruct_walker, NULL, NULL);
+  set_irg_callee_info_state(irg, irg_callee_info_none);
+}
+
+
 /* Optimize the address expressions passed to call nodes.
- * Alle SymConst-Operationen, die auf interne Methoden verweisen,
- * werden durch Const-Operationen ersetzt.
- * Sel Knoten deren entitaeten nicht ueberschrieben werden, werden
- * durch Const ersetzt.
- * Sel Knoten, fuer die keine Implementierung existiert, werden
- * durch Bad ersetzt. */
+ *
+ * This optimization performs the following transformations for
+ * all ir graphs:
+ * - All SymConst operations that refer to intern methods are replaced
+ *   by Const operations refering to the corresponding entity.
+ * - Sel nodes, that select entities that are not overwritten are
+ *   replaced by Const nodes refering to the selected entity.
+ * - Sel nodes, for witch no method exists at all are replaced by Bad
+ *   nodes.
+ * - Sel nodes with a pointer input that is an Alloc node are replaced
+ *   by Const nodes refering to the entity that implements the method in
+ *   the type given by the Alloc node.
+ */
 void opt_call_addrs(void) {
   sel_methods_init();
   sel_methods_dispose();
@@ -779,7 +818,7 @@ void opt_call_addrs(void) {
   assert(entities == NULL);
   entities = eset_create();
   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
-    entity * ent = get_irg_ent(get_irp_irg(i));
+    entity * ent = get_irg_entity(get_irp_irg(i));
     /* Nur extern sichtbare Methoden können überhaupt mit SymConst
      * aufgerufen werden. */
     if (get_entity_visibility(ent) != local) {