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