Allow loading of stack parameters with a different mode than the parameter mode....
[libfirm] / ir / be / ia32 / ia32_intrinsics.c
index 034a08e..8a08d21 100644 (file)
@@ -67,18 +67,24 @@ void ia32_handle_intrinsics(void) {
  */
 static void resolve_call(ir_node *call, ir_node *l_res, ir_node *h_res, ir_graph *irg, ir_node *block) {
        ir_node *res, *in[2];
+       ir_node *bad   = get_irg_bad(irg);
+       ir_node *nomem = get_irg_no_mem(irg);
 
        in[0] = l_res;
        in[1] = h_res;
        res = new_r_Tuple(irg, block, h_res == NULL ? 1 : 2, in);
 
        turn_into_tuple(call, pn_Call_max);
-       set_Tuple_pred(call, pn_Call_M_regular,        get_irg_no_mem(irg));
-       set_Tuple_pred(call, pn_Call_X_regular,        new_r_Jmp(irg, block));
-       set_Tuple_pred(call, pn_Call_X_except,         get_irg_bad(irg));
+       set_Tuple_pred(call, pn_Call_M_regular,        nomem);
+       /* Matze: the new_r_Jmp here sometimes CSEs and then bad things happen
+        * (in movgen.c from 186.crafty for example) I don't know why it is here
+        * and if this fix is correct... */
+       /*set_Tuple_pred(call, pn_Call_X_regular,        new_r_Jmp(irg, block));*/
+       set_Tuple_pred(call, pn_Call_X_regular,        bad);
+       set_Tuple_pred(call, pn_Call_X_except,         bad);
        set_Tuple_pred(call, pn_Call_T_result,         res);
-       set_Tuple_pred(call, pn_Call_M_except,         get_irg_no_mem(irg));
-       set_Tuple_pred(call, pn_Call_P_value_res_base, get_irg_bad(irg));
+       set_Tuple_pred(call, pn_Call_M_except,         nomem);
+       set_Tuple_pred(call, pn_Call_P_value_res_base, bad);
 }
 
 /**
@@ -118,27 +124,33 @@ static int map_Add(ir_node *call, void *ctx) {
 /**
  * Map a Sub (a_l, a_h, b_l, b_h)
  */
-static int map_Sub(ir_node *call, void *ctx) {
-       ir_graph *irg     = current_ir_graph;
-       dbg_info *dbg     = get_irn_dbg_info(call);
-       ir_node  *block   = get_nodes_block(call);
-       ir_node  **params = get_Call_param_arr(call);
-       ir_type  *method  = get_Call_type(call);
-       ir_node  *a_l     = params[BINOP_Left_Low];
-       ir_node  *a_h     = params[BINOP_Left_High];
-       ir_node  *b_l     = params[BINOP_Right_Low];
-       ir_node  *b_h     = params[BINOP_Right_High];
-       ir_mode  *l_mode  = get_type_mode(get_method_res_type(method, 0));
-       ir_mode  *h_mode  = get_type_mode(get_method_res_type(method, 1));
-       ir_node  *l_res, *h_res, *res;
+static int map_Sub(ir_node *call, void *ctx)
+{
+       ir_graph *irg        = current_ir_graph;
+       dbg_info *dbg        = get_irn_dbg_info(call);
+       ir_node  *block      = get_nodes_block(call);
+       ir_node  **params    = get_Call_param_arr(call);
+       ir_type  *method     = get_Call_type(call);
+       ir_node  *a_l        = params[BINOP_Left_Low];
+       ir_node  *a_h        = params[BINOP_Left_High];
+       ir_node  *b_l        = params[BINOP_Right_Low];
+       ir_node  *b_h        = params[BINOP_Right_High];
+       ir_mode  *l_mode     = get_type_mode(get_method_res_type(method, 0));
+       ir_mode  *h_mode     = get_type_mode(get_method_res_type(method, 1));
+       ir_mode  *mode_flags = ia32_reg_classes[CLASS_ia32_flags].mode;
+       ir_node  *sub_low, *sub_high, *flags;
+       ir_node  *l_res, *h_res;
        (void) ctx;
 
        /* l_res = a_l - b_l */
        /* h_res = a_h - b_h - carry */
 
-       res   = new_rd_ia32_Sub64Bit(dbg, irg, block, a_l, a_h, b_l, b_h);
-       l_res = new_r_Proj(irg, block, res, l_mode, pn_ia32_Sub64Bit_low_res);
-       h_res = new_r_Proj(irg, block, res, h_mode, pn_ia32_Sub64Bit_high_res);
+       sub_low  = new_rd_ia32_l_Sub(dbg, irg, block, a_l, b_l, mode_T);
+       flags    = new_r_Proj(irg, block, sub_low, mode_flags, pn_ia32_flags);
+       sub_high = new_rd_ia32_l_Sbb(dbg, irg, block, a_h, b_h, flags, h_mode);
+
+       l_res = new_r_Proj(irg, block, sub_low, l_mode, pn_ia32_res);
+       h_res = sub_high;
 
        resolve_call(call, l_res, h_res, irg, block);
        return 1;
@@ -406,6 +418,39 @@ static int map_Shrs(ir_node *call, void *ctx) {
        return 1;
 }
 
+static int is_sign_extend(ir_node *low, ir_node *high)
+{
+       if (is_Shrs(high)) {
+               ir_node *high_l;
+               ir_node *high_r;
+               tarval  *shift_count;
+
+               high_r = get_Shrs_right(high);
+               if (!is_Const(high_r)) return 0;
+
+               shift_count = get_Const_tarval(high_r);
+               if (!tarval_is_long(shift_count))       return 0;
+               if (get_tarval_long(shift_count) != 31) return 0;
+
+               high_l = get_Shrs_left(high);
+
+               if (is_Conv(low)    && get_Conv_op(low)    == high_l) return 1;
+               if (is_Conv(high_l) && get_Conv_op(high_l) == low)    return 1;
+       } else if (is_Const(low) && is_Const(high)) {
+               tarval *tl = get_Const_tarval(low);
+               tarval *th = get_Const_tarval(high);
+
+               if (tarval_is_long(th) && tarval_is_long(tl)) {
+                       long l = get_tarval_long(tl);
+                       long h = get_tarval_long(th);
+
+                       return (h == 0  && l >= 0) || (h == -1 && l <  0);
+               }
+       }
+
+       return 0;
+}
+
 /**
  * Map a Mul (a_l, a_h, b_l, b_h)
  */
@@ -435,44 +480,12 @@ static int map_Mul(ir_node *call, void *ctx) {
        */
 
        /* handle the often used case of 32x32=64 mul */
-       if (is_Shrs(a_h) && get_Shrs_left(a_h) == a_l) {
-               ir_node *c1 = get_Shrs_right(a_h);
-
-               if (is_Const(c1)) {
-                       tarval *tv = get_Const_tarval(c1);
-
-                       if (tarval_is_long(tv) && get_tarval_long(tv) == 31) {
-                               /* a is a sign extend */
-
-                               if (is_Shrs(b_h) && get_Shrs_left(b_h) == b_l && c1 == get_Shrs_right(b_h)) {
-                                       /* b is a sign extend: it's a 32 * 32 = 64 signed multiplication */
-                                       mul   = new_rd_ia32_l_IMul(dbg, irg, block, a_l, b_l);
-                                       h_res = new_rd_Proj(dbg, irg, block, mul, h_mode, pn_ia32_l_Mul_EDX);
-                                       l_res = new_rd_Proj(dbg, irg, block, mul, l_mode, pn_ia32_l_Mul_EAX);
-
-                                       goto end;
-                               }
-                               /* we rely here on Consts being on the right side */
-                               if (is_Const(b_h) && is_Const(b_l)) {
-                                       tarval *th = get_Const_tarval(b_h);
-                                       tarval *tl = get_Const_tarval(b_l);
-
-                                       if (tarval_is_long(th) && tarval_is_long(tl)) {
-                                               long h = get_tarval_long(th);
-                                               long l = get_tarval_long(tl);
-
-                                               if ((h == 0 && l >= 0) || (h == -1 && l < 0)) {
-                                                       /* b is a sign extended const */
-                                                       mul   = new_rd_ia32_l_IMul(dbg, irg, block, a_l, b_l);
-                                                       h_res = new_rd_Proj(dbg, irg, block, mul, h_mode, pn_ia32_l_Mul_EDX);
-                                                       l_res = new_rd_Proj(dbg, irg, block, mul, l_mode, pn_ia32_l_Mul_EAX);
-
-                                                       goto end;
-                                               }
-                                       }
-                               }
-                       }
-               }
+       if (is_sign_extend(a_l, a_h) && is_sign_extend(b_l, b_h)) {
+               mul   = new_rd_ia32_l_IMul(dbg, irg, block, a_l, b_l);
+               h_res = new_rd_Proj(dbg, irg, block, mul, h_mode, pn_ia32_l_Mul_EDX);
+               l_res = new_rd_Proj(dbg, irg, block, mul, l_mode, pn_ia32_l_Mul_EAX);
+
+               goto end;
        }
 
        mul   = new_rd_ia32_l_Mul(dbg, irg, block, a_l, b_l);
@@ -521,17 +534,20 @@ static int map_Minus(ir_node *call, void *ctx) {
  * Map a Abs (a_l, a_h)
  */
 static int map_Abs(ir_node *call, void *ctx) {
-       ir_graph *irg     = current_ir_graph;
-       dbg_info *dbg     = get_irn_dbg_info(call);
-       ir_node  *block   = get_nodes_block(call);
-       ir_node  **params = get_Call_param_arr(call);
-       ir_type  *method  = get_Call_type(call);
-       ir_node  *a_l     = params[BINOP_Left_Low];
-       ir_node  *a_h     = params[BINOP_Left_High];
-       ir_mode  *l_mode  = get_type_mode(get_method_res_type(method, 0));
-       ir_mode  *h_mode  = get_type_mode(get_method_res_type(method, 1));
-       ir_node  *l_res, *h_res, *sign, *sub_l, *sub_h, *res;
+       ir_graph *irg        = current_ir_graph;
+       dbg_info *dbg        = get_irn_dbg_info(call);
+       ir_node  *block      = get_nodes_block(call);
+       ir_node  **params    = get_Call_param_arr(call);
+       ir_type  *method     = get_Call_type(call);
+       ir_node  *a_l        = params[BINOP_Left_Low];
+       ir_node  *a_h        = params[BINOP_Left_High];
+       ir_mode  *l_mode     = get_type_mode(get_method_res_type(method, 0));
+       ir_mode  *h_mode     = get_type_mode(get_method_res_type(method, 1));
+       ir_mode  *mode_flags = ia32_reg_classes[CLASS_ia32_flags].mode;
+       ir_node  *l_res, *h_res, *sign, *sub_l, *sub_h;
        ir_node  *sign_l;
+       ir_node  *l_sub;
+       ir_node  *flags;
        (void) ctx;
 
        /*
@@ -553,9 +569,11 @@ static int map_Abs(ir_node *call, void *ctx) {
        sign_l = new_rd_Conv(dbg, irg, block, sign, l_mode);
        sub_l  = new_rd_Eor(dbg, irg, block, a_l, sign_l, l_mode);
        sub_h  = new_rd_Eor(dbg, irg, block, a_h, sign,   h_mode);
-       res    = new_rd_ia32_Sub64Bit(dbg, irg, block, sub_l, sub_h, sign, sign);
-       l_res  = new_r_Proj(irg, block, res, l_mode, pn_ia32_Sub64Bit_low_res);
-       h_res  = new_r_Proj(irg, block, res, h_mode, pn_ia32_Sub64Bit_high_res);
+
+       l_sub  = new_rd_ia32_l_Sub(dbg, irg, block, sub_l, sign_l, mode_T);
+       l_res  = new_r_Proj(irg, block, l_sub, l_mode,     pn_ia32_res);
+       flags  = new_r_Proj(irg, block, l_sub, mode_flags, pn_ia32_flags);
+       h_res  = new_rd_ia32_l_Sbb(dbg, irg, block, sub_h, sign, flags, h_mode);
 
        resolve_call(call, l_res, h_res, irg, block);
 
@@ -711,11 +729,10 @@ static int map_Conv(ir_node *call, void *ctx) {
                /* We have a Conv long long -> float here */
                ir_node *a_l       = params[BINOP_Left_Low];
                ir_node *a_h       = params[BINOP_Left_High];
-               ir_mode *mode_a_l  = get_irn_mode(a_l);
-               ir_mode *mode_a_h  = get_irn_mode(a_h);
                ir_mode *fres_mode = get_type_mode(get_method_res_type(method, 0));
 
-               assert(! mode_is_float(mode_a_l) && ! mode_is_float(mode_a_h) && "unexpected Conv call");
+               assert(! mode_is_float(get_irn_mode(a_l))
+                               && ! mode_is_float(get_irn_mode(a_h)));
 
                /* allocate memory on frame to store args */
                ent = env->irg == irg ? env->ll_d_conv : NULL;