14cf316ad671f51713d06dd1fc4b6d03c2d2f915
[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_tarval* const f = get_tarval_b_false();
162         ir_tarval* const t = get_tarval_b_true();
163         ir_mode*   const m = get_irn_mode(irn);
164         ir_tarval*       z;
165         ir_tarval*       o;
166
167         if (m == mode_X) {
168                 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
169
170                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
171
172                 if (b->z == f && b->o == f) {
173                         z = f;
174                         o = f;
175                 } else switch (get_irn_opcode(irn)) {
176                         case iro_Proj: {
177                                 ir_node* const pred = get_Proj_pred(irn);
178                                 if (is_Start(pred)) {
179                                         goto result_unknown_X;
180                                 } else if (is_Cond(pred)) {
181                                         ir_node*   const selector = get_Cond_selector(pred);
182                                         bitinfo*   const b        = get_bitinfo(selector);
183                                         ir_tarval* const bz       = b->z;
184                                         ir_tarval* const bo       = b->o;
185                                         if (get_irn_mode(selector) == mode_b) {
186                                                 if (bz == bo) {
187                                                         if ((bz == t) == get_Proj_proj(irn)) {
188                                                                 z = o = t;
189                                                         } else {
190                                                                 z = o = f;
191                                                         }
192                                                 } else {
193                                                         goto result_unknown_X;
194                                                 }
195                                         } else {
196                                                 long const val = get_Proj_proj(irn);
197                                                 if (val != get_Cond_default_proj(pred)) {
198                                                         ir_tarval* const tv = new_tarval_from_long(val, get_irn_mode(selector));
199                                                         if (!tarval_is_null(tarval_andnot(tv, bz)) ||
200                                                                         !tarval_is_null(tarval_andnot(bo, tv))) {
201                                                                 // At least one bit differs.
202                                                                 z = o = f;
203 #if 0 // TODO must handle default Proj
204                                                         } else if (bz == bo && bz == tv) {
205                                                                 z = o = t;
206 #endif
207                                                         } else {
208                                                                 goto result_unknown_X;
209                                                         }
210                                                 } else {
211                                                         goto cannot_analyse_X;
212                                                 }
213                                         }
214                                 } else {
215                                         goto cannot_analyse_X;
216                                 }
217                                 break;
218                         }
219
220                         case iro_Jmp:
221                                 goto result_unknown_X;
222
223                         default:
224 cannot_analyse_X:
225                                 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
226 result_unknown_X:
227                                 z = t;
228                                 o = f;
229                                 break;
230                 }
231         } else if (is_Block(irn)) {
232                 int       reachable = 0;
233                 int const arity     = get_Block_n_cfgpreds(irn);
234                 int       i;
235
236                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
237                 for (i = 0; i != arity; ++i) {
238                         bitinfo* const b = get_bitinfo(get_Block_cfgpred(irn, i));
239                         if (b != NULL && b->z == t) {
240                                 reachable = 1;
241                                 break;
242                         }
243                 }
244
245                 if (!reachable) {
246                         ir_graph *const irg = get_Block_irg(irn);
247                         reachable =
248                                 irn == get_irg_start_block(irg) ||
249                                 irn == get_irg_end_block(irg);
250                 }
251
252                 o = f;
253                 z = reachable ? t : f;
254         } else if (mode_is_intb(m)) {
255                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
256                 switch (get_irn_opcode(irn)) {
257                         case iro_Const: {
258                                 z = o = get_Const_tarval(irn);
259                                 break;
260                         }
261
262                         case iro_Confirm: {
263                                 ir_node* const v = get_Confirm_value(irn);
264                                 bitinfo* const b = get_bitinfo(v);
265                                 /* TODO Use bound and relation. */
266                                 z = b->z;
267                                 o = b->o;
268                                 break;
269                         }
270
271                         case iro_Shl: {
272                                 bitinfo*   const l  = get_bitinfo(get_Shl_left(irn));
273                                 bitinfo*   const r  = get_bitinfo(get_Shl_right(irn));
274                                 ir_tarval* const rz = r->z;
275                                 if (rz == r->o) {
276                                         z = tarval_shl(l->z, rz);
277                                         o = tarval_shl(l->o, rz);
278                                 } else {
279                                         goto cannot_analyse;
280                                 }
281                                 break;
282                         }
283
284                         case iro_Shr: {
285                                 bitinfo*   const l  = get_bitinfo(get_Shr_left(irn));
286                                 bitinfo*   const r  = get_bitinfo(get_Shr_right(irn));
287                                 ir_tarval* const rz = r->z;
288                                 if (rz == r->o) {
289                                         z = tarval_shr(l->z, rz);
290                                         o = tarval_shr(l->o, rz);
291                                 } else {
292                                         goto cannot_analyse;
293                                 }
294                                 break;
295                         }
296
297                         case iro_Shrs: {
298                                 bitinfo*   const l  = get_bitinfo(get_Shrs_left(irn));
299                                 bitinfo*   const r  = get_bitinfo(get_Shrs_right(irn));
300                                 ir_tarval* const rz = r->z;
301                                 if (rz == r->o) {
302                                         z = tarval_shrs(l->z, rz);
303                                         o = tarval_shrs(l->o, rz);
304                                 } else {
305                                         goto cannot_analyse;
306                                 }
307                                 break;
308                         }
309
310                         case iro_Rotl: {
311                                 bitinfo*   const l  = get_bitinfo(get_Rotl_left(irn));
312                                 bitinfo*   const r  = get_bitinfo(get_Rotl_right(irn));
313                                 ir_tarval* const rz = r->z;
314                                 if (rz == r->o) {
315                                         z = tarval_rotl(l->z, rz);
316                                         o = tarval_rotl(l->o, rz);
317                                 } else {
318                                         goto cannot_analyse;
319                                 }
320                                 break;
321                         }
322
323                         case iro_Add: {
324                                 bitinfo*   const l  = get_bitinfo(get_Add_left(irn));
325                                 bitinfo*   const r  = get_bitinfo(get_Add_right(irn));
326                                 ir_tarval* const lz = l->z;
327                                 ir_tarval* const lo = l->o;
328                                 ir_tarval* const rz = r->z;
329                                 ir_tarval* const ro = r->o;
330                                 if (lz == lo && rz == ro) {
331                                         z = o = tarval_add(lz, rz);
332                                 } else {
333                                         // TODO improve: can only do lower disjoint bits
334                                         /* Determine where any of the operands has zero bits, i.e. where no
335                                          * carry out is generated if there is not carry in */
336                                         ir_tarval* const no_c_in_no_c_out = tarval_and(lz, rz);
337                                         /* Generate a mask of the lower consecutive zeroes: x | -x.  In this
338                                          * range the addition is disjoint and therefore Add behaves like Or.
339                                          */
340                                         ir_tarval* const low_zero_mask = tarval_or(no_c_in_no_c_out, tarval_neg(no_c_in_no_c_out));
341                                         ir_tarval* const low_one_mask  = tarval_not(low_zero_mask);
342                                         z = tarval_or( tarval_or(lz, rz), low_zero_mask);
343                                         o = tarval_and(tarval_or(lo, ro), low_one_mask);
344                                 }
345                                 break;
346                         }
347
348                         case iro_Sub: {
349                                 bitinfo* const l = get_bitinfo(get_Sub_left(irn));
350                                 bitinfo* const r = get_bitinfo(get_Sub_right(irn));
351                                 if (l != NULL && r != NULL) { // Sub might subtract pointers.
352                                         ir_tarval* const lz = l->z;
353                                         ir_tarval* const lo = l->o;
354                                         ir_tarval* const rz = r->z;
355                                         ir_tarval* const ro = r->o;
356                                         if (lz == lo && rz == ro) {
357                                                 z = o = tarval_sub(lz, rz, NULL);
358                                         } else if (tarval_is_null(tarval_andnot(rz, lo))) {
359                                                 /* Every possible one of the subtrahend is backed by a safe one of the
360                                                  * minuend, i.e. there are no borrows. */
361                                                 // TODO extend no-borrow like carry for Add above
362                                                 z = tarval_andnot(lz, ro);
363                                                 o = tarval_andnot(lo, rz);
364                                         } else {
365                                                 goto cannot_analyse;
366                                         }
367                                 } else {
368                                         goto cannot_analyse;
369                                 }
370                                 break;
371                         }
372
373                         case iro_Mul: {
374                                 bitinfo*   const l  = get_bitinfo(get_Mul_left(irn));
375                                 bitinfo*   const r  = get_bitinfo(get_Mul_right(irn));
376                                 ir_tarval* const lz = l->z;
377                                 ir_tarval* const lo = l->o;
378                                 ir_tarval* const rz = r->z;
379                                 ir_tarval* const ro = r->o;
380                                 if (lz == lo && rz == ro) {
381                                         z = o = tarval_mul(lz, rz);
382                                 } else {
383                                         // TODO improve
384                                         // Determine safe lower zeroes: x | -x.
385                                         ir_tarval* const lzn = tarval_or(lz, tarval_neg(lz));
386                                         ir_tarval* const rzn = tarval_or(rz, tarval_neg(rz));
387                                         // Concatenate safe lower zeroes.
388                                         if (tarval_cmp(lzn, rzn) == ir_relation_less) {
389                                                 z = tarval_mul(tarval_eor(lzn, tarval_shl(lzn, get_tarval_one(m))), rzn);
390                                         } else {
391                                                 z = tarval_mul(tarval_eor(rzn, tarval_shl(rzn, get_tarval_one(m))), lzn);
392                                         }
393                                         o = get_tarval_null(m);
394                                 }
395                                 break;
396                         }
397
398                         case iro_Minus: {
399                                 bitinfo* const b = get_bitinfo(get_Minus_op(irn));
400                                 if (b->z == b->o) {
401                                         z = o = tarval_neg(b->z);
402                                 } else {
403                                         goto cannot_analyse;
404                                 }
405                                 break;
406                         }
407
408                         case iro_And: {
409                                 bitinfo* const l = get_bitinfo(get_And_left(irn));
410                                 bitinfo* const r = get_bitinfo(get_And_right(irn));
411                                 z = tarval_and(l->z, r->z);
412                                 o = tarval_and(l->o, r->o);
413                                 break;
414                         }
415
416                         case iro_Or: {
417                                 bitinfo* const l = get_bitinfo(get_Or_left(irn));
418                                 bitinfo* const r = get_bitinfo(get_Or_right(irn));
419                                 z = tarval_or(l->z, r->z);
420                                 o = tarval_or(l->o, r->o);
421                                 break;
422                         }
423
424                         case iro_Eor: {
425                                 bitinfo*   const l  = get_bitinfo(get_Eor_left(irn));
426                                 bitinfo*   const r  = get_bitinfo(get_Eor_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                                 z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
432                                 o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
433                                 break;
434                         }
435
436                         case iro_Not: {
437                                 bitinfo* const b = get_bitinfo(get_Not_op(irn));
438                                 z = tarval_not(b->o);
439                                 o = tarval_not(b->z);
440                                 break;
441                         }
442
443                         case iro_Conv: {
444                                 bitinfo* const b = get_bitinfo(get_Conv_op(irn));
445                                 if (b == NULL) // Happens when converting from float values.
446                                         goto result_unknown;
447                                 z = tarval_convert_to(b->z, m);
448                                 o = tarval_convert_to(b->o, m);
449                                 break;
450                         }
451
452                         case iro_Mux: {
453                                 bitinfo* const bf = get_bitinfo(get_Mux_false(irn));
454                                 bitinfo* const bt = get_bitinfo(get_Mux_true(irn));
455                                 bitinfo* const c  = get_bitinfo(get_Mux_sel(irn));
456                                 if (c->o == t) {
457                                         z = bt->z;
458                                         o = bt->o;
459                                 } else if (c->z == f) {
460                                         z = bf->z;
461                                         o = bf->o;
462                                 } else {
463                                         z = tarval_or( bf->z, bt->z);
464                                         o = tarval_and(bf->o, bt->o);
465                                 }
466                                 break;
467                         }
468
469                         case iro_Phi: {
470                                 ir_node* const block = get_nodes_block(irn);
471                                 int      const arity = get_Phi_n_preds(irn);
472                                 int            i;
473
474                                 z = get_tarval_null(m);
475                                 o = get_tarval_all_one(m);
476                                 for (i = 0; i != arity; ++i) {
477                                         bitinfo* const b_cfg = get_bitinfo(get_Block_cfgpred(block, i));
478                                         if (b_cfg != NULL && b_cfg->z != f) {
479                                                 bitinfo* const b = get_bitinfo(get_Phi_pred(irn, i));
480                                                 z = tarval_or( z, b->z);
481                                                 o = tarval_and(o, b->o);
482                                         }
483                                 }
484                                 break;
485                         }
486
487                         case iro_Cmp: {
488                                 bitinfo* const l = get_bitinfo(get_Cmp_left(irn));
489                                 bitinfo* const r = get_bitinfo(get_Cmp_right(irn));
490                                 if (l == NULL || r == NULL) {
491                                         goto result_unknown; // Cmp compares something we cannot evaluate.
492                                 } else {
493                                         ir_tarval*  const lz       = l->z;
494                                         ir_tarval*  const lo       = l->o;
495                                         ir_tarval*  const rz       = r->z;
496                                         ir_tarval*  const ro       = r->o;
497                                         ir_relation const relation = get_Cmp_relation(irn);
498                                         switch (relation) {
499                                                 case ir_relation_less_greater:
500                                                         if (!tarval_is_null(tarval_andnot(ro, lz)) ||
501                                                                         !tarval_is_null(tarval_andnot(lo, rz))) {
502                                                                 // At least one bit differs.
503                                                                 z = o = t;
504                                                         } else if (lz == lo && rz == ro && lz == rz) {
505                                                                 z = o = f;
506                                                         } else {
507                                                                 goto result_unknown;
508                                                         }
509                                                         break;
510
511                                                 case ir_relation_equal:
512                                                         if (!tarval_is_null(tarval_andnot(ro, lz)) ||
513                                                                         !tarval_is_null(tarval_andnot(lo, rz))) {
514                                                                 // At least one bit differs.
515                                                                 z = o = f;
516                                                         } else if (lz == lo && rz == ro && lz == rz) {
517                                                                 z = o = t;
518                                                         } else {
519                                                                 goto result_unknown;
520                                                         }
521                                                         break;
522
523                                                 case ir_relation_less_equal:
524                                                 case ir_relation_less:
525                                                         /* TODO handle negative values */
526                                                         if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
527                                                                         tarval_is_negative(rz) || tarval_is_negative(ro))
528                                                                 goto result_unknown;
529
530                                                         if (tarval_cmp(lz, ro) & relation) {
531                                                                 /* Left upper bound is smaller(/equal) than right lower bound. */
532                                                                 z = o = t;
533                                                         } else if (!(tarval_cmp(lo, rz) & relation)) {
534                                                                 /* Left lower bound is not smaller(/equal) than right upper bound. */
535                                                                 z = o = f;
536                                                         } else {
537                                                                 goto result_unknown;
538                                                         }
539                                                         break;
540
541                                                 case ir_relation_greater_equal:
542                                                 case ir_relation_greater:
543                                                         /* TODO handle negative values */
544                                                         if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
545                                                                         tarval_is_negative(rz) || tarval_is_negative(ro))
546                                                                 goto result_unknown;
547
548                                                         if (!(tarval_cmp(lz, ro) & relation)) {
549                                                                 /* Left upper bound is not greater(/equal) than right lower bound. */
550                                                                 z = o = f;
551                                                         } else if (tarval_cmp(lo, rz) & relation) {
552                                                                 /* Left lower bound is greater(/equal) than right upper bound. */
553                                                                 z = o = t;
554                                                         } else {
555                                                                 goto result_unknown;
556                                                         }
557                                                         break;
558
559                                                 default:
560                                                         goto cannot_analyse;
561                                         }
562                                 }
563                                 break;
564                         }
565
566                         default: {
567 cannot_analyse:
568                                 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
569 result_unknown:
570                                 z = get_tarval_all_one(m);
571                                 o = get_tarval_null(m);
572                                 break;
573                         }
574                 }
575         } else {
576                 return 0;
577         }
578
579         return set_bitinfo(irn, z, o);
580 }
581
582 static void first_round(ir_node* const irn, void* const env)
583 {
584         pdeq* const q = (pdeq*)env;
585
586         transfer(irn);
587         if (is_Phi(irn) || is_Block(irn)) {
588                 /* Only Phis (and their users) need another round, if we did not have
589                  * information about all their inputs in the first round, i.e. in loops. */
590                 /* TODO inserts all Phis, should only insert Phis, which did no have all
591                  * predecessors available */
592                 pdeq_putr(q, irn);
593         }
594 }
595
596 static void apply_result(ir_node* const irn, void* ctx)
597 {
598         environment_t* env = (environment_t*)ctx;
599         ir_node*       block;
600         bitinfo*       b;
601         ir_tarval*     z;
602         ir_tarval*     o;
603
604         if (is_Block(irn)) {
605                 bitinfo* const block_b = get_bitinfo(irn);
606                 /* Trivially unreachable blocks have no info. */
607                 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
608                         exchange(irn, get_irg_bad(get_Block_irg(irn)));
609                         env->modified = 1;
610                 }
611                 return;
612         }
613
614         /* Unreachable blocks are replaced before the nodes in them. */
615         block = get_nodes_block(irn);
616         if (is_Bad(block)) {
617                 exchange(irn, block);
618                 env->modified = 1;
619                 return;
620         }
621
622         b = get_bitinfo(irn);
623         if (!b) return;
624         if (is_Const(irn)) return; // It cannot get any better than a Const.
625
626         z = b->z;
627         o = b->o;
628         // Only display information if we could find out anything about the value.
629         DEBUG_ONLY(if (!tarval_is_all_one(z) || !tarval_is_null(o)))
630                 DB((dbg, LEVEL_2, "%+F: 0:%T 1:%T%s\n", irn, z, o, z == o ? " --- constant" : ""));
631
632         // Replace node with constant value by Const.
633         if (z == o) {
634                 ir_mode* const m = get_irn_mode(irn);
635                 ir_node*       n;
636                 if (mode_is_intb(m)) {
637                         ir_graph *irg = get_irn_irg(irn);
638                         n = new_r_Const(irg, z);
639                 } else if (m == mode_X) {
640                         ir_graph* const irg = get_Block_irg(block);
641                         if (z == get_tarval_b_true()) {
642                                 // Might produce an endless loop, so keep the block.
643                                 add_End_keepalive(get_irg_end(irg), block);
644                                 n = new_r_Jmp(block);
645                         } else {
646                                 n = new_r_Bad(irg);
647                                 /* Transferring analysis information to the bad node makes it a
648                                  * candidate for replacement. */
649                                 goto exchange_only;
650                         }
651                 } else {
652                         return;
653                 }
654                 set_irn_link(n, b);
655 exchange_only:
656                 exchange(irn, n);
657                 env->modified = 1;
658         }
659
660         switch (get_irn_opcode(irn)) {
661                 case iro_And: {
662                         ir_node*       const l  = get_And_left(irn);
663                         ir_node*       const r  = get_And_right(irn);
664                         bitinfo const* const bl = get_bitinfo(l);
665                         bitinfo const* const br = get_bitinfo(r);
666                         if (bl->z == bl->o) {
667                                 if (tarval_is_null(tarval_andnot(br->z, bl->z))) {
668                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
669                                         exchange(irn, r);
670                                         env->modified = 1;
671                                 }
672                         } else if (br->z == br->o) {
673                                 if (tarval_is_null(tarval_andnot(bl->z, br->z))) {
674                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
675                                         exchange(irn, l);
676                                         env->modified = 1;
677                                 }
678                         }
679                         break;
680                 }
681
682                 case iro_Or: {
683                         ir_node*       const l  = get_Or_left(irn);
684                         ir_node*       const r  = get_Or_right(irn);
685                         bitinfo const* const bl = get_bitinfo(l);
686                         bitinfo const* const br = get_bitinfo(r);
687                         if (bl->z == bl->o) {
688                                 if (tarval_is_null(tarval_andnot(bl->o, br->o))) {
689                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
690                                         exchange(irn, r);
691                                         env->modified = 1;
692                                 }
693                         } else if (br->z == br->o) {
694                                 if (tarval_is_null(tarval_andnot(br->o, bl->o))) {
695                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
696                                         exchange(irn, l);
697                                         env->modified = 1;
698                                 }
699                         }
700                         break;
701                 }
702         }
703 }
704
705 static void queue_users(pdeq* const q, ir_node* const n)
706 {
707         if (get_irn_mode(n) == mode_X) {
708                 /* When the state of a control flow node changes, not only queue its
709                  * successor blocks, but also the Phis in these blocks, because the Phis
710                  * must reconsider this input path. */
711                 ir_edge_t const* e;
712                 foreach_out_edge(n, e) {
713                         ir_node*  const  src = get_edge_src_irn(e);
714                         pdeq_putr(q, src);
715                         /* should always be a block */
716                         if (is_Block(src)) {
717                                 ir_node *phi;
718                                 for (phi = get_Block_phis(src); phi; phi = get_Phi_next(phi))
719                                         pdeq_putr(q, phi);
720                         }
721                 }
722         } else {
723                 ir_edge_t const* e;
724                 foreach_out_edge(n, e) {
725                         ir_node* const src = get_edge_src_irn(e);
726                         if (get_irn_mode(src) == mode_T) {
727                                 queue_users(q, src);
728                         } else {
729                                 pdeq_putr(q, src);
730                         }
731                 }
732         }
733 }
734
735 static void clear_links(ir_node *irn, void *env)
736 {
737         (void) env;
738         set_irn_link(irn, NULL);
739         if (is_Block(irn))
740                 set_Block_phis(irn, NULL);
741 }
742
743 static void build_phi_lists(ir_node *irn, void *env)
744 {
745         (void) env;
746         if (is_Phi(irn))
747                 add_Block_phi(get_nodes_block(irn), irn);
748 }
749
750 void fixpoint_vrp(ir_graph* const irg)
751 {
752         environment_t env;
753
754         FIRM_DBG_REGISTER(dbg, "firm.opt.fp-vrp");
755         DB((dbg, LEVEL_1, "===> Performing constant propagation on %+F\n", irg));
756
757         obstack_init(&obst);
758
759         /* HACK: to avoid finding dead code */
760         edges_deactivate(irg);
761         edges_activate(irg);
762
763         edges_assure(irg);
764         assure_doms(irg);
765
766         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
767
768         {
769                 pdeq* const q = new_pdeq();
770
771                 /* We need this extra step because the dom tree does not contain unreachable
772                    blocks in Firm. Moreover build phi list. */
773                 irg_walk_anchors(irg, clear_links, build_phi_lists, NULL);
774
775                 /* TODO Improve iteration order. Best is reverse postorder in data flow
776                  * direction and respecting loop nesting for fastest convergence. */
777                 irg_walk_blkwise_dom_top_down(irg, NULL, first_round, q);
778
779                 while (!pdeq_empty(q)) {
780                         ir_node* const n = (ir_node*)pdeq_getl(q);
781                         if (transfer(n))
782                                 queue_users(q, n);
783                 }
784
785                 del_pdeq(q);
786         }
787
788         DB((dbg, LEVEL_2, "---> Applying analysis results\n"));
789         env.modified = 0;
790         irg_walk_graph(irg, NULL, apply_result, &env);
791
792         if (env.modified) {
793                 /* control flow might changed */
794                 set_irg_outs_inconsistent(irg);
795                 set_irg_extblk_inconsistent(irg);
796                 set_irg_doms_inconsistent(irg);
797                 set_irg_loopinfo_inconsistent(irg);
798                 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
799         }
800
801         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
802
803         obstack_free(&obst, NULL);
804 }
805
806 ir_graph_pass_t *fixpoint_vrp_irg_pass(const char *name)
807 {
808         return def_graph_pass(name ? name : "fixpoint_vrp", fixpoint_vrp);
809 }