+/**
+ * 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.
+ */
+static ir_node *search(opcode code, ir_node *op1, ir_node *op2, iv_env *env) {
+ quad_t key, *entry;
+
+ key.code = code;
+ key.op1 = op2;
+ 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 was already calculated.
+ */
+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 = op2;
+ 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.
+ *
+ * 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 block1;
+ assert(block_dominates(block2, block1));
+ return block2;
+}
+
+/**
+ * create an op1 code op1 operation.
+ */
+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.
+ */
+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.
+ */
+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);
+ if (mode_is_reference(mode)) {
+ /* bad case: we replace a reference mode calculation.
+ assure that the new IV will be a reference one */
+ 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 {
+ switch (code) {
+ case iro_Mul:
+ o = apply(orig, o, rc, env);
+ break;
+ }
+ }
+ set_irn_n(result, i, o);
+ }
+ }
+ return result;
+}
+