some fixes for xml dumper / still buggy.
[libfirm] / ir / ir / irnode.c
index b67c1f7..da0cee2 100644 (file)
@@ -1,8 +1,8 @@
 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
-** All rights reserved.
-**
-** Authors: Martin Trapp, Christian Schaefer, Goetz Lindenmaier
-**
+* All rights reserved.
+*
+* Authors: Martin Trapp, Christian Schaefer, Goetz Lindenmaier
+*
 */
 
 /* $Id$ */
 
 #include "irnode_t.h"
 #include "irgraph_t.h"
-#include "ident_t.h"
 #include "irmode_t.h"
 #include "typegmod_t.h"
 #include "array.h"
+#include "irbackedge_t.h"
+#include "irdump.h"
+#include "irflag.h"
 
 #ifdef DEBUG_libfirm
 #include "irprog_t.h"
 #define RETURN_RESULT_OFFSET 1  /* mem is not a result */
 #define END_KEEPALIVE_OFFSET 0
 
-/* Declarations for INLINEing */
-INLINE ir_node ** get_irn_in (ir_node *node);
-INLINE ir_mode *get_irn_mode (ir_node *node);
-INLINE ir_op *get_irn_op (ir_node *node);
-INLINE opcode get_irn_opcode (ir_node *node);
-INLINE ident *get_irn_opident (ir_node *node);
+/* Declarations for inlineing */
+INLINE ir_node ** get_irn_in (const ir_node *node);
+INLINE ir_mode *get_irn_mode (const ir_node *node);
+INLINE ir_op *get_irn_op (const ir_node *node);
+INLINE opcode get_irn_opcode (const ir_node *node);
+INLINE ident *get_irn_opident (const ir_node *node);
 INLINE type *get_SymConst_type (ir_node *node);
 INLINE ir_node *skip_nop (ir_node *node);
-INLINE ir_node *skip_nop (ir_node *node);
-INLINE int is_Proj (ir_node *node);
+INLINE int is_Proj (const ir_node *node);
 
 
-static char *pnc_name_arr [] = {"False", "Eq", "Lt", "Le",
-                               "Gt", "Ge", "Lg", "Leg", "Uo",
-                               "Ue", "Ul", "Ule", "Ug", "Uge",
-                               "Ne", "True" };
+static const char *pnc_name_arr [] = {
+  "False", "Eq", "Lt", "Le",
+  "Gt", "Ge", "Lg", "Leg", "Uo",
+  "Ue", "Ul", "Ule", "Ug", "Uge",
+  "Ne", "True"
+};
 
-INLINE char *get_pnc_string(int pnc) {
+INLINE const char *get_pnc_string(int pnc) {
   return pnc_name_arr[pnc];
 }
 
@@ -75,10 +78,14 @@ get_negated_pnc(int pnc) {
   return 99; /* to shut up gcc */
 }
 
-static char *pns_name_arr [] = {"initial_exec", "global_store",
-                               "frame_base", "globals", "args"};
+const char *pns_name_arr [] = {
+  "initial_exec", "global_store",
+  "frame_base", "globals", "args"
+};
 
-static char *symconst_name_arr [] = {"type_tag", "size", "linkage_ptr_info"};
+const char *symconst_name_arr [] = {
+  "type_tag", "size", "linkage_ptr_info"
+};
 
 void
 init_irnode (void)
@@ -128,69 +135,20 @@ copy_attrs (ir_node *old, ir_node *new) {
   memcpy (&new->attr, &old->attr, get_op_attr_size(get_irn_op(old)));
 }
 
-/* IR-Nodes with attributes */
-int
-ir_node_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
-{
-  int printed = 0;
-  ir_node *np = XP_GETARG (ir_node *, 0);
-
-  if (!np) {
-    XPS ("<null ir_node>");
-    return printed;
-  }
-
-  XPF1 ("%I", get_irn_opident(np));
-
-  switch (get_irn_opcode (np)) {       /* node label */
-  case iro_Const:
-    XPF1 ("%I", get_irn_mode(np)->name);
-    XPS (" : ");
-    XPF1 ("%v", get_irn_const_attr);
-    break;
-  case iro_Proj:
-    if (get_irn_mode (np) == mode_b) {
-      XPC (" ");
-      XP (pnc_name_arr[get_irn_proj_attr(np)]);
-    } else if (get_irn_opcode (get_irn_in (np)[1]) == iro_Start) {
-      XPC (" ");
-      XP (pns_name_arr[get_irn_proj_attr(np)]);
-    } else {
-      XPF1 ("%I", get_irn_mode(np)->name);
-      XPC (" ");
-      XPF1 ("%d", get_irn_proj_attr(np));
-    }
-    break;
-  case iro_SymConst:
-    XPF1 ("%I", get_irn_mode(np)->name);
-    XPC  (" ");
-    XP   (symconst_name_arr[get_irn_symconst_attr(np).num]);
-    XPF1 (" %#N", get_type_ident(get_SymConst_type(np)));
-    break;
-  case iro_Start:              /* don't dump mode of these */
-  case iro_Cond:
-  case iro_Block:
-  case iro_Call:
-  case iro_Jmp:
-  case iro_Return:
-  case iro_End:
-  case iro_Break:
-  case iro_EndReg:
-  case iro_EndExcept:
-  case iro_CallBegin:
-    break;
-  default:
-    XPF1 ("%I", get_irn_mode(np)->name);
-  }
+/** getting some parameters from ir_nodes **/
 
-  return printed;
+int
+is_ir_node (void *thing) {
+  assert(thing);
+  if (get_kind(thing) == k_ir_node)
+    return 1;
+  else
+    return 0;
 }
 
-/** getting some parameters from ir_nodes **/
-
 /* returns the number of predecessors without the block predecessor. */
 INLINE int
-get_irn_arity (ir_node *node) {
+get_irn_arity (const ir_node *node) {
   assert(node);
   if (interprocedural_view) { /* handle Filter and Block specially */
     if (get_irn_opcode(node) == iro_Filter) {
@@ -211,7 +169,7 @@ get_irn_arity (ir_node *node) {
    lists of operands as predecessors of Block or arguments of a Call are
    consecutive. */
 INLINE ir_node **
-get_irn_in (ir_node *node) {
+get_irn_in (const ir_node *node) {
   assert(node);
   if (interprocedural_view) { /* handle Filter and Block specially */
     if (get_irn_opcode(node) == iro_Filter) {
@@ -246,6 +204,7 @@ set_irn_in (ir_node *node, int arity, ir_node **in) {
     *arr = NEW_ARR_D(ir_node *, current_ir_graph->obst, arity + 1);
     (*arr)[0] = block;
   }
+  fix_backedges(current_ir_graph->obst, node);
   memcpy((*arr) + 1, in, sizeof(ir_node *) * arity);
 }
 
@@ -256,7 +215,12 @@ set_irn_in (ir_node *node, int arity, ir_node **in) {
    If it is a block, the entry -1 is NULL. */
 INLINE ir_node *
 get_irn_n (ir_node *node, int n) {
-  assert(node && -1 <= n && n < get_irn_arity(node));
+  /* debug @@@
+  if (-1 > n || get_irn_arity(node) <= n) {
+    printf("pos: %d, arity: %d ", n, get_irn_arity(node));
+    DDMN(node);
+    } */
+  assert(node); assert(-1 <= n && n < get_irn_arity(node));
   if (interprocedural_view) { /* handle Filter and Block specially */
     if (get_irn_opcode(node) == iro_Filter) {
       assert(node->attr.filter.in_cg);
@@ -294,14 +258,22 @@ set_irn_n (ir_node *node, int n, ir_node *in) {
 }
 
 INLINE ir_mode *
-get_irn_mode (ir_node *node)
+get_irn_mode (const ir_node *node)
 {
   assert (node);
   return node->mode;
 }
 
+INLINE void
+set_irn_mode (ir_node *node, ir_mode *mode)
+{
+  assert (node);
+  node->mode=mode;
+  return;
+}
+
 INLINE modecode
-get_irn_modecode (ir_node *node)
+get_irn_modecode (const ir_node *node)
 {
   assert (node);
   return node->mode->code;
@@ -309,14 +281,14 @@ get_irn_modecode (ir_node *node)
 
 
 INLINE ident *
-get_irn_modeident (ir_node *node)
+get_irn_modeident (const ir_node *node)
 {
   assert(node);
-  return node->mode->name;
+  return get_mode_ident(node->mode);
 }
 
 INLINE ir_op *
-get_irn_op (ir_node *node)
+get_irn_op (const ir_node *node)
 {
   assert (node);
   return node->op;
@@ -331,28 +303,30 @@ set_irn_op (ir_node *node, ir_op *op)
 }
 
 INLINE opcode
-get_irn_opcode (ir_node *node)
+get_irn_opcode (const ir_node *node)
 {
   assert (node);
+  assert (k_ir_node == get_kind(node));
+  assert (node -> op);
   return node->op->code;
 }
 
 INLINE const char *
-get_irn_opname (ir_node *node)
+get_irn_opname (const ir_node *node)
 {
   assert(node);
   return id_to_str(node->op->name);
 }
 
 INLINE ident *
-get_irn_opident (ir_node *node)
+get_irn_opident (const ir_node *node)
 {
   assert(node);
   return node->op->name;
 }
 
 INLINE unsigned long
-get_irn_visited (ir_node *node)
+get_irn_visited (const ir_node *node)
 {
   assert (node);
   return node->visited;
@@ -372,26 +346,36 @@ mark_irn_visited (ir_node *node) {
 }
 
 INLINE int
-irn_not_visited  (ir_node *node) {
+irn_not_visited  (const ir_node *node) {
   assert (node);
   return (node->visited < current_ir_graph->visited);
 }
 
+INLINE int
+irn_visited  (const ir_node *node) {
+  assert (node);
+  return (node->visited >= current_ir_graph->visited);
+}
+
 INLINE void
 set_irn_link (ir_node *node, void *link) {
   assert (node);
+  /* Link field is used for Phi construction and various optimizations
+     in iropt. */
+  assert(get_irg_phase_state(current_ir_graph) != phase_building);
+
   node->link = link;
 }
 
 INLINE void *
-get_irn_link (ir_node *node) {
+get_irn_link (const ir_node *node) {
   assert (node);
   return node->link;
 }
 
 /* Outputs a unique number for this node */
 INLINE long
-get_irn_node_nr(ir_node *node) {
+get_irn_node_nr(const ir_node *node) {
   assert(node);
 #ifdef DEBUG_libfirm
   return node->node_nr;
@@ -499,6 +483,13 @@ get_Block_n_cfgpreds (ir_node *node) {
 INLINE ir_node *
 get_Block_cfgpred (ir_node *node, int pos) {
   assert (node->op == op_Block);
+  /* debug @@@
+  if (-1 > pos || get_irn_arity(node) <= pos) {
+    dump_ir_block_graph(current_ir_graph);
+    printf("pos: %d, arity: %d ", pos, get_irn_arity(node));
+    DDMN(node);
+    } */
+  assert(node); assert(-1 <= pos && pos < get_irn_arity(node));
   return get_irn_n(node, pos);
 }
 
@@ -532,12 +523,14 @@ set_Block_block_visited (ir_node *node, unsigned long visit) {
 }
 
 /* For this current_ir_graph must be set. */
-INLINE void mark_Block_block_visited (ir_node *node) {
+INLINE void
+mark_Block_block_visited (ir_node *node) {
   assert (node->op == op_Block);
   node->attr.block.block_visited = get_irg_block_visited(current_ir_graph);
 }
 
-INLINE int Block_not_block_visited(ir_node *node) {
+INLINE int
+Block_not_block_visited(ir_node *node) {
   assert (node->op == op_Block);
   return (node->attr.block.block_visited < get_irg_block_visited(current_ir_graph));
 }
@@ -555,55 +548,48 @@ set_Block_graph_arr (ir_node *node, int pos, ir_node *value) {
 }
 
 /* handler handling for Blocks */
-void set_Block_handler (ir_node *block, ir_node *handler)
-{
+void
+set_Block_handler (ir_node *block, ir_node *handler)  {
   assert ((block->op == op_Block));
   assert ((handler->op == op_Block));
-
   block->attr.block.handler_entry = handler;
 }
 
-ir_node *get_Block_handler (ir_node *block)
-{
+ir_node *
+get_Block_handler (ir_node *block) {
   assert ((block->op == op_Block));
-
   return (block->attr.block.handler_entry);
 }
 
 /* handler handling for Nodes */
-void set_Node_handler (ir_node *node, ir_node *handler)
-{
+void
+set_Node_handler (ir_node *node, ir_node *handler) {
   set_Block_handler (get_nodes_Block (node), handler);
 }
 
-ir_node *get_Node_handler (ir_node *node)
-{
+ir_node *
+get_Node_handler (ir_node *node) {
   return (get_Block_handler (get_nodes_Block (node)));
 }
 
-
 /* exc_t handling for Blocks */
-void set_Block_exc (ir_node *block, exc_t exc)
-{
+void set_Block_exc (ir_node *block, exc_t exc) {
   assert ((block->op == op_Block));
   block->attr.block.exc = exc;
 }
 
-exc_t get_Block_exc (ir_node *block)
-{
+exc_t get_Block_exc (ir_node *block) {
   assert ((block->op == op_Block));
 
   return (block->attr.block.exc);
 }
 
 /* exc_t handling for Nodes */
-void set_Node_exc (ir_node *node, exc_t exc)
-{
+void set_Node_exc (ir_node *node, exc_t exc) {
   set_Block_exc (get_nodes_Block (node), exc);
 }
 
-exc_t get_Node_exc (ir_node *node)
-{
+exc_t get_Node_exc (ir_node *node) {
   return (get_Block_exc (get_nodes_Block (node)));
 }
 
@@ -612,6 +598,15 @@ void set_Block_cg_cfgpred_arr(ir_node * node, int arity, ir_node ** in) {
   if (node->attr.block.in_cg == NULL || arity != ARR_LEN(node->attr.block.in_cg) - 1) {
     node->attr.block.in_cg = NEW_ARR_D(ir_node *, current_ir_graph->obst, arity + 1);
     node->attr.block.in_cg[0] = NULL;
+    node->attr.block.cg_backedge = new_backedge_arr(current_ir_graph->obst, arity);
+    {
+      /* Fix backedge array.  fix_backedges operates depending on
+        interprocedural_view. */
+      bool ipv = interprocedural_view;
+      interprocedural_view = true;
+      fix_backedges(current_ir_graph->obst, node);
+      interprocedural_view = ipv;
+    }
   }
   memcpy(node->attr.block.in_cg + 1, in, sizeof(ir_node *) * arity);
 }
@@ -629,8 +624,13 @@ ir_node ** get_Block_cg_cfgpred_arr(ir_node * node) {
 }
 
 int get_Block_cg_n_cfgpreds(ir_node * node) {
+  assert(node->op == op_Block);
+  return node->attr.block.in_cg == NULL ? 0 : ARR_LEN(node->attr.block.in_cg) - 1;
+}
+
+ir_node * get_Block_cg_cfgpred(ir_node * node, int pos) {
   assert(node->op == op_Block && node->attr.block.in_cg);
-  return ARR_LEN(node->attr.block.in_cg) - 1;
+  return node->attr.block.in_cg[pos + 1];
 }
 
 void remove_Block_cg_cfgpred_arr(ir_node * node) {
@@ -670,12 +670,12 @@ free_End (ir_node *end) {
                       in array afterwards ... */
 }
 
-ir_graph *get_EndReg_irg (ir_node *end) {
+ir_graph *get_EndReg_irg (const ir_node *end) {
   assert (end->op == op_EndReg);
   return end->attr.end.irg;
 }
 
-ir_graph *get_EndExcept_irg  (ir_node *end) {
+ir_graph *get_EndExcept_irg  (const ir_node *end) {
   assert (end->op == op_EndReg);
   return end->attr.end.irg;
 }
@@ -819,7 +819,7 @@ set_Const_tarval (ir_node *node, tarval *con) {
 }
 
 INLINE symconst_kind
-get_SymConst_kind (ir_node *node) {
+get_SymConst_kind (const ir_node *node) {
   assert (node->op == op_SymConst);
   return node->attr.i.num;
 }
@@ -839,11 +839,11 @@ get_SymConst_type (ir_node *node) {
 }
 
 INLINE void
-set_SymConst_type (ir_node *node, type *type) {
+set_SymConst_type (ir_node *node, type *tp) {
   assert (   (node->op == op_SymConst)
           && (   get_SymConst_kind(node) == type_tag
               || get_SymConst_kind(node) == size));
-  node->attr.i.tori.typ = type;
+  node->attr.i.tori.typ = tp;
 }
 
 INLINE ident *
@@ -1048,10 +1048,10 @@ get_Call_type (ir_node *node) {
 }
 
 INLINE void
-set_Call_type (ir_node *node, type *type) {
+set_Call_type (ir_node *node, type *tp) {
   assert (node->op == op_Call);
-  assert (is_method_type(type));
-  node->attr.call.cld_tp = type;
+  assert (is_method_type(tp));
+  node->attr.call.cld_tp = tp;
 }
 
 int get_Call_n_callees(ir_node * node) {
@@ -1893,9 +1893,9 @@ get_Alloc_type (ir_node *node) {
 }
 
 INLINE void
-set_Alloc_type (ir_node *node, type *type) {
+set_Alloc_type (ir_node *node, type *tp) {
   assert (node->op == op_Alloc);
-  node->attr.a.type = type;
+  node->attr.a.type = tp;
 }
 
 INLINE where_alloc
@@ -1954,9 +1954,9 @@ get_Free_type (ir_node *node) {
 }
 
 INLINE void
-set_Free_type (ir_node *node, type *type) {
+set_Free_type (ir_node *node, type *tp) {
   assert (node->op == op_Free);
-  node->attr.f = type;
+  node->attr.f = tp;
 }
 
 INLINE ir_node **
@@ -2084,29 +2084,48 @@ set_Filter_proj (ir_node *node, long proj) {
   node->attr.filter.proj = proj;
 }
 
+/* Don't use get_irn_arity, get_irn_n in implementation as access
+   shall work independent of view!!! */
 void set_Filter_cg_pred_arr(ir_node * node, int arity, ir_node ** in) {
   assert(node->op == op_Filter);
   if (node->attr.filter.in_cg == NULL || arity != ARR_LEN(node->attr.filter.in_cg) - 1) {
     node->attr.filter.in_cg = NEW_ARR_D(ir_node *, current_ir_graph->obst, arity + 1);
+    node->attr.filter.backedge = NEW_ARR_D (int, current_ir_graph->obst, arity);
+    memset(node->attr.filter.backedge, 0, sizeof(int) * arity);
     node->attr.filter.in_cg[0] = node->in[0];
   }
   memcpy(node->attr.filter.in_cg + 1, in, sizeof(ir_node *) * arity);
 }
 
 void set_Filter_cg_pred(ir_node * node, int pos, ir_node * pred) {
-  assert(node->op == op_Filter && node->attr.filter.in_cg && 0 <= pos && pos < ARR_LEN(node->attr.filter.in_cg) - 1);
+  assert(node->op == op_Filter && node->attr.filter.in_cg &&
+        0 <= pos && pos < ARR_LEN(node->attr.filter.in_cg) - 1);
   node->attr.filter.in_cg[pos + 1] = pred;
 }
+int get_Filter_n_cg_preds(ir_node *node) {
+  assert(node->op == op_Filter && node->attr.filter.in_cg);
+  return (ARR_LEN(node->attr.filter.in_cg) - 1);
+}
+ir_node *get_Filter_cg_pred(ir_node *node, int pos) {
+  int arity;
+  assert(node->op == op_Filter && node->attr.filter.in_cg &&
+        0 <= pos);
+  arity = ARR_LEN(node->attr.filter.in_cg);
+  assert(pos <  arity - 1);
+  return node->attr.filter.in_cg[pos + 1];
+}
 
 
 INLINE ir_graph *
 get_irn_irg(ir_node *node) {
   if (get_irn_op(node) == op_CallBegin) {
     return node->attr.callbegin.irg;
-  } else if (get_irn_op(node) == op_EndReg || get_irn_op(node) == op_EndExcept) {
+  } else if (get_irn_op(node) == op_EndReg ||
+            get_irn_op(node) == op_EndExcept) {
     return node->attr.end.irg;
   } else {
     assert(0 && "no irg attr");
+    return NULL;
   }
 }
 
@@ -2129,6 +2148,8 @@ INLINE ir_node *
 skip_Tuple (ir_node *node) {
   ir_node *pred;
 
+  if (!get_opt_normalize()) return node;
+
   node = skip_nop(node);
   if (get_irn_op(node) == op_Proj) {
     pred = skip_nop(get_Proj_pred(node));
@@ -2140,15 +2161,29 @@ skip_Tuple (ir_node *node) {
   return node;
 }
 
+/* This should compact Id-cycles to self-cycles. It has the same (or less?) complexity
+   than any other approach, as Id chains are resolved and all point to the real node, or
+   all id's are self loops. */
 INLINE ir_node *
 skip_nop (ir_node *node) {
   /* don't assert node !!! */
 
-  if (node && (node->op == op_Id) && (node != get_Id_pred(node))) {
-    /* Don't use get_Id_pred:  We get into an endless loop for
-       self-referencing Ids. */
+  if (!get_opt_normalize()) return node;
+
+  /* Don't use get_Id_pred:  We get into an endless loop for
+     self-referencing Ids. */
+  if (node && (node->op == op_Id) && (node != node->in[0+1])) {
+    ir_node *rem_pred = node->in[0+1];
+    ir_node *res;
+
     assert (get_irn_arity (node) > 0);
-    return node->in[0+1];
+
+    node->in[0+1] = node;
+    res = skip_nop(rem_pred);
+    if (res->op == op_Id) /* self-loop */ return node;
+
+    node->in[0+1] = res;
+    return res;
   } else {
     return node;
   }
@@ -2174,7 +2209,13 @@ is_no_Block (ir_node *node) {
 }
 
 INLINE int
-is_Proj (ir_node *node) {
+is_Block (ir_node *node) {
+  assert(node);
+  return (get_irn_opcode(node) == iro_Block);
+}
+
+INLINE int
+is_Proj (const ir_node *node) {
   assert(node);
   return node->op == op_Proj
     || (!interprocedural_view && node->op == op_Filter);
@@ -2186,6 +2227,26 @@ is_cfop(ir_node *node) {
   return is_cfopcode(get_irn_op(node));
 }
 
+/* Returns true if the operation manipulates interprocedural control flow:
+   CallBegin, EndReg, EndExcept */
+INLINE int is_ip_cfop(ir_node *node) {
+  return is_ip_cfopcode(get_irn_op(node));
+}
+
+ir_graph *get_ip_cfop_irg(ir_node *n) {
+  switch (get_irn_opcode(n)) {
+  case iro_EndReg:
+    return get_EndReg_irg(n);
+  case iro_EndExcept:
+    return get_EndExcept_irg(n);
+  case iro_CallBegin:
+    return get_CallBegin_irg(n);
+  default:
+    assert(is_ip_cfop(n));
+  }
+  return NULL; /* should never be reached */
+}
+
 /* Returns true if the operation can change the control flow because
    of an exception. */
 int