2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Machine dependent Firm optimizations.
10 * @author Sebastian Hack, Michael Beck
12 * Implements "Strength Reduction of Multiplications by Integer Constants"
14 * Implements Division and Modulo by Consts from "Hackers Delight",
22 #include "irgraph_t.h"
29 #include "dbginfo_t.h"
30 #include "iropt_dbg.h"
39 /** The bit mask, which optimizations to apply. */
40 static arch_dep_opts_t opts;
42 void arch_dep_set_opts(arch_dep_opts_t the_opts)
47 /** check, whether a mode allows a Mulh instruction. */
48 static int allow_Mulh(const ir_settings_arch_dep_t *params, ir_mode *mode)
50 if (get_mode_size_bits(mode) > params->max_bits_for_mulh)
52 return (mode_is_signed(mode) && params->allow_mulhs) || (!mode_is_signed(mode) && params->allow_mulhu);
58 typedef struct instruction instruction;
60 insn_kind kind; /**< the instruction kind */
61 instruction *in[2]; /**< the ins */
62 unsigned shift_count; /**< shift count for LEA and SHIFT */
63 ir_node *irn; /**< the generated node for this instruction if any. */
64 int costs; /**< the costs for this instruction */
68 * The environment for the strength reduction of multiplications.
70 typedef struct mul_env {
71 struct obstack obst; /**< an obstack for local space. */
72 const ir_settings_arch_dep_t *params;
73 ir_mode *mode; /**< the mode of the multiplication constant */
74 unsigned bits; /**< number of bits in the mode */
75 unsigned max_S; /**< the maximum LEA shift value. */
76 instruction *root; /**< the root of the instruction tree */
77 ir_node *op; /**< the operand that is multiplied */
78 ir_node *blk; /**< the block where the new graph is built */
80 dbg_info *dbg; /**< the debug info for the new graph. */
81 ir_mode *shf_mode; /**< the (unsigned) mode for the shift constants */
82 int fail; /**< set to 1 if the instruction sequence fails the constraints */
83 int n_shift; /**< maximum number of allowed shift instructions */
85 evaluate_costs_func evaluate; /**< the evaluate callback */
89 * Some kind of default evaluator. Return the cost of
92 static int default_evaluate(insn_kind kind, const ir_mode *mode, ir_tarval *tv)
103 * emit a LEA (or an Add) instruction
105 static instruction *emit_LEA(mul_env *env, instruction *a, instruction *b, unsigned shift)
107 instruction *res = OALLOC(&env->obst, instruction);
108 res->kind = shift > 0 ? LEA : ADD;
111 res->shift_count = shift;
118 * emit a SHIFT (or an Add or a Zero) instruction
120 static instruction *emit_SHIFT(mul_env *env, instruction *a, unsigned shift)
122 instruction *res = OALLOC(&env->obst, instruction);
123 if (shift == env->bits) {
124 /* a 2^bits with bits resolution is a zero */
128 res->shift_count = 0;
129 } else if (shift != 1) {
133 res->shift_count = shift;
138 res->shift_count = 0;
146 * emit a SUB instruction
148 static instruction *emit_SUB(mul_env *env, instruction *a, instruction *b)
150 instruction *res = OALLOC(&env->obst, instruction);
154 res->shift_count = 0;
161 * emit the ROOT instruction
163 static instruction *emit_ROOT(mul_env *env, ir_node *root_op)
165 instruction *res = OALLOC(&env->obst, instruction);
169 res->shift_count = 0;
177 * Returns the condensed representation of the tarval tv
179 static unsigned char *value_to_condensed(mul_env *env, ir_tarval *tv, int *pr)
181 ir_mode *mode = get_tarval_mode(tv);
182 int bits = get_mode_size_bits(mode);
183 char *bitstr = get_tarval_bitpattern(tv);
185 unsigned char *R = (unsigned char*)obstack_alloc(&env->obst, bits);
188 for (i = 0; bitstr[i] != '\0'; ++i) {
189 if (bitstr[i] == '1') {
202 * Calculate the gain when using the generalized complementary technique
204 static int calculate_gain(unsigned char *R, int r)
210 /* the gain for r == 1 */
212 for (i = 2; i < r; ++i) {
213 /* calculate the gain for r from the gain for r-1 */
214 gain += 2 - R[i - 1];
216 if (gain > max_gain) {
225 * Calculates the condensed complement of a given (R,r) tuple
227 static unsigned char *complement_condensed(mul_env *env, unsigned char *R, int r, int gain, int *prs)
229 unsigned char *value = (unsigned char*)obstack_alloc(&env->obst, env->bits);
233 memset(value, 0, env->bits);
236 for (i = 0; i < gain; ++i) {
241 /* negate and propagate 1 */
243 for (i = 0; i <= j; ++i) {
244 unsigned char v = !value[i];
250 /* condense it again */
253 for (i = 0; i <= j; ++i) {
266 * creates a tarval from a condensed representation.
268 static ir_tarval *condensed_to_value(mul_env *env, unsigned char *R, int r)
270 ir_tarval *tv = get_mode_one(env->mode);
271 ir_tarval *res = NULL;
272 for (int i = 0; i < r; ++i) {
275 ir_tarval *t = new_tarval_from_long(j, mode_Iu);
276 tv = tarval_shl(tv, t);
278 res = res ? tarval_add(res, tv) : tv;
284 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, ir_tarval *N);
287 * handle simple cases with up-to 2 bits set
289 static instruction *decompose_simple_cases(mul_env *env, unsigned char *R, int r, ir_tarval *N)
291 instruction *ins, *ins2;
295 return emit_SHIFT(env, env->root, R[0]);
300 if (R[1] <= env->max_S) {
301 ins = emit_LEA(env, ins, ins, R[1]);
303 ins = emit_SHIFT(env, ins, R[0]);
308 ins = emit_SHIFT(env, ins, R[0]);
311 ins2 = emit_SHIFT(env, env->root, R[0] + R[1]);
312 return emit_LEA(env, ins, ins2, 0);
317 * Main decompose driver.
319 static instruction *decompose_mul(mul_env *env, unsigned char *R, int r, ir_tarval *N)
325 return decompose_simple_cases(env, R, r, N);
327 if (env->params->also_use_subs) {
328 gain = calculate_gain(R, r);
330 instruction *instr1, *instr2;
331 unsigned char *R1, *R2;
334 R1 = complement_condensed(env, R, r, gain, &r1);
336 R2 = (unsigned char*)obstack_alloc(&env->obst, r2);
339 for (i = 0; i < gain; ++i) {
346 /* Two identical bits: normalize */
351 for (i = gain + 1; i < r; ++i) {
355 instr1 = decompose_mul(env, R1, r1, NULL);
356 instr2 = decompose_mul(env, R2, r2, NULL);
357 return emit_SUB(env, instr2, instr1);
362 N = condensed_to_value(env, R, r);
364 for (i = env->max_S; i > 0; --i) {
365 ir_tarval *div_res, *mod_res;
366 ir_tarval *tv = new_tarval_from_long((1 << i) + 1, env->mode);
368 div_res = tarval_divmod(N, tv, &mod_res);
369 if (mod_res == get_mode_null(env->mode)) {
373 Rs = value_to_condensed(env, div_res, &rs);
375 instruction *N1 = decompose_mul(env, Rs, rs, div_res);
376 return emit_LEA(env, N1, N1, i);
380 return basic_decompose_mul(env, R, r, N);
383 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
386 * basic decomposition routine
388 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, ir_tarval *N)
393 if (R[0] == 0) { /* Case 1 */
394 t = R[1] > IMAX(env->max_S, R[1]);
396 Ns = decompose_mul(env, &R[1], r - 1, N);
397 return emit_LEA(env, env->root, Ns, t);
398 } else if (R[0] <= env->max_S) { /* Case 2 */
401 Ns = decompose_mul(env, &R[1], r - 1, N);
402 return emit_LEA(env, Ns, env->root, t);
406 Ns = decompose_mul(env, R, r, N);
407 return emit_SHIFT(env, Ns, t);
412 * recursive build the graph form the instructions.
414 * @param env the environment
415 * @param inst the instruction
417 static ir_node *build_graph(mul_env *env, instruction *inst)
420 ir_graph *irg = env->irg;
425 switch (inst->kind) {
427 l = build_graph(env, inst->in[0]);
428 r = build_graph(env, inst->in[1]);
429 c = new_r_Const_long(irg, env->shf_mode, inst->shift_count);
430 r = new_rd_Shl(env->dbg, env->blk, r, c, env->mode);
431 return inst->irn = new_rd_Add(env->dbg, env->blk, l, r, env->mode);
433 l = build_graph(env, inst->in[0]);
434 c = new_r_Const_long(irg, env->shf_mode, inst->shift_count);
435 return inst->irn = new_rd_Shl(env->dbg, env->blk, l, c, env->mode);
437 l = build_graph(env, inst->in[0]);
438 r = build_graph(env, inst->in[1]);
439 return inst->irn = new_rd_Sub(env->dbg, env->blk, l, r, env->mode);
441 l = build_graph(env, inst->in[0]);
442 r = build_graph(env, inst->in[1]);
443 return inst->irn = new_rd_Add(env->dbg, env->blk, l, r, env->mode);
445 return inst->irn = new_r_Const(irg, get_mode_null(env->mode));
447 panic("Unsupported instruction kind");
452 * Calculate the costs for the given instruction sequence.
453 * Note that additional costs due to higher register pressure are NOT evaluated yet
455 static int evaluate_insn(mul_env *env, instruction *inst)
459 if (inst->costs >= 0) {
460 /* was already evaluated */
464 switch (inst->kind) {
468 costs = evaluate_insn(env, inst->in[0]);
469 costs += evaluate_insn(env, inst->in[1]);
470 costs += env->evaluate(inst->kind, env->mode, NULL);
474 if (inst->shift_count > env->params->highest_shift_amount)
476 if (env->n_shift <= 0)
480 costs = evaluate_insn(env, inst->in[0]);
481 costs += env->evaluate(inst->kind, env->mode, NULL);
485 inst->costs = costs = env->evaluate(inst->kind, env->mode, NULL);
491 panic("Unsupported instruction kind");
495 * Evaluate the replacement instructions and build a new graph
496 * if faster than the Mul.
497 * Returns the root of the new graph then or irn otherwise.
499 * @param irn the Mul operation
500 * @param operand the multiplication operand
501 * @param tv the multiplication constant
503 * @return the new graph
505 static ir_node *do_decomposition(ir_node *irn, ir_node *operand, ir_tarval *tv)
514 obstack_init(&env.obst);
515 env.params = be_get_backend_param()->dep_param;
516 env.mode = get_tarval_mode(tv);
517 env.bits = (unsigned)get_mode_size_bits(env.mode);
519 env.root = emit_ROOT(&env, operand);
521 env.n_shift = env.params->maximum_shifts;
522 env.evaluate = env.params->evaluate != NULL ? env.params->evaluate : default_evaluate;
523 env.irg = get_irn_irg(irn);
525 R = value_to_condensed(&env, tv, &r);
526 inst = decompose_mul(&env, R, r, tv);
528 /* the paper suggests 70% here */
529 mul_costs = (env.evaluate(MUL, env.mode, tv) * 7 + 5) / 10;
530 if (evaluate_insn(&env, inst) <= mul_costs && !env.fail) {
532 env.blk = get_nodes_block(irn);
533 env.dbg = get_irn_dbg_info(irn);
534 env.shf_mode = find_unsigned_mode(env.mode);
535 if (env.shf_mode == NULL)
536 env.shf_mode = mode_Iu;
538 res = build_graph(&env, inst);
540 obstack_free(&env.obst, NULL);
544 /* Replace Muls with Shifts and Add/Subs. */
545 ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
548 ir_mode *mode = get_irn_mode(irn);
554 const ir_settings_arch_dep_t *params = be_get_backend_param()->dep_param;
556 /* If the architecture dependent optimizations were not initialized
557 or this optimization was not enabled. */
558 if (params == NULL || (opts & arch_dep_mul_to_shift) == 0)
562 if (!mode_is_int(mode))
565 /* we should never do the reverse transformations again
567 irg = get_irn_irg(irn);
568 add_irg_constraints(irg, IR_GRAPH_CONSTRAINT_ARCH_DEP);
570 left = get_binop_left(irn);
571 right = get_binop_right(irn);
575 /* Look, if one operand is a constant. */
576 if (is_Const(left)) {
577 tv = get_Const_tarval(left);
579 } else if (is_Const(right)) {
580 tv = get_Const_tarval(right);
584 /* multiplications with 0 are a special case which we leave for
585 * equivalent_node_Mul because the code here can't handle them */
586 if (tv == get_mode_null(mode))
590 res = do_decomposition(irn, operand, tv);
593 hook_arch_dep_replace_mul_with_shifts(irn);
602 * calculated the ld2 of a tarval if tarval is 2^n, else returns -1.
604 static int tv_ld2(ir_tarval *tv, int bits)
608 for (num = i = 0; i < bits; ++i) {
609 unsigned char v = get_tarval_sub_bits(tv, i);
614 for (j = 0; j < 8; ++j)
627 /* for shorter lines */
628 #define ABS(a) tarval_abs(a)
629 #define NEG(a) tarval_neg(a)
630 #define NOT(a) tarval_not(a)
631 #define SHL(a, b) tarval_shl(a, b)
632 #define SHR(a, b) tarval_shr(a, b)
633 #define ADD(a, b) tarval_add(a, b)
634 #define SUB(a, b) tarval_sub(a, b, NULL)
635 #define MUL(a, b) tarval_mul(a, b)
636 #define DIV(a, b) tarval_div(a, b)
637 #define MOD(a, b) tarval_mod(a, b)
638 #define CMP(a, b) tarval_cmp(a, b)
639 #define CNV(a, m) tarval_convert_to(a, m)
640 #define ONE(m) get_mode_one(m)
641 #define ZERO(m) get_mode_null(m)
643 /** The result of a the magic() function. */
645 ir_tarval *M; /**< magic number */
646 int s; /**< shift amount */
647 int need_add; /**< an additional add is needed */
648 int need_sub; /**< an additional sub is needed */
652 * Signed division by constant d: calculate the Magic multiplier M and the shift amount s
654 * see Hacker's Delight: 10-6 Integer Division by Constants: Incorporation into a Compiler
656 static struct ms magic(ir_tarval *d)
658 ir_mode *mode = get_tarval_mode(d);
659 ir_mode *u_mode = find_unsigned_mode(mode);
660 int bits = get_mode_size_bits(u_mode);
662 ir_tarval *ad, *anc, *delta, *q1, *r1, *q2, *r2, *t; /* unsigned */
663 ir_relation d_cmp, M_cmp;
665 ir_tarval *bits_minus_1, *two_bits_1;
669 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
671 /* we need overflow mode to work correctly */
672 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
675 bits_minus_1 = new_tarval_from_long(bits - 1, u_mode);
676 two_bits_1 = SHL(get_mode_one(u_mode), bits_minus_1);
678 ad = CNV(ABS(d), u_mode);
679 t = ADD(two_bits_1, SHR(CNV(d, u_mode), bits_minus_1));
680 anc = SUB(SUB(t, ONE(u_mode)), MOD(t, ad)); /* Absolute value of nc */
681 p = bits - 1; /* Init: p */
682 q1 = DIV(two_bits_1, anc); /* Init: q1 = 2^p/|nc| */
683 r1 = SUB(two_bits_1, MUL(q1, anc)); /* Init: r1 = rem(2^p, |nc|) */
684 q2 = DIV(two_bits_1, ad); /* Init: q2 = 2^p/|d| */
685 r2 = SUB(two_bits_1, MUL(q2, ad)); /* Init: r2 = rem(2^p, |d|) */
689 q1 = ADD(q1, q1); /* Update q1 = 2^p/|nc| */
690 r1 = ADD(r1, r1); /* Update r1 = rem(2^p, |nc|) */
692 if (CMP(r1, anc) & ir_relation_greater_equal) {
693 q1 = ADD(q1, ONE(u_mode));
697 q2 = ADD(q2, q2); /* Update q2 = 2^p/|d| */
698 r2 = ADD(r2, r2); /* Update r2 = rem(2^p, |d|) */
700 if (CMP(r2, ad) & ir_relation_greater_equal) {
701 q2 = ADD(q2, ONE(u_mode));
706 } while (CMP(q1, delta) & ir_relation_less || (CMP(q1, delta) & ir_relation_equal && CMP(r1, ZERO(u_mode)) & ir_relation_equal));
708 d_cmp = CMP(d, ZERO(mode));
710 if (d_cmp & ir_relation_greater_equal)
711 mag.M = ADD(CNV(q2, mode), ONE(mode));
713 mag.M = SUB(ZERO(mode), ADD(CNV(q2, mode), ONE(mode)));
715 M_cmp = CMP(mag.M, ZERO(mode));
719 /* need an add if d > 0 && M < 0 */
720 mag.need_add = d_cmp & ir_relation_greater && M_cmp & ir_relation_less;
722 /* need a sub if d < 0 && M > 0 */
723 mag.need_sub = d_cmp & ir_relation_less && M_cmp & ir_relation_greater;
725 tarval_set_integer_overflow_mode(rem);
730 /** The result of the magicu() function. */
732 ir_tarval *M; /**< magic add constant */
733 int s; /**< shift amount */
734 int need_add; /**< add indicator */
738 * Unsigned division by constant d: calculate the Magic multiplier M and the shift amount s
740 * see Hacker's Delight: 10-10 Integer Division by Constants: Incorporation into a Compiler (Unsigned)
742 static struct mu magicu(ir_tarval *d)
744 ir_mode *mode = get_tarval_mode(d);
745 int bits = get_mode_size_bits(mode);
747 ir_tarval *nc, *delta, *q1, *r1, *q2, *r2;
748 ir_tarval *bits_minus_1, *two_bits_1, *seven_ff;
752 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
754 /* we need overflow mode to work correctly */
755 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
757 bits_minus_1 = new_tarval_from_long(bits - 1, mode);
758 two_bits_1 = SHL(get_mode_one(mode), bits_minus_1);
759 seven_ff = SUB(two_bits_1, ONE(mode));
761 magu.need_add = 0; /* initialize the add indicator */
762 nc = SUB(NEG(ONE(mode)), MOD(NEG(d), d));
763 p = bits - 1; /* Init: p */
764 q1 = DIV(two_bits_1, nc); /* Init: q1 = 2^p/nc */
765 r1 = SUB(two_bits_1, MUL(q1, nc)); /* Init: r1 = rem(2^p, nc) */
766 q2 = DIV(seven_ff, d); /* Init: q2 = (2^p - 1)/d */
767 r2 = SUB(seven_ff, MUL(q2, d)); /* Init: r2 = rem(2^p - 1, d) */
771 if (CMP(r1, SUB(nc, r1)) & ir_relation_greater_equal) {
772 q1 = ADD(ADD(q1, q1), ONE(mode));
773 r1 = SUB(ADD(r1, r1), nc);
780 if (CMP(ADD(r2, ONE(mode)), SUB(d, r2)) & ir_relation_greater_equal) {
781 if (CMP(q2, seven_ff) & ir_relation_greater_equal)
784 q2 = ADD(ADD(q2, q2), ONE(mode));
785 r2 = SUB(ADD(ADD(r2, r2), ONE(mode)), d);
788 if (CMP(q2, two_bits_1) & ir_relation_greater_equal)
792 r2 = ADD(ADD(r2, r2), ONE(mode));
794 delta = SUB(SUB(d, ONE(mode)), r2);
795 } while (p < 2*bits &&
796 (CMP(q1, delta) & ir_relation_less || (CMP(q1, delta) & ir_relation_equal && CMP(r1, ZERO(mode)) & ir_relation_equal)));
798 magu.M = ADD(q2, ONE(mode)); /* Magic number */
799 magu.s = p - bits; /* and shift amount */
801 tarval_set_integer_overflow_mode(rem);
807 * Build the Mulh replacement code for n / tv.
809 * Note that 'div' might be a Mod operation as well
811 static ir_node *replace_div_by_mulh(ir_node *div, ir_tarval *tv)
813 dbg_info *dbg = get_irn_dbg_info(div);
814 ir_node *n = get_binop_left(div);
815 ir_node *block = get_nodes_block(div);
816 ir_mode *mode = get_irn_mode(n);
817 int bits = get_mode_size_bits(mode);
820 /* Beware: do not transform bad code */
821 if (is_Bad(n) || is_Bad(block))
824 if (mode_is_signed(mode)) {
825 ir_graph *irg = get_irn_irg(div);
826 struct ms mag = magic(tv);
828 /* generate the Mulh instruction */
829 ir_node *c = new_r_Const(irg, mag.M);
831 q = new_rd_Mulh(dbg, block, n, c, mode);
833 /* do we need an Add or Sub */
835 q = new_rd_Add(dbg, block, q, n, mode);
836 else if (mag.need_sub)
837 q = new_rd_Sub(dbg, block, q, n, mode);
839 /* Do we need the shift */
841 c = new_r_Const_long(irg, mode_Iu, mag.s);
842 q = new_rd_Shrs(dbg, block, q, c, mode);
846 c = new_r_Const_long(irg, mode_Iu, bits - 1);
847 t = new_rd_Shr(dbg, block, q, c, mode);
849 q = new_rd_Add(dbg, block, q, t, mode);
851 struct mu mag = magicu(tv);
852 ir_graph *irg = get_irn_irg(div);
854 /* generate the Mulh instruction */
855 ir_node *c = new_r_Const(irg, mag.M);
856 q = new_rd_Mulh(dbg, block, n, c, mode);
860 /* use the GM scheme */
861 ir_node *t = new_rd_Sub(dbg, block, n, q, mode);
863 c = new_r_Const(irg, get_mode_one(mode_Iu));
864 t = new_rd_Shr(dbg, block, t, c, mode);
866 t = new_rd_Add(dbg, block, t, q, mode);
868 c = new_r_Const_long(irg, mode_Iu, mag.s - 1);
869 q = new_rd_Shr(dbg, block, t, c, mode);
871 /* use the default scheme */
872 q = new_rd_Add(dbg, block, q, n, mode);
874 } else if (mag.s > 0) { /* default scheme, shift needed */
875 c = new_r_Const_long(irg, mode_Iu, mag.s);
876 q = new_rd_Shr(dbg, block, q, c, mode);
882 /* Replace Divs with Shifts and Add/Subs and Mulh. */
883 ir_node *arch_dep_replace_div_by_const(ir_node *irn)
885 const ir_settings_arch_dep_t *params = be_get_backend_param()->dep_param;
888 /* If the architecture dependent optimizations were not initialized
889 or this optimization was not enabled. */
890 if (params == NULL || (opts & arch_dep_div_by_const) == 0)
896 ir_node *c = get_Div_right(irn);
897 ir_node *block, *left;
908 tv = get_Const_tarval(c);
910 /* check for division by zero */
911 if (tarval_is_null(tv))
914 left = get_Div_left(irn);
915 mode = get_irn_mode(left);
917 /* can only handle integer Div's */
918 if (!mode_is_int(mode))
921 block = get_nodes_block(irn);
922 dbg = get_irn_dbg_info(irn);
924 bits = get_mode_size_bits(mode);
928 if (mode_is_signed(mode)) {
929 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
930 ntv = tarval_neg(tv);
940 if (k > 0) { /* division by 2^k or -2^k */
941 ir_graph *irg = get_irn_irg(irn);
942 if (mode_is_signed(mode)) {
944 ir_node *curr = left;
946 /* create the correction code for signed values only if there might be a remainder */
947 if (! get_Div_no_remainder(irn)) {
949 k_node = new_r_Const_long(irg, mode_Iu, k - 1);
950 curr = new_rd_Shrs(dbg, block, left, k_node, mode);
953 k_node = new_r_Const_long(irg, mode_Iu, bits - k);
954 curr = new_rd_Shr(dbg, block, curr, k_node, mode);
955 /* curr is now 2^(k-1) in case left < 0
956 * or 0 in case left >= 0
958 * For an example, where this fixup is necessary consider -3 / 2,
959 * which should compute to -1,
960 * but simply shifting right by one computes -2.
963 curr = new_rd_Add(dbg, block, left, curr, mode);
966 k_node = new_r_Const_long(irg, mode_Iu, k);
967 res = new_rd_Shrs(dbg, block, curr, k_node, mode);
969 if (n_flag) { /* negate the result */
970 k_node = new_r_Const(irg, get_mode_null(mode));
971 res = new_rd_Sub(dbg, block, k_node, res, mode);
973 } else { /* unsigned case */
976 k_node = new_r_Const_long(irg, mode_Iu, k);
977 res = new_rd_Shr(dbg, block, left, k_node, mode);
981 if (allow_Mulh(params, mode))
982 res = replace_div_by_mulh(irn, tv);
983 } else { /* k == 0 i.e. division by 1 */
988 hook_arch_dep_replace_division_by_const(irn);
993 /* Replace Mods with Shifts and Add/Subs and Mulh. */
994 ir_node *arch_dep_replace_mod_by_const(ir_node *irn)
996 const ir_settings_arch_dep_t *params = be_get_backend_param()->dep_param;
999 /* If the architecture dependent optimizations were not initialized
1000 or this optimization was not enabled. */
1001 if (params == NULL || (opts & arch_dep_mod_by_const) == 0)
1005 ir_node *c = get_Mod_right(irn);
1006 ir_node *block, *left;
1008 ir_tarval *tv, *ntv;
1016 tv = get_Const_tarval(c);
1018 /* check for division by zero */
1019 if (tarval_is_null(tv))
1022 left = get_Mod_left(irn);
1023 mode = get_irn_mode(left);
1024 block = get_nodes_block(irn);
1025 dbg = get_irn_dbg_info(irn);
1026 bits = get_mode_size_bits(mode);
1030 if (mode_is_signed(mode)) {
1031 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1032 ntv = tarval_neg(tv);
1040 /* k == 0 i.e. modulo by 1 */
1042 ir_graph *irg = get_irn_irg(irn);
1044 res = new_r_Const(irg, get_mode_null(mode));
1047 ir_graph *irg = get_irn_irg(irn);
1048 /* division by 2^k or -2^k:
1049 * we use "modulus" here, so x % y == x % -y that's why is no difference between the case 2^k and -2^k
1051 if (mode_is_signed(mode)) {
1053 ir_node *curr = left;
1056 k_node = new_r_Const_long(irg, mode_Iu, k - 1);
1057 curr = new_rd_Shrs(dbg, block, left, k_node, mode);
1060 k_node = new_r_Const_long(irg, mode_Iu, bits - k);
1061 curr = new_rd_Shr(dbg, block, curr, k_node, mode);
1063 curr = new_rd_Add(dbg, block, left, curr, mode);
1065 k_node = new_r_Const_long(irg, mode, (-1) << k);
1066 curr = new_rd_And(dbg, block, curr, k_node, mode);
1068 res = new_rd_Sub(dbg, block, left, curr, mode);
1069 } else { /* unsigned case */
1072 k_node = new_r_Const_long(irg, mode, (1 << k) - 1);
1073 res = new_rd_And(dbg, block, left, k_node, mode);
1076 /* other constant */
1077 if (allow_Mulh(params, mode)) {
1078 res = replace_div_by_mulh(irn, tv);
1080 res = new_rd_Mul(dbg, block, res, c, mode);
1082 /* res = arch_dep_mul_to_shift(res); */
1084 res = new_rd_Sub(dbg, block, left, res, mode);
1090 hook_arch_dep_replace_division_by_const(irn);