2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Machine dependent Firm optimizations.
24 * @author Sebastian Hack, Michael Beck
27 * Implements "Strength Reduction of Multiplications by Integer Constants"
29 * Implements Division and Modulo by Consts from "Hackers Delight",
37 #include "irgraph_t.h"
44 #include "dbginfo_t.h"
45 #include "iropt_dbg.h"
54 /** The bit mask, which optimizations to apply. */
55 static arch_dep_opts_t opts;
57 void arch_dep_set_opts(arch_dep_opts_t the_opts)
62 /** check, whether a mode allows a Mulh instruction. */
63 static int allow_Mulh(const ir_settings_arch_dep_t *params, ir_mode *mode)
65 if (get_mode_size_bits(mode) > params->max_bits_for_mulh)
67 return (mode_is_signed(mode) && params->allow_mulhs) || (!mode_is_signed(mode) && params->allow_mulhu);
73 typedef struct instruction instruction;
75 insn_kind kind; /**< the instruction kind */
76 instruction *in[2]; /**< the ins */
77 unsigned shift_count; /**< shift count for LEA and SHIFT */
78 ir_node *irn; /**< the generated node for this instruction if any. */
79 int costs; /**< the costs for this instruction */
83 * The environment for the strength reduction of multiplications.
85 typedef struct mul_env {
86 struct obstack obst; /**< an obstack for local space. */
87 const ir_settings_arch_dep_t *params;
88 ir_mode *mode; /**< the mode of the multiplication constant */
89 unsigned bits; /**< number of bits in the mode */
90 unsigned max_S; /**< the maximum LEA shift value. */
91 instruction *root; /**< the root of the instruction tree */
92 ir_node *op; /**< the operand that is multiplied */
93 ir_node *blk; /**< the block where the new graph is built */
95 dbg_info *dbg; /**< the debug info for the new graph. */
96 ir_mode *shf_mode; /**< the (unsigned) mode for the shift constants */
97 int fail; /**< set to 1 if the instruction sequence fails the constraints */
98 int n_shift; /**< maximum number of allowed shift instructions */
100 evaluate_costs_func evaluate; /**< the evaluate callback */
104 * Some kind of default evaluator. Return the cost of
107 static int default_evaluate(insn_kind kind, const ir_mode *mode, ir_tarval *tv)
118 * emit a LEA (or an Add) instruction
120 static instruction *emit_LEA(mul_env *env, instruction *a, instruction *b, unsigned shift)
122 instruction *res = OALLOC(&env->obst, instruction);
123 res->kind = shift > 0 ? LEA : ADD;
126 res->shift_count = shift;
133 * emit a SHIFT (or an Add or a Zero) instruction
135 static instruction *emit_SHIFT(mul_env *env, instruction *a, unsigned shift)
137 instruction *res = OALLOC(&env->obst, instruction);
138 if (shift == env->bits) {
139 /* a 2^bits with bits resolution is a zero */
143 res->shift_count = 0;
144 } else if (shift != 1) {
148 res->shift_count = shift;
153 res->shift_count = 0;
161 * emit a SUB instruction
163 static instruction *emit_SUB(mul_env *env, instruction *a, instruction *b)
165 instruction *res = OALLOC(&env->obst, instruction);
169 res->shift_count = 0;
176 * emit the ROOT instruction
178 static instruction *emit_ROOT(mul_env *env, ir_node *root_op)
180 instruction *res = OALLOC(&env->obst, instruction);
184 res->shift_count = 0;
192 * Returns the condensed representation of the tarval tv
194 static unsigned char *value_to_condensed(mul_env *env, ir_tarval *tv, int *pr)
196 ir_mode *mode = get_tarval_mode(tv);
197 int bits = get_mode_size_bits(mode);
198 char *bitstr = get_tarval_bitpattern(tv);
200 unsigned char *R = (unsigned char*)obstack_alloc(&env->obst, bits);
203 for (i = 0; bitstr[i] != '\0'; ++i) {
204 if (bitstr[i] == '1') {
217 * Calculate the gain when using the generalized complementary technique
219 static int calculate_gain(unsigned char *R, int r)
225 /* the gain for r == 1 */
227 for (i = 2; i < r; ++i) {
228 /* calculate the gain for r from the gain for r-1 */
229 gain += 2 - R[i - 1];
231 if (gain > max_gain) {
240 * Calculates the condensed complement of a given (R,r) tuple
242 static unsigned char *complement_condensed(mul_env *env, unsigned char *R, int r, int gain, int *prs)
244 unsigned char *value = (unsigned char*)obstack_alloc(&env->obst, env->bits);
248 memset(value, 0, env->bits);
251 for (i = 0; i < gain; ++i) {
256 /* negate and propagate 1 */
258 for (i = 0; i <= j; ++i) {
259 unsigned char v = !value[i];
265 /* condense it again */
268 for (i = 0; i <= j; ++i) {
281 * creates a tarval from a condensed representation.
283 static ir_tarval *condensed_to_value(mul_env *env, unsigned char *R, int r)
289 tv = get_mode_one(env->mode);
291 for (i = 0; i < r; ++i) {
294 ir_tarval *t = new_tarval_from_long(j, mode_Iu);
295 tv = tarval_shl(tv, t);
297 res = res ? tarval_add(res, tv) : tv;
303 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, ir_tarval *N);
306 * handle simple cases with up-to 2 bits set
308 static instruction *decompose_simple_cases(mul_env *env, unsigned char *R, int r, ir_tarval *N)
310 instruction *ins, *ins2;
314 return emit_SHIFT(env, env->root, R[0]);
319 if (R[1] <= env->max_S) {
320 ins = emit_LEA(env, ins, ins, R[1]);
322 ins = emit_SHIFT(env, ins, R[0]);
327 ins = emit_SHIFT(env, ins, R[0]);
330 ins2 = emit_SHIFT(env, env->root, R[0] + R[1]);
331 return emit_LEA(env, ins, ins2, 0);
336 * Main decompose driver.
338 static instruction *decompose_mul(mul_env *env, unsigned char *R, int r, ir_tarval *N)
344 return decompose_simple_cases(env, R, r, N);
346 if (env->params->also_use_subs) {
347 gain = calculate_gain(R, r);
349 instruction *instr1, *instr2;
350 unsigned char *R1, *R2;
353 R1 = complement_condensed(env, R, r, gain, &r1);
355 R2 = (unsigned char*)obstack_alloc(&env->obst, r2);
358 for (i = 0; i < gain; ++i) {
365 /* Two identical bits: normalize */
370 for (i = gain + 1; i < r; ++i) {
374 instr1 = decompose_mul(env, R1, r1, NULL);
375 instr2 = decompose_mul(env, R2, r2, NULL);
376 return emit_SUB(env, instr2, instr1);
381 N = condensed_to_value(env, R, r);
383 for (i = env->max_S; i > 0; --i) {
384 ir_tarval *div_res, *mod_res;
385 ir_tarval *tv = new_tarval_from_long((1 << i) + 1, env->mode);
387 div_res = tarval_divmod(N, tv, &mod_res);
388 if (mod_res == get_mode_null(env->mode)) {
392 Rs = value_to_condensed(env, div_res, &rs);
394 instruction *N1 = decompose_mul(env, Rs, rs, div_res);
395 return emit_LEA(env, N1, N1, i);
399 return basic_decompose_mul(env, R, r, N);
402 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
405 * basic decomposition routine
407 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, ir_tarval *N)
412 if (R[0] == 0) { /* Case 1 */
413 t = R[1] > IMAX(env->max_S, R[1]);
415 Ns = decompose_mul(env, &R[1], r - 1, N);
416 return emit_LEA(env, env->root, Ns, t);
417 } else if (R[0] <= env->max_S) { /* Case 2 */
420 Ns = decompose_mul(env, &R[1], r - 1, N);
421 return emit_LEA(env, Ns, env->root, t);
425 Ns = decompose_mul(env, R, r, N);
426 return emit_SHIFT(env, Ns, t);
431 * recursive build the graph form the instructions.
433 * @param env the environment
434 * @param inst the instruction
436 static ir_node *build_graph(mul_env *env, instruction *inst)
439 ir_graph *irg = env->irg;
444 switch (inst->kind) {
446 l = build_graph(env, inst->in[0]);
447 r = build_graph(env, inst->in[1]);
448 c = new_r_Const_long(irg, env->shf_mode, inst->shift_count);
449 r = new_rd_Shl(env->dbg, env->blk, r, c, env->mode);
450 return inst->irn = new_rd_Add(env->dbg, env->blk, l, r, env->mode);
452 l = build_graph(env, inst->in[0]);
453 c = new_r_Const_long(irg, env->shf_mode, inst->shift_count);
454 return inst->irn = new_rd_Shl(env->dbg, env->blk, l, c, env->mode);
456 l = build_graph(env, inst->in[0]);
457 r = build_graph(env, inst->in[1]);
458 return inst->irn = new_rd_Sub(env->dbg, env->blk, l, r, env->mode);
460 l = build_graph(env, inst->in[0]);
461 r = build_graph(env, inst->in[1]);
462 return inst->irn = new_rd_Add(env->dbg, env->blk, l, r, env->mode);
464 return inst->irn = new_r_Const(irg, get_mode_null(env->mode));
466 panic("Unsupported instruction kind");
471 * Calculate the costs for the given instruction sequence.
472 * Note that additional costs due to higher register pressure are NOT evaluated yet
474 static int evaluate_insn(mul_env *env, instruction *inst)
478 if (inst->costs >= 0) {
479 /* was already evaluated */
483 switch (inst->kind) {
487 costs = evaluate_insn(env, inst->in[0]);
488 costs += evaluate_insn(env, inst->in[1]);
489 costs += env->evaluate(inst->kind, env->mode, NULL);
493 if (inst->shift_count > env->params->highest_shift_amount)
495 if (env->n_shift <= 0)
499 costs = evaluate_insn(env, inst->in[0]);
500 costs += env->evaluate(inst->kind, env->mode, NULL);
504 inst->costs = costs = env->evaluate(inst->kind, env->mode, NULL);
510 panic("Unsupported instruction kind");
514 * Evaluate the replacement instructions and build a new graph
515 * if faster than the Mul.
516 * Returns the root of the new graph then or irn otherwise.
518 * @param irn the Mul operation
519 * @param operand the multiplication operand
520 * @param tv the multiplication constant
522 * @return the new graph
524 static ir_node *do_decomposition(ir_node *irn, ir_node *operand, ir_tarval *tv)
533 obstack_init(&env.obst);
534 env.params = be_get_backend_param()->dep_param;
535 env.mode = get_tarval_mode(tv);
536 env.bits = (unsigned)get_mode_size_bits(env.mode);
538 env.root = emit_ROOT(&env, operand);
540 env.n_shift = env.params->maximum_shifts;
541 env.evaluate = env.params->evaluate != NULL ? env.params->evaluate : default_evaluate;
542 env.irg = get_irn_irg(irn);
544 R = value_to_condensed(&env, tv, &r);
545 inst = decompose_mul(&env, R, r, tv);
547 /* the paper suggests 70% here */
548 mul_costs = (env.evaluate(MUL, env.mode, tv) * 7 + 5) / 10;
549 if (evaluate_insn(&env, inst) <= mul_costs && !env.fail) {
551 env.blk = get_nodes_block(irn);
552 env.dbg = get_irn_dbg_info(irn);
553 env.shf_mode = find_unsigned_mode(env.mode);
554 if (env.shf_mode == NULL)
555 env.shf_mode = mode_Iu;
557 res = build_graph(&env, inst);
559 obstack_free(&env.obst, NULL);
563 /* Replace Muls with Shifts and Add/Subs. */
564 ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
567 ir_mode *mode = get_irn_mode(irn);
573 const ir_settings_arch_dep_t *params = be_get_backend_param()->dep_param;
575 /* If the architecture dependent optimizations were not initialized
576 or this optimization was not enabled. */
577 if (params == NULL || (opts & arch_dep_mul_to_shift) == 0)
581 if (!mode_is_int(mode))
584 /* we should never do the reverse transformations again
586 irg = get_irn_irg(irn);
587 set_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP);
589 left = get_binop_left(irn);
590 right = get_binop_right(irn);
594 /* Look, if one operand is a constant. */
595 if (is_Const(left)) {
596 tv = get_Const_tarval(left);
598 } else if (is_Const(right)) {
599 tv = get_Const_tarval(right);
603 /* multiplications with 0 are a special case which we leave for
604 * equivalent_node_Mul because the code here can't handle them */
605 if (tv == get_mode_null(mode))
609 res = do_decomposition(irn, operand, tv);
612 hook_arch_dep_replace_mul_with_shifts(irn);
621 * calculated the ld2 of a tarval if tarval is 2^n, else returns -1.
623 static int tv_ld2(ir_tarval *tv, int bits)
627 for (num = i = 0; i < bits; ++i) {
628 unsigned char v = get_tarval_sub_bits(tv, i);
633 for (j = 0; j < 8; ++j)
646 /* for shorter lines */
647 #define ABS(a) tarval_abs(a)
648 #define NEG(a) tarval_neg(a)
649 #define NOT(a) tarval_not(a)
650 #define SHL(a, b) tarval_shl(a, b)
651 #define SHR(a, b) tarval_shr(a, b)
652 #define ADD(a, b) tarval_add(a, b)
653 #define SUB(a, b) tarval_sub(a, b, NULL)
654 #define MUL(a, b) tarval_mul(a, b)
655 #define DIV(a, b) tarval_div(a, b)
656 #define MOD(a, b) tarval_mod(a, b)
657 #define CMP(a, b) tarval_cmp(a, b)
658 #define CNV(a, m) tarval_convert_to(a, m)
659 #define ONE(m) get_mode_one(m)
660 #define ZERO(m) get_mode_null(m)
662 /** The result of a the magic() function. */
664 ir_tarval *M; /**< magic number */
665 int s; /**< shift amount */
666 int need_add; /**< an additional add is needed */
667 int need_sub; /**< an additional sub is needed */
671 * Signed division by constant d: calculate the Magic multiplier M and the shift amount s
673 * see Hacker's Delight: 10-6 Integer Division by Constants: Incorporation into a Compiler
675 static struct ms magic(ir_tarval *d)
677 ir_mode *mode = get_tarval_mode(d);
678 ir_mode *u_mode = find_unsigned_mode(mode);
679 int bits = get_mode_size_bits(u_mode);
681 ir_tarval *ad, *anc, *delta, *q1, *r1, *q2, *r2, *t; /* unsigned */
682 ir_relation d_cmp, M_cmp;
684 ir_tarval *bits_minus_1, *two_bits_1;
688 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
690 /* we need overflow mode to work correctly */
691 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
694 bits_minus_1 = new_tarval_from_long(bits - 1, u_mode);
695 two_bits_1 = SHL(get_mode_one(u_mode), bits_minus_1);
697 ad = CNV(ABS(d), u_mode);
698 t = ADD(two_bits_1, SHR(CNV(d, u_mode), bits_minus_1));
699 anc = SUB(SUB(t, ONE(u_mode)), MOD(t, ad)); /* Absolute value of nc */
700 p = bits - 1; /* Init: p */
701 q1 = DIV(two_bits_1, anc); /* Init: q1 = 2^p/|nc| */
702 r1 = SUB(two_bits_1, MUL(q1, anc)); /* Init: r1 = rem(2^p, |nc|) */
703 q2 = DIV(two_bits_1, ad); /* Init: q2 = 2^p/|d| */
704 r2 = SUB(two_bits_1, MUL(q2, ad)); /* Init: r2 = rem(2^p, |d|) */
708 q1 = ADD(q1, q1); /* Update q1 = 2^p/|nc| */
709 r1 = ADD(r1, r1); /* Update r1 = rem(2^p, |nc|) */
711 if (CMP(r1, anc) & ir_relation_greater_equal) {
712 q1 = ADD(q1, ONE(u_mode));
716 q2 = ADD(q2, q2); /* Update q2 = 2^p/|d| */
717 r2 = ADD(r2, r2); /* Update r2 = rem(2^p, |d|) */
719 if (CMP(r2, ad) & ir_relation_greater_equal) {
720 q2 = ADD(q2, ONE(u_mode));
725 } while (CMP(q1, delta) & ir_relation_less || (CMP(q1, delta) & ir_relation_equal && CMP(r1, ZERO(u_mode)) & ir_relation_equal));
727 d_cmp = CMP(d, ZERO(mode));
729 if (d_cmp & ir_relation_greater_equal)
730 mag.M = ADD(CNV(q2, mode), ONE(mode));
732 mag.M = SUB(ZERO(mode), ADD(CNV(q2, mode), ONE(mode)));
734 M_cmp = CMP(mag.M, ZERO(mode));
738 /* need an add if d > 0 && M < 0 */
739 mag.need_add = d_cmp & ir_relation_greater && M_cmp & ir_relation_less;
741 /* need a sub if d < 0 && M > 0 */
742 mag.need_sub = d_cmp & ir_relation_less && M_cmp & ir_relation_greater;
744 tarval_set_integer_overflow_mode(rem);
749 /** The result of the magicu() function. */
751 ir_tarval *M; /**< magic add constant */
752 int s; /**< shift amount */
753 int need_add; /**< add indicator */
757 * Unsigned division by constant d: calculate the Magic multiplier M and the shift amount s
759 * see Hacker's Delight: 10-10 Integer Division by Constants: Incorporation into a Compiler (Unsigned)
761 static struct mu magicu(ir_tarval *d)
763 ir_mode *mode = get_tarval_mode(d);
764 int bits = get_mode_size_bits(mode);
766 ir_tarval *nc, *delta, *q1, *r1, *q2, *r2;
767 ir_tarval *bits_minus_1, *two_bits_1, *seven_ff;
771 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
773 /* we need overflow mode to work correctly */
774 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
776 bits_minus_1 = new_tarval_from_long(bits - 1, mode);
777 two_bits_1 = SHL(get_mode_one(mode), bits_minus_1);
778 seven_ff = SUB(two_bits_1, ONE(mode));
780 magu.need_add = 0; /* initialize the add indicator */
781 nc = SUB(NEG(ONE(mode)), MOD(NEG(d), d));
782 p = bits - 1; /* Init: p */
783 q1 = DIV(two_bits_1, nc); /* Init: q1 = 2^p/nc */
784 r1 = SUB(two_bits_1, MUL(q1, nc)); /* Init: r1 = rem(2^p, nc) */
785 q2 = DIV(seven_ff, d); /* Init: q2 = (2^p - 1)/d */
786 r2 = SUB(seven_ff, MUL(q2, d)); /* Init: r2 = rem(2^p - 1, d) */
790 if (CMP(r1, SUB(nc, r1)) & ir_relation_greater_equal) {
791 q1 = ADD(ADD(q1, q1), ONE(mode));
792 r1 = SUB(ADD(r1, r1), nc);
799 if (CMP(ADD(r2, ONE(mode)), SUB(d, r2)) & ir_relation_greater_equal) {
800 if (CMP(q2, seven_ff) & ir_relation_greater_equal)
803 q2 = ADD(ADD(q2, q2), ONE(mode));
804 r2 = SUB(ADD(ADD(r2, r2), ONE(mode)), d);
807 if (CMP(q2, two_bits_1) & ir_relation_greater_equal)
811 r2 = ADD(ADD(r2, r2), ONE(mode));
813 delta = SUB(SUB(d, ONE(mode)), r2);
814 } while (p < 2*bits &&
815 (CMP(q1, delta) & ir_relation_less || (CMP(q1, delta) & ir_relation_equal && CMP(r1, ZERO(mode)) & ir_relation_equal)));
817 magu.M = ADD(q2, ONE(mode)); /* Magic number */
818 magu.s = p - bits; /* and shift amount */
820 tarval_set_integer_overflow_mode(rem);
826 * Build the Mulh replacement code for n / tv.
828 * Note that 'div' might be a Mod operation as well
830 static ir_node *replace_div_by_mulh(ir_node *div, ir_tarval *tv)
832 dbg_info *dbg = get_irn_dbg_info(div);
833 ir_node *n = get_binop_left(div);
834 ir_node *block = get_irn_n(div, -1);
835 ir_mode *mode = get_irn_mode(n);
836 int bits = get_mode_size_bits(mode);
839 /* Beware: do not transform bad code */
840 if (is_Bad(n) || is_Bad(block))
843 if (mode_is_signed(mode)) {
844 ir_graph *irg = get_irn_irg(div);
845 struct ms mag = magic(tv);
847 /* generate the Mulh instruction */
848 ir_node *c = new_r_Const(irg, mag.M);
850 q = new_rd_Mulh(dbg, block, n, c, mode);
852 /* do we need an Add or Sub */
854 q = new_rd_Add(dbg, block, q, n, mode);
855 else if (mag.need_sub)
856 q = new_rd_Sub(dbg, block, q, n, mode);
858 /* Do we need the shift */
860 c = new_r_Const_long(irg, mode_Iu, mag.s);
861 q = new_rd_Shrs(dbg, block, q, c, mode);
865 c = new_r_Const_long(irg, mode_Iu, bits - 1);
866 t = new_rd_Shr(dbg, block, q, c, mode);
868 q = new_rd_Add(dbg, block, q, t, mode);
870 struct mu mag = magicu(tv);
871 ir_graph *irg = get_irn_irg(div);
873 /* generate the Mulh instruction */
874 ir_node *c = new_r_Const(irg, mag.M);
875 q = new_rd_Mulh(dbg, block, n, c, mode);
879 /* use the GM scheme */
880 ir_node *t = new_rd_Sub(dbg, block, n, q, mode);
882 c = new_r_Const(irg, get_mode_one(mode_Iu));
883 t = new_rd_Shr(dbg, block, t, c, mode);
885 t = new_rd_Add(dbg, block, t, q, mode);
887 c = new_r_Const_long(irg, mode_Iu, mag.s - 1);
888 q = new_rd_Shr(dbg, block, t, c, mode);
890 /* use the default scheme */
891 q = new_rd_Add(dbg, block, q, n, mode);
893 } else if (mag.s > 0) { /* default scheme, shift needed */
894 c = new_r_Const_long(irg, mode_Iu, mag.s);
895 q = new_rd_Shr(dbg, block, q, c, mode);
901 /* Replace Divs with Shifts and Add/Subs and Mulh. */
902 ir_node *arch_dep_replace_div_by_const(ir_node *irn)
904 const ir_settings_arch_dep_t *params = be_get_backend_param()->dep_param;
907 /* If the architecture dependent optimizations were not initialized
908 or this optimization was not enabled. */
909 if (params == NULL || (opts & arch_dep_div_by_const) == 0)
915 ir_node *c = get_Div_right(irn);
916 ir_node *block, *left;
927 tv = get_Const_tarval(c);
929 /* check for division by zero */
930 if (tarval_is_null(tv))
933 left = get_Div_left(irn);
934 mode = get_irn_mode(left);
936 /* can only handle integer Div's */
937 if (!mode_is_int(mode))
940 block = get_irn_n(irn, -1);
941 dbg = get_irn_dbg_info(irn);
943 bits = get_mode_size_bits(mode);
947 if (mode_is_signed(mode)) {
948 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
949 ntv = tarval_neg(tv);
959 if (k > 0) { /* division by 2^k or -2^k */
960 ir_graph *irg = get_irn_irg(irn);
961 if (mode_is_signed(mode)) {
963 ir_node *curr = left;
965 /* create the correction code for signed values only if there might be a remainder */
966 if (! get_Div_no_remainder(irn)) {
968 k_node = new_r_Const_long(irg, mode_Iu, k - 1);
969 curr = new_rd_Shrs(dbg, block, left, k_node, mode);
972 k_node = new_r_Const_long(irg, mode_Iu, bits - k);
973 curr = new_rd_Shr(dbg, block, curr, k_node, mode);
974 /* curr is now 2^(k-1) in case left < 0
975 * or 0 in case left >= 0
977 * For an example, where this fixup is necessary consider -3 / 2,
978 * which should compute to -1,
979 * but simply shifting right by one computes -2.
982 curr = new_rd_Add(dbg, block, left, curr, mode);
985 k_node = new_r_Const_long(irg, mode_Iu, k);
986 res = new_rd_Shrs(dbg, block, curr, k_node, mode);
988 if (n_flag) { /* negate the result */
989 k_node = new_r_Const(irg, get_mode_null(mode));
990 res = new_rd_Sub(dbg, block, k_node, res, mode);
992 } else { /* unsigned case */
995 k_node = new_r_Const_long(irg, mode_Iu, k);
996 res = new_rd_Shr(dbg, block, left, k_node, mode);
1000 if (allow_Mulh(params, mode))
1001 res = replace_div_by_mulh(irn, tv);
1002 } else { /* k == 0 i.e. division by 1 */
1007 hook_arch_dep_replace_division_by_const(irn);
1012 /* Replace Mods with Shifts and Add/Subs and Mulh. */
1013 ir_node *arch_dep_replace_mod_by_const(ir_node *irn)
1015 const ir_settings_arch_dep_t *params = be_get_backend_param()->dep_param;
1018 /* If the architecture dependent optimizations were not initialized
1019 or this optimization was not enabled. */
1020 if (params == NULL || (opts & arch_dep_mod_by_const) == 0)
1024 ir_node *c = get_Mod_right(irn);
1025 ir_node *block, *left;
1027 ir_tarval *tv, *ntv;
1035 tv = get_Const_tarval(c);
1037 /* check for division by zero */
1038 if (tarval_is_null(tv))
1041 left = get_Mod_left(irn);
1042 mode = get_irn_mode(left);
1043 block = get_irn_n(irn, -1);
1044 dbg = get_irn_dbg_info(irn);
1045 bits = get_mode_size_bits(mode);
1049 if (mode_is_signed(mode)) {
1050 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1051 ntv = tarval_neg(tv);
1059 /* k == 0 i.e. modulo by 1 */
1061 ir_graph *irg = get_irn_irg(irn);
1063 res = new_r_Const(irg, get_mode_null(mode));
1066 ir_graph *irg = get_irn_irg(irn);
1067 /* division by 2^k or -2^k:
1068 * we use "modulus" here, so x % y == x % -y that's why is no difference between the case 2^k and -2^k
1070 if (mode_is_signed(mode)) {
1072 ir_node *curr = left;
1075 k_node = new_r_Const_long(irg, mode_Iu, k - 1);
1076 curr = new_rd_Shrs(dbg, block, left, k_node, mode);
1079 k_node = new_r_Const_long(irg, mode_Iu, bits - k);
1080 curr = new_rd_Shr(dbg, block, curr, k_node, mode);
1082 curr = new_rd_Add(dbg, block, left, curr, mode);
1084 k_node = new_r_Const_long(irg, mode, (-1) << k);
1085 curr = new_rd_And(dbg, block, curr, k_node, mode);
1087 res = new_rd_Sub(dbg, block, left, curr, mode);
1088 } else { /* unsigned case */
1091 k_node = new_r_Const_long(irg, mode, (1 << k) - 1);
1092 res = new_rd_And(dbg, block, left, k_node, mode);
1095 /* other constant */
1096 if (allow_Mulh(params, mode)) {
1097 res = replace_div_by_mulh(irn, tv);
1099 res = new_rd_Mul(dbg, block, res, c, mode);
1101 /* res = arch_dep_mul_to_shift(res); */
1103 res = new_rd_Sub(dbg, block, left, res, mode);
1109 hook_arch_dep_replace_division_by_const(irn);