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