fixed DBG_OPT_RAW() call
[libfirm] / ir / opt / ldstopt.c
index 4070a87..d535879 100644 (file)
@@ -307,18 +307,22 @@ static long get_Sel_array_index_long(ir_node *n, int dim) {
   return get_tarval_long(get_Const_tarval(index));
 }
 
-compound_graph_path *rec_get_accessed_path(ir_node *ptr, int depth) {
-  compound_graph_path *res;
+static compound_graph_path *rec_get_accessed_path(ir_node *ptr, int depth) {
+  compound_graph_path *res = NULL;
+  entity              *root, *field;
+  int                 path_len, pos;
+
   if (get_irn_op(ptr) == op_SymConst) {
     assert(get_SymConst_kind(ptr) == symconst_addr_ent);
-    entity *root = get_SymConst_entity(ptr);
-    res = new_compound_graph_path(get_entity_type(root), depth);
-  } else {
+    root = get_SymConst_entity(ptr);
+    res = (depth == 0) ? NULL : new_compound_graph_path(get_entity_type(root), depth);
+  }
+  else {
     assert(get_irn_op(ptr) == op_Sel);
     res = rec_get_accessed_path(get_Sel_ptr(ptr), depth+1);
-    entity *field = get_Sel_entity(ptr);
-    int path_len = get_compound_graph_path_length(res);
-    int pos = path_len - depth -1;
+    field = get_Sel_entity(ptr);
+    path_len = get_compound_graph_path_length(res);
+    pos = path_len - depth - 1;
     set_compound_graph_path_node(res, pos, field);
     if (is_Array_type(get_entity_owner(field))) {
       assert(get_Sel_n_indexs(ptr) == 1 && "multi dim arrays not implemented");
@@ -328,11 +332,13 @@ compound_graph_path *rec_get_accessed_path(ir_node *ptr, int depth) {
   return res;
 }
 
-compound_graph_path *get_accessed_path(ir_node *ptr) {
+
+/** Returns an access path or NULL.  The access path is only
+ *  valid, if the graph is in phase_high and _no_ address computation is used. */
+static compound_graph_path *get_accessed_path(ir_node *ptr) {
   return rec_get_accessed_path(ptr, 0);
 }
 
-
 /**
  * optimize a Load
  */
@@ -340,7 +346,7 @@ static int optimize_load(ir_node *load)
 {
   ldst_info_t *info = get_irn_link(load);
   ir_mode *load_mode = get_Load_mode(load);
-  ir_node *pred, *mem, *ptr;
+  ir_node *pred, *mem, *ptr, *new_node;
   entity *ent;
   int res = 0;
 
@@ -359,7 +365,7 @@ static int optimize_load(ir_node *load)
     if (get_irn_op(ptr) == op_Sel) {
       ir_node *mem = get_Sel_mem(ptr);
 
-      if (get_irn_op(mem) == op_Alloc) {
+      if (get_irn_op(skip_Proj(mem)) == op_Alloc) {
         /* ok, check the types */
         entity *ent  = get_Sel_entity(ptr);
         type *s_type = get_entity_type(ent);
@@ -374,7 +380,8 @@ static int optimize_load(ir_node *load)
         }
       }
     }
-    else if (get_irn_op(ptr) == op_Alloc) {
+    else if ((get_irn_op(skip_Proj(ptr)) == op_Alloc) ||
+            ((get_irn_op(ptr) == op_Cast) && (get_irn_op(skip_Proj(get_Cast_op(ptr))) == op_Alloc))) {
       /* simple case: a direct load after an Alloc. Firm Alloc throw
        * an exception in case of out-of-memory. So, there is no way for an
        * exception in this load.
@@ -402,7 +409,7 @@ static int optimize_load(ir_node *load)
 
   /* Load from a constant polymorphic field, where we can resolve
      polymorphy. */
-  ir_node *new_node = transform_node_Load(load);
+  new_node = transform_node_Load(load);
   if (new_node != load) {
     if (info->projs[pn_Load_M]) {
       exchange(info->projs[pn_Load_M], mem);
@@ -432,10 +439,11 @@ static int optimize_load(ir_node *load)
       }
 
       if (variability_constant == get_entity_variability(ent)
-         && is_atomic_entity(ent)) {   /* Might not be atomic after
-                                        lowering of Sels.  In this
-                                        case we could also load, but
-                                        it's more complicated. */
+               && is_atomic_entity(ent)) {
+        /* Might not be atomic after
+           lowering of Sels.  In this
+           case we could also load, but
+           it's more complicated. */
         /* more simpler case: we load the content of a constant value:
          * replace it by the constant itself
          */
@@ -457,40 +465,56 @@ static int optimize_load(ir_node *load)
         }
       }
       else if (variability_constant == get_entity_variability(ent)) {
+        compound_graph_path *path;
+       /*
         printf(">>>>>>>>>>>>> Found access to constant entity %s in function %s\n", get_entity_name(ent),
-              get_entity_name(get_irg_entity(current_ir_graph)));
-       printf("  load: "); DDMN(load);
-       printf("  ptr:  "); DDMN(ptr);
+        get_entity_name(get_irg_entity(current_ir_graph)));
+        printf("  load: "); DDMN(load);
+        printf("  ptr:  "); DDMN(ptr);
+       */
+        path = get_accessed_path(ptr);
+        if (path) {
+          ir_node *c;
 
-       compound_graph_path *path = get_accessed_path(ptr);
-       assert(is_proper_compound_graph_path(path, get_compound_graph_path_length(path)-1));
-       ir_node *c = get_compound_ent_value_by_path(ent, path);
+          assert(is_proper_compound_graph_path(path, get_compound_graph_path_length(path)-1));
+          c = get_compound_ent_value_by_path(ent, path);
 
-       printf("  cons: "); DDMN(c);
+          /* printf("  cons: "); DDMN(c); */
 
-        if (info->projs[pn_Load_M])
-          exchange(info->projs[pn_Load_M], mem);
-        if (info->projs[pn_Load_res])
-         exchange(info->projs[pn_Load_res], copy_const_value(c));
-
-       {
-       int j;
-               for (j = 0; j < get_compound_graph_path_length(path); ++j) {
-                 entity *node = get_compound_graph_path_node(path, j);
-                 fprintf(stdout, ".%s", get_entity_name(node));
-                 if (is_Array_type(get_entity_owner(node)))
-                   fprintf(stdout, "[%d]", get_compound_graph_path_array_index(path, j));
-               }
-               printf("\n");
+          if (info->projs[pn_Load_M])
+            exchange(info->projs[pn_Load_M], mem);
+          if (info->projs[pn_Load_res])
+            exchange(info->projs[pn_Load_res], copy_const_value(c));
+          /*
+          {
+            int j;
+            for (j = 0; j < get_compound_graph_path_length(path); ++j) {
+              entity *node = get_compound_graph_path_node(path, j);
+              fprintf(stdout, ".%s", get_entity_name(node));
+              if (is_Array_type(get_entity_owner(node)))
+                      fprintf(stdout, "[%d]", get_compound_graph_path_array_index(path, j));
+            }
+            printf("\n");
+          }
+         */
+        } else {
+         /*  We can not determine a correct access path.  E.g., in jack, we load
+             a byte from an object to generate an exception.   Happens in test program
+             Reflectiontest.
+         printf(">>>>>>>>>>>>> Found access to constant entity %s in function %s\n", get_entity_name(ent),
+                get_entity_name(get_irg_entity(current_ir_graph)));
+         printf("  load: "); DDMN(load);
+         printf("  ptr:  "); DDMN(ptr);
+         if (get_irn_op(ptr) == op_SymConst &&
+               get_SymConst_kind(ptr) == symconst_addr_ent) { printf("        "); DDMEO(get_SymConst_entity(ptr)); }
+          printf("cannot optimize.\n");
+         */
        }
-
       }
 
       /* we changed the irg, but try further */
       res = 1;
     }
-
-
   }
 
   /* Check, if the address of this load is used more than once.
@@ -525,8 +549,10 @@ static int optimize_load(ir_node *load)
        */
       if ((!pred_info->projs[pn_Store_X_except] && !info->projs[pn_Load_X_except]) ||
           get_nodes_block(load) == get_nodes_block(pred)) {
-        DBG_OPT_RAW(load, pred);
-        exchange( info->projs[pn_Load_res], get_Store_value(pred) );
+        ir_node *value = get_Store_value(pred);
+
+        DBG_OPT_RAW(load, value);
+        exchange(info->projs[pn_Load_res], value);
 
         if (info->projs[pn_Load_M])
           exchange(info->projs[pn_Load_M], mem);