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