90562918c5d04c3d9427673a039056bf2f96270c
[libfirm] / ir / opt / fp-vrp.c
1 /*
2  * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Data-flow driven minimal fixpoint value range propagation
23  * @author  Christoph Mallon
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <stdbool.h>
29
30 #include "adt/pdeq.h"
31 #include "adt/obst.h"
32 #include "adt/xmalloc.h"
33 #include "debug.h"
34 #include "ircons.h"
35 #include "irdom.h"
36 #include "iredges.h"
37 #include "irgmod.h"
38 #include "irgraph.h"
39 #include "irgwalk.h"
40 #include "irnode.h"
41 #include "iroptimize.h"
42 #include "irtools.h"
43 #include "tv.h"
44 #include "irpass.h"
45 #include "irmemory.h"
46
47 /* TODO:
48  * - Implement cleared/set bit calculation for Add, Sub, Minus, Mul, Div, Mod, Shl, Shr, Shrs, Rotl
49  * - Implement min/max calculation for And, Eor, Or, Not, Conv, Shl, Shr, Shrs, Rotl, Mux
50  * - Implement min/max calculation for Add, Sub, Minus, Mul, Div, Mod, Conv, Shl, Shr, Shrs, Rotl, Mux
51  */
52
53 /* Tables of the cleared/set bit lattice
54  *
55  * Encoding of the lattice
56  * zo
57  * 00 0 zero
58  * 01 - impossible state, is zero /and/ one
59  * 10 T top, may be either zero or one
60  * 11 1 one
61  *
62  * S = Sum
63  * c = Carry
64  * D = Difference
65  * b = Borrow
66  *
67  * Not
68  * A ~
69  * 0 1
70  * 1 0
71  * T T
72  *
73  * Half adder, half subtractor, and, xor, or, Mux
74  * AB  Sc  Db  &  ^  |  M
75  * 00  00  00  0  0  0  0
76  * 01  10  11  0  1  1  T
77  * 0T  T0  TT  0  T  T  T
78  * 10  10  10  0  1  1  T
79  * 11  01  00  1  0  1  1
80  * 1T  TT  T0  T  T  1  T
81  * T0  T0  T0  0  T  T  T
82  * T1  TT  TT  T  T  1  T
83  * TT  TT  TT  T  T  T  T
84  *
85  * Full adder, full subtractor
86  * ABc-1  Sc  Db
87  * 000    00  00
88  * 001    10  11
89  * 00T    T0  TT
90  * 010    10  11
91  * 011    01  01
92  * 01T    TT  T1
93  * 0T0    T0  TT
94  * 0T1    TT  T1
95  * 0TT    TT  TT
96  * 100    10  10
97  * 101    01  00
98  * 10T    TT  T0
99  * 110    01  00
100  * 111    11  11
101  * 11T    T1  TT
102  * 1T0    TT  T0
103  * 1T1    T1  TT
104  * 1TT    TT  TT
105  * T00    T0  T0
106  * T01    TT  TT
107  * T0T    TT  TT
108  * T10    TT  TT
109  * T11    T1  T1
110  * T1T    TT  TT
111  * TT0    TT  TT
112  * TT1    TT  TT
113  * TTT    TT  TT
114  *
115  *
116  * Assume: Xmin <= Xmax and no overflow
117  * A + B = (Amin + Bmin, Amax + Bmax)
118  *    -A = (-Amax, -Amin)
119  * A - B = A + -B = (Amin (-B)min, Amax + (-B)max) = (Amin - Bmax, Amax - Bmin)
120  */
121
122 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
123
124 static struct obstack obst;
125
126 typedef struct bitinfo
127 {
128         ir_tarval* z; // safe zeroes, 0 = bit is zero,       1 = bit maybe is 1
129         ir_tarval* o; // safe ones,   0 = bit maybe is zero, 1 = bit is 1
130 } bitinfo;
131
132 typedef struct environment_t {
133         unsigned modified:1;     /**< Set, if the graph was modified. */
134 } environment_t;
135
136 static bool is_undefined(bitinfo const* const b)
137 {
138         return tarval_is_null(b->z) && tarval_is_all_one(b->o);
139 }
140
141 static inline bitinfo* get_bitinfo(ir_node const* const irn)
142 {
143         return (bitinfo*)get_irn_link(irn);
144 }
145
146 static int set_bitinfo(ir_node* const irn, ir_tarval* const z, ir_tarval* const o)
147 {
148         bitinfo* b = get_bitinfo(irn);
149         if (b == NULL) {
150                 b = OALLOCZ(&obst, bitinfo);
151                 set_irn_link(irn, b);
152         } else if (z == b->z && o == b->o) {
153                 return 0;
154         }
155         b->z = z;
156         b->o = o;
157         DB((dbg, LEVEL_3, "%+F: 0:%T 1:%T\n", irn, z, o));
158         return 1;
159 }
160
161 static int mode_is_intb(ir_mode const* const m)
162 {
163         return mode_is_int(m) || m == mode_b;
164 }
165
166 static int transfer(ir_node* const irn)
167 {
168         ir_tarval* const f = get_tarval_b_false();
169         ir_tarval* const t = get_tarval_b_true();
170         ir_mode*   const m = get_irn_mode(irn);
171         ir_tarval*       z;
172         ir_tarval*       o;
173
174         if (m == mode_X) {
175                 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
176
177                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
178
179                 /* Unreachble blocks might have no bitinfo. */
180                 if (b == NULL || b->z == f) {
181 unreachable_X:
182                         z = f;
183                         o = t;
184                 } else switch (get_irn_opcode(irn)) {
185                         case iro_Proj: {
186                                 ir_node* const pred = get_Proj_pred(irn);
187                                 if (is_Start(pred)) {
188                                         goto result_unknown_X;
189                                 } else if (is_Cond(pred)) {
190                                         ir_node*   const selector = get_Cond_selector(pred);
191                                         bitinfo*   const b        = get_bitinfo(selector);
192                                         if (is_undefined(b)) {
193                                                 goto unreachable_X;
194                                         } else if (get_irn_mode(selector) == mode_b) {
195                                                 if (b->z == b->o) {
196                                                         if ((b->z == t) == get_Proj_proj(irn)) {
197                                                                 z = o = t;
198                                                         } else {
199                                                                 z = o = f;
200                                                         }
201                                                 } else {
202                                                         goto result_unknown_X;
203                                                 }
204                                         } else {
205                                                 long const val = get_Proj_proj(irn);
206                                                 if (val != get_Cond_default_proj(pred)) {
207                                                         ir_tarval* const tv = new_tarval_from_long(val, get_irn_mode(selector));
208                                                         if (!tarval_is_null(tarval_andnot(tv, b->z)) ||
209                                                                         !tarval_is_null(tarval_andnot(b->o, tv))) {
210                                                                 // At least one bit differs.
211                                                                 z = o = f;
212 #if 0 // TODO must handle default Proj
213                                                         } else if (b->z == b->o && b->z == tv) {
214                                                                 z = o = t;
215 #endif
216                                                         } else {
217                                                                 goto result_unknown_X;
218                                                         }
219                                                 } else {
220                                                         goto cannot_analyse_X;
221                                                 }
222                                         }
223                                 } else {
224                                         goto cannot_analyse_X;
225                                 }
226                                 break;
227                         }
228
229                         case iro_Jmp:
230                                 goto result_unknown_X;
231
232                         default:
233 cannot_analyse_X:
234                                 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
235 result_unknown_X:
236                                 z = t;
237                                 o = f;
238                                 break;
239                 }
240         } else if (is_Block(irn)) {
241                 int       reachable = 0;
242                 int const arity     = get_Block_n_cfgpreds(irn);
243                 int       i;
244
245                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
246                 for (i = 0; i != arity; ++i) {
247                         bitinfo* const b = get_bitinfo(get_Block_cfgpred(irn, i));
248                         if (b != NULL && b->z == t) {
249                                 reachable = 1;
250                                 break;
251                         }
252                 }
253
254                 if (!reachable) {
255                         ir_graph *const irg = get_Block_irg(irn);
256                         reachable =
257                                 irn == get_irg_start_block(irg) ||
258                                 irn == get_irg_end_block(irg);
259                 }
260
261                 if (reachable) {
262                         z = t;
263                         o = f;
264                 } else {
265                         z = f;
266                         o = t;
267                 }
268         } else if (mode_is_intb(m)) {
269                 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
270
271                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
272
273                 if (b->z == f) {
274 undefined:
275                         z = get_tarval_null(m);
276                         o = get_tarval_all_one(m);
277                 } else if (is_Phi(irn)) {
278                         ir_node* const block = get_nodes_block(irn);
279                         int      const arity = get_Phi_n_preds(irn);
280                         int            i;
281
282                         z = get_tarval_null(m);
283                         o = get_tarval_all_one(m);
284                         for (i = 0; i != arity; ++i) {
285                                 bitinfo* const b_cfg = get_bitinfo(get_Block_cfgpred(block, i));
286                                 if (b_cfg != NULL && b_cfg->z != f) {
287                                         bitinfo* const b = get_bitinfo(get_Phi_pred(irn, i));
288                                         /* Only use input if it's not undefined. */
289                                         if (!is_undefined(b)) {
290                                                 z = tarval_or( z, b->z);
291                                                 o = tarval_and(o, b->o);
292                                         }
293                                 }
294                         }
295                 } else {
296                         int const arity = get_irn_arity(irn);
297                         int       i;
298
299                         /* Undefined if any input is undefined. */
300                         for (i = 0; i != arity; ++i) {
301                                 ir_node* const pred   = get_irn_n(irn, i);
302                                 bitinfo* const pred_b = get_bitinfo(pred);
303                                 if (pred_b != NULL && is_undefined(pred_b))
304                                         goto undefined;
305                         }
306
307                         switch (get_irn_opcode(irn)) {
308                                 case iro_Const: {
309                                         z = o = get_Const_tarval(irn);
310                                         break;
311                                 }
312
313                                 case iro_Confirm: {
314                                         ir_node* const v = get_Confirm_value(irn);
315                                         bitinfo* const b = get_bitinfo(v);
316                                         /* TODO Use bound and relation. */
317                                         z = b->z;
318                                         o = b->o;
319                                         break;
320                                 }
321
322                                 case iro_Shl: {
323                                         bitinfo*   const l  = get_bitinfo(get_Shl_left(irn));
324                                         bitinfo*   const r  = get_bitinfo(get_Shl_right(irn));
325                                         ir_tarval* const rz = r->z;
326                                         if (rz == r->o) {
327                                                 z = tarval_shl(l->z, rz);
328                                                 o = tarval_shl(l->o, rz);
329                                         } else {
330                                                 goto cannot_analyse;
331                                         }
332                                         break;
333                                 }
334
335                                 case iro_Shr: {
336                                         bitinfo*   const l  = get_bitinfo(get_Shr_left(irn));
337                                         bitinfo*   const r  = get_bitinfo(get_Shr_right(irn));
338                                         ir_tarval* const rz = r->z;
339                                         if (rz == r->o) {
340                                                 z = tarval_shr(l->z, rz);
341                                                 o = tarval_shr(l->o, rz);
342                                         } else {
343                                                 goto cannot_analyse;
344                                         }
345                                         break;
346                                 }
347
348                                 case iro_Shrs: {
349                                         bitinfo*   const l  = get_bitinfo(get_Shrs_left(irn));
350                                         bitinfo*   const r  = get_bitinfo(get_Shrs_right(irn));
351                                         ir_tarval* const rz = r->z;
352                                         if (rz == r->o) {
353                                                 z = tarval_shrs(l->z, rz);
354                                                 o = tarval_shrs(l->o, rz);
355                                         } else {
356                                                 goto cannot_analyse;
357                                         }
358                                         break;
359                                 }
360
361                                 case iro_Rotl: {
362                                         bitinfo*   const l  = get_bitinfo(get_Rotl_left(irn));
363                                         bitinfo*   const r  = get_bitinfo(get_Rotl_right(irn));
364                                         ir_tarval* const rz = r->z;
365                                         if (rz == r->o) {
366                                                 z = tarval_rotl(l->z, rz);
367                                                 o = tarval_rotl(l->o, rz);
368                                         } else {
369                                                 goto cannot_analyse;
370                                         }
371                                         break;
372                                 }
373
374                                 case iro_Add: {
375                                         bitinfo*   const l  = get_bitinfo(get_Add_left(irn));
376                                         bitinfo*   const r  = get_bitinfo(get_Add_right(irn));
377                                         ir_tarval* const lz = l->z;
378                                         ir_tarval* const lo = l->o;
379                                         ir_tarval* const rz = r->z;
380                                         ir_tarval* const ro = r->o;
381                                         if (lz == lo && rz == ro) {
382                                                 z = o = tarval_add(lz, rz);
383                                         } else {
384                                                 // TODO improve: can only do lower disjoint bits
385                                                 /* Determine where any of the operands has zero bits, i.e. where no
386                                                  * carry out is generated if there is not carry in */
387                                                 ir_tarval* const no_c_in_no_c_out = tarval_and(lz, rz);
388                                                 /* Generate a mask of the lower consecutive zeroes: x | -x.  In this
389                                                  * range the addition is disjoint and therefore Add behaves like Or.
390                                                  */
391                                                 ir_tarval* const low_zero_mask = tarval_or(no_c_in_no_c_out, tarval_neg(no_c_in_no_c_out));
392                                                 ir_tarval* const low_one_mask  = tarval_not(low_zero_mask);
393                                                 z = tarval_or( tarval_or(lz, rz), low_zero_mask);
394                                                 o = tarval_and(tarval_or(lo, ro), low_one_mask);
395                                         }
396                                         break;
397                                 }
398
399                                 case iro_Sub: {
400                                         bitinfo* const l = get_bitinfo(get_Sub_left(irn));
401                                         bitinfo* const r = get_bitinfo(get_Sub_right(irn));
402                                         if (l != NULL && r != NULL) { // Sub might subtract pointers.
403                                                 ir_tarval* const lz = l->z;
404                                                 ir_tarval* const lo = l->o;
405                                                 ir_tarval* const rz = r->z;
406                                                 ir_tarval* const ro = r->o;
407                                                 if (lz == lo && rz == ro) {
408                                                         z = o = tarval_sub(lz, rz, NULL);
409                                                 } else if (tarval_is_null(tarval_andnot(rz, lo))) {
410                                                         /* Every possible one of the subtrahend is backed by a safe one of the
411                                                          * minuend, i.e. there are no borrows. */
412                                                         // TODO extend no-borrow like carry for Add above
413                                                         z = tarval_andnot(lz, ro);
414                                                         o = tarval_andnot(lo, rz);
415                                                 } else {
416                                                         goto cannot_analyse;
417                                                 }
418                                         } else {
419                                                 goto cannot_analyse;
420                                         }
421                                         break;
422                                 }
423
424                                 case iro_Mul: {
425                                         bitinfo*   const l  = get_bitinfo(get_Mul_left(irn));
426                                         bitinfo*   const r  = get_bitinfo(get_Mul_right(irn));
427                                         ir_tarval* const lz = l->z;
428                                         ir_tarval* const lo = l->o;
429                                         ir_tarval* const rz = r->z;
430                                         ir_tarval* const ro = r->o;
431                                         if (lz == lo && rz == ro) {
432                                                 z = o = tarval_mul(lz, rz);
433                                         } else {
434                                                 // TODO improve
435                                                 // Determine safe lower zeroes: x | -x.
436                                                 ir_tarval* const lzn = tarval_or(lz, tarval_neg(lz));
437                                                 ir_tarval* const rzn = tarval_or(rz, tarval_neg(rz));
438                                                 // Concatenate safe lower zeroes.
439                                                 if (tarval_cmp(lzn, rzn) == ir_relation_less) {
440                                                         z = tarval_mul(tarval_eor(lzn, tarval_shl(lzn, get_tarval_one(m))), rzn);
441                                                 } else {
442                                                         z = tarval_mul(tarval_eor(rzn, tarval_shl(rzn, get_tarval_one(m))), lzn);
443                                                 }
444                                                 o = get_tarval_null(m);
445                                         }
446                                         break;
447                                 }
448
449                                 case iro_Minus: {
450                                         bitinfo* const b = get_bitinfo(get_Minus_op(irn));
451                                         if (b->z == b->o) {
452                                                 z = o = tarval_neg(b->z);
453                                         } else {
454                                                 goto cannot_analyse;
455                                         }
456                                         break;
457                                 }
458
459                                 case iro_And: {
460                                         bitinfo* const l = get_bitinfo(get_And_left(irn));
461                                         bitinfo* const r = get_bitinfo(get_And_right(irn));
462                                         z = tarval_and(l->z, r->z);
463                                         o = tarval_and(l->o, r->o);
464                                         break;
465                                 }
466
467                                 case iro_Or: {
468                                         bitinfo* const l = get_bitinfo(get_Or_left(irn));
469                                         bitinfo* const r = get_bitinfo(get_Or_right(irn));
470                                         z = tarval_or(l->z, r->z);
471                                         o = tarval_or(l->o, r->o);
472                                         break;
473                                 }
474
475                                 case iro_Eor: {
476                                         bitinfo*   const l  = get_bitinfo(get_Eor_left(irn));
477                                         bitinfo*   const r  = get_bitinfo(get_Eor_right(irn));
478                                         ir_tarval* const lz = l->z;
479                                         ir_tarval* const lo = l->o;
480                                         ir_tarval* const rz = r->z;
481                                         ir_tarval* const ro = r->o;
482                                         z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
483                                         o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
484                                         break;
485                                 }
486
487                                 case iro_Not: {
488                                         bitinfo* const b = get_bitinfo(get_Not_op(irn));
489                                         z = tarval_not(b->o);
490                                         o = tarval_not(b->z);
491                                         break;
492                                 }
493
494                                 case iro_Conv: {
495                                         bitinfo* const b = get_bitinfo(get_Conv_op(irn));
496                                         if (b == NULL) // Happens when converting from float values.
497                                                 goto result_unknown;
498                                         z = tarval_convert_to(b->z, m);
499                                         o = tarval_convert_to(b->o, m);
500                                         break;
501                                 }
502
503                                 case iro_Mux: {
504                                         bitinfo* const bf = get_bitinfo(get_Mux_false(irn));
505                                         bitinfo* const bt = get_bitinfo(get_Mux_true(irn));
506                                         bitinfo* const c  = get_bitinfo(get_Mux_sel(irn));
507                                         if (c->o == t) {
508                                                 z = bt->z;
509                                                 o = bt->o;
510                                         } else if (c->z == f) {
511                                                 z = bf->z;
512                                                 o = bf->o;
513                                         } else {
514                                                 z = tarval_or( bf->z, bt->z);
515                                                 o = tarval_and(bf->o, bt->o);
516                                         }
517                                         break;
518                                 }
519
520                                 case iro_Cmp: {
521                                         bitinfo* const l = get_bitinfo(get_Cmp_left(irn));
522                                         bitinfo* const r = get_bitinfo(get_Cmp_right(irn));
523                                         if (l == NULL || r == NULL) {
524                                                 goto result_unknown; // Cmp compares something we cannot evaluate.
525                                         } else {
526                                                 ir_tarval*  const lz       = l->z;
527                                                 ir_tarval*  const lo       = l->o;
528                                                 ir_tarval*  const rz       = r->z;
529                                                 ir_tarval*  const ro       = r->o;
530                                                 ir_relation const relation = get_Cmp_relation(irn);
531                                                 switch (relation) {
532                                                         case ir_relation_less_greater:
533                                                                 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
534                                                                                 !tarval_is_null(tarval_andnot(lo, rz))) {
535                                                                         // At least one bit differs.
536                                                                         z = o = t;
537                                                                 } else if (lz == lo && rz == ro && lz == rz) {
538                                                                         z = o = f;
539                                                                 } else {
540                                                                         goto result_unknown;
541                                                                 }
542                                                                 break;
543
544                                                         case ir_relation_equal:
545                                                                 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
546                                                                                 !tarval_is_null(tarval_andnot(lo, rz))) {
547                                                                         // At least one bit differs.
548                                                                         z = o = f;
549                                                                 } else if (lz == lo && rz == ro && lz == rz) {
550                                                                         z = o = t;
551                                                                 } else {
552                                                                         goto result_unknown;
553                                                                 }
554                                                                 break;
555
556                                                         case ir_relation_less_equal:
557                                                         case ir_relation_less:
558                                                                 /* TODO handle negative values */
559                                                                 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
560                                                                                 tarval_is_negative(rz) || tarval_is_negative(ro))
561                                                                         goto result_unknown;
562
563                                                                 if (tarval_cmp(lz, ro) & relation) {
564                                                                         /* Left upper bound is smaller(/equal) than right lower bound. */
565                                                                         z = o = t;
566                                                                 } else if (!(tarval_cmp(lo, rz) & relation)) {
567                                                                         /* Left lower bound is not smaller(/equal) than right upper bound. */
568                                                                         z = o = f;
569                                                                 } else {
570                                                                         goto result_unknown;
571                                                                 }
572                                                                 break;
573
574                                                         case ir_relation_greater_equal:
575                                                         case ir_relation_greater:
576                                                                 /* TODO handle negative values */
577                                                                 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
578                                                                                 tarval_is_negative(rz) || tarval_is_negative(ro))
579                                                                         goto result_unknown;
580
581                                                                 if (!(tarval_cmp(lz, ro) & relation)) {
582                                                                         /* Left upper bound is not greater(/equal) than right lower bound. */
583                                                                         z = o = f;
584                                                                 } else if (tarval_cmp(lo, rz) & relation) {
585                                                                         /* Left lower bound is greater(/equal) than right upper bound. */
586                                                                         z = o = t;
587                                                                 } else {
588                                                                         goto result_unknown;
589                                                                 }
590                                                                 break;
591
592                                                         default:
593                                                                 goto cannot_analyse;
594                                                 }
595                                         }
596                                         break;
597                                 }
598
599                                 default: {
600 cannot_analyse:
601                                         DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
602 result_unknown:
603                                         z = get_tarval_all_one(m);
604                                         o = get_tarval_null(m);
605                                         break;
606                                 }
607                         }
608                 }
609         } else {
610                 return 0;
611         }
612
613         return set_bitinfo(irn, z, o);
614 }
615
616 static void first_round(ir_node* const irn, void* const env)
617 {
618         pdeq* const q = (pdeq*)env;
619
620         transfer(irn);
621         if (is_Phi(irn) || is_Block(irn)) {
622                 /* Only Phis (and their users) need another round, if we did not have
623                  * information about all their inputs in the first round, i.e. in loops. */
624                 /* TODO inserts all Phis, should only insert Phis, which did no have all
625                  * predecessors available */
626                 pdeq_putr(q, irn);
627         }
628 }
629
630 static void apply_result(ir_node* const irn, void* ctx)
631 {
632         environment_t* env = (environment_t*)ctx;
633         ir_node*       block;
634         bitinfo*       block_b;
635         bitinfo*       b;
636         ir_tarval*     z;
637         ir_tarval*     o;
638
639         if (is_Block(irn)) {
640                 block_b = get_bitinfo(irn);
641                 /* Trivially unreachable blocks have no info. */
642                 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
643                         exchange(irn, get_irg_bad(get_Block_irg(irn)));
644                         env->modified = 1;
645                 }
646                 return;
647         }
648
649         block   = get_nodes_block(irn);
650         block_b = get_bitinfo(block);
651         /* Trivially unreachable blocks have no info. */
652         if (block_b == NULL || block_b->z == get_tarval_b_false()) {
653                 /* Unreachable blocks might be replaced before the nodes in them. */
654                 exchange(irn, is_Bad(block) ? block : get_irg_bad(get_Block_irg(block)));
655                 env->modified = 1;
656                 return;
657         }
658
659         b = get_bitinfo(irn);
660         if (!b) return;
661         if (is_Const(irn)) return; // It cannot get any better than a Const.
662
663         z = b->z;
664         o = b->o;
665         // Only display information if we could find out anything about the value.
666         DEBUG_ONLY(if (!tarval_is_all_one(z) || !tarval_is_null(o)))
667                 DB((dbg, LEVEL_2, "%+F: 0:%T 1:%T%s\n", irn, z, o, z == o ? " --- constant" : ""));
668
669         // Replace node with constant value by Const.
670         if (z == o) {
671                 ir_mode* const m = get_irn_mode(irn);
672                 ir_node*       n;
673                 if (mode_is_intb(m)) {
674                         ir_graph *irg = get_irn_irg(irn);
675                         n = new_r_Const(irg, z);
676                 } else if (m == mode_X) {
677                         ir_graph* const irg = get_Block_irg(block);
678                         if (z == get_tarval_b_true()) {
679                                 // Might produce an endless loop, so keep the block.
680                                 add_End_keepalive(get_irg_end(irg), block);
681                                 n = new_r_Jmp(block);
682                         } else {
683                                 n = new_r_Bad(irg);
684                                 /* Transferring analysis information to the bad node makes it a
685                                  * candidate for replacement. */
686                                 goto exchange_only;
687                         }
688                 } else {
689                         return;
690                 }
691                 set_irn_link(n, b);
692 exchange_only:
693                 exchange(irn, n);
694                 env->modified = 1;
695         }
696
697         switch (get_irn_opcode(irn)) {
698                 case iro_And: {
699                         ir_node*       const l  = get_And_left(irn);
700                         ir_node*       const r  = get_And_right(irn);
701                         bitinfo const* const bl = get_bitinfo(l);
702                         bitinfo const* const br = get_bitinfo(r);
703                         if (bl->z == bl->o) {
704                                 if (tarval_is_null(tarval_andnot(br->z, bl->z))) {
705                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
706                                         exchange(irn, r);
707                                         env->modified = 1;
708                                 }
709                         } else if (br->z == br->o) {
710                                 if (tarval_is_null(tarval_andnot(bl->z, br->z))) {
711                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
712                                         exchange(irn, l);
713                                         env->modified = 1;
714                                 }
715                         }
716                         break;
717                 }
718
719                 case iro_Or: {
720                         ir_node*       const l  = get_Or_left(irn);
721                         ir_node*       const r  = get_Or_right(irn);
722                         bitinfo const* const bl = get_bitinfo(l);
723                         bitinfo const* const br = get_bitinfo(r);
724                         if (bl->z == bl->o) {
725                                 if (tarval_is_null(tarval_andnot(bl->o, br->o))) {
726                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
727                                         exchange(irn, r);
728                                         env->modified = 1;
729                                 }
730                         } else if (br->z == br->o) {
731                                 if (tarval_is_null(tarval_andnot(br->o, bl->o))) {
732                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
733                                         exchange(irn, l);
734                                         env->modified = 1;
735                                 }
736                         }
737                         break;
738                 }
739         }
740 }
741
742 static void queue_users(pdeq* const q, ir_node* const n)
743 {
744         if (get_irn_mode(n) == mode_X) {
745                 /* When the state of a control flow node changes, not only queue its
746                  * successor blocks, but also the Phis in these blocks, because the Phis
747                  * must reconsider this input path. */
748                 ir_edge_t const* e;
749                 foreach_out_edge(n, e) {
750                         ir_node*  const  src = get_edge_src_irn(e);
751                         pdeq_putr(q, src);
752                         /* should always be a block */
753                         if (is_Block(src)) {
754                                 ir_node *phi;
755                                 for (phi = get_Block_phis(src); phi; phi = get_Phi_next(phi))
756                                         pdeq_putr(q, phi);
757                         }
758                 }
759         } else {
760                 ir_edge_t const* e;
761                 foreach_out_edge(n, e) {
762                         ir_node* const src = get_edge_src_irn(e);
763                         if (get_irn_mode(src) == mode_T) {
764                                 queue_users(q, src);
765                         } else {
766                                 pdeq_putr(q, src);
767                         }
768                 }
769         }
770 }
771
772 static void clear_links(ir_node *irn, void *env)
773 {
774         (void) env;
775         set_irn_link(irn, NULL);
776         if (is_Block(irn))
777                 set_Block_phis(irn, NULL);
778 }
779
780 static void build_phi_lists(ir_node *irn, void *env)
781 {
782         (void) env;
783         if (is_Phi(irn))
784                 add_Block_phi(get_nodes_block(irn), irn);
785 }
786
787 void fixpoint_vrp(ir_graph* const irg)
788 {
789         environment_t env;
790
791         FIRM_DBG_REGISTER(dbg, "firm.opt.fp-vrp");
792         DB((dbg, LEVEL_1, "===> Performing constant propagation on %+F\n", irg));
793
794         obstack_init(&obst);
795
796         /* HACK: to avoid finding dead code */
797         edges_deactivate(irg);
798         edges_activate(irg);
799
800         edges_assure(irg);
801         assure_doms(irg);
802
803         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
804
805         {
806                 pdeq* const q = new_pdeq();
807
808                 /* We need this extra step because the dom tree does not contain unreachable
809                    blocks in Firm. Moreover build phi list. */
810                 irg_walk_anchors(irg, clear_links, build_phi_lists, NULL);
811
812                 { ir_tarval* const f = get_tarval_b_false();
813                         ir_tarval* const t = get_tarval_b_true();
814                         set_bitinfo(get_irg_bad(irg),       f, t); /* Undefined. */
815                         set_bitinfo(get_irg_end_block(irg), t, f); /* Reachable. */
816                 }
817
818                 /* TODO Improve iteration order. Best is reverse postorder in data flow
819                  * direction and respecting loop nesting for fastest convergence. */
820                 irg_walk_blkwise_dom_top_down(irg, NULL, first_round, q);
821
822                 while (!pdeq_empty(q)) {
823                         ir_node* const n = (ir_node*)pdeq_getl(q);
824                         if (transfer(n))
825                                 queue_users(q, n);
826                 }
827
828                 del_pdeq(q);
829         }
830
831         DB((dbg, LEVEL_2, "---> Applying analysis results\n"));
832         env.modified = 0;
833         irg_walk_graph(irg, NULL, apply_result, &env);
834
835         if (env.modified) {
836                 /* control flow might changed */
837                 set_irg_outs_inconsistent(irg);
838                 set_irg_extblk_inconsistent(irg);
839                 set_irg_doms_inconsistent(irg);
840                 set_irg_loopinfo_inconsistent(irg);
841                 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
842         }
843
844         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
845
846         obstack_free(&obst, NULL);
847 }
848
849 ir_graph_pass_t *fixpoint_vrp_irg_pass(const char *name)
850 {
851         return def_graph_pass(name ? name : "fixpoint_vrp", fixpoint_vrp);
852 }