e64c96835983ebc4087402ed97fb606273979402
[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
27 #include "config.h"
28
29 #include "adt/pdeq.h"
30 #include "adt/obst.h"
31 #include "adt/xmalloc.h"
32 #include "debug.h"
33 #include "ircons.h"
34 #include "irdom.h"
35 #include "iredges.h"
36 #include "irgmod.h"
37 #include "irgraph.h"
38 #include "irgwalk.h"
39 #include "irnode.h"
40 #include "iroptimize.h"
41 #include "irtools.h"
42 #include "tv.h"
43 #include "irpass.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         tarval* z; // safe zeroes, 0 = bit is zero,       1 = bit maybe is 1
127         tarval* o; // safe ones,   0 = bit maybe is zero, 1 = bit is 1
128 } bitinfo;
129
130 static inline bitinfo* get_bitinfo(ir_node const* const irn)
131 {
132         return get_irn_link(irn);
133 }
134
135 static int set_bitinfo(ir_node* const irn, tarval* const z, tarval* const o)
136 {
137         bitinfo* b = get_bitinfo(irn);
138         if (b == NULL) {
139                 b = OALLOCZ(&obst, bitinfo);
140                 set_irn_link(irn, b);
141         } else if (z == b->z && o == b->o) {
142                 return 0;
143         }
144         b->z = z;
145         b->o = o;
146         DB((dbg, LEVEL_3, "%+F: 0:%T 1:%T\n", irn, z, o));
147         return 1;
148 }
149
150 static int mode_is_intb(ir_mode const* const m)
151 {
152         return mode_is_int(m) || m == mode_b;
153 }
154
155 static int transfer(ir_node* const irn)
156 {
157         ir_mode* const m = get_irn_mode(irn);
158         tarval*        z;
159         tarval*        o;
160
161         if (m == mode_X) {
162                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
163                 switch (get_irn_opcode(irn)) {
164                         case iro_Proj: {
165                                 ir_node* const pred = get_Proj_pred(irn);
166                                 if (is_Start(pred)) {
167                                         z = get_tarval_b_true();
168                                         o = get_tarval_b_false();
169                                 } else if (is_Cond(pred)) {
170                                         ir_node* const selector = get_Cond_selector(pred);
171                                         bitinfo* const b        = get_bitinfo(selector);
172                                         tarval*  const bz       = b->z;
173                                         tarval*  const bo       = b->o;
174                                         if (get_irn_mode(selector) == mode_b) {
175                                                 if (bz == bo) {
176                                                         if ((bz == get_tarval_b_true()) == get_Proj_proj(irn)) {
177                                                                 z = o = get_tarval_b_true();
178                                                         } else {
179                                                                 z = o = get_tarval_b_false();
180                                                         }
181                                                 } else {
182                                                         goto result_unknown_X;
183                                                 }
184                                         } else {
185                                                 long const val = get_Proj_proj(irn);
186                                                 if (val != get_Cond_default_proj(pred)) {
187                                                         tarval* const tv = new_tarval_from_long(val, get_irn_mode(selector));
188                                                         if (!tarval_is_null(tarval_andnot(tv, bz)) ||
189                                                                         !tarval_is_null(tarval_andnot(bo, tv))) {
190                                                                 // At least one bit differs.
191                                                                 z = o = get_tarval_b_false();
192 #if 0 // TODO must handle default Proj
193                                                         } else if (bz == bo && bz == tv) {
194                                                                 z = o = get_tarval_b_true();
195 #endif
196                                                         } else {
197                                                                 goto result_unknown_X;
198                                                         }
199                                                 } else {
200                                                         goto cannot_analyse_X;
201                                                 }
202                                         }
203                                 } else {
204                                         goto cannot_analyse_X;
205                                 }
206                                 break;
207                         }
208
209                         case iro_Jmp: {
210                                 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
211                                 z = b->z;
212                                 o = b->o;
213                                 break;
214                         }
215
216                         default:
217 cannot_analyse_X:
218                                 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
219 result_unknown_X:
220                                 z = get_tarval_b_true();
221                                 o = get_tarval_b_false();
222                                 break;
223                 }
224         } else if (is_Block(irn)) {
225                 int       reachable = 0;
226                 int const arity     = get_Block_n_cfgpreds(irn);
227                 int       i;
228
229                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
230                 for (i = 0; i != arity; ++i) {
231                         bitinfo* const b = get_bitinfo(get_Block_cfgpred(irn, i));
232                         if (b != NULL && b->z == get_tarval_b_true()) {
233                                 reachable = 1;
234                                 break;
235                         }
236                 }
237
238                 o = get_tarval_b_false();
239                 z = reachable || irn == get_irg_start_block(get_irn_irg(irn)) ? get_tarval_b_true() : o;
240         } else if (mode_is_intb(m)) {
241                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
242                 switch (get_irn_opcode(irn)) {
243                         case iro_Const: {
244                                 z = o = get_Const_tarval(irn);
245                                 break;
246                         }
247
248                         case iro_Shl: {
249                                 bitinfo* const l  = get_bitinfo(get_Shl_left(irn));
250                                 bitinfo* const r  = get_bitinfo(get_Shl_right(irn));
251                                 tarval*  const rz = r->z;
252                                 if (rz == r->o) {
253                                         z = tarval_shl(l->z, rz);
254                                         o = tarval_shl(l->o, rz);
255                                 } else {
256                                         goto cannot_analyse;
257                                 }
258                                 break;
259                         }
260
261                         case iro_Shr: {
262                                 bitinfo* const l  = get_bitinfo(get_Shr_left(irn));
263                                 bitinfo* const r  = get_bitinfo(get_Shr_right(irn));
264                                 tarval*  const rz = r->z;
265                                 if (rz == r->o) {
266                                         z = tarval_shr(l->z, rz);
267                                         o = tarval_shr(l->o, rz);
268                                 } else {
269                                         goto cannot_analyse;
270                                 }
271                                 break;
272                         }
273
274                         case iro_Shrs: {
275                                 bitinfo* const l  = get_bitinfo(get_Shrs_left(irn));
276                                 bitinfo* const r  = get_bitinfo(get_Shrs_right(irn));
277                                 tarval*  const rz = r->z;
278                                 if (rz == r->o) {
279                                         z = tarval_shrs(l->z, rz);
280                                         o = tarval_shrs(l->o, rz);
281                                 } else {
282                                         goto cannot_analyse;
283                                 }
284                                 break;
285                         }
286
287                         case iro_Rotl: {
288                                 bitinfo* const l  = get_bitinfo(get_Rotl_left(irn));
289                                 bitinfo* const r  = get_bitinfo(get_Rotl_right(irn));
290                                 tarval*  const rz = r->z;
291                                 if (rz == r->o) {
292                                         z = tarval_rotl(l->z, rz);
293                                         o = tarval_rotl(l->o, rz);
294                                 } else {
295                                         goto cannot_analyse;
296                                 }
297                                 break;
298                         }
299
300                         case iro_Add: {
301                                 bitinfo* const l  = get_bitinfo(get_Add_left(irn));
302                                 bitinfo* const r  = get_bitinfo(get_Add_right(irn));
303                                 tarval*  const lz = l->z;
304                                 tarval*  const lo = l->o;
305                                 tarval*  const rz = r->z;
306                                 tarval*  const ro = r->o;
307                                 if (lz == lo && rz == ro) {
308                                         z = o = tarval_add(lz, rz);
309                                 } else {
310                                         // TODO improve: can only do lower disjoint bits
311                                         /* Determine where any of the operands has zero bits, i.e. where no
312                                          * carry out is generated if there is not carry in */
313                                         tarval* const no_c_in_no_c_out = tarval_and(lz, rz);
314                                         /* Generate a mask of the lower consecutive zeroes: x | -x.  In this
315                                          * range the addition is disjoint and therefore Add behaves like Or.
316                                          */
317                                         tarval* const low_zero_mask = tarval_or(no_c_in_no_c_out, tarval_neg(no_c_in_no_c_out));
318                                         tarval* const low_one_mask  = tarval_not(low_zero_mask);
319                                         z = tarval_or( tarval_or(lz, rz), low_zero_mask);
320                                         o = tarval_and(tarval_or(lo, ro), low_one_mask);
321                                 }
322                                 break;
323                         }
324
325                         case iro_Sub: {
326                                 bitinfo* const l = get_bitinfo(get_Sub_left(irn));
327                                 bitinfo* const r = get_bitinfo(get_Sub_right(irn));
328                                 if (l != NULL && r != NULL) { // Sub might subtract pointers.
329                                         tarval* const lz = l->z;
330                                         tarval* const lo = l->o;
331                                         tarval* const rz = r->z;
332                                         tarval* const ro = r->o;
333                                         if (lz == lo && rz == ro) {
334                                                 z = o = tarval_sub(lz, rz, NULL);
335                                         } else if (tarval_is_null(tarval_andnot(rz, lo))) {
336                                                 /* Every possible one of the subtrahend is backed by a safe one of the
337                                                  * minuend, i.e. there are no borrows. */
338                                                 // TODO extend no-borrow like carry for Add above
339                                                 z = tarval_andnot(lz, ro);
340                                                 o = tarval_andnot(lo, rz);
341                                         } else {
342                                                 goto cannot_analyse;
343                                         }
344                                 } else {
345                                         goto cannot_analyse;
346                                 }
347                                 break;
348                         }
349
350                         case iro_Mul: {
351                                 bitinfo* const l  = get_bitinfo(get_Mul_left(irn));
352                                 bitinfo* const r  = get_bitinfo(get_Mul_right(irn));
353                                 tarval*  const lz = l->z;
354                                 tarval*  const lo = l->o;
355                                 tarval*  const rz = r->z;
356                                 tarval*  const ro = r->o;
357                                 if (lz == lo && rz == ro) {
358                                         z = o = tarval_mul(lz, rz);
359                                 } else {
360                                         // TODO improve
361                                         // Determine safe lower zeroes: x | -x.
362                                         tarval* const lzn = tarval_or(lz, tarval_neg(lz));
363                                         tarval* const rzn = tarval_or(rz, tarval_neg(rz));
364                                         // Concatenate safe lower zeroes.
365                                         if (tarval_cmp(lzn, rzn) == pn_Cmp_Lt) {
366                                                 z = tarval_mul(tarval_eor(lzn, tarval_shl(lzn, get_tarval_one(m))), rzn);
367                                         } else {
368                                                 z = tarval_mul(tarval_eor(rzn, tarval_shl(rzn, get_tarval_one(m))), lzn);
369                                         }
370                                         o = get_tarval_null(m);
371                                 }
372                                 break;
373                         }
374
375                         case iro_Minus: {
376                                 bitinfo* const b = get_bitinfo(get_Minus_op(irn));
377                                 if (b->z == b->o) {
378                                         z = o = tarval_neg(b->z);
379                                 } else {
380                                         goto cannot_analyse;
381                                 }
382                                 break;
383                         }
384
385                         case iro_And: {
386                                 bitinfo* const l = get_bitinfo(get_And_left(irn));
387                                 bitinfo* const r = get_bitinfo(get_And_right(irn));
388                                 z = tarval_and(l->z, r->z);
389                                 o = tarval_and(l->o, r->o);
390                                 break;
391                         }
392
393                         case iro_Or: {
394                                 bitinfo* const l = get_bitinfo(get_Or_left(irn));
395                                 bitinfo* const r = get_bitinfo(get_Or_right(irn));
396                                 z = tarval_or(l->z, r->z);
397                                 o = tarval_or(l->o, r->o);
398                                 break;
399                         }
400
401                         case iro_Eor: {
402                                 bitinfo* const l  = get_bitinfo(get_Eor_left(irn));
403                                 bitinfo* const r  = get_bitinfo(get_Eor_right(irn));
404                                 tarval*  const lz = l->z;
405                                 tarval*  const lo = l->o;
406                                 tarval*  const rz = r->z;
407                                 tarval*  const ro = r->o;
408                                 z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
409                                 o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
410                                 break;
411                         }
412
413                         case iro_Not: {
414                                 bitinfo* const b = get_bitinfo(get_Not_op(irn));
415                                 z = tarval_not(b->o);
416                                 o = tarval_not(b->z);
417                                 break;
418                         }
419
420                         case iro_Conv: {
421                                 bitinfo* const b = get_bitinfo(get_Conv_op(irn));
422                                 if (b == NULL) // Happens when converting from float values.
423                                         goto result_unknown;
424                                 z = tarval_convert_to(b->z, m);
425                                 o = tarval_convert_to(b->o, m);
426                                 break;
427                         }
428
429                         case iro_Mux: {
430                                 bitinfo* const f = get_bitinfo(get_Mux_false(irn));
431                                 bitinfo* const t = get_bitinfo(get_Mux_true(irn));
432                                 bitinfo* const c = get_bitinfo(get_Mux_sel(irn));
433                                 if (c->o == get_tarval_b_true()) {
434                                         z = t->z;
435                                         o = t->o;
436                                 } else if (c->z == get_tarval_b_false()) {
437                                         z = f->z;
438                                         o = f->o;
439                                 } else {
440                                         z = tarval_or( f->z, t->z);
441                                         o = tarval_and(f->o, t->o);
442                                 }
443                         }
444
445                         case iro_Phi: {
446                                 ir_node* const block = get_nodes_block(irn);
447                                 int      const arity = get_Phi_n_preds(irn);
448                                 int            i;
449
450                                 z = get_tarval_null(m);
451                                 o = get_tarval_all_one(m);
452                                 for (i = 0; i != arity; ++i) {
453                                         bitinfo* const b_cfg = get_bitinfo(get_Block_cfgpred(block, i));
454                                         if (b_cfg != NULL && b_cfg->z != get_tarval_b_false()) {
455                                                 bitinfo* const b = get_bitinfo(get_Phi_pred(irn, i));
456                                                 z = tarval_or( z, b->z);
457                                                 o = tarval_and(o, b->o);
458                                         }
459                                 }
460                                 break;
461                         }
462
463                         case iro_Proj: {
464                                 ir_node* const pred = get_Proj_pred(irn);
465                                 if (is_Cmp(pred)) { // TODO generalize
466                                         bitinfo* const l = get_bitinfo(get_Cmp_left(pred));
467                                         bitinfo* const r = get_bitinfo(get_Cmp_right(pred));
468                                         if (l == NULL || r == NULL)
469                                                 goto result_unknown; // Cmp compares something we cannot evaluate.
470                                         switch (get_Proj_proj(irn)) {
471                                                 case pn_Cmp_Lg: {
472                                                         tarval* const lz = l->z;
473                                                         tarval* const lo = l->o;
474                                                         tarval* const rz = r->z;
475                                                         tarval* const ro = r->o;
476                                                         if (!tarval_is_null(tarval_andnot(ro, lz)) ||
477                                                                         !tarval_is_null(tarval_andnot(lo, rz))) {
478                                                                 // At least one bit differs.
479                                                                 z = o = get_tarval_b_true();
480                                                         } else if (lz == lo && rz == ro && lz == rz) {
481                                                                 z = o = get_tarval_b_false();
482                                                         } else {
483                                                                 goto result_unknown;
484                                                         }
485                                                         break;
486                                                 }
487
488                                                 case pn_Cmp_Eq: {
489                                                         tarval* const lz = l->z;
490                                                         tarval* const lo = l->o;
491                                                         tarval* const rz = r->z;
492                                                         tarval* const ro = r->o;
493                                                         if (!tarval_is_null(tarval_andnot(ro, lz)) ||
494                                                                         !tarval_is_null(tarval_andnot(lo, rz))) {
495                                                                 // At least one bit differs.
496                                                                 z = o = get_tarval_b_false();
497                                                         } else if (lz == lo && rz == ro && lz == rz) {
498                                                                 z = o = get_tarval_b_true();
499                                                         } else {
500                                                                 goto result_unknown;
501                                                         }
502                                                         break;
503                                                 }
504
505                                                 default:
506                                                         goto cannot_analyse;
507                                         }
508                                 } else {
509                                         goto cannot_analyse;
510                                 }
511                                 break;
512                         }
513
514                         default: {
515 cannot_analyse:
516                                 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
517 result_unknown:
518                                 z = get_tarval_all_one(m);
519                                 o = get_tarval_null(m);
520                                 break;
521                         }
522                 }
523         } else {
524                 return 0;
525         }
526
527         return set_bitinfo(irn, z, o);
528 }
529
530 static void first_round(ir_node* const irn, void* const env)
531 {
532         pdeq* const q = env;
533
534         transfer(irn);
535         if (is_Phi(irn) || is_Block(irn)) {
536                 /* Only Phis (and their users) need another round, if we did not have
537                  * information about all their inputs in the first round, i.e. in loops. */
538                 /* TODO inserts all Phis, should only insert Phis, which did no have all
539                  * predecessors available */
540                 pdeq_putr(q, irn);
541         }
542 }
543
544 static void apply_result(ir_node* const irn, void* const env)
545 {
546         bitinfo* const b = get_bitinfo(irn);
547         tarval*        z;
548         tarval*        o;
549         (void)env;
550
551         if (!b) return;
552         if (is_Const(irn)) return; // It cannot get any better than a Const.
553
554         z = b->z;
555         o = b->o;
556         // Only display information if we could find out anything about the value.
557         DEBUG_ONLY(if (!tarval_is_all_one(z) || !tarval_is_null(o)))
558                 DB((dbg, LEVEL_2, "%+F: 0:%T 1:%T%s\n", irn, z, o, z == o ? " --- constant" : ""));
559
560         // Replace node with constant value by Const.
561         if (z == o) {
562                 ir_mode* const m = get_irn_mode(irn);
563                 ir_node*       n;
564                 if (mode_is_intb(m)) {
565                         n = new_Const(z);
566                 } else if (m == mode_X) {
567                         ir_node*  const block = get_nodes_block(irn);
568                         ir_graph* const irg   = get_Block_irg(block);
569                         if (z == get_tarval_b_true()) {
570                                 // Might produce an endless loop, so keep the block.
571                                 add_End_keepalive(get_irg_end(irg), block);
572                                 n = new_r_Jmp(block);
573                         } else {
574                                 n = new_r_Bad(irg);
575                                 /* Transferring analysis information to the bad node makes it a
576                                  * candidate for replacement. */
577                                 goto exchange_only;
578                         }
579                 } else {
580                         return;
581                 }
582                 set_irn_link(n, b);
583 exchange_only:
584                 exchange(irn, n);
585         }
586
587         switch (get_irn_opcode(irn)) {
588                 case iro_And: {
589                         ir_node*       const l  = get_And_left(irn);
590                         ir_node*       const r  = get_And_right(irn);
591                         bitinfo const* const bl = get_bitinfo(l);
592                         bitinfo const* const br = get_bitinfo(r);
593                         if (bl->z == bl->o) {
594                                 if (tarval_is_null(tarval_andnot(br->z, bl->z))) {
595                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
596                                         exchange(irn, r);
597                                 }
598                         } else if (br->z == br->o) {
599                                 if (tarval_is_null(tarval_andnot(bl->z, br->z))) {
600                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
601                                         exchange(irn, l);
602                                 }
603                         }
604                         break;
605                 }
606
607                 case iro_Or: {
608                         ir_node*       const l  = get_Or_left(irn);
609                         ir_node*       const r  = get_Or_right(irn);
610                         bitinfo const* const bl = get_bitinfo(l);
611                         bitinfo const* const br = get_bitinfo(r);
612                         if (bl->z == bl->o) {
613                                 if (tarval_is_null(tarval_andnot(bl->o, br->o))) {
614                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
615                                         exchange(irn, r);
616                                 }
617                         } else if (br->z == br->o) {
618                                 if (tarval_is_null(tarval_andnot(br->o, bl->o))) {
619                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
620                                         exchange(irn, l);
621                                 }
622                         }
623                         break;
624                 }
625         }
626 }
627
628 static void queue_users(pdeq* const q, ir_node* const n)
629 {
630         if (get_irn_mode(n) == mode_X) {
631                 /* When the state of a control flow node changes, not only queue its
632                  * successor blocks, but also the Phis in these blocks, because the Phis
633                  * must reconsider this input path. */
634                 ir_edge_t const* e;
635                 foreach_out_edge(n, e) {
636                         ir_node*  const  src = get_edge_src_irn(e);
637                         ir_edge_t const* src_e;
638                         pdeq_putr(q, src);
639                         foreach_out_edge(src, src_e) {
640                                 ir_node* const src_src = get_edge_src_irn(src_e);
641                                 if (is_Phi(src_src))
642                                         pdeq_putr(q, src_src);
643                         }
644                 }
645         } else {
646                 ir_edge_t const* e;
647                 foreach_out_edge(n, e) {
648                         ir_node* const src = get_edge_src_irn(e);
649                         if (get_irn_mode(src) == mode_T) {
650                                 queue_users(q, src);
651                         } else {
652                                 pdeq_putr(q, src);
653                         }
654                 }
655         }
656 }
657
658 void fixpoint_vrp(ir_graph* const irg)
659 {
660         FIRM_DBG_REGISTER(dbg, "firm.opt.fp-vrp");
661         DB((dbg, LEVEL_1, "===> Performing constant propagation on %+F\n", irg));
662
663         obstack_init(&obst);
664
665         edges_assure(irg);
666         assure_doms(irg);
667
668         { pdeq* const q = new_pdeq();
669
670                 /* TODO Improve iteration order. Best is reverse postorder in data flow
671                  * direction and respecting loop nesting for fastest convergence. */
672                 irg_walk_blkwise_dom_top_down(irg, firm_clear_link, first_round, q);
673
674                 while (!pdeq_empty(q)) {
675                         ir_node* const n = pdeq_getl(q);
676                         if (transfer(n))
677                                 queue_users(q, n);
678                 }
679
680                 del_pdeq(q);
681         }
682
683         DB((dbg, LEVEL_2, "---> Applying analysis results\n"));
684         irg_walk_graph(irg, NULL, apply_result, NULL);
685
686         obstack_free(&obst, NULL);
687 }
688
689 ir_graph_pass_t *fixpoint_vrp_irg_pass(const char *name)
690 {
691         return def_graph_pass(name ? name : "fixpoint_vrp", fixpoint_vrp);
692 }