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