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"
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 /* we need this new pseudo op */
73 static ir_op *op_Mulh = NULL;
76 * construct a Mulh: Mulh(a,b) = (a * b) >> w, w is the with in bits of a, b
79 new_rd_Mulh (dbg_info *db, ir_graph *irg, ir_node *block,
80 ir_node *op1, ir_node *op2, ir_mode *mode) {
86 res = new_ir_node(db, irg, block, op_Mulh, mode, 2, in);
87 res = optimize_node(res);
88 IRN_VRFY_IRG(res, irg);
92 ir_op *get_op_Mulh(void) { return op_Mulh; }
94 void arch_dep_init(arch_dep_params_factory_t factory) {
101 int mulh_opc = get_next_ir_opcode();
103 /* create the Mulh operation */
104 op_Mulh = new_ir_op(mulh_opc, "Mulh", op_pin_state_floats, irop_flag_commutative, oparity_binary, 0, 0, NULL);
108 void arch_dep_set_opts(arch_dep_opts_t the_opts) {
112 /** check, whether a mode allows a Mulh instruction. */
113 static int allow_Mulh(ir_mode *mode) {
114 if (get_mode_size_bits(mode) > params->max_bits_for_mulh)
116 return (mode_is_signed(mode) && params->allow_mulhs) || (!mode_is_signed(mode) && params->allow_mulhu);
122 typedef struct instruction instruction;
124 insn_kind kind; /**< the instruction kind */
125 instruction *in[2]; /**< the ins */
126 int shift_count; /**< shift count for LEA and SHIFT */
127 ir_node *irn; /**< the generated node for this instruction if any. */
128 int costs; /**< the costs for this instruction */
132 * The environment for the strength reduction of multiplications.
134 typedef struct _mul_env {
135 struct obstack obst; /**< an obstack for local space. */
136 ir_mode *mode; /**< the mode of the multiplication constant */
137 int bits; /**< number of bits in the mode */
138 unsigned max_S; /**< the maximum LEA shift value. */
139 instruction *root; /**< the root of the instruction tree */
140 ir_node *op; /**< the operand that is multiplied */
141 ir_node *blk; /**< the block where the new graph is built */
142 dbg_info *dbg; /**< the debug info for the new graph. */
143 ir_mode *shf_mode; /**< the (unsigned) mode for the shift constants */
144 int fail; /**< set to 1 if the instruction sequence fails the constraints */
145 int n_shift; /**< maximum number of allowed shift instructions */
147 evaluate_costs_func evaluate; /**< the evaluate callback */
151 * Some kind of default evaluator.
153 static int default_evaluate(insn_kind kind, tarval *tv) {
160 * emit a LEA (or an Add) instruction
162 static instruction *emit_LEA(mul_env *env, instruction *a, instruction *b, int shift) {
163 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
164 res->kind = shift > 0 ? LEA : ADD;
167 res->shift_count = shift;
174 * emit a SHIFT (or an Add) instruction
176 static instruction *emit_SHIFT(mul_env *env, instruction *a, int shift) {
177 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
182 res->shift_count = shift;
187 res->shift_count = 0;
195 * emit a SUB instruction
197 static instruction *emit_SUB(mul_env *env, instruction *a, instruction *b) {
198 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
202 res->shift_count = 0;
209 * emit the ROOT instruction
211 static instruction *emit_ROOT(mul_env *env, ir_node *root_op) {
212 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
216 res->shift_count = 0;
224 * Returns the condensed representation of the tarval tv
226 static unsigned char *value_to_condensed(mul_env *env, tarval *tv, int *pr) {
227 ir_mode *mode = get_tarval_mode(tv);
228 int bits = get_mode_size_bits(mode);
229 char *bitstr = get_tarval_bitpattern(tv);
231 unsigned char *R = obstack_alloc(&env->obst, bits);
234 for (i = 0; bitstr[i] != '\0'; ++i) {
235 if (bitstr[i] == '1') {
248 * Calculate the gain when using the generalized complementary technique
250 static int calculate_gain(unsigned char *R, int r) {
255 /* the gain for r == 1 */
257 for (i = 2; i < r; ++i) {
258 /* calculate the gain for r from the gain for r-1 */
259 gain += 2 - R[i - 1];
261 if (gain > max_gain) {
272 * Calculates the condensed complement of a given (R,r) tuple
274 static unsigned char *complement_condensed(mul_env *env, unsigned char *R, int r, int gain, int *prs) {
275 unsigned char *value = obstack_alloc(&env->obst, env->bits);
279 memset(value, 0, env->bits);
282 for (i = 0; i < gain; ++i) {
287 /* negate and propagate 1 */
289 for (i = 0; i <= j; ++i) {
290 unsigned char v = !value[i];
296 /* condense it again */
299 for (i = 0; i <= j; ++i) {
312 * creates a tarval from a condensed representation.
314 static tarval *condensed_to_value(mul_env *env, unsigned char *R, int r) {
319 tv = get_mode_one(env->mode);
321 for (i = 0; i < r; ++i) {
324 tarval *t = new_tarval_from_long(j, mode_Iu);
325 tv = tarval_shl(tv, t);
327 res = res ? tarval_add(res, tv) : tv;
333 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N);
336 * handle simple cases with up-to 2 bits set
338 static instruction *decompose_simple_cases(mul_env *env, unsigned char *R, int r, tarval *N) {
339 instruction *ins, *ins2;
342 return emit_SHIFT(env, env->root, R[0]);
348 ins = emit_SHIFT(env, ins, R[0]);
350 if (R[1] <= env->max_S)
351 return emit_LEA(env, ins, ins, R[1]);
353 ins2 = emit_SHIFT(env, env->root, R[0] + R[1]);
354 return emit_LEA(env, ins, ins2, 0);
359 * Main decompose driver.
361 static instruction *decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
366 return decompose_simple_cases(env, R, r, N);
368 if (params->also_use_subs) {
369 gain = calculate_gain(R, r);
371 instruction *instr1, *instr2;
372 unsigned char *R1, *R2;
375 R1 = complement_condensed(env, R, r, gain, &r1);
377 R2 = obstack_alloc(&env->obst, r2);
380 for (i = 0; i < gain; ++i) {
385 for (i = gain; i < r; ++i) {
389 instr1 = decompose_mul(env, R1, r1, NULL);
390 instr2 = decompose_mul(env, R2, r2, NULL);
391 return emit_SUB(env, instr2, instr1);
396 N = condensed_to_value(env, R, r);
398 for (i = env->max_S; i > 0; --i) {
399 tarval *div_res, *mod_res;
400 tarval *tv = new_tarval_from_long((1 << i) + 1, env->mode);
402 div_res = tarval_divmod(N, tv, &mod_res);
403 if (mod_res == get_mode_null(env->mode)) {
407 Rs = value_to_condensed(env, div_res, &rs);
409 instruction *N1 = decompose_mul(env, Rs, rs, div_res);
410 return emit_LEA(env, N1, N1, i);
414 return basic_decompose_mul(env, R, r, N);
418 * basic decomposition routine
420 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
424 if (R[0] == 0) { /* Case 1 */
425 t = R[1] > max(env->max_S, R[1]);
427 Ns = decompose_mul(env, &R[1], r - 1, N);
428 return emit_LEA(env, env->root, Ns, t);
429 } else if (R[0] <= env->max_S) { /* Case 2 */
432 Ns = decompose_mul(env, &R[1], r - 1, N);
433 return emit_LEA(env, Ns, env->root, t);
437 Ns = decompose_mul(env, R, r, N);
438 return emit_SHIFT(env, Ns, t);
443 * recursive build the graph form the instructions.
445 * @param env the environment
446 * @param inst the instruction
448 static ir_node *build_graph(mul_env *env, instruction *inst) {
454 switch (inst->kind) {
456 l = build_graph(env, inst->in[0]);
457 r = build_graph(env, inst->in[1]);
458 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
459 r = new_rd_Shl(env->dbg, current_ir_graph, env->blk, r, c, env->mode);
460 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
462 l = build_graph(env, inst->in[0]);
463 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
464 return inst->irn = new_rd_Shl(env->dbg, current_ir_graph, env->blk, l, c, env->mode);
466 l = build_graph(env, inst->in[0]);
467 r = build_graph(env, inst->in[1]);
468 return inst->irn = new_rd_Sub(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
470 l = build_graph(env, inst->in[0]);
471 r = build_graph(env, inst->in[1]);
472 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
480 * Calculate the costs for the given instruction sequence.
481 * Note that additional costs due to higher register pressure are NOT evaluated yet
483 static int evaluate_insn(mul_env *env, instruction *inst) {
486 if (inst->costs >= 0) {
487 /* was already evaluated */
491 switch (inst->kind) {
495 costs = evaluate_insn(env, inst->in[0]);
496 costs += evaluate_insn(env, inst->in[1]);
497 costs += env->evaluate(inst->kind, NULL);
501 if (inst->shift_count > params->highest_shift_amount)
503 if (env->n_shift <= 0)
507 costs = evaluate_insn(env, inst->in[0]);
508 costs += env->evaluate(inst->kind, NULL);
518 * Evaluate the replacement instructions and build a new graph
519 * if faster than the Mul.
520 * returns the root of the new graph then or irn otherwise.
522 * @param irn the Mul operation
523 * @param operand the multiplication operand
524 * @param tv the multiplication constant
526 * @return the new graph
528 static ir_node *do_decomposition(ir_node *irn, ir_node *operand, tarval *tv) {
536 obstack_init(&env.obst);
537 env.mode = get_tarval_mode(tv);
538 env.bits = get_mode_size_bits(env.mode);
540 env.root = emit_ROOT(&env, operand);
542 env.n_shift = params->maximum_shifts;
543 env.evaluate = params->evaluate != NULL ? params->evaluate : default_evaluate;
545 R = value_to_condensed(&env, tv, &r);
546 inst = decompose_mul(&env, R, r, tv);
548 /* the paper suggests 70% here */
549 mul_costs = (env.evaluate(MUL, tv) * 7) / 10;
550 if (evaluate_insn(&env, inst) <= mul_costs && !env.fail) {
552 env.blk = get_nodes_block(irn);
553 env.dbg = get_irn_dbg_info(irn);
554 env.shf_mode = find_unsigned_mode(env.mode);
555 if (env.shf_mode == NULL)
556 env.shf_mode = mode_Iu;
558 res = build_graph(&env, inst);
560 obstack_free(&env.obst, NULL);
564 /* Replace Muls with Shifts and Add/Subs. */
565 ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn) {
567 ir_mode *mode = get_irn_mode(irn);
569 /* If the architecture dependent optimizations were not initialized
570 or this optimization was not enabled. */
571 if (params == NULL || (opts & arch_dep_mul_to_shift) == 0)
574 if (is_Mul(irn) && mode_is_int(mode)) {
575 ir_node *block = get_nodes_block(irn);
576 ir_node *left = get_binop_left(irn);
577 ir_node *right = get_binop_right(irn);
579 ir_node *operand = NULL;
581 /* Look, if one operand is a constant. */
582 if (is_Const(left)) {
583 tv = get_Const_tarval(left);
585 } else if (is_Const(right)) {
586 tv = get_Const_tarval(right);
591 res = do_decomposition(irn, operand, tv);
594 hook_arch_dep_replace_mul_with_shifts(irn);
604 * calculated the ld2 of a tarval if tarval is 2^n, else returns -1.
606 static int tv_ld2(tarval *tv, int bits) {
609 for (num = i = 0; i < bits; ++i) {
610 unsigned char v = get_tarval_sub_bits(tv, i);
615 for (j = 0; j < 8; ++j)
628 /* for shorter lines */
629 #define ABS(a) tarval_abs(a)
630 #define NEG(a) tarval_neg(a)
631 #define NOT(a) tarval_not(a)
632 #define SHL(a, b) tarval_shl(a, b)
633 #define SHR(a, b) tarval_shr(a, b)
634 #define ADD(a, b) tarval_add(a, b)
635 #define SUB(a, b) tarval_sub(a, b)
636 #define MUL(a, b) tarval_mul(a, b)
637 #define DIV(a, b) tarval_div(a, b)
638 #define MOD(a, b) tarval_mod(a, b)
639 #define CMP(a, b) tarval_cmp(a, b)
640 #define CNV(a, m) tarval_convert_to(a, m)
641 #define ONE(m) get_mode_one(m)
642 #define ZERO(m) get_mode_null(m)
644 /** The result of a the magic() function. */
646 tarval *M; /**< magic number */
647 int s; /**< shift amount */
648 int need_add; /**< an additional add is needed */
649 int need_sub; /**< an additional sub is needed */
653 * Signed division by constant d: calculate the Magic multiplier M and the shift amount s
655 * see Hacker's Delight: 10-6 Integer Division by Constants: Incorporation into a Compiler
657 static struct ms magic(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 tarval *ad, *anc, *delta, *q1, *r1, *q2, *r2, *t; /* unsigned */
665 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) & pn_Cmp_Ge) {
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) & pn_Cmp_Ge) {
701 q2 = ADD(q2, ONE(u_mode));
706 } while (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(u_mode)) & pn_Cmp_Eq));
708 d_cmp = CMP(d, ZERO(mode));
710 if (d_cmp & pn_Cmp_Ge)
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 & pn_Cmp_Gt && M_cmp & pn_Cmp_Lt;
722 /* need a sub if d < 0 && M > 0 */
723 mag.need_sub = d_cmp & pn_Cmp_Lt && M_cmp & pn_Cmp_Gt;
725 tarval_set_integer_overflow_mode(rem);
730 /** The result of the magicu() function. */
732 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(tarval *d) {
743 ir_mode *mode = get_tarval_mode(d);
744 int bits = get_mode_size_bits(mode);
746 tarval *nc, *delta, *q1, *r1, *q2, *r2;
747 tarval *bits_minus_1, *two_bits_1, *seven_ff;
751 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
753 /* we need overflow mode to work correctly */
754 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
756 bits_minus_1 = new_tarval_from_long(bits - 1, mode);
757 two_bits_1 = SHL(get_mode_one(mode), bits_minus_1);
758 seven_ff = SUB(two_bits_1, ONE(mode));
760 magu.need_add = 0; /* initialize the add indicator */
761 nc = SUB(NEG(ONE(mode)), MOD(NEG(d), d));
762 p = bits - 1; /* Init: p */
763 q1 = DIV(two_bits_1, nc); /* Init: q1 = 2^p/nc */
764 r1 = SUB(two_bits_1, MUL(q1, nc)); /* Init: r1 = rem(2^p, nc) */
765 q2 = DIV(seven_ff, d); /* Init: q2 = (2^p - 1)/d */
766 r2 = SUB(seven_ff, MUL(q2, d)); /* Init: r2 = rem(2^p - 1, d) */
770 if (CMP(r1, SUB(nc, r1)) & pn_Cmp_Ge) {
771 q1 = ADD(ADD(q1, q1), ONE(mode));
772 r1 = SUB(ADD(r1, r1), nc);
779 if (CMP(ADD(r2, ONE(mode)), SUB(d, r2)) & pn_Cmp_Ge) {
780 if (CMP(q2, seven_ff) & pn_Cmp_Ge)
783 q2 = ADD(ADD(q2, q2), ONE(mode));
784 r2 = SUB(ADD(ADD(r2, r2), ONE(mode)), d);
787 if (CMP(q2, two_bits_1) & pn_Cmp_Ge)
791 r2 = ADD(ADD(r2, r2), ONE(mode));
793 delta = SUB(SUB(d, ONE(mode)), r2);
794 } while (p < 2*bits &&
795 (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(mode)) & pn_Cmp_Eq)));
797 magu.M = ADD(q2, ONE(mode)); /* Magic number */
798 magu.s = p - bits; /* and shift amount */
800 tarval_set_integer_overflow_mode(rem);
806 * Build the Mulh replacement code for n / tv.
808 * Note that 'div' might be a mod or DivMod operation as well
810 static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv) {
811 dbg_info *dbg = get_irn_dbg_info(div);
812 ir_node *n = get_binop_left(div);
813 ir_node *block = get_irn_n(div, -1);
814 ir_mode *mode = get_irn_mode(n);
815 int bits = get_mode_size_bits(mode);
818 /* Beware: do not transform bad code */
819 if (is_Bad(n) || is_Bad(block))
822 if (mode_is_signed(mode)) {
823 struct ms mag = magic(tv);
825 /* generate the Mulh instruction */
826 c = new_r_Const(current_ir_graph, block, mode, mag.M);
827 q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
829 /* do we need an Add or Sub */
831 q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
832 else if (mag.need_sub)
833 q = new_rd_Sub(dbg, current_ir_graph, block, q, n, mode);
835 /* Do we need the shift */
837 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
838 q = new_rd_Shrs(dbg, current_ir_graph, block, q, c, mode);
842 c = new_r_Const_long(current_ir_graph, block, mode_Iu, bits-1);
843 t = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
845 q = new_rd_Add(dbg, current_ir_graph, block, q, t, mode);
847 struct mu mag = magicu(tv);
850 /* generate the Mulh instruction */
851 c = new_r_Const(current_ir_graph, block, mode, mag.M);
852 q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
856 /* use the GM scheme */
857 t = new_rd_Sub(dbg, current_ir_graph, block, n, q, mode);
859 c = new_r_Const(current_ir_graph, block, mode_Iu, get_mode_one(mode_Iu));
860 t = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
862 t = new_rd_Add(dbg, current_ir_graph, block, t, q, mode);
864 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s-1);
865 q = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
867 /* use the default scheme */
868 q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
870 } else if (mag.s > 0) { /* default scheme, shift needed */
871 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
872 q = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
878 /* Replace Divs with Shifts and Add/Subs and Mulh. */
879 ir_node *arch_dep_replace_div_by_const(ir_node *irn) {
882 /* If the architecture dependent optimizations were not initialized
883 or this optimization was not enabled. */
884 if (params == NULL || (opts & arch_dep_div_by_const) == 0)
887 if (get_irn_opcode(irn) == iro_Div) {
888 ir_node *c = get_Div_right(irn);
889 ir_node *block, *left;
896 if (get_irn_op(c) != op_Const)
899 tv = get_Const_tarval(c);
901 /* check for division by zero */
902 if (classify_tarval(tv) == TV_CLASSIFY_NULL)
905 left = get_Div_left(irn);
906 mode = get_irn_mode(left);
907 block = get_irn_n(irn, -1);
908 dbg = get_irn_dbg_info(irn);
910 bits = get_mode_size_bits(mode);
914 if (mode_is_signed(mode)) {
915 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
916 ntv = tarval_neg(tv);
926 if (k >= 0) { /* division by 2^k or -2^k */
927 if (mode_is_signed(mode)) {
929 ir_node *curr = left;
932 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
933 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
936 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
937 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
939 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
941 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
942 res = new_rd_Shrs(dbg, current_ir_graph, block, curr, k_node, mode);
944 if (n_flag) { /* negate the result */
947 k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
948 res = new_rd_Sub(dbg, current_ir_graph, block, k_node, res, mode);
950 } else { /* unsigned case */
953 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
954 res = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
958 if (allow_Mulh(mode))
959 res = replace_div_by_mulh(irn, tv);
964 hook_arch_dep_replace_division_by_const(irn);
969 /* Replace Mods with Shifts and Add/Subs and Mulh. */
970 ir_node *arch_dep_replace_mod_by_const(ir_node *irn) {
973 /* If the architecture dependent optimizations were not initialized
974 or this optimization was not enabled. */
975 if (params == NULL || (opts & arch_dep_mod_by_const) == 0)
978 if (get_irn_opcode(irn) == iro_Mod) {
979 ir_node *c = get_Mod_right(irn);
980 ir_node *block, *left;
987 if (get_irn_op(c) != op_Const)
990 tv = get_Const_tarval(c);
992 /* check for division by zero */
993 if (classify_tarval(tv) == TV_CLASSIFY_NULL)
996 left = get_Mod_left(irn);
997 mode = get_irn_mode(left);
998 block = get_irn_n(irn, -1);
999 dbg = get_irn_dbg_info(irn);
1000 bits = get_mode_size_bits(mode);
1004 if (mode_is_signed(mode)) {
1005 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1006 ntv = tarval_neg(tv);
1015 /* division by 2^k or -2^k:
1016 * we use "modulus" here, so x % y == x % -y that's why is no difference between the case 2^k and -2^k
1018 if (mode_is_signed(mode)) {
1020 ir_node *curr = left;
1023 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1024 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1027 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1028 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1030 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1032 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1033 curr = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1035 res = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1036 } else { /* unsigned case */
1039 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1040 res = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1043 /* other constant */
1044 if (allow_Mulh(mode)) {
1045 res = replace_div_by_mulh(irn, tv);
1047 res = new_rd_Mul(dbg, current_ir_graph, block, res, c, mode);
1049 /* res = arch_dep_mul_to_shift(res); */
1051 res = new_rd_Sub(dbg, current_ir_graph, block, left, res, mode);
1057 hook_arch_dep_replace_division_by_const(irn);
1062 /* Replace DivMods with Shifts and Add/Subs and Mulh. */
1063 void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn) {
1066 /* If the architecture dependent optimizations were not initialized
1067 or this optimization was not enabled. */
1068 if (params == NULL ||
1069 ((opts & (arch_dep_div_by_const|arch_dep_mod_by_const)) != (arch_dep_div_by_const|arch_dep_mod_by_const)))
1072 if (get_irn_opcode(irn) == iro_DivMod) {
1073 ir_node *c = get_DivMod_right(irn);
1074 ir_node *block, *left;
1081 if (get_irn_op(c) != op_Const)
1084 tv = get_Const_tarval(c);
1086 /* check for division by zero */
1087 if (classify_tarval(tv) == TV_CLASSIFY_NULL)
1090 left = get_DivMod_left(irn);
1091 mode = get_irn_mode(left);
1092 block = get_irn_n(irn, -1);
1093 dbg = get_irn_dbg_info(irn);
1095 bits = get_mode_size_bits(mode);
1099 if (mode_is_signed(mode)) {
1100 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1101 ntv = tarval_neg(tv);
1111 if (k >= 0) { /* division by 2^k or -2^k */
1112 if (mode_is_signed(mode)) {
1113 ir_node *k_node, *c_k;
1114 ir_node *curr = left;
1117 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1118 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1121 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1122 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1124 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1126 c_k = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1128 *div = new_rd_Shrs(dbg, current_ir_graph, block, curr, c_k, mode);
1130 if (n_flag) { /* negate the div result */
1133 k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
1134 *div = new_rd_Sub(dbg, current_ir_graph, block, k_node, *div, mode);
1137 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1138 curr = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1140 *mod = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1141 } else { /* unsigned case */
1144 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1145 *div = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
1147 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1148 *mod = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1151 /* other constant */
1152 if (allow_Mulh(mode)) {
1155 *div = replace_div_by_mulh(irn, tv);
1157 t = new_rd_Mul(dbg, current_ir_graph, block, *div, c, mode);
1159 /* t = arch_dep_mul_to_shift(t); */
1161 *mod = new_rd_Sub(dbg, current_ir_graph, block, left, t, mode);
1167 hook_arch_dep_replace_division_by_const(irn);
1171 static const ir_settings_arch_dep_t default_params = {
1172 1, /* also use subs */
1173 4, /* maximum shifts */
1174 31, /* maximum shift amount */
1176 0, /* allow Mulhs */
1177 0, /* allow Mulus */
1178 32 /* Mulh allowed up to 32 bit */
1181 /* A default parameter factory for testing purposes. */
1182 const ir_settings_arch_dep_t *arch_dep_default_factory(void) {
1183 return &default_params;