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