fixed output
[libfirm] / ir / opt / opt_polymorphy.c
index 48e8ac8..a1d1f22 100644 (file)
@@ -2,12 +2,15 @@
  * Project:     libFIRM
  * File name:   ir/opt/opt_polymorphy
  * Purpose:     Optimize polymorphic Sel nodes.
- * Author:
+ * Author:      Goetz Lindenmaier, Michael Beck
  * Created:
  * CVS-ID:      $Id$
  * Copyright:   (c) 2005 Universität Karlsruhe
  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #include "irprog_t.h"
 #include "entity_t.h"
 #include "iropt_dbg.h"
 #include "irflag_t.h"
 
+/**
+ * Checks if a graph allocates new memory and returns the
+ * type of the newly allocated entity.
+ * Returns NULL if the graph did not represent an Allocation.
+ *
+ * The default implementation hecks for Alloc nodes only.
+ */
+ir_type *default_firm_get_Alloc(ir_node *n) {
+  n = skip_Proj(n);
+  if (get_irn_op(n) == op_Alloc) {
+    return get_Alloc_type(n);
+  }
+  return NULL;
+}
+
+typedef ir_type *(*get_Alloc_func)(ir_node *n);
+
+/** The get_Alloc function */
+static get_Alloc_func firm_get_Alloc = default_firm_get_Alloc;
+
+/** Set a new get_Alloc_func and returns the old one. */
+get_Alloc_func firm_set_Alloc_func(get_Alloc_func newf) {
+  get_Alloc_func old = firm_get_Alloc;
+  firm_get_Alloc = newf;
+  return old;
+}
+
 /** Return dynamic type of ptr.
  *
  * If we can deduct the dynamic type from the firm nodes
  * If we find a dynamic type this means that the pointer always points
  * to an object of this type during runtime.   We resolved polymorphy.
  */
-static type *get_dynamic_type(ir_node *ptr) {
-  ptr = skip_Cast(skip_Proj(ptr));
-  if (get_irn_op(ptr) == op_Alloc)
-    return get_Alloc_type(ptr);
-  return firm_unknown_type;
+static ir_type *get_dynamic_type(ir_node *ptr) {
+  ir_type *tp;
+
+  /* skip Cast and Confirm nodes */
+  for (;;) {
+    opcode code = get_irn_opcode(ptr);
+
+    switch (code) {
+    case iro_Cast:
+      ptr = get_Cast_op(ptr);
+      continue;
+    case iro_Confirm:
+      ptr = get_Confirm_value(ptr);
+      continue;
+    default:
+      ;
+    }
+    break;
+  }
+  tp = (*firm_get_Alloc)(ptr);
+  return tp ? tp : firm_unknown_type;
+}
+
+/**
+ * Check, if a entity is final, i.e. is not anymore overridden.
+ */
+static int is_final_ent(entity *ent) {
+  if (get_entity_final(ent)) {
+    /* not possible to override this entity. */
+    return 1;
+  }
+  if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
+    /* we have a closed world, so simply check how often it was
+       overridden. */
+    return 1;
+  }
+  return 0;
 }
 
 /*
- * Transform  Sel(Alloc)[method]
- * to SymC[method]
+ * Transform Sel[method] to SymC[method] if possible.
  */
 ir_node *transform_node_Sel(ir_node *node)
 {
   ir_node *new_node, *ptr;
-  type    *dyn_tp;
+  ir_type *dyn_tp;
   entity  *ent = get_Sel_entity(node);
 
   if (get_irp_phase_state() == phase_building) return node;
@@ -55,7 +116,7 @@ ir_node *transform_node_Sel(ir_node *node)
 
   /* If the entity is a leave in the inheritance tree,
      we can replace the Sel by a constant. */
-  if (get_entity_n_overwrittenby(ent) == 0) {
+  if (is_final_ent(ent)) {
     /* In dead code, we might call a leave entity that is a description.
        Do not turn the Sel to a SymConst. */
     if (get_entity_peculiarity(ent) == peculiarity_description) {
@@ -64,11 +125,10 @@ ir_node *transform_node_Sel(ir_node *node)
     } else {
       ir_node *rem_block = get_cur_block();
       set_cur_block(get_nodes_block(node));
-      new_node = copy_const_value(get_atomic_ent_value(ent));
+      new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
       set_cur_block(rem_block);
-      DBG_OPT_POLY_ALLOC(node, new_node);
+      DBG_OPT_POLY(node, new_node);
     }
-
     return new_node;
   }
 
@@ -87,9 +147,9 @@ ir_node *transform_node_Sel(ir_node *node)
 
     rem_block = get_cur_block();
     set_cur_block(get_nodes_block(node));
-    new_node = copy_const_value(get_atomic_ent_value(called_ent));
+    new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
     set_cur_block(rem_block);
-    DBG_OPT_POLY_ALLOC(node, new_node);
+    DBG_OPT_POLY(node, new_node);
 
     return new_node;
   }
@@ -109,14 +169,14 @@ ir_node *transform_node_Load(ir_node *n)
 {
   ir_node *field_ptr, *new_node, *ptr;
   entity  *ent;
-  type    *dyn_tp;
+  ir_type *dyn_tp;
 
   if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
     return n;
 
   field_ptr = get_Load_ptr(n);
 
-  if (get_irn_op(field_ptr) != op_Sel) return n;
+  if (! is_Sel(field_ptr)) return n;
 
   ent = get_Sel_entity(field_ptr);
   if ((get_entity_allocation(ent) != allocation_static)    ||
@@ -125,9 +185,9 @@ ir_node *transform_node_Load(ir_node *n)
 
   /* If the entity is a leave in the inheritance tree,
      we can replace the Sel by a constant. */
-  if ((get_irp_phase_state() != phase_building) && (get_entity_n_overwrittenby(ent) == 0)) {
-    new_node = copy_const_value(get_atomic_ent_value(ent));
-    DBG_OPT_POLY_ALLOC(field_ptr, new_node);
+  if ((get_irp_phase_state() != phase_building) && is_final_ent(ent)) {
+    new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(ent));
+    DBG_OPT_POLY(field_ptr, new_node);
 
     return new_node;
   }
@@ -144,8 +204,8 @@ ir_node *transform_node_Load(ir_node *n)
     /* called_ent may not be description: has no Address/Const to Call! */
     assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
 
-    new_node = copy_const_value(get_atomic_ent_value(loaded_ent));
-    DBG_OPT_POLY_ALLOC(field_ptr, new_node);
+    new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(loaded_ent));
+    DBG_OPT_POLY(field_ptr, new_node);
 
     return new_node;
   }