reassoc.c 55.1 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Matthias Braun's avatar
Matthias Braun committed
6
7
8
9
/**
 * @file
 * @brief   Reassociation
 * @author  Michael Beck
Michael Beck's avatar
Michael Beck committed
10
 */
11
#include "iroptimize.h"
12
#include "iropt_t.h"
13
14
15
16
17
#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "ircons_t.h"
#include "irgmod.h"
18
#include "irgopt.h"
19
20
21
#include "iropt_dbg.h"
#include "irflag_t.h"
#include "irgwalk.h"
22
#include "irouts.h"
23
#include "reassoc_t.h"
24
#include "opt_init.h"
25
26
#include "irhooks.h"
#include "irloop.h"
27
#include "pdeq.h"
28
#include "debug.h"
Victor Pfautz's avatar
Victor Pfautz committed
29
#include "panic.h"
30

31
32
33
#include "unionfind.h"
#include "plist.h"

34
DEBUG_ONLY(static firm_dbg_module_t *dbg;)
Michael Beck's avatar
Michael Beck committed
35
36

typedef enum {
37
38
39
    NO_CONSTANT,   /**< node is not constant */
    REAL_CONSTANT, /**< a Const that is suitable for constant folding */
    REGION_CONST   /**< a constant expression in the current context */
Michael Beck's avatar
Michael Beck committed
40
41
42
} const_class_t;

/**
43
 * returns whether a node is constant ie is a constant or
44
 * is loop invariant (called region constant)
45
46
47
 *
 * @param n     the node to be checked for constant
 * @param block a block that might be in a loop
Michael Beck's avatar
Michael Beck committed
48
 */
49
static const_class_t get_const_class(const ir_node *n, const ir_node *block)
Michael Beck's avatar
Michael Beck committed
50
{
51
	if (is_Const(n))
52
		return REAL_CONSTANT;
53

54
55
	/* constant nodes which can't be folded are region constants */
	if (is_irn_constlike(n))
56
		return REGION_CONST;
57

58
59
60
61
	/*
	 * Beware: Bad nodes are always loop-invariant, but
	 * cannot handled in later code, so filter them here.
	 */
Matthias Braun's avatar
Matthias Braun committed
62
	if (!is_Bad(n) && is_loop_invariant(n, block))
63
		return REGION_CONST;
Michael Beck's avatar
Michael Beck committed
64

65
	return NO_CONSTANT;
66
}
Michael Beck's avatar
Michael Beck committed
67
68
69

/**
 * returns the operands of a commutative bin-op, if one operand is
70
 * a region constant, it is returned as the second one.
Michael Beck's avatar
Michael Beck committed
71
 *
Michael Beck's avatar
Michael Beck committed
72
 * Beware: Real constants must be returned with higher priority than
73
 * region constants, because they might be folded.
Michael Beck's avatar
Michael Beck committed
74
75
76
 */
static void get_comm_Binop_ops(ir_node *binop, ir_node **a, ir_node **c)
{
Matthias Braun's avatar
Matthias Braun committed
77
	assert(is_op_commutative(get_irn_op(binop)));
yb9976's avatar
yb9976 committed
78
79
80
81
82
	ir_node       *op_a    = get_binop_left(binop);
	ir_node       *op_b    = get_binop_right(binop);
	ir_node       *block   = get_nodes_block(binop);
	const_class_t  class_a = get_const_class(op_a, block);
	const_class_t  class_b = get_const_class(op_b, block);
83

Matthias Braun's avatar
Matthias Braun committed
84
	if (class_a == REAL_CONSTANT && class_b == REAL_CONSTANT) {
85
		/* if both are constants, one might be a
Matthias Braun's avatar
Matthias Braun committed
86
		 * pointer constant like NULL, return the other */
87
88
89
90
91
92
93
		if (mode_is_reference(get_irn_mode(op_a))) {
			*a = op_a;
			*c = op_b;
		} else {
			*a = op_b;
			*c = op_a;
		}
yb9976's avatar
yb9976 committed
94
	} else if (class_a == REAL_CONSTANT
Matthias Braun's avatar
Matthias Braun committed
95
	           || (class_a == REGION_CONST && class_b == NO_CONSTANT)) {
96
97
		*a = op_b;
		*c = op_a;
Matthias Braun's avatar
Matthias Braun committed
98
	} else {
99
100
101
		*a = op_a;
		*c = op_b;
	}
102
}
Michael Beck's avatar
Michael Beck committed
103

Matthias Braun's avatar
Matthias Braun committed
104
105
/**
 * Retrieve a mode from the operands. We need this, because
Michael Beck's avatar
Michael Beck committed
106
107
108
109
 * Add and Sub are allowed to operate on (P, Is)
 */
static ir_mode *get_mode_from_ops(ir_node *op1, ir_node *op2)
{
Matthias Braun's avatar
Matthias Braun committed
110
	ir_mode *m1 = get_irn_mode(op1);
111
112
	if (mode_is_reference(m1))
		return m1;
Michael Beck's avatar
Michael Beck committed
113

Matthias Braun's avatar
Matthias Braun committed
114
	ir_mode *m2 = get_irn_mode(op2);
115
116
	if (mode_is_reference(m2))
		return m2;
Michael Beck's avatar
Michael Beck committed
117

118
119
	assert(m1 == m2);
	return m1;
120
}
Michael Beck's avatar
Michael Beck committed
121

Matthias Braun's avatar
Matthias Braun committed
122
123
124
125
126
127
128
129
130
static ir_node *create_node(dbg_info *dbgi, ir_node *block, ir_op *op,
                            ir_mode *mode, int n_in, ir_node **in)
{
	ir_graph *irg = get_irn_irg(block);
	ir_node  *new = new_ir_node(dbgi, irg, block, op, mode, n_in, in);
	verify_new_node(irg, new);
	return optimize_node(new);
}

Michael Beck's avatar
Michael Beck committed
131
132
133
134
/**
 * reassociate a commutative Binop
 *
 * BEWARE: this rule leads to a potential loop, if
135
 * two operands are region constants and the third is a
Michael Beck's avatar
Michael Beck committed
136
137
 * constant, so avoid this situation.
 */
138
static int reassoc_commutative(ir_node **node)
Michael Beck's avatar
Michael Beck committed
139
{
140
	ir_node *n     = *node;
141
	ir_op   *op    = get_irn_op(n);
142
	ir_node *block = get_nodes_block(n);
Matthias Braun's avatar
Matthias Braun committed
143
144
	ir_node *t1;
	ir_node *c1;
145
146
147
148

	get_comm_Binop_ops(n, &t1, &c1);

	if (get_irn_op(t1) == op) {
Matthias Braun's avatar
Matthias Braun committed
149
150
		ir_node *t2;
		ir_node *c2;
151
		get_comm_Binop_ops(t1, &t2, &c2);
Matthias Braun's avatar
Matthias Braun committed
152
153
154
		const_class_t c_c1 = get_const_class(c1, block);
		const_class_t c_c2 = get_const_class(c2, block);
		const_class_t c_t2 = get_const_class(t2, block);
yb9976's avatar
yb9976 committed
155
156
157
158
159
160
161
162
163
164
165
		if (c_c1 != NO_CONSTANT) {
			if (((c_c1 ^ c_c2 ^ c_t2) & REGION_CONST) == 0
			    || (c_c1 & c_c2 & c_t2) == REGION_CONST) {
				/* All three are constant and either all are constant expressions
				 * or two of them are:
				 * then applying this rule would lead into a cycle
				 *
				 * Note that if t2 is a constant so is c2 hence we save one test.
				 */
				return 0;
			}
166
167

			/* handles rules R7, R8, R9, R10:
168
			 * convert c1 .OP. (c2 .OP. x) => x .OP. (c1 .OP. c2)
169
			 */
yb9976's avatar
yb9976 committed
170
171
			ir_mode *mode_c1 = get_irn_mode(c1);
			ir_mode *mode_c2 = get_irn_mode(c2);
172

173
174
			/* It might happen, that c1 and c2 have different modes, for
			 * instance Is and Iu.
175
176
			 * Handle this here.
			 */
yb9976's avatar
yb9976 committed
177
178
179
180
181
182
183
184
185
186
187
			if (mode_c1 != mode_c2 && mode_is_int(mode_c1) && mode_is_int(mode_c2)) {
				unsigned bits_c1 = get_mode_size_bits(mode_c1);
				unsigned bits_c2 = get_mode_size_bits(mode_c2);
				/* get the bigger one */
				if (bits_c1 > bits_c2) {
					c2 = new_r_Conv(block, c2, mode_c1);
				} else if (bits_c1 < bits_c2) {
					c1 = new_r_Conv(block, c1, mode_c2);
				} else {
					/* Try to cast the real const */
					if (c_c1 == REAL_CONSTANT)
188
						c1 = new_r_Conv(block, c1, mode_c2);
yb9976's avatar
yb9976 committed
189
190
					else
						c2 = new_r_Conv(block, c2, mode_c1);
191
192
193
				}
			}

yb9976's avatar
yb9976 committed
194
195
196
197
			dbg_info *dbgi  = get_irn_dbg_info(n);
			ir_node  *in0[] = { c1, c2};
			ir_mode  *mode0 = get_mode_from_ops(c1, c2);
			ir_node  *irn0  = create_node(dbgi, block, op, mode0, ARRAY_SIZE(in0), in0);
198

Matthias Braun's avatar
Matthias Braun committed
199
200
			ir_node *in1[] = { t2, irn0 };
			ir_mode *mode1 = get_mode_from_ops(t2, irn0);
yb9976's avatar
yb9976 committed
201
			ir_node *irn1  = create_node(dbgi, block, op, mode1, ARRAY_SIZE(in1), in1);
202

203
204
205
			DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
			     c1, get_irn_opname(n), c2, get_irn_opname(n), t2,
			     t2, get_irn_opname(n), c1, get_irn_opname(n), c2));
yb9976's avatar
yb9976 committed
206
207
208
209

			exchange(n, irn1);
			*node = irn1;
			return 1;
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
		}
	}
	if (get_irn_op(c1) == op) {
		ir_node *t = c1;
		c1 = t1;
		t1 = t;
	}
	if (get_irn_op(t1) == op) {
		ir_node *l = get_binop_left(t1);
		ir_node *r = get_binop_right(t1);
		if (r == c1) {
			ir_node *t = r;
			r = l;
			l = t;
		}
Matthias Braun's avatar
Matthias Braun committed
225
226

		const_class_t c_r = get_const_class(r, block);
227
228
229
230
231
232
233
234
		if (c_r != NO_CONSTANT) {
			/*
			 * Beware: don't do the following op if a constant was
			 * placed below, else we will fall into a loop.
			 */
			return 0;
		}

235
		if (l == c1 && r != c1) {
236
			/* convert x .OP. (x .OP. y) => y .OP. (x .OP. x) */
yb9976's avatar
yb9976 committed
237
238
239
240
241
			dbg_info *dbgi     = get_irn_dbg_info(n);
			ir_mode  *mode_res = get_irn_mode(n);
			ir_mode  *mode_c1  = get_irn_mode(c1);
			ir_node  *in[]     = { c1, c1 };
			ir_node  *irn0     = create_node(dbgi, block, op, mode_c1,
242
			                                 ARRAY_SIZE(in), in);
243

Matthias Braun's avatar
Matthias Braun committed
244
			ir_node *in1[] = { r, irn0 };
yb9976's avatar
yb9976 committed
245
			ir_node *irn1  = create_node(dbgi, block, op, mode_res,
Matthias Braun's avatar
Matthias Braun committed
246
			                             ARRAY_SIZE(in1), in1);
247
248

			DBG((dbg, LEVEL_5, "Applied: %n .%s. (%n .%s. %n) => %n .%s. (%n .%s. %n)\n",
249
250
			     c1, get_irn_opname(n), l, get_irn_opname(n), r,
			     r, get_irn_opname(n), c1, get_irn_opname(n), c1));
251

yb9976's avatar
yb9976 committed
252
253
254
			exchange(n, irn1);
			*node = irn1;
			return 1;
255
256
257
		}
	}
	return 0;
258
}
Michael Beck's avatar
Michael Beck committed
259
260

/**
261
 * The walker for the reassociation.
Michael Beck's avatar
Michael Beck committed
262
 */
263
static void wq_walker(ir_node *n, void *env)
Michael Beck's avatar
Michael Beck committed
264
{
265
	waitq *const wq = (waitq *)env;
Michael Beck's avatar
Michael Beck committed
266

267
	set_irn_link(n, NULL);
268
	if (!is_Block(n)) {
269
270
		waitq_put(wq, n);
		set_irn_link(n, wq);
271
	}
272
}
273
274
275
276

/**
 * The walker for the reassociation.
 */
277
static void do_reassociation(waitq *const wq)
278
{
Matthias Braun's avatar
Matthias Braun committed
279
	while (!waitq_empty(wq)) {
280
		ir_node *n = (ir_node *)waitq_get(wq);
281
282
		set_irn_link(n, NULL);

283
		hook_reassociate(1);
Michael Beck's avatar
Michael Beck committed
284

285
		/* reassociation must run until a fixpoint is reached. */
Matthias Braun's avatar
Matthias Braun committed
286
287
		bool changed = false;
		bool res;
288
		do {
Matthias Braun's avatar
Matthias Braun committed
289
			res = false;
yb9976's avatar
yb9976 committed
290
291
			ir_op   *op   = get_irn_op(n);
			ir_mode *mode = get_irn_mode(n);
292

293
294
			/* reassociating floatingpoint ops is imprecise */
			if (mode_is_float(mode) && !ir_imprecise_float_transforms_allowed())
295
				break;
Michael Beck's avatar
Michael Beck committed
296

297
298
299
			if (op->ops.reassociate) {
				res = op->ops.reassociate(&n);

300
				changed |= res;
301
			}
Matthias Braun's avatar
Matthias Braun committed
302
		} while (res);
303
		hook_reassociate(0);
304
305

		if (changed) {
306
			foreach_irn_in_r(n, i, pred) {
307
308
309
				if (get_irn_link(pred) != wq) {
					waitq_put(wq, pred);
					set_irn_link(pred, wq);
310
311
312
				}
			}
		}
313
	}
314
}
Michael Beck's avatar
Michael Beck committed
315

316
317
/**
 * Returns the earliest were a,b are available.
Christoph Mallon's avatar
Christoph Mallon committed
318
 * Note that we know that a, b both dominate
319
 * the block of the previous operation, so one must dominate the other.
320
321
 *
 * If the earliest block is the start block, return curr_blk instead
322
 */
323
324
static ir_node *earliest_block(ir_node *a, ir_node *b, ir_node *curr_blk)
{
Matthias Braun's avatar
Matthias Braun committed
325
	/* if blk_a != blk_b, one must dominate the other */
326
327
	ir_node *blk_a = get_nodes_block(a);
	ir_node *blk_b = get_nodes_block(b);
Matthias Braun's avatar
Matthias Braun committed
328
	ir_node *res   = block_dominates(blk_a, blk_b) ? blk_b : blk_a;
329
	if (res == get_irg_start_block(get_irn_irg(curr_blk)))
330
331
		return curr_blk;
	return res;
332
}
333

334
335
336
static bool is_const(ir_node *const node)
{
	switch (get_irn_opcode(node)) {
337
	case iro_Address:
338
	case iro_Align:
339
	case iro_Const:
340
	case iro_Offset:
341
	case iro_Size:
342
343
344
345
346
347
		return true;
	default:
		return false;
	}
}

348
/**
349
350
351
 * Checks whether a node is a Constant expression.
 * The following trees are constant expressions:
 *
352
 * Address, Const, Offset, TypeConst, Const + Address/Offset/TypeConst
353
 *
354
 * Handling Address/Offset/TypeConsts as const might be not a good idea for all
355
 * architectures ...
356
 */
357
358
static int is_constant_expr(ir_node *irn)
{
359
	switch (get_irn_opcode(irn)) {
360
	case iro_Address:
361
	case iro_Align:
362
	case iro_Const:
363
	case iro_Offset:
364
	case iro_Size:
365
		return 1;
366

367
368
	case iro_Add:
		return is_const(get_Add_left(irn)) && is_const(get_Add_right(irn));
369

370
	default:
371
		return 0;
372
	}
373
}
374

375
376
377
/**
 * Apply distributive Law for Mul and Add/Sub
 */
378
379
static int reverse_rule_distributive(ir_node **node)
{
Matthias Braun's avatar
Matthias Braun committed
380
	ir_node *n     = *node;
381
382
	ir_node *left  = get_binop_left(n);
	ir_node *right = get_binop_right(n);
Matthias Braun's avatar
Matthias Braun committed
383
	ir_op   *op    = get_irn_op(left);
384
	if (op != get_irn_op(right))
385
386
		return 0;

Matthias Braun's avatar
Matthias Braun committed
387
388
389
	ir_node *x;
	ir_node *a;
	ir_node *b;
390
391
392
393
	if (op == op_Shl) {
		x = get_Shl_right(left);

		if (x == get_Shl_right(right)) {
Michael Beck's avatar
Michael Beck committed
394
			/* (a << x) +/- (b << x) ==> (a +/- b) << x */
395
396
397
398
399
400
401
402
			a = get_Shl_left(left);
			b = get_Shl_left(right);
			goto transform;
		}
	} else if (op == op_Mul) {
		x = get_Mul_left(left);

		if (x == get_Mul_left(right)) {
Michael Beck's avatar
Michael Beck committed
403
			/* (x * a) +/- (x * b) ==> (a +/- b) * x */
404
405
406
407
			a = get_Mul_right(left);
			b = get_Mul_right(right);
			goto transform;
		} else if (x == get_Mul_right(right)) {
Michael Beck's avatar
Michael Beck committed
408
			/* (x * a) +/- (b * x) ==> (a +/- b) * x */
409
410
411
412
			a = get_Mul_right(left);
			b = get_Mul_left(right);
			goto transform;
		}
413

414
415
416
		x = get_Mul_right(left);

		if (x == get_Mul_right(right)) {
Michael Beck's avatar
Michael Beck committed
417
			/* (a * x) +/- (b * x) ==> (a +/- b) * x */
418
419
420
421
			a = get_Mul_left(left);
			b = get_Mul_left(right);
			goto transform;
		} else if (x == get_Mul_left(right)) {
Michael Beck's avatar
Michael Beck committed
422
			/* (a * x) +/- (x * b) ==> (a +/- b) * x */
423
424
425
426
			a = get_Mul_left(left);
			b = get_Mul_right(right);
			goto transform;
		}
427
428
429
	}
	return 0;

Matthias Braun's avatar
Matthias Braun committed
430
431
432
433
434
transform:;
	ir_node  *curr_blk = get_nodes_block(n);
	ir_node  *blk      = earliest_block(a, b, curr_blk);
	dbg_info *dbg      = get_irn_dbg_info(n);
	ir_mode  *mode     = get_irn_mode(n);
435

Matthias Braun's avatar
Matthias Braun committed
436
	ir_node *irn;
437
	if (is_Add(n))
438
		irn = new_rd_Add(dbg, blk, a, b, mode);
439
	else
440
		irn = new_rd_Sub(dbg, blk, a, b, mode);
441

yb9976's avatar
yb9976 committed
442
	blk = earliest_block(irn, x, curr_blk);
443
	if (op == op_Mul)
444
		irn = new_rd_Mul(dbg, blk, irn, x, mode);
445
	else
446
		irn = new_rd_Shl(dbg, blk, irn, x, mode);
447
448
449
450

	exchange(n, irn);
	*node = irn;
	return 1;
451
}
452

453
454
455
/**
 * Move Constants towards the root.
 */
456
457
static int move_consts_up(ir_node **node)
{
458
	ir_node *n = *node;
Matthias Braun's avatar
Matthias Braun committed
459
460
	ir_node *l = get_binop_left(n);
	ir_node *r = get_binop_right(n);
461
462
463
464
465

	/* check if one is already a constant expression */
	if (is_constant_expr(l) || is_constant_expr(r))
		return 0;

yb9976's avatar
yb9976 committed
466
467
468
469
470
	ir_op   *op  = get_irn_op(n);
	ir_node *blk;
	ir_node *a;
	ir_node *b;
	ir_node *c;
471
	if (get_irn_op(l) == op) {
Michael Beck's avatar
Michael Beck committed
472
		/* (a .op. b) .op. r */
473
474
475
476
		a = get_binop_left(l);
		b = get_binop_right(l);

		if (is_constant_expr(a)) {
Michael Beck's avatar
Michael Beck committed
477
			/* (C .op. b) .op. r ==> (r .op. b) .op. C */
yb9976's avatar
yb9976 committed
478
479
			c   = a;
			a   = r;
480
481
482
			blk = get_nodes_block(l);
			goto transform;
		} else if (is_constant_expr(b)) {
Michael Beck's avatar
Michael Beck committed
483
			/* (a .op. C) .op. r ==> (a .op. r) .op. C */
yb9976's avatar
yb9976 committed
484
485
			c   = b;
			b   = r;
486
487
488
			blk = get_nodes_block(l);
			goto transform;
		}
489
490
	}
	if (get_irn_op(r) == op) {
Michael Beck's avatar
Michael Beck committed
491
		/* l .op. (a .op. b) */
492
493
494
495
		a = get_binop_left(r);
		b = get_binop_right(r);

		if (is_constant_expr(a)) {
Michael Beck's avatar
Michael Beck committed
496
			/* l .op. (C .op. b) ==> (l .op. b) .op. C */
yb9976's avatar
yb9976 committed
497
498
			c   = a;
			a   = l;
499
500
501
			blk = get_nodes_block(r);
			goto transform;
		} else if (is_constant_expr(b)) {
Michael Beck's avatar
Michael Beck committed
502
			/* l .op. (a .op. C) ==> (a .op. l) .op. C */
yb9976's avatar
yb9976 committed
503
504
			c   = b;
			b   = l;
505
506
507
508
509
510
			blk = get_nodes_block(r);
			goto transform;
		}
	}
	return 0;

Matthias Braun's avatar
Matthias Braun committed
511
transform:;
512
	/* In some cases a and b might be both of different integer mode, and c a
513
	 * Address/Offset/TypeConst.
514
515
516
517
518
	 * in that case we could either
	 * 1.) cast into unsigned mode
	 * 2.) ignore
	 * we implement the second here
	 */
Matthias Braun's avatar
Matthias Braun committed
519
520
	ir_mode *ma = get_irn_mode(a);
	ir_mode *mb = get_irn_mode(b);
521
522
523
	if (ma != mb && mode_is_int(ma) && mode_is_int(mb))
		return 0;

Matthias Braun's avatar
Matthias Braun committed
524
525
	/* check if (a .op. b) can be calculated in the same block is the old
	 * instruction */
526
527
528
529
530
	if (! block_dominates(get_nodes_block(a), blk))
		return 0;
	if (! block_dominates(get_nodes_block(b), blk))
		return 0;
	/* ok */
yb9976's avatar
yb9976 committed
531
532
533
534
	dbg_info *dbgi  = get_irn_dbg_info(n);
	ir_mode  *mode  = get_mode_from_ops(a, b);
	ir_node  *in0[] = { a, b };
	ir_node  *irn   = create_node(dbgi, blk, op, mode, ARRAY_SIZE(in0), in0);
535

536
537
	/* beware: optimize_node might have changed the opcode, check again */
	if (is_Add(irn) || is_Sub(irn)) {
Matthias Braun's avatar
Matthias Braun committed
538
		reverse_rule_distributive(&irn);
539
540
	}

Matthias Braun's avatar
Matthias Braun committed
541
542
	ir_node *in1[] = { irn, c };
	ir_mode *mode1 = get_mode_from_ops(irn, c);
yb9976's avatar
yb9976 committed
543
	ir_node *irn1  = create_node(dbgi, blk, op, mode1, ARRAY_SIZE(in1), in1);
544

Matthias Braun's avatar
Matthias Braun committed
545
546
	exchange(n, irn1);
	*node = irn1;
547
	return 1;
548
}
549

550
551
552
/**
 * Apply the rules in reverse order, removing code that was not collapsed
 */
553
554
static void reverse_rules(ir_node *node, void *env)
{
555
	(void)env;
556
	/* reassociating floatingpoint ops is imprecise */
Matthias Braun's avatar
Matthias Braun committed
557
	ir_mode *mode = get_irn_mode(node);
558
	if (mode_is_float(mode) && !ir_imprecise_float_transforms_allowed())
559
560
		return;

Matthias Braun's avatar
Matthias Braun committed
561
	bool res;
562
	do {
Matthias Braun's avatar
Matthias Braun committed
563
		res = false;
564

Matthias Braun's avatar
Matthias Braun committed
565
		ir_op *op = get_irn_op(node);
566
		if (is_op_commutative(op)) {
567
			res = move_consts_up(&node);
568
		}
Michael Beck's avatar
Michael Beck committed
569
570
		/* beware: move_consts_up might have changed the opcode, check again */
		if (is_Add(node) || is_Sub(node)) {
571
			res = reverse_rule_distributive(&node);
572
573
574
575
		}
	} while (res);
}

576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
/**
 * Returns true iff node is a bitwise function.
 */
static bool is_bitop(ir_node *node)
{
	return is_And(node) || is_Eor(node) || is_Or(node) || is_Not(node);
}

typedef struct {
	ir_graph *irg;
	plist_t  *optimizations;

	pmap        *walk_counter;
	unsigned int walk_base;
	unsigned int walk_max;
} shannon_data;


typedef struct {
	ir_node *base_node;
	ir_node *middle_node;
	ir_node *top_node;
	ir_node *other_node;
} optimization_t;

/**
 * Try to find middle_node or top_node, from base_node over a non-direct path.
 *
 *              top_node
 *              ^      ^
 *              |      |
 *          +---+      +------+
 *          |                 |
 *     other_node       middle_node (optional)
 *          ^                 ^
 *          |                 |
 *          |                 |
 *          .                 |
 *          .                 |
 *          .                 |
 *          |                 |
 *          |                 |
 *          +-------+   +-----+
 *                  |   |
 *               base_node
 *
 * @param current      Current node of the search
 * @param other_node   Previous visited node of the search
 * @param base_node    Common root node
 * @param middle_node  Not node,  Eor node with constant operand, or NULL
 * @param top_node     Non-constant Operand of middle_node
 * @param shdata       Shannon data
 */
static void find_path_to_top_node(ir_node *current, ir_node *other_node, ir_node *base_node, ir_node *middle_node, ir_node *top_node, shannon_data *shdata)
{
	ir_node *top_node2;
	ir_node *middle_node2;
	if (current == middle_node) {
		top_node2    = middle_node;
		middle_node2 = NULL;
	} else {
		top_node2    = top_node;
		middle_node2 = middle_node;
	}

	if (current == top_node2 && ((middle_node && get_irn_n_outs(middle_node) > 1) || base_node != other_node)) {
		optimization_t *optimization = XMALLOC(optimization_t);
		optimization->base_node      = base_node;
		optimization->middle_node    = middle_node2;
		optimization->top_node       = top_node2;
		optimization->other_node     = other_node;

		plist_insert_back(shdata->optimizations, optimization);

		return;
	}

	uintptr_t counter = (uintptr_t)pmap_get(void, shdata->walk_counter, current);
	if (counter < shdata->walk_base) {
		counter = shdata->walk_base;
	}
	counter++;
	if (counter > shdata->walk_max) {
		shdata->walk_max = counter;
	}
	pmap_insert(shdata->walk_counter, current, (void *)counter);

	if ((counter - shdata->walk_base) == get_irn_n_outs(current) && is_bitop(current)) {
		foreach_irn_in(current, i, n) {
			find_path_to_top_node(n, current, base_node, middle_node, top_node, shdata);
		}
	}
}

/**
 * If given node is a middle_node, return the top_node. Else return the node itself.
 */
static ir_node *get_topnode_from_middlenode(ir_node *node)
{
	if (is_Not(node))
		return get_Not_op(node);

	if (is_Eor(node)) {
		assert(!is_Const(get_Eor_left(node)));

		ir_node *r = get_Eor_right(node);
		if (is_Const(r)) {
			return get_Eor_left(node);
		}
	}

	return node;
}

/**
 * Walker function that tries to find a top_node to given base_node.
 */
static void try_basenode(ir_node *base_node, void *env)
{
	if (!is_And(base_node) && !is_Or(base_node)) {
		return;
	}

	shannon_data *shdata = (shannon_data *)env;
	ir_node      *l      = get_binop_left(base_node);
	ir_node      *r      = get_binop_right(base_node);

	for (int i = 0; i < 2; i++) {
		ir_node *top_node    = get_topnode_from_middlenode(l);
		ir_node *middle_node = NULL;
		if (top_node != l) {
			middle_node = l;
		}

		shdata->walk_base = shdata->walk_max;
		find_path_to_top_node(r, base_node, base_node, middle_node, top_node, shdata);

		ir_node *t = l;
		l = r;
		r = t;
	}
}

/**
 * Replace top_node from given other_node by constant. base_node could be And or Or and is used to
 * decide if the constant will be (replacement Eor -1) or (replacement Eor 0).
 */
static void replace_node(ir_node *top_node, ir_node *base_node, ir_node *other_node, ir_tarval *replacement)
{
	assert(is_And(base_node) || is_Or(base_node));

	/* find index of top_node from other_node */
	int pos = -1;
	foreach_irn_in(other_node, i, n) {
		if (n == top_node) {
			pos = i;
			break;
		}
	}
	assert(pos >= 0);

	ir_mode   *other_mode = get_irn_mode(other_node);
	ir_tarval *base_val   = is_And(base_node) ? get_mode_all_one(other_mode) : get_mode_null(other_mode);
	dbg_info  *dbgi       = get_irn_dbg_info(other_node);
	ir_graph  *irg        = get_irn_irg(top_node);
	ir_tarval *tv         = tarval_eor(base_val, replacement);
	ir_node   *c          = new_rd_Const(dbgi, irg, tv);
	set_irn_n(other_node, pos, c);
}

/**
 * Returns the tarval of the const operator of the node.
 */
static ir_tarval *get_Eor_tarval(ir_node *node)
{
	assert(is_Eor(node));
	ir_node *l = get_Eor_left(node);
	ir_node *r = get_Eor_right(node);

	if (is_Const(l))
		return get_Const_tarval(l);

	assert(is_Const(r));
	return get_Const_tarval(r);
}

/**
 * Returns true iff operand is a operand of node.
 */
static bool has_operand(ir_node *node, ir_node *operand)
{
	foreach_irn_in(node, i, n) {
		if (n == operand) {
			return true;
		}
	}
	return false;
}

/**
 * Applies Shannon to given irg.
 */
static void do_shannon(ir_graph *irg)
{
	plist_t     *optimizations = plist_new();
	pmap        *walk_counter  = pmap_create();
	shannon_data shdata        = {.irg = irg, .optimizations = optimizations, .walk_counter = walk_counter, .walk_base = 0, .walk_max = 0};

	/* walk and get optimization data */
	irg_walk_edges(get_irg_start_block(irg), NULL, try_basenode, &shdata);

	/* optimize */
	DBG((dbg, LEVEL_4, "optimizations:\n"));
	foreach_plist(optimizations, el) {
		optimization_t *optimization = el->data;
		ir_node        *middle_node  = optimization->middle_node;
		ir_node        *top_node     = optimization->top_node;
		ir_node        *base_node    = optimization->base_node;
		ir_node        *other_node   = optimization->other_node;

		DBG((dbg, LEVEL_4, "base_node: %li, middle_node: %li, top_node: %li, other_node: %li\n",
		     get_irn_node_nr(base_node),
		     middle_node ? get_irn_node_nr(middle_node) : 0,
		     get_irn_node_nr(top_node),
		     get_irn_node_nr(other_node)));

		/* check if optimization is still valid */
		if (middle_node) {
			if (!has_operand(middle_node, top_node) || !has_operand(base_node, middle_node)) {
				continue;
			}
		} else if (!has_operand(base_node, top_node)) {
			continue;
		}

		if (!has_operand(other_node, top_node)) {
			continue;
		}

		/* calculate replacement */
		ir_mode   *mode        = get_irn_mode(top_node);
		ir_tarval *replacement;
		if (!middle_node) {
			replacement = get_mode_null(mode);
		} else if (is_Not(middle_node)) {
			replacement = get_mode_all_one(mode);
		} else {
			assert(is_Eor(middle_node));
			replacement = get_Eor_tarval(middle_node);
		}

		/* replace */
		replace_node(top_node, base_node, other_node, replacement);
		DBG((dbg, LEVEL_4, "replaced\n"));

		free(optimization);
	}

	pmap_destroy(walk_counter);
	plist_free(optimizations);
}

Victor Pfautz's avatar
Victor Pfautz committed
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
/**
 * Returns true if we can be sure that @p node only has a single read user.
 */
static bool only_one_user(const ir_node *node)
{
	ir_graph *irg = get_irn_irg(node);
	if (!edges_activated(irg))
		return false;
	return get_irn_n_edges(node) <= 1;
}

typedef enum {
    NOT_FOUND,    /**< no match found */
    FOUND_FIRST,  /**< found a match for the first node */
    FOUND_SECOND  /**< found a match for the second node */
} match_result_t;

/**
 * Recursively replaces a with b/~b, or b with a/~a, according to need_not.
 *
 * We only want to replace something if our subgraph contains a and b.
 * So we set the @p replace flag when we found the first node and perform
 * the actual replacement if we found the second one.
 *
 * @param node      The currently visited node
 * @param a         The first node we want to match
 * @param b         The second node we want to match
 * @param need_not  Whether we should replace a with ~b (instead of b), or vice versa.
 * @param replace   Whether we already found one node and thus can replace the other.
 *
 * @return match result that indicates whether we found a or b first, or none of them.
 */
static match_result_t replace_until_other_user(ir_node *node, ir_node *a, ir_node *b, bool need_not, bool replace)
{
	match_result_t ret = replace;
	if (!is_bitop(node) || !only_one_user(node)) {
		return ret;
	}

	foreach_irn_in(node, i, t) {
		if (t == a) {
			if (replace) {
				ir_node *op = b;
				if (need_not) {
					op = new_rd_Not(get_irn_dbg_info(op), get_nodes_block(op), op, get_irn_mode(op));
				}
				DBG((dbg, LEVEL_4, "replace %li @ %li with %li\n", get_irn_node_nr(t), get_irn_node_nr(node), get_irn_node_nr(op)));
				set_irn_n(node, i, op);
			} else {
				replace = true;
				ir_node *t  = a;
				a   = b;
				b   = t;
				ret = FOUND_SECOND;
			}
		} else if (t == b) {
			if (!replace) {
				replace = true;
				ret     = FOUND_FIRST;
			}
		} else {
			match_result_t res = replace_until_other_user(t, a, b, need_not, replace);
			switch (res) {
			case FOUND_SECOND: {
				ir_node *t  = a;
				a = b;
				b = t;
				/* fall through */
			}
			case FOUND_FIRST:
				if (!replace)
					ret = res;
				replace = true;
				break;
			default:
				break;
			}
		}
	}

	return ret;
}

/**
 * If one of the following cases is matched, replace_until_other_user will be called:
 *  (a ^ b) & f(a, b) ->  (a ^ b) & f(a, ~a)
 * ~(a ^ b) & f(a, b) -> ~(a ^ b) & f(a,  a)
 *  (a ^ b) | f(a, b) ->  (a ^ b) | f(a,  a)
 * ~(a ^ b) | f(a, b) -> ~(a ^ b) | f(a, ~a)
 */
static void walk_equality(ir_node *node, void *env)
{
	(void)env;

	if (!is_And(node) && !is_Or(node)) {
		return;
	}

	ir_node *l        = get_binop_left(node);
	ir_node *r        = get_binop_right(node);
	bool     need_not = is_And(node);

	if (is_Not(l) && is_Eor(get_Not_op(l))) {
		l        = get_Not_op(l);
		need_not = !need_not;
	} else if (is_Eor(l)) {
	} else if (is_Not(r) && is_Eor(get_Not_op(r))) {
		ir_node *t = get_Not_op(r);
		r          = l;
		l          = t;
		need_not   = !need_not;
	} else if (is_Eor(r)) {
		ir_node *t = r;
		r          = l;
		l          = t;
	} else {
		return;
	}

	ir_node *a = get_binop_left(l);
	ir_node *b = get_binop_right(l);
	assert(a != b);

	if (is_Const(b)) {
		replace_until_other_user(r, a, b, need_not, true);
	} else if (is_Const(a)) {
		replace_until_other_user(r, b, a, need_not, true);
	} else {
		if (is_Not(a)) {
			need_not = !need_not;
			a        = get_Not_op(a);
		}

		if (is_Not(b)) {
			need_not = !need_not;
			b        = get_Not_op(b);
		}

		replace_until_other_user(r, a, b, need_not, false);
	}
}

Victor Pfautz's avatar
Victor Pfautz committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
typedef struct {
	ir_node *base_node;      /**< Root node of the multiset */
	pset    *operands;       /**< Non-constant operands of the multiop */
	pset    *nodes;          /**< Nodes that belong to the multiop */
	pmap    *multiplier;     /**< Maps each node to its multiplier,
	                              e.g. x - y + x has the mapping x -> 2 and y -> -1 */
	pmap    *edge_value;     /**< Edge-based multiplier, used to compute the multiplier above */
	pset    *multi_operands; /**< Multiops of all operands */
	pset    *multi_users;    /**< Multiops of all users */
	bool     other_op;       /**< Whether a node has a user that does not belong to any multiop in multi_users
	                              (because the operation is not supported by multiops) */
	bool     changed;        /**< Whether we reassociate the nodes in the multiop */
} multi_op;

typedef struct {
	pmap *set_map;     /**< Maps each node to its multi_op */
	pset *sets;        /**< Set of all multi_ops */
	pset *walkhistory; /**< Visited nodes during computation of multi_ops */
} multi_op_env;

/**
 * Creates a new multi_op. This is a set of operations, with same type and mode.
 * Sub and scalar Mul is seen as Add.
 */
static multi_op *new_multi_op(multi_op_env *multi_env, ir_node *base_node)
{
	multi_op *data       = XMALLOC(multi_op);
	data->operands       = pset_new_ptr_default();
	data->nodes          = pset_new_ptr_default();
	data->multi_operands = pset_new_ptr_default();
	data->multi_users    = pset_new_ptr_default();
	data->base_node      = base_node;
	data->other_op       = false;
	data->changed        = false;
	data->multiplier     = pmap_create();
	data->edge_value     = pmap_create();

	if (is_Add(base_node) || is_Sub(base_node)) {
		ir_mode *mode = get_irn_mode(base_node);
		if (mode_is_reference(mode)) {
			ir_node *right      = get_binop_right(base_node);
			ir_mode *right_mode = get_irn_mode(right);
			if (mode_is_reference(right_mode)) {
				ir_node *left = get_binop_left(base_node);
				mode = get_irn_mode(left);
			} else {
				mode = right_mode;
			}
		}
		pmap_insert(data->edge_value, base_node, get_mode_one(mode));
	} else if (is_Mul(base_node)) {
		ir_node *l = get_Mul_left(base_node);
		ir_node *r = get_Mul_right(base_node);
		if (is_Const(l)) {
			pmap_insert(data->edge_value, base_node, get_Const_tarval(l));
			if (tarval_is_negative(get_Const_tarval(l))) {
				data->changed = true;
			}
		} else {
			assert(is_Const(r));
			pmap_insert(data->edge_value, base_node, get_Const_tarval(r));
			if (tarval_is_negative(get_Const_tarval(r))) {
				data->changed = true;
			}
		}
	}

	pset_insert_ptr(data->nodes, base_node);
	pmap_insert(multi_env->set_map, base_node, data);
	pset_insert_ptr(multi_env->sets, data);

	return data;
}

static ir_op *get_multi_op_op(multi_op *multi_op)
{
	ir_node *base_node = multi_op->base_node;
	return is_Sub(base_node) || is_Mul(base_node) ? op_Add : get_irn_op(base_node);
}

/**
 * Destroys a multi_op and frees its memory.
 */
static void destroy_multi_op(multi_op *o)
{
	del_pset(o->operands);
	del_pset(o->nodes);
	del_pset(o->multi_operands);
	del_pset(o->multi_users);
	pmap_destroy(o->multiplier);
	pmap_destroy(o->edge_value);

	free(o);
}

/**
 * Returns true if the given node is a scalar multiplication.
 */
static bool is_scalar_Mul(ir_node *node)
{
	return is_Mul(node) && (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node)));
}

/**
 * Adds a node to a Eor set.
 */
static void add_to_Eor_set(multi_op_env *multi_env, ir_node *node, multi_op *set)
{
	if (get_irn_op(node) == get_multi_op_op(set)) {
		assert(!pset_find_ptr(set->nodes, node));

		pset_insert_ptr(set->nodes, node);
		pmap_insert(multi_env->set_map, node, set);
	} else if (pset_find_ptr(set->operands, node)) {
		pset_remove_ptr(set->operands, node);
	} else {
		pset_insert_ptr(set->operands, node);
	}
}

/**
 * Adds a node to an And or Or set.
 */
static void add_to_And_Or_set(multi_op_env *multi_env, ir_node *node, multi_op *set)
{
	if (get_irn_op(node) == get_multi_op_op(set)) {
		pset_insert_ptr(set->nodes, node);
		pmap_insert(multi_env->set_map, node, set);
	} else {
		pset_insert_ptr(set->operands, node);
	}
}

/**
 * Adds a node to a Add set, with given entry. The entry is the user of the node and indicates it's value.
 * Example: In 3*(x + y) is 3 the value of (x+y) and * is the entry.
 */
static void add_to_Add_set(multi_op_env *multi_env, ir_node *node, multi_op *set, ir_node *entry)
{
	ir_mode *mode      = get_irn_mode(node);
	ir_op   *op        = get_irn_op(node);
	ir_node *base_node = set->base_node;
	ir_mode *base_mode = get_irn_mode(base_node);
	if (is_scalar_Mul(node) && mode == base_mode) {
		/* add scalar Mul to set */
		assert(!entry);
		assert(!pset_find_ptr(set->nodes, node));

		ir_tarval *val = get_mode_null(mode);

		foreach_out_edge(node, edge) {
			ir_node   *src = get_edge_src_irn(edge);
			ir_tarval *t   = pmap_get(ir_tarval, set->edge_value, src);
			assert(t);
			if (is_Sub(src) && get_Sub_right(src) == node) {
				t = tarval_neg(t);
			}
			val = tarval_add(val, t);
		}

		if (is_Const(get_Mul_left(node))) {
			val = tarval_mul(val, get_Const_tarval(get_Mul_left(node)));
		} else {
			val = tarval_mul(val, get_Const_tarval(get_Mul_right(node)));
		}
		assert(tarval_is_constant(val));

		pset_insert_ptr(set->nodes, node);
		pmap_insert(multi_env->set_map, node, set);
		pmap_insert(set->edge_value, node, val);
	} else if ((op == op_Add || (op == op_Sub && !mode_is_reference(get_irn_mode(get_Sub_right(node)))
		       && !mode_is_reference(get_irn_mode(get_Sub_left(node))))) && mode == base_mode) {
		/* add Add or Sub to set */
		assert(!entry);
		assert(!pset_find_ptr(set->nodes, node));

		if (mode_is_reference(mode)) {
			if (mode_is_reference(get_irn_mode(get_binop_right(node)))) {
				mode = get_irn_mode(get_binop_left(node));
			} else {
				mode = get_irn_mode(get_binop_right(node));
			}
		}
		ir_tarval *val = get_mode_null(mode);
		foreach_out_edge(node, edge) {
			ir_node *src = get_edge_src_irn(edge);

			ir_tarval *t = pmap_get(ir_tarval, set->edge_value, src);
			if (tarval_is_one(t) && get_tarval_mode(t) != mode) {
				t = get_mode_one(get_tarval_mode(val));
			}
			assert(t);
			if (is_Sub(src) && get_Sub_right(src) == node) {
				t = tarval_neg(t);
			}
			val = tarval_add(val, t);
		}
		assert(tarval_is_constant(val));

		pset_insert_ptr(set->nodes, node);
		pmap_insert(multi_env->set_map, node, set);
		pmap_insert(set->edge_value, node, val);
	} else {
		assert(entry);

		ir_tarval *new_val = pmap_get(ir_tarval, set->edge_value, entry);
		assert(new_val);

		if (is_Sub(entry) && get_Sub_right(entry) == node) {
			new_val = tarval_neg(new_val);
		}

		if (is_Const