Add and fix some comments.
[libfirm] / ir / lower / lower_intrinsics.c
index cd5fb3a..d612343 100644 (file)
@@ -57,7 +57,7 @@ typedef struct walker_env {
  */
 static void call_mapper(ir_node *node, void *env)
 {
-       walker_env_t *wenv = env;
+       walker_env_t *wenv = (walker_env_t*)env;
        ir_op *op = get_irn_op(node);
 
        if (op == op_Call) {
@@ -74,7 +74,7 @@ static void call_mapper(ir_node *node, void *env)
                p   = pmap_find(wenv->c_map, ent);
 
                if (p) {
-                       r = p->value;
+                       r = (const i_call_record*)p->value;
                        wenv->nr_of_intrinsics += r->i_mapper(node, r->ctx) ? 1 : 0;
                }
        } else {
@@ -86,7 +86,7 @@ static void call_mapper(ir_node *node, void *env)
                                        ++wenv->nr_of_intrinsics;
                                        break;
                                }
-                               r = r->link;
+                               r = (const i_instr_record*)r->link;
                        }
                }
        }
@@ -103,7 +103,7 @@ unsigned lower_intrinsics(i_record *list, int length, int part_block_used)
        walker_env_t   wenv;
 
        /* we use the ir_op generic pointers here */
-       NEW_ARR_A(const i_instr_record *, i_map, n_ops);
+       NEW_ARR_A(i_instr_record *, i_map, n_ops);
        memset((void *)i_map, 0, sizeof(*i_map) * n_ops);
 
        /* fill a map for faster search */
@@ -163,20 +163,20 @@ unsigned lower_intrinsics(i_record *list, int length, int part_block_used)
        return nr_of_intrinsics;
 }  /* lower_intrinsics */
 
-struct pass_t {
+typedef struct pass_t {
        ir_prog_pass_t pass;
 
        int part_block_used;
        int      length;
        i_record list[1];
-};
+} pass_t;
 
 /**
  * Wrapper for running lower_intrinsics() as an ir_prog pass.
  */
 static int pass_wrapper(ir_prog *irp, void *context)
 {
-       struct pass_t *pass = context;
+       pass_t *pass = (pass_t*)context;
        (void) irp; /* TODO: set current irp, or remove parameter */
        lower_intrinsics(pass->list, pass->length, pass->part_block_used);
        /* probably this pass should not run again */
@@ -195,7 +195,7 @@ ir_prog_pass_t *lower_intrinsics_pass(
        const char *name,
        i_record *list, int length, int part_block_used)
 {
-       struct pass_t *pass = xmalloc(sizeof(*pass) + (length-1) * sizeof(pass->list[0]));
+       pass_t *pass = (pass_t*)xmalloc(sizeof(*pass) + (length-1) * sizeof(pass->list[0]));
 
        memcpy(pass->list, list, sizeof(list[0]) * length);
        pass->length          = length;
@@ -216,7 +216,8 @@ ir_prog_pass_t *lower_intrinsics_pass(
  */
 static void replace_call(ir_node *irn, ir_node *call, ir_node *mem, ir_node *reg_jmp, ir_node *exc_jmp)
 {
-       ir_node *block = get_nodes_block(call);
+       ir_node  *block = get_nodes_block(call);
+       ir_graph *irg   = get_irn_irg(block);
 
        if (reg_jmp == NULL) {
 
@@ -225,7 +226,7 @@ static void replace_call(ir_node *irn, ir_node *call, ir_node *mem, ir_node *reg
                set_opt_cse(0);
                reg_jmp = new_r_Jmp(block);
                set_opt_cse(old_cse);
-               exc_jmp = new_Bad();
+               exc_jmp = new_r_Bad(irg);
        }
        irn = new_r_Tuple(block, 1, &irn);
 
@@ -234,7 +235,7 @@ static void replace_call(ir_node *irn, ir_node *call, ir_node *mem, ir_node *reg
        set_Tuple_pred(call, pn_Call_X_regular, reg_jmp);
        set_Tuple_pred(call, pn_Call_X_except, exc_jmp);
        set_Tuple_pred(call, pn_Call_T_result, irn);
-       set_Tuple_pred(call, pn_Call_P_value_res_base, new_Bad());
+       set_Tuple_pred(call, pn_Call_P_value_res_base, new_r_Bad(irg));
 }  /* replace_call */
 
 /* A mapper for the integer abs. */
@@ -316,9 +317,9 @@ int i_mapper_alloca(ir_node *call, void *ctx)
 /* A mapper for the floating point sqrt. */
 int i_mapper_sqrt(ir_node *call, void *ctx)
 {
-       ir_node *mem;
-       tarval *tv;
-       ir_node *op = get_Call_param(call, 0);
+       ir_node   *mem;
+       ir_tarval *tv;
+       ir_node   *op = get_Call_param(call, 0);
        (void) ctx;
 
        if (!is_Const(op))
@@ -339,9 +340,9 @@ int i_mapper_sqrt(ir_node *call, void *ctx)
 /* A mapper for the floating point cbrt. */
 int i_mapper_cbrt(ir_node *call, void *ctx)
 {
-       ir_node *mem;
-       tarval *tv;
-       ir_node *op = get_Call_param(call, 0);
+       ir_node   *mem;
+       ir_tarval *tv;
+       ir_node   *op = get_Call_param(call, 0);
        (void) ctx;
 
        if (!is_Const(op))
@@ -363,22 +364,23 @@ int i_mapper_cbrt(ir_node *call, void *ctx)
 int i_mapper_pow(ir_node *call, void *ctx)
 {
        dbg_info *dbg;
-       ir_node *mem;
-       ir_node *left  = get_Call_param(call, 0);
-       ir_node *right = get_Call_param(call, 1);
-       ir_node *block = get_nodes_block(call);
-       ir_node *irn, *reg_jmp = NULL, *exc_jmp = NULL;
+       ir_node  *mem;
+       ir_node  *left  = get_Call_param(call, 0);
+       ir_node  *right = get_Call_param(call, 1);
+       ir_node  *block = get_nodes_block(call);
+       ir_graph *irg   = get_irn_irg(block);
+       ir_node  *irn, *reg_jmp = NULL, *exc_jmp = NULL;
        (void) ctx;
 
        if (is_Const(left) && is_Const_one(left)) {
                /* pow (1.0, x) = 1.0 */
                irn = left;
        } else if (is_Const(right)) {
-               tarval *tv = get_Const_tarval(right);
+               ir_tarval *tv = get_Const_tarval(right);
                if (tarval_is_null(tv)) {
                        /* pow(x, 0.0) = 1.0 */
                        ir_mode *mode = get_tarval_mode(tv);
-                       irn = new_Const(get_mode_one(mode));
+                       irn = new_r_Const(irg, get_mode_one(mode));
                } else if (tarval_is_one(tv)) {
                        /* pow(x, 1.0) = x */
                        irn = left;
@@ -398,7 +400,7 @@ int i_mapper_pow(ir_node *call, void *ctx)
                ir_mode *mode = get_irn_mode(left);
                ir_node *quot;
 
-               irn  = new_Const(get_mode_one(mode));
+               irn  = new_r_Const(irg, get_mode_one(mode));
                quot = new_rd_Quot(dbg, block, mem, irn, left, mode, op_pin_state_pinned);
                mem  = new_r_Proj(quot, mode_M, pn_Quot_M);
                irn  = new_r_Proj(quot, mode, pn_Quot_res);
@@ -418,8 +420,9 @@ int i_mapper_exp(ir_node *call, void *ctx)
 
        if (is_Const(val) && is_Const_null(val)) {
                /* exp(0.0) = 1.0 */
+               ir_graph *irg  = get_irn_irg(val);
                ir_mode *mode  = get_irn_mode(val);
-               ir_node *irn   = new_Const(get_mode_one(mode));
+               ir_node *irn   = new_r_Const(irg, get_mode_one(mode));
                ir_node *mem   = get_Call_mem(call);
                DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_EXP);
                replace_call(irn, call, mem, NULL, NULL);
@@ -456,9 +459,10 @@ static int i_mapper_one_to_zero(ir_node *call, void *ctx, int reason)
 
        if (is_Const(val) && is_Const_one(val)) {
                /* acos(1.0) = 0.0 */
-               ir_mode *mode  = get_irn_mode(val);
-               ir_node *irn   = new_Const(get_mode_null(mode));
-               ir_node *mem   = get_Call_mem(call);
+               ir_graph *irg = get_irn_irg(val);
+               ir_mode *mode = get_irn_mode(val);
+               ir_node *irn  = new_r_Const(irg, get_mode_null(mode));
+               ir_node *mem  = get_Call_mem(call);
                DBG_OPT_ALGSIM0(call, irn, reason);
                replace_call(irn, call, mem, NULL, NULL);
                return 1;
@@ -502,8 +506,9 @@ static int i_mapper_symmetric_zero_to_one(ir_node *call, void *ctx, int reason)
 
        if (is_Const(val) && is_Const_null(val)) {
                /* f(0.0) = 1.0 */
+               ir_graph *irg  = get_irn_irg(val);
                ir_mode *mode  = get_irn_mode(val);
-               ir_node *irn   = new_Const(get_mode_one(mode));
+               ir_node *irn   = new_r_Const(irg, get_mode_one(mode));
                ir_node *mem   = get_Call_mem(call);
                DBG_OPT_ALGSIM0(call, irn, reason);
                replace_call(irn, call, mem, NULL, NULL);
@@ -603,7 +608,7 @@ static ir_entity *get_const_entity(ir_node *ptr)
 
 static bool initializer_val_is_null(ir_initializer_t *init)
 {
-       tarval *tv;
+       ir_tarval *tv;
 
        if (get_initializer_kind(init) == IR_INITIALIZER_NULL)
                return true;
@@ -631,7 +636,7 @@ static bool initializer_val_is_null(ir_initializer_t *init)
  * @return a Const node containing the strlen() result or NULL
  *         if the evaluation fails
  */
-static ir_node *eval_strlen(ir_entity *ent, ir_type *res_tp)
+static ir_node *eval_strlen(ir_graph *irg, ir_entity *ent, ir_type *res_tp)
 {
        ir_type *tp = get_entity_type(ent);
        ir_mode *mode;
@@ -669,8 +674,8 @@ static ir_node *eval_strlen(ir_entity *ent, ir_type *res_tp)
                        }
                }
                if (len >= 0) {
-                       tarval *tv = new_tarval_from_long(len, get_type_mode(res_tp));
-                       return new_Const_type(tv, res_tp);
+                       ir_tarval *tv = new_tarval_from_long(len, get_type_mode(res_tp));
+                       return new_r_Const(irg, tv);
                }
                return NULL;
        }
@@ -683,8 +688,8 @@ static ir_node *eval_strlen(ir_entity *ent, ir_type *res_tp)
        for (i = 0; i < size; ++i) {
                ir_initializer_t *val = get_initializer_compound_value(initializer, i);
                if (initializer_val_is_null(val)) {
-                       tarval *tv = new_tarval_from_long(i, get_type_mode(res_tp));
-                       return new_Const_type(tv, res_tp);
+                       ir_tarval *tv = new_tarval_from_long(i, get_type_mode(res_tp));
+                       return new_r_Const(irg, tv);
                }
        }
 
@@ -706,7 +711,7 @@ int i_mapper_strlen(ir_node *call, void *ctx)
                ir_node *irn;
 
                tp  = get_method_res_type(tp, 0);
-               irn = eval_strlen(ent, tp);
+               irn = eval_strlen(get_irn_irg(call), ent, tp);
 
                if (irn) {
                        ir_node *mem = get_Call_mem(call);
@@ -728,7 +733,8 @@ int i_mapper_strlen(ir_node *call, void *ctx)
  * @return a Const node containing the strcmp() result or NULL
  *         if the evaluation fails
  */
-static ir_node *eval_strcmp(ir_entity *left, ir_entity *right, ir_type *res_tp)
+static ir_node *eval_strcmp(ir_graph *irg, ir_entity *left, ir_entity *right,
+                            ir_type *res_tp)
 {
        ir_type *tp;
        ir_mode *mode;
@@ -769,7 +775,7 @@ static ir_node *eval_strcmp(ir_entity *left, ir_entity *right, ir_type *res_tp)
                for (i = 0; i < n; ++i) {
                        ir_node *irn;
                        long v_l, v_r;
-                       tarval *tv;
+                       ir_tarval *tv;
 
                        irn = get_compound_ent_value(left, i);
                        if (! is_Const(irn))
@@ -799,8 +805,8 @@ static ir_node *eval_strcmp(ir_entity *left, ir_entity *right, ir_type *res_tp)
                }
                if (i < n) {
                        /* we found an end */
-                       tarval *tv = new_tarval_from_long(res, get_type_mode(res_tp));
-                       return new_Const_type(tv, res_tp);
+                       ir_tarval *tv = new_tarval_from_long(res, get_type_mode(res_tp));
+                       return new_r_Const(irg, tv);
                }
                return NULL;
        }
@@ -885,10 +891,11 @@ int i_mapper_strcmp(ir_node *call, void *ctx)
 
        if (left == right) {
                /* a strcmp(s, s) ==> 0 */
+               ir_graph *irg  = get_irn_irg(call);
                ir_node *mem   = get_Call_mem(call);
                ir_mode *mode  = get_type_mode(res_tp);
 
-               irn = new_Const(get_mode_null(mode));
+               irn = new_r_Const(irg, get_mode_null(mode));
                DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRCMP);
                replace_call(irn, call, mem, NULL, NULL);
                return 1;
@@ -898,7 +905,7 @@ int i_mapper_strcmp(ir_node *call, void *ctx)
 
        if (ent_l != NULL && ent_r != NULL) {
                /* both entities are const, try to evaluate */
-               irn = eval_strcmp(ent_l, ent_r, res_tp);
+               irn = eval_strcmp(get_irn_irg(call), ent_l, ent_r, res_tp);
        } else if (ent_l != NULL) {
                if (is_empty_string(ent_l)) {
                        /* s strcmp("", s) ==> -(*s)*/
@@ -920,7 +927,7 @@ replace_by_call:
                        mode  = get_type_mode(char_tp);
 
                        /* replace the strcmp by (*x) */
-                       irn = new_rd_Load(dbg, block, mem, v, mode, 0);
+                       irn = new_rd_Load(dbg, block, mem, v, mode, cons_none);
                        mem = new_r_Proj(irn, mode_M, pn_Load_M);
                        exc = new_r_Proj(irn, mode_X, pn_Load_X_except);
                        reg = new_r_Proj(irn, mode_X, pn_Load_X_regular);
@@ -959,6 +966,7 @@ int i_mapper_strncmp(ir_node *call, void *ctx)
        if (left == right || (is_Const(len) && is_Const_null(len))) {
                /* a strncmp(s, s, len) ==> 0 OR
                   a strncmp(a, b, 0) ==> 0 */
+               ir_graph  *irg     = get_irn_irg(call);
                ir_node   *mem     = get_Call_mem(call);
                ir_node   *adr     = get_Call_ptr(call);
                ir_entity *ent     = get_SymConst_entity(adr);
@@ -966,7 +974,7 @@ int i_mapper_strncmp(ir_node *call, void *ctx)
                ir_type   *res_tp  = get_method_res_type(call_tp, 0);
                ir_mode   *mode    = get_type_mode(res_tp);
 
-               irn = new_Const(get_mode_null(mode));
+               irn = new_r_Const(irg, get_mode_null(mode));
                DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRNCMP);
                replace_call(irn, call, mem, NULL, NULL);
                return 1;
@@ -1087,6 +1095,7 @@ int i_mapper_memcmp(ir_node *call, void *ctx)
        if (left == right || (is_Const(len) && is_Const_null(len))) {
                /* a memcmp(s, s, len) ==> 0 OR
                   a memcmp(a, b, 0) ==> 0 */
+               ir_graph  *irg     = get_irn_irg(call);
                ir_node   *mem     = get_Call_mem(call);
                ir_node   *adr     = get_Call_ptr(call);
                ir_entity *ent     = get_SymConst_entity(adr);
@@ -1094,7 +1103,7 @@ int i_mapper_memcmp(ir_node *call, void *ctx)
                ir_type   *res_tp  = get_method_res_type(call_tp, 0);
                ir_mode   *mode    = get_type_mode(res_tp);
 
-               irn = new_Const(get_mode_null(mode));
+               irn = new_r_Const(irg, get_mode_null(mode));
                DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRNCMP);
                replace_call(irn, call, mem, NULL, NULL);
                return 1;