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