remove Abs node, backends can match the abs patterns themselfes
[libfirm] / ir / ir / ircons.c
index 4fa0efd..6d7a3fc 100644 (file)
@@ -32,7 +32,7 @@
 #include "irnode_t.h"
 #include "irmode_t.h"
 #include "ircons_t.h"
-#include "irvrfy.h"
+#include "irverify.h"
 #include "irop_t.h"
 #include "iropt_t.h"
 #include "irgmod.h"
@@ -46,9 +46,9 @@
 
 /* when we need verifying */
 #ifdef NDEBUG
-# define IRN_VRFY_IRG(res, irg)
+# define IRN_VERIFY_IRG(res, irg)
 #else
-# define IRN_VRFY_IRG(res, irg)  irn_vrfy_irg(res, irg)
+# define IRN_VERIFY_IRG(res, irg)  irn_verify_irg(res, irg)
 #endif /* NDEBUG */
 
 /**
  */
 static uninitialized_local_variable_func_t *default_initialize_local_variable = NULL;
 
-/* creates a bd constructor for a binop */
-#define NEW_BD_BINOP(instr)                                     \
-static ir_node *                                                \
-new_bd_##instr(dbg_info *db, ir_node *block,                    \
-       ir_node *op1, ir_node *op2, ir_mode *mode)               \
-{                                                               \
-  ir_node  *in[2];                                              \
-  ir_node  *res;                                                \
-  ir_graph *irg = current_ir_graph;                             \
-  in[0] = op1;                                                  \
-  in[1] = op2;                                                  \
-  res = new_ir_node(db, irg, block, op_##instr, mode, 2, in);   \
-  res = optimize_node(res);                                     \
-  IRN_VRFY_IRG(res, irg);                                       \
-  return res;                                                   \
-}
-
-/* creates a bd constructor for an unop */
-#define NEW_BD_UNOP(instr)                                      \
-static ir_node *                                                \
-new_bd_##instr(dbg_info *db, ir_node *block,                    \
-              ir_node *op, ir_mode *mode)                       \
-{                                                               \
-  ir_node  *res;                                                \
-  ir_graph *irg = current_ir_graph;                             \
-  res = new_ir_node(db, irg, block, op_##instr, mode, 1, &op);  \
-  res = optimize_node(res);                                     \
-  IRN_VRFY_IRG(res, irg);                                       \
-  return res;                                                   \
-}
-
-/* creates a bd constructor for an divop */
-#define NEW_BD_DIVOP(instr)                                     \
-static ir_node *                                                \
-new_bd_##instr(dbg_info *db, ir_node *block,                    \
-            ir_node *memop, ir_node *op1, ir_node *op2, ir_mode *mode, op_pin_state state) \
-{                                                               \
-  ir_node  *in[3];                                              \
-  ir_node  *res;                                                \
-  ir_graph *irg = current_ir_graph;                             \
-  in[0] = memop;                                                \
-  in[1] = op1;                                                  \
-  in[2] = op2;                                                  \
-  res = new_ir_node(db, irg, block, op_##instr, mode_T, 3, in); \
-  res->attr.divmod.exc.pin_state = state;                       \
-  res->attr.divmod.resmode = mode;                              \
-  res->attr.divmod.no_remainder = 0;                            \
-  res = optimize_node(res);                                     \
-  IRN_VRFY_IRG(res, irg);                                       \
-  return res;                                                   \
-}
-
-/* creates a rd constructor for a binop */
-#define NEW_RD_BINOP(instr)                                     \
-ir_node *                                                       \
-new_rd_##instr(dbg_info *db, ir_graph *irg, ir_node *block,     \
-       ir_node *op1, ir_node *op2, ir_mode *mode)               \
-{                                                               \
-  ir_node  *res;                                                \
-  ir_graph *rem = current_ir_graph;                             \
-  current_ir_graph = irg;                                       \
-  res = new_bd_##instr(db, block, op1, op2, mode);              \
-  current_ir_graph = rem;                                       \
-  return res;                                                   \
-}
-
-/* creates a rd constructor for an unop */
-#define NEW_RD_UNOP(instr)                                      \
-ir_node *                                                       \
-new_rd_##instr(dbg_info *db, ir_graph *irg, ir_node *block,     \
-              ir_node *op, ir_mode *mode)                       \
-{                                                               \
-  ir_node  *res;                                                \
-  ir_graph *rem = current_ir_graph;                             \
-  current_ir_graph = irg;                                       \
-  res = new_bd_##instr(db, block, op, mode);                    \
-  current_ir_graph = rem;                                       \
-  return res;                                                   \
-}
-
-/* creates a rd constructor for an divop */
-#define NEW_RD_DIVOP(instr)                                     \
-ir_node *                                                       \
-new_rd_##instr(dbg_info *db, ir_graph *irg, ir_node *block,     \
-            ir_node *memop, ir_node *op1, ir_node *op2, ir_mode *mode, op_pin_state state) \
-{                                                               \
-  ir_node  *res;                                                \
-  ir_graph *rem = current_ir_graph;                             \
-  current_ir_graph = irg;                                       \
-  res = new_bd_##instr(db, block, memop, op1, op2, mode, state);\
-  current_ir_graph = rem;                                       \
-  return res;                                                   \
-}
-
-/* creates a d constructor for an binop */
-#define NEW_D_BINOP(instr)                                                    \
-ir_node *                                                                     \
-new_d_##instr(dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {      \
-  return new_bd_##instr(db, current_ir_graph->current_block, op1, op2, mode); \
-}
-
-/* creates a d constructor for an unop */
-#define NEW_D_UNOP(instr)                                                     \
-ir_node *                                                                     \
-new_d_##instr(dbg_info *db, ir_node *op, ir_mode *mode) {                     \
-  return new_bd_##instr(db, current_ir_graph->current_block, op, mode);       \
-}
-
 #include "gen_ir_cons.c.inl"
 
 static ir_node *new_bd_Start(dbg_info *db, ir_node *block)
 {
        ir_node  *res;
-       ir_graph *irg = current_ir_graph;
+       ir_graph *irg = get_irn_irg(block);
 
        res = new_ir_node(db, irg, block, op_Start, mode_T, 0, NULL);
 
-       IRN_VRFY_IRG(res, irg);
+       IRN_VERIFY_IRG(res, irg);
        return res;
 }  /* new_bd_Start */
 
 static ir_node *new_bd_End(dbg_info *db, ir_node *block)
 {
        ir_node  *res;
-       ir_graph *irg = current_ir_graph;
+       ir_graph *irg = get_irn_irg(block);
 
        res = new_ir_node(db, irg, block, op_End, mode_X, -1, NULL);
 
-       IRN_VRFY_IRG(res, irg);
+       IRN_VERIFY_IRG(res, irg);
        return res;
 }  /* new_bd_End */
 
@@ -195,7 +87,7 @@ static ir_node *new_bd_End(dbg_info *db, ir_node *block)
 static ir_node *new_bd_Phi(dbg_info *db, ir_node *block, int arity, ir_node **in, ir_mode *mode)
 {
        ir_node  *res;
-       ir_graph *irg = current_ir_graph;
+       ir_graph *irg = get_irn_irg(block);
        int i;
        int has_unknown = 0;
 
@@ -215,7 +107,7 @@ static ir_node *new_bd_Phi(dbg_info *db, ir_node *block, int arity, ir_node **in
                }
 
        if (!has_unknown) res = optimize_node(res);
-       IRN_VRFY_IRG(res, irg);
+       IRN_VERIFY_IRG(res, irg);
 
        /* Memory Phis in endless loops must be kept alive.
           As we can't distinguish these easily we keep all of them alive. */
@@ -234,7 +126,7 @@ static ir_node *new_bd_Const_type(dbg_info *db, tarval *con, ir_type *tp)
        set_Const_type(res, tp);  /* Call method because of complex assertion. */
        res = optimize_node (res);
        assert(get_Const_type(res) == tp);
-       IRN_VRFY_IRG(res, irg);
+       IRN_VERIFY_IRG(res, irg);
 
        return res;
 }  /* new_bd_Const_type */
@@ -270,7 +162,7 @@ static ir_node *new_bd_Sel(dbg_info *db, ir_node *block, ir_node *store,
        ir_node  **r_in;
        ir_node  *res;
        int      r_arity;
-       ir_graph *irg = current_ir_graph;
+       ir_graph *irg  = get_irn_irg(block);
        ir_mode  *mode = is_Method_type(get_entity_type(ent)) ? mode_P_code : mode_P_data;
 
        assert(ent != NULL && is_entity(ent) && "entity expected in Sel construction");
@@ -286,7 +178,7 @@ static ir_node *new_bd_Sel(dbg_info *db, ir_node *block, ir_node *store,
        res = new_ir_node(db, irg, block, op_Sel, mode, r_arity, r_in);
        res->attr.sel.entity = ent;
        res = optimize_node(res);
-       IRN_VRFY_IRG(res, irg);
+       IRN_VERIFY_IRG(res, irg);
        return res;
 }  /* new_bd_Sel */
 
@@ -294,7 +186,7 @@ static ir_node *new_bd_SymConst_type(dbg_info *db, ir_node *block,
                                      ir_mode *mode, symconst_symbol value,
                                      symconst_kind symkind, ir_type *tp)
 {
-       ir_graph *irg = current_ir_graph;
+       ir_graph *irg = get_irn_irg(block);
        ir_node  *res = new_ir_node(db, irg, block, op_SymConst, mode, 0, NULL);
 
        res->attr.symc.kind = symkind;
@@ -302,51 +194,28 @@ static ir_node *new_bd_SymConst_type(dbg_info *db, ir_node *block,
        res->attr.symc.tp   = tp;
 
        res = optimize_node(res);
-       IRN_VRFY_IRG(res, irg);
+       IRN_VERIFY_IRG(res, irg);
        return res;
 }  /* new_bd_SymConst_type */
 
 static ir_node *new_bd_Sync(dbg_info *db, ir_node *block)
 {
        ir_node  *res;
-       ir_graph *irg = current_ir_graph;
+       ir_graph *irg = get_irn_irg(block);
 
        res = new_ir_node(db, irg, block, op_Sync, mode_M, -1, NULL);
        /* no need to call optimize node here, Sync are always created with no predecessors */
-       IRN_VRFY_IRG(res, irg);
+       IRN_VERIFY_IRG(res, irg);
        return res;
 }  /* new_bd_Sync */
 
-
-static ir_node *new_bd_EndReg(dbg_info *db, ir_node *block)
-{
-       ir_node  *res;
-       ir_graph *irg = current_ir_graph;
-
-       res = new_ir_node(db, irg, block, op_EndReg, mode_T, -1, NULL);
-       set_irg_end_reg(irg, res);
-       IRN_VRFY_IRG(res, irg);
-       return res;
-}  /* new_bd_EndReg */
-
-static ir_node *new_bd_EndExcept(dbg_info *db, ir_node *block)
-{
-       ir_node  *res;
-       ir_graph *irg = current_ir_graph;
-
-       res = new_ir_node(db, irg, block, op_EndExcept, mode_T, -1, NULL);
-       set_irg_end_except(irg, res);
-       IRN_VRFY_IRG (res, irg);
-       return res;
-}  /* new_bd_EndExcept */
-
 static ir_node *new_bd_ASM(dbg_info *db, ir_node *block, int arity,
                            ir_node *in[], ir_asm_constraint *inputs, int n_outs,
                                 ir_asm_constraint *outputs, int n_clobber,
                                 ident *clobber[], ident *text)
 {
        ir_node  *res;
-       ir_graph *irg = current_ir_graph;
+       ir_graph *irg = get_irn_irg(block);
 
        res = new_ir_node(db, irg, block, op_ASM, mode_T, arity, in);
        res->attr.assem.pin_state = op_pin_state_pinned;
@@ -362,7 +231,7 @@ static ir_node *new_bd_ASM(dbg_info *db, ir_node *block, int arity,
        memcpy(res->attr.assem.clobbers, clobber, sizeof(clobber[0]) * n_clobber);
 
        res = optimize_node(res);
-       IRN_VRFY_IRG(res, irg);
+       IRN_VERIFY_IRG(res, irg);
        return res;
 }  /* new_bd_ASM */
 
@@ -531,26 +400,6 @@ ir_node *new_rd_Sync(dbg_info *db, ir_node *block, int arity, ir_node *in[])
        return res;
 }  /* new_rd_Sync */
 
-ir_node *new_rd_EndReg(dbg_info *db, ir_graph *irg, ir_node *block)
-{
-       ir_node *res;
-
-       res = new_ir_node(db, irg, block, op_EndReg, mode_T, -1, NULL);
-       set_irg_end_reg(irg, res);
-       IRN_VRFY_IRG(res, irg);
-       return res;
-}  /* new_rd_EndReg */
-
-ir_node *new_rd_EndExcept(dbg_info *db, ir_graph *irg, ir_node *block)
-{
-       ir_node *res;
-
-       res = new_ir_node(db, irg, block, op_EndExcept, mode_T, -1, NULL);
-       set_irg_end_except(irg, res);
-       IRN_VRFY_IRG (res, irg);
-       return res;
-}  /* new_rd_EndExcept */
-
 ir_node *new_rd_ASM(dbg_info *db, ir_node *block,
                     int arity, ir_node *in[], ir_asm_constraint *inputs,
                     int n_outs, ir_asm_constraint *outputs,
@@ -612,14 +461,6 @@ ir_node *new_r_Bad(ir_graph *irg)
 {
        return get_irg_bad(irg);
 }
-ir_node *new_r_EndReg(ir_graph *irg, ir_node *block)
-{
-       return new_rd_EndReg(NULL, irg, block);
-}
-ir_node *new_r_EndExcept(ir_graph *irg, ir_node *block)
-{
-       return new_rd_EndExcept(NULL, irg, block);
-}
 ir_node *new_r_NoMem(ir_graph *irg)
 {
        return get_irg_no_mem(irg);
@@ -644,7 +485,7 @@ ir_node *new_d_Start(dbg_info *db)
                          op_Start, mode_T, 0, NULL);
 
        res = optimize_node(res);
-       IRN_VRFY_IRG(res, current_ir_graph);
+       IRN_VERIFY_IRG(res, current_ir_graph);
        return res;
 }  /* new_d_Start */
 
@@ -654,7 +495,7 @@ ir_node *new_d_End(dbg_info *db)
        res = new_ir_node(db, current_ir_graph,  current_ir_graph->current_block,
                          op_End, mode_X, -1, NULL);
        res = optimize_node(res);
-       IRN_VRFY_IRG(res, current_ir_graph);
+       IRN_VERIFY_IRG(res, current_ir_graph);
 
        return res;
 }  /* new_d_End */
@@ -691,7 +532,7 @@ static inline ir_node *new_rd_Phi0(ir_graph *irg, ir_node *block, ir_mode *mode)
        ir_node *res;
 
        res = new_ir_node(NULL, irg, block, op_Phi, mode, 0, NULL);
-       IRN_VRFY_IRG(res, irg);
+       IRN_VERIFY_IRG(res, irg);
        return res;
 }  /* new_rd_Phi0 */
 
@@ -764,7 +605,7 @@ static inline ir_node *new_rd_Phi_in(ir_graph *irg, ir_node *block,
                }
        } else {
                res = optimize_node(res);  /* This is necessary to add the node to the hash table for cse. */
-               IRN_VRFY_IRG(res, irg);
+               IRN_VERIFY_IRG(res, irg);
                /* Memory Phis in endless loops must be kept alive.
                   As we can't distinguish these easily we keep all of them alive. */
                if (is_Phi(res) && mode == mode_M)
@@ -1171,7 +1012,7 @@ void mature_immBlock(ir_node *block)
                   nodes refer to the unoptimized node.
                   We can call optimize_in_place_2(), as global cse has no effect on blocks. */
                block = optimize_in_place_2(block);
-               IRN_VRFY_IRG(block, irg);
+               IRN_VERIFY_IRG(block, irg);
        }
 }  /* mature_immBlock */
 
@@ -1250,17 +1091,6 @@ ir_node *new_d_Sync(dbg_info *db, int arity, ir_node *in[])
        return new_rd_Sync(db, current_ir_graph->current_block, arity, in);
 }  /* new_d_Sync */
 
-ir_node *new_d_EndReg(dbg_info *db)
-{
-       return new_bd_EndReg(db, current_ir_graph->current_block);
-}  /* new_d_EndReg */
-
-ir_node *new_d_EndExcept(dbg_info *db)
-{
-       return new_bd_EndExcept(db, current_ir_graph->current_block);
-}  /* new_d_EndExcept */
-
-
 ir_node *new_d_ASM(dbg_info *db, int arity, ir_node *in[], ir_asm_constraint *inputs,
                    int n_outs, ir_asm_constraint *outputs, int n_clobber,
                    ident *clobber[], ident *text)
@@ -1306,7 +1136,7 @@ ir_node *new_d_immBlock(dbg_info *db)
        memset(res->attr.block.graph_arr, 0, sizeof(ir_node *)*current_ir_graph->n_loc);
 
        /* Immature block may not be optimized! */
-       IRN_VRFY_IRG(res, current_ir_graph);
+       IRN_VERIFY_IRG(res, current_ir_graph);
 
        return res;
 }  /* new_d_immBlock */
@@ -1380,6 +1210,57 @@ ir_node *get_value(int pos, ir_mode *mode)
        return get_d_value(NULL, pos, mode);
 }  /* get_value */
 
+/**
+ * helper function for guess_mode: recursively look for a definition for
+ * local variable @p pos, returns its mode if found.
+ */
+static ir_mode *guess_recursively(ir_node *block, int pos)
+{
+       ir_node *value;
+       int      n_preds;
+       int      i;
+
+       if (irn_visited(block))
+               return NULL;
+       mark_irn_visited(block);
+
+       /* already have a defintion -> we can simply look at its mode */
+       value = block->attr.block.graph_arr[pos];
+       if (value != NULL)
+               return get_irn_mode(value);
+
+       /* now we try to guess, by looking at the predecessor blocks */
+       n_preds = get_irn_arity(block);
+       for (i = 0; i < n_preds; ++i) {
+               ir_node *pred_block = get_Block_cfgpred_block(block, i);
+               ir_mode *mode       = guess_recursively(pred_block, pos);
+               if (mode != NULL)
+                       return mode;
+       }
+
+       /* no way to guess */
+       return NULL;
+}
+
+ir_mode *ir_guess_mode(int pos)
+{
+       ir_graph *irg   = current_ir_graph;
+       ir_node  *block = irg->current_block;
+       ir_node  *value = block->attr.block.graph_arr[pos+1];
+       ir_mode  *mode;
+
+       /* already have a defintion -> we can simply look at its mode */
+       if (value != NULL)
+               return get_irn_mode(value);
+
+       ir_reserve_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
+       inc_irg_visited(current_ir_graph);
+       mode = guess_recursively(block, pos+1);
+       ir_free_resources(current_ir_graph, IR_RESOURCE_IRN_VISITED);
+
+       return mode;
+}
+
 /* set a value at position pos in the parameter array from the current block */
 void set_value(int pos, ir_node *value)
 {
@@ -1548,14 +1429,6 @@ ir_node *new_Bad(void)
 {
        return get_irg_bad(current_ir_graph);
 }
-ir_node *new_EndReg(void)
-{
-       return new_d_EndReg(NULL);
-}
-ir_node *new_EndExcept(void)
-{
-       return new_d_EndExcept(NULL);
-}
 ir_node *new_NoMem(void)
 {
        return get_irg_no_mem(current_ir_graph);
@@ -1571,6 +1444,14 @@ ir_node *new_ASM(int arity, ir_node *in[], ir_asm_constraint *inputs,
 ir_node *new_Anchor(ir_graph *irg)
 {
        ir_node *in[anchor_last];
+       ir_node *res;
        memset(in, 0, sizeof(in));
-       return new_ir_node(NULL, irg, NULL, op_Anchor, mode_ANY, anchor_last, in);
+       res = new_ir_node(NULL, irg, NULL, op_Anchor, mode_ANY, anchor_last, in);
+
+       /* hack to get get_irn_irg working: set block to ourself and allow
+        * get_Block_irg for anchor */
+       res->attr.irg.irg = irg;
+       res->in[0] = res;
+
+       return res;
 }