2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Data-flow driven minimal fixpoint value range propagation
23 * @author Christoph Mallon
30 #include "adt/xmalloc.h"
39 #include "iroptimize.h"
46 * - Implement cleared/set bit calculation for Add, Sub, Minus, Mul, Div, Mod, Shl, Shr, Shrs, Rotl
47 * - Implement min/max calculation for And, Eor, Or, Not, Conv, Shl, Shr, Shrs, Rotl, Mux
48 * - Implement min/max calculation for Add, Sub, Minus, Mul, Div, Mod, Conv, Shl, Shr, Shrs, Rotl, Mux
51 /* Tables of the cleared/set bit lattice
53 * Encoding of the lattice
56 * 01 - impossible state, is zero /and/ one
57 * 10 T top, may be either zero or one
71 * Half adder, half subtractor, and, xor, or, Mux
83 * Full adder, full subtractor
114 * Assume: Xmin <= Xmax and no overflow
115 * A + B = (Amin + Bmin, Amax + Bmax)
116 * -A = (-Amax, -Amin)
117 * A - B = A + -B = (Amin (-B)min, Amax + (-B)max) = (Amin - Bmax, Amax - Bmin)
120 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
122 static struct obstack obst;
124 typedef struct bitinfo
126 ir_tarval* z; // safe zeroes, 0 = bit is zero, 1 = bit maybe is 1
127 ir_tarval* o; // safe ones, 0 = bit maybe is zero, 1 = bit is 1
130 typedef struct environment_t {
131 unsigned modified:1; /**< Set, if the graph was modified. */
134 static inline bitinfo* get_bitinfo(ir_node const* const irn)
136 return (bitinfo*)get_irn_link(irn);
139 static int set_bitinfo(ir_node* const irn, ir_tarval* const z, ir_tarval* const o)
141 bitinfo* b = get_bitinfo(irn);
143 b = OALLOCZ(&obst, bitinfo);
144 set_irn_link(irn, b);
145 } else if (z == b->z && o == b->o) {
150 DB((dbg, LEVEL_3, "%+F: 0:%T 1:%T\n", irn, z, o));
154 static int mode_is_intb(ir_mode const* const m)
156 return mode_is_int(m) || m == mode_b;
159 static int transfer(ir_node* const irn)
161 ir_mode* const m = get_irn_mode(irn);
166 ir_tarval* const f = get_tarval_b_false();
167 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
169 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
171 if (b->z == f && b->o == f) {
174 } else switch (get_irn_opcode(irn)) {
176 ir_node* const pred = get_Proj_pred(irn);
177 if (is_Start(pred)) {
178 goto result_unknown_X;
179 } else if (is_Cond(pred)) {
180 ir_node* const selector = get_Cond_selector(pred);
181 bitinfo* const b = get_bitinfo(selector);
182 ir_tarval* const bz = b->z;
183 ir_tarval* const bo = b->o;
184 if (get_irn_mode(selector) == mode_b) {
186 if ((bz == get_tarval_b_true()) == get_Proj_proj(irn)) {
187 z = o = get_tarval_b_true();
189 z = o = get_tarval_b_false();
192 goto result_unknown_X;
195 long const val = get_Proj_proj(irn);
196 if (val != get_Cond_default_proj(pred)) {
197 ir_tarval* const tv = new_tarval_from_long(val, get_irn_mode(selector));
198 if (!tarval_is_null(tarval_andnot(tv, bz)) ||
199 !tarval_is_null(tarval_andnot(bo, tv))) {
200 // At least one bit differs.
201 z = o = get_tarval_b_false();
202 #if 0 // TODO must handle default Proj
203 } else if (bz == bo && bz == tv) {
204 z = o = get_tarval_b_true();
207 goto result_unknown_X;
210 goto cannot_analyse_X;
214 goto cannot_analyse_X;
220 goto result_unknown_X;
224 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
226 z = get_tarval_b_true();
227 o = get_tarval_b_false();
230 } else if (is_Block(irn)) {
232 int const arity = get_Block_n_cfgpreds(irn);
235 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
236 for (i = 0; i != arity; ++i) {
237 bitinfo* const b = get_bitinfo(get_Block_cfgpred(irn, i));
238 if (b != NULL && b->z == get_tarval_b_true()) {
245 ir_graph *const irg = get_Block_irg(irn);
247 irn == get_irg_start_block(irg) ||
248 irn == get_irg_end_block(irg);
251 o = get_tarval_b_false();
252 z = reachable ? get_tarval_b_true() : o;
253 } else if (mode_is_intb(m)) {
254 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
255 switch (get_irn_opcode(irn)) {
257 z = o = get_Const_tarval(irn);
262 bitinfo* const l = get_bitinfo(get_Shl_left(irn));
263 bitinfo* const r = get_bitinfo(get_Shl_right(irn));
264 ir_tarval* const rz = r->z;
266 z = tarval_shl(l->z, rz);
267 o = tarval_shl(l->o, rz);
275 bitinfo* const l = get_bitinfo(get_Shr_left(irn));
276 bitinfo* const r = get_bitinfo(get_Shr_right(irn));
277 ir_tarval* const rz = r->z;
279 z = tarval_shr(l->z, rz);
280 o = tarval_shr(l->o, rz);
288 bitinfo* const l = get_bitinfo(get_Shrs_left(irn));
289 bitinfo* const r = get_bitinfo(get_Shrs_right(irn));
290 ir_tarval* const rz = r->z;
292 z = tarval_shrs(l->z, rz);
293 o = tarval_shrs(l->o, rz);
301 bitinfo* const l = get_bitinfo(get_Rotl_left(irn));
302 bitinfo* const r = get_bitinfo(get_Rotl_right(irn));
303 ir_tarval* const rz = r->z;
305 z = tarval_rotl(l->z, rz);
306 o = tarval_rotl(l->o, rz);
314 bitinfo* const l = get_bitinfo(get_Add_left(irn));
315 bitinfo* const r = get_bitinfo(get_Add_right(irn));
316 ir_tarval* const lz = l->z;
317 ir_tarval* const lo = l->o;
318 ir_tarval* const rz = r->z;
319 ir_tarval* const ro = r->o;
320 if (lz == lo && rz == ro) {
321 z = o = tarval_add(lz, rz);
323 // TODO improve: can only do lower disjoint bits
324 /* Determine where any of the operands has zero bits, i.e. where no
325 * carry out is generated if there is not carry in */
326 ir_tarval* const no_c_in_no_c_out = tarval_and(lz, rz);
327 /* Generate a mask of the lower consecutive zeroes: x | -x. In this
328 * range the addition is disjoint and therefore Add behaves like Or.
330 ir_tarval* const low_zero_mask = tarval_or(no_c_in_no_c_out, tarval_neg(no_c_in_no_c_out));
331 ir_tarval* const low_one_mask = tarval_not(low_zero_mask);
332 z = tarval_or( tarval_or(lz, rz), low_zero_mask);
333 o = tarval_and(tarval_or(lo, ro), low_one_mask);
339 bitinfo* const l = get_bitinfo(get_Sub_left(irn));
340 bitinfo* const r = get_bitinfo(get_Sub_right(irn));
341 if (l != NULL && r != NULL) { // Sub might subtract pointers.
342 ir_tarval* const lz = l->z;
343 ir_tarval* const lo = l->o;
344 ir_tarval* const rz = r->z;
345 ir_tarval* const ro = r->o;
346 if (lz == lo && rz == ro) {
347 z = o = tarval_sub(lz, rz, NULL);
348 } else if (tarval_is_null(tarval_andnot(rz, lo))) {
349 /* Every possible one of the subtrahend is backed by a safe one of the
350 * minuend, i.e. there are no borrows. */
351 // TODO extend no-borrow like carry for Add above
352 z = tarval_andnot(lz, ro);
353 o = tarval_andnot(lo, rz);
364 bitinfo* const l = get_bitinfo(get_Mul_left(irn));
365 bitinfo* const r = get_bitinfo(get_Mul_right(irn));
366 ir_tarval* const lz = l->z;
367 ir_tarval* const lo = l->o;
368 ir_tarval* const rz = r->z;
369 ir_tarval* const ro = r->o;
370 if (lz == lo && rz == ro) {
371 z = o = tarval_mul(lz, rz);
374 // Determine safe lower zeroes: x | -x.
375 ir_tarval* const lzn = tarval_or(lz, tarval_neg(lz));
376 ir_tarval* const rzn = tarval_or(rz, tarval_neg(rz));
377 // Concatenate safe lower zeroes.
378 if (tarval_cmp(lzn, rzn) == ir_relation_less) {
379 z = tarval_mul(tarval_eor(lzn, tarval_shl(lzn, get_tarval_one(m))), rzn);
381 z = tarval_mul(tarval_eor(rzn, tarval_shl(rzn, get_tarval_one(m))), lzn);
383 o = get_tarval_null(m);
389 bitinfo* const b = get_bitinfo(get_Minus_op(irn));
391 z = o = tarval_neg(b->z);
399 bitinfo* const l = get_bitinfo(get_And_left(irn));
400 bitinfo* const r = get_bitinfo(get_And_right(irn));
401 z = tarval_and(l->z, r->z);
402 o = tarval_and(l->o, r->o);
407 bitinfo* const l = get_bitinfo(get_Or_left(irn));
408 bitinfo* const r = get_bitinfo(get_Or_right(irn));
409 z = tarval_or(l->z, r->z);
410 o = tarval_or(l->o, r->o);
415 bitinfo* const l = get_bitinfo(get_Eor_left(irn));
416 bitinfo* const r = get_bitinfo(get_Eor_right(irn));
417 ir_tarval* const lz = l->z;
418 ir_tarval* const lo = l->o;
419 ir_tarval* const rz = r->z;
420 ir_tarval* const ro = r->o;
421 z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
422 o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
427 bitinfo* const b = get_bitinfo(get_Not_op(irn));
428 z = tarval_not(b->o);
429 o = tarval_not(b->z);
434 bitinfo* const b = get_bitinfo(get_Conv_op(irn));
435 if (b == NULL) // Happens when converting from float values.
437 z = tarval_convert_to(b->z, m);
438 o = tarval_convert_to(b->o, m);
443 bitinfo* const f = get_bitinfo(get_Mux_false(irn));
444 bitinfo* const t = get_bitinfo(get_Mux_true(irn));
445 bitinfo* const c = get_bitinfo(get_Mux_sel(irn));
446 if (c->o == get_tarval_b_true()) {
449 } else if (c->z == get_tarval_b_false()) {
453 z = tarval_or( f->z, t->z);
454 o = tarval_and(f->o, t->o);
460 ir_node* const block = get_nodes_block(irn);
461 int const arity = get_Phi_n_preds(irn);
464 z = get_tarval_null(m);
465 o = get_tarval_all_one(m);
466 for (i = 0; i != arity; ++i) {
467 bitinfo* const b_cfg = get_bitinfo(get_Block_cfgpred(block, i));
468 if (b_cfg != NULL && b_cfg->z != get_tarval_b_false()) {
469 bitinfo* const b = get_bitinfo(get_Phi_pred(irn, i));
470 z = tarval_or( z, b->z);
471 o = tarval_and(o, b->o);
478 bitinfo* const l = get_bitinfo(get_Cmp_left(irn));
479 bitinfo* const r = get_bitinfo(get_Cmp_right(irn));
480 if (l == NULL || r == NULL) {
481 goto result_unknown; // Cmp compares something we cannot evaluate.
483 ir_tarval* const lz = l->z;
484 ir_tarval* const lo = l->o;
485 ir_tarval* const rz = r->z;
486 ir_tarval* const ro = r->o;
487 ir_relation const relation = get_Cmp_relation(irn);
489 case ir_relation_less_greater:
490 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
491 !tarval_is_null(tarval_andnot(lo, rz))) {
492 // At least one bit differs.
493 z = o = get_tarval_b_true();
494 } else if (lz == lo && rz == ro && lz == rz) {
495 z = o = get_tarval_b_false();
501 case ir_relation_equal:
502 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
503 !tarval_is_null(tarval_andnot(lo, rz))) {
504 // At least one bit differs.
505 z = o = get_tarval_b_false();
506 } else if (lz == lo && rz == ro && lz == rz) {
507 z = o = get_tarval_b_true();
513 case ir_relation_less_equal:
514 case ir_relation_less:
515 /* TODO handle negative values */
516 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
517 tarval_is_negative(rz) || tarval_is_negative(ro))
520 if (tarval_cmp(lz, ro) & relation) {
521 /* Left upper bound is smaller(/equal) than right lower bound. */
522 z = o = get_tarval_b_true();
523 } else if (!(tarval_cmp(lo, rz) & relation)) {
524 /* Left lower bound is not smaller(/equal) than right upper bound. */
525 z = o = get_tarval_b_false();
531 case ir_relation_greater_equal:
532 case ir_relation_greater:
533 /* TODO handle negative values */
534 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
535 tarval_is_negative(rz) || tarval_is_negative(ro))
538 if (!(tarval_cmp(lz, ro) & relation)) {
539 /* Left upper bound is not greater(/equal) than right lower bound. */
540 z = o = get_tarval_b_false();
541 } else if (tarval_cmp(lo, rz) & relation) {
542 /* Left lower bound is greater(/equal) than right upper bound. */
543 z = o = get_tarval_b_true();
558 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
560 z = get_tarval_all_one(m);
561 o = get_tarval_null(m);
569 return set_bitinfo(irn, z, o);
572 static void first_round(ir_node* const irn, void* const env)
574 pdeq* const q = (pdeq*)env;
577 if (is_Phi(irn) || is_Block(irn)) {
578 /* Only Phis (and their users) need another round, if we did not have
579 * information about all their inputs in the first round, i.e. in loops. */
580 /* TODO inserts all Phis, should only insert Phis, which did no have all
581 * predecessors available */
586 static void apply_result(ir_node* const irn, void* ctx)
588 environment_t* env = (environment_t*)ctx;
595 bitinfo* const block_b = get_bitinfo(irn);
596 /* Trivially unreachable blocks have no info. */
597 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
598 exchange(irn, get_irg_bad(get_Block_irg(irn)));
604 /* Unreachable blocks are replaced before the nodes in them. */
605 block = get_nodes_block(irn);
607 exchange(irn, block);
612 b = get_bitinfo(irn);
614 if (is_Const(irn)) return; // It cannot get any better than a Const.
618 // Only display information if we could find out anything about the value.
619 DEBUG_ONLY(if (!tarval_is_all_one(z) || !tarval_is_null(o)))
620 DB((dbg, LEVEL_2, "%+F: 0:%T 1:%T%s\n", irn, z, o, z == o ? " --- constant" : ""));
622 // Replace node with constant value by Const.
624 ir_mode* const m = get_irn_mode(irn);
626 if (mode_is_intb(m)) {
627 ir_graph *irg = get_irn_irg(irn);
628 n = new_r_Const(irg, z);
629 } else if (m == mode_X) {
630 ir_graph* const irg = get_Block_irg(block);
631 if (z == get_tarval_b_true()) {
632 // Might produce an endless loop, so keep the block.
633 add_End_keepalive(get_irg_end(irg), block);
634 n = new_r_Jmp(block);
637 /* Transferring analysis information to the bad node makes it a
638 * candidate for replacement. */
650 switch (get_irn_opcode(irn)) {
652 ir_node* const l = get_And_left(irn);
653 ir_node* const r = get_And_right(irn);
654 bitinfo const* const bl = get_bitinfo(l);
655 bitinfo const* const br = get_bitinfo(r);
656 if (bl->z == bl->o) {
657 if (tarval_is_null(tarval_andnot(br->z, bl->z))) {
658 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
662 } else if (br->z == br->o) {
663 if (tarval_is_null(tarval_andnot(bl->z, br->z))) {
664 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
673 ir_node* const l = get_Or_left(irn);
674 ir_node* const r = get_Or_right(irn);
675 bitinfo const* const bl = get_bitinfo(l);
676 bitinfo const* const br = get_bitinfo(r);
677 if (bl->z == bl->o) {
678 if (tarval_is_null(tarval_andnot(bl->o, br->o))) {
679 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
683 } else if (br->z == br->o) {
684 if (tarval_is_null(tarval_andnot(br->o, bl->o))) {
685 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
695 static void queue_users(pdeq* const q, ir_node* const n)
697 if (get_irn_mode(n) == mode_X) {
698 /* When the state of a control flow node changes, not only queue its
699 * successor blocks, but also the Phis in these blocks, because the Phis
700 * must reconsider this input path. */
702 foreach_out_edge(n, e) {
703 ir_node* const src = get_edge_src_irn(e);
705 /* should always be a block */
708 for (phi = get_Block_phis(src); phi; phi = get_Phi_next(phi))
714 foreach_out_edge(n, e) {
715 ir_node* const src = get_edge_src_irn(e);
716 if (get_irn_mode(src) == mode_T) {
725 static void clear_links(ir_node *irn, void *env)
728 set_irn_link(irn, NULL);
730 set_Block_phis(irn, NULL);
733 static void build_phi_lists(ir_node *irn, void *env)
737 add_Block_phi(get_nodes_block(irn), irn);
740 void fixpoint_vrp(ir_graph* const irg)
744 FIRM_DBG_REGISTER(dbg, "firm.opt.fp-vrp");
745 DB((dbg, LEVEL_1, "===> Performing constant propagation on %+F\n", irg));
749 /* HACK: to avoid finding dead code */
750 edges_deactivate(irg);
756 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
759 pdeq* const q = new_pdeq();
761 /* We need this extra step because the dom tree does not contain unreachable
762 blocks in Firm. Moreover build phi list. */
763 irg_walk_anchors(irg, clear_links, build_phi_lists, NULL);
765 /* TODO Improve iteration order. Best is reverse postorder in data flow
766 * direction and respecting loop nesting for fastest convergence. */
767 irg_walk_blkwise_dom_top_down(irg, NULL, first_round, q);
769 while (!pdeq_empty(q)) {
770 ir_node* const n = (ir_node*)pdeq_getl(q);
778 DB((dbg, LEVEL_2, "---> Applying analysis results\n"));
780 irg_walk_graph(irg, NULL, apply_result, &env);
783 /* control flow might changed */
784 set_irg_outs_inconsistent(irg);
785 set_irg_extblk_inconsistent(irg);
786 set_irg_doms_inconsistent(irg);
787 set_irg_loopinfo_inconsistent(irg);
788 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
791 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
793 obstack_free(&obst, NULL);
796 ir_graph_pass_t *fixpoint_vrp_irg_pass(const char *name)
798 return def_graph_pass(name ? name : "fixpoint_vrp", fixpoint_vrp);