replaced the max macro
[libfirm] / ir / ir / irarch.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Machine dependent Firm optimizations.
23  * @date    28.9.2004
24  * @author  Sebastian Hack, Michael Beck
25  * @version $Id$
26  *
27  * Implements "Strenght Reduction of Multiplications by Integer Constants" by Youfeng Wu.
28  * Implements Division and Modulo by Consts from "Hackers Delight",
29  */
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #ifdef HAVE_STDLIB_H
35 # include <stdlib.h>
36 #endif
37
38 #include <assert.h>
39
40 #include "irnode_t.h"
41 #include "irgraph_t.h"
42 #include "irmode_t.h"
43 #include "iropt_t.h"
44 #include "ircons_t.h"
45 #include "irgmod.h"
46 #include "irvrfy.h"
47 #include "tv_t.h"
48 #include "dbginfo_t.h"
49 #include "iropt_dbg.h"
50 #include "irflag_t.h"
51 #include "irhooks.h"
52 #include "ircons.h"
53 #include "irarch.h"
54
55 #undef DEB
56
57 #define MAX_BITSTR 64
58
59 /* when we need verifying */
60 #ifdef NDEBUG
61 # define IRN_VRFY_IRG(res, irg)
62 #else
63 # define IRN_VRFY_IRG(res, irg)  irn_vrfy_irg(res, irg)
64 #endif
65
66 /** The params got from the factory in arch_dep_init(...). */
67 static const ir_settings_arch_dep_t *params = NULL;
68
69 /** The bit mask, which optimizations to apply. */
70 static arch_dep_opts_t opts;
71
72 /* we need this new pseudo op */
73 static ir_op *op_Mulh = NULL;
74
75 /**
76  * construct a Mulh: Mulh(a,b) = (a * b) >> w, w is the with in bits of a, b
77  */
78 static ir_node *
79 new_rd_Mulh (dbg_info *db, ir_graph *irg, ir_node *block,
80        ir_node *op1, ir_node *op2, ir_mode *mode) {
81         ir_node *in[2];
82         ir_node *res;
83
84         in[0] = op1;
85         in[1] = op2;
86         res = new_ir_node(db, irg, block, op_Mulh, mode, 2, in);
87         res = optimize_node(res);
88         IRN_VRFY_IRG(res, irg);
89         return res;
90 }
91
92 ir_op *get_op_Mulh(void)  { return op_Mulh; }
93
94 void arch_dep_init(arch_dep_params_factory_t factory) {
95         opts = arch_dep_none;
96
97         if (factory != NULL)
98                 params = factory();
99
100         if (! op_Mulh) {
101                 int mulh_opc = get_next_ir_opcode();
102
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);
105         }
106 }
107
108 void arch_dep_set_opts(arch_dep_opts_t the_opts) {
109         opts = the_opts;
110 }
111
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)
115                 return 0;
116         return (mode_is_signed(mode) && params->allow_mulhs) || (!mode_is_signed(mode) && params->allow_mulhu);
117 }
118
119 /**
120  * An instruction,
121  */
122 typedef struct instruction instruction;
123 struct 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 */
129 };
130
131 /**
132  * The environment for the strength reduction of multiplications.
133  */
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 */
146
147         evaluate_costs_func evaluate;  /**< the evaluate callback */
148 } mul_env;
149
150 /**
151  * Some kind of default evaluator.
152  */
153 static int default_evaluate(insn_kind kind, tarval *tv) {
154         if (kind == MUL)
155                 return 13;
156         return 1;
157 }
158
159 /**
160  * emit a LEA (or an Add) instruction
161  */
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;
165         res->in[0] = a;
166         res->in[1] = b;
167         res->shift_count = shift;
168         res->irn = NULL;
169         res->costs = -1;
170         return res;
171 }
172
173 /**
174  * emit a SHIFT (or an Add) instruction
175  */
176 static instruction *emit_SHIFT(mul_env *env, instruction *a, int shift) {
177         instruction *res = obstack_alloc(&env->obst, sizeof(*res));
178         if (shift != 1) {
179                 res->kind = SHIFT;
180                 res->in[0] = a;
181                 res->in[1] = NULL;
182                 res->shift_count = shift;
183         } else {
184                 res->kind = ADD;
185                 res->in[0] = a;
186                 res->in[1] = a;
187                 res->shift_count = 0;
188         }
189         res->irn = NULL;
190         res->costs = -1;
191         return res;
192 }
193
194 /**
195  * emit a SUB instruction
196  */
197 static instruction *emit_SUB(mul_env *env, instruction *a, instruction *b) {
198         instruction *res = obstack_alloc(&env->obst, sizeof(*res));
199         res->kind = SUB;
200         res->in[0] = a;
201         res->in[1] = b;
202         res->shift_count = 0;
203         res->irn = NULL;
204         res->costs = -1;
205         return res;
206 }
207
208 /**
209  * emit the ROOT instruction
210  */
211 static instruction *emit_ROOT(mul_env *env, ir_node *root_op) {
212         instruction *res = obstack_alloc(&env->obst, sizeof(*res));
213         res->kind = ROOT;
214         res->in[0] = NULL;
215         res->in[1] = NULL;
216         res->shift_count = 0;
217         res->irn = root_op;
218         res->costs = 0;
219         return res;
220 }
221
222
223 /**
224  * Returns the condensed representation of the tarval tv
225  */
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);
230         int     i, l, r;
231         unsigned char *R = obstack_alloc(&env->obst, bits);
232
233         l = r = 0;
234         for (i = 0; bitstr[i] != '\0'; ++i) {
235                 if (bitstr[i] == '1') {
236                         R[r] = i - l;
237                         l = i;
238                         ++r;
239                 }
240         }
241         free(bitstr);
242
243         *pr = r;
244         return R;
245 }
246
247 /**
248  * Calculate the gain when using the generalized complementary technique
249  */
250 static int calculate_gain(unsigned char *R, int r) {
251         int max_gain = -1;
252         int idx, i;
253         int gain;
254
255         /* the gain for r == 1 */
256         gain = 2 - 3 - R[0];
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];
260
261                 if (gain > max_gain) {
262                         max_gain = gain;
263                         idx = i;
264                 }
265         }
266         if (max_gain > 0)
267                 return idx;
268         return -1;
269 }
270
271 /**
272  * Calculates the condensed complement of a given (R,r) tuple
273  */
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);
276         int i, l, j;
277         unsigned char c;
278
279         memset(value, 0, env->bits);
280
281         j = 0;
282         for (i = 0; i < gain; ++i) {
283                 j += R[i];
284                 value[j] = 1;
285         }
286
287         /* negate and propagate 1 */
288         c = 1;
289         for (i = 0; i <= j; ++i) {
290                 unsigned char v = !value[i];
291
292                 value[i] = v ^ c;
293                 c = v & c;
294         }
295
296         /* condense it again */
297         l = r = 0;
298         R = value;
299         for (i = 0; i <= j; ++i) {
300                 if (value[i] == 1) {
301                         R[r] = i - l;
302                         l = i;
303                         ++r;
304                 }
305         }
306
307         *prs = r;
308         return R;
309 }
310
311 /**
312  * creates a tarval from a condensed representation.
313  */
314 static tarval *condensed_to_value(mul_env *env, unsigned char *R, int r) {
315         tarval *res, *tv;
316         int i, j;
317
318         j = 0;
319         tv = get_mode_one(env->mode);
320         res = NULL;
321         for (i = 0; i < r; ++i) {
322                 j = R[i];
323                 if (j) {
324                         tarval *t = new_tarval_from_long(j, mode_Iu);
325                         tv = tarval_shl(tv, t);
326                 }
327                 res = res ? tarval_add(res, tv) : tv;
328         }
329         return res;
330 }
331
332 /* forward */
333 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N);
334
335 /*
336  * handle simple cases with up-to 2 bits set
337  */
338 static instruction *decompose_simple_cases(mul_env *env, unsigned char *R, int r, tarval *N) {
339         instruction *ins, *ins2;
340
341         if (r == 1) {
342                 return emit_SHIFT(env, env->root, R[0]);
343         } else {
344                 assert(r == 2);
345
346                 ins = env->root;
347                 if (R[0] != 0) {
348                         ins = emit_SHIFT(env, ins, R[0]);
349                 }
350                 if (R[1] <= env->max_S)
351                         return emit_LEA(env, ins, ins, R[1]);
352
353                 ins2 = emit_SHIFT(env, env->root, R[0] + R[1]);
354                 return emit_LEA(env, ins, ins2, 0);
355         }
356 }
357
358 /**
359  * Main decompose driver.
360  */
361 static instruction *decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
362         unsigned i;
363         int gain;
364
365         if (r <= 2)
366                 return decompose_simple_cases(env, R, r, N);
367
368         if (params->also_use_subs) {
369                 gain = calculate_gain(R, r);
370                 if (gain > 0) {
371                         instruction *instr1, *instr2;
372                         unsigned char *R1, *R2;
373                         int r1, r2, i, k;
374
375                         R1 = complement_condensed(env, R, r, gain, &r1);
376                         r2 = r - gain + 1;
377                         R2 = obstack_alloc(&env->obst, r2);
378
379                         k = 1;
380                         for (i = 0; i < gain; ++i) {
381                                 k += R[i];
382                         }
383                         R2[0] = k;
384                         R2[1] = R[gain] - 1;
385                         for (i = gain; i < r; ++i) {
386                                 R2[i] = R[i];
387                         }
388
389                         instr1 = decompose_mul(env, R1, r1, NULL);
390                         instr2 = decompose_mul(env, R2, r2, NULL);
391                         return emit_SUB(env, instr2, instr1);
392                 }
393         }
394
395         if (N == NULL)
396                 N = condensed_to_value(env, R, r);
397
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);
401
402                 div_res = tarval_divmod(N, tv, &mod_res);
403                 if (mod_res == get_mode_null(env->mode)) {
404                         unsigned char *Rs;
405                         int rs;
406
407                         Rs = value_to_condensed(env, div_res, &rs);
408                         if (rs < r) {
409                                 instruction *N1 = decompose_mul(env, Rs, rs, div_res);
410                                 return emit_LEA(env, N1, N1, i);
411                         }
412                 }
413         }
414         return basic_decompose_mul(env, R, r, N);
415 }
416
417 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
418
419 /**
420  * basic decomposition routine
421  */
422 static instruction *basic_decompose_mul(mul_env *env, unsigned char *R, int r, tarval *N) {
423         instruction *Ns;
424         unsigned t;
425
426         if (R[0] == 0) {                                        /* Case 1 */
427                 t = R[1] > IMAX(env->max_S, R[1]);
428                 R[1] -= t;
429                 Ns = decompose_mul(env, &R[1], r - 1, N);
430                 return emit_LEA(env, env->root, Ns, t);
431         } else if (R[0] <= env->max_S) {        /* Case 2 */
432                 t = R[0];
433                 R[1] += t;
434                 Ns = decompose_mul(env, &R[1], r - 1, N);
435                 return emit_LEA(env, Ns, env->root, t);
436         } else {
437                 t = R[0];
438                 R[0] = 0;
439                 Ns = decompose_mul(env, R, r, N);
440                 return emit_SHIFT(env, Ns, t);
441         }
442 }
443
444 /**
445  * recursive build the graph form the instructions.
446  *
447  * @param env   the environment
448  * @param inst  the instruction
449  */
450 static ir_node *build_graph(mul_env *env, instruction *inst) {
451         ir_node *l, *r, *c;
452
453         if (inst->irn)
454                 return inst->irn;
455
456         switch (inst->kind) {
457         case LEA:
458                 l = build_graph(env, inst->in[0]);
459                 r = build_graph(env, inst->in[1]);
460                 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
461                 r = new_rd_Shl(env->dbg, current_ir_graph, env->blk, r, c, env->mode);
462                 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
463         case SHIFT:
464                 l = build_graph(env, inst->in[0]);
465                 c = new_r_Const(current_ir_graph, env->blk, env->shf_mode, new_tarval_from_long(inst->shift_count, env->shf_mode));
466                 return inst->irn = new_rd_Shl(env->dbg, current_ir_graph, env->blk, l, c, env->mode);
467         case SUB:
468                 l = build_graph(env, inst->in[0]);
469                 r = build_graph(env, inst->in[1]);
470                 return inst->irn = new_rd_Sub(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
471         case ADD:
472                 l = build_graph(env, inst->in[0]);
473                 r = build_graph(env, inst->in[1]);
474                 return inst->irn = new_rd_Add(env->dbg, current_ir_graph, env->blk, l, r, env->mode);
475         default:
476                 assert(0);
477                 return NULL;
478         }
479 }
480
481 /**
482  * Calculate the costs for the given instruction sequence.
483  * Note that additional costs due to higher register pressure are NOT evaluated yet
484  */
485 static int evaluate_insn(mul_env *env, instruction *inst) {
486         int costs;
487
488         if (inst->costs >= 0) {
489                 /* was already evaluated */
490                 return 0;
491         }
492
493         switch (inst->kind) {
494         case LEA:
495         case SUB:
496         case ADD:
497                 costs  = evaluate_insn(env, inst->in[0]);
498                 costs += evaluate_insn(env, inst->in[1]);
499                 costs += env->evaluate(inst->kind, NULL);
500                 inst->costs = costs;
501                 return costs;
502         case SHIFT:
503                 if (inst->shift_count > params->highest_shift_amount)
504                         env->fail = 1;
505                 if (env->n_shift <= 0)
506                         env->fail = 1;
507                 else
508                         --env->n_shift;
509                 costs  = evaluate_insn(env, inst->in[0]);
510                 costs += env->evaluate(inst->kind, NULL);
511                 inst->costs = costs;
512                 return costs;
513         default:
514                 assert(0);
515                 return 0;
516         }
517 }
518
519 /**
520  * Evaluate the replacement instructions and build a new graph
521  * if faster than the Mul.
522  * returns the root of the new graph then or irn otherwise.
523  *
524  * @param irn      the Mul operation
525  * @param operand  the multiplication operand
526  * @param tv       the multiplication constant
527  *
528  * @return the new graph
529  */
530 static ir_node *do_decomposition(ir_node *irn, ir_node *operand, tarval *tv) {
531         mul_env       env;
532         instruction   *inst;
533         unsigned char *R;
534         int           r;
535         ir_node       *res = irn;
536         int           mul_costs;
537
538         obstack_init(&env.obst);
539         env.mode     = get_tarval_mode(tv);
540         env.bits     = get_mode_size_bits(env.mode);
541         env.max_S    = 3;
542         env.root     = emit_ROOT(&env, operand);
543         env.fail     = 0;
544         env.n_shift  = params->maximum_shifts;
545         env.evaluate = params->evaluate != NULL ? params->evaluate : default_evaluate;
546
547         R = value_to_condensed(&env, tv, &r);
548         inst = decompose_mul(&env, R, r, tv);
549
550         /* the paper suggests 70% here */
551         mul_costs = (env.evaluate(MUL, tv) * 7) / 10;
552         if (evaluate_insn(&env, inst) <= mul_costs && !env.fail) {
553                 env.op       = operand;
554                 env.blk      = get_nodes_block(irn);
555                 env.dbg      = get_irn_dbg_info(irn);
556                 env.shf_mode = find_unsigned_mode(env.mode);
557                 if (env.shf_mode == NULL)
558                         env.shf_mode = mode_Iu;
559
560                 res = build_graph(&env, inst);
561         }
562         obstack_free(&env.obst, NULL);
563         return res;
564 }
565
566 /* Replace Muls with Shifts and Add/Subs. */
567 ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn) {
568         ir_node *res = irn;
569         ir_mode *mode = get_irn_mode(irn);
570
571         /* If the architecture dependent optimizations were not initialized
572            or this optimization was not enabled. */
573         if (params == NULL || (opts & arch_dep_mul_to_shift) == 0)
574                 return irn;
575
576         if (is_Mul(irn) && mode_is_int(mode)) {
577                 ir_node *block   = get_nodes_block(irn);
578                 ir_node *left    = get_binop_left(irn);
579                 ir_node *right   = get_binop_right(irn);
580                 tarval *tv       = NULL;
581                 ir_node *operand = NULL;
582
583                 /* Look, if one operand is a constant. */
584                 if (is_Const(left)) {
585                         tv = get_Const_tarval(left);
586                         operand = right;
587                 } else if (is_Const(right)) {
588                         tv = get_Const_tarval(right);
589                         operand = left;
590                 }
591
592                 if (tv != NULL) {
593                         res = do_decomposition(irn, operand, tv);
594
595                         if (res != irn) {
596                                 hook_arch_dep_replace_mul_with_shifts(irn);
597                                 exchange(irn, res);
598                         }
599                 }
600         }
601
602         return res;
603 }
604
605 /**
606  * calculated the ld2 of a tarval if tarval is 2^n, else returns -1.
607  */
608 static int tv_ld2(tarval *tv, int bits) {
609         int i, k = 0, num;
610
611         for (num = i = 0; i < bits; ++i) {
612                 unsigned char v = get_tarval_sub_bits(tv, i);
613
614                 if (v) {
615                         int j;
616
617                         for (j = 0; j < 8; ++j)
618                                 if ((1 << j) & v) {
619                                         ++num;
620                                         k = 8 * i + j;
621                                 }
622                 }
623         }
624         if (num == 1)
625                 return k;
626         return -1;
627 }
628
629
630 /* for shorter lines */
631 #define ABS(a)    tarval_abs(a)
632 #define NEG(a)    tarval_neg(a)
633 #define NOT(a)    tarval_not(a)
634 #define SHL(a, b) tarval_shl(a, b)
635 #define SHR(a, b) tarval_shr(a, b)
636 #define ADD(a, b) tarval_add(a, b)
637 #define SUB(a, b) tarval_sub(a, b)
638 #define MUL(a, b) tarval_mul(a, b)
639 #define DIV(a, b) tarval_div(a, b)
640 #define MOD(a, b) tarval_mod(a, b)
641 #define CMP(a, b) tarval_cmp(a, b)
642 #define CNV(a, m) tarval_convert_to(a, m)
643 #define ONE(m)    get_mode_one(m)
644 #define ZERO(m)   get_mode_null(m)
645
646 /** The result of a the magic() function. */
647 struct ms {
648         tarval *M;        /**< magic number */
649         int s;            /**< shift amount */
650         int need_add;     /**< an additional add is needed */
651         int need_sub;     /**< an additional sub is needed */
652 };
653
654 /**
655  * Signed division by constant d: calculate the Magic multiplier M and the shift amount s
656  *
657  * see Hacker's Delight: 10-6 Integer Division by Constants: Incorporation into a Compiler
658  */
659 static struct ms magic(tarval *d) {
660         ir_mode *mode   = get_tarval_mode(d);
661         ir_mode *u_mode = find_unsigned_mode(mode);
662         int bits        = get_mode_size_bits(u_mode);
663         int p;
664         tarval *ad, *anc, *delta, *q1, *r1, *q2, *r2, *t;     /* unsigned */
665         pn_Cmp d_cmp, M_cmp;
666
667         tarval *bits_minus_1, *two_bits_1;
668
669         struct ms mag;
670
671         tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
672
673         /* we need overflow mode to work correctly */
674         tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
675
676         /* 2^(bits-1) */
677         bits_minus_1 = new_tarval_from_long(bits - 1, u_mode);
678         two_bits_1   = SHL(get_mode_one(u_mode), bits_minus_1);
679
680         ad  = CNV(ABS(d), u_mode);
681         t   = ADD(two_bits_1, SHR(CNV(d, u_mode), bits_minus_1));
682         anc = SUB(SUB(t, ONE(u_mode)), MOD(t, ad));   /* Absolute value of nc */
683         p   = bits - 1;                               /* Init: p */
684         q1  = DIV(two_bits_1, anc);                   /* Init: q1 = 2^p/|nc| */
685         r1  = SUB(two_bits_1, MUL(q1, anc));          /* Init: r1 = rem(2^p, |nc|) */
686         q2  = DIV(two_bits_1, ad);                    /* Init: q2 = 2^p/|d| */
687         r2  = SUB(two_bits_1, MUL(q2, ad));           /* Init: r2 = rem(2^p, |d|) */
688
689         do {
690                 ++p;
691                 q1 = ADD(q1, q1);                           /* Update q1 = 2^p/|nc| */
692                 r1 = ADD(r1, r1);                           /* Update r1 = rem(2^p, |nc|) */
693
694                 if (CMP(r1, anc) & pn_Cmp_Ge) {
695                         q1 = ADD(q1, ONE(u_mode));
696                         r1 = SUB(r1, anc);
697                 }
698
699                 q2 = ADD(q2, q2);                           /* Update q2 = 2^p/|d| */
700                 r2 = ADD(r2, r2);                           /* Update r2 = rem(2^p, |d|) */
701
702                 if (CMP(r2, ad) & pn_Cmp_Ge) {
703                         q2 = ADD(q2, ONE(u_mode));
704                         r2 = SUB(r2, ad);
705                 }
706
707                 delta = SUB(ad, r2);
708         } while (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(u_mode)) & pn_Cmp_Eq));
709
710         d_cmp = CMP(d, ZERO(mode));
711
712         if (d_cmp & pn_Cmp_Ge)
713                 mag.M = ADD(CNV(q2, mode), ONE(mode));
714         else
715                 mag.M = SUB(ZERO(mode), ADD(CNV(q2, mode), ONE(mode)));
716
717         M_cmp = CMP(mag.M, ZERO(mode));
718
719         mag.s = p - bits;
720
721         /* need an add if d > 0 && M < 0 */
722         mag.need_add = d_cmp & pn_Cmp_Gt && M_cmp & pn_Cmp_Lt;
723
724         /* need a sub if d < 0 && M > 0 */
725         mag.need_sub = d_cmp & pn_Cmp_Lt && M_cmp & pn_Cmp_Gt;
726
727         tarval_set_integer_overflow_mode(rem);
728
729         return mag;
730 }
731
732 /** The result of the magicu() function. */
733 struct mu {
734         tarval *M;        /**< magic add constant */
735         int s;            /**< shift amount */
736         int need_add;     /**< add indicator */
737 };
738
739 /**
740  * Unsigned division by constant d: calculate the Magic multiplier M and the shift amount s
741  *
742  * see Hacker's Delight: 10-10 Integer Division by Constants: Incorporation into a Compiler (Unsigned)
743  */
744 static struct mu magicu(tarval *d) {
745         ir_mode *mode   = get_tarval_mode(d);
746         int bits        = get_mode_size_bits(mode);
747         int p;
748         tarval *nc, *delta, *q1, *r1, *q2, *r2;
749         tarval *bits_minus_1, *two_bits_1, *seven_ff;
750
751         struct mu magu;
752
753         tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
754
755         /* we need overflow mode to work correctly */
756         tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
757
758         bits_minus_1 = new_tarval_from_long(bits - 1, mode);
759         two_bits_1   = SHL(get_mode_one(mode), bits_minus_1);
760         seven_ff     = SUB(two_bits_1, ONE(mode));
761
762         magu.need_add = 0;                            /* initialize the add indicator */
763         nc = SUB(NEG(ONE(mode)), MOD(NEG(d), d));
764         p  = bits - 1;                                /* Init: p */
765         q1 = DIV(two_bits_1, nc);                     /* Init: q1 = 2^p/nc */
766         r1 = SUB(two_bits_1, MUL(q1, nc));            /* Init: r1 = rem(2^p, nc) */
767         q2 = DIV(seven_ff, d);                        /* Init: q2 = (2^p - 1)/d */
768         r2 = SUB(seven_ff, MUL(q2, d));               /* Init: r2 = rem(2^p - 1, d) */
769
770         do {
771                 ++p;
772                 if (CMP(r1, SUB(nc, r1)) & pn_Cmp_Ge) {
773                         q1 = ADD(ADD(q1, q1), ONE(mode));
774                         r1 = SUB(ADD(r1, r1), nc);
775                 }
776                 else {
777                         q1 = ADD(q1, q1);
778                         r1 = ADD(r1, r1);
779                 }
780
781                 if (CMP(ADD(r2, ONE(mode)), SUB(d, r2)) & pn_Cmp_Ge) {
782                         if (CMP(q2, seven_ff) & pn_Cmp_Ge)
783                                 magu.need_add = 1;
784
785                         q2 = ADD(ADD(q2, q2), ONE(mode));
786                         r2 = SUB(ADD(ADD(r2, r2), ONE(mode)), d);
787                 }
788                 else {
789                         if (CMP(q2, two_bits_1) & pn_Cmp_Ge)
790                                 magu.need_add = 1;
791
792                         q2 = ADD(q2, q2);
793                         r2 = ADD(ADD(r2, r2), ONE(mode));
794                 }
795                 delta = SUB(SUB(d, ONE(mode)), r2);
796         } while (p < 2*bits &&
797                 (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(mode)) & pn_Cmp_Eq)));
798
799         magu.M = ADD(q2, ONE(mode));                       /* Magic number */
800         magu.s = p - bits;                                 /* and shift amount */
801
802         tarval_set_integer_overflow_mode(rem);
803
804         return magu;
805 }
806
807 /**
808  * Build the Mulh replacement code for n / tv.
809  *
810  * Note that 'div' might be a mod or DivMod operation as well
811  */
812 static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv) {
813         dbg_info *dbg  = get_irn_dbg_info(div);
814         ir_node *n     = get_binop_left(div);
815         ir_node *block = get_irn_n(div, -1);
816         ir_mode *mode  = get_irn_mode(n);
817         int bits       = get_mode_size_bits(mode);
818         ir_node *q, *t, *c;
819
820         /* Beware: do not transform bad code */
821         if (is_Bad(n) || is_Bad(block))
822                 return div;
823
824         if (mode_is_signed(mode)) {
825                 struct ms mag = magic(tv);
826
827                 /* generate the Mulh instruction */
828                 c = new_r_Const(current_ir_graph, block, mode, mag.M);
829                 q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
830
831                 /* do we need an Add or Sub */
832                 if (mag.need_add)
833                         q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
834                 else if (mag.need_sub)
835                         q = new_rd_Sub(dbg, current_ir_graph, block, q, n, mode);
836
837                 /* Do we need the shift */
838                 if (mag.s > 0) {
839                         c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
840                         q    = new_rd_Shrs(dbg, current_ir_graph, block, q, c, mode);
841                 }
842
843                 /* final */
844                 c = new_r_Const_long(current_ir_graph, block, mode_Iu, bits-1);
845                 t = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
846
847                 q = new_rd_Add(dbg, current_ir_graph, block, q, t, mode);
848         } else {
849                 struct mu mag = magicu(tv);
850                 ir_node *c;
851
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);
855
856                 if (mag.need_add) {
857                         if (mag.s > 0) {
858                                 /* use the GM scheme */
859                                 t = new_rd_Sub(dbg, current_ir_graph, block, n, q, mode);
860
861                                 c = new_r_Const(current_ir_graph, block, mode_Iu, get_mode_one(mode_Iu));
862                                 t = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
863
864                                 t = new_rd_Add(dbg, current_ir_graph, block, t, q, mode);
865
866                                 c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s-1);
867                                 q = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
868                         } else {
869                                 /* use the default scheme */
870                                 q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
871                         }
872                 } else if (mag.s > 0) { /* default scheme, shift needed */
873                         c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
874                         q = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
875                 }
876         }
877         return q;
878 }
879
880 /* Replace Divs with Shifts and Add/Subs and Mulh. */
881 ir_node *arch_dep_replace_div_by_const(ir_node *irn) {
882         ir_node *res  = irn;
883
884         /* If the architecture dependent optimizations were not initialized
885         or this optimization was not enabled. */
886         if (params == NULL || (opts & arch_dep_div_by_const) == 0)
887                 return irn;
888
889         if (get_irn_opcode(irn) == iro_Div) {
890                 ir_node *c = get_Div_right(irn);
891                 ir_node *block, *left;
892                 ir_mode *mode;
893                 tarval *tv, *ntv;
894                 dbg_info *dbg;
895                 int n, bits;
896                 int k, n_flag;
897
898                 if (get_irn_op(c) != op_Const)
899                         return irn;
900
901                 tv = get_Const_tarval(c);
902
903                 /* check for division by zero */
904                 if (classify_tarval(tv) == TV_CLASSIFY_NULL)
905                         return irn;
906
907                 left  = get_Div_left(irn);
908                 mode  = get_irn_mode(left);
909                 block = get_irn_n(irn, -1);
910                 dbg   = get_irn_dbg_info(irn);
911
912                 bits = get_mode_size_bits(mode);
913                 n    = (bits + 7) / 8;
914
915                 k = -1;
916                 if (mode_is_signed(mode)) {
917                         /* for signed divisions, the algorithm works for a / -2^k by negating the result */
918                         ntv = tarval_neg(tv);
919                         n_flag = 1;
920                         k = tv_ld2(ntv, n);
921                 }
922
923                 if (k < 0) {
924                         n_flag = 0;
925                         k = tv_ld2(tv, n);
926                 }
927
928                 if (k >= 0) { /* division by 2^k or -2^k */
929                         if (mode_is_signed(mode)) {
930                                 ir_node *k_node;
931                                 ir_node *curr = left;
932
933                                 if (k != 1) {
934                                         k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
935                                         curr   = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
936                                 }
937
938                                 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
939                                 curr   = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
940
941                                 curr   = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
942
943                                 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
944                                 res    = new_rd_Shrs(dbg, current_ir_graph, block, curr, k_node, mode);
945
946                                 if (n_flag) { /* negate the result */
947                                         ir_node *k_node;
948
949                                         k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
950                                         res = new_rd_Sub(dbg, current_ir_graph, block, k_node, res, mode);
951                                 }
952                         } else {      /* unsigned case */
953                                 ir_node *k_node;
954
955                                 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
956                                 res    = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
957                         }
958                 } else {
959                         /* other constant */
960                         if (allow_Mulh(mode))
961                                 res = replace_div_by_mulh(irn, tv);
962                 }
963         }
964
965         if (res != irn)
966                 hook_arch_dep_replace_division_by_const(irn);
967
968         return res;
969 }
970
971 /* Replace Mods with Shifts and Add/Subs and Mulh. */
972 ir_node *arch_dep_replace_mod_by_const(ir_node *irn) {
973         ir_node *res  = irn;
974
975         /* If the architecture dependent optimizations were not initialized
976            or this optimization was not enabled. */
977         if (params == NULL || (opts & arch_dep_mod_by_const) == 0)
978                 return irn;
979
980         if (get_irn_opcode(irn) == iro_Mod) {
981                 ir_node *c = get_Mod_right(irn);
982                 ir_node *block, *left;
983                 ir_mode *mode;
984                 tarval *tv, *ntv;
985                 dbg_info *dbg;
986                 int n, bits;
987                 int k;
988
989                 if (get_irn_op(c) != op_Const)
990                         return irn;
991
992                 tv = get_Const_tarval(c);
993
994                 /* check for division by zero */
995                 if (classify_tarval(tv) == TV_CLASSIFY_NULL)
996                         return irn;
997
998                 left  = get_Mod_left(irn);
999                 mode  = get_irn_mode(left);
1000                 block = get_irn_n(irn, -1);
1001                 dbg   = get_irn_dbg_info(irn);
1002                 bits = get_mode_size_bits(mode);
1003                 n    = (bits + 7) / 8;
1004
1005                 k = -1;
1006                 if (mode_is_signed(mode)) {
1007                         /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1008                         ntv = tarval_neg(tv);
1009                         k = tv_ld2(ntv, n);
1010                 }
1011
1012                 if (k < 0) {
1013                         k = tv_ld2(tv, n);
1014                 }
1015
1016                 if (k >= 0) {
1017                         /* division by 2^k or -2^k:
1018                          * we use "modulus" here, so x % y == x % -y that's why is no difference between the case 2^k and -2^k
1019                          */
1020                         if (mode_is_signed(mode)) {
1021                                 ir_node *k_node;
1022                                 ir_node *curr = left;
1023
1024                                 if (k != 1) {
1025                                         k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1026                                         curr   = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1027                                 }
1028
1029                                 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1030                                 curr   = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1031
1032                                 curr   = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1033
1034                                 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1035                                 curr   = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1036
1037                                 res    = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1038                         } else {      /* unsigned case */
1039                                 ir_node *k_node;
1040
1041                                 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1042                                 res    = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1043                         }
1044                 } else {
1045                         /* other constant */
1046                         if (allow_Mulh(mode)) {
1047                                 res = replace_div_by_mulh(irn, tv);
1048
1049                                 res = new_rd_Mul(dbg, current_ir_graph, block, res, c, mode);
1050
1051                                 /* res = arch_dep_mul_to_shift(res); */
1052
1053                                 res = new_rd_Sub(dbg, current_ir_graph, block, left, res, mode);
1054                         }
1055                 }
1056         }
1057
1058         if (res != irn)
1059                 hook_arch_dep_replace_division_by_const(irn);
1060
1061         return res;
1062 }
1063
1064 /* Replace DivMods with Shifts and Add/Subs and Mulh. */
1065 void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn) {
1066         *div = *mod = NULL;
1067
1068         /* If the architecture dependent optimizations were not initialized
1069            or this optimization was not enabled. */
1070         if (params == NULL ||
1071                 ((opts & (arch_dep_div_by_const|arch_dep_mod_by_const)) != (arch_dep_div_by_const|arch_dep_mod_by_const)))
1072                 return;
1073
1074         if (get_irn_opcode(irn) == iro_DivMod) {
1075                 ir_node *c = get_DivMod_right(irn);
1076                 ir_node *block, *left;
1077                 ir_mode *mode;
1078                 tarval *tv, *ntv;
1079                 dbg_info *dbg;
1080                 int n, bits;
1081                 int k, n_flag;
1082
1083                 if (get_irn_op(c) != op_Const)
1084                         return;
1085
1086                 tv = get_Const_tarval(c);
1087
1088                 /* check for division by zero */
1089                 if (classify_tarval(tv) == TV_CLASSIFY_NULL)
1090                         return;
1091
1092                 left  = get_DivMod_left(irn);
1093                 mode  = get_irn_mode(left);
1094                 block = get_irn_n(irn, -1);
1095                 dbg   = get_irn_dbg_info(irn);
1096
1097                 bits = get_mode_size_bits(mode);
1098                 n    = (bits + 7) / 8;
1099
1100                 k = -1;
1101                 if (mode_is_signed(mode)) {
1102                         /* for signed divisions, the algorithm works for a / -2^k by negating the result */
1103                         ntv = tarval_neg(tv);
1104                         n_flag = 1;
1105                         k = tv_ld2(ntv, n);
1106                 }
1107
1108                 if (k < 0) {
1109                         n_flag = 0;
1110                         k = tv_ld2(tv, n);
1111                 }
1112
1113                 if (k >= 0) { /* division by 2^k or -2^k */
1114                         if (mode_is_signed(mode)) {
1115                                 ir_node *k_node, *c_k;
1116                                 ir_node *curr = left;
1117
1118                                 if (k != 1) {
1119                                         k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
1120                                         curr   = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
1121                                 }
1122
1123                                 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
1124                                 curr   = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
1125
1126                                 curr   = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
1127
1128                                 c_k    = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1129
1130                                 *div   = new_rd_Shrs(dbg, current_ir_graph, block, curr, c_k, mode);
1131
1132                                 if (n_flag) { /* negate the div result */
1133                                         ir_node *k_node;
1134
1135                                         k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
1136                                         *div = new_rd_Sub(dbg, current_ir_graph, block, k_node, *div, mode);
1137                                 }
1138
1139                                 k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
1140                                 curr   = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
1141
1142                                 *mod   = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
1143                         } else {      /* unsigned case */
1144                                 ir_node *k_node;
1145
1146                                 k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
1147                                 *div   = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
1148
1149                                 k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
1150                                 *mod   = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
1151                         }
1152                 } else {
1153                         /* other constant */
1154                         if (allow_Mulh(mode)) {
1155                                 ir_node *t;
1156
1157                                 *div = replace_div_by_mulh(irn, tv);
1158
1159                                 t    = new_rd_Mul(dbg, current_ir_graph, block, *div, c, mode);
1160
1161                                 /* t = arch_dep_mul_to_shift(t); */
1162
1163                                 *mod = new_rd_Sub(dbg, current_ir_graph, block, left, t, mode);
1164                         }
1165                 }
1166         }
1167
1168         if (*div)
1169                 hook_arch_dep_replace_division_by_const(irn);
1170 }
1171
1172
1173 static const ir_settings_arch_dep_t default_params = {
1174         1,  /* also use subs */
1175         4,  /* maximum shifts */
1176         31, /* maximum shift amount */
1177
1178         0,  /* allow Mulhs */
1179         0,  /* allow Mulus */
1180         32  /* Mulh allowed up to 32 bit */
1181 };
1182
1183 /* A default parameter factory for testing purposes. */
1184 const ir_settings_arch_dep_t *arch_dep_default_factory(void) {
1185         return &default_params;
1186 }