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