replaced variable args macros by functions to make it c89 compatible
[libfirm] / ir / ana / cgana.c
index db38379..a750e02 100644 (file)
@@ -1,14 +1,23 @@
-/* -------------------------------------------------------------------
- * $Id$
- * -------------------------------------------------------------------
+/*
+ * Project:     libFIRM
+ * File name:   ir/ana/cgana.c
+ * Purpose:     Intraprozedural analyses to estimate the call graph.
+ * Author:      Hubert Schmid
+ * Modified by:
+ * Created:     09.06.2002
+ * CVS-ID:      $Id$
+ * Copyright:   (c) 1999-2003 Universität Karlsruhe
+ * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
+ */
+
+/**
  * Intraprozedurale Analyse zur Abschätzung der Aufrulrelation. Es
  * wird eine Menge von freien Methoden und anschließend die an den
  * Call-Operationen aufrufbaren Methoden bestimmt.
  *
- * Erstellt: Hubert Schmid, 09.06.2002
- * ---------------------------------------------------------------- */
-
+ */
 
+#include <stdlib.h>
 #include "cgana.h"
 
 
 #include "irgwalk.h"
 #include "ircons.h"
 #include "irgmod.h"
+#include "irnode.h"
+#include "irflag.h"
 
+#include "dbginfo_t.h"
 
 /* Eindeutige Adresse zur Markierung von besuchten Knoten und zur
  * Darstellung der unbekannten Methode. */
@@ -37,22 +49,85 @@ static eset * entities = NULL;
  * übergebenene (dynamischen) Typ überschreibt. */
 static entity * get_implementation(type * class, entity * method) {
   int i;
-  if (get_entity_peculiarity(method) != description && get_entity_owner(method) == class) {
+  if (get_entity_peculiarity(method) != peculiarity_description &&
+      get_entity_owner(method) == class) {
     return method;
   }
   for (i = get_entity_n_overwrittenby(method) - 1; i >= 0; --i) {
     entity * e = get_entity_overwrittenby(method, i);
-    if (get_entity_peculiarity(e) != description && get_entity_owner(e) == class) {
+    if (get_entity_peculiarity(e) != peculiarity_description && get_entity_owner(e) == class) {
       return e;
     }
   }
-  for (i = get_class_n_supertype(class) - 1; i >= 0; --i) {
+  for (i = get_class_n_supertypes(class) - 1; i >= 0; --i) {
     entity * e = get_implementation(get_class_supertype(class, i), method);
     if (e) {
       return e;
     }
   }
   assert(0 && "implemenation not found");
+  return NULL;
+}
+
+/* Returns the entity that contains the implementation of the inherited
+   entity if available, else returns the entity passed. */
+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));
+  } else {
+    assert(0 && "Complex constant values not supported -- adress of method should be straight constant!");
+  }
+  if (impl_meth && (get_entity_peculiarity(impl_meth) != peculiarity_existent)) {
+    printf("this_meth: "); DDMEO(inh_meth);
+    printf("impl meth: "); DDMEO(impl_meth);
+    assert(!impl_meth || get_entity_peculiarity(impl_meth) == peculiarity_existent);
+  }
+  return impl_meth? impl_meth : inh_meth;
+}
+
+
+/* Collect the entity representing the implementation of this
+   entity (not the same if inherited) and all entities for overwriting
+   implementations in "set".
+   If the implementation of the method is not included in the
+   compilation unit "open" is set to true.
+   A recursive descend in the overwritten relation.
+   Cycle-free, therefore must terminate. */
+void collect_impls(entity *method, eset *set, int *size, bool *open) {
+  int i;
+  if (get_entity_peculiarity(method) == peculiarity_existent) {
+    if (get_entity_visibility(method) == visibility_external_allocated) {
+      assert(get_entity_irg(method) == NULL);
+      *open = true;
+    } else {
+      assert(get_entity_irg(method) != NULL);
+      if (!eset_contains(set, method)) {
+       eset_insert(set, method);
+       ++(*size);
+      }
+    }
+  }
+  if (get_entity_peculiarity(method) == peculiarity_inherited) {
+    entity *impl_ent = get_inherited_methods_implementation(method);
+    assert(impl_ent && "no implementation for inherited entity");
+    if (get_entity_visibility(impl_ent) == visibility_external_allocated) {
+      assert(get_entity_irg(impl_ent) == NULL);
+      *open = true;
+    } else {
+      assert(get_entity_irg(impl_ent) != NULL);
+      if (!eset_contains(set, impl_ent)) {
+       eset_insert(set, impl_ent);
+       ++(*size);
+      }
+    }
+  }
+  /** recursive descend **/
+  for (i = get_entity_n_overwrittenby(method) - 1; i >= 0; --i)
+    collect_impls(get_entity_overwrittenby(method, i), set, size, open);
 }
 
 
@@ -66,77 +141,83 @@ static entity * get_implementation(type * class, entity * method) {
 static entity ** get_impl_methods(entity * method) {
   eset * set = eset_create();
   int size = 0;
-  int i;
   entity ** arr;
   bool open = false;
-  if (get_entity_peculiarity(method) == existent) {
-    if (get_entity_visibility(method) == external_allocated) {
-      assert(get_entity_irg(method) == NULL);
-      open = true;
-    } else {
-      assert(get_entity_irg(method) != NULL);
-      eset_insert(set, method);
-      ++size;
-    }
-  }
-  for (i = get_entity_n_overwrittenby(method) - 1; i >= 0; --i) {
-    entity * ent = get_entity_overwrittenby(method, i);
-    if (get_entity_peculiarity(ent) == existent) {
-      if (get_entity_visibility(ent) == external_allocated) {
-       assert(get_entity_irg(ent) == NULL);
-       open = true;
-      } else {
-       assert(get_entity_irg(ent) != NULL);
-       if (!eset_contains(set, ent)) {
-         eset_insert(set, ent);
-         ++size;
-       }
-      }
-    }
-  }
+
+  /** Collect all method entities that can be called here **/
+  collect_impls(method, set, &size, &open);
+
+/**
+     Vorgaenger einfuegen. **/
   if (size == 0 && !open) {
     /* keine implementierte überschriebene Methode */
     arr = NULL;
   } else if (open) {
     entity * ent;
     arr = NEW_ARR_F(entity *, size + 1);
-    arr[0] = NULL;
-    for (ent = eset_first(set); size > 0; ent = eset_next(set), --size) arr[size] = ent;
+    arr[0] = NULL;  /* Represents open method */
+    for (ent = eset_first(set); size > 0; ent = eset_next(set), --size)
+      arr[size] = ent;
   } else {
     entity * ent;
     arr = NEW_ARR_F(entity *, size);
-    for (size -= 1, ent = eset_first(set); size >= 0; ent = eset_next(set), --size) arr[size] = ent;
+    for (size -= 1, ent = eset_first(set); size >= 0; ent = eset_next(set), --size)
+      arr[size] = ent;
   }
   eset_destroy(set);
   return arr;
 }
 
 
+/* debug makros used in sel_methods_walker */
+#define SIZ(x)    sizeof(x)/sizeof((x)[0])
+
+#define DBG_OPT_NORMALIZE                                                      \
+         __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); \
+     } while(0)
+#define DBG_OPT_POLY                                                           \
+         __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) {
     /* 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) {
+      if (entry != NULL) { /* Method is declared in the compiled code */
        entity * ent = entry->value;
-       if (get_entity_visibility(ent) != external_allocated) {
+       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));
-         exchange(node, new_Const(mode_p, tarval_p_from_entity(ent)));
+         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 (get_irn_op(node) == op_Sel &&
+            is_method_type(get_entity_type(get_Sel_entity(node)))) {
     entity * ent = get_Sel_entity(node);
-    if (get_irn_op(skip_Proj(get_Sel_ptr(node))) == op_Alloc) {
-      ent = get_implementation(get_Alloc_type(skip_Proj(get_Sel_ptr(node))), ent);
-      if (get_entity_visibility(ent) == external_allocated) {
-       exchange(node, new_SymConst((type_or_id_p) get_entity_ld_ident(ent), linkage_ptr_info));
-      } else {
-       exchange(node, new_Const(mode_p, tarval_p_from_entity(ent)));
-      }
+    if (get_optimize() && get_opt_dyn_meth_dispatch() &&
+       (get_irn_op(skip_Proj(get_Sel_ptr(node))) == op_Alloc)) {
+      ir_node *new_node;
+      /* We know which method will be called, no dispatch necessary. */
+      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. */
+      new_node = copy_const_value(get_atomic_ent_value(ent));              DBG_OPT_POLY_ALLOC;
+      exchange (node, new_node);
     } 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
@@ -148,19 +229,41 @@ static void sel_methods_walker(ir_node * node, pmap * ldname_map) {
        /* Die Sel-Operation kann nie einen Zeiger auf eine aufrufbare
         * Methode zurückgeben. Damit ist sie insbesondere nicht
         * ausführbar und nicht erreichbar. */
-       exchange(node, new_Bad());
+       /* 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());
+       }
       } else {
        entity ** arr = get_entity_link(ent);
-       if (ARR_LEN(arr) == 1 && arr[0] != NULL) {
+
+#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");
+#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. */
-         if (get_entity_visibility(arr[0]) == external_allocated) {
-           exchange(node, new_SymConst((type_or_id_p) get_entity_ld_ident(arr[0]), linkage_ptr_info));
-         } else {
-           exchange(node, new_Const(mode_p, tarval_p_from_entity(arr[0])));
-         }
+         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);
        }
       }
     }
@@ -169,7 +272,7 @@ static void sel_methods_walker(ir_node * node, pmap * ldname_map) {
 
 
 /* Datenstruktur initialisieren. Zusätzlich werden alle
- * SymConst-Operationen, die auf interne Methode verweisen, durch
+ * SymConst-Operationen, die auf interne Methoden verweisen, durch
  * Const-Operationen ersetzt. */
 static void sel_methods_init(void) {
   int i;
@@ -180,14 +283,15 @@ static void sel_methods_init(void) {
     entity * ent = get_irg_ent(get_irp_irg(i));
     /* Nur extern sichtbare Methode können überhaupt mit SymConst
      * aufgerufen werden. */
-    if (get_entity_visibility(ent) != local) {
+    if (get_entity_visibility(ent) != visibility_local) {
       pmap_insert(ldname_map, (void *) get_entity_ld_ident(ent), ent);
     }
   }
-  all_irg_walk((irg_walk_func) sel_methods_walker, NULL, ldname_map);
+  all_irg_walk((irg_walk_func *) sel_methods_walker, NULL, ldname_map);
   pmap_destroy(ldname_map);
 }
 
+/*****************************************************************************/
 
 /* Datenstruktur freigeben. */
 static void sel_methods_dispose(void) {
@@ -292,7 +396,7 @@ static void callee_ana_proj(ir_node * node, long n, eset * methods) {
 static void callee_ana_node(ir_node * node, eset * methods) {
   int i;
 
-  assert(get_irn_mode(node) == mode_p);
+  assert(mode_is_reference(get_irn_mode(node)) || is_Bad(node));
   /* rekursion verhindern */
   if (get_irn_link(node) == MARK) {
     /* already visited */
@@ -308,9 +412,9 @@ static void callee_ana_node(ir_node * node, eset * methods) {
 
   case iro_Const: {
     /* interne Methode */
-    entity * ent = get_Const_tarval(node)->u.p.ent;
+    entity * ent = tarval_to_entity(get_Const_tarval(node));
     assert(ent && is_method_type(get_entity_type(ent)));
-    if (get_entity_visibility(ent) != external_allocated) {
+    if (get_entity_visibility(ent) != visibility_external_allocated) {
       assert(get_entity_irg(ent));
       eset_insert(methods, ent);
     } else {
@@ -460,7 +564,7 @@ static void free_mark_proj(ir_node * node, long n, eset * set) {
 
 static void free_mark(ir_node * node, eset * set) {
   int i;
-  assert(get_irn_mode(node) == mode_p);
+  assert(mode_is_reference(get_irn_mode(node)));
   if (get_irn_link(node) == MARK) {
     return; /* already visited */
   }
@@ -480,9 +584,11 @@ static void free_mark(ir_node * node, eset * set) {
     break;
   case iro_Const: {
     tarval * val = get_Const_tarval(node);
-    entity * ent = val->u.p.ent;
-    if (ent != NULL && is_method_type(get_entity_type(ent))) {
-      eset_insert(set, ent);
+    if (tarval_is_entity(val)) { /* filter null pointer */
+      entity * ent = tarval_to_entity(val);
+      if (is_method_type(get_entity_type(ent))) {
+        eset_insert(set, ent);
+      }
     }
     break;
   }
@@ -528,7 +634,7 @@ 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 (get_irn_mode(pred) == mode_p) {
+      if (mode_is_reference(get_irn_mode(pred))) {
        free_mark(pred, set);
       }
     }
@@ -539,7 +645,7 @@ static void free_ana_walker(ir_node * node, eset * set) {
     set_irn_link(node, MARK);
     for (i = get_irn_arity(node) - 1; i >= 0; --i) {
       ir_node * pred = get_irn_n(node, i);
-      if (get_irn_mode(pred) == mode_p) {
+      if (mode_is_reference(get_irn_mode(pred))) {
        free_mark(pred, set);
       }
     }
@@ -563,14 +669,16 @@ static entity ** get_free_methods(void) {
     ir_graph * irg = get_irp_irg(i);
     entity * ent = get_irg_ent(irg);
     /* insert "external visible" methods. */
-    if (get_entity_visibility(ent) != local) {
+    if (get_entity_visibility(ent) != visibility_local) {
       eset_insert(set, ent);
     }
-    irg_walk_graph(irg, NULL, (irg_walk_func) free_ana_walker, set);
+    irg_walk_graph(irg, NULL, (irg_walk_func *) free_ana_walker, set);
   }
   /* Hauptprogramm ist auch frei, auch wenn es nicht "external
    * visible" ist. */
-  eset_insert(set, get_irg_ent(get_irp_main_irg()));
+  if(get_irp_main_irg()) {
+    eset_insert(set, get_irg_ent(get_irp_main_irg()));
+  }
   for (ent = eset_first(set); ent; ent = eset_next(set)) {
     ARR_APP1(entity *, arr, ent);
   }
@@ -595,3 +703,31 @@ void cgana(int *length, entity ***free_methods) {
   for (i = 0; i < (*length); i++) (*free_methods)[i] = free_meths[i];
   DEL_ARR_F(free_meths);
 }
+
+/* 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. */
+void opt_call_addrs(void) {
+  sel_methods_init();
+  sel_methods_dispose();
+#if 0
+  int i;
+  pmap * ldname_map = pmap_create();
+  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));
+    /* Nur extern sichtbare Methoden können überhaupt mit SymConst
+     * aufgerufen werden. */
+    if (get_entity_visibility(ent) != local) {
+      pmap_insert(ldname_map, (void *) get_entity_ld_ident(ent), ent);
+    }
+  }
+  all_irg_walk((irg_walk_func *) sel_methods_walker, NULL, ldname_map);
+  pmap_destroy(ldname_map);
+#endif
+}