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