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
33 #include "adt/xmalloc.h"
42 #include "iroptimize.h"
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
54 /* Tables of the cleared/set bit lattice
56 * Encoding of the lattice
59 * 01 - impossible state, is zero /and/ one
60 * 10 T top, may be either zero or one
74 * Half adder, half subtractor, and, xor, or, Mux
86 * Full adder, full subtractor
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)
123 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
125 static struct obstack obst;
127 typedef struct bitinfo
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
133 typedef struct environment_t {
134 unsigned modified:1; /**< Set, if the graph was modified. */
137 static bool is_undefined(bitinfo const* const b)
139 return tarval_is_null(b->z) && tarval_is_all_one(b->o);
142 static inline bitinfo* get_bitinfo(ir_node const* const irn)
144 return (bitinfo*)get_irn_link(irn);
147 static int set_bitinfo(ir_node* const irn, ir_tarval* const z, ir_tarval* const o)
149 bitinfo* b = get_bitinfo(irn);
151 b = OALLOCZ(&obst, bitinfo);
152 set_irn_link(irn, b);
153 } else if (z == b->z && o == b->o) {
156 /* Assert monotonicity. */
157 assert(tarval_is_null(tarval_andnot(b->z, z)));
158 assert(tarval_is_null(tarval_andnot(o, b->o)));
162 DB((dbg, LEVEL_3, "%+F: 0:%T 1:%T\n", irn, z, o));
166 static int mode_is_intb(ir_mode const* const m)
168 return mode_is_int(m) || m == mode_b;
171 static int transfer(ir_node* const irn)
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);
180 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
182 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
184 /* Unreachble blocks might have no bitinfo. */
185 if (b == NULL || b->z == f) {
189 } else switch (get_irn_opcode(irn)) {
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)) {
199 } else if (get_irn_mode(selector) == mode_b) {
201 if ((b->z == t) == get_Proj_proj(irn)) {
207 goto result_unknown_X;
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.
217 #if 0 // TODO must handle default Proj
218 } else if (b->z == b->o && b->z == tv) {
222 goto result_unknown_X;
225 goto cannot_analyse_X;
229 goto cannot_analyse_X;
235 goto result_unknown_X;
239 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
245 } else if (is_Block(irn)) {
247 int const arity = get_Block_n_cfgpreds(irn);
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) {
260 ir_graph *const irg = get_Block_irg(irn);
262 irn == get_irg_start_block(irg) ||
263 irn == get_irg_end_block(irg);
273 } else if (mode_is_intb(m)) {
274 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
276 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
278 if (b == NULL || b->z == f) {
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);
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);
301 int const arity = get_irn_arity(irn);
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))
312 switch (get_irn_opcode(irn)) {
314 z = o = get_Const_tarval(irn);
319 ir_node* const v = get_Confirm_value(irn);
320 bitinfo* const b = get_bitinfo(v);
321 /* TODO Use bound and relation. */
328 bitinfo* const l = get_bitinfo(get_Shl_left(irn));
329 bitinfo* const r = get_bitinfo(get_Shl_right(irn));
330 ir_tarval* const rz = r->z;
332 z = tarval_shl(l->z, rz);
333 o = tarval_shl(l->o, rz);
341 bitinfo* const l = get_bitinfo(get_Shr_left(irn));
342 bitinfo* const r = get_bitinfo(get_Shr_right(irn));
343 ir_tarval* const rz = r->z;
345 z = tarval_shr(l->z, rz);
346 o = tarval_shr(l->o, rz);
354 bitinfo* const l = get_bitinfo(get_Shrs_left(irn));
355 bitinfo* const r = get_bitinfo(get_Shrs_right(irn));
356 ir_tarval* const rz = r->z;
358 z = tarval_shrs(l->z, rz);
359 o = tarval_shrs(l->o, rz);
367 bitinfo* const l = get_bitinfo(get_Rotl_left(irn));
368 bitinfo* const r = get_bitinfo(get_Rotl_right(irn));
369 ir_tarval* const rz = r->z;
371 z = tarval_rotl(l->z, rz);
372 o = tarval_rotl(l->o, rz);
380 bitinfo* const l = get_bitinfo(get_Add_left(irn));
381 bitinfo* const r = get_bitinfo(get_Add_right(irn));
382 ir_tarval* const lz = l->z;
383 ir_tarval* const lo = l->o;
384 ir_tarval* const rz = r->z;
385 ir_tarval* const ro = r->o;
386 if (lz == lo && rz == ro) {
387 z = o = tarval_add(lz, rz);
389 // TODO improve: can only do lower disjoint bits
390 /* Determine where any of the operands has zero bits, i.e. where no
391 * carry out is generated if there is not carry in */
392 ir_tarval* const no_c_in_no_c_out = tarval_and(lz, rz);
393 /* Generate a mask of the lower consecutive zeroes: x | -x. In this
394 * range the addition is disjoint and therefore Add behaves like Or.
396 ir_tarval* const low_zero_mask = tarval_or(no_c_in_no_c_out, tarval_neg(no_c_in_no_c_out));
397 ir_tarval* const low_one_mask = tarval_not(low_zero_mask);
398 z = tarval_or( tarval_or(lz, rz), low_zero_mask);
399 o = tarval_and(tarval_or(lo, ro), low_one_mask);
405 bitinfo* const l = get_bitinfo(get_Sub_left(irn));
406 bitinfo* const r = get_bitinfo(get_Sub_right(irn));
407 if (l != NULL && r != NULL) { // Sub might subtract pointers.
408 ir_tarval* const lz = l->z;
409 ir_tarval* const lo = l->o;
410 ir_tarval* const rz = r->z;
411 ir_tarval* const ro = r->o;
412 if (lz == lo && rz == ro) {
413 z = o = tarval_sub(lz, rz, NULL);
414 } else if (tarval_is_null(tarval_andnot(rz, lo))) {
415 /* Every possible one of the subtrahend is backed by a safe one of the
416 * minuend, i.e. there are no borrows. */
417 // TODO extend no-borrow like carry for Add above
418 z = tarval_andnot(lz, ro);
419 o = tarval_andnot(lo, rz);
430 bitinfo* const l = get_bitinfo(get_Mul_left(irn));
431 bitinfo* const r = get_bitinfo(get_Mul_right(irn));
432 ir_tarval* const lz = l->z;
433 ir_tarval* const lo = l->o;
434 ir_tarval* const rz = r->z;
435 ir_tarval* const ro = r->o;
436 if (lz == lo && rz == ro) {
437 z = o = tarval_mul(lz, rz);
440 // Determine safe lower zeroes: x | -x.
441 ir_tarval* const lzn = tarval_or(lz, tarval_neg(lz));
442 ir_tarval* const rzn = tarval_or(rz, tarval_neg(rz));
443 // Concatenate safe lower zeroes.
444 if (tarval_cmp(lzn, rzn) == ir_relation_less) {
445 z = tarval_mul(tarval_eor(lzn, tarval_shl(lzn, get_tarval_one(m))), rzn);
447 z = tarval_mul(tarval_eor(rzn, tarval_shl(rzn, get_tarval_one(m))), lzn);
449 o = get_tarval_null(m);
455 bitinfo* const b = get_bitinfo(get_Minus_op(irn));
457 z = o = tarval_neg(b->z);
465 bitinfo* const l = get_bitinfo(get_And_left(irn));
466 bitinfo* const r = get_bitinfo(get_And_right(irn));
467 z = tarval_and(l->z, r->z);
468 o = tarval_and(l->o, r->o);
473 bitinfo* const l = get_bitinfo(get_Or_left(irn));
474 bitinfo* const r = get_bitinfo(get_Or_right(irn));
475 z = tarval_or(l->z, r->z);
476 o = tarval_or(l->o, r->o);
481 bitinfo* const l = get_bitinfo(get_Eor_left(irn));
482 bitinfo* const r = get_bitinfo(get_Eor_right(irn));
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 z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
488 o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
493 bitinfo* const b = get_bitinfo(get_Not_op(irn));
494 z = tarval_not(b->o);
495 o = tarval_not(b->z);
500 bitinfo* const b = get_bitinfo(get_Conv_op(irn));
501 if (b == NULL) // Happens when converting from float values.
503 z = tarval_convert_to(b->z, m);
504 o = tarval_convert_to(b->o, m);
509 bitinfo* const bf = get_bitinfo(get_Mux_false(irn));
510 bitinfo* const bt = get_bitinfo(get_Mux_true(irn));
511 bitinfo* const c = get_bitinfo(get_Mux_sel(irn));
515 } else if (c->z == f) {
519 z = tarval_or( bf->z, bt->z);
520 o = tarval_and(bf->o, bt->o);
526 bitinfo* const l = get_bitinfo(get_Cmp_left(irn));
527 bitinfo* const r = get_bitinfo(get_Cmp_right(irn));
528 if (l == NULL || r == NULL) {
529 goto result_unknown; // Cmp compares something we cannot evaluate.
531 ir_tarval* const lz = l->z;
532 ir_tarval* const lo = l->o;
533 ir_tarval* const rz = r->z;
534 ir_tarval* const ro = r->o;
535 ir_relation const relation = get_Cmp_relation(irn);
537 case ir_relation_less_greater:
538 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
539 !tarval_is_null(tarval_andnot(lo, rz))) {
540 // At least one bit differs.
542 } else if (lz == lo && rz == ro && lz == rz) {
549 case ir_relation_equal:
550 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
551 !tarval_is_null(tarval_andnot(lo, rz))) {
552 // At least one bit differs.
554 } else if (lz == lo && rz == ro && lz == rz) {
561 case ir_relation_less_equal:
562 case ir_relation_less:
563 /* TODO handle negative values */
564 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
565 tarval_is_negative(rz) || tarval_is_negative(ro))
568 if (tarval_cmp(lz, ro) & relation) {
569 /* Left upper bound is smaller(/equal) than right lower bound. */
571 } else if (!(tarval_cmp(lo, rz) & relation)) {
572 /* Left lower bound is not smaller(/equal) than right upper bound. */
579 case ir_relation_greater_equal:
580 case ir_relation_greater:
581 /* TODO handle negative values */
582 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
583 tarval_is_negative(rz) || tarval_is_negative(ro))
586 if (!(tarval_cmp(lz, ro) & relation)) {
587 /* Left upper bound is not greater(/equal) than right lower bound. */
589 } else if (tarval_cmp(lo, rz) & relation) {
590 /* Left lower bound is greater(/equal) than right upper bound. */
606 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
608 z = get_tarval_all_one(m);
609 o = get_tarval_null(m);
618 return set_bitinfo(irn, z, o);
621 static void first_round(ir_node* const irn, void* const env)
623 pdeq* const q = (pdeq*)env;
626 if (is_Phi(irn) || is_Block(irn)) {
627 /* Only Phis (and their users) need another round, if we did not have
628 * information about all their inputs in the first round, i.e. in loops. */
629 /* TODO inserts all Phis, should only insert Phis, which did no have all
630 * predecessors available */
635 static void apply_result(ir_node* const irn, void* ctx)
637 environment_t* env = (environment_t*)ctx;
645 block_b = get_bitinfo(irn);
646 /* Trivially unreachable blocks have no info. */
647 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
648 exchange(irn, get_irg_bad(get_Block_irg(irn)));
654 block = get_nodes_block(irn);
655 block_b = get_bitinfo(block);
656 /* Trivially unreachable blocks have no info. */
657 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
658 /* Unreachable blocks might be replaced before the nodes in them. */
659 exchange(irn, is_Bad(block) ? block : get_irg_bad(get_Block_irg(block)));
664 b = get_bitinfo(irn);
666 if (is_Const(irn)) return; // It cannot get any better than a Const.
670 // Only display information if we could find out anything about the value.
671 DEBUG_ONLY(if (!tarval_is_all_one(z) || !tarval_is_null(o)))
672 DB((dbg, LEVEL_2, "%+F: 0:%T 1:%T%s\n", irn, z, o, z == o ? " --- constant" : ""));
674 // Replace node with constant value by Const.
676 ir_mode* const m = get_irn_mode(irn);
678 if (mode_is_intb(m)) {
679 ir_graph *irg = get_irn_irg(irn);
680 n = new_r_Const(irg, z);
681 } else if (m == mode_X) {
682 ir_graph* const irg = get_Block_irg(block);
683 if (z == get_tarval_b_true()) {
684 // Might produce an endless loop, so keep the block.
685 add_End_keepalive(get_irg_end(irg), block);
686 n = new_r_Jmp(block);
689 /* Transferring analysis information to the bad node makes it a
690 * candidate for replacement. */
702 switch (get_irn_opcode(irn)) {
704 ir_node* const l = get_And_left(irn);
705 ir_node* const r = get_And_right(irn);
706 bitinfo const* const bl = get_bitinfo(l);
707 bitinfo const* const br = get_bitinfo(r);
708 if (bl->z == bl->o) {
709 if (tarval_is_null(tarval_andnot(br->z, bl->z))) {
710 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
714 } else if (br->z == br->o) {
715 if (tarval_is_null(tarval_andnot(bl->z, br->z))) {
716 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
725 ir_node* const l = get_Or_left(irn);
726 ir_node* const r = get_Or_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(bl->o, br->o))) {
731 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
735 } else if (br->z == br->o) {
736 if (tarval_is_null(tarval_andnot(br->o, bl->o))) {
737 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
747 static void queue_users(pdeq* const q, ir_node* const n)
749 if (get_irn_mode(n) == mode_X) {
750 /* When the state of a control flow node changes, not only queue its
751 * successor blocks, but also the Phis in these blocks, because the Phis
752 * must reconsider this input path. */
754 foreach_out_edge(n, e) {
755 ir_node* const src = get_edge_src_irn(e);
757 /* should always be a block */
760 for (phi = get_Block_phis(src); phi; phi = get_Phi_next(phi))
766 foreach_out_edge(n, e) {
767 ir_node* const src = get_edge_src_irn(e);
768 if (get_irn_mode(src) == mode_T) {
777 static void clear_links(ir_node *irn, void *env)
780 set_irn_link(irn, NULL);
782 set_Block_phis(irn, NULL);
785 static void build_phi_lists(ir_node *irn, void *env)
789 add_Block_phi(get_nodes_block(irn), irn);
792 void fixpoint_vrp(ir_graph* const irg)
796 FIRM_DBG_REGISTER(dbg, "firm.opt.fp-vrp");
797 DB((dbg, LEVEL_1, "===> Performing constant propagation on %+F\n", irg));
801 /* HACK: to avoid finding dead code */
802 edges_deactivate(irg);
808 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
811 pdeq* const q = new_pdeq();
813 /* We need this extra step because the dom tree does not contain unreachable
814 blocks in Firm. Moreover build phi list. */
815 irg_walk_anchors(irg, clear_links, build_phi_lists, NULL);
817 { ir_tarval* const f = get_tarval_b_false();
818 ir_tarval* const t = get_tarval_b_true();
819 set_bitinfo(get_irg_bad(irg), f, t); /* Undefined. */
820 set_bitinfo(get_irg_end_block(irg), t, f); /* Reachable. */
823 /* TODO Improve iteration order. Best is reverse postorder in data flow
824 * direction and respecting loop nesting for fastest convergence. */
825 irg_walk_blkwise_dom_top_down(irg, NULL, first_round, q);
827 while (!pdeq_empty(q)) {
828 ir_node* const n = (ir_node*)pdeq_getl(q);
836 DB((dbg, LEVEL_2, "---> Applying analysis results\n"));
838 irg_walk_graph(irg, NULL, apply_result, &env);
841 /* control flow might changed */
842 set_irg_outs_inconsistent(irg);
843 set_irg_extblk_inconsistent(irg);
844 set_irg_doms_inconsistent(irg);
845 set_irg_loopinfo_inconsistent(irg);
846 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
849 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
851 obstack_free(&obst, NULL);
854 ir_graph_pass_t *fixpoint_vrp_irg_pass(const char *name)
856 return def_graph_pass(name ? name : "fixpoint_vrp", fixpoint_vrp);