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