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