Added RTA --flo
[libfirm] / ir / ana / cgana.c
index 619c304..1809e2f 100644 (file)
  */
 
 /**
- * Intraprozedurale Analyse zur Abschätzung der Aufrulrelation. Es
+ * Intraprozedurale Analyse zur Abschätzung der Aufrufrelation. Es
  * wird eine Menge von freien Methoden und anschließend die an den
  * Call-Operationen aufrufbaren Methoden bestimmt.
  *
  */
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
 
 #include <stdlib.h>
 #include "cgana.h"
+#include "rta.h"
 
 
 #include "eset.h"
@@ -28,8 +32,8 @@
 #include "irgwalk.h"
 #include "ircons.h"
 #include "irgmod.h"
-#include "irnode.h"
-#include "irflag.h"
+#include "irnode_t.h"
+#include "irflag_t.h"
 
 #include "dbginfo_t.h"
 
@@ -47,7 +51,7 @@ static eset * entities = NULL;
 
 /* Bestimmt die eindeutige Methode, die die Methode für den
  * übergebenene (dynamischen) Typ überschreibt. */
-static entity * get_implementation(type * class, entity * method) {
+entity * get_implementation(type * class, entity * method) {
   int i;
   if (get_entity_peculiarity(method) != peculiarity_description &&
       get_entity_owner(method) == class) {
@@ -65,7 +69,7 @@ static entity * get_implementation(type * class, entity * method) {
       return e;
     }
   }
-  assert(0 && "implemenation not found");
+  assert(0 && "implementation not found");
   return NULL;
 }
 
@@ -76,10 +80,10 @@ entity *get_inherited_methods_implementation(entity *inh_meth) {
   ir_node *addr = get_atomic_ent_value(inh_meth);
   assert(addr && "constant entity without value");
 
-  if (get_irn_op(addr) == op_Const) {
+  if (intern_get_irn_op(addr) == op_Const) {
     impl_meth = tarval_to_entity(get_Const_tarval(addr));
   } else {
-    assert(0 && "Complex constant values not supported -- adress of method should be straight constant!");
+    assert(0 && "Complex constant values not supported -- address of method should be straight constant!");
   }
   if (impl_meth && (get_entity_peculiarity(impl_meth) != peculiarity_existent)) {
     printf("this_meth: "); DDMEO(inh_meth);
@@ -106,8 +110,8 @@ void collect_impls(entity *method, eset *set, int *size, bool *open) {
     } else {
       assert(get_entity_irg(method) != NULL);
       if (!eset_contains(set, method)) {
-       eset_insert(set, method);
-       ++(*size);
+    eset_insert(set, method);
+    ++(*size);
       }
     }
   }
@@ -120,8 +124,8 @@ void collect_impls(entity *method, eset *set, int *size, bool *open) {
     } else {
       assert(get_entity_irg(impl_ent) != NULL);
       if (!eset_contains(set, impl_ent)) {
-       eset_insert(set, impl_ent);
-       ++(*size);
+    eset_insert(set, impl_ent);
+    ++(*size);
       }
     }
   }
@@ -173,41 +177,41 @@ static entity ** get_impl_methods(entity * method) {
 #define SIZ(x)    sizeof(x)/sizeof((x)[0])
 
 #define DBG_OPT_NORMALIZE                                                      \
-         __dbg_info_merge_pair(new_node, node, dbg_const_eval)
+      __dbg_info_merge_pair(new_node, node, dbg_const_eval)
 #define DBG_OPT_POLY_ALLOC                                                     \
   do {                                                                         \
-       ir_node *ons[2];                                                       \
-       ons[0] = node;                                                         \
-       ons[1] = skip_Proj(get_Sel_ptr(node));                                 \
-       __dbg_info_merge_sets(&new_node, 1, ons, SIZ(ons), dbg_rem_poly_call); \
+    ir_node *ons[2];                                                       \
+    ons[0] = node;                                                         \
+    ons[1] = skip_Proj(get_Sel_ptr(node));                                 \
+    __dbg_info_merge_sets(&new_node, 1, ons, SIZ(ons), dbg_rem_poly_call); \
      } while(0)
 #define DBG_OPT_POLY                                                           \
-         __dbg_info_merge_pair(new_node, node, dbg_rem_poly_call)
+      __dbg_info_merge_pair(new_node, node, dbg_rem_poly_call)
 
 
 static void sel_methods_walker(ir_node * node, pmap * ldname_map) {
-  if (get_irn_op(node) == op_SymConst) {
+  if (intern_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 (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 */
+      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);
+    }
       }
     }
-  } else if (get_irn_op(node) == op_Sel &&
-            is_method_type(get_entity_type(get_Sel_entity(node)))) {
+  } else if (intern_get_irn_op(node) == op_Sel &&
+         is_method_type(get_entity_type(get_Sel_entity(node)))) {
     entity * ent = get_Sel_entity(node);
-    if (get_optimize() && get_opt_dyn_meth_dispatch() &&
-       (get_irn_op(skip_Proj(get_Sel_ptr(node))) == op_Alloc)) {
+    if (get_opt_optimize() && get_opt_dyn_meth_dispatch() &&
+    (intern_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. */
@@ -215,7 +219,7 @@ static void sel_methods_walker(ir_node * node, pmap * ldname_map) {
       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. */
+     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
@@ -229,52 +233,55 @@ static void sel_methods_walker(ir_node * node, pmap * ldname_map) {
     } else {
       assert(get_entity_peculiarity(ent) != peculiarity_inherited);
       if (!eset_contains(entities, ent)) {
-       /* Entity noch nicht behandelt. Alle (intern oder extern)
-        * implementierten Methoden suchen, die diese Entity
-        * überschreiben. Die Menge an entity.link speichern. */
-       set_entity_link(ent, get_impl_methods(ent));
-       eset_insert(entities, ent);
+    /* Entity noch nicht behandelt. Alle (intern oder extern)
+     * implementierten Methoden suchen, die diese Entity
+     * überschreiben. Die Menge an entity.link speichern. */
+    set_entity_link(ent, get_impl_methods(ent));
+    eset_insert(entities, ent);
       }
       if (get_entity_link(ent) == NULL) {
-       /* Die Sel-Operation kann nie einen Zeiger auf eine aufrufbare
-        * Methode zurückgeben. Damit ist sie insbesondere nicht
-        * ausführbar und nicht erreichbar. */
-       /* 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 in method %s which has "
-                 "no implementation!\n", get_entity_name(ent),
-                 get_entity_name(get_irg_ent(current_ir_graph)));
-       } else {
-         exchange(node, new_Bad());
-       }
+    /* Die Sel-Operation kann nie einen Zeiger auf eine aufrufbare
+     * Methode zurückgeben. Damit ist sie insbesondere nicht
+     * ausführbar und nicht erreichbar. */
+    /* 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");
+    } else {
+      exchange(node, new_Bad());
+    }
       } else {
-       entity ** arr = get_entity_link(ent);
+    entity ** arr = get_entity_link(ent);
 
 #if 0
-       int i;
-       printf("\nCall site "); DDMN(node);
-       printf("  in "); DDME(get_irg_ent(current_ir_graph));
-       printf("  can call:\n");
-       for (i = 0; i < ARR_LEN(arr); i++) {
-         printf("   - "); DDME(arr[i]);
-         printf("     with owner "); DDMT(get_entity_owner(arr[i]));
-       }
-       printf("\n");
+    int i;
+    printf("\nCall site "); DDMN(node);
+    printf("  in "); DDME(get_irg_ent(current_ir_graph));
+    printf("  can call:\n");
+    for (i = 0; i < ARR_LEN(arr); i++) {
+      printf("   - "); DDME(arr[i]);
+      printf("     with owner "); DDMT(get_entity_owner(arr[i]));
+    }
+    printf("\n");
 #endif
 
-       if (get_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
-          * 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));
-         new_node = copy_const_value(get_atomic_ent_value(arr[0]));         DBG_OPT_POLY;
-         exchange (node, new_node);
-       }
+    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
+       * 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));
+      new_node = copy_const_value(get_atomic_ent_value(arr[0]));         DBG_OPT_POLY;
+      exchange (node, new_node);
+    }
       }
     }
   }
@@ -326,7 +333,7 @@ static entity ** get_Sel_arr(ir_node * sel) {
   static entity ** NULL_ARRAY = NULL;
   entity * ent;
   entity ** arr;
-  assert(sel && get_irn_op(sel) == op_Sel);
+  assert(sel && intern_get_irn_op(sel) == op_Sel);
   ent = get_Sel_entity(sel);
   assert(is_method_type(get_entity_type(ent))); /* what else? */
   arr = get_entity_link(ent);
@@ -370,17 +377,17 @@ static void callee_ana_proj(ir_node * node, long n, eset * methods) {
   }
   set_irn_link(node, MARK);
 
-  switch (get_irn_opcode(node)) {
+  switch (intern_get_irn_opcode(node)) {
   case iro_Proj: {
     /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
      * op_Tuple oder ein Knoten, der eine "freie Methode"
      * zurückgibt. */
     ir_node * pred = get_Proj_pred(node);
     if (get_irn_link(pred) != MARK) {
-      if (get_irn_op(pred) == op_Tuple) {
-       callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
+      if (intern_get_irn_op(pred) == op_Tuple) {
+    callee_ana_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, methods);
       } else {
-       eset_insert(methods, MARK); /* free method -> unknown */
+    eset_insert(methods, MARK); /* free method -> unknown */
       }
     }
     break;
@@ -414,7 +421,7 @@ static void callee_ana_node(ir_node * node, eset * methods) {
   }
   set_irn_link(node, MARK);
 
-  switch (get_irn_opcode(node)) {
+  switch (intern_get_irn_opcode(node)) {
   case iro_SymConst:
     /* externe Methode (wegen fix_symconst!) */
     eset_insert(methods, MARK); /* free method -> unknown */
@@ -438,9 +445,9 @@ static void callee_ana_node(ir_node * node, eset * methods) {
     for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
       entity * ent = get_Sel_method(node, i);
       if (ent) {
-       eset_insert(methods, ent);
+    eset_insert(methods, ent);
       } else {
-       eset_insert(methods, MARK);
+    eset_insert(methods, MARK);
       }
     }
     break;
@@ -480,7 +487,7 @@ static void callee_ana_node(ir_node * node, eset * methods) {
 
 
 static void callee_walker(ir_node * call, void * env) {
-  if (get_irn_op(call) == op_Call) {
+  if (intern_get_irn_op(call) == op_Call) {
     eset * methods = eset_create();
     entity * ent;
     entity ** arr = NEW_ARR_F(entity *, 0);
@@ -490,7 +497,7 @@ static void callee_walker(ir_node * call, void * env) {
     }
     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) {
@@ -536,13 +543,13 @@ static void free_mark_proj(ir_node * node, long n, eset * set) {
     return;
   }
   set_irn_link(node, MARK);
-  switch (get_irn_opcode(node)) {
+  switch (intern_get_irn_opcode(node)) {
   case iro_Proj: {
     /* proj_proj: in einem "sinnvollen" Graphen kommt jetzt ein
      * op_Tuple oder ein Knoten, der in "free_ana_walker" behandelt
      * wird. */
     ir_node * pred = get_Proj_pred(node);
-    if (get_irn_link(pred) != MARK && get_irn_op(pred) == op_Tuple) {
+    if (get_irn_link(pred) != MARK && intern_get_irn_op(pred) == op_Tuple) {
       free_mark_proj(get_Tuple_pred(pred, get_Proj_proj(node)), n, set);
     } else {
       /* nothing: da in "free_ana_walker" behandelt. */
@@ -580,12 +587,12 @@ static void free_mark(ir_node * node, eset * set) {
     return; /* already visited */
   }
   set_irn_link(node, MARK);
-  switch (get_irn_opcode(node)) {
+  switch (intern_get_irn_opcode(node)) {
   case iro_Sel: {
     entity * ent = get_Sel_entity(node);
     if (is_method_type(get_entity_type(ent))) {
       for (i = get_Sel_n_methods(node) - 1; i >= 0; --i) {
-       eset_insert(set, get_Sel_method(node, i));
+    eset_insert(set, get_Sel_method(node, i));
       }
     }
     break;
@@ -628,7 +635,7 @@ static void free_ana_walker(ir_node * node, eset * set) {
     /* bereits in einem Zyklus besucht. */
     return;
   }
-  switch (get_irn_opcode(node)) {
+  switch (intern_get_irn_opcode(node)) {
   /* special nodes */
   case iro_Sel:
   case iro_SymConst:
@@ -645,8 +652,8 @@ static void free_ana_walker(ir_node * node, eset * set) {
     set_irn_link(node, MARK);
     for (i = get_Call_arity(node) - 1; i >= 0; --i) {
       ir_node * pred = get_Call_param(node, i);
-      if (mode_is_reference(get_irn_mode(pred))) {
-       free_mark(pred, set);
+      if (mode_is_reference(intern_get_irn_mode(pred))) {
+    free_mark(pred, set);
       }
     }
     break;
@@ -654,10 +661,10 @@ static void free_ana_walker(ir_node * node, eset * set) {
    * jemand das Gegenteil implementiert. */
   default:
     set_irn_link(node, MARK);
-    for (i = get_irn_arity(node) - 1; i >= 0; --i) {
+    for (i = intern_get_irn_arity(node) - 1; i >= 0; --i) {
       ir_node * pred = get_irn_n(node, i);
-      if (mode_is_reference(get_irn_mode(pred))) {
-       free_mark(pred, set);
+      if (mode_is_reference(intern_get_irn_mode(pred))) {
+    free_mark(pred, set);
       }
     }
     break;
@@ -676,6 +683,7 @@ static entity ** get_free_methods(void) {
   int i;
   entity ** arr = NEW_ARR_F(entity *, 0);
   entity * ent;
+
   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
     ir_graph * irg = get_irp_irg(i);
     entity * ent = get_irg_ent(irg);
@@ -683,13 +691,16 @@ static entity ** get_free_methods(void) {
     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);
   }
-  /* Hauptprogramm ist auch frei, auch wenn es nicht "external
+  /* Hauptprogramm ist auch dann frei, wenn es nicht "external
    * visible" ist. */
-  if(get_irp_main_irg()) {
+  if (get_irp_main_irg()) {
     eset_insert(set, get_irg_ent(get_irp_main_irg()));
   }
+  /* Wandle Menge in Feld um.  Effizienter. */
   for (ent = eset_first(set); ent; ent = eset_next(set)) {
     ARR_APP1(entity *, arr, ent);
   }
@@ -702,6 +713,9 @@ void cgana(int *length, entity ***free_methods) {
   entity ** free_meths;
   int i;
 
+  rta_init    ();
+  rta_cleanup ();
+
   sel_methods_init();
   free_meths = get_free_methods();
   callee_ana();