+ return (block != header_block) && block_dominates(block, header_block);
+}
+
+/**
+ * Set compare function for the quad set.
+ */
+static int quad_cmp(const void *e1, const void *e2, size_t size) {
+ const quad_t *c1 = e1;
+ const quad_t *c2 = e2;
+
+ return c1->code != c2->code || c1->op1 != c2->op1 || c1->op2 != c2->op2;
+}
+
+/**
+ * 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(opcode code, ir_node *op1, ir_node *op2, iv_env *env) {
+ quad_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;
+}
+
+/**
+ * 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(opcode code, ir_node *op1, ir_node *op2, ir_node *result, iv_env *env) {
+ quad_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));
+}
+
+/**
+ * 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;
+}
+
+/**
+ * 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(opcode code, dbg_info *db, ir_node *op1, ir_node *op2, ir_mode *mode) {
+ ir_graph *irg = current_ir_graph;
+ 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);
+ break;
+ case iro_Add:
+ result = new_rd_Add(db, irg, block, op1, op2, mode);
+ break;
+ case iro_Sub:
+ result = new_rd_Sub(db, irg, block, op1, op2, mode);
+ break;
+ default:
+ assert(0);
+ result = NULL;
+ }
+ return result;
+}
+
+/**
+ * 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 *orig, ir_node *op1, ir_node *op2, iv_env *env) {
+ opcode code = get_irn_opcode(orig);
+ ir_node *result = search(code, op1, op2, env);
+
+ if (! result) {
+ 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 != NULL && is_rc(op2, op1_header)) {
+ result = reduce(orig, op1, op2, env);
+ }
+ else if (op2_header != NULL && 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;
+}
+
+/**
+ * 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) {
+ opcode code = get_irn_opcode(orig);
+ ir_node *result = search(code, iv, rc, env);
+
+ if (! result) {
+ node_entry *e, *iv_e;
+ int i, n;
+ ir_mode *mode = get_irn_mode(orig);
+
+ result = exact_copy(iv);
+
+ /* Beware: we must always create a new nduction variable with the same mode
+ as the node we are replacing. Espicially 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. */
+ set_irn_mode(result, mode);
+ 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);
+
+ n = get_irn_arity(result);
+ for (i = 0; i < n; ++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))
+ o = apply(orig, o, rc, env);
+ else {
+ if (code == iro_Mul)
+ o = apply(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;