constbits.c 25.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @file
 * @brief   Data-flow driven minimal fixpoint value range analysis
 * @author  Christoph Mallon
 */
11
12
#include "constbits.h"

13
14
15
16
#include <assert.h>

#include "adt/pdeq.h"
#include "debug.h"
17
#include "iredges_t.h"
18
19
#include "irgwalk.h"
#include "irnode_t.h"
20
#include "irnodemap.h"
21
#include "iropt.h"
22

23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef VERIFY_CONSTBITS
#	ifdef DEBUG_libfirm
#		define VERIFY_CONSTBITS 1
#	else
#		define VERIFY_CONSTBITS 0
#	endif
#endif

#if VERIFY_CONSTBITS
#include "irdump.h"
#include "irprintf.h"
#include "panic.h"
#endif

37
/* TODO:
38
 * - Implement cleared/set bit calculation for Div, Mod
39
40
 * - Implement min/max calculation for And, Eor, Or, Not, Conv, Shl, Shr, Shrs, Mux
 * - Implement min/max calculation for Add, Sub, Minus, Mul, Div, Mod, Conv, Shl, Shr, Shrs, Mux
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 */

/* Tables of the cleared/set bit lattice
 *
 * Encoding of the lattice
 * zo
 * 00 0 zero
 * 01 - impossible state, is zero /and/ one
 * 10 T top, may be either zero or one
 * 11 1 one
 *
 * S = Sum
 * c = Carry
 * D = Difference
 * b = Borrow
 *
 * Not
 * A ~
 * 0 1
 * 1 0
 * T T
 *
 * Half adder, half subtractor, and, xor, or, Mux
 * AB  Sc  Db  &  ^  |  M
 * 00  00  00  0  0  0  0
 * 01  10  11  0  1  1  T
 * 0T  T0  TT  0  T  T  T
 * 10  10  10  0  1  1  T
 * 11  01  00  1  0  1  1
 * 1T  TT  T0  T  T  1  T
 * T0  T0  T0  0  T  T  T
 * T1  TT  TT  T  T  1  T
 * TT  TT  TT  T  T  T  T
 *
 * Full adder, full subtractor
 * ABc-1  Sc  Db
 * 000    00  00
 * 001    10  11
 * 00T    T0  TT
 * 010    10  11
 * 011    01  01
 * 01T    TT  T1
 * 0T0    T0  TT
 * 0T1    TT  T1
 * 0TT    TT  TT
 * 100    10  10
 * 101    01  00
 * 10T    TT  T0
 * 110    01  00
 * 111    11  11
 * 11T    T1  TT
 * 1T0    TT  T0
 * 1T1    T1  TT
 * 1TT    TT  TT
 * T00    T0  T0
 * T01    TT  TT
 * T0T    TT  TT
 * T10    TT  TT
 * T11    T1  T1
 * T1T    TT  TT
 * TT0    TT  TT
 * TT1    TT  TT
 * TTT    TT  TT
 *
 *
 * Assume: Xmin <= Xmax and no overflow
 * A + B = (Amin + Bmin, Amax + Bmax)
 *    -A = (-Amax, -Amin)
 * A - B = A + -B = (Amin (-B)min, Amax + (-B)max) = (Amin - Bmax, Amax - Bmin)
 */

DEBUG_ONLY(static firm_dbg_module_t *dbg;)

Christoph Mallon's avatar
Christoph Mallon committed
114
static bool is_undefined(bitinfo const *const b)
115
116
117
118
{
	return tarval_is_null(b->z) && tarval_is_all_one(b->o);
}

119
120
/** Set analysis information for node @p irn. */
static bool set_bitinfo(ir_node const *const irn, ir_tarval *const z, ir_tarval *const o)
121
{
122
123
124
	ir_graph   *const irg  = get_irn_irg(irn);
	ir_nodemap *const map  = &irg->bitinfo.map;
	bitinfo          *b    = ir_nodemap_get(bitinfo, map, irn);
125
	if (b == NULL) {
yb9976's avatar
yb9976 committed
126
		struct obstack *const obst = &irg->bitinfo.obst;
127
		b = OALLOCZ(obst, bitinfo);
128
		ir_nodemap_insert(map, irn, b);
129
	} else if (z == b->z && o == b->o) {
Matthias Braun's avatar
Matthias Braun committed
130
		return false;
131
	} else {
132
		/* Assert ascending chain. */
133
134
135
136
137
		assert(tarval_is_null(tarval_andnot(b->z, z)));
		assert(tarval_is_null(tarval_andnot(o, b->o)));
	}
	b->z = z;
	b->o = o;
138
	DB((dbg, LEVEL_3, "Set %+F: 0:%T 1:%T\n", irn, z, o));
Matthias Braun's avatar
Matthias Braun committed
139
	return true;
140
141
}

Christoph Mallon's avatar
Christoph Mallon committed
142
static bool mode_is_intb(ir_mode const *const m)
143
144
145
146
{
	return mode_is_int(m) || m == mode_b;
}

147
148
149
150
151
152
153
bitinfo const *try_get_bitinfo(ir_node const *const irn)
{
	ir_graph   *const irg = get_irn_irg(irn);
	ir_nodemap *const map = &irg->bitinfo.map;
	return map->data ? ir_nodemap_get(bitinfo, map, irn) : NULL;
}

154
155
156
157
158
159
160
161
162
163
static bitinfo *get_bitinfo_null(ir_node const *const irn)
{
	(void)irn;
	return NULL;
}

static bitinfo *get_bitinfo_direct(ir_node const *const irn)
{
	ir_graph   *const irg = get_irn_irg(irn);
	ir_nodemap *const map = &irg->bitinfo.map;
164
165
166
167
168
169
170
	bitinfo          *b   = ir_nodemap_get(bitinfo, map, irn);
	if (!b && is_Const(irn) && mode_is_intb(get_irn_mode(irn))) {
		ir_tarval *const tv = get_Const_tarval(irn);
		set_bitinfo(irn, tv, tv);
		b = ir_nodemap_get(bitinfo, map, irn);
	}
	return b;
171
172
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
static void calc_bitinfo(ir_node const *irn, bitinfo *b);

/**
 * Get analysis information for node @p irn.
 *
 * If it is not available, calculate it.
 */
static bitinfo *get_bitinfo_recursive(ir_node const *const irn)
{
	ir_graph   *const irg = get_irn_irg(irn);
	ir_nodemap *const map = &irg->bitinfo.map;
	bitinfo          *b   = ir_nodemap_get(bitinfo, map, irn);
	if (!b || b->state == BITINFO_INVALID || b->state == BITINFO_UNSTABLE) {
		ir_mode *mode = get_irn_mode(irn);
		if (mode == mode_BB || mode == mode_X) {
			/* Blocks and jumps use a boolean domain. */
			mode = mode_b;
		} else if (!mode_is_intb(mode)) {
			return NULL;
		}

		/* Insert bottom to break cycles. */
		struct obstack *const obst = &irg->bitinfo.obst;
		if (!b)
			b = OALLOCZ(obst, bitinfo);
		if (b->state == BITINFO_INVALID) {
			b->z = get_mode_null(mode);
			b->o = get_mode_all_one(mode);
		}
		ir_nodemap_insert(map, irn, b);

		calc_bitinfo(irn, b);
	}
	return b;
}

209
210
211
212
213
214
215
static bitinfo *(*get_bitinfo_func)(ir_node const*) = &get_bitinfo_null;

bitinfo *get_bitinfo(ir_node const *const irn)
{
	return get_bitinfo_func(irn);
}

216
static bool transfer(ir_node const *const irn)
217
{
218
219
	ir_tarval *const f = tarval_b_false;
	ir_tarval *const t = tarval_b_true;
Christoph Mallon's avatar
Christoph Mallon committed
220
221
222
	ir_mode   *const m = get_irn_mode(irn);
	ir_tarval       *z;
	ir_tarval       *o;
223
224
225
226

	if (m == mode_X) {
		DB((dbg, LEVEL_3, "transfer %+F\n", irn));

227
228
		bitinfo *const b = get_bitinfo_recursive(get_nodes_block(irn));
		if (b->z == f) {
229
230
231
232
unreachable_X:
			z = f;
			o = t;
		} else switch (get_irn_opcode(irn)) {
233
234
235
			case iro_Bad:
				goto unreachable_X;

236
			case iro_Proj: {
Christoph Mallon's avatar
Christoph Mallon committed
237
				ir_node *const pred = get_Proj_pred(irn);
238
239
240
				if (is_Start(pred)) {
					goto result_unknown_X;
				} else if (is_Cond(pred)) {
Christoph Mallon's avatar
Christoph Mallon committed
241
					ir_node   *const selector = get_Cond_selector(pred);
242
					bitinfo   *const b        = get_bitinfo_recursive(selector);
243
244
245
					if (is_undefined(b))
						goto unreachable_X;
					if (b->z == b->o) {
246
						if ((b->z == t) == get_Proj_num(irn)) {
247
248
249
250
251
252
253
254
							z = o = t;
						} else {
							z = o = f;
						}
					} else {
						goto result_unknown_X;
					}
				} else if (is_Switch(pred)) {
Christoph Mallon's avatar
Christoph Mallon committed
255
					ir_node *const selector = get_Switch_selector(pred);
256
					bitinfo *const b        = get_bitinfo_recursive(selector);
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
					if (is_undefined(b))
						goto unreachable_X;
					/* TODO */
					goto cannot_analyse_X;
				} else {
					goto cannot_analyse_X;
				}
				break;
			}

			case iro_Jmp:
				goto result_unknown_X;

			default:
cannot_analyse_X:
				DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
result_unknown_X:
				z = t;
				o = f;
				break;
		}
	} else if (is_Block(irn)) {
		DB((dbg, LEVEL_3, "transfer %+F\n", irn));
Matthias Braun's avatar
Matthias Braun committed
280
281
		bool reachable = false;
		foreach_irn_in(irn, i, pred_block) {
282
283
			bitinfo *const b = get_bitinfo_recursive(pred_block);
			if (b->z == t) {
Matthias Braun's avatar
Matthias Braun committed
284
				reachable = true;
285
286
287
288
289
				break;
			}
		}

		if (!reachable) {
290
			ir_graph *const irg = get_irn_irg(irn);
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
			reachable =
				irn == get_irg_start_block(irg) ||
				irn == get_irg_end_block(irg);
		}

		if (reachable) {
			z = t;
			o = f;
		} else {
			z = f;
			o = t;
		}
	} else if (mode_is_intb(m)) {
		DB((dbg, LEVEL_3, "transfer %+F\n", irn));

306
		if (is_Phi(irn)) {
Christoph Mallon's avatar
Christoph Mallon committed
307
			ir_node *const block = get_nodes_block(irn);
308

309
310
			z = get_mode_null(m);
			o = get_mode_all_one(m);
Matthias Braun's avatar
Matthias Braun committed
311
			foreach_irn_in(block, i, pred_block) {
312
313
314
				bitinfo *const b_cfg = get_bitinfo_recursive(pred_block);
				if (b_cfg->z != f) {
					bitinfo *const b = get_bitinfo_recursive(get_Phi_pred(irn, i));
315
316
317
318
319
320
321
322
323
					/* Only use input if it's not undefined. */
					if (!is_undefined(b)) {
						z = tarval_or( z, b->z);
						o = tarval_and(o, b->o);
					}
				}
			}
		} else {
			/* Undefined if any input is undefined. */
324
			foreach_irn_in(irn, i, pred) {
325
				bitinfo *const pred_b = get_bitinfo_recursive(pred);
326
327
328
329
				if (pred_b != NULL && is_undefined(pred_b))
					goto undefined;
			}

330
331
			/* leverage compute_value_node_XXX knowledge */
			ir_tarval *computed = computed_value(irn);
332
			if (tarval_is_constant(computed)) {
333
334
				z = o = computed;
				goto set_info;
335
336
			} else if (computed == tarval_bad) {
				goto undefined;
337
338
			}

339
			switch (get_irn_opcode(irn)) {
340
				case iro_Bad:
341
342
343
344
undefined:
					z = get_mode_null(m);
					o = get_mode_all_one(m);
					break;
345

346
347
348
349
350
351
				case iro_Const: {
					z = o = get_Const_tarval(irn);
					break;
				}

				case iro_Confirm: {
Christoph Mallon's avatar
Christoph Mallon committed
352
					ir_node *const v = get_Confirm_value(irn);
353
					bitinfo *const b = get_bitinfo_recursive(v);
354
355
356
357
					/* TODO Use bound and relation. */
					z = b->z;
					o = b->o;
					if ((get_Confirm_relation(irn) & ~ir_relation_unordered) == ir_relation_equal) {
358
						bitinfo *const bound_b = get_bitinfo_recursive(get_Confirm_bound(irn));
359
360
361
362
363
364
365
						z = tarval_and(z, bound_b->z);
						o = tarval_or( o, bound_b->o);
					}
					break;
				}

				case iro_Shl: {
366
					ir_node   *const right = get_Shl_right(irn);
367
368
					bitinfo   *const l     = get_bitinfo_recursive(get_Shl_left(irn));
					bitinfo   *const r     = get_bitinfo_recursive(right);
369
370
371
372
373
374
375
					ir_tarval *const lo    = l->o;
					ir_tarval *const lz    = l->z;
					ir_tarval *const ro    = r->o;
					ir_tarval *const rz    = r->z;
					if (rz == ro) {
						z = tarval_shl(lz, rz);
						o = tarval_shl(lo, rz);
376
					} else {
377
378
379
380
						const long        size_bits     = get_mode_size_bits(m);
						const long        modulo_shift  = get_mode_modulo_shift(m);
						ir_mode    *const rmode         = get_irn_mode(right);
						ir_tarval  *const rone          = get_mode_one(rmode);
381
382
						ir_tarval  *const size_mask     = tarval_sub(new_tarval_from_long(size_bits, rmode), rone);
						ir_tarval  *const modulo_mask   = tarval_sub(new_tarval_from_long(modulo_shift, rmode), rone);
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
						ir_tarval  *const zero          = get_mode_null(m);
						ir_tarval  *const all_one       = get_mode_all_one(m);
						ir_tarval  *const oversize_mask = tarval_andnot(modulo_mask, size_mask);

						z = zero;
						o = tarval_is_null(tarval_and(rz, oversize_mask)) ? all_one : zero;

						if (tarval_is_null(tarval_and(ro, oversize_mask))) {
							ir_tarval *const rmask  = tarval_and(size_mask, modulo_mask);
							ir_tarval *const rsure  = tarval_and(tarval_not(tarval_eor(ro, rz)), rmask);
							ir_tarval *const rbound = tarval_add(rmask, rone);
							ir_tarval *const rzero  = get_mode_null(rmode);
							for (ir_tarval *shift_amount = rzero; shift_amount != rbound; shift_amount = tarval_add(shift_amount, rone)) {
								if (tarval_is_null(tarval_and(rsure, tarval_eor(shift_amount, rz)))) {
									z = tarval_or(z, tarval_shl(lz, shift_amount));
									o = tarval_and(o, tarval_shl(lo, shift_amount));
								}
							}
						}

						/* Ensure that we do not create undefined bit information. */
						assert(!tarval_is_null(z) || !tarval_is_all_one(o));
405
406
407
408
409
					}
					break;
				}

				case iro_Shr: {
410
					ir_node   *const right = get_Shr_right(irn);
411
412
					bitinfo   *const l     = get_bitinfo_recursive(get_Shr_left(irn));
					bitinfo   *const r     = get_bitinfo_recursive(right);
413
414
415
416
					ir_tarval *const lz    = l->z;
					ir_tarval *const lo    = l->o;
					ir_tarval *const rz    = r->z;
					ir_tarval *const ro    = r->o;
417
					if (rz == r->o) {
418
419
						z = tarval_shr(lz, rz);
						o = tarval_shr(lo, rz);
420
					} else {
421
422
423
424
						const long        size_bits     = get_mode_size_bits(m);
						const long        modulo_shift  = get_mode_modulo_shift(m);
						ir_mode    *const rmode         = get_irn_mode(right);
						ir_tarval  *const rone          = get_mode_one(rmode);
425
426
						ir_tarval  *const size_mask     = tarval_sub(new_tarval_from_long(size_bits, rmode), rone);
						ir_tarval  *const modulo_mask   = tarval_sub(new_tarval_from_long(modulo_shift, rmode), rone);
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
						ir_tarval  *const zero          = get_mode_null(m);
						ir_tarval  *const all_one       = get_mode_all_one(m);
						ir_tarval  *const oversize_mask = tarval_andnot(modulo_mask, size_mask);

						z = zero;
						o = tarval_is_null(tarval_and(rz, oversize_mask)) ? all_one : zero;

						if (tarval_is_null(tarval_and(ro, oversize_mask))) {
							ir_tarval *const rmask  = tarval_and(size_mask, modulo_mask);
							ir_tarval *const rsure  = tarval_and(tarval_not(tarval_eor(ro, rz)), rmask);
							ir_tarval *const rbound = tarval_add(rmask, rone);
							ir_tarval *const rzero  = get_mode_null(rmode);
							for (ir_tarval *shift_amount = rzero; shift_amount != rbound; shift_amount = tarval_add(shift_amount, rone)) {
								if (tarval_is_null(tarval_and(rsure, tarval_eor(shift_amount, rz)))) {
									z = tarval_or(z, tarval_shr(lz, shift_amount));
									o = tarval_and(o, tarval_shr(lo, shift_amount));
								}
							}
445
						}
446
447
448

						/* Ensure that we do not create undefined bit information. */
						assert(!tarval_is_null(z) || !tarval_is_all_one(o));
449
450
451
452
453
					}
					break;
				}

				case iro_Shrs: {
454
					ir_node   *const right = get_Shrs_right(irn);
455
456
					bitinfo   *const l     = get_bitinfo_recursive(get_Shrs_left(irn));
					bitinfo   *const r     = get_bitinfo_recursive(right);
457
458
459
460
					ir_tarval *const lz    = l->z;
					ir_tarval *const lo    = l->o;
					ir_tarval *const rz    = r->z;
					ir_tarval *const ro    = r->o;
461
					if (rz == r->o) {
462
463
						z = tarval_shrs(lz, rz);
						o = tarval_shrs(lo, rz);
464
					} else {
465
466
467
468
						const long        size_bits     = get_mode_size_bits(m);
						const long        modulo_shift  = get_mode_modulo_shift(m);
						ir_mode    *const rmode         = get_irn_mode(right);
						ir_tarval  *const rone          = get_mode_one(rmode);
469
470
						ir_tarval  *const size_mask     = tarval_sub(new_tarval_from_long(size_bits, rmode), rone);
						ir_tarval  *const modulo_mask   = tarval_sub(new_tarval_from_long(modulo_shift, rmode), rone);
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
						ir_tarval  *const zero          = get_mode_null(m);
						ir_tarval  *const all_one       = get_mode_all_one(m);
						ir_tarval  *const oversize_mask = tarval_andnot(modulo_mask, size_mask);

						z = zero;
						o = tarval_is_null(tarval_and(rz, oversize_mask)) ? all_one : zero;

						if (tarval_is_null(tarval_and(ro, oversize_mask))) {
							ir_tarval *const rmask  = tarval_and(size_mask, modulo_mask);
							ir_tarval *const rsure  = tarval_and(tarval_not(tarval_eor(ro, rz)), rmask);
							ir_tarval *const rbound = tarval_add(rmask, rone);
							ir_tarval *const rzero  = get_mode_null(rmode);
							for (ir_tarval *shift_amount = rzero; shift_amount != rbound; shift_amount = tarval_add(shift_amount, rone)) {
								if (tarval_is_null(tarval_and(rsure, tarval_eor(shift_amount, rz)))) {
									z = tarval_or(z, tarval_shrs(lz, shift_amount));
									o = tarval_and(o, tarval_shrs(lo, shift_amount));
								}
							}
489
						}
490
491
492

						/* Ensure that we do not create undefined bit information. */
						assert(!tarval_is_null(z) || !tarval_is_all_one(o));
493
494
495
496
497
					}
					break;
				}

				case iro_Add: {
498
499
					bitinfo   *const l   = get_bitinfo_recursive(get_Add_left(irn));
					bitinfo   *const r   = get_bitinfo_recursive(get_Add_right(irn));
Christoph Mallon's avatar
Christoph Mallon committed
500
501
502
503
504
505
506
507
508
509
510
511
					ir_tarval *const lz  = l->z;
					ir_tarval *const lo  = l->o;
					ir_tarval *const rz  = r->z;
					ir_tarval *const ro  = r->o;
					ir_tarval *const vz  = tarval_add(lz, rz);
					ir_tarval *const vo  = tarval_add(lo, ro);
					ir_tarval *const lnc = tarval_eor(lz, lo);
					ir_tarval *const rnc = tarval_eor(rz, ro);
					ir_tarval *const vnc = tarval_eor(vz, vo);
					ir_tarval *const nc  = tarval_or(tarval_or(lnc, rnc), vnc);
					z = tarval_or(vz, nc);
					o = tarval_andnot(vz, nc);
512
513
514
515
					break;
				}

				case iro_Sub: {
516
517
					bitinfo *const l = get_bitinfo_recursive(get_Sub_left(irn));
					bitinfo *const r = get_bitinfo_recursive(get_Sub_right(irn));
Matthias Braun's avatar
Matthias Braun committed
518
519
520
521
					// might subtract pointers
					if (l == NULL || r == NULL)
						goto cannot_analyse;

Christoph Mallon's avatar
Christoph Mallon committed
522
523
524
525
					ir_tarval *const lz  = l->z;
					ir_tarval *const lo  = l->o;
					ir_tarval *const rz  = r->z;
					ir_tarval *const ro  = r->o;
526
527
					ir_tarval *const vz  = tarval_sub(lo, rz);
					ir_tarval *const vo  = tarval_sub(lz, ro);
Christoph Mallon's avatar
Christoph Mallon committed
528
529
530
531
532
533
					ir_tarval *const lnc = tarval_eor(lz, lo);
					ir_tarval *const rnc = tarval_eor(rz, ro);
					ir_tarval *const vnc = tarval_eor(vz, vo);
					ir_tarval *const nc  = tarval_or(tarval_or(lnc, rnc), vnc);
					z = tarval_or(vz, nc);
					o = tarval_andnot(vz, nc);
534
535
536
537
					break;
				}

				case iro_Mul: {
538
539
					bitinfo   *const l  = get_bitinfo_recursive(get_Mul_left(irn));
					bitinfo   *const r  = get_bitinfo_recursive(get_Mul_right(irn));
540
541
542
543
					ir_tarval *      lz = l->z;
					ir_tarval *      lo = l->o;
					ir_tarval *      rz = r->z;
					ir_tarval *      ro = r->o;
544
545
546
					if (lz == lo && rz == ro) {
						z = o = tarval_mul(lz, rz);
					} else {
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
						ir_tarval *one = get_mode_one(m);
						z = o = get_mode_null(m);
						while (!tarval_is_null(rz)) {
							if (!tarval_is_null(tarval_and(rz, one))) {
								ir_tarval *const vz  = tarval_add(lz, z);
								ir_tarval *const vo  = tarval_add(lo, o);
								ir_tarval *const lnc = tarval_eor(lz, lo);
								ir_tarval *const rnc = tarval_eor(z, o);
								ir_tarval *const vnc = tarval_eor(vz, vo);
								ir_tarval *const nc  = tarval_or(tarval_or(lnc, rnc), vnc);
								ir_tarval *const az  = tarval_or(vz, nc);
								ir_tarval *const ao  = tarval_andnot(vz, nc);

								if (tarval_is_null(tarval_andnot(one, ro))) {
									z = az;
									o = ao;
								} else {
									z = tarval_or(z, az);
									o = tarval_and(o, ao);
								}
							}
							lz = tarval_shl(lz, one);
							lo = tarval_shl(lo, one);
							rz = tarval_shr(rz, one);
							ro = tarval_shr(ro, one);
572
573
574
575
576
577
						}
					}
					break;
				}

				case iro_Minus: {
578
					/* -a = 0 - a */
579
					bitinfo   *const b   = get_bitinfo_recursive(get_Minus_op(irn));
580
581
					ir_tarval *const bz  = b->z;
					ir_tarval *const bo  = b->o;
Christoph Mallon's avatar
Christoph Mallon committed
582
583
584
585
586
587
588
					ir_tarval *const vz  = tarval_neg(bz);
					ir_tarval *const vo  = tarval_neg(bo);
					ir_tarval *const bnc = tarval_eor(bz, bo);
					ir_tarval *const vnc = tarval_eor(vz, vo);
					ir_tarval *const nc  = tarval_or(bnc, vnc);
					z = tarval_or(vz, nc);
					o = tarval_andnot(vz, nc);
589
					break;
590

591
592
593
				}

				case iro_And: {
594
595
					bitinfo *const l = get_bitinfo_recursive(get_And_left(irn));
					bitinfo *const r = get_bitinfo_recursive(get_And_right(irn));
596
597
598
599
600
601
					z = tarval_and(l->z, r->z);
					o = tarval_and(l->o, r->o);
					break;
				}

				case iro_Or: {
602
603
					bitinfo *const l = get_bitinfo_recursive(get_Or_left(irn));
					bitinfo *const r = get_bitinfo_recursive(get_Or_right(irn));
604
605
606
607
608
609
					z = tarval_or(l->z, r->z);
					o = tarval_or(l->o, r->o);
					break;
				}

				case iro_Eor: {
610
611
					bitinfo   *const l  = get_bitinfo_recursive(get_Eor_left(irn));
					bitinfo   *const r  = get_bitinfo_recursive(get_Eor_right(irn));
Christoph Mallon's avatar
Christoph Mallon committed
612
613
614
615
					ir_tarval *const lz = l->z;
					ir_tarval *const lo = l->o;
					ir_tarval *const rz = r->z;
					ir_tarval *const ro = r->o;
616
617
618
619
620
621
					z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
					o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
					break;
				}

				case iro_Not: {
622
					bitinfo *const b = get_bitinfo_recursive(get_Not_op(irn));
623
624
625
626
627
628
					z = tarval_not(b->o);
					o = tarval_not(b->z);
					break;
				}

				case iro_Conv: {
629
					bitinfo *const b = get_bitinfo_recursive(get_Conv_op(irn));
630
631
632
633
634
635
636
637
					if (b == NULL) // Happens when converting from float values.
						goto result_unknown;
					z = tarval_convert_to(b->z, m);
					o = tarval_convert_to(b->o, m);
					break;
				}

				case iro_Mux: {
638
639
640
					bitinfo *const bf = get_bitinfo_recursive(get_Mux_false(irn));
					bitinfo *const bt = get_bitinfo_recursive(get_Mux_true(irn));
					bitinfo *const c  = get_bitinfo_recursive(get_Mux_sel(irn));
641
642
643
644
645
646
647
648
649
650
651
652
653
654
					if (c->o == t) {
						z = bt->z;
						o = bt->o;
					} else if (c->z == f) {
						z = bf->z;
						o = bf->o;
					} else {
						z = tarval_or( bf->z, bt->z);
						o = tarval_and(bf->o, bt->o);
					}
					break;
				}

				case iro_Cmp: {
655
656
					bitinfo *const l = get_bitinfo_recursive(get_Cmp_left(irn));
					bitinfo *const r = get_bitinfo_recursive(get_Cmp_right(irn));
Matthias Braun's avatar
Matthias Braun committed
657
					if (l == NULL || r == NULL)
658
						goto result_unknown; // Cmp compares something we cannot evaluate.
Christoph Mallon's avatar
Christoph Mallon committed
659
660
661
662
					ir_tarval  *const lz       = l->z;
					ir_tarval  *const lo       = l->o;
					ir_tarval  *const rz       = r->z;
					ir_tarval  *const ro       = r->o;
Matthias Braun's avatar
Matthias Braun committed
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
					ir_relation const relation = get_Cmp_relation(irn);
					switch (relation) {
						case ir_relation_less_greater:
							if (!tarval_is_null(tarval_andnot(ro, lz)) ||
									!tarval_is_null(tarval_andnot(lo, rz))) {
								// At least one bit differs.
								z = o = t;
							} else if (lz == lo && rz == ro && lz == rz) {
								z = o = f;
							} else {
								goto result_unknown;
							}
							break;

						case ir_relation_equal:
							if (!tarval_is_null(tarval_andnot(ro, lz)) ||
									!tarval_is_null(tarval_andnot(lo, rz))) {
								// At least one bit differs.
								z = o = f;
							} else if (lz == lo && rz == ro && lz == rz) {
								z = o = t;
							} else {
								goto result_unknown;
							}
							break;

						case ir_relation_less_equal:
						case ir_relation_less:
							/* TODO handle negative values */
							if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
									tarval_is_negative(rz) || tarval_is_negative(ro))
								goto result_unknown;

							if (tarval_cmp(lz, ro) & relation) {
								/* Left upper bound is smaller(/equal) than right lower bound. */
								z = o = t;
							} else if (!(tarval_cmp(lo, rz) & relation)) {
								/* Left lower bound is not smaller(/equal) than right upper bound. */
								z = o = f;
							} else {
								goto result_unknown;
							}
							break;

						case ir_relation_greater_equal:
						case ir_relation_greater:
							/* TODO handle negative values */
							if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
									tarval_is_negative(rz) || tarval_is_negative(ro))
								goto result_unknown;

							if (!(tarval_cmp(lz, ro) & relation)) {
								/* Left upper bound is not greater(/equal) than right lower bound. */
								z = o = f;
							} else if (tarval_cmp(lo, rz) & relation) {
								/* Left lower bound is greater(/equal) than right upper bound. */
								z = o = t;
							} else {
								goto result_unknown;
							}
							break;

						default:
							goto cannot_analyse;
727
728
729
730
					}
					break;
				}

731
732
733
				case iro_Proj: {
					ir_node *const pred = get_Proj_pred(irn);
					if (is_Tuple(pred)) {
734
						unsigned       pn = get_Proj_num(irn);
735
						ir_node *const op = get_Tuple_pred(pred, pn);
736
						bitinfo *const b  = get_bitinfo_recursive(op);
737
738
739
740
741
742
						z = b->z;
						o = b->o;
						goto set_info;
					}
				}

743
744
745
746
				default: {
cannot_analyse:
					DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
result_unknown:
747
748
					z = get_mode_all_one(m);
					o = get_mode_null(m);
749
750
751
752
753
					break;
				}
			}
		}
	} else {
Matthias Braun's avatar
Matthias Braun committed
754
		return false;
755
756
	}

757
set_info:
758
759
760
	return set_bitinfo(irn, z, o);
}

761
762
763
static void trigger_users(ir_node const *irn);

static void trigger(ir_node const *const irn, ir_node const *const operand)
764
{
765
766
	(void)operand;

767
768
769
770
771
772
773
	bitinfo *const b = get_bitinfo_direct(irn);
	if (b && b->state == BITINFO_VALID) {
		DB((dbg, LEVEL_5, "%+F triggers %+F\n", operand, irn));
		b->state = BITINFO_UNSTABLE;
		trigger_users(irn);
	} else {
		DB((dbg, LEVEL_5, "%+F does not trigger %+F\n", operand, irn));
774
775
776
	}
}

777
static void trigger_users(ir_node const *const irn)
778
{
779
780
781
782
783
784
785
	if (is_Bad(irn))
		return;

	if (is_Block(irn)) {
		/* Blocks just trigger the jump nodes inside.  The value of all other nodes
		 * should not depend on the reachability of the block. */
		foreach_out_edge(irn, e) {
Christoph Mallon's avatar
Christoph Mallon committed
786
			ir_node *const src = get_edge_src_irn(e);
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
			if (get_irn_mode(src) == mode_X)
				trigger(src, irn);
		}
	} else if (get_irn_mode(irn) == mode_X) {
		if (!is_End(irn)) {
			/* When the state of a control flow node changes, not only trigger its
			 * successor blocks, but also the Phis in these blocks, because the Phis
			 * must reconsider this input path. */
			foreach_out_edge(irn, e) {
				ir_node *const src = get_edge_src_irn(e);
				if (is_Block(src)) {
					trigger(src, irn);
					foreach_out_edge(src, f) {
						ir_node *const phi = get_edge_src_irn(f);
						if (is_Phi(phi))
							trigger(phi, irn);
					}
				} else {
					assert(is_Tuple(src) && get_nodes_block(src) == get_nodes_block(irn));
				}
807
808
809
			}
		}
	} else {
810
811
		foreach_out_edge(irn, e) {
			ir_node* const src = get_edge_src_irn(e);
812
			if (get_irn_mode(src) == mode_T) {
813
814
815
				/* Trigger Projs of tuple nodes.  They might contain analysis information,
				 * but the tuple node does not. */
				trigger_users(src);
816
			} else {
817
				trigger(src, irn);
818
819
820
821
822
			}
		}
	}
}

823
static void calc_bitinfo(ir_node const *const irn, bitinfo *const b)
824
{
825
826
827
828
829
830
831
	do {
		b->state = BITINFO_IN_FLIGHT;
		bool const changed = transfer(irn);
		b->state = BITINFO_VALID;
		if (changed)
			trigger_users(irn);
	} while (b->state != BITINFO_VALID);
832
833
}

834
static void calc_bitinfo_walker(ir_node *const n, void *const env)
835
{
Matthias Braun's avatar
Matthias Braun committed
836
	(void)env;
837
838
839
840

	ir_mode *const mode = get_irn_mode(n);
	if (mode_is_intb(mode) || mode == mode_BB || mode == mode_X)
		get_bitinfo_recursive(n);
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
#if VERIFY_CONSTBITS
static void verify_constbits_walker(ir_node *const n, void *const env)
{
	bool *const failed = (bool*)env;

	bitinfo *const bi = get_bitinfo_direct(n);
	if (bi) {
		bitinfo const old = *bi;
		if (transfer(n)) {
			ir_fprintf(stderr, "---> no fixpoint for %+F\n", n);
			*bi     = old;
			*failed = true;
		}
	}
}

static void verify_constbits(ir_graph *const irg)
{
	bool failed = false;
	irg_walk_graph(irg, NULL, verify_constbits_walker, &failed);
	if (failed) {
		dump_ir_graph(irg, "verify-constbits");
		panic("verify constbits failed");
	}
}
#endif

Christoph Mallon's avatar
Christoph Mallon committed
870
void constbits_analyze(ir_graph *const irg)
871
{
yb9976's avatar
yb9976 committed
872
	FIRM_DBG_REGISTER(dbg, "firm.ana.constbits");
873
	DB((dbg, LEVEL_1, "---> activating constbits for %+F\n", irg));
874

875
	assure_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
876

Matthias Braun's avatar
Matthias Braun committed
877
878
	obstack_init(&irg->bitinfo.obst);
	ir_nodemap_init(&irg->bitinfo.map, irg);
879
	get_bitinfo_func = &get_bitinfo_recursive;
880
	irg_walk_graph(irg, NULL, calc_bitinfo_walker, NULL);
881
	get_bitinfo_func = &get_bitinfo_direct;
882
883
884
885

#if VERIFY_CONSTBITS
	verify_constbits(irg);
#endif
886
}
887

Christoph Mallon's avatar
Christoph Mallon committed
888
void constbits_clear(ir_graph *const irg)
889
{
890
	get_bitinfo_func = &get_bitinfo_null;
891
	ir_nodemap_destroy(&irg->bitinfo.map);
yb9976's avatar
yb9976 committed
892
	obstack_free(&irg->bitinfo.obst, NULL);
893
}