clarify some node comments
[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  */
25 #include "config.h"
26
27 #include <assert.h>
28 #include <stdbool.h>
29
30 #include "adt/pdeq.h"
31 #include "adt/obst.h"
32 #include "adt/xmalloc.h"
33 #include "debug.h"
34 #include "ircons.h"
35 #include "irdom.h"
36 #include "iredges.h"
37 #include "irgmod.h"
38 #include "irgraph_t.h"
39 #include "irgwalk.h"
40 #include "irnode_t.h"
41 #include "iroptimize.h"
42 #include "irtools.h"
43 #include "tv.h"
44 #include "irpass.h"
45 #include "irmemory.h"
46 #include "opt_manage.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                                         if (b->z == b->o) {
202                                                 if ((b->z == t) == get_Proj_proj(irn)) {
203                                                         z = o = t;
204                                                 } else {
205                                                         z = o = f;
206                                                 }
207                                         } else {
208                                                 goto result_unknown_X;
209                                         }
210                                 } else if (is_Switch(pred)) {
211                                         ir_node* const selector = get_Switch_selector(pred);
212                                         bitinfo* const b        = get_bitinfo(selector);
213                                         if (is_undefined(b))
214                                                 goto unreachable_X;
215                                         /* TODO */
216                                         goto cannot_analyse_X;
217                                 } else {
218                                         goto cannot_analyse_X;
219                                 }
220                                 break;
221                         }
222
223                         case iro_Jmp:
224                                 goto result_unknown_X;
225
226                         default:
227 cannot_analyse_X:
228                                 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
229 result_unknown_X:
230                                 z = t;
231                                 o = f;
232                                 break;
233                 }
234         } else if (is_Block(irn)) {
235                 int       reachable = 0;
236                 int const arity     = get_Block_n_cfgpreds(irn);
237                 int       i;
238
239                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
240                 for (i = 0; i != arity; ++i) {
241                         bitinfo* const b = get_bitinfo(get_Block_cfgpred(irn, i));
242                         if (b != NULL && b->z == t) {
243                                 reachable = 1;
244                                 break;
245                         }
246                 }
247
248                 if (!reachable) {
249                         ir_graph *const irg = get_Block_irg(irn);
250                         reachable =
251                                 irn == get_irg_start_block(irg) ||
252                                 irn == get_irg_end_block(irg);
253                 }
254
255                 if (reachable) {
256                         z = t;
257                         o = f;
258                 } else {
259                         z = f;
260                         o = t;
261                 }
262         } else if (mode_is_intb(m)) {
263                 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
264
265                 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
266
267                 if (b == NULL || b->z == f) {
268 undefined:
269                         z = get_tarval_null(m);
270                         o = get_tarval_all_one(m);
271                 } else if (is_Phi(irn)) {
272                         ir_node* const block = get_nodes_block(irn);
273                         int      const arity = get_Phi_n_preds(irn);
274                         int            i;
275
276                         z = get_tarval_null(m);
277                         o = get_tarval_all_one(m);
278                         for (i = 0; i != arity; ++i) {
279                                 bitinfo* const b_cfg = get_bitinfo(get_Block_cfgpred(block, i));
280                                 if (b_cfg != NULL && b_cfg->z != f) {
281                                         bitinfo* const b = get_bitinfo(get_Phi_pred(irn, i));
282                                         /* Only use input if it's not undefined. */
283                                         if (!is_undefined(b)) {
284                                                 z = tarval_or( z, b->z);
285                                                 o = tarval_and(o, b->o);
286                                         }
287                                 }
288                         }
289                 } else {
290                         int const arity = get_irn_arity(irn);
291                         int       i;
292
293                         /* Undefined if any input is undefined. */
294                         for (i = 0; i != arity; ++i) {
295                                 ir_node* const pred   = get_irn_n(irn, i);
296                                 bitinfo* const pred_b = get_bitinfo(pred);
297                                 if (pred_b != NULL && is_undefined(pred_b))
298                                         goto undefined;
299                         }
300
301                         switch (get_irn_opcode(irn)) {
302                                 case iro_Const: {
303                                         z = o = get_Const_tarval(irn);
304                                         break;
305                                 }
306
307                                 case iro_Confirm: {
308                                         ir_node* const v = get_Confirm_value(irn);
309                                         bitinfo* const b = get_bitinfo(v);
310                                         /* TODO Use bound and relation. */
311                                         z = b->z;
312                                         o = b->o;
313                                         if ((get_Confirm_relation(irn) & ~ir_relation_unordered) == ir_relation_equal) {
314                                                 bitinfo* const bound_b = get_bitinfo(get_Confirm_bound(irn));
315                                                 z = tarval_and(z, bound_b->z);
316                                                 o = tarval_or( o, bound_b->o);
317                                         }
318                                         break;
319                                 }
320
321                                 case iro_Shl: {
322                                         bitinfo*   const l  = get_bitinfo(get_Shl_left(irn));
323                                         bitinfo*   const r  = get_bitinfo(get_Shl_right(irn));
324                                         ir_tarval* const rz = r->z;
325                                         if (rz == r->o) {
326                                                 z = tarval_shl(l->z, rz);
327                                                 o = tarval_shl(l->o, rz);
328                                         } else {
329                                                 goto cannot_analyse;
330                                         }
331                                         break;
332                                 }
333
334                                 case iro_Shr: {
335                                         bitinfo*   const l  = get_bitinfo(get_Shr_left(irn));
336                                         bitinfo*   const r  = get_bitinfo(get_Shr_right(irn));
337                                         ir_tarval* const rz = r->z;
338                                         if (rz == r->o) {
339                                                 z = tarval_shr(l->z, rz);
340                                                 o = tarval_shr(l->o, rz);
341                                         } else {
342                                                 goto cannot_analyse;
343                                         }
344                                         break;
345                                 }
346
347                                 case iro_Shrs: {
348                                         bitinfo*   const l  = get_bitinfo(get_Shrs_left(irn));
349                                         bitinfo*   const r  = get_bitinfo(get_Shrs_right(irn));
350                                         ir_tarval* const rz = r->z;
351                                         if (rz == r->o) {
352                                                 z = tarval_shrs(l->z, rz);
353                                                 o = tarval_shrs(l->o, rz);
354                                         } else {
355                                                 goto cannot_analyse;
356                                         }
357                                         break;
358                                 }
359
360                                 case iro_Rotl: {
361                                         bitinfo*   const l  = get_bitinfo(get_Rotl_left(irn));
362                                         bitinfo*   const r  = get_bitinfo(get_Rotl_right(irn));
363                                         ir_tarval* const rz = r->z;
364                                         if (rz == r->o) {
365                                                 z = tarval_rotl(l->z, rz);
366                                                 o = tarval_rotl(l->o, rz);
367                                         } else {
368                                                 goto cannot_analyse;
369                                         }
370                                         break;
371                                 }
372
373                                 case iro_Add: {
374                                         bitinfo*   const l  = get_bitinfo(get_Add_left(irn));
375                                         bitinfo*   const r  = get_bitinfo(get_Add_right(irn));
376                                         ir_tarval* const lz = l->z;
377                                         ir_tarval* const lo = l->o;
378                                         ir_tarval* const rz = r->z;
379                                         ir_tarval* const ro = r->o;
380                                         if (lz == lo && rz == ro) {
381                                                 z = o = tarval_add(lz, rz);
382                                         } else {
383                                                 // TODO improve: can only do lower disjoint bits
384                                                 /* Determine where any of the operands has zero bits, i.e. where no
385                                                  * carry out is generated if there is not carry in */
386                                                 ir_tarval* const no_c_in_no_c_out = tarval_and(lz, rz);
387                                                 /* Generate a mask of the lower consecutive zeroes: x | -x.  In this
388                                                  * range the addition is disjoint and therefore Add behaves like Or.
389                                                  */
390                                                 ir_tarval* const low_zero_mask = tarval_or(no_c_in_no_c_out, tarval_neg(no_c_in_no_c_out));
391                                                 ir_tarval* const low_one_mask  = tarval_not(low_zero_mask);
392                                                 z = tarval_or( tarval_or(lz, rz), low_zero_mask);
393                                                 o = tarval_and(tarval_or(lo, ro), low_one_mask);
394                                         }
395                                         break;
396                                 }
397
398                                 case iro_Sub: {
399                                         bitinfo* const l = get_bitinfo(get_Sub_left(irn));
400                                         bitinfo* const r = get_bitinfo(get_Sub_right(irn));
401                                         if (l != NULL && r != NULL) { // Sub might subtract pointers.
402                                                 ir_tarval* const lz = l->z;
403                                                 ir_tarval* const lo = l->o;
404                                                 ir_tarval* const rz = r->z;
405                                                 ir_tarval* const ro = r->o;
406                                                 if (lz == lo && rz == ro) {
407                                                         z = o = tarval_sub(lz, rz, NULL);
408                                                 } else if (tarval_is_null(tarval_andnot(rz, lo))) {
409                                                         /* Every possible one of the subtrahend is backed by a safe one of the
410                                                          * minuend, i.e. there are no borrows. */
411                                                         // TODO extend no-borrow like carry for Add above
412                                                         z = tarval_andnot(lz, ro);
413                                                         o = tarval_andnot(lo, rz);
414                                                 } else {
415                                                         goto cannot_analyse;
416                                                 }
417                                         } else {
418                                                 goto cannot_analyse;
419                                         }
420                                         break;
421                                 }
422
423                                 case iro_Mul: {
424                                         bitinfo*   const l  = get_bitinfo(get_Mul_left(irn));
425                                         bitinfo*   const r  = get_bitinfo(get_Mul_right(irn));
426                                         ir_tarval* const lz = l->z;
427                                         ir_tarval* const lo = l->o;
428                                         ir_tarval* const rz = r->z;
429                                         ir_tarval* const ro = r->o;
430                                         if (lz == lo && rz == ro) {
431                                                 z = o = tarval_mul(lz, rz);
432                                         } else {
433                                                 // TODO improve
434                                                 // Determine safe lower zeroes: x | -x.
435                                                 ir_tarval* const lzn = tarval_or(lz, tarval_neg(lz));
436                                                 ir_tarval* const rzn = tarval_or(rz, tarval_neg(rz));
437                                                 // Concatenate safe lower zeroes.
438                                                 if (tarval_cmp(lzn, rzn) == ir_relation_less) {
439                                                         z = tarval_mul(tarval_eor(lzn, tarval_shl(lzn, get_tarval_one(m))), rzn);
440                                                 } else {
441                                                         z = tarval_mul(tarval_eor(rzn, tarval_shl(rzn, get_tarval_one(m))), lzn);
442                                                 }
443                                                 o = get_tarval_null(m);
444                                         }
445                                         break;
446                                 }
447
448                                 case iro_Minus: {
449                                         bitinfo* const b = get_bitinfo(get_Minus_op(irn));
450                                         if (b->z == b->o) {
451                                                 z = o = tarval_neg(b->z);
452                                         } else {
453                                                 goto cannot_analyse;
454                                         }
455                                         break;
456                                 }
457
458                                 case iro_And: {
459                                         bitinfo* const l = get_bitinfo(get_And_left(irn));
460                                         bitinfo* const r = get_bitinfo(get_And_right(irn));
461                                         z = tarval_and(l->z, r->z);
462                                         o = tarval_and(l->o, r->o);
463                                         break;
464                                 }
465
466                                 case iro_Or: {
467                                         bitinfo* const l = get_bitinfo(get_Or_left(irn));
468                                         bitinfo* const r = get_bitinfo(get_Or_right(irn));
469                                         z = tarval_or(l->z, r->z);
470                                         o = tarval_or(l->o, r->o);
471                                         break;
472                                 }
473
474                                 case iro_Eor: {
475                                         bitinfo*   const l  = get_bitinfo(get_Eor_left(irn));
476                                         bitinfo*   const r  = get_bitinfo(get_Eor_right(irn));
477                                         ir_tarval* const lz = l->z;
478                                         ir_tarval* const lo = l->o;
479                                         ir_tarval* const rz = r->z;
480                                         ir_tarval* const ro = r->o;
481                                         z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
482                                         o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
483                                         break;
484                                 }
485
486                                 case iro_Not: {
487                                         bitinfo* const b = get_bitinfo(get_Not_op(irn));
488                                         z = tarval_not(b->o);
489                                         o = tarval_not(b->z);
490                                         break;
491                                 }
492
493                                 case iro_Conv: {
494                                         bitinfo* const b = get_bitinfo(get_Conv_op(irn));
495                                         if (b == NULL) // Happens when converting from float values.
496                                                 goto result_unknown;
497                                         z = tarval_convert_to(b->z, m);
498                                         o = tarval_convert_to(b->o, m);
499                                         break;
500                                 }
501
502                                 case iro_Mux: {
503                                         bitinfo* const bf = get_bitinfo(get_Mux_false(irn));
504                                         bitinfo* const bt = get_bitinfo(get_Mux_true(irn));
505                                         bitinfo* const c  = get_bitinfo(get_Mux_sel(irn));
506                                         if (c->o == t) {
507                                                 z = bt->z;
508                                                 o = bt->o;
509                                         } else if (c->z == f) {
510                                                 z = bf->z;
511                                                 o = bf->o;
512                                         } else {
513                                                 z = tarval_or( bf->z, bt->z);
514                                                 o = tarval_and(bf->o, bt->o);
515                                         }
516                                         break;
517                                 }
518
519                                 case iro_Cmp: {
520                                         bitinfo* const l = get_bitinfo(get_Cmp_left(irn));
521                                         bitinfo* const r = get_bitinfo(get_Cmp_right(irn));
522                                         if (l == NULL || r == NULL) {
523                                                 goto result_unknown; // Cmp compares something we cannot evaluate.
524                                         } else {
525                                                 ir_tarval*  const lz       = l->z;
526                                                 ir_tarval*  const lo       = l->o;
527                                                 ir_tarval*  const rz       = r->z;
528                                                 ir_tarval*  const ro       = r->o;
529                                                 ir_relation const relation = get_Cmp_relation(irn);
530                                                 switch (relation) {
531                                                         case ir_relation_less_greater:
532                                                                 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
533                                                                                 !tarval_is_null(tarval_andnot(lo, rz))) {
534                                                                         // At least one bit differs.
535                                                                         z = o = t;
536                                                                 } else if (lz == lo && rz == ro && lz == rz) {
537                                                                         z = o = f;
538                                                                 } else {
539                                                                         goto result_unknown;
540                                                                 }
541                                                                 break;
542
543                                                         case ir_relation_equal:
544                                                                 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
545                                                                                 !tarval_is_null(tarval_andnot(lo, rz))) {
546                                                                         // At least one bit differs.
547                                                                         z = o = f;
548                                                                 } else if (lz == lo && rz == ro && lz == rz) {
549                                                                         z = o = t;
550                                                                 } else {
551                                                                         goto result_unknown;
552                                                                 }
553                                                                 break;
554
555                                                         case ir_relation_less_equal:
556                                                         case ir_relation_less:
557                                                                 /* TODO handle negative values */
558                                                                 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
559                                                                                 tarval_is_negative(rz) || tarval_is_negative(ro))
560                                                                         goto result_unknown;
561
562                                                                 if (tarval_cmp(lz, ro) & relation) {
563                                                                         /* Left upper bound is smaller(/equal) than right lower bound. */
564                                                                         z = o = t;
565                                                                 } else if (!(tarval_cmp(lo, rz) & relation)) {
566                                                                         /* Left lower bound is not smaller(/equal) than right upper bound. */
567                                                                         z = o = f;
568                                                                 } else {
569                                                                         goto result_unknown;
570                                                                 }
571                                                                 break;
572
573                                                         case ir_relation_greater_equal:
574                                                         case ir_relation_greater:
575                                                                 /* TODO handle negative values */
576                                                                 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
577                                                                                 tarval_is_negative(rz) || tarval_is_negative(ro))
578                                                                         goto result_unknown;
579
580                                                                 if (!(tarval_cmp(lz, ro) & relation)) {
581                                                                         /* Left upper bound is not greater(/equal) than right lower bound. */
582                                                                         z = o = f;
583                                                                 } else if (tarval_cmp(lo, rz) & relation) {
584                                                                         /* Left lower bound is greater(/equal) than right upper bound. */
585                                                                         z = o = t;
586                                                                 } else {
587                                                                         goto result_unknown;
588                                                                 }
589                                                                 break;
590
591                                                         default:
592                                                                 goto cannot_analyse;
593                                                 }
594                                         }
595                                         break;
596                                 }
597
598                                 default: {
599 cannot_analyse:
600                                         DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
601 result_unknown:
602                                         z = get_tarval_all_one(m);
603                                         o = get_tarval_null(m);
604                                         break;
605                                 }
606                         }
607                 }
608         } else {
609                 return 0;
610         }
611
612         return set_bitinfo(irn, z, o);
613 }
614
615 static void first_round(ir_node* const irn, void* const env)
616 {
617         pdeq* const q = (pdeq*)env;
618
619         transfer(irn);
620         if (is_Phi(irn) || is_Block(irn)) {
621                 /* Only Phis (and their users) need another round, if we did not have
622                  * information about all their inputs in the first round, i.e. in loops. */
623                 /* TODO inserts all Phis, should only insert Phis, which did no have all
624                  * predecessors available */
625                 pdeq_putr(q, irn);
626         }
627 }
628
629 static ir_node *make_bad_block(ir_graph *irg)
630 {
631         ir_node *bad = new_r_Bad(irg, mode_BB);
632         bitinfo *bb  = get_bitinfo(bad);
633         if (bb == NULL) {
634                 ir_tarval* const f = get_tarval_b_false();
635                 ir_tarval* const t = get_tarval_b_true();
636                 set_bitinfo(bad, f, t); /* Undefined. */
637         }
638         return bad;
639 }
640
641 static void apply_result(ir_node* const irn, void* ctx)
642 {
643         environment_t* env = (environment_t*)ctx;
644         ir_node*       block;
645         bitinfo*       block_b;
646         bitinfo*       b;
647         ir_tarval*     z;
648         ir_tarval*     o;
649
650         if (is_Block(irn)) {
651                 block_b = get_bitinfo(irn);
652                 /* Trivially unreachable blocks have no info. */
653                 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
654                         ir_node  *bad = make_bad_block(get_irn_irg(irn));
655                         exchange(irn, bad);
656                         env->modified = 1;
657                 }
658                 return;
659         }
660
661         block   = get_nodes_block(irn);
662         block_b = get_bitinfo(block);
663         /* Trivially unreachable blocks have no info. */
664         if (block_b == NULL || block_b->z == get_tarval_b_false()) {
665                 /* Unreachable blocks might be replaced before the nodes in them. */
666                 ir_mode  *mode = get_irn_mode(irn);
667                 ir_graph *irg  = get_irn_irg(irn);
668                 ir_node  *bad  = new_r_Bad(irg, mode);
669                 exchange(irn, bad);
670                 env->modified = 1;
671                 return;
672         }
673
674         b = get_bitinfo(irn);
675         if (!b) return;
676         if (is_Const(irn)) return; // It cannot get any better than a Const.
677
678         z = b->z;
679         o = b->o;
680         // Only display information if we could find out anything about the value.
681         DEBUG_ONLY(if (!tarval_is_all_one(z) || !tarval_is_null(o)))
682                 DB((dbg, LEVEL_2, "%+F: 0:%T 1:%T%s\n", irn, z, o, z == o ? " --- constant" : ""));
683
684         // Replace node with constant value by Const.
685         if (z == o) {
686                 ir_mode* const m = get_irn_mode(irn);
687                 ir_node*       n;
688                 if (mode_is_intb(m)) {
689                         ir_graph *irg = get_irn_irg(irn);
690                         n = new_r_Const(irg, z);
691                 } else if (m == mode_X) {
692                         ir_graph* const irg = get_Block_irg(block);
693                         if (z == get_tarval_b_true()) {
694                                 // Might produce an endless loop, so keep the block.
695                                 add_End_keepalive(get_irg_end(irg), block);
696                                 n = new_r_Jmp(block);
697                         } else {
698                                 n = new_r_Bad(irg, mode_X);
699                                 /* Transferring analysis information to the bad node makes it a
700                                  * candidate for replacement. */
701                                 goto exchange_only;
702                         }
703                 } else {
704                         return;
705                 }
706                 set_irn_link(n, b);
707 exchange_only:
708                 exchange(irn, n);
709                 env->modified = 1;
710         }
711
712         switch (get_irn_opcode(irn)) {
713                 case iro_And: {
714                         ir_node*       const l  = get_And_left(irn);
715                         ir_node*       const r  = get_And_right(irn);
716                         bitinfo const* const bl = get_bitinfo(l);
717                         bitinfo const* const br = get_bitinfo(r);
718                         if (bl->z == bl->o) {
719                                 if (tarval_is_null(tarval_andnot(br->z, bl->z))) {
720                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
721                                         exchange(irn, r);
722                                         env->modified = 1;
723                                 }
724                         } else if (br->z == br->o) {
725                                 if (tarval_is_null(tarval_andnot(bl->z, br->z))) {
726                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
727                                         exchange(irn, l);
728                                         env->modified = 1;
729                                 }
730                         }
731                         break;
732                 }
733
734                 case iro_Eor: {
735                         ir_node*       const l  = get_Eor_left(irn);
736                         ir_node*       const r  = get_Eor_right(irn);
737                         bitinfo const* const bl = get_bitinfo(l);
738                         bitinfo const* const br = get_bitinfo(r);
739                         /* if each bit is guaranteed to be zero on either the left or right
740                          * then an Add will have the same effect as the Eor. Change it for
741                          * normalisation */
742                         if (tarval_is_null(tarval_and(bl->z, br->z))) {
743                                 dbg_info      *dbgi     = get_irn_dbg_info(irn);
744                                 ir_node       *block    = get_nodes_block(irn);
745                                 ir_mode       *mode     = get_irn_mode(irn);
746                                 ir_node       *new_node = new_rd_Add(dbgi, block, l, r, mode);
747                                 bitinfo const *bi       = get_bitinfo(irn);
748                                 DB((dbg, LEVEL_2, "%+F(%+F, %+F) normalised to Add\n", irn, l, r));
749                                 set_bitinfo(new_node, bi->z, bi->o);
750                                 exchange(irn, new_node);
751                                 env->modified = 1;
752                         }
753                         break;
754                 }
755
756                 case iro_Or: {
757                         ir_node*       const l  = get_Or_left(irn);
758                         ir_node*       const r  = get_Or_right(irn);
759                         bitinfo const* const bl = get_bitinfo(l);
760                         bitinfo const* const br = get_bitinfo(r);
761                         if (bl->z == bl->o) {
762                                 if (tarval_is_null(tarval_andnot(bl->o, br->o))) {
763                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
764                                         exchange(irn, r);
765                                         env->modified = 1;
766                                 }
767                         } else if (br->z == br->o) {
768                                 if (tarval_is_null(tarval_andnot(br->o, bl->o))) {
769                                         DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
770                                         exchange(irn, l);
771                                         env->modified = 1;
772                                 }
773                         }
774
775                         /* if each bit is guaranteed to be zero on either the left or right
776                          * then an Add will have the same effect as the Or. Change it for
777                          * normalisation */
778                         if (tarval_is_null(tarval_and(bl->z, br->z))) {
779                                 dbg_info      *dbgi     = get_irn_dbg_info(irn);
780                                 ir_node       *block    = get_nodes_block(irn);
781                                 ir_mode       *mode     = get_irn_mode(irn);
782                                 ir_node       *new_node = new_rd_Add(dbgi, block, l, r, mode);
783                                 bitinfo const *bi       = get_bitinfo(irn);
784                                 DB((dbg, LEVEL_2, "%+F(%+F, %+F) normalised to Add\n", irn, l, r));
785                                 set_bitinfo(new_node, bi->z, bi->o);
786                                 exchange(irn, new_node);
787                                 env->modified = 1;
788                         }
789
790                         break;
791                 }
792         }
793 }
794
795 static void queue_users(pdeq* const q, ir_node* const n)
796 {
797         if (get_irn_mode(n) == mode_X) {
798                 /* When the state of a control flow node changes, not only queue its
799                  * successor blocks, but also the Phis in these blocks, because the Phis
800                  * must reconsider this input path. */
801                 ir_edge_t const* e;
802                 foreach_out_edge(n, e) {
803                         ir_node*  const  src = get_edge_src_irn(e);
804                         pdeq_putr(q, src);
805                         /* should always be a block */
806                         if (is_Block(src)) {
807                                 ir_node *phi;
808                                 for (phi = get_Block_phis(src); phi; phi = get_Phi_next(phi))
809                                         pdeq_putr(q, phi);
810                         }
811                 }
812         } else {
813                 ir_edge_t const* e;
814                 foreach_out_edge(n, e) {
815                         ir_node* const src = get_edge_src_irn(e);
816                         if (get_irn_mode(src) == mode_T) {
817                                 queue_users(q, src);
818                         } else {
819                                 pdeq_putr(q, src);
820                         }
821                 }
822         }
823 }
824
825 static void clear_links(ir_node *irn, void *env)
826 {
827         (void) env;
828         set_irn_link(irn, NULL);
829         if (is_Block(irn))
830                 set_Block_phis(irn, NULL);
831 }
832
833 static void build_phi_lists(ir_node *irn, void *env)
834 {
835         (void) env;
836         if (is_Phi(irn))
837                 add_Block_phi(get_nodes_block(irn), irn);
838 }
839
840 static ir_graph_state_t do_fixpoint_vrp(ir_graph* const irg)
841 {
842         environment_t env;
843         ir_graph_state_t res = 0;
844
845         FIRM_DBG_REGISTER(dbg, "firm.opt.fp-vrp");
846         DB((dbg, LEVEL_1, "===> Performing constant propagation on %+F\n", irg));
847
848         obstack_init(&obst);
849
850         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
851
852         {
853                 pdeq* const q = new_pdeq();
854
855                 /* We need this extra step because the dom tree does not contain
856                  * unreachable blocks in Firm. Moreover build phi list. */
857                 irg_walk_anchors(irg, clear_links, build_phi_lists, NULL);
858
859                 {
860                         ir_tarval* const f = get_tarval_b_false();
861                         ir_tarval* const t = get_tarval_b_true();
862                         set_bitinfo(get_irg_end_block(irg), t, f); /* Reachable. */
863                 }
864
865                 /* TODO Improve iteration order. Best is reverse postorder in data flow
866                  * direction and respecting loop nesting for fastest convergence. */
867                 irg_walk_blkwise_dom_top_down(irg, NULL, first_round, q);
868
869                 while (!pdeq_empty(q)) {
870                         ir_node* const n = (ir_node*)pdeq_getl(q);
871                         if (transfer(n))
872                                 queue_users(q, n);
873                 }
874
875                 del_pdeq(q);
876         }
877
878         DB((dbg, LEVEL_2, "---> Applying analysis results\n"));
879         env.modified = 0;
880         irg_walk_graph(irg, NULL, apply_result, &env);
881
882         if (! env.modified) {
883                 res |= IR_GRAPH_STATE_CONSISTENT_DOMINANCE | IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE;
884         }
885
886         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
887
888         obstack_free(&obst, NULL);
889
890         return res;
891 }
892
893 static optdesc_t opt_fpvrp = {
894         "fp-vrp",
895         IR_GRAPH_STATE_NO_BADS | IR_GRAPH_STATE_NO_UNREACHABLE_CODE | IR_GRAPH_STATE_CONSISTENT_DOMINANCE | IR_GRAPH_STATE_CONSISTENT_OUT_EDGES,
896         do_fixpoint_vrp,
897 };
898
899 void fixpoint_vrp(ir_graph* const irg)
900 {
901         perform_irg_optimization(irg, &opt_fpvrp);
902 }
903
904 ir_graph_pass_t *fixpoint_vrp_irg_pass(const char *name)
905 {
906         return def_graph_pass(name ? name : "fixpoint_vrp", fixpoint_vrp);
907 }