doxygen docu improved
[libfirm] / ir / opt / opt_osr.c
1 /**
2  * Project:     libFIRM
3  * File name:   ir/opt/opt_osr.
4  * Purpose:     Operator Strength Reduction, based on
5  *              Keith D. Cooper, L. Taylor Simpson, Christopher A. Vick
6  * Author:      Michael Beck
7  * Modified by:
8  * Created:     12.5.2006
9  * CVS-ID:      $Id$
10  * Copyright:   (c) 2006 Universität Karlsruhe
11  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
12  */
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #ifdef HAVE_MALLOC_H
18 #include <malloc.h>
19 #endif
20 #ifdef HAVE_ALLOCA_H
21 #include <alloca.h>
22 #endif
23
24 #include "opt_osr.h"
25 #include "irgraph.h"
26 #include "ircons.h"
27 #include "irop_t.h"
28 #include "irloop.h"
29 #include "irdom.h"
30 #include "irgmod.h"
31 #include "irflag_t.h"
32 #include "irgwalk.h"
33 #include "irouts.h"
34 #include "debug.h"
35 #include "obst.h"
36 #include "set.h"
37 #include "tv.h"
38 #include "hashptr.h"
39 #include "irtools.h"
40 #include "irloop_t.h"
41 #include "array.h"
42 #include "firmstat.h"
43
44 /** The debug handle. */
45 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
46
47 /** A scc. */
48 typedef struct scc {
49         ir_node *head;          /**< the head of the list */
50 } scc;
51
52 /** A node entry */
53 typedef struct node_entry {
54         unsigned DFSnum;    /**< the DFS number of this node */
55         unsigned low;       /**< the low number of this node */
56         ir_node  *header;   /**< the header of this node */
57         int      in_stack;  /**< flag, set if the node is on the stack */
58         ir_node  *next;     /**< link to the next node the the same scc */
59         scc      *pscc;     /**< the scc of this node */
60         unsigned POnum;     /**< the post order number for blocks */
61 } node_entry;
62
63 /** The environment. */
64 typedef struct iv_env {
65         struct obstack obst;    /**< an obstack for allocations */
66         ir_node  **stack;       /**< the node stack */
67         int      tos;           /**< tos index */
68         unsigned nextDFSnum;    /**< the current DFS number */
69         unsigned POnum;         /**< current post order number */
70         set      *quad_map;     /**< a map from (op, iv, rc) to node */
71         set      *lftr_edges;   /**< the set of lftr edges */
72         unsigned replaced;      /**< number of replaced ops */
73         unsigned lftr_replaced; /**< number of applied linear function test replacements */
74         unsigned flags;         /**< additional flags */
75 } iv_env;
76
77 /**
78  * An entry in the (op, node, node) -> node map.
79  */
80 typedef struct quad_t {
81         opcode  code;  /**< the opcode of the reduced operation */
82         ir_node *op1;  /**< the first operand the reduced operation */
83         ir_node *op2;  /**< the second operand of the reduced operation */
84
85         ir_node *res; /**< the reduced operation */
86 } quad_t;
87
88 /**
89  * A LFTR edge.
90  */
91 typedef struct LFTR_edge {
92         ir_node *src;   /**< the source node */
93         ir_node *dst;   /**< the destination node */
94         opcode  code;   /**< the opcode that must be applied */
95         ir_node *rc;    /**< the region const that must be applied */
96 } LFTR_edge;
97
98 /* forward */
99 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env);
100
101 /**
102  * Compare two LFTR edges.
103  */
104 static int LFTR_cmp(const void *e1, const void *e2, size_t size) {
105         const LFTR_edge *l1 = e1;
106         const LFTR_edge *l2 = e2;
107
108         return l1->src != l2->src;
109 }
110
111 /**
112  * Find a LFTR edge.
113  */
114 static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) {
115         LFTR_edge key;
116
117         key.src  = src;
118
119         return set_find(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
120 }
121
122 /**
123  * Add a LFTR edge.
124  */
125 static void LFTR_add(ir_node *src, ir_node *dst, opcode code, ir_node *rc, iv_env *env) {
126         LFTR_edge key;
127
128         key.src  = src;
129         key.dst  = dst;
130         key.code = code;
131         key.rc   = rc;
132
133         /*
134          * There might be more than one edge here. This is rather bad
135          * because we currently store only one.
136          */
137 //      assert(LFTR_find(src, env) == NULL);
138         set_insert(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
139 }
140
141 /**
142  * Gets the node_entry of a node
143  */
144 static node_entry *get_irn_ne(ir_node *irn, iv_env *env) {
145         node_entry *e = get_irn_link(irn);
146
147         if (! e) {
148                 e = obstack_alloc(&env->obst, sizeof(*e));
149                 memset(e, 0, sizeof(*e));
150                 set_irn_link(irn, e);
151         }
152         return e;
153 }
154
155 /**
156  * Check if irn is an IV.
157  *
158  * @param irn  the node to check
159  * @param env  the environment
160  *
161  * @returns the header if it is one, NULL else
162  */
163 static ir_node *is_iv(ir_node *irn, iv_env *env) {
164         return get_irn_ne(irn, env)->header;
165 }
166
167 /**
168  * Check if irn is a region constant.
169  * The block or irn must strictly dominate the header block.
170  *
171  * @param irn           the node to check
172  * @param header_block  the header block of the induction variable
173  */
174 static int is_rc(ir_node *irn, ir_node *header_block) {
175         ir_node *block = get_nodes_block(irn);
176
177         return (block != header_block) && block_dominates(block, header_block);
178 }
179
180 /**
181  * Set compare function for the quad set.
182  */
183 static int quad_cmp(const void *e1, const void *e2, size_t size) {
184         const quad_t *c1 = e1;
185         const quad_t *c2 = e2;
186
187         return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
188 }
189
190 /**
191  * Check if an reduced operation was already calculated.
192  *
193  * @param code  the opcode of the operation
194  * @param op1   the first operand of the operation
195  * @param op2   the second operand of the operation
196  * @param env   the environment
197  *
198  * @return the already reduced node or NULL if this operation is not yet reduced
199  */
200 static ir_node *search(opcode code, ir_node *op1, ir_node *op2, iv_env *env) {
201         quad_t key, *entry;
202
203         key.code = code;
204         key.op1 = op1;
205         key.op2 = op2;
206
207         entry = set_find(env->quad_map, &key, sizeof(key),
208                          (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
209         if (entry)
210                 return entry->res;
211         return NULL;
212 }
213
214 /**
215  * Add an reduced operation.
216  *
217  * @param code    the opcode of the operation
218  * @param op1     the first operand of the operation
219  * @param op2     the second operand of the operation
220  * @param result  the result of the reduced operation
221  * @param env     the environment
222  */
223 static void add(opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env) {
224         quad_t key;
225
226         key.code = code;
227         key.op1  = op1;
228         key.op2  = op2;
229         key.res  = result;
230
231         set_insert(env->quad_map, &key, sizeof(key),
232                    (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
233 }
234
235 /**
236  * Find a location where to place a bin-op whose operands are in
237  * block1 and block2.
238  *
239  * @param block1  the block of the first operand
240  * @param block2  the block of the second operand
241  *
242  * Note that we know here that such a place must exists. Moreover, this means
243  * that either block1 dominates block2 or vice versa. So, just return
244  * the "smaller" one.
245  */
246 static ir_node *find_location(ir_node *block1, ir_node *block2) {
247         if (block_dominates(block1, block2))
248                 return block2;
249         assert(block_dominates(block2, block1));
250         return block1;
251 }
252
253 /**
254  * Create a node that executes an op1 code op1 operation.
255  *
256  * @param code   the opcode to execute
257  * @param db     debug info to add to the new node
258  * @param op1    the first operand
259  * @param op2    the second operand
260  * @param mode   the mode of the new operation
261  *
262  * @return the newly created node
263  */
264 static ir_node *do_apply(opcode code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
265         ir_graph *irg = current_ir_graph;
266         ir_node *result;
267         ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
268
269         switch (code) {
270         case iro_Mul:
271                 result = new_rd_Mul(db, irg, block, op1, op2, mode);
272                 break;
273         case iro_Add:
274                 result = new_rd_Add(db, irg, block, op1, op2, mode);
275                 break;
276         case iro_Sub:
277                 result = new_rd_Sub(db, irg, block, op1, op2, mode);
278                 break;
279         default:
280                 assert(0);
281                 result = NULL;
282         }
283         return result;
284 }
285
286 /**
287  * The Apply operation.
288  *
289  * @param orig   the node that represent the original operation and determines
290  *               the opcode, debug-info and mode of a newly created one
291  * @param op1    the first operand
292  * @param op2    the second operand
293  * @param env     the environment
294  *
295  * @return the newly created node
296  */
297 static ir_node *apply(ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env) {
298         opcode code = get_irn_opcode(orig);
299         ir_node *result = search(code, op1, op2, env);
300
301         if (! result) {
302                 dbg_info *db = get_irn_dbg_info(orig);
303                 ir_node *op1_header = get_irn_ne(op1, env)->header;
304                 ir_node *op2_header = get_irn_ne(op2, env)->header;
305
306                 if (op1_header != NULL && is_rc(op2, op1_header)) {
307                         result = reduce(orig, op1, op2, env);
308                 }
309                 else if (op2_header != NULL && is_rc(op1, op2_header)) {
310                         result = reduce(orig, op2, op1, env);
311                 }
312                 else {
313                         result = do_apply(code, db, op1, op2, get_irn_mode(orig));
314                         get_irn_ne(result, env)->header = NULL;         }
315         }
316         return result;
317 }
318
319 /**
320  * The Reduce operation.
321  *
322  * @param orig   the node that represent the original operation and determines
323  *               the opcode, debug-info and mode of a newly created one
324  * @param iv     the induction variable
325  * @param rc     the region constant
326  * @param env    the environment
327  *
328  * @return the reduced node
329  */
330 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) {
331         opcode code = get_irn_opcode(orig);
332         ir_node *result = search(code, iv, rc, env);
333
334         if (! result) {
335                 node_entry *e, *iv_e;
336                 int i, n;
337                 ir_mode *mode = get_irn_mode(orig);
338
339                 result = exact_copy(iv);
340
341                 /* Beware: we must always create a new nduction variable with the same mode
342                    as the node we are replacing. Espicially this means the mode might be changed
343                    from P to I and back. This is always possible, because we have only Phi, Add
344                    and Sub nodes. */
345                 set_irn_mode(result, mode);
346                 add(code, iv, rc, result, env);
347                 DB((dbg, LEVEL_3, "   Created new %+F for %+F (%s %+F)\n", result, iv,
348                         get_irn_opname(orig), rc));
349
350                 iv_e = get_irn_ne(iv, env);
351                 e    = get_irn_ne(result, env);
352                 e->header = iv_e->header;
353
354                 /* create the LFTR edge */
355                 LFTR_add(iv, result, code, rc, env);
356
357                 n = get_irn_arity(result);
358                 for (i = 0; i < n; ++i) {
359                         ir_node *o = get_irn_n(result, i);
360
361                         e = get_irn_ne(o, env);
362                         if (e->header == iv_e->header)
363                                 o = reduce(orig, o, rc, env);
364                         else if (is_Phi(result))
365                                 o = apply(orig, o, rc, env);
366                         else {
367                                 if (code == iro_Mul)
368                                         o = apply(orig, o, rc, env);
369                         }
370                         set_irn_n(result, i, o);
371                 }
372         }
373         else {
374                 DB((dbg, LEVEL_3, "   Already Created %+F for %+F (%s %+F)\n", result, iv,
375                         get_irn_opname(orig), rc));
376         }
377         return result;
378 }
379
380 /**
381  * The Replace operation.
382  *
383  * @param irn   the node that will be replaced
384  * @param iv    the induction variable
385  * @param rc    the region constant
386  * @param env   the environment
387  */
388 static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) {
389         ir_node *result;
390         ir_loop *iv_loop  = get_irn_loop(get_nodes_block(iv));
391         ir_loop *irn_loop = get_irn_loop(get_nodes_block(irn));
392
393         /* only replace nodes that are in the same (or deeper loops) */
394         if (get_loop_depth(irn_loop) >= get_loop_depth(iv_loop)) {
395                 DB((dbg, LEVEL_2, "  Replacing %+F\n", irn));
396
397                 result = reduce(irn, iv, rc, env);
398                 if (result != irn) {
399                         node_entry *e, *iv_e;
400
401                         hook_strength_red(current_ir_graph, irn);
402                         exchange(irn, result);
403                         e = get_irn_ne(result, env);
404                         iv_e = get_irn_ne(iv, env);
405                         e->header = iv_e->header;
406                 }
407                 ++env->replaced;
408                 return 1;
409         }
410         return 0;
411 }
412
413 /**
414  * Check if a node can be replaced (+, -, *).
415  *
416  * @param irn   the node to check
417  * @param env   the environment
418  *
419  * @return non-zero if irn should be Replace'd
420  */
421 static int check_replace(ir_node *irn, iv_env *env) {
422         ir_node *left, *right, *iv, *rc;
423         ir_op   *op  = get_irn_op(irn);
424         opcode  code = get_op_code(op);
425         ir_node *liv, *riv;
426
427         switch (code) {
428         case iro_Mul:
429         case iro_Add:
430         case iro_Sub:
431                 iv = rc = NULL;
432
433                 left  = get_binop_left(irn);
434                 right = get_binop_right(irn);
435
436                 liv = is_iv(left, env);
437                 riv = is_iv(right, env);
438                 if (liv && is_rc(right, liv)) {
439                         iv = left; rc = right;
440                 }
441                 else if (riv && is_op_commutative(op) &&
442                                     is_rc(left, riv)) {
443                         iv = right; rc = left;
444                 }
445
446                 if (iv)
447                         return replace(irn, iv, rc, env);
448                 break;
449         default:
450                 break;
451         }
452         return 0;
453 }
454
455 /**
456  * Check which SCC's are induction variables.
457  *
458  * @param pscc  a SCC
459  * @param env   the environment
460  */
461 static void classify_iv(scc *pscc, iv_env *env) {
462         ir_node *irn, *next, *header = NULL;
463         node_entry *b, *h = NULL;
464         int j, only_phi, num_outside;
465         ir_node *out_rc;
466
467         /* find the header block for this scc */
468         for (irn = pscc->head; irn; irn = next) {
469                 node_entry *e = get_irn_link(irn);
470                 ir_node *block = get_nodes_block(irn);
471
472                 next = e->next;
473                 b = get_irn_ne(block, env);
474
475                 if (header) {
476                         if (h->POnum < b->POnum) {
477                                 header = block;
478                                 h      = b;
479                         }
480                 }
481                 else {
482                         header = block;
483                         h      = b;
484                 }
485         }
486
487         /* check if this scc contains only Phi, Add or Sub nodes */
488         only_phi    = 1;
489         num_outside = 0;
490         out_rc      = NULL;
491         for (irn = pscc->head; irn; irn = next) {
492                 node_entry *e = get_irn_ne(irn, env);
493
494                 next = e->next;
495                 switch (get_irn_opcode(irn)) {
496                 case iro_Add:
497                 case iro_Sub:
498                         only_phi = 0;
499                         /* fall through */
500                 case iro_Phi:
501                         for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
502                                 ir_node *pred  = get_irn_n(irn, j);
503                                 node_entry *pe = get_irn_ne(pred, env);
504
505                                 if (pe->pscc != e->pscc) {
506                                         /* not in the same SCC, must be a region const */
507                                         if (! is_rc(pred, header)) {
508                                                 /* not an induction variable */
509                                                 goto fail;
510                                         }
511                                         if (! out_rc) {
512                                                 out_rc = pred;
513                                                 ++num_outside;
514                                         }
515                                         else if (out_rc != pred)
516                                                 ++num_outside;
517                                 }
518                         }
519                         break;
520                 default:
521                         /* not an induction variable */
522                         goto fail;
523                 }
524         }
525         /* found an induction variable */
526         DB((dbg, LEVEL_2, "  Found an induction variable:\n  "));
527         if (only_phi && num_outside == 1) {
528                 DB((dbg, LEVEL_2, "  Found an USELESS Phi cycle:\n  "));
529         }
530
531         /* set the header for every node in this scc */
532         for (irn = pscc->head; irn; irn = next) {
533                 node_entry *e = get_irn_ne(irn, env);
534                 e->header = header;
535                 next = e->next;
536                 DB((dbg, LEVEL_2, " %+F,", irn));
537         }
538         DB((dbg, LEVEL_2, "\n"));
539         return;
540
541 fail:
542         for (irn = pscc->head; irn; irn = next) {
543                 node_entry *e = get_irn_ne(irn, env);
544
545                 next = e->next;
546                 if (! check_replace(irn, env))
547                         e->header = NULL;
548         }
549 }
550
551 /**
552  * Process a SCC.
553  *
554  * @param pscc  the SCC
555  * @param env   the environment
556  */
557 static void process_scc(scc *pscc, iv_env *env) {
558         ir_node *head = pscc->head;
559         node_entry *e = get_irn_link(head);
560
561 #ifdef DEBUG_libfirm
562         {
563                 ir_node *irn, *next;
564
565                 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
566                 for (irn = pscc->head; irn; irn = next) {
567                         node_entry *e = get_irn_link(irn);
568
569                         next = e->next;
570
571                         DB((dbg, LEVEL_4, " %+F,", irn));
572                 }
573                 DB((dbg, LEVEL_4, "\n"));
574         }
575 #endif
576
577         if (e->next == NULL) {
578                 /* this SCC has only a single member */
579                 check_replace(head, env);
580         }
581         else {
582                 classify_iv(pscc, env);
583         }
584 }
585
586 /**
587  * Push a node onto the stack.
588  *
589  * @param env   the environment
590  * @param n     the node to push
591  */
592 static void push(iv_env *env, ir_node *n) {
593         node_entry *e;
594
595         if (env->tos == ARR_LEN(env->stack)) {
596                 int nlen = ARR_LEN(env->stack) * 2;
597                 ARR_RESIZE(ir_node *, env->stack, nlen);
598         }
599         env->stack[env->tos++] = n;
600         e = get_irn_ne(n, env);
601         e->in_stack = 1;
602 }
603
604 /**
605  * pop a node from the stack
606  *
607  * @param env   the environment
608  *
609  * @return  The topmost node
610  */
611 static ir_node *pop(iv_env *env)
612 {
613         ir_node *n = env->stack[--env->tos];
614         node_entry *e = get_irn_ne(n, env);
615
616         e->in_stack = 0;
617         return n;
618 }
619
620 /**
621  * Do Tarjan's SCC algorithm and drive OSR.
622  *
623  * @param irn  start at this node
624  * @param env  the environment
625  */
626 static void dfs(ir_node *irn, iv_env *env)
627 {
628         int i, n;
629         node_entry *node = get_irn_ne(irn, env);
630
631         mark_irn_visited(irn);
632
633         /* do not put blocks into the scc */
634         if (is_Block(irn)) {
635                 n = get_irn_arity(irn);
636                 for (i = 0; i < n; ++i) {
637                         ir_node *pred = get_irn_n(irn, i);
638
639                         if (irn_not_visited(pred))
640                                 dfs(pred, env);
641                 }
642         }
643         else {
644                 ir_node *block = get_nodes_block(irn);
645
646                 node->DFSnum = env->nextDFSnum++;
647                 node->low    = node->DFSnum;
648                 push(env, irn);
649
650                 /* handle the block */
651                 if (irn_not_visited(block))
652                         dfs(block, env);
653
654                 n = get_irn_arity(irn);
655                 for (i = 0; i < n; ++i) {
656                         ir_node *pred = get_irn_n(irn, i);
657                         node_entry *o = get_irn_ne(pred, env);
658
659                         if (irn_not_visited(pred)) {
660                                 dfs(pred, env);
661                                 node->low = MIN(node->low, o->low);
662                         }
663                         if (o->DFSnum < node->DFSnum && o->in_stack)
664                                 node->low = MIN(o->DFSnum, node->low);
665                 }
666                 if (node->low == node->DFSnum) {
667                         scc *pscc = obstack_alloc(&env->obst, sizeof(*pscc));
668                         ir_node *x;
669
670                         pscc->head = NULL;
671                         do {
672                                 node_entry *e;
673
674                                 x = pop(env);
675                                 e = get_irn_ne(x, env);
676                                 e->pscc    = pscc;
677                                 e->next    = pscc->head;
678                                 pscc->head = x;
679                         } while (x != irn);
680
681                         process_scc(pscc, env);
682                 }
683         }
684 }
685
686 /**
687  * Do the DFS by starting at the End node of a graph.
688  *
689  * @param irg  the graph to process
690  * @param env  the environment
691  */
692 static void do_dfs(ir_graph *irg, iv_env *env) {
693         ir_graph *rem = current_ir_graph;
694         ir_node *end = get_irg_end(irg);
695         int i, n;
696
697         current_ir_graph = irg;
698         inc_irg_visited(irg);
699
700         /* visit all visible nodes */
701         dfs(end, env);
702
703         /* visit the keep-alives */
704         n = get_End_n_keepalives(end);
705         for (i = 0; i < n; ++i) {
706                 ir_node *ka = get_End_keepalive(end, i);
707
708                 if (irn_not_visited(ka))
709                         dfs(ka, env);
710         }
711
712         current_ir_graph = rem;
713 }
714
715 /**
716  * Post-block-walker: assign the post-order number.
717  */
718 static void assign_po(ir_node *block, void *ctx) {
719         iv_env *env = ctx;
720         node_entry *e = get_irn_ne(block, env);
721
722         e->POnum = env->POnum++;
723 }
724
725 /**
726  * Follows the LFTR edges and return the last node in the chain.
727  *
728  * @param irn  the node that should be followed
729  * @param env  the IV environment
730  *
731  * @note
732  * In the current implementation only the last edge is stored, so
733  * only one chain exists. That's why we might miss some opportunities.
734  */
735 static ir_node *followEdges(ir_node *irn, iv_env *env) {
736         for (;;) {
737                 LFTR_edge *e = LFTR_find(irn, env);
738                 if (e)
739                         irn = e->dst;
740                 else
741                         return irn;
742         }
743 }
744
745 /**
746  * Apply one LFTR edge operation.
747  * Return NULL if the transformation cannot be done safely without
748  * an Overflow.
749  *
750  * @param rc   the IV node that should be translated
751  * @param e    the LFTR edge
752  * @param env  the IV environment
753  *
754  * @return the translated region constant or NULL
755  *         if the translation was not possible
756  *
757  * @note
758  * In the current implementation only the last edge is stored, so
759  * only one chain exists. That's why we might miss some opportunities.
760  */
761 static ir_node *applyOneEdge(ir_node *rc, LFTR_edge *e, iv_env *env) {
762         if (env->flags & osr_flag_lftr_with_ov_check) {
763                 tarval *tv_l, *tv_r, *tv;
764                 tarval_int_overflow_mode_t ovmode;
765
766                 /* overflow can only be decided for Consts */
767                 if (! is_Const(e->rc)) {
768                         DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
769                         return NULL;
770                 }
771
772                 tv_l = get_Const_tarval(rc);
773                 tv_r = get_Const_tarval(e->rc);
774
775                 ovmode = tarval_get_integer_overflow_mode();
776                 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
777
778                 switch (e->code) {
779                 case iro_Mul:
780                         tv = tarval_mul(tv_l, tv_r);
781                         DB((dbg, LEVEL_4, " * %+F", tv_r));
782                         break;
783                 case iro_Add:
784                         tv = tarval_add(tv_l, tv_r);
785                         DB((dbg, LEVEL_4, " + %+F", tv_r));
786                         break;
787                 case iro_Sub:
788                         tv = tarval_sub(tv_l, tv_r);
789                         DB((dbg, LEVEL_4, " - %+F", tv_r));
790                         break;
791                 default:
792                         assert(0);
793                         tv = tarval_bad;
794                 }
795                 tarval_set_integer_overflow_mode(ovmode);
796
797                 if (tv == tarval_bad) {
798                         DB((dbg, LEVEL_4, " = OVERFLOW"));
799                         return NULL;
800                 }
801                 return new_r_Const(current_ir_graph, get_irn_n(rc, -1), get_tarval_mode(tv), tv);
802         }
803         return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(rc));
804 }
805
806 /**
807  * Applies the operations represented by the LFTR edges to a
808  * region constant and returns the value.
809  * Return NULL if the transformation cannot be done safely without
810  * an Overflow.
811  *
812  * @param iv   the IV node that starts the LFTR edge chain
813  * @param rc   the region constant that should be translated
814  * @param env  the IV environment
815  *
816  * @return the translated region constant or NULL
817  *         if the translation was not possible
818  */
819 static ir_node *applyEdges(ir_node *iv, ir_node *rc, iv_env *env) {
820         ir_node *irn = iv;
821
822         if (env->flags & osr_flag_lftr_with_ov_check) {
823                 /* overflow can only be decided for Consts */
824                 if (! is_Const(rc)) {
825                         DB((dbg, LEVEL_4, " = UNKNOWN (%+F)\n", rc));
826                         return NULL;
827                 }
828                 DB((dbg, LEVEL_4, "%+F", get_Const_tarval(rc)));
829         }
830
831         for (irn = iv; rc;) {
832                 LFTR_edge *e = LFTR_find(irn, env);
833                 if (e) {
834                         rc = applyOneEdge(rc, e, env);
835                         irn = e->dst;
836                 }
837                 else
838                         break;
839         }
840         DB((dbg, LEVEL_3, "\n"));
841         return rc;
842 }
843
844 /**
845  * Walker, finds Cmp(iv, rc) or Cmp(rc, iv)
846  * and tries to optimize them.
847  */
848 static void do_lftr(ir_node *cmp, void *ctx) {
849         iv_env *env = ctx;
850         ir_node *left, *right, *liv, *riv;
851         ir_node *iv, *rc;
852         ir_node *nleft = NULL, *nright = NULL;
853
854         if (get_irn_op(cmp) != op_Cmp)
855                 return;
856
857         left  = get_Cmp_left(cmp);
858         right = get_Cmp_right(cmp);
859
860         liv = is_iv(left, env);
861         riv = is_iv(right, env);
862         if (liv && is_rc(right, liv)) {
863                 iv = left; rc = right;
864
865                 nright = applyEdges(iv, rc, env);
866                 if (nright && nright != rc) {
867                         nleft = followEdges(iv, env);
868                 }
869         }
870         else if (riv && is_rc(left, riv)) {
871                 iv = right; rc = left;
872
873                 nleft = applyEdges(iv, rc, env);
874                 if (nleft && nleft != rc) {
875                         nright = followEdges(iv, env);
876                 }
877         }
878
879         if (nleft && nright) {
880                 DB((dbg, LEVEL_2, "  LFTR for %+F\n", cmp));
881                 set_Cmp_left(cmp, nleft);
882                 set_Cmp_right(cmp, nright);
883                 ++env->lftr_replaced;
884         }
885 }
886
887 /**
888  * do linear function test replacement.
889  *
890  * @param irg   the graph that should be optimized
891  * @param env   the IV environment
892  */
893 static void lftr(ir_graph *irg, iv_env *env) {
894         irg_walk_graph(irg, NULL, do_lftr, env);
895 }
896
897 /**
898  * Pre-walker: set all node links to NULL and fix the
899  * block of Proj nodes.
900  */
901 static void clear_and_fix(ir_node *irn, void *env)
902 {
903         set_irn_link(irn, NULL);
904
905         if (is_Proj(irn)) {
906                 ir_node *pred = get_Proj_pred(irn);
907                 set_irn_n(irn, -1, get_irn_n(pred, -1));
908         }
909 }
910
911 /* Performs Operator Strength Reduction for the passed graph. */
912 void opt_osr(ir_graph *irg, unsigned flags) {
913         iv_env env;
914
915         if (! get_opt_strength_red())
916                 return;
917
918         FIRM_DBG_REGISTER(dbg, "firm.opt.osr");
919 //      firm_dbg_set_mask(dbg, SET_LEVEL_3);
920
921         DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));
922
923         obstack_init(&env.obst);
924         env.stack         = NEW_ARR_F(ir_node *, 128);
925         env.tos           = 0;
926         env.nextDFSnum    = 0;
927         env.POnum         = 0;
928         env.quad_map      = new_set(quad_cmp, 64);
929         env.lftr_edges    = new_set(LFTR_cmp, 64);
930         env.replaced      = 0;
931         env.lftr_replaced = 0;
932         env.flags         = flags;
933
934         /* we need control flow loop information to decide whether
935          * we should do a replacement or not. */
936         construct_cf_backedges(irg);
937
938         /* Clear all links and move Proj nodes into the
939            the same block as it's predecessors.
940            This can improve the placement of new nodes.
941          */
942         irg_walk_graph(irg, NULL, clear_and_fix, NULL);
943
944         /* we need dominance */
945         assure_doms(irg);
946         assure_irg_outs(irg);
947
948         /* calculate the post order number for blocks. */
949         irg_out_block_walk(get_irg_start_block(irg), NULL, assign_po, &env);
950
951         /* calculate the SCC's and drive OSR. */
952         do_dfs(irg, &env);
953
954         if (env.replaced) {
955                 /* try linear function test replacements */
956                 //lftr(irg, &env);
957
958                 set_irg_outs_inconsistent(irg);
959                 /* cfg loop still valid */
960
961                 DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));
962         }
963
964         del_set(env.lftr_edges);
965         del_set(env.quad_map);
966         DEL_ARR_F(env.stack);
967         obstack_free(&env.obst, NULL);
968 }