Only (IV - RC) is allowed for induction variables.
[libfirm] / ir / opt / opt_osr.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Operator Strength Reduction.
23  * @date    12.5.2006
24  * @author  Michael Beck
25  * @brief
26  *  Implementation of the Operator Strength Reduction algorithm
27  *  by Keith D. Cooper, L. Taylor Simpson, Christopher A. Vick.
28  *  Extended version.
29  */
30 #include "config.h"
31
32 #include "adt/pdeq.h"
33 #include "iroptimize.h"
34 #include "irgraph.h"
35 #include "ircons.h"
36 #include "irop_t.h"
37 #include "irdom.h"
38 #include "irgmod.h"
39 #include "irflag_t.h"
40 #include "irgwalk.h"
41 #include "irouts.h"
42 #include "iredges.h"
43 #include "debug.h"
44 #include "obst.h"
45 #include "set.h"
46 #include "tv.h"
47 #include "hashptr.h"
48 #include "util.h"
49 #include "irtools.h"
50 #include "irloop_t.h"
51 #include "array.h"
52 #include "firmstat.h"
53 #include "error.h"
54 #include "irpass_t.h"
55
56 /** The debug handle. */
57 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
58
59 /** A scc. */
60 typedef struct scc {
61         ir_node   *head; /**< the head of the list */
62         ir_tarval *init; /**< the init value iff only one exists. */
63         ir_tarval *incr; /**< the induction variable increment if only a single const exists. */
64         unsigned   code; /**< == iro_Add if +incr, iro_Sub if -incr, 0 if not analysed, iro_Bad else */
65 } scc;
66
67 /** A node entry */
68 typedef struct node_entry {
69         unsigned DFSnum;    /**< the DFS number of this node */
70         unsigned low;       /**< the low number of this node */
71         ir_node  *header;   /**< the header of this node */
72         int      in_stack;  /**< flag, set if the node is on the stack */
73         ir_node  *next;     /**< link to the next node the the same scc */
74         scc      *pscc;     /**< the scc of this node */
75         unsigned POnum;     /**< the post order number for blocks */
76 } node_entry;
77
78 /** The environment. */
79 typedef struct iv_env {
80         struct obstack obst;    /**< an obstack for allocations */
81         ir_node  **stack;       /**< the node stack */
82         size_t   tos;           /**< tos index */
83         unsigned nextDFSnum;    /**< the current DFS number */
84         unsigned POnum;         /**< current post order number */
85         set      *quad_map;     /**< a map from (op, iv, rc) to node */
86         set      *lftr_edges;   /**< the set of lftr edges */
87         unsigned replaced;      /**< number of replaced ops */
88         unsigned lftr_replaced; /**< number of applied linear function test replacements */
89         unsigned osr_flags;     /**< additional flags steering the transformation */
90         unsigned need_postpass; /**< set, if a post pass is needed to fix Add and Sub nodes */
91         /** Function called to process a SCC. */
92         void (*process_scc)(scc *pscc, struct iv_env *env);
93 } iv_env;
94
95 /**
96  * An entry in the (op, node, node) -> node map.
97  */
98 typedef struct quadruple_t {
99         unsigned   code; /**< the opcode of the reduced operation */
100         ir_node   *op1;  /**< the first operand the reduced operation */
101         ir_node   *op2;  /**< the second operand of the reduced operation */
102
103         ir_node   *res; /**< the reduced operation */
104 } quadruple_t;
105
106 /**
107  * A LFTR edge.
108  */
109 typedef struct LFTR_edge {
110         ir_node   *src;   /**< the source node */
111         ir_node   *dst;   /**< the destination node */
112         unsigned  code;   /**< the opcode that must be applied */
113         ir_node   *rc;    /**< the region const that must be applied */
114 } LFTR_edge;
115
116 /* forward */
117 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env);
118
119 /**
120  * Compare two LFTR edges.
121  */
122 static int LFTR_cmp(const void *e1, const void *e2, size_t size)
123 {
124         const LFTR_edge *l1 = (const LFTR_edge*)e1;
125         const LFTR_edge *l2 = (const LFTR_edge*)e2;
126         (void) size;
127
128         return l1->src != l2->src;
129 }  /* LFTR_cmp */
130
131 /**
132  * Find a LFTR edge.
133  *
134  * @param src  the source node of the transition
135  */
136 static LFTR_edge *LFTR_find(ir_node *src, iv_env *env)
137 {
138         LFTR_edge key;
139
140         key.src  = src;
141
142         return (LFTR_edge*)set_find(env->lftr_edges, &key, sizeof(key), hash_ptr(src));
143 }  /* LFTR_find */
144
145 /**
146  * Add a LFTR edge.
147  *
148  * @param src   the source node of the edge
149  * @param dst   the destination node of the edge
150  * @param code  the opcode of the transformed transition
151  * @param rc    the region const used in the transition
152  * @param env   the environment
153  */
154 static void LFTR_add(ir_node *src, ir_node *dst, unsigned code, ir_node *rc, iv_env *env)
155 {
156         LFTR_edge key;
157
158         key.src  = src;
159         key.dst  = dst;
160         key.code = code;
161         key.rc   = rc;
162
163         /*
164          * There might be more than one edge here. This is rather bad
165          * because we currently store only one.
166          */
167         set_insert(env->lftr_edges, &key, sizeof(key), hash_ptr(src));
168 }  /* LFTR_add */
169
170 /**
171  * Gets the node_entry of a node.
172  *
173  * @param irn  the node
174  * @param env  the environment
175  */
176 static node_entry *get_irn_ne(ir_node *irn, iv_env *env)
177 {
178         node_entry *e = (node_entry*)get_irn_link(irn);
179
180         if (e == NULL) {
181                 e = OALLOCZ(&env->obst, node_entry);
182                 set_irn_link(irn, e);
183         }
184         return e;
185 }  /* get_irn_ne */
186
187 /**
188  * Gets the scc from an induction variable.
189  *
190  * @param iv   any node of the induction variable
191  * @param env  the environment
192  */
193 static scc *get_iv_scc(ir_node *iv, iv_env *env)
194 {
195         node_entry *e = get_irn_ne(iv, env);
196         return e->pscc;
197 }  /* get_iv_scc */
198
199 /**
200  * Check if irn is an IV.
201  *
202  * @param irn  the node to check
203  * @param env  the environment
204  *
205  * @returns the header if it is one, NULL else
206  */
207 static ir_node *is_iv(ir_node *irn, iv_env *env)
208 {
209         return get_irn_ne(irn, env)->header;
210 }  /* is_iv */
211
212 /**
213  * Check if irn is a region constant.
214  * The block or irn must strictly dominate the header block.
215  *
216  * @param irn           the node to check
217  * @param header_block  the header block of the induction variable
218  */
219 static int is_rc(ir_node *irn, ir_node *header_block)
220 {
221         ir_node *block = get_nodes_block(irn);
222
223         return (block != header_block) && block_dominates(block, header_block);
224 }  /* is_rc */
225
226 /**
227  * Set compare function for the quad set.
228  */
229 static int quad_cmp(const void *e1, const void *e2, size_t size)
230 {
231         const quadruple_t *c1 = (const quadruple_t*)e1;
232         const quadruple_t *c2 = (const quadruple_t*)e2;
233         (void) size;
234
235         return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
236 }  /* quad_cmp */
237
238 /**
239  * Check if an reduced operation was already calculated.
240  *
241  * @param code  the opcode of the operation
242  * @param op1   the first operand of the operation
243  * @param op2   the second operand of the operation
244  * @param env   the environment
245  *
246  * @return the already reduced node or NULL if this operation is not yet reduced
247  */
248 static ir_node *search(unsigned code, ir_node *op1, ir_node *op2, iv_env *env)
249 {
250         quadruple_t key, *entry;
251
252         key.code = code;
253         key.op1 = op1;
254         key.op2 = op2;
255
256         entry = (quadruple_t*)set_find(env->quad_map, &key, sizeof(key),
257                                        (code * 9) ^ hash_ptr(op1) ^hash_ptr(op2));
258         if (entry)
259                 return entry->res;
260         return NULL;
261 }  /* search */
262
263 /**
264  * Add an reduced operation.
265  *
266  * @param code    the opcode of the operation
267  * @param op1     the first operand of the operation
268  * @param op2     the second operand of the operation
269  * @param result  the result of the reduced operation
270  * @param env     the environment
271  */
272 static void add(unsigned code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env)
273 {
274         quadruple_t key;
275
276         key.code = code;
277         key.op1  = op1;
278         key.op2  = op2;
279         key.res  = result;
280
281         set_insert(env->quad_map, &key, sizeof(key),
282                    (code * 9) ^ hash_ptr(op1) ^hash_ptr(op2));
283 }  /* add */
284
285 /**
286  * Find a location where to place a bin-op whose operands are in
287  * block1 and block2.
288  *
289  * @param block1  the block of the first operand
290  * @param block2  the block of the second operand
291  *
292  * Note that we know here that such a place must exists. Moreover, this means
293  * that either block1 dominates block2 or vice versa. So, just return
294  * the "smaller" one.
295  */
296 static ir_node *find_location(ir_node *block1, ir_node *block2)
297 {
298         if (block_dominates(block1, block2))
299                 return block2;
300         assert(block_dominates(block2, block1));
301         return block1;
302 }  /* find_location */
303
304 /**
305  * Create a node that executes an op1 code op1 operation.
306  *
307  * @param code   the opcode to execute
308  * @param db     debug info to add to the new node
309  * @param op1    the first operand
310  * @param op2    the second operand
311  * @param mode   the mode of the new operation
312  *
313  * @return the newly created node
314  */
315 static ir_node *do_apply(unsigned code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode)
316 {
317         ir_node *result;
318         ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
319
320         switch (code) {
321         case iro_Mul:
322                 result = new_rd_Mul(db, block, op1, op2, mode);
323                 break;
324         case iro_Add:
325                 result = new_rd_Add(db, block, op1, op2, mode);
326                 break;
327         case iro_Sub:
328                 result = new_rd_Sub(db, block, op1, op2, mode);
329                 break;
330         default:
331                 panic("Unsupported opcode");
332         }
333         return result;
334 }  /* do_apply */
335
336 /**
337  * The Apply operation.
338  *
339  * @param orig   the node that represent the original operation and determines
340  *               the opcode, debug-info and mode of a newly created one
341  * @param op1    the first operand
342  * @param op2    the second operand
343  * @param env    the environment
344  *
345  * @return the newly created node
346  */
347 static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env)
348 {
349         unsigned code = get_irn_opcode(orig);
350         ir_node *result = search(code, op1, op2, env);
351
352         if (result == NULL) {
353                 dbg_info *db = get_irn_dbg_info(orig);
354                 ir_node *op1_header = get_irn_ne(op1, env)->header;
355                 ir_node *op2_header = get_irn_ne(op2, env)->header;
356
357                 if (op1_header == header && is_rc(op2, op1_header)) {
358                         result = reduce(orig, op1, op2, env);
359                 }
360                 else if (op2_header == header && is_rc(op1, op2_header)) {
361                         result = reduce(orig, op2, op1, env);
362                 }
363                 else {
364                         result = do_apply(code, db, op1, op2, get_irn_mode(orig));
365                         get_irn_ne(result, env)->header = NULL;
366                 }
367         }
368         return result;
369 }  /* apply */
370
371 /**
372  * The Reduce operation.
373  *
374  * @param orig   the node that represent the original operation and determines
375  *               the opcode, debug-info and mode of a newly created one
376  * @param iv     the induction variable
377  * @param rc     the region constant
378  * @param env    the environment
379  *
380  * @return the reduced node
381  */
382 static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env)
383 {
384         unsigned code = get_irn_opcode(orig);
385         ir_node *result = search(code, iv, rc, env);
386
387         /* check if we have already done this operation on the iv */
388         if (result == NULL) {
389                 node_entry *e, *iv_e;
390                 int i;
391                 ir_mode *mode = get_irn_mode(orig);
392
393                 result = exact_copy(iv);
394
395                 if (get_irn_mode(result) != mode) {
396                         /*
397                          * Beware: we must always create a new induction variable with the same mode
398                          * as the node we are replacing. Especially this means the mode might be changed
399                          * from P to I and back. This is always possible, because we have only Phi, Add
400                          * and Sub nodes.
401                          * However, this might lead to AddIs(Iu,Is) which we must fix. The best way to do this
402                          * seems to be a post-pass, or we might end with useless Conv's.
403                          */
404                         set_irn_mode(result, mode);
405                         env->need_postpass = 1;
406                 }
407                 add(code, iv, rc, result, env);
408                 DB((dbg, LEVEL_3, "   Created new %+F for %+F (%s %+F)\n", result, iv,
409                         get_irn_opname(orig), rc));
410
411                 iv_e = get_irn_ne(iv, env);
412                 e    = get_irn_ne(result, env);
413                 e->header = iv_e->header;
414
415                 /* create the LFTR edge */
416                 LFTR_add(iv, result, code, rc, env);
417
418                 for (i = get_irn_arity(result) - 1; i >= 0; --i) {
419                         ir_node *o = get_irn_n(result, i);
420
421                         e = get_irn_ne(o, env);
422                         if (e->header == iv_e->header)
423                                 o = reduce(orig, o, rc, env);
424                         else if (is_Phi(result) || code == iro_Mul)
425                                 o = apply(iv_e->header, orig, o, rc, env);
426                         set_irn_n(result, i, o);
427                 }
428         } else {
429                 DB((dbg, LEVEL_3, "   Already Created %+F for %+F (%s %+F)\n", result, iv,
430                         get_irn_opname(orig), rc));
431         }
432         return result;
433 }  /* reduce */
434
435 /**
436  * Update the scc for a newly created IV.
437  */
438 static void update_scc(ir_node *iv, node_entry *e, iv_env *env)
439 {
440         scc     *pscc   = e->pscc;
441         ir_node *header = e->header;
442         waitq    *wq = new_waitq();
443
444         DB((dbg, LEVEL_2, "  Creating SCC for new an induction variable:\n  "));
445         pscc->head = NULL;
446         waitq_put(wq, iv);
447         do {
448                 ir_node    *irn = (ir_node*)waitq_get(wq);
449                 node_entry *ne  = get_irn_ne(irn, env);
450                 int        i;
451
452                 ne->pscc   = pscc;
453                 ne->next   = pscc->head;
454                 pscc->head = irn;
455                 DB((dbg, LEVEL_2, " %+F,", irn));
456
457                 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
458                         ir_node    *pred = get_irn_n(irn, i);
459                         node_entry *pe   = get_irn_ne(pred, env);
460
461                         if (pe->header == header && pe->pscc == NULL) {
462                                 /* set the pscc here to ensure that the node is NOT enqueued another time */
463                                 pe->pscc = pscc;
464                                 waitq_put(wq, pred);
465                         }
466                 }
467         } while (! waitq_empty(wq));
468         del_waitq(wq);
469         DB((dbg, LEVEL_2, "\n"));
470 }  /* update_scc */
471
472 /**
473  * The Replace operation. We found a node representing iv (+,-,*) rc
474  * that can be removed by replacing the induction variable iv by a new
475  * one that 'applies' the operation 'irn'.
476  *
477  * @param irn   the node that will be replaced
478  * @param iv    the induction variable
479  * @param rc    the region constant
480  * @param env   the environment
481  */
482 static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env)
483 {
484         ir_node *result;
485
486         DB((dbg, LEVEL_2, "  Replacing %+F\n", irn));
487
488         result = reduce(irn, iv, rc, env);
489         if (result != irn) {
490                 node_entry *e;
491
492                 hook_strength_red(get_irn_irg(irn), irn);
493                 exchange(irn, result);
494                 e = get_irn_ne(result, env);
495                 if (e->pscc == NULL) {
496                         e->pscc = OALLOCZ(&env->obst, scc);
497                         update_scc(result, e, env);
498                 }
499                 ++env->replaced;
500                 return 1;
501         }
502         return 0;
503 }  /* replace */
504
505 #if 0
506 /**
507  * check if a given node is a mul with 2, 4, 8
508  */
509 static int is_x86_shift_const(ir_node *mul)
510 {
511         ir_node *rc;
512
513         if (! is_Mul(mul))
514                 return 0;
515
516         /* normalization put constants on the right side */
517         rc = get_Mul_right(mul);
518         if (is_Const(rc)) {
519                 ir_tarval *tv = get_Const_tarval(rc);
520
521                 if (tarval_is_long(tv)) {
522                         long value = get_tarval_long(tv);
523
524                         if (value == 2 || value == 4 || value == 8) {
525                                 /* do not reduce multiplications by 2, 4, 8 */
526                                 return 1;
527                         }
528                 }
529         }
530         return 0;
531 }  /* is_x86_shift_const */
532 #endif
533
534 /**
535  * Check if an IV represents a counter with constant limits.
536  *
537  * @param iv    any node of the induction variable
538  * @param env   the environment
539  */
540 static int is_counter_iv(ir_node *iv, iv_env *env)
541 {
542         node_entry *e         = get_irn_ne(iv, env);
543         scc        *pscc      = e->pscc;
544         ir_node    *have_init = NULL;
545         ir_node    *have_incr = NULL;
546         ir_opcode  code       = iro_Bad;
547         ir_node    *irn;
548
549         if (pscc->code != 0) {
550                 /* already analysed */
551                 return pscc->code != iro_Bad;
552         }
553
554         pscc->code = iro_Bad;
555         for (irn = pscc->head; irn != NULL; irn = e->next) {
556                 if (is_Add(irn)) {
557                         if (have_incr != NULL)
558                                 return 0;
559
560                         have_incr = get_Add_right(irn);
561                         if (! is_Const(have_incr)) {
562                                 have_incr = get_Add_left(irn);
563                                 if (! is_Const(have_incr))
564                                         return 0;
565                         }
566                         code = iro_Add;
567                 } else if (is_Sub(irn)) {
568                         if (have_incr != NULL)
569                                 return 0;
570
571                         have_incr = get_Sub_right(irn);
572                         if (! is_Const(have_incr))
573                                 return 0;
574                         code = iro_Sub;
575                 } else if (is_Phi(irn)) {
576                         int i;
577
578                         for (i = get_Phi_n_preds(irn) - 1; i >= 0; --i) {
579                                 ir_node    *pred = get_Phi_pred(irn, i);
580                                 node_entry *ne   = get_irn_ne(pred, env);
581
582                                 if (ne->header == e->header)
583                                         continue;
584                                 if (have_init != NULL)
585                                         return 0;
586                                 have_init = pred;
587                                 if (! is_Const(pred))
588                                         return 0;
589                         }
590                 } else
591                         return 0;
592                 e = get_irn_ne(irn, env);
593         }
594         pscc->init = get_Const_tarval(have_init);
595         pscc->incr = get_Const_tarval(have_incr);
596         pscc->code = code;
597         return code != iro_Bad;
598 }  /* is_counter_iv */
599
600 /**
601  * Check the users of an induction variable for register pressure.
602  *
603  * @param iv    any node of the induction variable
604  * @param env   the environment
605  *
606  * @return non-zero if the register pressure is estimated
607  *         to not increase, zero else
608  */
609 static int check_users_for_reg_pressure(ir_node *iv, iv_env *env)
610 {
611         ir_node    *irn;
612         ir_node    *have_user = NULL;
613         ir_node    *have_cmp  = NULL;
614         node_entry *e         = get_irn_ne(iv, env);
615         scc        *pscc      = e->pscc;
616
617         for (irn = pscc->head; irn != NULL; irn = e->next) {
618                 const ir_edge_t *edge;
619
620                 foreach_out_edge(irn, edge) {
621                         ir_node    *user = get_edge_src_irn(edge);
622                         node_entry *ne = get_irn_ne(user, env);
623
624                         if (e->header == ne->header) {
625                                 /* found user from the same IV */
626                                 continue;
627                         }
628                         if (is_Cmp(user)) {
629                                 if (have_cmp != NULL) {
630                                         /* more than one cmp, for now end here */
631                                         return 0;
632                                 }
633                                 have_cmp = user;
634                         } else {
635                                 /* user is a real user of the IV */
636                                 if (have_user != NULL) {
637                                         /* found the second user */
638                                         return 0;
639                                 }
640                                 have_user = user;
641                         }
642                 }
643                 e = get_irn_ne(irn, env);
644         }
645
646         if (have_user == NULL) {
647                 /* no user, ignore */
648                 return 1;
649         }
650
651         if (have_cmp == NULL) {
652                 /* fine, only one user, try to reduce */
653                 return 1;
654         }
655         /*
656          * We found one user AND at least one cmp.
657          * We should check here if we can transform the Cmp.
658          *
659          * For now our capabilities for doing linear function test
660          * are limited, so check if the iv has the right form: Only ONE
661          * Phi, only one Add/Sub with a Const.
662          */
663         if (! is_counter_iv(iv, env))
664                 return 0;
665
666         /*
667          * Ok, we have only one increment AND it is a Const, we might be able
668          * to do a linear function test replacement, so go on.
669          */
670         return 1;
671 }  /* check_users_for_reg_pressure */
672
673 /**
674  * Check if a node can be replaced (+, -, *).
675  *
676  * @param irn   the node to check
677  * @param env   the environment
678  *
679  * @return non-zero if irn should be Replace'd
680  */
681 static int check_replace(ir_node *irn, iv_env *env)
682 {
683         ir_node   *left, *right, *iv, *rc;
684         ir_op     *op  = get_irn_op(irn);
685         unsigned  code = get_op_code(op);
686         ir_node   *liv, *riv;
687
688         switch (code) {
689         case iro_Mul:
690         case iro_Add:
691         case iro_Sub:
692                 iv = rc = NULL;
693
694                 left  = get_binop_left(irn);
695                 right = get_binop_right(irn);
696
697                 liv = is_iv(left, env);
698                 riv = is_iv(right, env);
699                 if (liv && is_rc(right, liv)) {
700                         iv = left; rc = right;
701                 }
702                 else if (riv && is_op_commutative(op) &&
703                                     is_rc(left, riv)) {
704                         iv = right; rc = left;
705                 }
706
707                 if (iv) {
708                         if (env->osr_flags & osr_flag_keep_reg_pressure) {
709                                 if (! check_users_for_reg_pressure(iv, env))
710                                         return 0;
711                         }
712                         return replace(irn, iv, rc, env);
713                 }
714                 break;
715         default:
716                 break;
717         }
718         return 0;
719 }  /* check_replace */
720
721 /**
722  * Check which SCC's are induction variables.
723  *
724  * @param pscc  a SCC
725  * @param env   the environment
726  */
727 static void classify_iv(scc *pscc, iv_env *env)
728 {
729         ir_node *irn, *next, *header = NULL;
730         node_entry *b, *h = NULL;
731         int j, only_phi, num_outside;
732         ir_node *out_rc;
733
734         /* find the header block for this scc */
735         for (irn = pscc->head; irn; irn = next) {
736                 node_entry *e = (node_entry*)get_irn_link(irn);
737                 ir_node *block = get_nodes_block(irn);
738
739                 next = e->next;
740                 b = get_irn_ne(block, env);
741
742                 if (header) {
743                         if (h->POnum < b->POnum) {
744                                 header = block;
745                                 h      = b;
746                         }
747                 }
748                 else {
749                         header = block;
750                         h      = b;
751                 }
752         }
753
754         /* check if this scc contains only Phi, Add or Sub nodes */
755         only_phi    = 1;
756         num_outside = 0;
757         out_rc      = NULL;
758         for (irn = pscc->head; irn; irn = next) {
759                 node_entry *e = get_irn_ne(irn, env);
760
761                 next = e->next;
762                 switch (get_irn_opcode(irn)) {
763                 case iro_Sub:
764                         only_phi = 0;
765                         {
766                                 ir_node    *left        = get_Sub_left(irn);
767                                 node_entry *left_entry  = get_irn_ne(left, env);
768                                 ir_node    *right       = get_Sub_right(irn);
769                                 node_entry *right_entry = get_irn_ne(right, env);
770
771                                 if (left_entry->pscc != e->pscc ||
772                                     (right_entry->pscc != e->pscc && !is_rc(right, header))) {
773                                         /*
774                                          * Not an induction variable.
775                                          * Region constant are only allowed on right hand side.
776                                          */
777                                         goto fail;
778                                 }
779                         }
780                         break;
781
782                 case iro_Add:
783                         only_phi = 0;
784                         /* fall through */
785                 case iro_Phi:
786                         for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
787                                 ir_node *pred  = get_irn_n(irn, j);
788                                 node_entry *pe = get_irn_ne(pred, env);
789
790                                 if (pe->pscc != e->pscc) {
791                                         /* not in the same SCC, must be a region const */
792                                         if (! is_rc(pred, header)) {
793                                                 /* not an induction variable */
794                                                 goto fail;
795                                         }
796                                         if (! out_rc) {
797                                                 out_rc = pred;
798                                                 ++num_outside;
799                                         } else if (out_rc != pred) {
800                                                 ++num_outside;
801                                         }
802                                 }
803                         }
804                         break;
805                 default:
806                         /* not an induction variable */
807                         goto fail;
808                 }
809         }
810         /* found an induction variable */
811         DB((dbg, LEVEL_2, "  Found an induction variable:\n  "));
812         if (only_phi && num_outside == 1) {
813                 /* a phi cycle with only one real predecessor can be collapsed */
814                 DB((dbg, LEVEL_2, "  Found an USELESS Phi cycle:\n  "));
815
816                 for (irn = pscc->head; irn; irn = next) {
817                         node_entry *e = get_irn_ne(irn, env);
818                         next = e->next;
819                         e->header = NULL;
820                         exchange(irn, out_rc);
821                 }
822                 ++env->replaced;
823                 return;
824         }
825
826         /* set the header for every node in this scc */
827         for (irn = pscc->head; irn; irn = next) {
828                 node_entry *e = get_irn_ne(irn, env);
829                 e->header = header;
830                 next = e->next;
831                 DB((dbg, LEVEL_2, " %+F,", irn));
832         }
833         DB((dbg, LEVEL_2, "\n"));
834         return;
835
836 fail:
837         for (irn = pscc->head; irn; irn = next) {
838                 node_entry *e = get_irn_ne(irn, env);
839
840                 next = e->next;
841                 e->header = NULL;
842         }
843 }  /* classify_iv */
844
845 /**
846  * Process an SCC for the operator strength reduction.
847  *
848  * @param pscc  the SCC
849  * @param env   the environment
850  */
851 static void process_scc(scc *pscc, iv_env *env)
852 {
853         ir_node *head = pscc->head;
854         node_entry *e = (node_entry*)get_irn_link(head);
855
856 #ifdef DEBUG_libfirm
857         {
858                 ir_node *irn, *next;
859
860                 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
861                 for (irn = pscc->head; irn != NULL; irn = next) {
862                         node_entry *e = (node_entry*)get_irn_link(irn);
863
864                         next = e->next;
865
866                         DB((dbg, LEVEL_4, " %+F,", irn));
867                 }
868                 DB((dbg, LEVEL_4, "\n"));
869         }
870 #endif
871
872         if (e->next == NULL) {
873                 /* this SCC has only a single member */
874                 check_replace(head, env);
875         } else {
876                 classify_iv(pscc, env);
877         }
878 }  /* process_scc */
879
880 /**
881  * If an SCC is a Phi only cycle, remove it.
882  *
883  * @param pscc  an SCC that consists of Phi nodes only
884  * @param env   the environment
885  */
886 static void remove_phi_cycle(scc *pscc, iv_env *env)
887 {
888         ir_node *irn, *next;
889         int j;
890         ir_node *out_rc;
891
892         /* check if this scc contains only Phi nodes */
893         out_rc      = NULL;
894         for (irn = pscc->head; irn; irn = next) {
895                 node_entry *e = get_irn_ne(irn, env);
896
897                 next = e->next;
898                 if (! is_Phi(irn))
899                         return;
900
901                 for (j = get_irn_arity(irn) - 1; j >= 0; --j) {
902                         ir_node *pred  = get_irn_n(irn, j);
903                         node_entry *pe = get_irn_ne(pred, env);
904
905                         if (pe->pscc != e->pscc) {
906                                 /* not in the same SCC, must be the only input */
907                                 if (! out_rc) {
908                                         out_rc = pred;
909                                 } else if (out_rc != pred) {
910                                         return;
911                                 }
912                         }
913                 }
914         }
915         /* found a Phi cycle */
916         DB((dbg, LEVEL_2, "  Found an USELESS Phi cycle:\n  "));
917
918         for (irn = pscc->head; irn; irn = next) {
919                 node_entry *e = get_irn_ne(irn, env);
920                 next = e->next;
921                 e->header = NULL;
922                 exchange(irn, out_rc);
923         }
924         ++env->replaced;
925 }  /* remove_phi_cycle */
926
927 /**
928  * Process a SCC for the Phi cycle remove.
929  *
930  * @param pscc  the SCC
931  * @param env   the environment
932  */
933 static void process_phi_only_scc(scc *pscc, iv_env *env)
934 {
935         ir_node *head = pscc->head;
936         node_entry *e = (node_entry*)get_irn_link(head);
937
938 #ifdef DEBUG_libfirm
939         {
940                 ir_node *irn, *next;
941
942                 DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
943                 for (irn = pscc->head; irn; irn = next) {
944                         node_entry *e = (node_entry*)get_irn_link(irn);
945
946                         next = e->next;
947
948                         DB((dbg, LEVEL_4, " %+F,", irn));
949                 }
950                 DB((dbg, LEVEL_4, "\n"));
951         }
952 #endif
953
954         if (e->next != NULL)
955                 remove_phi_cycle(pscc, env);
956 }  /* process_phi_only_scc */
957
958
959 /**
960  * Push a node onto the stack.
961  *
962  * @param env   the environment
963  * @param n     the node to push
964  */
965 static void push(iv_env *env, ir_node *n)
966 {
967         node_entry *e;
968
969         if (env->tos == ARR_LEN(env->stack)) {
970                 size_t nlen = ARR_LEN(env->stack) * 2;
971                 ARR_RESIZE(ir_node *, env->stack, nlen);
972         }
973         env->stack[env->tos++] = n;
974         e = get_irn_ne(n, env);
975         e->in_stack = 1;
976 }  /* push */
977
978 /**
979  * Pop a node from the stack.
980  *
981  * @param env   the environment
982  *
983  * @return  The topmost node
984  */
985 static ir_node *pop(iv_env *env)
986 {
987         ir_node *n = env->stack[--env->tos];
988         node_entry *e = get_irn_ne(n, env);
989
990         e->in_stack = 0;
991         return n;
992 }  /* pop */
993
994 /**
995  * Do Tarjan's SCC algorithm and drive OSR.
996  *
997  * @param irn  start at this node
998  * @param env  the environment
999  */
1000 static void dfs(ir_node *irn, iv_env *env)
1001 {
1002         int i, n;
1003         node_entry *node = get_irn_ne(irn, env);
1004
1005         mark_irn_visited(irn);
1006
1007         /* do not put blocks into the scc */
1008         if (is_Block(irn)) {
1009                 n = get_irn_arity(irn);
1010                 for (i = 0; i < n; ++i) {
1011                         ir_node *pred = get_irn_n(irn, i);
1012
1013                         if (!irn_visited(pred))
1014                                 dfs(pred, env);
1015                 }
1016         } else {
1017                 ir_node *block = get_nodes_block(irn);
1018
1019                 node->DFSnum = env->nextDFSnum++;
1020                 node->low    = node->DFSnum;
1021                 push(env, irn);
1022
1023                 /* handle the block */
1024                 if (!irn_visited(block))
1025                         dfs(block, env);
1026
1027                 n = get_irn_arity(irn);
1028                 for (i = 0; i < n; ++i) {
1029                         ir_node *pred = get_irn_n(irn, i);
1030                         node_entry *o = get_irn_ne(pred, env);
1031
1032                         if (!irn_visited(pred)) {
1033                                 dfs(pred, env);
1034                                 node->low = MIN(node->low, o->low);
1035                         }
1036                         if (o->DFSnum < node->DFSnum && o->in_stack)
1037                                 node->low = MIN(o->DFSnum, node->low);
1038                 }
1039                 if (node->low == node->DFSnum) {
1040                         scc *pscc = OALLOCZ(&env->obst, scc);
1041                         ir_node *x;
1042
1043                         do {
1044                                 node_entry *e;
1045
1046                                 x = pop(env);
1047                                 e = get_irn_ne(x, env);
1048                                 e->pscc    = pscc;
1049                                 e->next    = pscc->head;
1050                                 pscc->head = x;
1051                         } while (x != irn);
1052
1053                         env->process_scc(pscc, env);
1054                 }
1055         }
1056 }  /* dfs */
1057
1058 /**
1059  * Do the DFS by starting at the End node of a graph.
1060  *
1061  * @param irg  the graph to process
1062  * @param env  the environment
1063  */
1064 static void do_dfs(ir_graph *irg, iv_env *env)
1065 {
1066         ir_node  *end = get_irg_end(irg);
1067         int i;
1068
1069         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
1070
1071         inc_irg_visited(irg);
1072
1073         /* visit all visible nodes */
1074         dfs(end, env);
1075
1076         /* visit the keep-alives */
1077         for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
1078                 ir_node *ka = get_End_keepalive(end, i);
1079
1080                 if (!irn_visited(ka))
1081                         dfs(ka, env);
1082         }
1083
1084         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
1085 }  /* do_dfs */
1086
1087 /**
1088  * Post-block-walker: assign the post-order number.
1089  */
1090 static void assign_po(ir_node *block, void *ctx)
1091 {
1092         iv_env *env = (iv_env*)ctx;
1093         node_entry *e = get_irn_ne(block, env);
1094
1095         e->POnum = env->POnum++;
1096 }  /* assign_po */
1097
1098 /**
1099  * Apply one LFTR edge operation.
1100  * Return NULL if the transformation cannot be done safely without
1101  * an Overflow.
1102  *
1103  * @param iv   the induction variable
1104  * @param rc   the constant that should be translated
1105  * @param e    the LFTR edge
1106  * @param env  the IV environment
1107  *
1108  * @return the translated region constant or NULL
1109  *         if the translation was not possible
1110  *
1111  * @note
1112  * In the current implementation only the last edge is stored, so
1113  * only one chain exists. That's why we might miss some opportunities.
1114  */
1115 static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env)
1116 {
1117         if (env->osr_flags & osr_flag_lftr_with_ov_check) {
1118                 ir_tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr, *tv_end;
1119                 tarval_int_overflow_mode_t ovmode;
1120                 scc *pscc;
1121                 ir_graph *irg;
1122
1123                 if (! is_counter_iv(iv, env)) {
1124                         DB((dbg, LEVEL_4, " not counter IV"));
1125                         return NULL;
1126                 }
1127
1128                 /* overflow can only be decided for Consts */
1129                 if (! is_Const(e->rc)) {
1130                         if (e->code == iro_Add && mode_is_reference(get_irn_mode(e->rc))) {
1131                                 /* However we allow ONE Pointer Add, as pointer arithmetic with wrap
1132                                    around is undefined anyway */
1133                                 return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(e->rc));
1134                         }
1135                         DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
1136                         return NULL;
1137                 }
1138
1139                 tv_l = get_Const_tarval(rc);
1140                 tv_r = get_Const_tarval(e->rc);
1141
1142                 ovmode = tarval_get_integer_overflow_mode();
1143                 tarval_set_integer_overflow_mode(TV_OVERFLOW_BAD);
1144
1145                 pscc    = get_iv_scc(iv, env);
1146                 tv_incr = pscc->incr;
1147                 tv_init = pscc->init;
1148
1149                 /*
1150                  * Check that no overflow occurs:
1151                  * init must be transformed without overflow
1152                  * the new rc must be transformed without overflow
1153                  * rc +/- incr must be possible without overflow
1154                  */
1155                 switch (e->code) {
1156                 case iro_Mul:
1157                         tv      = tarval_mul(tv_l, tv_r);
1158                         tv_init = tarval_mul(tv_init, tv_r);
1159                         tv_incr = tarval_mul(tv_incr, tv_r);
1160                         DB((dbg, LEVEL_4, " * %+F", tv_r));
1161                         break;
1162                 case iro_Add:
1163                         tv      = tarval_add(tv_l, tv_r);
1164                         tv_init = tarval_add(tv_init, tv_r);
1165                         DB((dbg, LEVEL_4, " + %+F", tv_r));
1166                         break;
1167                 case iro_Sub:
1168                         tv      = tarval_sub(tv_l, tv_r, NULL);
1169                         tv_init = tarval_sub(tv_init, tv_r, NULL);
1170                         DB((dbg, LEVEL_4, " - %+F", tv_r));
1171                         break;
1172                 default:
1173                         panic("Unsupported opcode");
1174                 }
1175
1176                 if (tv == tarval_bad || tv_init == tarval_bad) {
1177                         tarval_set_integer_overflow_mode(ovmode);
1178                         DB((dbg, LEVEL_4, " = OVERFLOW"));
1179                         return NULL;
1180                 }
1181
1182                 if (pscc->code == iro_Add) {
1183                         tv_end = tarval_add(tv, tv_incr);
1184                 } else {
1185                         assert(pscc->code == iro_Sub);
1186                         tv_end = tarval_sub(tv, tv_incr, NULL);
1187                 }
1188
1189                 tarval_set_integer_overflow_mode(ovmode);
1190
1191                 if (tv_end == tarval_bad) {
1192                         DB((dbg, LEVEL_4, " = OVERFLOW"));
1193                         return NULL;
1194                 }
1195                 irg = get_irn_irg(iv);
1196                 return new_r_Const(irg, tv);
1197         }
1198         return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(e->dst));
1199 }  /* applyOneEdge */
1200
1201 /**
1202  * Applies the operations represented by the LFTR edges to a
1203  * region constant and returns the value.
1204  * Return NULL if the transformation cannot be done safely without
1205  * an Overflow.
1206  *
1207  * @param pIV  points to the IV node that starts the LFTR edge chain
1208  *             after translation points to the new IV
1209  * @param rc   the region constant that should be translated
1210  * @param env  the IV environment
1211  *
1212  * @return the translated region constant or NULL
1213  *         if the translation was not possible
1214  */
1215 static ir_node *applyEdges(ir_node **pIV, ir_node *rc, iv_env *env)
1216 {
1217         ir_node *iv = *pIV;
1218         if (env->osr_flags & osr_flag_lftr_with_ov_check) {
1219                 /* overflow can only be decided for Consts */
1220                 if (! is_counter_iv(iv, env)) {
1221                         DB((dbg, LEVEL_4, "not counter IV\n", rc));
1222                         return NULL;
1223                 }
1224                 if (! is_Const(rc)) {
1225                         DB((dbg, LEVEL_4, " = UNKNOWN (%+F)\n", rc));
1226                         return NULL;
1227                 }
1228                 DB((dbg, LEVEL_4, "%+F", get_Const_tarval(rc)));
1229         }
1230
1231         for (; rc;) {
1232                 LFTR_edge *e = LFTR_find(iv, env);
1233                 if (e != NULL) {
1234                         rc = applyOneEdge(iv, rc, e, env);
1235                         iv = e->dst;
1236                 } else
1237                         break;
1238         }
1239         DB((dbg, LEVEL_3, "\n"));
1240         *pIV = iv;
1241         return rc;
1242 }  /* applyEdges */
1243
1244 /**
1245  * Walker, finds Cmp(iv, rc) or Cmp(rc, iv)
1246  * and tries to optimize them.
1247  */
1248 static void do_lftr(ir_node *cmp, void *ctx)
1249 {
1250         iv_env *env = (iv_env*)ctx;
1251         ir_node *left, *right, *liv, *riv;
1252         ir_node *iv, *rc;
1253         ir_node *nleft = NULL, *nright = NULL;
1254
1255         if (!is_Cmp(cmp))
1256                 return;
1257
1258         left  = get_Cmp_left(cmp);
1259         right = get_Cmp_right(cmp);
1260
1261         liv = is_iv(left, env);
1262         riv = is_iv(right, env);
1263         if (liv && is_rc(right, liv)) {
1264                 iv = left; rc = right;
1265
1266                 nright = applyEdges(&iv, rc, env);
1267                 nleft  = iv;
1268         }
1269         else if (riv && is_rc(left, riv)) {
1270                 iv = right; rc = left;
1271
1272                 nleft  = applyEdges(&iv, rc, env);
1273                 nright = iv;
1274         }
1275
1276         if (nleft && nright) {
1277                 DB((dbg, LEVEL_2, "  LFTR for %+F\n", cmp));
1278                 set_Cmp_left(cmp, nleft);
1279                 set_Cmp_right(cmp, nright);
1280                 ++env->lftr_replaced;
1281         }
1282 }  /* do_lftr */
1283
1284 /**
1285  * do linear function test replacement.
1286  *
1287  * @param irg   the graph that should be optimized
1288  * @param env   the IV environment
1289  */
1290 static void lftr(ir_graph *irg, iv_env *env)
1291 {
1292         irg_walk_graph(irg, NULL, do_lftr, env);
1293 }  /* lftr */
1294
1295 /* Remove any Phi cycles with only one real input. */
1296 void remove_phi_cycles(ir_graph *irg)
1297 {
1298         iv_env env;
1299
1300         assure_irg_properties(irg,
1301                 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
1302                 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS
1303                 | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
1304
1305         FIRM_DBG_REGISTER(dbg, "firm.opt.remove_phi");
1306
1307         DB((dbg, LEVEL_1, "Doing Phi cycle removement for %+F\n", irg));
1308
1309         obstack_init(&env.obst);
1310         env.stack         = NEW_ARR_F(ir_node *, 128);
1311         env.tos           = 0;
1312         env.nextDFSnum    = 0;
1313         env.POnum         = 0;
1314         env.quad_map      = NULL;
1315         env.lftr_edges    = NULL;
1316         env.replaced      = 0;
1317         env.lftr_replaced = 0;
1318         env.osr_flags     = 0;
1319         env.need_postpass = 0;
1320         env.process_scc   = process_phi_only_scc;
1321
1322         /* Clear all links and move Proj nodes into the
1323          * the same block as their predecessors.
1324          * This can improve the placement of new nodes.
1325          */
1326         irg_walk_graph(irg, NULL, firm_clear_link, NULL);
1327
1328         /* calculate the post order number for blocks. */
1329         irg_out_block_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1330
1331         /* calculate the SCC's and drive OSR. */
1332         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
1333         do_dfs(irg, &env);
1334         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1335
1336         if (env.replaced) {
1337                 DB((dbg, LEVEL_1, "remove_phi_cycles: %u Cycles removed\n\n",
1338                     env.replaced));
1339         }
1340
1341         DEL_ARR_F(env.stack);
1342         obstack_free(&env.obst, NULL);
1343
1344         confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_CONTROL_FLOW);
1345 }
1346
1347 ir_graph_pass_t *remove_phi_cycles_pass(const char *name)
1348 {
1349         return def_graph_pass(name ? name : "remove_phi_cycles", remove_phi_cycles);
1350 }  /* remove_phi_cycles_pass */
1351
1352 /**
1353  * Post-walker: fix Add and Sub nodes that where results of I<->P conversions.
1354  */
1355 static void fix_adds_and_subs(ir_node *irn, void *ctx)
1356 {
1357         (void) ctx;
1358
1359         if (is_Add(irn)) {
1360                 ir_mode *mode = get_irn_mode(irn);
1361
1362                 if (mode_is_int(mode)) {
1363                         ir_node *pred;
1364
1365                         pred = get_Add_left(irn);
1366                         if (get_irn_mode(pred) != mode) {
1367                                 ir_node *block = get_nodes_block(pred);
1368
1369                                 pred = new_r_Conv(block, pred, mode);
1370                                 set_Add_left(irn, pred);
1371                         }
1372                         pred = get_Add_right(irn);
1373                         if (get_irn_mode(pred) != mode) {
1374                                 ir_node *block = get_nodes_block(pred);
1375
1376                                 pred = new_r_Conv(block, pred, mode);
1377                                 set_Add_right(irn, pred);
1378                         }
1379                 }
1380         } else if (is_Sub(irn)) {
1381                 ir_mode *mode = get_irn_mode(irn);
1382
1383                 if (mode_is_int(mode)) {
1384                         ir_node *left   = get_Sub_left(irn);
1385                         ir_node *right  = get_Sub_right(irn);
1386                         ir_mode *l_mode = get_irn_mode(left);
1387                         ir_mode *r_mode = get_irn_mode(right);
1388
1389                         if (mode_is_int(l_mode) && mode_is_int(r_mode)) {
1390                                 if (l_mode != mode) {
1391                                         ir_node *block = get_nodes_block(left);
1392
1393                                         left = new_r_Conv(block, left, mode);
1394                                         set_Sub_left(irn, left);
1395                                 }
1396                                 if (r_mode != mode) {
1397                                         ir_node *block = get_nodes_block(right);
1398
1399                                         right = new_r_Conv(block, right, mode);
1400                                         set_Sub_right(irn, right);
1401                                 }
1402                         }
1403                 } else if (mode_is_reference(mode)) {
1404                         ir_node *left   = get_Sub_left(irn);
1405                         ir_node *right  = get_Sub_right(irn);
1406                         ir_mode *l_mode = get_irn_mode(left);
1407                         ir_mode *r_mode = get_irn_mode(right);
1408                         if (mode_is_int(l_mode)) {
1409                                 /* Usually, Sub(I*,P) is an error, hence the verifier rejects it.
1410                                  * However, it is correct in this case, so add Conv to make verifier happy. */
1411                                 ir_node *block = get_nodes_block(right);
1412                                 ir_node *lconv = new_r_Conv(block, left, r_mode);
1413                                 assert(mode_is_reference(r_mode));
1414                                 set_Sub_left(irn, lconv);
1415                         }
1416                 }
1417         }
1418 }  /* fix_adds_and_subs */
1419
1420 /* Performs Operator Strength Reduction for the passed graph. */
1421 void opt_osr(ir_graph *irg, unsigned flags)
1422 {
1423         iv_env env;
1424
1425         FIRM_DBG_REGISTER(dbg, "firm.opt.osr");
1426
1427         assure_irg_properties(irg,
1428                 IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
1429                 | IR_GRAPH_PROPERTY_CONSISTENT_OUTS
1430                 | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
1431
1432         DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));
1433
1434         obstack_init(&env.obst);
1435         env.stack         = NEW_ARR_F(ir_node *, 128);
1436         env.tos           = 0;
1437         env.nextDFSnum    = 0;
1438         env.POnum         = 0;
1439         env.quad_map      = new_set(quad_cmp, 64);
1440         env.lftr_edges    = new_set(LFTR_cmp, 64);
1441         env.replaced      = 0;
1442         env.lftr_replaced = 0;
1443         env.osr_flags     = flags;
1444         env.need_postpass = 0;
1445         env.process_scc   = process_scc;
1446
1447         /* Clear all links and move Proj nodes into the
1448          * the same block as its predecessors.
1449          * This can improve the placement of new nodes.
1450          */
1451         irg_walk_graph(irg, NULL, firm_clear_link, NULL);
1452
1453         irg_block_edges_walk(get_irg_start_block(irg), NULL, assign_po, &env);
1454
1455         /* calculate the SCC's and drive OSR. */
1456         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
1457         do_dfs(irg, &env);
1458
1459         if (env.replaced) {
1460                 if (env.need_postpass)
1461                         irg_walk_graph(irg, NULL, fix_adds_and_subs, &env);
1462
1463                 /* try linear function test replacements */
1464                 lftr(irg, &env);
1465                 (void)lftr;
1466
1467                 DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));
1468         }
1469         ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
1470
1471         del_set(env.lftr_edges);
1472         del_set(env.quad_map);
1473         DEL_ARR_F(env.stack);
1474         obstack_free(&env.obst, NULL);
1475
1476         confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
1477 }
1478
1479 typedef struct pass_t {
1480         ir_graph_pass_t pass;
1481         unsigned        flags;
1482 } pass_t;
1483
1484 /**
1485 * Wrapper for running opt_osr() as an ir_graph pass.
1486 */
1487 static int pass_wrapper(ir_graph *irg, void *context)
1488 {
1489         pass_t *pass = (pass_t*)context;
1490         opt_osr(irg, pass->flags);
1491         return 0;
1492 }  /* pass_wrapper */
1493
1494 ir_graph_pass_t *opt_osr_pass(const char *name, unsigned flags)
1495 {
1496         pass_t *pass = XMALLOCZ(pass_t);
1497
1498         pass->flags = flags;
1499         return def_graph_pass_constructor(
1500                 &pass->pass, name ? name : "osr", pass_wrapper);
1501 }  /* opt_osr_pass */