typos fixed
[libfirm] / ir / ir / irarch.c
1 /**
2  * @file irarch.c
3  * @date 28.9.2004
4  * @author Sebastian Hack
5  * @brief Machine dependent firm optimizations.
6  *
7  * $Id$
8  */
9 #include <stdlib.h>
10 #include <assert.h>
11
12 #include "irnode_t.h"
13 #include "irgraph_t.h"
14 #include "irmode_t.h"
15 #include "iropt_t.h"
16 #include "ircons_t.h"
17 #include "irgmod.h"
18 #include "irvrfy.h"
19 #include "tv_t.h"
20 #include "dbginfo_t.h"
21 #include "iropt_dbg.h"
22 #include "irflag_t.h"
23 #include "irhooks.h"
24 #include "ircons.h"
25 #include "irarch.h"
26
27 #undef DEB
28
29 #define MAX_BITSTR 64
30
31 /* when we need verifying */
32 #ifdef NDEBUG
33 # define IRN_VRFY_IRG(res, irg)
34 #else
35 # define IRN_VRFY_IRG(res, irg)  irn_vrfy_irg(res, irg)
36 #endif
37
38 /** The params got from the factory in arch_dep_init(...). */
39 static const arch_dep_params_t *params = NULL;
40
41 /** The bit mask, which optimizations to apply. */
42 static arch_dep_opts_t opts;
43
44 /* we need this new pseudo op */
45 static ir_op *op_Mulh = NULL;
46
47 /**
48  * construct a Mulh: Mulh(a,b) = (a * b) >> w, w is the with in bits of a, b
49  */
50 static ir_node *
51 new_rd_Mulh (dbg_info *db, ir_graph *irg, ir_node *block,
52        ir_node *op1, ir_node *op2, ir_mode *mode)
53 {
54   ir_node *in[2];
55   ir_node *res;
56
57   if (! op_Mulh) {
58     op_Mulh = new_ir_op(get_next_ir_opcode(), "Mulh",  op_pin_state_floats, irop_flag_commutative, oparity_binary, 0, 0);
59   }
60
61   in[0] = op1;
62   in[1] = op2;
63   res = new_ir_node(db, irg, block, op_Mulh, mode, 2, in);
64   res = optimize_node(res);
65   IRN_VRFY_IRG(res, irg);
66   return res;
67 }
68
69 ir_op *get_op_Mulh(void)  { return op_Mulh; }
70
71 void arch_dep_init(arch_dep_params_factory_t factory)
72 {
73   opts = arch_dep_none;
74
75   if (factory != NULL)
76     params = factory();
77
78   if (params && (opts & (arch_dep_div_by_const|arch_dep_mod_by_const))) {
79     if (! op_Mulh) {
80       /* create the Mulh operation */
81       op_Mulh = new_ir_op(get_next_ir_opcode(), "Mulh",  op_pin_state_floats, irop_flag_commutative, oparity_binary, 0, 0);
82     }
83   }
84 }
85
86 void arch_dep_set_opts(arch_dep_opts_t the_opts) {
87   opts = the_opts;
88 }
89
90 /* check, whether a mode allows a Mulh instruction */
91 static int allow_Mulh(ir_mode *mode)
92 {
93   if (get_mode_size_bits(mode) > params->max_bits_for_mulh)
94     return 0;
95   return (mode_is_signed(mode) && params->allow_mulhs) || (!mode_is_signed(mode) && params->allow_mulhu);
96 }
97
98 ir_node *arch_dep_replace_mul_with_shifts(ir_node *irn)
99 {
100   ir_node *res = irn;
101   ir_mode *mode = get_irn_mode(irn);
102
103   /* If the architecture dependent optimizations were not initialized
104      or this optimization was not enabled. */
105   if (params == NULL || (opts & arch_dep_mul_to_shift) == 0)
106     return irn;
107
108   if (get_irn_op(irn) == op_Mul && mode_is_int(mode)) {
109     ir_node *block   = get_irn_n(irn, -1);
110     ir_node *left    = get_binop_left(irn);
111     ir_node *right   = get_binop_right(irn);
112     tarval *tv       = NULL;
113     ir_node *operand = NULL;
114
115     /* Look, if one operand is a constant. */
116     if (get_irn_opcode(left) == iro_Const) {
117       tv = get_Const_tarval(left);
118       operand = right;
119     } else if(get_irn_opcode(right) == iro_Const) {
120       tv = get_Const_tarval(right);
121       operand = left;
122     }
123
124     if (tv != NULL) {
125       int maximum_shifts = params->maximum_shifts;
126       int also_use_subs = params->also_use_subs;
127       int highest_shift_amount = params->highest_shift_amount;
128
129       char *bitstr = get_tarval_bitpattern(tv);
130       char *p;
131       int i, last = 0;
132       int counter = 0;
133       int curr_bit;
134       int compr_len = 0;
135       char compr[MAX_BITSTR];
136
137       int singleton;
138       int end_of_group;
139       int shift_with_sub[MAX_BITSTR] = { 0 };
140       int shift_without_sub[MAX_BITSTR] = { 0 };
141       int shift_with_sub_pos = 0;
142       int shift_without_sub_pos = 0;
143
144 #if DEB
145       {
146         long val = get_tarval_long(tv);
147         fprintf(stderr, "Found mul with %ld(%lx) = ", val, val);
148         for(p = bitstr; *p != '\0'; p++)
149           printf("%c", *p);
150         printf("\n");
151       }
152 #endif
153
154       for(p = bitstr; *p != '\0'; p++) {
155         int bit = *p != '0';
156
157         if (bit != last) {
158           /* The last was 1 we are now at 0 OR
159            * The last was 0 and we are now at 1 */
160           compr[compr_len++] = counter;
161           counter = 1;
162         }
163         else
164           counter++;
165
166         last = bit;
167       }
168       compr[compr_len++] = counter;
169
170
171 #ifdef DEB
172       {
173         const char *prefix = "";
174         for(i = 0; i < compr_len; i++, prefix = ",")
175           fprintf(stderr, "%s%d", prefix, compr[i]);
176         fprintf("\n");
177       }
178 #endif
179
180       // Go over all recorded one groups.
181       curr_bit = compr[0];
182
183       for(i = 1; i < compr_len; i = end_of_group + 2) {
184         int j, zeros_in_group, ones_in_group;
185
186         ones_in_group = compr[i];
187         zeros_in_group = 0;
188
189         // Scan for singular 0s in a sequence
190         for(j = i + 1; j < compr_len && compr[j] == 1; j += 2) {
191           zeros_in_group += 1;
192           ones_in_group += (j + 1 < compr_len ? compr[j + 1] : 0);
193         }
194         end_of_group = j - 1;
195
196         if(zeros_in_group >= ones_in_group - 1)
197           end_of_group = i;
198
199 #ifdef DEB
200         fprintf(stderr, "  i:%d, eg:%d\n", i, end_of_group);
201 #endif
202
203         singleton = compr[i] == 1 && i == end_of_group;
204         for(j = i; j <= end_of_group; j += 2) {
205           int curr_ones = compr[j];
206           int biased_curr_bit = curr_bit + 1;
207           int k;
208
209 #ifdef DEB
210           fprintf(stderr, "    j:%d, ones:%d\n", j, curr_ones);
211 #endif
212
213           // If this ones group is a singleton group (it has no
214           // singleton zeros inside
215           if(singleton)
216             shift_with_sub[shift_with_sub_pos++] = biased_curr_bit;
217           else if(j == i)
218             shift_with_sub[shift_with_sub_pos++] = -biased_curr_bit;
219
220           for(k = 0; k < curr_ones; k++)
221             shift_without_sub[shift_without_sub_pos++] = biased_curr_bit + k;
222
223           curr_bit += curr_ones;
224           biased_curr_bit = curr_bit + 1;
225
226           if(!singleton && j == end_of_group)
227             shift_with_sub[shift_with_sub_pos++] = biased_curr_bit;
228           else if(j != end_of_group)
229             shift_with_sub[shift_with_sub_pos++] = -biased_curr_bit;
230
231           curr_bit += compr[j + 1];
232         }
233
234       }
235
236       {
237         int *shifts = shift_with_sub;
238         int n = shift_with_sub_pos;
239         int highest_shift_wide = 0;
240         int highest_shift_seq = 0;
241         int last_shift = 0;
242
243         /* If we may not use subs, or we can achive the same with adds,
244            prefer adds. */
245         if(!also_use_subs || shift_with_sub_pos >= shift_without_sub_pos) {
246           shifts = shift_without_sub;
247           n = shift_without_sub_pos;
248         }
249
250         /* If the number of needed shifts exceeds the given maximum,
251            use the Mul and exit. */
252         if(n > maximum_shifts) {
253 #ifdef DEB
254           fprintf(stderr, "Only allowed %d shifts, but %d are needed\n",
255               maximum_shifts, n);
256 #endif
257           return irn;
258         }
259
260         /* Compute the highest shift needed for both, the
261            sequential and wide representations. */
262         for(i = 0; i < n; i++) {
263           int curr = abs(shifts[i]);
264           int curr_seq = curr - last;
265
266           highest_shift_wide = curr > highest_shift_wide ? curr
267             : highest_shift_wide;
268           highest_shift_seq = curr_seq > highest_shift_seq ? curr_seq
269             : highest_shift_seq;
270
271           last_shift = curr;
272         }
273
274         /* If the highest shift amount is greater than the given limit,
275            give back the Mul */
276         if(highest_shift_seq > highest_shift_amount) {
277 #ifdef DEB
278           fprintf(stderr, "Shift argument %d exceeds maximum %d\n",
279               highest_shift_seq, highest_shift_amount);
280 #endif
281           return irn;
282         }
283
284         /* If we have subs, we cannot do sequential. */
285         if(1 /* also_use_subs */) {
286           if(n > 0) {
287             ir_node *curr = NULL;
288
289             i = n - 1;
290
291             do {
292               int curr_shift = shifts[i];
293               int sub = curr_shift < 0;
294               int amount = abs(curr_shift) - 1;
295               ir_node *aux = operand;
296
297               assert(amount >= 0 && "What is a negative shift??");
298
299               if (amount != 0) {
300                 ir_node *cnst = new_r_Const_long(current_ir_graph, block, mode_Iu, amount);
301                 aux = new_r_Shl(current_ir_graph, block, operand, cnst, mode);
302               }
303
304               if (curr) {
305                 if (sub)
306                   curr = new_r_Sub(current_ir_graph, block, curr, aux, mode);
307                 else
308                   curr = new_r_Add(current_ir_graph, block, curr, aux, mode);
309               } else
310                 curr = aux;
311
312             } while(--i >= 0);
313
314             res = curr;
315           }
316         }
317
318 #ifdef DEB
319         {
320           const char *prefix = "";
321           for (i = 0; i < n; ++i) {
322             fprintf(stderr, "%s%d", prefix, shifts[i]);
323             prefix = ", ";
324           }
325           fprintf(stderr, "\n");
326         }
327 #endif
328
329       }
330
331       if(bitstr)
332         free(bitstr);
333     }
334
335   }
336
337   if (res != irn)
338     hook_arch_dep_replace_mul_with_shifts(irn);
339
340   return res;
341 }
342
343 /**
344  * calculated the ld2 of a tarval if tarval is 2^n, else returns -1.
345  */
346 static int tv_ld2(tarval *tv, int bits)
347 {
348   int i, k, num;
349
350   for (num = i = 0; i < bits; ++i) {
351     unsigned char v = get_tarval_sub_bits(tv, i);
352
353     if (v) {
354       int j;
355
356       for (j = 0; j < 8; ++j)
357         if ((1 << j) & v) {
358           ++num;
359           k = 8 * i + j;
360         }
361     }
362   }
363   if (num == 1)
364     return k;
365   return -1;
366 }
367
368
369 /* for shorter lines */
370 #define ABS(a)    tarval_abs(a)
371 #define NEG(a)    tarval_neg(a)
372 #define NOT(a)    tarval_not(a)
373 #define SHL(a, b) tarval_shl(a, b)
374 #define SHR(a, b) tarval_shr(a, b)
375 #define ADD(a, b) tarval_add(a, b)
376 #define SUB(a, b) tarval_sub(a, b)
377 #define MUL(a, b) tarval_mul(a, b)
378 #define DIV(a, b) tarval_div(a, b)
379 #define MOD(a, b) tarval_mod(a, b)
380 #define CMP(a, b) tarval_cmp(a, b)
381 #define CNV(a, m) tarval_convert_to(a, m)
382 #define ONE(m)    get_mode_one(m)
383 #define ZERO(m)   get_mode_null(m)
384
385 struct ms {
386   tarval *M;        /**< magic number */
387   int s;            /**< shift amount */
388   int need_add;     /**< an additional add is needed */
389   int need_sub;     /**< an additional sub is needed */
390 };
391
392 /**
393  * Signed division by constant d: calculate the Magic multiplier M and the shift amount s
394  *
395  * see Hacker's Delight: 10-6 Integer Division by Constants: Incorporation into a Compiler
396  */
397 static struct ms magic(tarval *d)
398 {
399   ir_mode *mode   = get_tarval_mode(d);
400   ir_mode *u_mode = find_unsigned_mode(mode);
401   int bits        = get_mode_size_bits(u_mode);
402   int p;
403   tarval *ad, *anc, *delta, *q1, *r1, *q2, *r2, *t;     /* unsigned */
404   pn_Cmp d_cmp, M_cmp;
405
406   tarval *bits_minus_1, *two_bits_1;
407
408   struct ms mag;
409
410   tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
411
412   /* we need overflow mode to work correctly */
413   tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
414
415   /* 2^(bits-1) */
416   bits_minus_1 = new_tarval_from_long(bits - 1, u_mode);
417   two_bits_1   = SHL(get_mode_one(u_mode), bits_minus_1);
418
419   ad  = CNV(ABS(d), u_mode);
420   t   = ADD(two_bits_1, SHR(CNV(d, u_mode), bits_minus_1));
421   anc = SUB(SUB(t, ONE(u_mode)), MOD(t, ad));   /* Absolute value of nc */
422   p   = bits - 1;                               /* Init: p */
423   q1  = DIV(two_bits_1, anc);                   /* Init: q1 = 2^p/|nc| */
424   r1  = SUB(two_bits_1, MUL(q1, anc));          /* Init: r1 = rem(2^p, |nc|) */
425   q2  = DIV(two_bits_1, ad);                    /* Init: q2 = 2^p/|d| */
426   r2  = SUB(two_bits_1, MUL(q2, ad));           /* Init: r2 = rem(2^p, |d|) */
427
428   do {
429     ++p;
430     q1 = ADD(q1, q1);                           /* Update q1 = 2^p/|nc| */
431     r1 = ADD(r1, r1);                           /* Update r1 = rem(2^p, |nc|) */
432
433     if (CMP(r1, anc) & pn_Cmp_Ge) {
434       q1 = ADD(q1, ONE(u_mode));
435       r1 = SUB(r1, anc);
436     }
437
438     q2 = ADD(q2, q2);                           /* Update q2 = 2^p/|d| */
439     r2 = ADD(r2, r2);                           /* Update r2 = rem(2^p, |d|) */
440
441     if (CMP(r2, ad) & pn_Cmp_Ge) {
442       q2 = ADD(q2, ONE(u_mode));
443       r2 = SUB(r2, ad);
444     }
445
446     delta = SUB(ad, r2);
447   } while (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(u_mode)) & pn_Cmp_Eq));
448
449   d_cmp = CMP(d, ZERO(mode));
450
451   if (d_cmp & pn_Cmp_Ge)
452     mag.M = ADD(CNV(q2, mode), ONE(mode));
453   else
454     mag.M = SUB(ZERO(mode), ADD(CNV(q2, mode), ONE(mode)));
455
456   M_cmp = CMP(mag.M, ZERO(mode));
457
458   mag.s = p - bits;
459
460   /* need an add if d > 0 && M < 0 */
461   mag.need_add = d_cmp & pn_Cmp_Gt && M_cmp & pn_Cmp_Lt;
462
463   /* need a sub if d < 0 && M > 0 */
464   mag.need_sub = d_cmp & pn_Cmp_Lt && M_cmp & pn_Cmp_Gt;
465
466   tarval_set_integer_overflow_mode(rem);
467
468   return mag;
469 }
470
471 struct mu {
472   tarval *M;        /**< magic add constant */
473   int s;            /**< shift amount */
474   int need_add;     /**< add indicator */
475 };
476
477 /**
478  * Unsigned division by constant d: calculate the Magic multiplier M and the shift amount s
479  *
480  * see Hacker's Delight: 10-10 Integer Division by Constants: Incorporation into a Compiler (Unsigned)
481  */
482 static struct mu magicu(tarval *d)
483 {
484   ir_mode *mode   = get_tarval_mode(d);
485   int bits        = get_mode_size_bits(mode);
486   int p;
487   tarval *nc, *delta, *q1, *r1, *q2, *r2;
488   tarval *bits_minus_1, *two_bits_1, *seven_ff;
489
490   struct mu magu;
491
492   tarval_int_overflow_mode_t rem = tarval_get_integer_overflow_mode();
493
494   /* we need overflow mode to work correctly */
495   tarval_set_integer_overflow_mode(TV_OVERFLOW_WRAP);
496
497   bits_minus_1 = new_tarval_from_long(bits - 1, mode);
498   two_bits_1   = SHL(get_mode_one(mode), bits_minus_1);
499   seven_ff     = SUB(two_bits_1, ONE(mode));
500
501   magu.need_add = 0;                            /* initialize the add indicator */
502   nc = SUB(NEG(ONE(mode)), MOD(NEG(d), d));
503   p  = bits - 1;                                /* Init: p */
504   q1 = DIV(two_bits_1, nc);                     /* Init: q1 = 2^p/nc */
505   r1 = SUB(two_bits_1, MUL(q1, nc));            /* Init: r1 = rem(2^p, nc) */
506   q2 = DIV(seven_ff, d);                        /* Init: q2 = (2^p - 1)/d */
507   r2 = SUB(seven_ff, MUL(q2, d));               /* Init: r2 = rem(2^p - 1, d) */
508
509   do {
510     ++p;
511     if (CMP(r1, SUB(nc, r1)) & pn_Cmp_Ge) {
512       q1 = ADD(ADD(q1, q1), ONE(mode));
513       r1 = SUB(ADD(r1, r1), nc);
514     }
515     else {
516       q1 = ADD(q1, q1);
517       r1 = ADD(r1, r1);
518     }
519
520     if (CMP(ADD(r2, ONE(mode)), SUB(d, r2)) & pn_Cmp_Ge) {
521       if (CMP(q2, seven_ff) & pn_Cmp_Ge)
522         magu.need_add = 1;
523
524       q2 = ADD(ADD(q2, q2), ONE(mode));
525       r2 = SUB(ADD(ADD(r2, r2), ONE(mode)), d);
526     }
527     else {
528       if (CMP(q2, two_bits_1) & pn_Cmp_Ge)
529         magu.need_add = 1;
530
531       q2 = ADD(q2, q2);
532       r2 = ADD(ADD(r2, r2), ONE(mode));
533     }
534     delta = SUB(SUB(d, ONE(mode)), r2);
535   } while (p < 2*bits &&
536           (CMP(q1, delta) & pn_Cmp_Lt || (CMP(q1, delta) & pn_Cmp_Eq && CMP(r1, ZERO(mode)) & pn_Cmp_Eq)));
537
538   magu.M = ADD(q2, ONE(mode));                       /* Magic number */
539   magu.s = p - bits;                                 /* and shift amount */
540
541   tarval_set_integer_overflow_mode(rem);
542
543   return magu;
544 }
545
546 /**
547  * build the Mulh replacement code for n / tv
548  *
549  * Note that 'div' might be a mod or DivMod operation as well
550  */
551 static ir_node *replace_div_by_mulh(ir_node *div, tarval *tv)
552 {
553   dbg_info *dbg  = get_irn_dbg_info(div);
554   ir_node *n     = get_binop_left(div);
555   ir_node *block = get_irn_n(div, -1);
556   ir_mode *mode  = get_irn_mode(n);
557   int bits       = get_mode_size_bits(mode);
558   ir_node *q, *t, *c;
559
560   /* Beware: do not transform bad code */
561   if (is_Bad(n) || is_Bad(block))
562     return div;
563
564   if (mode_is_signed(mode)) {
565     struct ms mag = magic(tv);
566
567     /* generate the Mulh instruction */
568     c = new_r_Const(current_ir_graph, block, mode, mag.M);
569     q = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
570
571     /* do we need an Add or Sub */
572     if (mag.need_add)
573       q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
574     else if (mag.need_sub)
575       q = new_rd_Sub(dbg, current_ir_graph, block, q, n, mode);
576
577     /* Do we need the shift */
578     if (mag.s > 0) {
579       c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
580       q    = new_rd_Shrs(dbg, current_ir_graph, block, q, c, mode);
581     }
582
583     /* final */
584     c = new_r_Const_long(current_ir_graph, block, mode_Iu, bits-1);
585     t = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
586
587     q = new_rd_Add(dbg, current_ir_graph, block, q, t, mode);
588   }
589   else {
590     struct mu mag = magicu(tv);
591     ir_node *c;
592
593     /* generate the Mulh instruction */
594     c = new_r_Const(current_ir_graph, block, mode, mag.M);
595     q    = new_rd_Mulh(dbg, current_ir_graph, block, n, c, mode);
596
597     if (mag.need_add) {
598       if (mag.s > 0) {
599         /* use the GM scheme */
600         t = new_rd_Sub(dbg, current_ir_graph, block, n, q, mode);
601
602         c = new_r_Const(current_ir_graph, block, mode_Iu, get_mode_one(mode_Iu));
603         t = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
604
605         t = new_rd_Add(dbg, current_ir_graph, block, t, q, mode);
606
607         c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s-1);
608         q = new_rd_Shr(dbg, current_ir_graph, block, t, c, mode);
609       }
610       else {
611         /* use the default scheme */
612         q = new_rd_Add(dbg, current_ir_graph, block, q, n, mode);
613       }
614     }
615     else if (mag.s > 0) { /* default scheme, shift needed */
616       c = new_r_Const_long(current_ir_graph, block, mode_Iu, mag.s);
617       q = new_rd_Shr(dbg, current_ir_graph, block, q, c, mode);
618     }
619   }
620   return q;
621 }
622
623 ir_node *arch_dep_replace_div_by_const(ir_node *irn)
624 {
625   ir_node *res  = irn;
626
627   /* If the architecture dependent optimizations were not initialized
628      or this optimization was not enabled. */
629   if (params == NULL || (opts & arch_dep_div_by_const) == 0)
630     return irn;
631
632   if (get_irn_opcode(irn) == iro_Div) {
633     ir_node *c = get_Div_right(irn);
634     ir_node *block, *left;
635     ir_mode *mode;
636     tarval *tv, *ntv;
637     dbg_info *dbg;
638     int n, bits;
639     int k, n_flag;
640
641     if (get_irn_op(c) != op_Const)
642       return irn;
643
644     left  = get_Div_left(irn);
645     mode  = get_irn_mode(left);
646     block = get_irn_n(irn, -1);
647     dbg   = get_irn_dbg_info(irn);
648     tv    = get_Const_tarval(c);
649
650     bits = get_mode_size_bits(mode);
651     n    = (bits + 7) / 8;
652
653     k = -1;
654     if (mode_is_signed(mode)) {
655       /* for signed divisions, the algorithm works for a / -2^k by negating the result */
656       ntv = tarval_neg(tv);
657       n_flag = 1;
658       k = tv_ld2(ntv, n);
659     }
660
661     if (k < 0) {
662       n_flag = 0;
663       k = tv_ld2(tv, n);
664     }
665
666     if (k >= 0) { /* division by 2^k or -2^k */
667       if (mode_is_signed(mode)) {
668         ir_node *k_node;
669         ir_node *curr = left;
670
671         if (k != 1) {
672           k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
673           curr   = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
674         }
675
676         k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
677         curr   = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
678
679         curr   = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
680
681         k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
682         res    = new_rd_Shrs(dbg, current_ir_graph, block, curr, k_node, mode);
683
684         if (n_flag) { /* negate the result */
685           ir_node *k_node;
686
687           k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
688           res = new_rd_Sub(dbg, current_ir_graph, block, k_node, res, mode);
689         }
690       }
691       else {      /* unsigned case */
692         ir_node *k_node;
693
694         k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
695         res    = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
696       }
697     }
698     else {
699       /* other constant */
700       if (allow_Mulh(mode))
701         res = replace_div_by_mulh(irn, tv);
702     }
703   }
704
705   if (res != irn)
706     hook_arch_dep_replace_div_by_const(irn);
707
708   return res;
709 }
710
711 ir_node *arch_dep_replace_mod_by_const(ir_node *irn)
712 {
713   ir_node *res  = irn;
714
715   /* If the architecture dependent optimizations were not initialized
716      or this optimization was not enabled. */
717   if (params == NULL || (opts & arch_dep_mod_by_const) == 0)
718     return irn;
719
720   if (get_irn_opcode(irn) == iro_Mod) {
721     ir_node *c = get_Mod_right(irn);
722     ir_node *block, *left;
723     ir_mode *mode;
724     tarval *tv, *ntv;
725     dbg_info *dbg;
726     int n, bits;
727     int k;
728
729     if (get_irn_op(c) != op_Const)
730       return irn;
731
732     left  = get_Mod_left(irn);
733     mode  = get_irn_mode(left);
734     block = get_irn_n(irn, -1);
735     dbg   = get_irn_dbg_info(irn);
736     tv    = get_Const_tarval(c);
737
738     bits = get_mode_size_bits(mode);
739     n    = (bits + 7) / 8;
740
741     k = -1;
742     if (mode_is_signed(mode)) {
743       /* for signed divisions, the algorithm works for a / -2^k by negating the result */
744       ntv = tarval_neg(tv);
745       k = tv_ld2(ntv, n);
746     }
747
748     if (k < 0) {
749       k = tv_ld2(tv, n);
750     }
751
752     if (k >= 0) {
753       /* division by 2^k or -2^k:
754        * we use "modulus" here, so x % y == x % -y that's why is no difference between the case 2^k and -2^k
755        */
756       if (mode_is_signed(mode)) {
757         ir_node *k_node;
758         ir_node *curr = left;
759
760         if (k != 1) {
761           k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
762           curr   = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
763         }
764
765         k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
766         curr   = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
767
768         curr   = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
769
770         k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
771         curr   = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
772
773         res    = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
774       }
775       else {      /* unsigned case */
776         ir_node *k_node;
777
778         k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
779         res    = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
780       }
781     }
782     else {
783       /* other constant */
784       if (allow_Mulh(mode)) {
785         res = replace_div_by_mulh(irn, tv);
786
787         res = new_rd_Mul(dbg, current_ir_graph, block, res, c, mode);
788
789         /* res = arch_dep_mul_to_shift(res); */
790
791         res = new_rd_Sub(dbg, current_ir_graph, block, left, res, mode);
792       }
793     }
794   }
795
796   if (res != irn)
797     hook_arch_dep_replace_mod_by_const(irn);
798
799   return res;
800 }
801
802 void arch_dep_replace_divmod_by_const(ir_node **div, ir_node **mod, ir_node *irn)
803 {
804   *div = *mod = NULL;
805
806   /* If the architecture dependent optimizations were not initialized
807      or this optimization was not enabled. */
808   if (params == NULL ||
809       ((opts & (arch_dep_div_by_const|arch_dep_mod_by_const)) != (arch_dep_div_by_const|arch_dep_mod_by_const)))
810     return;
811
812   if (get_irn_opcode(irn) == iro_DivMod) {
813     ir_node *c = get_DivMod_right(irn);
814     ir_node *block, *left;
815     ir_mode *mode;
816     tarval *tv, *ntv;
817     dbg_info *dbg;
818     int n, bits;
819     int k, n_flag;
820
821     if (get_irn_op(c) != op_Const)
822       return;
823
824     left  = get_DivMod_left(irn);
825     mode  = get_irn_mode(left);
826     block = get_irn_n(irn, -1);
827     dbg   = get_irn_dbg_info(irn);
828     tv    = get_Const_tarval(c);
829
830     bits = get_mode_size_bits(mode);
831     n    = (bits + 7) / 8;
832
833     k = -1;
834     if (mode_is_signed(mode)) {
835       /* for signed divisions, the algorithm works for a / -2^k by negating the result */
836       ntv = tarval_neg(tv);
837       n_flag = 1;
838       k = tv_ld2(ntv, n);
839     }
840
841     if (k < 0) {
842       n_flag = 0;
843       k = tv_ld2(tv, n);
844     }
845
846     if (k >= 0) { /* division by 2^k or -2^k */
847       if (mode_is_signed(mode)) {
848         ir_node *k_node, *c_k;
849         ir_node *curr = left;
850
851         if (k != 1) {
852           k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k - 1);
853           curr   = new_rd_Shrs(dbg, current_ir_graph, block, left, k_node, mode);
854         }
855
856         k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, bits - k);
857         curr   = new_rd_Shr(dbg, current_ir_graph, block, curr, k_node, mode);
858
859         curr   = new_rd_Add(dbg, current_ir_graph, block, left, curr, mode);
860
861         c_k    = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
862
863         *div   = new_rd_Shrs(dbg, current_ir_graph, block, curr, c_k, mode);
864
865         if (n_flag) { /* negate the div result */
866           ir_node *k_node;
867
868           k_node = new_r_Const(current_ir_graph, block, mode, get_mode_null(mode));
869           *div = new_rd_Sub(dbg, current_ir_graph, block, k_node, *div, mode);
870         }
871
872         k_node = new_r_Const_long(current_ir_graph, block, mode, (-1) << k);
873         curr   = new_rd_And(dbg, current_ir_graph, block, curr, k_node, mode);
874
875         *mod   = new_rd_Sub(dbg, current_ir_graph, block, left, curr, mode);
876       }
877       else {      /* unsigned case */
878         ir_node *k_node;
879
880         k_node = new_r_Const_long(current_ir_graph, block, mode_Iu, k);
881         *div   = new_rd_Shr(dbg, current_ir_graph, block, left, k_node, mode);
882
883         k_node = new_r_Const_long(current_ir_graph, block, mode, (1 << k) - 1);
884         *mod   = new_rd_And(dbg, current_ir_graph, block, left, k_node, mode);
885       }
886     }
887     else {
888       /* other constant */
889       if (allow_Mulh(mode)) {
890         ir_node *t;
891
892         *div = replace_div_by_mulh(irn, tv);
893
894         t    = new_rd_Mul(dbg, current_ir_graph, block, *div, c, mode);
895
896         /* t = arch_dep_mul_to_shift(t); */
897
898         *mod = new_rd_Sub(dbg, current_ir_graph, block, left, t, mode);
899       }
900     }
901   }
902
903   if (*div)
904     hook_arch_dep_replace_DivMod_by_const(irn);
905 }
906
907
908 static const arch_dep_params_t default_params = {
909   1,  /* also use subs */
910   4,  /* maximum shifts */
911   31, /* maximum shift amount */
912
913   0,  /* allow Mulhs */
914   0,  /* allow Mulus */
915   32  /* Mulh allowed up to 32 bit */
916 };
917
918 const arch_dep_params_t *arch_dep_default_factory(void) {
919   return &default_params;
920 }