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",
41 #include "irgraph_t.h"
48 #include "dbginfo_t.h"
49 #include "iropt_dbg.h"
61 /* when we need verifying */
63 # define IRN_VRFY_IRG(res, irg)
65 # define IRN_VRFY_IRG(res, irg) irn_vrfy_irg(res, irg)
68 /** The params got from the factory in arch_dep_init(...). */
69 static const ir_settings_arch_dep_t *params = NULL;
71 /** The bit mask, which optimizations to apply. */
72 static arch_dep_opts_t opts;
74 void arch_dep_init(arch_dep_params_factory_t factory) {
81 void arch_dep_set_opts(arch_dep_opts_t the_opts) {
85 /** check, whether a mode allows a Mulh instruction. */
86 static int allow_Mulh(ir_mode *mode) {
87 if (get_mode_size_bits(mode) > params->max_bits_for_mulh)
89 return (mode_is_signed(mode) && params->allow_mulhs) || (!mode_is_signed(mode) && params->allow_mulhu);
95 typedef struct instruction instruction;
97 insn_kind kind; /**< the instruction kind */
98 instruction *in[2]; /**< the ins */
99 unsigned shift_count; /**< shift count for LEA and SHIFT */
100 ir_node *irn; /**< the generated node for this instruction if any. */
101 int costs; /**< the costs for this instruction */
105 * The environment for the strength reduction of multiplications.
107 typedef struct _mul_env {
108 struct obstack obst; /**< an obstack for local space. */
109 ir_mode *mode; /**< the mode of the multiplication constant */
110 unsigned bits; /**< number of bits in the mode */
111 unsigned max_S; /**< the maximum LEA shift value. */
112 instruction *root; /**< the root of the instruction tree */
113 ir_node *op; /**< the operand that is multiplied */
114 ir_node *blk; /**< the block where the new graph is built */
115 dbg_info *dbg; /**< the debug info for the new graph. */
116 ir_mode *shf_mode; /**< the (unsigned) mode for the shift constants */
117 int fail; /**< set to 1 if the instruction sequence fails the constraints */
118 int n_shift; /**< maximum number of allowed shift instructions */
120 evaluate_costs_func evaluate; /**< the evaluate callback */
124 * Some kind of default evaluator. Return the cost of
127 static int default_evaluate(insn_kind kind, tarval *tv) {
136 * emit a LEA (or an Add) instruction
138 static instruction *emit_LEA(mul_env *env, instruction *a, instruction *b, unsigned shift) {
139 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
140 res->kind = shift > 0 ? LEA : ADD;
143 res->shift_count = shift;
150 * emit a SHIFT (or an Add or a Zero) instruction
152 static instruction *emit_SHIFT(mul_env *env, instruction *a, unsigned shift) {
153 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
154 if (shift == env->bits) {
155 /* a 2^bits with bits resolution is a zero */
159 res->shift_count = 0;
160 } else if (shift != 1) {
164 res->shift_count = shift;
169 res->shift_count = 0;
177 * emit a SUB instruction
179 static instruction *emit_SUB(mul_env *env, instruction *a, instruction *b) {
180 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
184 res->shift_count = 0;
191 * emit the ROOT instruction
193 static instruction *emit_ROOT(mul_env *env, ir_node *root_op) {
194 instruction *res = obstack_alloc(&env->obst, sizeof(*res));
198 res->shift_count = 0;
206 * Returns the condensed representation of the tarval tv
208 static unsigned char *value_to_condensed(mul_env *env, tarval *tv, int *pr) {
209 ir_mode *mode = get_tarval_mode(tv);
210 int bits = get_mode_size_bits(mode);
211 char *bitstr = get_tarval_bitpattern(tv);
213 unsigned char *R = obstack_alloc(&env->obst, bits);
216 for (i = 0; bitstr[i] != '\0'; ++i) {
217 if (bitstr[i] == '1') {
230 * Calculate the gain when using the generalized complementary technique
232 static int calculate_gain(unsigned char *R, int r) {
237 /* the gain for r == 1 */
239 for (i = 2; i < r; ++i) {
240 /* calculate the gain for r from the gain for r-1 */
241 gain += 2 - R[i - 1];
243 if (gain > max_gain) {
252 * Calculates the condensed complement of a given (R,r) tuple
254 static unsigned char *complement_condensed(mul_env *env, unsigned char *R, int r, int gain, int *prs) {
255 unsigned char *value = obstack_alloc(&env->obst, env->bits);
259 memset(value, 0, env->bits);
262 for (i = 0; i < gain; ++i) {
267 /* negate and propagate 1 */
269 for (i = 0; i <= j; ++i) {
270 unsigned char v = !value[i];
276 /* condense it again */
279 for (i = 0; i <= j; ++i) {
292 * creates a tarval from a condensed representation.
294 static tarval *condensed_to_value(mul_env *env, unsigned char *R, int r) {
299 tv = get_mode_one(env->mode);
301 for (i = 0; i < r; ++i) {
304 tarval *t = new_tarval_from_long(j, mode_Iu);
305 tv = tarval_shl(tv, t);
307 res = res ? tarval_add(res, tv) : tv;
313 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N);
316 * handle simple cases with up-to 2 bits set
318 static instruction *decompose_simple_cases(mul_env *env, unsigned char *R, int r, tarval *N) {
319 instruction *ins, *ins2;
323 return emit_SHIFT(env, env->root, R[0]);
328 if (R[1] <= env->max_S) {
329 ins = emit_LEA(env, ins, ins, R[1]);
331 ins = emit_SHIFT(env, ins, R[0]);
336 ins = emit_SHIFT(env, ins, R[0]);
339 ins2 = emit_SHIFT(env, env->root, R[0] + R[1]);
340 return emit_LEA(env, ins, ins2, 0);
345 * Main decompose driver.
347 static instruction *decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
352 return decompose_simple_cases(env, R, r, N);
354 if (params->also_use_subs) {
355 gain = calculate_gain(R, r);
357 instruction *instr1, *instr2;
358 unsigned char *R1, *R2;
361 R1 = complement_condensed(env, R, r, gain, &r1);
363 R2 = obstack_alloc(&env->obst, r2);
366 for (i = 0; i < gain; ++i) {
373 /* Two identical bits: normalize */
378 for (i = gain + 1; i < r; ++i) {
382 instr1 = decompose_mul(env, R1, r1, NULL);
383 instr2 = decompose_mul(env, R2, r2, NULL);
384 return emit_SUB(env, instr2, instr1);
389 N = condensed_to_value(env, R, r);
391 for (i = env->max_S; i > 0; --i) {
392 tarval *div_res, *mod_res;
393 tarval *tv = new_tarval_from_long((1 << i) + 1, env->mode);
395 div_res = tarval_divmod(N, tv, &mod_res);
396 if (mod_res == get_mode_null(env->mode)) {
400 Rs = value_to_condensed(env, div_res, &rs);
402 instruction *N1 = decompose_mul(env, Rs, rs, div_res);
403 return emit_LEA(env, N1, N1, i);
407 return basic_decompose_mul(env, R, r, N);
410 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
413 * basic decomposition routine
415 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
419 if (R[0] == 0) { /* Case 1 */
420 t = R[1] > IMAX(env->max_S, R[1]);
422 Ns = decompose_mul(env, &R[1], r - 1, N);
423 return emit_LEA(env, env->root, Ns, t);
424 } else if (R[0] <= env->max_S) { /* Case 2 */
427 Ns = decompose_mul(env, &R[1], r - 1, N);
428 return emit_LEA(env, Ns, env->root, t);
432 Ns = decompose_mul(env, R, r, N);
433 return emit_SHIFT(env, Ns, t);
438 * recursive build the graph form the instructions.
440 * @param env the environment
441 * @param inst the instruction
443 static ir_node *build_graph(mul_env *env, instruction *inst) {
449 switch (inst->kind) {
451 l = build_graph(env, inst->in[0]);
452 r = build_graph(env, inst->in[1]);
453 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
454 r = new_rd_Shl(env->dbg, current_ir_graph, env->blk, r, c, env->mode);
455 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
457 l = build_graph(env, inst->in[0]);
458 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
459 return inst->irn = new_rd_Shl(env->dbg, current_ir_graph, env->blk, l, c, env->mode);
461 l = build_graph(env, inst->in[0]);
462 r = build_graph(env, inst->in[1]);
463 return inst->irn = new_rd_Sub(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
465 l = build_graph(env, inst->in[0]);
466 r = build_graph(env, inst->in[1]);
467 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
469 return inst->irn = new_r_Const(current_ir_graph, env->blk, env->mode, get_mode_null(env->mode));
471 panic("Unsupported instruction kind");
477 * Calculate the costs for the given instruction sequence.
478 * Note that additional costs due to higher register pressure are NOT evaluated yet
480 static int evaluate_insn(mul_env *env, instruction *inst) {
483 if (inst->costs >= 0) {
484 /* was already evaluated */
488 switch (inst->kind) {
492 costs = evaluate_insn(env, inst->in[0]);
493 costs += evaluate_insn(env, inst->in[1]);
494 costs += env->evaluate(inst->kind, NULL);
498 if (inst->shift_count > params->highest_shift_amount)
500 if (env->n_shift <= 0)
504 costs = evaluate_insn(env, inst->in[0]);
505 costs += env->evaluate(inst->kind, NULL);
509 inst->costs = costs = env->evaluate(inst->kind, NULL);
512 panic("Unsupported instruction kind");
516 * Evaluate the replacement instructions and build a new graph
517 * if faster than the Mul.
518 * Returns the root of the new graph then or irn otherwise.
520 * @param irn the Mul operation
521 * @param operand the multiplication operand
522 * @param tv the multiplication constant
524 * @return the new graph
526 static ir_node *do_decomposition(ir_node *irn, ir_node *operand, tarval *tv) {
534 obstack_init(&env.obst);
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 = params->maximum_shifts;
541 env.evaluate = params->evaluate != NULL ? params->evaluate : default_evaluate;
543 R = value_to_condensed(&env, tv, &r);
544 inst = decompose_mul(&env, R, r, tv);
546 /* the paper suggests 70% here */
547 mul_costs = (env.evaluate(MUL, tv) * 7 + 5) / 10;
548 if (evaluate_insn(&env, inst) <= mul_costs && !env.fail) {
550 env.blk = get_nodes_block(irn);
551 env.dbg = get_irn_dbg_info(irn);
552 env.shf_mode = find_unsigned_mode(env.mode);
553 if (env.shf_mode == NULL)
554 env.shf_mode = mode_Iu;
556 res = build_graph(&env, inst);
558 obstack_free(&env.obst, NULL);
562 /* Replace Muls with Shifts and Add/Subs. */
563 ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn) {
565 ir_mode *mode = get_irn_mode(irn);
567 /* If the architecture dependent optimizations were not initialized
568 or this optimization was not enabled. */
569 if (params == NULL || (opts & arch_dep_mul_to_shift) == 0)
572 set_arch_dep_running(1);
574 if (is_Mul(irn) && mode_is_int(mode)) {
575 ir_node *left = get_binop_left(irn);
576 ir_node *right = get_binop_right(irn);
578 ir_node *operand = NULL;
580 /* Look, if one operand is a constant. */
581 if (is_Const(left)) {
582 tv = get_Const_tarval(left);
584 } else if (is_Const(right)) {
585 tv = get_Const_tarval(right);
590 res = do_decomposition(irn, operand, tv);
593 hook_arch_dep_replace_mul_with_shifts(irn);
599 //set_arch_dep_running(0);
605 * calculated the ld2 of a tarval if tarval is 2^n, else returns -1.
607 static int tv_ld2(tarval *tv, int bits) {
610 for (num = i = 0; i < bits; ++i) {
611 unsigned char v = get_tarval_sub_bits(tv, i);
616 for (j = 0; j < 8; ++j)
629 /* for shorter lines */
630 #define ABS(a) tarval_abs(a)
631 #define NEG(a) tarval_neg(a)
632 #define NOT(a) tarval_not(a)
633 #define SHL(a, b) tarval_shl(a, b)
634 #define SHR(a, b) tarval_shr(a, b)
635 #define ADD(a, b) tarval_add(a, b)
636 #define SUB(a, b) tarval_sub(a, b, NULL)
637 #define MUL(a, b) tarval_mul(a, b)
638 #define DIV(a, b) tarval_div(a, b)
639 #define MOD(a, b) tarval_mod(a, b)
640 #define CMP(a, b) tarval_cmp(a, b)
641 #define CNV(a, m) tarval_convert_to(a, m)
642 #define ONE(m) get_mode_one(m)
643 #define ZERO(m) get_mode_null(m)
645 /** The result of a the magic() function. */
647 tarval *M; /**< magic number */
648 int s; /**< shift amount */
649 int need_add; /**< an additional add is needed */
650 int need_sub; /**< an additional sub is needed */
654 * Signed division by constant d: calculate the Magic multiplier M and the shift amount s
656 * see Hacker's Delight: 10-6 Integer Division by Constants: Incorporation into a Compiler
658 static struct ms magic(tarval *d) {
659 ir_mode *mode = get_tarval_mode(d);
660 ir_mode *u_mode = find_unsigned_mode(mode);
661 int bits = get_mode_size_bits(u_mode);
663 tarval *ad, *anc, *delta, *q1, *r1, *q2, *r2, *t; /* unsigned */
666 tarval *bits_minus_1, *two_bits_1;
670 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
672 /* we need overflow mode to work correctly */
673 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
676 bits_minus_1 = new_tarval_from_long(bits - 1, u_mode);
677 two_bits_1 = SHL(get_mode_one(u_mode), bits_minus_1);
679 ad = CNV(ABS(d), u_mode);
680 t = ADD(two_bits_1, SHR(CNV(d, u_mode), bits_minus_1));
681 anc = SUB(SUB(t, ONE(u_mode)), MOD(t, ad)); /* Absolute value of nc */
682 p = bits - 1; /* Init: p */
683 q1 = DIV(two_bits_1, anc); /* Init: q1 = 2^p/|nc| */
684 r1 = SUB(two_bits_1, MUL(q1, anc)); /* Init: r1 = rem(2^p, |nc|) */
685 q2 = DIV(two_bits_1, ad); /* Init: q2 = 2^p/|d| */
686 r2 = SUB(two_bits_1, MUL(q2, ad)); /* Init: r2 = rem(2^p, |d|) */
690 q1 = ADD(q1, q1); /* Update q1 = 2^p/|nc| */
691 r1 = ADD(r1, r1); /* Update r1 = rem(2^p, |nc|) */
693 if (CMP(r1, anc) & pn_Cmp_Ge) {
694 q1 = ADD(q1, ONE(u_mode));
698 q2 = ADD(q2, q2); /* Update q2 = 2^p/|d| */
699 r2 = ADD(r2, r2); /* Update r2 = rem(2^p, |d|) */
701 if (CMP(r2, ad) & pn_Cmp_Ge) {
702 q2 = ADD(q2, ONE(u_mode));
707 } while (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(u_mode)) & pn_Cmp_Eq));
709 d_cmp = CMP(d, ZERO(mode));
711 if (d_cmp & pn_Cmp_Ge)
712 mag.M = ADD(CNV(q2, mode), ONE(mode));
714 mag.M = SUB(ZERO(mode), ADD(CNV(q2, mode), ONE(mode)));
716 M_cmp = CMP(mag.M, ZERO(mode));
720 /* need an add if d > 0 && M < 0 */
721 mag.need_add = d_cmp & pn_Cmp_Gt && M_cmp & pn_Cmp_Lt;
723 /* need a sub if d < 0 && M > 0 */
724 mag.need_sub = d_cmp & pn_Cmp_Lt && M_cmp & pn_Cmp_Gt;
726 tarval_set_integer_overflow_mode(rem);
731 /** The result of the magicu() function. */
733 tarval *M; /**< magic add constant */
734 int s; /**< shift amount */
735 int need_add; /**< add indicator */
739 * Unsigned division by constant d: calculate the Magic multiplier M and the shift amount s
741 * see Hacker's Delight: 10-10 Integer Division by Constants: Incorporation into a Compiler (Unsigned)
743 static struct mu magicu(tarval *d) {
744 ir_mode *mode = get_tarval_mode(d);
745 int bits = get_mode_size_bits(mode);
747 tarval *nc, *delta, *q1, *r1, *q2, *r2;
748 tarval *bits_minus_1, *two_bits_1, *seven_ff;
752 tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
754 /* we need overflow mode to work correctly */
755 tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
757 bits_minus_1 = new_tarval_from_long(bits - 1, mode);
758 two_bits_1 = SHL(get_mode_one(mode), bits_minus_1);
759 seven_ff = SUB(two_bits_1, ONE(mode));
761 magu.need_add = 0; /* initialize the add indicator */
762 nc = SUB(NEG(ONE(mode)), MOD(NEG(d), d));
763 p = bits - 1; /* Init: p */
764 q1 = DIV(two_bits_1, nc); /* Init: q1 = 2^p/nc */
765 r1 = SUB(two_bits_1, MUL(q1, nc)); /* Init: r1 = rem(2^p, nc) */
766 q2 = DIV(seven_ff, d); /* Init: q2 = (2^p - 1)/d */
767 r2 = SUB(seven_ff, MUL(q2, d)); /* Init: r2 = rem(2^p - 1, d) */
771 if (CMP(r1, SUB(nc, r1)) & pn_Cmp_Ge) {
772 q1 = ADD(ADD(q1, q1), ONE(mode));
773 r1 = SUB(ADD(r1, r1), nc);
780 if (CMP(ADD(r2, ONE(mode)), SUB(d, r2)) & pn_Cmp_Ge) {
781 if (CMP(q2, seven_ff) & pn_Cmp_Ge)
784 q2 = ADD(ADD(q2, q2), ONE(mode));
785 r2 = SUB(ADD(ADD(r2, r2), ONE(mode)), d);
788 if (CMP(q2, two_bits_1) & pn_Cmp_Ge)
792 r2 = ADD(ADD(r2, r2), ONE(mode));
794 delta = SUB(SUB(d, ONE(mode)), r2);
795 } while (p < 2*bits &&
796 (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(mode)) & pn_Cmp_Eq)));
798 magu.M = ADD(q2, ONE(mode)); /* Magic number */
799 magu.s = p - bits; /* and shift amount */
801 tarval_set_integer_overflow_mode(rem);
807 * Build the Mulh replacement code for n / tv.
809 * Note that 'div' might be a mod or DivMod operation as well
811 static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv) {
812 dbg_info *dbg = get_irn_dbg_info(div);
813 ir_node *n = get_binop_left(div);
814 ir_node *block = get_irn_n(div, -1);
815 ir_mode *mode = get_irn_mode(n);
816 int bits = get_mode_size_bits(mode);
819 /* Beware: do not transform bad code */
820 if (is_Bad(n) || is_Bad(block))
823 if (mode_is_signed(mode)) {
824 struct ms mag = magic(tv);
826 /* generate the Mulh instruction */
827 c = new_r_Const(current_ir_graph, block, mode, mag.M);
828 q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
830 /* do we need an Add or Sub */
832 q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
833 else if (mag.need_sub)
834 q = new_rd_Sub(dbg, current_ir_graph, block, q, n, mode);
836 /* Do we need the shift */
838 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
839 q = new_rd_Shrs(dbg, current_ir_graph, block, q, c, mode);
843 c = new_r_Const_long(current_ir_graph, block, mode_Iu, bits-1);
844 t = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
846 q = new_rd_Add(dbg, current_ir_graph, block, q, t, mode);
848 struct mu mag = magicu(tv);
851 /* generate the Mulh instruction */
852 c = new_r_Const(current_ir_graph, block, mode, mag.M);
853 q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
857 /* use the GM scheme */
858 t = new_rd_Sub(dbg, current_ir_graph, block, n, q, mode);
860 c = new_r_Const(current_ir_graph, block, mode_Iu, get_mode_one(mode_Iu));
861 t = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
863 t = new_rd_Add(dbg, current_ir_graph, block, t, q, mode);
865 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s-1);
866 q = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
868 /* use the default scheme */
869 q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
871 } else if (mag.s > 0) { /* default scheme, shift needed */
872 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
873 q = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
879 /* Replace Divs with Shifts and Add/Subs and Mulh. */
880 ir_node *arch_dep_replace_div_by_const(ir_node *irn) {
883 /* If the architecture dependent optimizations were not initialized
884 or this optimization was not enabled. */
885 if (params == NULL || (opts & arch_dep_div_by_const) == 0)
889 ir_node *c = get_Div_right(irn);
890 ir_node *block, *left;
900 tv = get_Const_tarval(c);
902 /* check for division by zero */
903 if (tarval_is_null(tv))
906 left = get_Div_left(irn);
907 mode = get_irn_mode(left);
908 block = get_irn_n(irn, -1);
909 dbg = get_irn_dbg_info(irn);
911 bits = get_mode_size_bits(mode);
915 if (mode_is_signed(mode)) {
916 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
917 ntv = tarval_neg(tv);
927 if (k >= 0) { /* division by 2^k or -2^k */
928 if (mode_is_signed(mode)) {
930 ir_node *curr = left;
932 /* create the correction code for signed values only if there might be a remainder */
933 if (! is_Div_remainderless(irn)) {
935 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
936 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
939 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
940 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
942 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
947 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
948 res = new_rd_Shrs(dbg, current_ir_graph, block, curr, k_node, mode);
950 if (n_flag) { /* negate the result */
953 k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
954 res = new_rd_Sub(dbg, current_ir_graph, block, k_node, res, mode);
956 } else { /* unsigned case */
959 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
960 res = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
964 if (allow_Mulh(mode))
965 res = replace_div_by_mulh(irn, tv);
970 hook_arch_dep_replace_division_by_const(irn);
975 /* Replace Mods with Shifts and Add/Subs and Mulh. */
976 ir_node *arch_dep_replace_mod_by_const(ir_node *irn) {
979 /* If the architecture dependent optimizations were not initialized
980 or this optimization was not enabled. */
981 if (params == NULL || (opts & arch_dep_mod_by_const) == 0)
985 ir_node *c = get_Mod_right(irn);
986 ir_node *block, *left;
996 tv = get_Const_tarval(c);
998 /* check for division by zero */
999 if (tarval_is_null(tv))
1002 left = get_Mod_left(irn);
1003 mode = get_irn_mode(left);
1004 block = get_irn_n(irn, -1);
1005 dbg = get_irn_dbg_info(irn);
1006 bits = get_mode_size_bits(mode);
1010 if (mode_is_signed(mode)) {
1011 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1012 ntv = tarval_neg(tv);
1021 /* division by 2^k or -2^k:
1022 * we use "modulus" here, so x % y == x % -y that's why is no difference between the case 2^k and -2^k
1024 if (mode_is_signed(mode)) {
1026 ir_node *curr = left;
1029 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1030 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1033 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1034 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1036 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1038 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1039 curr = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1041 res = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1042 } else { /* unsigned case */
1045 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1046 res = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1049 /* other constant */
1050 if (allow_Mulh(mode)) {
1051 res = replace_div_by_mulh(irn, tv);
1053 res = new_rd_Mul(dbg, current_ir_graph, block, res, c, mode);
1055 /* res = arch_dep_mul_to_shift(res); */
1057 res = new_rd_Sub(dbg, current_ir_graph, block, left, res, mode);
1063 hook_arch_dep_replace_division_by_const(irn);
1068 /* Replace DivMods with Shifts and Add/Subs and Mulh. */
1069 void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn) {
1072 /* If the architecture dependent optimizations were not initialized
1073 or this optimization was not enabled. */
1074 if (params == NULL ||
1075 ((opts & (arch_dep_div_by_const|arch_dep_mod_by_const)) != (arch_dep_div_by_const|arch_dep_mod_by_const)))
1078 if (is_DivMod(irn)) {
1079 ir_node *c = get_DivMod_right(irn);
1080 ir_node *block, *left;
1090 tv = get_Const_tarval(c);
1092 /* check for division by zero */
1093 if (tarval_is_null(tv))
1096 left = get_DivMod_left(irn);
1097 mode = get_irn_mode(left);
1098 block = get_irn_n(irn, -1);
1099 dbg = get_irn_dbg_info(irn);
1101 bits = get_mode_size_bits(mode);
1105 if (mode_is_signed(mode)) {
1106 /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1107 ntv = tarval_neg(tv);
1117 if (k >= 0) { /* division by 2^k or -2^k */
1118 if (mode_is_signed(mode)) {
1119 ir_node *k_node, *c_k;
1120 ir_node *curr = left;
1123 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1124 curr = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1127 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1128 curr = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1130 curr = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1132 c_k = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1134 *div = new_rd_Shrs(dbg, current_ir_graph, block, curr, c_k, mode);
1136 if (n_flag) { /* negate the div result */
1139 k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
1140 *div = new_rd_Sub(dbg, current_ir_graph, block, k_node, *div, mode);
1143 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1144 curr = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1146 *mod = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1147 } else { /* unsigned case */
1150 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1151 *div = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
1153 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1154 *mod = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1157 /* other constant */
1158 if (allow_Mulh(mode)) {
1161 *div = replace_div_by_mulh(irn, tv);
1163 t = new_rd_Mul(dbg, current_ir_graph, block, *div, c, mode);
1165 /* t = arch_dep_mul_to_shift(t); */
1167 *mod = new_rd_Sub(dbg, current_ir_graph, block, left, t, mode);
1173 hook_arch_dep_replace_division_by_const(irn);
1177 static const ir_settings_arch_dep_t default_params = {
1178 1, /* also use subs */
1179 4, /* maximum shifts */
1180 31, /* maximum shift amount */
1181 default_evaluate, /* default evaluator */
1183 0, /* allow Mulhs */
1184 0, /* allow Mulus */
1185 32 /* Mulh allowed up to 32 bit */
1188 /* A default parameter factory for testing purposes. */
1189 const ir_settings_arch_dep_t *arch_dep_default_factory(void) {
1190 return &default_params;