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