simplify opt_funccall interface
[libfirm] / ir / opt / opt_osr.c
index c479549..9f8043e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
@@ -23,7 +23,7 @@
  * @date    12.5.2006
  * @author  Michael Beck
  * @version $Id$
- * @summary
+ * @brief
  *  Implementation of the Operator Strength Reduction algorithm
  *  by Keith D. Cooper, L. Taylor Simpson, Christopher A. Vick.
  *  Extended version.
 #include "array.h"
 #include "firmstat.h"
 #include "error.h"
+#include "irpass_t.h"
 
 /** The debug handle. */
 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
 
 /** A scc. */
 typedef struct scc {
-       ir_node  *head;         /**< the head of the list */
-       tarval   *init;     /**< the init value iff only one exists. */
-       tarval   *incr;     /**< the induction variable increment if only a single const exists. */
-       unsigned code;      /**< == iro_Add if +incr, iro_Sub if -incr, 0 if not analysed, iro_Bad else */
+       ir_node   *head; /**< the head of the list */
+       ir_tarval *init; /**< the init value iff only one exists. */
+       ir_tarval *incr; /**< the induction variable increment if only a single const exists. */
+       unsigned   code; /**< == iro_Add if +incr, iro_Sub if -incr, 0 if not analysed, iro_Bad else */
 } scc;
 
 /** A node entry */
@@ -78,7 +79,7 @@ typedef struct node_entry {
 typedef struct iv_env {
        struct obstack obst;    /**< an obstack for allocations */
        ir_node  **stack;       /**< the node stack */
-       int      tos;           /**< tos index */
+       size_t   tos;           /**< tos index */
        unsigned nextDFSnum;    /**< the current DFS number */
        unsigned POnum;         /**< current post order number */
        set      *quad_map;     /**< a map from (op, iv, rc) to node */
@@ -95,7 +96,7 @@ typedef struct iv_env {
  * An entry in the (op, node, node) -> node map.
  */
 typedef struct quadruple_t {
-       ir_opcode code;  /**< the opcode of the reduced operation */
+       unsigned   code; /**< the opcode of the reduced operation */
        ir_node   *op1;  /**< the first operand the reduced operation */
        ir_node   *op2;  /**< the second operand of the reduced operation */
 
@@ -108,7 +109,7 @@ typedef struct quadruple_t {
 typedef struct LFTR_edge {
        ir_node   *src;   /**< the source node */
        ir_node   *dst;   /**< the destination node */
-       ir_opcode code;   /**< the opcode that must be applied */
+       unsigned  code;   /**< the opcode that must be applied */
        ir_node   *rc;    /**< the region const that must be applied */
 } LFTR_edge;
 
@@ -118,9 +119,10 @@ static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env);
 /**
  * Compare two LFTR edges.
  */
-static int LFTR_cmp(const void *e1, const void *e2, size_t size) {
-       const LFTR_edge *l1 = e1;
-       const LFTR_edge *l2 = e2;
+static int LFTR_cmp(const void *e1, const void *e2, size_t size)
+{
+       const LFTR_edge *l1 = (const LFTR_edge*)e1;
+       const LFTR_edge *l2 = (const LFTR_edge*)e2;
        (void) size;
 
        return l1->src != l2->src;
@@ -131,12 +133,13 @@ static int LFTR_cmp(const void *e1, const void *e2, size_t size) {
  *
  * @param src  the source node of the transition
  */
-static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) {
+static LFTR_edge *LFTR_find(ir_node *src, iv_env *env)
+{
        LFTR_edge key;
 
        key.src  = src;
 
-       return set_find(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
+       return (LFTR_edge*)set_find(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
 }  /* LFTR_find */
 
 /**
@@ -148,7 +151,8 @@ static LFTR_edge *LFTR_find(ir_node *src, iv_env *env) {
  * @param rc    the region const used in the transition
  * @param env   the environment
  */
-static void LFTR_add(ir_node *src, ir_node *dst, ir_opcode code, ir_node *rc, iv_env *env) {
+static void LFTR_add(ir_node *src, ir_node *dst, unsigned code, ir_node *rc, iv_env *env)
+{
        LFTR_edge key;
 
        key.src  = src;
@@ -160,7 +164,6 @@ static void LFTR_add(ir_node *src, ir_node *dst, ir_opcode code, ir_node *rc, iv
         * There might be more than one edge here. This is rather bad
         * because we currently store only one.
         */
-//     assert(LFTR_find(src, env) == NULL);
        set_insert(env->lftr_edges, &key, sizeof(key), HASH_PTR(src));
 }  /* LFTR_add */
 
@@ -170,12 +173,12 @@ static void LFTR_add(ir_node *src, ir_node *dst, ir_opcode code, ir_node *rc, iv
  * @param irn  the node
  * @param env  the environment
  */
-static node_entry *get_irn_ne(ir_node *irn, iv_env *env) {
-       node_entry *e = get_irn_link(irn);
+static node_entry *get_irn_ne(ir_node *irn, iv_env *env)
+{
+       node_entry *e = (node_entry*)get_irn_link(irn);
 
        if (e == NULL) {
-               e = obstack_alloc(&env->obst, sizeof(*e));
-               memset(e, 0, sizeof(*e));
+               e = OALLOCZ(&env->obst, node_entry);
                set_irn_link(irn, e);
        }
        return e;
@@ -187,7 +190,8 @@ static node_entry *get_irn_ne(ir_node *irn, iv_env *env) {
  * @param iv   any node of the induction variable
  * @param env  the environment
  */
-static scc *get_iv_scc(ir_node *iv, iv_env *env) {
+static scc *get_iv_scc(ir_node *iv, iv_env *env)
+{
        node_entry *e = get_irn_ne(iv, env);
        return e->pscc;
 }  /* get_iv_scc */
@@ -200,7 +204,8 @@ static scc *get_iv_scc(ir_node *iv, iv_env *env) {
  *
  * @returns the header if it is one, NULL else
  */
-static ir_node *is_iv(ir_node *irn, iv_env *env) {
+static ir_node *is_iv(ir_node *irn, iv_env *env)
+{
        return get_irn_ne(irn, env)->header;
 }  /* is_iv */
 
@@ -211,7 +216,8 @@ static ir_node *is_iv(ir_node *irn, iv_env *env) {
  * @param irn           the node to check
  * @param header_block  the header block of the induction variable
  */
-static int is_rc(ir_node *irn, ir_node *header_block) {
+static int is_rc(ir_node *irn, ir_node *header_block)
+{
        ir_node *block = get_nodes_block(irn);
 
        return (block != header_block) && block_dominates(block, header_block);
@@ -220,9 +226,10 @@ static int is_rc(ir_node *irn, ir_node *header_block) {
 /**
  * Set compare function for the quad set.
  */
-static int quad_cmp(const void *e1, const void *e2, size_t size) {
-       const quadruple_t *c1 = e1;
-       const quadruple_t *c2 = e2;
+static int quad_cmp(const void *e1, const void *e2, size_t size)
+{
+       const quadruple_t *c1 = (const quadruple_t*)e1;
+       const quadruple_t *c2 = (const quadruple_t*)e2;
        (void) size;
 
        return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
@@ -238,15 +245,16 @@ static int quad_cmp(const void *e1, const void *e2, size_t size) {
  *
  * @return the already reduced node or NULL if this operation is not yet reduced
  */
-static ir_node *search(ir_opcode code, ir_node *op1, ir_node *op2, iv_env *env) {
+static ir_node *search(unsigned code, ir_node *op1, ir_node *op2, iv_env *env)
+{
        quadruple_t key, *entry;
 
        key.code = code;
        key.op1 = op1;
        key.op2 = op2;
 
-       entry = set_find(env->quad_map, &key, sizeof(key),
-                        (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
+       entry = (quadruple_t*)set_find(env->quad_map, &key, sizeof(key),
+                                      (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
        if (entry)
                return entry->res;
        return NULL;
@@ -261,7 +269,8 @@ static ir_node *search(ir_opcode code, ir_node *op1, ir_node *op2, iv_env *env)
  * @param result  the result of the reduced operation
  * @param env     the environment
  */
-static void add(ir_opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env) {
+static void add(unsigned code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env)
+{
        quadruple_t key;
 
        key.code = code;
@@ -284,7 +293,8 @@ static void add(ir_opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_
  * that either block1 dominates block2 or vice versa. So, just return
  * the "smaller" one.
  */
-static ir_node *find_location(ir_node *block1, ir_node *block2) {
+static ir_node *find_location(ir_node *block1, ir_node *block2)
+{
        if (block_dominates(block1, block2))
                return block2;
        assert(block_dominates(block2, block1));
@@ -302,24 +312,23 @@ static ir_node *find_location(ir_node *block1, ir_node *block2) {
  *
  * @return the newly created node
  */
-static ir_node *do_apply(ir_opcode code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
-       ir_graph *irg = current_ir_graph;
+static ir_node *do_apply(unsigned code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode)
+{
        ir_node *result;
        ir_node *block = find_location(get_nodes_block(op1), get_nodes_block(op2));
 
        switch (code) {
        case iro_Mul:
-               result = new_rd_Mul(db, irg, block, op1, op2, mode);
+               result = new_rd_Mul(db, block, op1, op2, mode);
                break;
        case iro_Add:
-               result = new_rd_Add(db, irg, block, op1, op2, mode);
+               result = new_rd_Add(db, block, op1, op2, mode);
                break;
        case iro_Sub:
-               result = new_rd_Sub(db, irg, block, op1, op2, mode);
+               result = new_rd_Sub(db, block, op1, op2, mode);
                break;
        default:
                panic("Unsupported opcode");
-               result = NULL;
        }
        return result;
 }  /* do_apply */
@@ -335,8 +344,9 @@ static ir_node *do_apply(ir_opcode code, dbg_info *db, ir_node *op1, ir_node *op
  *
  * @return the newly created node
  */
-static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env) {
-       ir_opcode code = get_irn_opcode(orig);
+static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1, ir_node *op2, iv_env *env)
+{
+       unsigned code = get_irn_opcode(orig);
        ir_node *result = search(code, op1, op2, env);
 
        if (result == NULL) {
@@ -369,8 +379,9 @@ static ir_node *apply(ir_node *header, ir_node *orig, ir_node *op1, ir_node *op2
  *
  * @return the reduced node
  */
-static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) {
-       ir_opcode code = get_irn_opcode(orig);
+static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env)
+{
+       unsigned code = get_irn_opcode(orig);
        ir_node *result = search(code, iv, rc, env);
 
        /* check if we have already done this operation on the iv */
@@ -424,7 +435,8 @@ static ir_node *reduce(ir_node *orig, ir_node *iv, ir_node *rc, iv_env *env) {
 /**
  * Update the scc for a newly created IV.
  */
-static void update_scc(ir_node *iv, node_entry *e, iv_env *env) {
+static void update_scc(ir_node *iv, node_entry *e, iv_env *env)
+{
        scc     *pscc   = e->pscc;
        ir_node *header = e->header;
        waitq    *wq = new_waitq();
@@ -433,7 +445,7 @@ static void update_scc(ir_node *iv, node_entry *e, iv_env *env) {
        pscc->head = NULL;
        waitq_put(wq, iv);
        do {
-               ir_node    *irn = waitq_get(wq);
+               ir_node    *irn = (ir_node*)waitq_get(wq);
                node_entry *ne  = get_irn_ne(irn, env);
                int        i;
 
@@ -467,7 +479,8 @@ static void update_scc(ir_node *iv, node_entry *e, iv_env *env) {
  * @param rc    the region constant
  * @param env   the environment
  */
-static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) {
+static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env)
+{
        ir_node *result;
 
        DB((dbg, LEVEL_2, "  Replacing %+F\n", irn));
@@ -476,12 +489,11 @@ static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) {
        if (result != irn) {
                node_entry *e;
 
-               hook_strength_red(current_ir_graph, irn);
+               hook_strength_red(get_irn_irg(irn), irn);
                exchange(irn, result);
                e = get_irn_ne(result, env);
                if (e->pscc == NULL) {
-                       e->pscc = obstack_alloc(&env->obst, sizeof(*e->pscc));
-                       memset(e->pscc, 0, sizeof(*e->pscc));
+                       e->pscc = OALLOCZ(&env->obst, scc);
                        update_scc(result, e, env);
                }
                ++env->replaced;
@@ -494,7 +506,8 @@ static int replace(ir_node *irn, ir_node *iv, ir_node *rc, iv_env *env) {
 /**
  * check if a given node is a mul with 2, 4, 8
  */
-static int is_x86_shift_const(ir_node *mul) {
+static int is_x86_shift_const(ir_node *mul)
+{
        ir_node *rc;
 
        if (! is_Mul(mul))
@@ -503,7 +516,7 @@ static int is_x86_shift_const(ir_node *mul) {
        /* normalization put constants on the right side */
        rc = get_Mul_right(mul);
        if (is_Const(rc)) {
-               tarval *tv = get_Const_tarval(rc);
+               ir_tarval *tv = get_Const_tarval(rc);
 
                if (tarval_is_long(tv)) {
                        long value = get_tarval_long(tv);
@@ -524,7 +537,8 @@ static int is_x86_shift_const(ir_node *mul) {
  * @param iv    any node of the induction variable
  * @param env   the environment
  */
-static int is_counter_iv(ir_node *iv, iv_env *env) {
+static int is_counter_iv(ir_node *iv, iv_env *env)
+{
        node_entry *e         = get_irn_ne(iv, env);
        scc        *pscc      = e->pscc;
        ir_node    *have_init = NULL;
@@ -592,7 +606,8 @@ static int is_counter_iv(ir_node *iv, iv_env *env) {
  * @return non-zero if the register pressure is estimated
  *         to not increase, zero else
  */
-static int check_users_for_reg_pressure(ir_node *iv, iv_env *env) {
+static int check_users_for_reg_pressure(ir_node *iv, iv_env *env)
+{
        ir_node    *irn, *header;
        ir_node    *have_user = NULL;
        ir_node    *have_cmp  = NULL;
@@ -644,7 +659,7 @@ static int check_users_for_reg_pressure(ir_node *iv, iv_env *env) {
         *
         * For now our capabilities for doing linear function test
         * are limited, so check if the iv has the right form: Only ONE
-        * phi, only one Add/Sub with a Const
+        * Phi, only one Add/Sub with a Const.
         */
        if (! is_counter_iv(iv, env))
                return 0;
@@ -664,10 +679,11 @@ static int check_users_for_reg_pressure(ir_node *iv, iv_env *env) {
  *
  * @return non-zero if irn should be Replace'd
  */
-static int check_replace(ir_node *irn, iv_env *env) {
+static int check_replace(ir_node *irn, iv_env *env)
+{
        ir_node   *left, *right, *iv, *rc;
        ir_op     *op  = get_irn_op(irn);
-       ir_opcode code = get_op_code(op);
+       unsigned  code = get_op_code(op);
        ir_node   *liv, *riv;
 
        switch (code) {
@@ -709,7 +725,8 @@ static int check_replace(ir_node *irn, iv_env *env) {
  * @param pscc  a SCC
  * @param env   the environment
  */
-static void classify_iv(scc *pscc, iv_env *env) {
+static void classify_iv(scc *pscc, iv_env *env)
+{
        ir_node *irn, *next, *header = NULL;
        node_entry *b, *h = NULL;
        int j, only_phi, num_outside;
@@ -717,7 +734,7 @@ static void classify_iv(scc *pscc, iv_env *env) {
 
        /* find the header block for this scc */
        for (irn = pscc->head; irn; irn = next) {
-               node_entry *e = get_irn_link(irn);
+               node_entry *e = (node_entry*)get_irn_link(irn);
                ir_node *block = get_nodes_block(irn);
 
                next = e->next;
@@ -814,9 +831,10 @@ fail:
  * @param pscc  the SCC
  * @param env   the environment
  */
-static void process_scc(scc *pscc, iv_env *env) {
+static void process_scc(scc *pscc, iv_env *env)
+{
        ir_node *head = pscc->head;
-       node_entry *e = get_irn_link(head);
+       node_entry *e = (node_entry*)get_irn_link(head);
 
 #ifdef DEBUG_libfirm
        {
@@ -824,7 +842,7 @@ static void process_scc(scc *pscc, iv_env *env) {
 
                DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
                for (irn = pscc->head; irn != NULL; irn = next) {
-                       node_entry *e = get_irn_link(irn);
+                       node_entry *e = (node_entry*)get_irn_link(irn);
 
                        next = e->next;
 
@@ -848,7 +866,8 @@ static void process_scc(scc *pscc, iv_env *env) {
  * @param pscc  an SCC that consists of Phi nodes only
  * @param env   the environment
  */
-static void remove_phi_cycle(scc *pscc, iv_env *env) {
+static void remove_phi_cycle(scc *pscc, iv_env *env)
+{
        ir_node *irn, *next;
        int j;
        ir_node *out_rc;
@@ -894,9 +913,10 @@ static void remove_phi_cycle(scc *pscc, iv_env *env) {
  * @param pscc  the SCC
  * @param env   the environment
  */
-static void process_phi_only_scc(scc *pscc, iv_env *env) {
+static void process_phi_only_scc(scc *pscc, iv_env *env)
+{
        ir_node *head = pscc->head;
-       node_entry *e = get_irn_link(head);
+       node_entry *e = (node_entry*)get_irn_link(head);
 
 #ifdef DEBUG_libfirm
        {
@@ -904,7 +924,7 @@ static void process_phi_only_scc(scc *pscc, iv_env *env) {
 
                DB((dbg, LEVEL_4, " SCC at %p:\n ", pscc));
                for (irn = pscc->head; irn; irn = next) {
-                       node_entry *e = get_irn_link(irn);
+                       node_entry *e = (node_entry*)get_irn_link(irn);
 
                        next = e->next;
 
@@ -925,11 +945,12 @@ static void process_phi_only_scc(scc *pscc, iv_env *env) {
  * @param env   the environment
  * @param n     the node to push
  */
-static void push(iv_env *env, ir_node *n) {
+static void push(iv_env *env, ir_node *n)
+{
        node_entry *e;
 
        if (env->tos == ARR_LEN(env->stack)) {
-               int nlen = ARR_LEN(env->stack) * 2;
+               size_t nlen = ARR_LEN(env->stack) * 2;
                ARR_RESIZE(ir_node *, env->stack, nlen);
        }
        env->stack[env->tos++] = n;
@@ -944,7 +965,8 @@ static void push(iv_env *env, ir_node *n) {
  *
  * @return  The topmost node
  */
-static ir_node *pop(iv_env *env) {
+static ir_node *pop(iv_env *env)
+{
        ir_node *n = env->stack[--env->tos];
        node_entry *e = get_irn_ne(n, env);
 
@@ -958,7 +980,8 @@ static ir_node *pop(iv_env *env) {
  * @param irn  start at this node
  * @param env  the environment
  */
-static void dfs(ir_node *irn, iv_env *env) {
+static void dfs(ir_node *irn, iv_env *env)
+{
        int i, n;
        node_entry *node = get_irn_ne(irn, env);
 
@@ -997,10 +1020,9 @@ static void dfs(ir_node *irn, iv_env *env) {
                                node->low = MIN(o->DFSnum, node->low);
                }
                if (node->low == node->DFSnum) {
-                       scc *pscc = obstack_alloc(&env->obst, sizeof(*pscc));
+                       scc *pscc = OALLOCZ(&env->obst, scc);
                        ir_node *x;
 
-                       memset(pscc, 0, sizeof(*pscc));
                        do {
                                node_entry *e;
 
@@ -1022,14 +1044,13 @@ static void dfs(ir_node *irn, iv_env *env) {
  * @param irg  the graph to process
  * @param env  the environment
  */
-static void do_dfs(ir_graph *irg, iv_env *env) {
-       ir_graph *rem = current_ir_graph;
+static void do_dfs(ir_graph *irg, iv_env *env)
+{
        ir_node  *end = get_irg_end(irg);
        int i;
 
        ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
 
-       current_ir_graph = irg;
        inc_irg_visited(irg);
 
        /* visit all visible nodes */
@@ -1044,40 +1065,19 @@ static void do_dfs(ir_graph *irg, iv_env *env) {
        }
 
        ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
-
-       current_ir_graph = rem;
 }  /* do_dfs */
 
 /**
  * Post-block-walker: assign the post-order number.
  */
-static void assign_po(ir_node *block, void *ctx) {
-       iv_env *env = ctx;
+static void assign_po(ir_node *block, void *ctx)
+{
+       iv_env *env = (iv_env*)ctx;
        node_entry *e = get_irn_ne(block, env);
 
        e->POnum = env->POnum++;
 }  /* assign_po */
 
-/**
- * Follows the LFTR edges and return the last node in the chain.
- *
- * @param irn  the node that should be followed
- * @param env  the IV environment
- *
- * @note
- * In the current implementation only the last edge is stored, so
- * only one chain exists. That's why we might miss some opportunities.
- */
-static ir_node *followEdges(ir_node *irn, iv_env *env) {
-       for (;;) {
-               LFTR_edge *e = LFTR_find(irn, env);
-               if (e)
-                       irn = e->dst;
-               else
-                       return irn;
-       }
-}  /* followEdges */
-
 /**
  * Apply one LFTR edge operation.
  * Return NULL if the transformation cannot be done safely without
@@ -1095,11 +1095,13 @@ static ir_node *followEdges(ir_node *irn, iv_env *env) {
  * In the current implementation only the last edge is stored, so
  * only one chain exists. That's why we might miss some opportunities.
  */
-static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env) {
+static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env)
+{
        if (env->osr_flags & osr_flag_lftr_with_ov_check) {
-               tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr;
+               ir_tarval *tv_l, *tv_r, *tv, *tv_init, *tv_incr, *tv_end;
                tarval_int_overflow_mode_t ovmode;
                scc *pscc;
+               ir_graph *irg;
 
                if (! is_counter_iv(iv, env)) {
                        DB((dbg, LEVEL_4, " not counter IV"));
@@ -1108,6 +1110,11 @@ static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env
 
                /* overflow can only be decided for Consts */
                if (! is_Const(e->rc)) {
+                       if (e->code == iro_Add && mode_is_reference(get_irn_mode(e->rc))) {
+                               /* However we allow ONE Pointer Add, as pointer arithmetic with wrap
+                                  around is undefined anyway */
+                               return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(e->rc));
+                       }
                        DB((dbg, LEVEL_4, " = UNKNOWN (%+F)", e->rc));
                        return NULL;
                }
@@ -1147,25 +1154,25 @@ static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env
                        break;
                default:
                        panic("Unsupported opcode");
-                       tv = tarval_bad;
                }
 
                if (pscc->code == iro_Add) {
-                       tv = tarval_add(tv, tv_incr);
+                       tv_end = tarval_add(tv, tv_incr);
                } else {
                        assert(pscc->code == iro_Sub);
-                       tv = tarval_sub(tv, tv_incr, NULL);
+                       tv_end = tarval_sub(tv, tv_incr, NULL);
                }
 
                tarval_set_integer_overflow_mode(ovmode);
 
-               if (tv == tarval_bad || tv_init == tarval_bad) {
+               if (tv == tarval_bad || tv_init == tarval_bad || tv_end == tarval_bad) {
                        DB((dbg, LEVEL_4, " = OVERFLOW"));
                        return NULL;
                }
-               return new_Const(tv);
+               irg = get_irn_irg(iv);
+               return new_r_Const(irg, tv);
        }
-       return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(rc));
+       return do_apply(e->code, NULL, rc, e->rc, get_irn_mode(e->dst));
 }  /* applyOneEdge */
 
 /**
@@ -1174,14 +1181,17 @@ static ir_node *applyOneEdge(ir_node *iv, ir_node *rc, LFTR_edge *e, iv_env *env
  * Return NULL if the transformation cannot be done safely without
  * an Overflow.
  *
- * @param iv   the IV node that starts the LFTR edge chain
+ * @param pIV  points to the IV node that starts the LFTR edge chain
+ *             after translation points to the new IV
  * @param rc   the region constant that should be translated
  * @param env  the IV environment
  *
  * @return the translated region constant or NULL
  *         if the translation was not possible
  */
-static ir_node *applyEdges(ir_node *iv, ir_node *rc, iv_env *env) {
+static ir_node *applyEdges(ir_node **pIV, ir_node *rc, iv_env *env)
+{
+       ir_node *iv = *pIV;
        if (env->osr_flags & osr_flag_lftr_with_ov_check) {
                /* overflow can only be decided for Consts */
                if (! is_counter_iv(iv, env)) {
@@ -1197,14 +1207,14 @@ static ir_node *applyEdges(ir_node *iv, ir_node *rc, iv_env *env) {
 
        for (; rc;) {
                LFTR_edge *e = LFTR_find(iv, env);
-               if (e) {
+               if (e != NULL) {
                        rc = applyOneEdge(iv, rc, e, env);
                        iv = e->dst;
-               }
-               else
+               } else
                        break;
        }
        DB((dbg, LEVEL_3, "\n"));
+       *pIV = iv;
        return rc;
 }  /* applyEdges */
 
@@ -1212,8 +1222,9 @@ static ir_node *applyEdges(ir_node *iv, ir_node *rc, iv_env *env) {
  * Walker, finds Cmp(iv, rc) or Cmp(rc, iv)
  * and tries to optimize them.
  */
-static void do_lftr(ir_node *cmp, void *ctx) {
-       iv_env *env = ctx;
+static void do_lftr(ir_node *cmp, void *ctx)
+{
+       iv_env *env = (iv_env*)ctx;
        ir_node *left, *right, *liv, *riv;
        ir_node *iv, *rc;
        ir_node *nleft = NULL, *nright = NULL;
@@ -1229,18 +1240,14 @@ static void do_lftr(ir_node *cmp, void *ctx) {
        if (liv && is_rc(right, liv)) {
                iv = left; rc = right;
 
-               nright = applyEdges(iv, rc, env);
-               if (nright && nright != rc) {
-                       nleft = followEdges(iv, env);
-               }
+               nright = applyEdges(&iv, rc, env);
+               nleft  = iv;
        }
        else if (riv && is_rc(left, riv)) {
                iv = right; rc = left;
 
-               nleft = applyEdges(iv, rc, env);
-               if (nleft && nleft != rc) {
-                       nright = followEdges(iv, env);
-               }
+               nleft  = applyEdges(&iv, rc, env);
+               nright = iv;
        }
 
        if (nleft && nright) {
@@ -1257,7 +1264,8 @@ static void do_lftr(ir_node *cmp, void *ctx) {
  * @param irg   the graph that should be optimized
  * @param env   the IV environment
  */
-static void lftr(ir_graph *irg, iv_env *env) {
+static void lftr(ir_graph *irg, iv_env *env)
+{
        irg_walk_graph(irg, NULL, do_lftr, env);
 }  /* lftr */
 
@@ -1265,8 +1273,9 @@ static void lftr(ir_graph *irg, iv_env *env) {
  * Pre-walker: set all node links to NULL and fix the
  * block of Proj nodes.
  */
-static void clear_and_fix(ir_node *irn, void *env) {
-       int *moved = env;
+static void clear_and_fix(ir_node *irn, void *env)
+{
+       int *moved = (int*)env;
        set_irn_link(irn, NULL);
 
        if (is_Proj(irn)) {
@@ -1282,13 +1291,10 @@ static void clear_and_fix(ir_node *irn, void *env) {
 
 
 /* Remove any Phi cycles with only one real input. */
-void remove_phi_cycles(ir_graph *irg) {
-       iv_env   env;
-       ir_graph *rem;
-       int      projs_moved;
-
-       rem = current_ir_graph;
-       current_ir_graph = irg;
+void remove_phi_cycles(ir_graph *irg)
+{
+       iv_env env;
+       int    projs_moved;
 
        FIRM_DBG_REGISTER(dbg, "firm.opt.remove_phi");
 
@@ -1308,8 +1314,8 @@ void remove_phi_cycles(ir_graph *irg) {
        env.process_scc   = process_phi_only_scc;
 
        /* Clear all links and move Proj nodes into the
-          the same block as it's predecessors.
-          This can improve the placement of new nodes.
+        * the same block as their predecessors.
+        * This can improve the placement of new nodes.
         */
        projs_moved = 0;
        irg_walk_graph(irg, NULL, clear_and_fix, &projs_moved);
@@ -1334,14 +1340,20 @@ void remove_phi_cycles(ir_graph *irg) {
 
        DEL_ARR_F(env.stack);
        obstack_free(&env.obst, NULL);
-
-       current_ir_graph = rem;
 }  /* remove_phi_cycles */
 
+ir_graph_pass_t *remove_phi_cycles_pass(const char *name)
+{
+       return def_graph_pass(name ? name : "remove_phi_cycles", remove_phi_cycles);
+}  /* remove_phi_cycles_pass */
+
 /**
  * Post-walker: fix Add and Sub nodes that where results of I<->P conversions.
  */
-static void fix_adds_and_subs(ir_node *irn, void *ctx) {
+static void fix_adds_and_subs(ir_node *irn, void *ctx)
+{
+       (void) ctx;
+
        if (is_Add(irn)) {
                ir_mode *mode = get_irn_mode(irn);
 
@@ -1352,14 +1364,14 @@ static void fix_adds_and_subs(ir_node *irn, void *ctx) {
                        if (get_irn_mode(pred) != mode) {
                                ir_node *block = get_nodes_block(pred);
 
-                               pred = new_r_Conv(current_ir_graph, block, pred, mode);
+                               pred = new_r_Conv(block, pred, mode);
                                set_Add_left(irn, pred);
                        }
                        pred = get_Add_right(irn);
                        if (get_irn_mode(pred) != mode) {
                                ir_node *block = get_nodes_block(pred);
 
-                               pred = new_r_Conv(current_ir_graph, block, pred, mode);
+                               pred = new_r_Conv(block, pred, mode);
                                set_Add_right(irn, pred);
                        }
                }
@@ -1376,38 +1388,41 @@ static void fix_adds_and_subs(ir_node *irn, void *ctx) {
                                if (l_mode != mode) {
                                        ir_node *block = get_nodes_block(left);
 
-                                       left = new_r_Conv(current_ir_graph, block, left, mode);
+                                       left = new_r_Conv(block, left, mode);
                                        set_Sub_left(irn, left);
                                }
                                if (r_mode != mode) {
                                        ir_node *block = get_nodes_block(right);
 
-                                       right = new_r_Conv(current_ir_graph, block, right, mode);
+                                       right = new_r_Conv(block, right, mode);
                                        set_Sub_right(irn, right);
                                }
                        }
+               } else if (mode_is_reference(mode)) {
+                       ir_node *left   = get_Sub_left(irn);
+                       ir_node *right  = get_Sub_right(irn);
+                       ir_mode *l_mode = get_irn_mode(left);
+                       ir_mode *r_mode = get_irn_mode(right);
+                       if (mode_is_int(l_mode)) {
+                               /* Usually, Sub(I*,P) is an error, hence the verifier rejects it.
+                                * However, it is correct in this case, so add Conv to make verifier happy. */
+                               ir_node *block = get_nodes_block(right);
+                               ir_node *lconv = new_r_Conv(block, left, r_mode);
+                               assert(mode_is_reference(r_mode));
+                               set_Sub_left(irn, lconv);
+                       }
                }
        }
 }  /* fix_adds_and_subs */
 
 /* Performs Operator Strength Reduction for the passed graph. */
-void opt_osr(ir_graph *irg, unsigned flags) {
+void opt_osr(ir_graph *irg, unsigned flags)
+{
        iv_env   env;
-       ir_graph *rem;
        int      edges;
        int      projs_moved;
 
-       if (! get_opt_strength_red()) {
-               /* only kill Phi cycles  */
-               remove_phi_cycles(irg);
-               return;
-       }
-
-       rem = current_ir_graph;
-       current_ir_graph = irg;
-
        FIRM_DBG_REGISTER(dbg, "firm.opt.osr");
-       firm_dbg_set_mask(dbg, SET_LEVEL_4);
 
        DB((dbg, LEVEL_1, "Doing Operator Strength Reduction for %+F\n", irg));
 
@@ -1425,8 +1440,8 @@ void opt_osr(ir_graph *irg, unsigned flags) {
        env.process_scc   = process_scc;
 
        /* Clear all links and move Proj nodes into the
-          the same block as it's predecessors.
-          This can improve the placement of new nodes.
+        * the same block as its predecessors.
+        * This can improve the placement of new nodes.
         */
        projs_moved = 0;
        irg_walk_graph(irg, NULL, clear_and_fix, &projs_moved);
@@ -1447,13 +1462,13 @@ void opt_osr(ir_graph *irg, unsigned flags) {
        do_dfs(irg, &env);
 
        if (env.replaced) {
-               /* try linear function test replacements */
-               //lftr(irg, &env); // currently buggy :-(
-               (void) lftr;
-
                if (env.need_postpass)
                        irg_walk_graph(irg, NULL, fix_adds_and_subs, &env);
 
+               /* try linear function test replacements */
+               lftr(irg, &env);
+               (void)lftr;
+
                set_irg_outs_inconsistent(irg);
                DB((dbg, LEVEL_1, "Replacements: %u + %u (lftr)\n\n", env.replaced, env.lftr_replaced));
        }
@@ -1466,6 +1481,28 @@ void opt_osr(ir_graph *irg, unsigned flags) {
 
        if (! edges)
                edges_deactivate(irg);
-
-       current_ir_graph = rem;
 }  /* opt_osr */
+
+typedef struct pass_t {
+       ir_graph_pass_t pass;
+       unsigned        flags;
+} pass_t;
+
+/**
+* Wrapper for running opt_osr() as an ir_graph pass.
+*/
+static int pass_wrapper(ir_graph *irg, void *context)
+{
+       pass_t *pass = (pass_t*)context;
+       opt_osr(irg, pass->flags);
+       return 0;
+}  /* pass_wrapper */
+
+ir_graph_pass_t *opt_osr_pass(const char *name, unsigned flags)
+{
+       pass_t *pass = XMALLOCZ(pass_t);
+
+       pass->flags = flags;
+       return def_graph_pass_constructor(
+               &pass->pass, name ? name : "osr", pass_wrapper);
+}  /* opt_osr_pass */