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
#include "iropt_dbg.h"
20
#include "iredges_t.h"
21
22
#include "irflag_t.h"
#include "irgwalk.h"
23
#include "irouts.h"
24
#include "reassoc_t.h"
25
#include "opt_init.h"
26
27
#include "irhooks.h"
#include "irloop.h"
28
#include "pdeq.h"
29
#include "debug.h"
Victor Pfautz's avatar
Victor Pfautz committed
30
#include "panic.h"
31

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

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

typedef enum {
38
39
40
    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
41
42
43
} const_class_t;

/**
44
 * returns whether a node is constant ie is a constant or
45
 * is loop invariant (called region constant)
46
47
48
 *
 * @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
49
 */
50
static const_class_t get_const_class(const ir_node *n, const ir_node *block)
Michael Beck's avatar
Michael Beck committed
51
{
52
	if (is_Const(n))
53
		return REAL_CONSTANT;
54

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

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

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

/**
 * returns the operands of a commutative bin-op, if one operand is
71
 * a region constant, it is returned as the second one.
Michael Beck's avatar
Michael Beck committed
72
 *
Michael Beck's avatar
Michael Beck committed
73
 * Beware: Real constants must be returned with higher priority than
74
 * region constants, because they might be folded.
Michael Beck's avatar
Michael Beck committed
75
76
77
 */
static void get_comm_Binop_ops(ir_node *binop, ir_node **a, ir_node **c)
{
Matthias Braun's avatar
Matthias Braun committed
78
	assert(is_op_commutative(get_irn_op(binop)));
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
79
80
81
82
83
	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);
84

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

Matthias Braun's avatar
Matthias Braun committed
105
106
/**
 * Retrieve a mode from the operands. We need this, because
Michael Beck's avatar
Michael Beck committed
107
108
109
110
 * 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
111
	ir_mode *m1 = get_irn_mode(op1);
112
113
	if (mode_is_reference(m1))
		return m1;
Michael Beck's avatar
Michael Beck committed
114

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

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

Matthias Braun's avatar
Matthias Braun committed
123
124
125
126
127
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);
128
	verify_new_node(new);
Matthias Braun's avatar
Matthias Braun committed
129
130
131
	return optimize_node(new);
}

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

	get_comm_Binop_ops(n, &t1, &c1);

	if (get_irn_op(t1) == op) {
Matthias Braun's avatar
Matthias Braun committed
150
151
		ir_node *t2;
		ir_node *c2;
152
		get_comm_Binop_ops(t1, &t2, &c2);
Matthias Braun's avatar
Matthias Braun committed
153
154
155
		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);
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
156
157
158
159
160
161
162
163
164
165
166
		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;
			}
167
168

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

174
175
			/* It might happen, that c1 and c2 have different modes, for
			 * instance Is and Iu.
176
177
			 * Handle this here.
			 */
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
178
179
180
181
182
183
184
185
186
187
188
			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)
189
						c1 = new_r_Conv(block, c1, mode_c2);
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
190
191
					else
						c2 = new_r_Conv(block, c2, mode_c1);
192
193
194
				}
			}

195
196
197
198
			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);
199

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

204
205
206
			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));
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
207
208
209
210

			exchange(n, irn1);
			*node = irn1;
			return 1;
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
		}
	}
	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
226
227

		const_class_t c_r = get_const_class(r, block);
228
229
230
231
232
233
234
235
		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;
		}

236
		if (l == c1 && r != c1) {
237
			/* convert x .OP. (x .OP. y) => y .OP. (x .OP. x) */
238
239
240
241
242
			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,
243
			                                 ARRAY_SIZE(in), in);
244

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

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

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

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

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

/**
 * The walker for the reassociation.
 */
278
static void do_reassociation(pdeq *const wq)
279
{
280
281
	while (!pdeq_empty(wq)) {
		ir_node *n = (ir_node *)pdeq_getl(wq);
282
283
		set_irn_link(n, NULL);

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

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

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

299
				changed |= res;
300
			}
Matthias Braun's avatar
Matthias Braun committed
301
		} while (res);
302
303

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

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

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

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

365
366
	case iro_Add:
		return is_const(get_Add_left(irn)) && is_const(get_Add_right(irn));
367

368
	default:
369
		return 0;
370
	}
371
}
372

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

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

		if (x == get_Shl_right(right)) {
Michael Beck's avatar
Michael Beck committed
392
			/* (a << x) +/- (b << x) ==> (a +/- b) << x */
393
394
395
396
397
398
399
400
			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
401
			/* (x * a) +/- (x * b) ==> (a +/- b) * x */
402
403
404
405
			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
406
			/* (x * a) +/- (b * x) ==> (a +/- b) * x */
407
408
409
410
			a = get_Mul_right(left);
			b = get_Mul_left(right);
			goto transform;
		}
411

412
413
414
		x = get_Mul_right(left);

		if (x == get_Mul_right(right)) {
Michael Beck's avatar
Michael Beck committed
415
			/* (a * x) +/- (b * x) ==> (a +/- b) * x */
416
417
418
419
			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
420
			/* (a * x) +/- (x * b) ==> (a +/- b) * x */
421
422
423
424
			a = get_Mul_left(left);
			b = get_Mul_right(right);
			goto transform;
		}
425
426
427
	}
	return 0;

Matthias Braun's avatar
Matthias Braun committed
428
429
430
431
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);
432

Matthias Braun's avatar
Matthias Braun committed
433
	ir_node *irn;
434
	if (is_Add(n))
435
		irn = new_rd_Add(dbg, blk, a, b);
436
	else
437
		irn = new_rd_Sub(dbg, blk, a, b);
438

439
	blk = earliest_block(irn, x, curr_blk);
440
	if (op == op_Mul)
441
		irn = new_rd_Mul(dbg, blk, irn, x);
442
	else
443
		irn = new_rd_Shl(dbg, blk, irn, x);
444
445
446
447

	exchange(n, irn);
	*node = irn;
	return 1;
448
}
449

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

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

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

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

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

Matthias Braun's avatar
Matthias Braun committed
508
transform:;
509
	/* In some cases a and b might be both of different integer mode, and c a
510
	 * Address/Offset/TypeConst.
511
512
513
514
515
	 * 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
516
517
	ir_mode *ma = get_irn_mode(a);
	ir_mode *mb = get_irn_mode(b);
518
519
520
	if (ma != mb && mode_is_int(ma) && mode_is_int(mb))
		return 0;

Matthias Braun's avatar
Matthias Braun committed
521
522
	/* check if (a .op. b) can be calculated in the same block is the old
	 * instruction */
523
524
525
526
527
	if (! block_dominates(get_nodes_block(a), blk))
		return 0;
	if (! block_dominates(get_nodes_block(b), blk))
		return 0;
	/* ok */
528
529
530
531
	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);
532

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

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

Matthias Braun's avatar
Matthias Braun committed
542
543
	exchange(n, irn1);
	*node = irn1;
544
	return 1;
545
}
546

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

Matthias Braun's avatar
Matthias Braun committed
558
	bool res;
559
	do {
Matthias Braun's avatar
Matthias Braun committed
560
		res = false;
561

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

573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/**
 * 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;

598
599
600
601
602
603
604
605
/**
 * 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)
{
	return get_irn_n_edges(node) <= 1;
}

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
/**
 * 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;
	}

646
	if (current == top_node2 && ((middle_node && !only_one_user(middle_node)) || base_node != other_node)) {
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
		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);

668
	if ((counter - shdata->walk_base) == (unsigned)get_irn_n_edges(current) && is_bitop(current)) {
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
		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)) {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
684
		ir_node *l = get_Eor_left(node);
685
686
		ir_node *r = get_Eor_right(node);
		if (is_Const(r)) {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
687
688
689
690
691
			assert(!is_Const(l));
			return l;
		}
		if (is_Const(l)) {
			return r;
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
		}
	}

	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)));

810
811
812
813
814
815
816
		/* This case can be handled by local optimizations.
		 * We skip the case, because we would otherwise
		 * simplify a & a to -1 & -1, or need to track edges
		 * instead of nodes in the optimization environment. */
		if (base_node == other_node)
			continue;

817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
		/* 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
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
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) {
886
					op = new_rd_Not(get_irn_dbg_info(op), get_nodes_block(op), op);
Victor Pfautz's avatar
Victor Pfautz committed
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
				}
				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)
 */
932
static void walk_equality(ir_node *node)
Victor Pfautz's avatar
Victor Pfautz committed
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
{
	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_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;
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
954
	} else if (!is_Eor(l)) {
Victor Pfautz's avatar
Victor Pfautz committed
955
956
957
958
959
		return;
	}

	ir_node *a = get_binop_left(l);
	ir_node *b = get_binop_right(l);
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
960
961
962
	if (a == b) {
		return;
	}
Victor Pfautz's avatar
Victor Pfautz committed
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982

	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
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
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(node) && is_Mul(entry)) {
			DBG((dbg, LEVEL_5, "Const skipped\n"));
			return;
		}

		if (pset_find_ptr(set->operands, node)) {
			ir_tarval *old_val = pmap_get(ir_tarval, set->multiplier, node);
			new_val            = tarval_add(old_val, new_val);
			set->changed       = true;
		} else {
			pset_insert_ptr(set->operands, node);
		}
		pmap_insert(set->multiplier, node, new_val);
	}
}

/**
 * Adds a node to a set, with given entry.
 */
static void add_to_set(multi_op_env *multi_env, ir_node *node, multi_op *set, ir_node *entry)
{
	switch (get_op_code(get_multi_op_op(set))) {
	case iro_Eor:
		add_to_Eor_set(multi_env, node, set);
		break;
	case iro_And:
	case iro_Or:
		add_to_And_Or_set(multi_env, node, set);
		break;
	case iro_Add:
		add_to_Add_set(multi_env, node, set, entry);
		break;

	default:
		panic("Operation not supported");
	}
}

/**
 * Connects an operand multi_op to a user multi_op.
 * If operand is not Eor it would be added to operands.
 */
static void connect_Eor(multi_op *operand, multi_op *user)
{
	assert(get_multi_op_op(user) == op_Eor);

	if (pset_find_ptr(user->multi_operands, operand)) {
		assert(pset_find_ptr(operand->multi_users, user));

		pset_remove_ptr(user->multi_operands, operand);
		pset_remove_ptr(operand->multi_users, user);

		user->changed = true;
	} else {
		pset_insert_ptr(operand->multi_users, user);
		pset_insert_ptr(user->multi_operands, operand);
	}
}

/**
 * Connects an operand multi_op to a user multi_op.
 * If operand is not And it would be added to operands.
 */
static void connect_And_Or(multi_op *operand, multi_op *user)
{
	assert(get_multi_op_op(user) == op_And || get_multi_op_op(user) == op_Or);

	if (pset_find_ptr(user->multi_operands, operand)) {
		assert(pset_find_ptr(operand->multi_users, user));

		user->changed = true;
	} else {
		pset_insert_ptr(operand->multi_users, user);
		pset_insert_ptr(user->multi_operands, operand);
	}
}

/**
 * Connects an operand multi_op to a user multi_op.
 * Entry gives the value of the operand, like in add_to_Add_set.
 */
static void connect_Add(multi_op *operand, multi_op *user, ir_node *op_node, ir_node *entry)
{
	assert(entry);

	ir_mode *mode = get_irn_mode(operand->base_node);
	if (mode_is_reference(mode)) {
		if (mode_is_reference(get_irn_mode(get_binop_right(operand->base_node)))) {
			mode = get_irn_mode(get_binop_left(operand->base_node));
		} else {
			mode = get_irn_mode(get_binop_right(operand->base_node));
		}
	}
	ir_tarval *new_val = get_mode_null(mode);
	ir_tarval *t = pmap_get(ir_tarval, user->edge_value, entry);

	if (tarval_is_one(t) && get_tarval_mode(t) != mode) {
		t = get_mode_one(mode);
	}

	new_val = tarval_add(new_val, t);
	assert(new_val);

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

	assert(get_multi_op_op(user) == op_Add);

	if (pset_find_ptr(user->multi_operands, operand)) {
		assert(pset_find_ptr(operand->multi_users, user));

		ir_tarval *val = pmap_get(ir_tarval, user->multiplier, operand);
		new_val        = tarval_add(val, new_val);

		user->changed = true;
	} else {
		pset_insert_ptr(operand->multi_users, user);
		pset_insert_ptr(user->multi_operands, operand);
	}
	pmap_insert(user->multiplier, operand, new_val);
}

/**
 * Connects a operand multi_op to a user multi_op. Connect means operand is a whole operand of user.
 */
static void connect_sets(multi_op *operand, multi_op *user, ir_node *op_node, ir_node *user_node)
{
	switch (get_op_code(get_multi_op_op(user))) {
	case iro_Eor:
		connect_Eor(operand, user);
		break;
	case iro_And:
	case iro_Or:
		connect_And_Or(operand, user);
		break;
	case iro_Add:
		connect_Add(operand, user, op_node, user_node);
		break;
	default:
		panic("Operation not supported");
	}

}

/**
 * Returns the first user of node.
 */
static ir_node *get_first_user(ir_node *node)
{
	return get_edge_src_irn(get_irn_out_edge_first(node));
}


/**
 * Returns true iff the given node is supported by setsort, else it would be handled as a variable.
 */
static bool is_supported_node(ir_node *node)
{
	if (mode_is_float(get_irn_mode(node))) {
		return false;
	}

	switch (get_op_code(get_irn_op(node))) {
	case iro_Eor:
	case iro_And:
	case iro_Or:
	case iro_Add:
		return true;
	case iro_Sub:
		return !mode_is_reference(get_irn_mode(get_Sub_left(node)))
		    && !