2 * Copyright (C) 1995-2008 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 "Strenght Reduction of Multiplications by Integer Constants" by Youfeng Wu.
28 * Implements Division and Modulo by Consts from "Hackers Delight",
39 #include "irgraph_t.h"
46 #include "dbginfo_t.h"
47 #include "iropt_dbg.h"
59 /* when we need verifying */
61 # define IRN_VRFY_IRG(res, irg)
63 # define IRN_VRFY_IRG(res, irg) irn_vrfy_irg(res, irg)
66 /** The params got from the factory in arch_dep_init(...). */
67 static const ir_settings_arch_dep_t *params = NULL;
69 /** The bit mask, which optimizations to apply. */
70 static arch_dep_opts_t opts;
72 void arch_dep_init(arch_dep_params_factory_t factory) {
79 void arch_dep_set_opts(arch_dep_opts_t the_opts) {
83 /** check, whether a mode allows a Mulh instruction. */
84 static int allow_Mulh(ir_mode *mode) {
85 if (get_mode_size_bits(mode) > params->max_bits_for_mulh)
87 return (mode_is_signed(mode) && params->allow_mulhs) || (!mode_is_signed(mode) && params->allow_mulhu);
93 typedef struct instruction instruction;
95 insn_kind kind; /**< the instruction kind */
96 instruction *in[2]; /**< the ins */
97 unsigned shift_count; /**< shift count for LEA and SHIFT */
98 ir_node *irn; /**< the generated node for this instruction if any. */
99 int costs; /**< the costs for this instruction */
103 * The environment for the strength reduction of multiplications.
105 typedef struct _mul_env {
106 struct obstack obst; /**< an obstack for local space. */
107 ir_mode *mode; /**< the mode of the multiplication constant */
108 unsigned bits; /**< number of bits in the mode */
109 unsigned max_S; /**< the maximum LEA shift value. */
110 instruction *root; /**< the root of the instruction tree */
111 ir_node *op; /**< the operand that is multiplied */
112 ir_node *blk; /**< the block where the new graph is built */
113 dbg_info *dbg; /**< the debug info for the new graph. */
114 ir_mode *shf_mode; /**< the (unsigned) mode for the shift constants */
115 int fail; /**< set to 1 if the instruction sequence fails the constraints */
116 int n_shift; /**< maximum number of allowed shift instructions */
118 evaluate_costs_func evaluate; /**< the evaluate callback */
122 * Some kind of default evaluator. Return the cost of
125 static int default_evaluate(insn_kind kind, tarval *tv) {
134 * emit a LEA (or an Add) instruction
136 static instruction *emit_LEA(mul_env *env, instruction *a, instruction *b, unsigned shift) {
137 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
138 res->kind = shift > 0 ? LEA : ADD;
141 res->shift_count = shift;
148 * emit a SHIFT (or an Add or a Zero) instruction
150 static instruction *emit_SHIFT(mul_env *env, instruction *a, unsigned shift) {
151 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
152 if (shift == env->bits) {
153 /* a 2^bits with bits resolution is a zero */
157 res->shift_count = 0;
158 } else if (shift != 1) {
162 res->shift_count = shift;
167 res->shift_count = 0;
175 * emit a SUB instruction
177 static instruction *emit_SUB(mul_env *env, instruction *a, instruction *b) {
178 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
182 res->shift_count = 0;
189 * emit the ROOT instruction
191 static instruction *emit_ROOT(mul_env *env, ir_node *root_op) {
192 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
196 res->shift_count = 0;
204 * Returns the condensed representation of the tarval tv
206 static unsigned char *value_to_condensed(mul_env *env, tarval *tv, int *pr) {
207 ir_mode *mode = get_tarval_mode(tv);
208 int bits = get_mode_size_bits(mode);
209 char *bitstr = get_tarval_bitpattern(tv);
211 unsigned char *R = obstack_alloc(&env->obst, bits);
214 for (i = 0; bitstr[i] != '\0'; ++i) {
215 if (bitstr[i] == '1') {
228 * Calculate the gain when using the generalized complementary technique
230 static int calculate_gain(unsigned char *R, int r) {
235 /* the gain for r == 1 */
237 for (i = 2; i < r; ++i) {
238 /* calculate the gain for r from the gain for r-1 */
239 gain += 2 - R[i - 1];
241 if (gain > max_gain) {
250 * Calculates the condensed complement of a given (R,r) tuple
252 static unsigned char *complement_condensed(mul_env *env, unsigned char *R, int r, int gain, int *prs) {
253 unsigned char *value = obstack_alloc(&env->obst, env->bits);
257 memset(value, 0, env->bits);
260 for (i = 0; i < gain; ++i) {
265 /* negate and propagate 1 */
267 for (i = 0; i <= j; ++i) {
268 unsigned char v = !value[i];
274 /* condense it again */
277 for (i = 0; i <= j; ++i) {
290 * creates a tarval from a condensed representation.
292 static tarval *condensed_to_value(mul_env *env, unsigned char *R, int r) {
297 tv = get_mode_one(env->mode);
299 for (i = 0; i < r; ++i) {
302 tarval *t = new_tarval_from_long(j, mode_Iu);
303 tv = tarval_shl(tv, t);
305 res = res ? tarval_add(res, tv) : tv;
311 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N);
314 * handle simple cases with up-to 2 bits set
316 static instruction *decompose_simple_cases(mul_env *env, unsigned char *R, int r, tarval *N) {
317 instruction *ins, *ins2;
321 return emit_SHIFT(env, env->root, R[0]);
326 if (R[1] <= env->max_S) {
327 ins = emit_LEA(env, ins, ins, R[1]);
329 ins = emit_SHIFT(env, ins, R[0]);
334 ins = emit_SHIFT(env, ins, R[0]);
337 ins2 = emit_SHIFT(env, env->root, R[0] + R[1]);
338 return emit_LEA(env, ins, ins2, 0);
343 * Main decompose driver.
345 static instruction *decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
350 return decompose_simple_cases(env, R, r, N);
352 if (params->also_use_subs) {
353 gain = calculate_gain(R, r);
355 instruction *instr1, *instr2;
356 unsigned char *R1, *R2;
359 R1 = complement_condensed(env, R, r, gain, &r1);
361 R2 = obstack_alloc(&env->obst, r2);
364 for (i = 0; i < gain; ++i) {
371 /* Two identical bits: normalize */
376 for (i = gain + 1; i < r; ++i) {
380 instr1 = decompose_mul(env, R1, r1, NULL);
381 instr2 = decompose_mul(env, R2, r2, NULL);
382 return emit_SUB(env, instr2, instr1);
387 N = condensed_to_value(env, R, r);
389 for (i = env->max_S; i > 0; --i) {
390 tarval *div_res, *mod_res;
391 tarval *tv = new_tarval_from_long((1 << i) + 1, env->mode);
393 div_res = tarval_divmod(N, tv, &mod_res);
394 if (mod_res == get_mode_null(env->mode)) {
398 Rs = value_to_condensed(env, div_res, &rs);
400 instruction *N1 = decompose_mul(env, Rs, rs, div_res);
401 return emit_LEA(env, N1, N1, i);
405 return basic_decompose_mul(env, R, r, N);
408 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
411 * basic decomposition routine
413 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
417 if (R[0] == 0) { /* Case 1 */
418 t = R[1] > IMAX(env->max_S, R[1]);
420 Ns = decompose_mul(env, &R[1], r - 1, N);
421 return emit_LEA(env, env->root, Ns, t);
422 } else if (R[0] <= env->max_S) { /* Case 2 */
425 Ns = decompose_mul(env, &R[1], r - 1, N);
426 return emit_LEA(env, Ns, env->root, t);
430 Ns = decompose_mul(env, R, r, N);
431 return emit_SHIFT(env, Ns, t);
436 * recursive build the graph form the instructions.
438 * @param env the environment
439 * @param inst the instruction
441 static ir_node *build_graph(mul_env *env, instruction *inst) {
447 switch (inst->kind) {
449 l = build_graph(env, inst->in[0]);
450 r = build_graph(env, inst->in[1]);
451 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
452 r = new_rd_Shl(env->dbg, current_ir_graph, env->blk, r, c, env->mode);
453 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
455 l = build_graph(env, inst->in[0]);
456 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
457 return inst->irn = new_rd_Shl(env->dbg, current_ir_graph, env->blk, l, c, env->mode);
459 l = build_graph(env, inst->in[0]);
460 r = build_graph(env, inst->in[1]);
461 return inst->irn = new_rd_Sub(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
463 l = build_graph(env, inst->in[0]);
464 r = build_graph(env, inst->in[1]);
465 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
467 return inst->irn = new_r_Const(current_ir_graph, env->blk, env->mode, get_mode_null(env->mode));
469 panic("Unsupported instruction kind");
475 * Calculate the costs for the given instruction sequence.
476 * Note that additional costs due to higher register pressure are NOT evaluated yet
478 static int evaluate_insn(mul_env *env, instruction *inst) {
481 if (inst->costs >= 0) {
482 /* was already evaluated */
486 switch (inst->kind) {
490 costs = evaluate_insn(env, inst->in[0]);
491 costs += evaluate_insn(env, inst->in[1]);
492 costs += env->evaluate(inst->kind, NULL);
496 if (inst->shift_count > params->highest_shift_amount)
498 if (env->n_shift <= 0)
502 costs = evaluate_insn(env, inst->in[0]);
503 costs += env->evaluate(inst->kind, NULL);
507 inst->costs = costs = env->evaluate(inst->kind, 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, tarval *tv) {
532 obstack_init(&env.obst);
533 env.mode = get_tarval_mode(tv);
534 env.bits = (unsigned)get_mode_size_bits(env.mode);
536 env.root = emit_ROOT(&env, operand);
538 env.n_shift = params->maximum_shifts;
539 env.evaluate = params->evaluate != NULL ? params->evaluate : default_evaluate;
541 R = value_to_condensed(&env, tv, &r);
542 inst = decompose_mul(&env, R, r, tv);
544 /* the paper suggests 70% here */
545 mul_costs = (env.evaluate(MUL, tv) * 7 + 5) / 10;
546 if (evaluate_insn(&env, inst) <= mul_costs && !env.fail) {
548 env.blk = get_nodes_block(irn);
549 env.dbg = get_irn_dbg_info(irn);
550 env.shf_mode = find_unsigned_mode(env.mode);
551 if (env.shf_mode == NULL)
552 env.shf_mode = mode_Iu;
554 res = build_graph(&env, inst);
556 obstack_free(&env.obst, NULL);
560 /* Replace Muls with Shifts and Add/Subs. */
561 ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn) {
563 ir_mode *mode = get_irn_mode(irn);
565 /* If the architecture dependent optimizations were not initialized
566 or this optimization was not enabled. */
567 if (params == NULL || (opts & arch_dep_mul_to_shift) == 0)
570 set_arch_dep_running(1);
572 if (is_Mul(irn) && mode_is_int(mode)) {
573 ir_node *left = get_binop_left(irn);
574 ir_node *right = get_binop_right(irn);
576 ir_node *operand = NULL;
578 /* Look, if one operand is a constant. */
579 if (is_Const(left)) {
580 tv = get_Const_tarval(left);
582 } else if (is_Const(right)) {
583 tv = get_Const_tarval(right);
588 res = do_decomposition(irn, operand, tv);
591 hook_arch_dep_replace_mul_with_shifts(irn);
597 //set_arch_dep_running(0);
603 * calculated the ld2 of a tarval if tarval is 2^n, else returns -1.
605 static int tv_ld2(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 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(tarval *d) {
657 ir_mode *mode = get_tarval_mode(d);
658 ir_mode *u_mode = find_unsigned_mode(mode);
659 int bits = get_mode_size_bits(u_mode);
661 tarval *ad, *anc, *delta, *q1, *r1, *q2, *r2, *t; /* unsigned */
664 tarval *bits_minus_1, *two_bits_1;
668 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
670 /* we need overflow mode to work correctly */
671 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
674 bits_minus_1 = new_tarval_from_long(bits - 1, u_mode);
675 two_bits_1 = SHL(get_mode_one(u_mode), bits_minus_1);
677 ad = CNV(ABS(d), u_mode);
678 t = ADD(two_bits_1, SHR(CNV(d, u_mode), bits_minus_1));
679 anc = SUB(SUB(t, ONE(u_mode)), MOD(t, ad)); /* Absolute value of nc */
680 p = bits - 1; /* Init: p */
681 q1 = DIV(two_bits_1, anc); /* Init: q1 = 2^p/|nc| */
682 r1 = SUB(two_bits_1, MUL(q1, anc)); /* Init: r1 = rem(2^p, |nc|) */
683 q2 = DIV(two_bits_1, ad); /* Init: q2 = 2^p/|d| */
684 r2 = SUB(two_bits_1, MUL(q2, ad)); /* Init: r2 = rem(2^p, |d|) */
688 q1 = ADD(q1, q1); /* Update q1 = 2^p/|nc| */
689 r1 = ADD(r1, r1); /* Update r1 = rem(2^p, |nc|) */
691 if (CMP(r1, anc) & pn_Cmp_Ge) {
692 q1 = ADD(q1, ONE(u_mode));
696 q2 = ADD(q2, q2); /* Update q2 = 2^p/|d| */
697 r2 = ADD(r2, r2); /* Update r2 = rem(2^p, |d|) */
699 if (CMP(r2, ad) & pn_Cmp_Ge) {
700 q2 = ADD(q2, ONE(u_mode));
705 } while (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(u_mode)) & pn_Cmp_Eq));
707 d_cmp = CMP(d, ZERO(mode));
709 if (d_cmp & pn_Cmp_Ge)
710 mag.M = ADD(CNV(q2, mode), ONE(mode));
712 mag.M = SUB(ZERO(mode), ADD(CNV(q2, mode), ONE(mode)));
714 M_cmp = CMP(mag.M, ZERO(mode));
718 /* need an add if d > 0 && M < 0 */
719 mag.need_add = d_cmp & pn_Cmp_Gt && M_cmp & pn_Cmp_Lt;
721 /* need a sub if d < 0 && M > 0 */
722 mag.need_sub = d_cmp & pn_Cmp_Lt && M_cmp & pn_Cmp_Gt;
724 tarval_set_integer_overflow_mode(rem);
729 /** The result of the magicu() function. */
731 tarval *M; /**< magic add constant */
732 int s; /**< shift amount */
733 int need_add; /**< add indicator */
737 * Unsigned division by constant d: calculate the Magic multiplier M and the shift amount s
739 * see Hacker's Delight: 10-10 Integer Division by Constants: Incorporation into a Compiler (Unsigned)
741 static struct mu magicu(tarval *d) {
742 ir_mode *mode = get_tarval_mode(d);
743 int bits = get_mode_size_bits(mode);
745 tarval *nc, *delta, *q1, *r1, *q2, *r2;
746 tarval *bits_minus_1, *two_bits_1, *seven_ff;
750 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
752 /* we need overflow mode to work correctly */
753 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
755 bits_minus_1 = new_tarval_from_long(bits - 1, mode);
756 two_bits_1 = SHL(get_mode_one(mode), bits_minus_1);
757 seven_ff = SUB(two_bits_1, ONE(mode));
759 magu.need_add = 0; /* initialize the add indicator */
760 nc = SUB(NEG(ONE(mode)), MOD(NEG(d), d));
761 p = bits - 1; /* Init: p */
762 q1 = DIV(two_bits_1, nc); /* Init: q1 = 2^p/nc */
763 r1 = SUB(two_bits_1, MUL(q1, nc)); /* Init: r1 = rem(2^p, nc) */
764 q2 = DIV(seven_ff, d); /* Init: q2 = (2^p - 1)/d */
765 r2 = SUB(seven_ff, MUL(q2, d)); /* Init: r2 = rem(2^p - 1, d) */
769 if (CMP(r1, SUB(nc, r1)) & pn_Cmp_Ge) {
770 q1 = ADD(ADD(q1, q1), ONE(mode));
771 r1 = SUB(ADD(r1, r1), nc);
778 if (CMP(ADD(r2, ONE(mode)), SUB(d, r2)) & pn_Cmp_Ge) {
779 if (CMP(q2, seven_ff) & pn_Cmp_Ge)
782 q2 = ADD(ADD(q2, q2), ONE(mode));
783 r2 = SUB(ADD(ADD(r2, r2), ONE(mode)), d);
786 if (CMP(q2, two_bits_1) & pn_Cmp_Ge)
790 r2 = ADD(ADD(r2, r2), ONE(mode));
792 delta = SUB(SUB(d, ONE(mode)), r2);
793 } while (p < 2*bits &&
794 (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(mode)) & pn_Cmp_Eq)));
796 magu.M = ADD(q2, ONE(mode)); /* Magic number */
797 magu.s = p - bits; /* and shift amount */
799 tarval_set_integer_overflow_mode(rem);
805 * Build the Mulh replacement code for n / tv.
807 * Note that 'div' might be a mod or DivMod operation as well
809 static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv) {
810 dbg_info *dbg = get_irn_dbg_info(div);
811 ir_node *n = get_binop_left(div);
812 ir_node *block = get_irn_n(div, -1);
813 ir_mode *mode = get_irn_mode(n);
814 int bits = get_mode_size_bits(mode);
817 /* Beware: do not transform bad code */
818 if (is_Bad(n) || is_Bad(block))
821 if (mode_is_signed(mode)) {
822 struct ms mag = magic(tv);
824 /* generate the Mulh instruction */
825 c = new_r_Const(current_ir_graph, block, mode, mag.M);
826 q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
828 /* do we need an Add or Sub */
830 q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
831 else if (mag.need_sub)
832 q = new_rd_Sub(dbg, current_ir_graph, block, q, n, mode);
834 /* Do we need the shift */
836 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
837 q = new_rd_Shrs(dbg, current_ir_graph, block, q, c, mode);
841 c = new_r_Const_long(current_ir_graph, block, mode_Iu, bits-1);
842 t = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
844 q = new_rd_Add(dbg, current_ir_graph, block, q, t, mode);
846 struct mu mag = magicu(tv);
849 /* generate the Mulh instruction */
850 c = new_r_Const(current_ir_graph, block, mode, mag.M);
851 q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
855 /* use the GM scheme */
856 t = new_rd_Sub(dbg, current_ir_graph, block, n, q, mode);
858 c = new_r_Const(current_ir_graph, block, mode_Iu, get_mode_one(mode_Iu));
859 t = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
861 t = new_rd_Add(dbg, current_ir_graph, block, t, q, mode);
863 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s-1);
864 q = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
866 /* use the default scheme */
867 q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
869 } else if (mag.s > 0) { /* default scheme, shift needed */
870 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
871 q = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
877 /* Replace Divs with Shifts and Add/Subs and Mulh. */
878 ir_node *arch_dep_replace_div_by_const(ir_node *irn) {
881 /* If the architecture dependent optimizations were not initialized
882 or this optimization was not enabled. */
883 if (params == NULL || (opts & arch_dep_div_by_const) == 0)
887 ir_node *c = get_Div_right(irn);
888 ir_node *block, *left;
898 tv = get_Const_tarval(c);
900 /* check for division by zero */
901 if (tarval_is_null(tv))
904 left = get_Div_left(irn);
905 mode = get_irn_mode(left);
906 block = get_irn_n(irn, -1);
907 dbg = get_irn_dbg_info(irn);
909 bits = get_mode_size_bits(mode);
913 if (mode_is_signed(mode)) {
914 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
915 ntv = tarval_neg(tv);
925 if (k >= 0) { /* division by 2^k or -2^k */
926 if (mode_is_signed(mode)) {
928 ir_node *curr = left;
930 /* create the correction code for signed values only if there might be a remainder */
931 if (! is_Div_remainderless(irn)) {
933 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
934 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
937 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
938 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
940 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
945 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
946 res = new_rd_Shrs(dbg, current_ir_graph, block, curr, k_node, mode);
948 if (n_flag) { /* negate the result */
951 k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
952 res = new_rd_Sub(dbg, current_ir_graph, block, k_node, res, mode);
954 } else { /* unsigned case */
957 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
958 res = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
962 if (allow_Mulh(mode))
963 res = replace_div_by_mulh(irn, tv);
968 hook_arch_dep_replace_division_by_const(irn);
973 /* Replace Mods with Shifts and Add/Subs and Mulh. */
974 ir_node *arch_dep_replace_mod_by_const(ir_node *irn) {
977 /* If the architecture dependent optimizations were not initialized
978 or this optimization was not enabled. */
979 if (params == NULL || (opts & arch_dep_mod_by_const) == 0)
983 ir_node *c = get_Mod_right(irn);
984 ir_node *block, *left;
994 tv = get_Const_tarval(c);
996 /* check for division by zero */
997 if (tarval_is_null(tv))
1000 left = get_Mod_left(irn);
1001 mode = get_irn_mode(left);
1002 block = get_irn_n(irn, -1);
1003 dbg = get_irn_dbg_info(irn);
1004 bits = get_mode_size_bits(mode);
1008 if (mode_is_signed(mode)) {
1009 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1010 ntv = tarval_neg(tv);
1019 /* division by 2^k or -2^k:
1020 * we use "modulus" here, so x % y == x % -y that's why is no difference between the case 2^k and -2^k
1022 if (mode_is_signed(mode)) {
1024 ir_node *curr = left;
1027 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1028 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1031 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1032 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1034 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1036 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1037 curr = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1039 res = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1040 } else { /* unsigned case */
1043 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1044 res = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1047 /* other constant */
1048 if (allow_Mulh(mode)) {
1049 res = replace_div_by_mulh(irn, tv);
1051 res = new_rd_Mul(dbg, current_ir_graph, block, res, c, mode);
1053 /* res = arch_dep_mul_to_shift(res); */
1055 res = new_rd_Sub(dbg, current_ir_graph, block, left, res, mode);
1061 hook_arch_dep_replace_division_by_const(irn);
1066 /* Replace DivMods with Shifts and Add/Subs and Mulh. */
1067 void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn) {
1070 /* If the architecture dependent optimizations were not initialized
1071 or this optimization was not enabled. */
1072 if (params == NULL ||
1073 ((opts & (arch_dep_div_by_const|arch_dep_mod_by_const)) != (arch_dep_div_by_const|arch_dep_mod_by_const)))
1076 if (is_DivMod(irn)) {
1077 ir_node *c = get_DivMod_right(irn);
1078 ir_node *block, *left;
1088 tv = get_Const_tarval(c);
1090 /* check for division by zero */
1091 if (tarval_is_null(tv))
1094 left = get_DivMod_left(irn);
1095 mode = get_irn_mode(left);
1096 block = get_irn_n(irn, -1);
1097 dbg = get_irn_dbg_info(irn);
1099 bits = get_mode_size_bits(mode);
1103 if (mode_is_signed(mode)) {
1104 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1105 ntv = tarval_neg(tv);
1115 if (k >= 0) { /* division by 2^k or -2^k */
1116 if (mode_is_signed(mode)) {
1117 ir_node *k_node, *c_k;
1118 ir_node *curr = left;
1121 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1122 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1125 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1126 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1128 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1130 c_k = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1132 *div = new_rd_Shrs(dbg, current_ir_graph, block, curr, c_k, mode);
1134 if (n_flag) { /* negate the div result */
1137 k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
1138 *div = new_rd_Sub(dbg, current_ir_graph, block, k_node, *div, mode);
1141 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1142 curr = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1144 *mod = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1145 } else { /* unsigned case */
1148 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1149 *div = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
1151 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1152 *mod = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1155 /* other constant */
1156 if (allow_Mulh(mode)) {
1159 *div = replace_div_by_mulh(irn, tv);
1161 t = new_rd_Mul(dbg, current_ir_graph, block, *div, c, mode);
1163 /* t = arch_dep_mul_to_shift(t); */
1165 *mod = new_rd_Sub(dbg, current_ir_graph, block, left, t, mode);
1171 hook_arch_dep_replace_division_by_const(irn);
1175 static const ir_settings_arch_dep_t default_params = {
1176 1, /* also use subs */
1177 4, /* maximum shifts */
1178 31, /* maximum shift amount */
1179 default_evaluate, /* default evaluator */
1181 0, /* allow Mulhs */
1182 0, /* allow Mulus */
1183 32 /* Mulh allowed up to 32 bit */
1186 /* A default parameter factory for testing purposes. */
1187 const ir_settings_arch_dep_t *arch_dep_default_factory(void) {
1188 return &default_params;