2 * Copyright (C) 1995-2007 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",
41 #include "irgraph_t.h"
48 #include "dbginfo_t.h"
49 #include "iropt_dbg.h"
60 /* when we need verifying */
62 # define IRN_VRFY_IRG(res, irg)
64 # define IRN_VRFY_IRG(res, irg) irn_vrfy_irg(res, irg)
67 /** The params got from the factory in arch_dep_init(...). */
68 static const ir_settings_arch_dep_t *params = NULL;
70 /** The bit mask, which optimizations to apply. */
71 static arch_dep_opts_t opts;
73 /* we need this new pseudo op */
74 static ir_op *op_Mulh = NULL;
77 * construct a Mulh: Mulh(a,b) = (a * b) >> w, w is the with in bits of a, b
80 new_rd_Mulh (dbg_info *db, ir_graph *irg, ir_node *block,
81 ir_node *op1, ir_node *op2, ir_mode *mode) {
87 res = new_ir_node(db, irg, block, op_Mulh, mode, 2, in);
88 res = optimize_node(res);
89 IRN_VRFY_IRG(res, irg);
93 ir_op *get_op_Mulh(void) { return op_Mulh; }
95 void arch_dep_init(arch_dep_params_factory_t factory) {
102 int mulh_opc = get_next_ir_opcode();
104 /* create the Mulh operation */
105 op_Mulh = new_ir_op(mulh_opc, "Mulh", op_pin_state_floats, irop_flag_commutative, oparity_binary, 0, 0, NULL);
109 void arch_dep_set_opts(arch_dep_opts_t the_opts) {
112 if (opts & arch_dep_mul_to_shift)
113 set_opt_arch_dep_running(1);
116 /** check, whether a mode allows a Mulh instruction. */
117 static int allow_Mulh(ir_mode *mode) {
118 if (get_mode_size_bits(mode) > params->max_bits_for_mulh)
120 return (mode_is_signed(mode) && params->allow_mulhs) || (!mode_is_signed(mode) && params->allow_mulhu);
126 typedef struct instruction instruction;
128 insn_kind kind; /**< the instruction kind */
129 instruction *in[2]; /**< the ins */
130 unsigned shift_count; /**< shift count for LEA and SHIFT */
131 ir_node *irn; /**< the generated node for this instruction if any. */
132 int costs; /**< the costs for this instruction */
136 * The environment for the strength reduction of multiplications.
138 typedef struct _mul_env {
139 struct obstack obst; /**< an obstack for local space. */
140 ir_mode *mode; /**< the mode of the multiplication constant */
141 unsigned bits; /**< number of bits in the mode */
142 unsigned max_S; /**< the maximum LEA shift value. */
143 instruction *root; /**< the root of the instruction tree */
144 ir_node *op; /**< the operand that is multiplied */
145 ir_node *blk; /**< the block where the new graph is built */
146 dbg_info *dbg; /**< the debug info for the new graph. */
147 ir_mode *shf_mode; /**< the (unsigned) mode for the shift constants */
148 int fail; /**< set to 1 if the instruction sequence fails the constraints */
149 int n_shift; /**< maximum number of allowed shift instructions */
151 evaluate_costs_func evaluate; /**< the evaluate callback */
155 * Some kind of default evaluator. Return the cost of
158 static int default_evaluate(insn_kind kind, tarval *tv) {
167 * emit a LEA (or an Add) instruction
169 static instruction *emit_LEA(mul_env *env, instruction *a, instruction *b, unsigned shift) {
170 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
171 res->kind = shift > 0 ? LEA : ADD;
174 res->shift_count = shift;
181 * emit a SHIFT (or an Add or a Zero) instruction
183 static instruction *emit_SHIFT(mul_env *env, instruction *a, unsigned shift) {
184 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
185 if (shift == env->bits) {
186 /* a 2^bits with bits resolution is a zero */
190 res->shift_count = 0;
191 } else if (shift != 1) {
195 res->shift_count = shift;
200 res->shift_count = 0;
208 * emit a SUB instruction
210 static instruction *emit_SUB(mul_env *env, instruction *a, instruction *b) {
211 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
215 res->shift_count = 0;
222 * emit the ROOT instruction
224 static instruction *emit_ROOT(mul_env *env, ir_node *root_op) {
225 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
229 res->shift_count = 0;
237 * Returns the condensed representation of the tarval tv
239 static unsigned char *value_to_condensed(mul_env *env, tarval *tv, int *pr) {
240 ir_mode *mode = get_tarval_mode(tv);
241 int bits = get_mode_size_bits(mode);
242 char *bitstr = get_tarval_bitpattern(tv);
244 unsigned char *R = obstack_alloc(&env->obst, bits);
247 for (i = 0; bitstr[i] != '\0'; ++i) {
248 if (bitstr[i] == '1') {
261 * Calculate the gain when using the generalized complementary technique
263 static int calculate_gain(unsigned char *R, int r) {
268 /* the gain for r == 1 */
270 for (i = 2; i < r; ++i) {
271 /* calculate the gain for r from the gain for r-1 */
272 gain += 2 - R[i - 1];
274 if (gain > max_gain) {
285 * Calculates the condensed complement of a given (R,r) tuple
287 static unsigned char *complement_condensed(mul_env *env, unsigned char *R, int r, int gain, int *prs) {
288 unsigned char *value = obstack_alloc(&env->obst, env->bits);
292 memset(value, 0, env->bits);
295 for (i = 0; i < gain; ++i) {
300 /* negate and propagate 1 */
302 for (i = 0; i <= j; ++i) {
303 unsigned char v = !value[i];
309 /* condense it again */
312 for (i = 0; i <= j; ++i) {
325 * creates a tarval from a condensed representation.
327 static tarval *condensed_to_value(mul_env *env, unsigned char *R, int r) {
332 tv = get_mode_one(env->mode);
334 for (i = 0; i < r; ++i) {
337 tarval *t = new_tarval_from_long(j, mode_Iu);
338 tv = tarval_shl(tv, t);
340 res = res ? tarval_add(res, tv) : tv;
346 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N);
349 * handle simple cases with up-to 2 bits set
351 static instruction *decompose_simple_cases(mul_env *env, unsigned char *R, int r, tarval *N) {
352 instruction *ins, *ins2;
356 return emit_SHIFT(env, env->root, R[0]);
362 ins = emit_SHIFT(env, ins, R[0]);
364 if (R[1] <= env->max_S)
365 return emit_LEA(env, ins, ins, R[1]);
367 ins2 = emit_SHIFT(env, env->root, R[0] + R[1]);
368 return emit_LEA(env, ins, ins2, 0);
373 * Main decompose driver.
375 static instruction *decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
380 return decompose_simple_cases(env, R, r, N);
382 if (params->also_use_subs) {
383 gain = calculate_gain(R, r);
385 instruction *instr1, *instr2;
386 unsigned char *R1, *R2;
389 R1 = complement_condensed(env, R, r, gain, &r1);
391 R2 = obstack_alloc(&env->obst, r2);
394 for (i = 0; i < gain; ++i) {
401 /* Two identical bits: normalize */
406 for (i = gain + 1; i < r; ++i) {
410 instr1 = decompose_mul(env, R1, r1, NULL);
411 instr2 = decompose_mul(env, R2, r2, NULL);
412 return emit_SUB(env, instr2, instr1);
417 N = condensed_to_value(env, R, r);
419 for (i = env->max_S; i > 0; --i) {
420 tarval *div_res, *mod_res;
421 tarval *tv = new_tarval_from_long((1 << i) + 1, env->mode);
423 div_res = tarval_divmod(N, tv, &mod_res);
424 if (mod_res == get_mode_null(env->mode)) {
428 Rs = value_to_condensed(env, div_res, &rs);
430 instruction *N1 = decompose_mul(env, Rs, rs, div_res);
431 return emit_LEA(env, N1, N1, i);
435 return basic_decompose_mul(env, R, r, N);
438 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
441 * basic decomposition routine
443 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
447 if (R[0] == 0) { /* Case 1 */
448 t = R[1] > IMAX(env->max_S, R[1]);
450 Ns = decompose_mul(env, &R[1], r - 1, N);
451 return emit_LEA(env, env->root, Ns, t);
452 } else if (R[0] <= env->max_S) { /* Case 2 */
455 Ns = decompose_mul(env, &R[1], r - 1, N);
456 return emit_LEA(env, Ns, env->root, t);
460 Ns = decompose_mul(env, R, r, N);
461 return emit_SHIFT(env, Ns, t);
466 * recursive build the graph form the instructions.
468 * @param env the environment
469 * @param inst the instruction
471 static ir_node *build_graph(mul_env *env, instruction *inst) {
477 switch (inst->kind) {
479 l = build_graph(env, inst->in[0]);
480 r = build_graph(env, inst->in[1]);
481 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
482 r = new_rd_Shl(env->dbg, current_ir_graph, env->blk, r, c, env->mode);
483 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
485 l = build_graph(env, inst->in[0]);
486 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
487 return inst->irn = new_rd_Shl(env->dbg, current_ir_graph, env->blk, l, c, env->mode);
489 l = build_graph(env, inst->in[0]);
490 r = build_graph(env, inst->in[1]);
491 return inst->irn = new_rd_Sub(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
493 l = build_graph(env, inst->in[0]);
494 r = build_graph(env, inst->in[1]);
495 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
497 return inst->irn = new_r_Const(current_ir_graph, env->blk, env->mode, get_mode_null(env->mode));
505 * Calculate the costs for the given instruction sequence.
506 * Note that additional costs due to higher register pressure are NOT evaluated yet
508 static int evaluate_insn(mul_env *env, instruction *inst) {
511 if (inst->costs >= 0) {
512 /* was already evaluated */
516 switch (inst->kind) {
520 costs = evaluate_insn(env, inst->in[0]);
521 costs += evaluate_insn(env, inst->in[1]);
522 costs += env->evaluate(inst->kind, NULL);
526 if (inst->shift_count > params->highest_shift_amount)
528 if (env->n_shift <= 0)
532 costs = evaluate_insn(env, inst->in[0]);
533 costs += env->evaluate(inst->kind, NULL);
537 inst->costs = costs = env->evaluate(inst->kind, NULL);
546 * Evaluate the replacement instructions and build a new graph
547 * if faster than the Mul.
548 * returns the root of the new graph then or irn otherwise.
550 * @param irn the Mul operation
551 * @param operand the multiplication operand
552 * @param tv the multiplication constant
554 * @return the new graph
556 static ir_node *do_decomposition(ir_node *irn, ir_node *operand, tarval *tv) {
564 obstack_init(&env.obst);
565 env.mode = get_tarval_mode(tv);
566 env.bits = (unsigned)get_mode_size_bits(env.mode);
568 env.root = emit_ROOT(&env, operand);
570 env.n_shift = params->maximum_shifts;
571 env.evaluate = params->evaluate != NULL ? params->evaluate : default_evaluate;
573 R = value_to_condensed(&env, tv, &r);
574 inst = decompose_mul(&env, R, r, tv);
576 /* the paper suggests 70% here */
577 mul_costs = (env.evaluate(MUL, tv) * 7) / 10;
578 if (evaluate_insn(&env, inst) <= mul_costs && !env.fail) {
580 env.blk = get_nodes_block(irn);
581 env.dbg = get_irn_dbg_info(irn);
582 env.shf_mode = find_unsigned_mode(env.mode);
583 if (env.shf_mode == NULL)
584 env.shf_mode = mode_Iu;
586 res = build_graph(&env, inst);
588 obstack_free(&env.obst, NULL);
592 /* Replace Muls with Shifts and Add/Subs. */
593 ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn) {
595 ir_mode *mode = get_irn_mode(irn);
597 /* If the architecture dependent optimizations were not initialized
598 or this optimization was not enabled. */
599 if (params == NULL || (opts & arch_dep_mul_to_shift) == 0)
602 if (is_Mul(irn) && mode_is_int(mode)) {
603 ir_node *left = get_binop_left(irn);
604 ir_node *right = get_binop_right(irn);
606 ir_node *operand = NULL;
608 /* Look, if one operand is a constant. */
609 if (is_Const(left)) {
610 tv = get_Const_tarval(left);
612 } else if (is_Const(right)) {
613 tv = get_Const_tarval(right);
618 res = do_decomposition(irn, operand, tv);
621 hook_arch_dep_replace_mul_with_shifts(irn);
631 * calculated the ld2 of a tarval if tarval is 2^n, else returns -1.
633 static int tv_ld2(tarval *tv, int bits) {
636 for (num = i = 0; i < bits; ++i) {
637 unsigned char v = get_tarval_sub_bits(tv, i);
642 for (j = 0; j < 8; ++j)
655 /* for shorter lines */
656 #define ABS(a) tarval_abs(a)
657 #define NEG(a) tarval_neg(a)
658 #define NOT(a) tarval_not(a)
659 #define SHL(a, b) tarval_shl(a, b)
660 #define SHR(a, b) tarval_shr(a, b)
661 #define ADD(a, b) tarval_add(a, b)
662 #define SUB(a, b) tarval_sub(a, b)
663 #define MUL(a, b) tarval_mul(a, b)
664 #define DIV(a, b) tarval_div(a, b)
665 #define MOD(a, b) tarval_mod(a, b)
666 #define CMP(a, b) tarval_cmp(a, b)
667 #define CNV(a, m) tarval_convert_to(a, m)
668 #define ONE(m) get_mode_one(m)
669 #define ZERO(m) get_mode_null(m)
671 /** The result of a the magic() function. */
673 tarval *M; /**< magic number */
674 int s; /**< shift amount */
675 int need_add; /**< an additional add is needed */
676 int need_sub; /**< an additional sub is needed */
680 * Signed division by constant d: calculate the Magic multiplier M and the shift amount s
682 * see Hacker's Delight: 10-6 Integer Division by Constants: Incorporation into a Compiler
684 static struct ms magic(tarval *d) {
685 ir_mode *mode = get_tarval_mode(d);
686 ir_mode *u_mode = find_unsigned_mode(mode);
687 int bits = get_mode_size_bits(u_mode);
689 tarval *ad, *anc, *delta, *q1, *r1, *q2, *r2, *t; /* unsigned */
692 tarval *bits_minus_1, *two_bits_1;
696 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
698 /* we need overflow mode to work correctly */
699 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
702 bits_minus_1 = new_tarval_from_long(bits - 1, u_mode);
703 two_bits_1 = SHL(get_mode_one(u_mode), bits_minus_1);
705 ad = CNV(ABS(d), u_mode);
706 t = ADD(two_bits_1, SHR(CNV(d, u_mode), bits_minus_1));
707 anc = SUB(SUB(t, ONE(u_mode)), MOD(t, ad)); /* Absolute value of nc */
708 p = bits - 1; /* Init: p */
709 q1 = DIV(two_bits_1, anc); /* Init: q1 = 2^p/|nc| */
710 r1 = SUB(two_bits_1, MUL(q1, anc)); /* Init: r1 = rem(2^p, |nc|) */
711 q2 = DIV(two_bits_1, ad); /* Init: q2 = 2^p/|d| */
712 r2 = SUB(two_bits_1, MUL(q2, ad)); /* Init: r2 = rem(2^p, |d|) */
716 q1 = ADD(q1, q1); /* Update q1 = 2^p/|nc| */
717 r1 = ADD(r1, r1); /* Update r1 = rem(2^p, |nc|) */
719 if (CMP(r1, anc) & pn_Cmp_Ge) {
720 q1 = ADD(q1, ONE(u_mode));
724 q2 = ADD(q2, q2); /* Update q2 = 2^p/|d| */
725 r2 = ADD(r2, r2); /* Update r2 = rem(2^p, |d|) */
727 if (CMP(r2, ad) & pn_Cmp_Ge) {
728 q2 = ADD(q2, ONE(u_mode));
733 } while (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(u_mode)) & pn_Cmp_Eq));
735 d_cmp = CMP(d, ZERO(mode));
737 if (d_cmp & pn_Cmp_Ge)
738 mag.M = ADD(CNV(q2, mode), ONE(mode));
740 mag.M = SUB(ZERO(mode), ADD(CNV(q2, mode), ONE(mode)));
742 M_cmp = CMP(mag.M, ZERO(mode));
746 /* need an add if d > 0 && M < 0 */
747 mag.need_add = d_cmp & pn_Cmp_Gt && M_cmp & pn_Cmp_Lt;
749 /* need a sub if d < 0 && M > 0 */
750 mag.need_sub = d_cmp & pn_Cmp_Lt && M_cmp & pn_Cmp_Gt;
752 tarval_set_integer_overflow_mode(rem);
757 /** The result of the magicu() function. */
759 tarval *M; /**< magic add constant */
760 int s; /**< shift amount */
761 int need_add; /**< add indicator */
765 * Unsigned division by constant d: calculate the Magic multiplier M and the shift amount s
767 * see Hacker's Delight: 10-10 Integer Division by Constants: Incorporation into a Compiler (Unsigned)
769 static struct mu magicu(tarval *d) {
770 ir_mode *mode = get_tarval_mode(d);
771 int bits = get_mode_size_bits(mode);
773 tarval *nc, *delta, *q1, *r1, *q2, *r2;
774 tarval *bits_minus_1, *two_bits_1, *seven_ff;
778 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
780 /* we need overflow mode to work correctly */
781 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
783 bits_minus_1 = new_tarval_from_long(bits - 1, mode);
784 two_bits_1 = SHL(get_mode_one(mode), bits_minus_1);
785 seven_ff = SUB(two_bits_1, ONE(mode));
787 magu.need_add = 0; /* initialize the add indicator */
788 nc = SUB(NEG(ONE(mode)), MOD(NEG(d), d));
789 p = bits - 1; /* Init: p */
790 q1 = DIV(two_bits_1, nc); /* Init: q1 = 2^p/nc */
791 r1 = SUB(two_bits_1, MUL(q1, nc)); /* Init: r1 = rem(2^p, nc) */
792 q2 = DIV(seven_ff, d); /* Init: q2 = (2^p - 1)/d */
793 r2 = SUB(seven_ff, MUL(q2, d)); /* Init: r2 = rem(2^p - 1, d) */
797 if (CMP(r1, SUB(nc, r1)) & pn_Cmp_Ge) {
798 q1 = ADD(ADD(q1, q1), ONE(mode));
799 r1 = SUB(ADD(r1, r1), nc);
806 if (CMP(ADD(r2, ONE(mode)), SUB(d, r2)) & pn_Cmp_Ge) {
807 if (CMP(q2, seven_ff) & pn_Cmp_Ge)
810 q2 = ADD(ADD(q2, q2), ONE(mode));
811 r2 = SUB(ADD(ADD(r2, r2), ONE(mode)), d);
814 if (CMP(q2, two_bits_1) & pn_Cmp_Ge)
818 r2 = ADD(ADD(r2, r2), ONE(mode));
820 delta = SUB(SUB(d, ONE(mode)), r2);
821 } while (p < 2*bits &&
822 (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(mode)) & pn_Cmp_Eq)));
824 magu.M = ADD(q2, ONE(mode)); /* Magic number */
825 magu.s = p - bits; /* and shift amount */
827 tarval_set_integer_overflow_mode(rem);
833 * Build the Mulh replacement code for n / tv.
835 * Note that 'div' might be a mod or DivMod operation as well
837 static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv) {
838 dbg_info *dbg = get_irn_dbg_info(div);
839 ir_node *n = get_binop_left(div);
840 ir_node *block = get_irn_n(div, -1);
841 ir_mode *mode = get_irn_mode(n);
842 int bits = get_mode_size_bits(mode);
845 /* Beware: do not transform bad code */
846 if (is_Bad(n) || is_Bad(block))
849 if (mode_is_signed(mode)) {
850 struct ms mag = magic(tv);
852 /* generate the Mulh instruction */
853 c = new_r_Const(current_ir_graph, block, mode, mag.M);
854 q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
856 /* do we need an Add or Sub */
858 q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
859 else if (mag.need_sub)
860 q = new_rd_Sub(dbg, current_ir_graph, block, q, n, mode);
862 /* Do we need the shift */
864 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
865 q = new_rd_Shrs(dbg, current_ir_graph, block, q, c, mode);
869 c = new_r_Const_long(current_ir_graph, block, mode_Iu, bits-1);
870 t = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
872 q = new_rd_Add(dbg, current_ir_graph, block, q, t, mode);
874 struct mu mag = magicu(tv);
877 /* generate the Mulh instruction */
878 c = new_r_Const(current_ir_graph, block, mode, mag.M);
879 q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
883 /* use the GM scheme */
884 t = new_rd_Sub(dbg, current_ir_graph, block, n, q, mode);
886 c = new_r_Const(current_ir_graph, block, mode_Iu, get_mode_one(mode_Iu));
887 t = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
889 t = new_rd_Add(dbg, current_ir_graph, block, t, q, mode);
891 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s-1);
892 q = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
894 /* use the default scheme */
895 q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
897 } else if (mag.s > 0) { /* default scheme, shift needed */
898 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
899 q = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
905 /* Replace Divs with Shifts and Add/Subs and Mulh. */
906 ir_node *arch_dep_replace_div_by_const(ir_node *irn) {
909 /* If the architecture dependent optimizations were not initialized
910 or this optimization was not enabled. */
911 if (params == NULL || (opts & arch_dep_div_by_const) == 0)
914 if (get_irn_opcode(irn) == iro_Div) {
915 ir_node *c = get_Div_right(irn);
916 ir_node *block, *left;
923 if (get_irn_op(c) != op_Const)
926 tv = get_Const_tarval(c);
928 /* check for division by zero */
929 if (classify_tarval(tv) == TV_CLASSIFY_NULL)
932 left = get_Div_left(irn);
933 mode = get_irn_mode(left);
934 block = get_irn_n(irn, -1);
935 dbg = get_irn_dbg_info(irn);
937 bits = get_mode_size_bits(mode);
941 if (mode_is_signed(mode)) {
942 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
943 ntv = tarval_neg(tv);
953 if (k >= 0) { /* division by 2^k or -2^k */
954 if (mode_is_signed(mode)) {
956 ir_node *curr = left;
959 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
960 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
963 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
964 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
966 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
968 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
969 res = new_rd_Shrs(dbg, current_ir_graph, block, curr, k_node, mode);
971 if (n_flag) { /* negate the result */
974 k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
975 res = new_rd_Sub(dbg, current_ir_graph, block, k_node, res, mode);
977 } else { /* unsigned case */
980 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
981 res = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
985 if (allow_Mulh(mode))
986 res = replace_div_by_mulh(irn, tv);
991 hook_arch_dep_replace_division_by_const(irn);
996 /* Replace Mods with Shifts and Add/Subs and Mulh. */
997 ir_node *arch_dep_replace_mod_by_const(ir_node *irn) {
1000 /* If the architecture dependent optimizations were not initialized
1001 or this optimization was not enabled. */
1002 if (params == NULL || (opts & arch_dep_mod_by_const) == 0)
1005 if (get_irn_opcode(irn) == iro_Mod) {
1006 ir_node *c = get_Mod_right(irn);
1007 ir_node *block, *left;
1014 if (get_irn_op(c) != op_Const)
1017 tv = get_Const_tarval(c);
1019 /* check for division by zero */
1020 if (classify_tarval(tv) == TV_CLASSIFY_NULL)
1023 left = get_Mod_left(irn);
1024 mode = get_irn_mode(left);
1025 block = get_irn_n(irn, -1);
1026 dbg = get_irn_dbg_info(irn);
1027 bits = get_mode_size_bits(mode);
1031 if (mode_is_signed(mode)) {
1032 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1033 ntv = tarval_neg(tv);
1042 /* division by 2^k or -2^k:
1043 * we use "modulus" here, so x % y == x % -y that's why is no difference between the case 2^k and -2^k
1045 if (mode_is_signed(mode)) {
1047 ir_node *curr = left;
1050 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1051 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1054 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1055 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1057 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1059 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1060 curr = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1062 res = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1063 } else { /* unsigned case */
1066 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1067 res = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1070 /* other constant */
1071 if (allow_Mulh(mode)) {
1072 res = replace_div_by_mulh(irn, tv);
1074 res = new_rd_Mul(dbg, current_ir_graph, block, res, c, mode);
1076 /* res = arch_dep_mul_to_shift(res); */
1078 res = new_rd_Sub(dbg, current_ir_graph, block, left, res, mode);
1084 hook_arch_dep_replace_division_by_const(irn);
1089 /* Replace DivMods with Shifts and Add/Subs and Mulh. */
1090 void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn) {
1093 /* If the architecture dependent optimizations were not initialized
1094 or this optimization was not enabled. */
1095 if (params == NULL ||
1096 ((opts & (arch_dep_div_by_const|arch_dep_mod_by_const)) != (arch_dep_div_by_const|arch_dep_mod_by_const)))
1099 if (get_irn_opcode(irn) == iro_DivMod) {
1100 ir_node *c = get_DivMod_right(irn);
1101 ir_node *block, *left;
1108 if (get_irn_op(c) != op_Const)
1111 tv = get_Const_tarval(c);
1113 /* check for division by zero */
1114 if (classify_tarval(tv) == TV_CLASSIFY_NULL)
1117 left = get_DivMod_left(irn);
1118 mode = get_irn_mode(left);
1119 block = get_irn_n(irn, -1);
1120 dbg = get_irn_dbg_info(irn);
1122 bits = get_mode_size_bits(mode);
1126 if (mode_is_signed(mode)) {
1127 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1128 ntv = tarval_neg(tv);
1138 if (k >= 0) { /* division by 2^k or -2^k */
1139 if (mode_is_signed(mode)) {
1140 ir_node *k_node, *c_k;
1141 ir_node *curr = left;
1144 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1145 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1148 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1149 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1151 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1153 c_k = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1155 *div = new_rd_Shrs(dbg, current_ir_graph, block, curr, c_k, mode);
1157 if (n_flag) { /* negate the div result */
1160 k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
1161 *div = new_rd_Sub(dbg, current_ir_graph, block, k_node, *div, mode);
1164 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1165 curr = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1167 *mod = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1168 } else { /* unsigned case */
1171 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1172 *div = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
1174 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1175 *mod = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1178 /* other constant */
1179 if (allow_Mulh(mode)) {
1182 *div = replace_div_by_mulh(irn, tv);
1184 t = new_rd_Mul(dbg, current_ir_graph, block, *div, c, mode);
1186 /* t = arch_dep_mul_to_shift(t); */
1188 *mod = new_rd_Sub(dbg, current_ir_graph, block, left, t, mode);
1194 hook_arch_dep_replace_division_by_const(irn);
1198 static const ir_settings_arch_dep_t default_params = {
1199 1, /* also use subs */
1200 4, /* maximum shifts */
1201 31, /* maximum shift amount */
1202 default_evaluate, /* default evaluator */
1204 0, /* allow Mulhs */
1205 0, /* allow Mulus */
1206 32 /* Mulh allowed up to 32 bit */
1209 /* A default parameter factory for testing purposes. */
1210 const ir_settings_arch_dep_t *arch_dep_default_factory(void) {
1211 return &default_params;