+ * Check if an reduced operation was already calculated.
+ *
+ * @param code the opcode of the operation
+ * @param op1 the first operand of the operation
+ * @param op2 the second operand of the operation
+ * @param env the environment
+ *
+ * @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) {
+ 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));
+ if (entry)
+ return entry->res;
+ return NULL;
+} /* search */
+
+/**
+ * Add an reduced operation.
+ *
+ * @param code the opcode of the operation
+ * @param op1 the first operand of the operation
+ * @param op2 the second operand of the operation
+ * @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) {
+ quadruple_t key;
+
+ key.code = code;
+ key.op1 = op1;
+ key.op2 = op2;
+ key.res = result;
+
+ set_insert(env->quad_map, &key, sizeof(key),
+ (code * 9) ^ HASH_PTR(op1) ^HASH_PTR(op2));
+} /* add */
+
+/**
+ * Find a location where to place a bin-op whose operands are in
+ * block1 and block2.
+ *
+ * @param block1 the block of the first operand
+ * @param block2 the block of the second operand
+ *
+ * Note that we know here that such a place must exists. Moreover, this means
+ * 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) {
+ if (block_dominates(block1, block2))
+ return block2;
+ assert(block_dominates(block2, block1));
+ return block1;
+} /* find_location */
+
+/**
+ * Create a node that executes an op1 code op1 operation.
+ *
+ * @param code the opcode to execute
+ * @param db debug info to add to the new node
+ * @param op1 the first operand
+ * @param op2 the second operand
+ * @param mode the mode of the new operation
+ *
+ * @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_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, block, op1, op2, mode);
+ break;
+ case iro_Add:
+ result = new_rd_Add(db, block, op1, op2, mode);
+ break;
+ case iro_Sub:
+ result = new_rd_Sub(db, block, op1, op2, mode);
+ break;
+ default:
+ panic("Unsupported opcode");
+ result = NULL;
+ }
+ return result;
+} /* do_apply */
+
+/**
+ * The Apply operation.
+ *
+ * @param orig the node that represent the original operation and determines
+ * the opcode, debug-info and mode of a newly created one
+ * @param op1 the first operand
+ * @param op2 the second operand
+ * @param env the environment
+ *
+ * @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);
+ ir_node *result = search(code, op1, op2, env);
+
+ if (result == NULL) {
+ dbg_info *db = get_irn_dbg_info(orig);
+ ir_node *op1_header = get_irn_ne(op1, env)->header;
+ ir_node *op2_header = get_irn_ne(op2, env)->header;
+
+ if (op1_header == header && is_rc(op2, op1_header)) {
+ result = reduce(orig, op1, op2, env);
+ }
+ else if (op2_header == header && is_rc(op1, op2_header)) {
+ result = reduce(orig, op2, op1, env);
+ }
+ else {
+ result = do_apply(code, db, op1, op2, get_irn_mode(orig));
+ get_irn_ne(result, env)->header = NULL;
+ }
+ }
+ return result;
+} /* apply */
+
+/**
+ * The Reduce operation.
+ *
+ * @param orig the node that represent the original operation and determines
+ * the opcode, debug-info and mode of a newly created one
+ * @param iv the induction variable
+ * @param rc the region constant
+ * @param env the environment
+ *
+ * @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);
+ ir_node *result = search(code, iv, rc, env);
+
+ /* check if we have already done this operation on the iv */
+ if (result == NULL) {
+ node_entry *e, *iv_e;
+ int i;
+ ir_mode *mode = get_irn_mode(orig);
+
+ result = exact_copy(iv);
+
+ if (get_irn_mode(result) != mode) {
+ /*
+ * Beware: we must always create a new induction variable with the same mode
+ * as the node we are replacing. Especially this means the mode might be changed
+ * from P to I and back. This is always possible, because we have only Phi, Add
+ * and Sub nodes.
+ * However, this might lead to AddIs(Iu,Is) which we must fix. The best way to do this
+ * seems to be a post-pass, or we might end with useless Conv's.
+ */
+ set_irn_mode(result, mode);
+ env->need_postpass = 1;
+ }
+ add(code, iv, rc, result, env);
+ DB((dbg, LEVEL_3, " Created new %+F for %+F (%s %+F)\n", result, iv,
+ get_irn_opname(orig), rc));
+
+ iv_e = get_irn_ne(iv, env);
+ e = get_irn_ne(result, env);
+ e->header = iv_e->header;
+
+ /* create the LFTR edge */
+ LFTR_add(iv, result, code, rc, env);
+
+ for (i = get_irn_arity(result) - 1; i >= 0; --i) {
+ ir_node *o = get_irn_n(result, i);
+
+ e = get_irn_ne(o, env);
+ if (e->header == iv_e->header)
+ o = reduce(orig, o, rc, env);
+ else if (is_Phi(result) || code == iro_Mul)
+ o = apply(iv_e->header, orig, o, rc, env);
+ set_irn_n(result, i, o);
+ }
+ } else {
+ DB((dbg, LEVEL_3, " Already Created %+F for %+F (%s %+F)\n", result, iv,
+ get_irn_opname(orig), rc));
+ }
+ return result;
+} /* reduce */
+
+/**
+ * Update the scc for a newly created IV.
+ */
+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();
+
+ DB((dbg, LEVEL_2, " Creating SCC for new an induction variable:\n "));
+ pscc->head = NULL;
+ waitq_put(wq, iv);
+ do {
+ ir_node *irn = waitq_get(wq);
+ node_entry *ne = get_irn_ne(irn, env);
+ int i;
+
+ ne->pscc = pscc;
+ ne->next = pscc->head;
+ pscc->head = irn;
+ DB((dbg, LEVEL_2, " %+F,", irn));
+
+ for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
+ ir_node *pred = get_irn_n(irn, i);
+ node_entry *pe = get_irn_ne(pred, env);
+
+ if (pe->header == header && pe->pscc == NULL) {
+ /* set the pscc here to ensure that the node is NOT enqueued another time */
+ pe->pscc = pscc;
+ waitq_put(wq, pred);
+ }
+ }
+ } while (! waitq_empty(wq));
+ del_waitq(wq);
+ DB((dbg, LEVEL_2, "\n"));
+} /* update_scc */
+
+/**
+ * The Replace operation. We found a node representing iv (+,-,*) rc
+ * that can be removed by replacing the induction variable iv by a new
+ * one that 'applies' the operation 'irn'.