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"
39 #include "irgraph_t.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);
179 if (is_Bad(irn)) return 0;
182 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
184 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
186 /* Unreachble blocks might have no bitinfo. */
187 if (b == NULL || b->z == f) {
191 } else switch (get_irn_opcode(irn)) {
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)) {
201 } else if (get_irn_mode(selector) == mode_b) {
203 if ((b->z == t) == get_Proj_proj(irn)) {
209 goto result_unknown_X;
212 long const val = get_Proj_proj(irn);
213 if (val != get_Cond_default_proj(pred)) {
214 ir_tarval* const tv = new_tarval_from_long(val, get_irn_mode(selector));
215 if (!tarval_is_null(tarval_andnot(tv, b->z)) ||
216 !tarval_is_null(tarval_andnot(b->o, tv))) {
217 // At least one bit differs.
219 #if 0 // TODO must handle default Proj
220 } else if (b->z == b->o && b->z == tv) {
224 goto result_unknown_X;
227 goto cannot_analyse_X;
231 goto cannot_analyse_X;
237 goto result_unknown_X;
241 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
247 } else if (is_Block(irn)) {
249 int const arity = get_Block_n_cfgpreds(irn);
252 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
253 for (i = 0; i != arity; ++i) {
254 bitinfo* const b = get_bitinfo(get_Block_cfgpred(irn, i));
255 if (b != NULL && b->z == t) {
262 ir_graph *const irg = get_Block_irg(irn);
264 irn == get_irg_start_block(irg) ||
265 irn == get_irg_end_block(irg);
275 } else if (mode_is_intb(m)) {
276 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
278 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
280 if (b == NULL || b->z == f) {
282 z = get_tarval_null(m);
283 o = get_tarval_all_one(m);
284 } else if (is_Phi(irn)) {
285 ir_node* const block = get_nodes_block(irn);
286 int const arity = get_Phi_n_preds(irn);
289 z = get_tarval_null(m);
290 o = get_tarval_all_one(m);
291 for (i = 0; i != arity; ++i) {
292 bitinfo* const b_cfg = get_bitinfo(get_Block_cfgpred(block, i));
293 if (b_cfg != NULL && b_cfg->z != f) {
294 bitinfo* const b = get_bitinfo(get_Phi_pred(irn, i));
295 /* Only use input if it's not undefined. */
296 if (!is_undefined(b)) {
297 z = tarval_or( z, b->z);
298 o = tarval_and(o, b->o);
303 int const arity = get_irn_arity(irn);
306 /* Undefined if any input is undefined. */
307 for (i = 0; i != arity; ++i) {
308 ir_node* const pred = get_irn_n(irn, i);
309 bitinfo* const pred_b = get_bitinfo(pred);
310 if (pred_b != NULL && is_undefined(pred_b))
314 switch (get_irn_opcode(irn)) {
316 z = o = get_Const_tarval(irn);
321 ir_node* const v = get_Confirm_value(irn);
322 bitinfo* const b = get_bitinfo(v);
323 /* TODO Use bound and relation. */
326 if ((get_Confirm_relation(irn) & ~ir_relation_unordered) == ir_relation_equal) {
327 bitinfo* const bound_b = get_bitinfo(get_Confirm_bound(irn));
328 z = tarval_and(z, bound_b->z);
329 o = tarval_or( o, bound_b->o);
335 bitinfo* const l = get_bitinfo(get_Shl_left(irn));
336 bitinfo* const r = get_bitinfo(get_Shl_right(irn));
337 ir_tarval* const rz = r->z;
339 z = tarval_shl(l->z, rz);
340 o = tarval_shl(l->o, rz);
348 bitinfo* const l = get_bitinfo(get_Shr_left(irn));
349 bitinfo* const r = get_bitinfo(get_Shr_right(irn));
350 ir_tarval* const rz = r->z;
352 z = tarval_shr(l->z, rz);
353 o = tarval_shr(l->o, rz);
361 bitinfo* const l = get_bitinfo(get_Shrs_left(irn));
362 bitinfo* const r = get_bitinfo(get_Shrs_right(irn));
363 ir_tarval* const rz = r->z;
365 z = tarval_shrs(l->z, rz);
366 o = tarval_shrs(l->o, rz);
374 bitinfo* const l = get_bitinfo(get_Rotl_left(irn));
375 bitinfo* const r = get_bitinfo(get_Rotl_right(irn));
376 ir_tarval* const rz = r->z;
378 z = tarval_rotl(l->z, rz);
379 o = tarval_rotl(l->o, rz);
387 bitinfo* const l = get_bitinfo(get_Add_left(irn));
388 bitinfo* const r = get_bitinfo(get_Add_right(irn));
389 ir_tarval* const lz = l->z;
390 ir_tarval* const lo = l->o;
391 ir_tarval* const rz = r->z;
392 ir_tarval* const ro = r->o;
393 if (lz == lo && rz == ro) {
394 z = o = tarval_add(lz, rz);
396 // TODO improve: can only do lower disjoint bits
397 /* Determine where any of the operands has zero bits, i.e. where no
398 * carry out is generated if there is not carry in */
399 ir_tarval* const no_c_in_no_c_out = tarval_and(lz, rz);
400 /* Generate a mask of the lower consecutive zeroes: x | -x. In this
401 * range the addition is disjoint and therefore Add behaves like Or.
403 ir_tarval* const low_zero_mask = tarval_or(no_c_in_no_c_out, tarval_neg(no_c_in_no_c_out));
404 ir_tarval* const low_one_mask = tarval_not(low_zero_mask);
405 z = tarval_or( tarval_or(lz, rz), low_zero_mask);
406 o = tarval_and(tarval_or(lo, ro), low_one_mask);
412 bitinfo* const l = get_bitinfo(get_Sub_left(irn));
413 bitinfo* const r = get_bitinfo(get_Sub_right(irn));
414 if (l != NULL && r != NULL) { // Sub might subtract pointers.
415 ir_tarval* const lz = l->z;
416 ir_tarval* const lo = l->o;
417 ir_tarval* const rz = r->z;
418 ir_tarval* const ro = r->o;
419 if (lz == lo && rz == ro) {
420 z = o = tarval_sub(lz, rz, NULL);
421 } else if (tarval_is_null(tarval_andnot(rz, lo))) {
422 /* Every possible one of the subtrahend is backed by a safe one of the
423 * minuend, i.e. there are no borrows. */
424 // TODO extend no-borrow like carry for Add above
425 z = tarval_andnot(lz, ro);
426 o = tarval_andnot(lo, rz);
437 bitinfo* const l = get_bitinfo(get_Mul_left(irn));
438 bitinfo* const r = get_bitinfo(get_Mul_right(irn));
439 ir_tarval* const lz = l->z;
440 ir_tarval* const lo = l->o;
441 ir_tarval* const rz = r->z;
442 ir_tarval* const ro = r->o;
443 if (lz == lo && rz == ro) {
444 z = o = tarval_mul(lz, rz);
447 // Determine safe lower zeroes: x | -x.
448 ir_tarval* const lzn = tarval_or(lz, tarval_neg(lz));
449 ir_tarval* const rzn = tarval_or(rz, tarval_neg(rz));
450 // Concatenate safe lower zeroes.
451 if (tarval_cmp(lzn, rzn) == ir_relation_less) {
452 z = tarval_mul(tarval_eor(lzn, tarval_shl(lzn, get_tarval_one(m))), rzn);
454 z = tarval_mul(tarval_eor(rzn, tarval_shl(rzn, get_tarval_one(m))), lzn);
456 o = get_tarval_null(m);
462 bitinfo* const b = get_bitinfo(get_Minus_op(irn));
464 z = o = tarval_neg(b->z);
472 bitinfo* const l = get_bitinfo(get_And_left(irn));
473 bitinfo* const r = get_bitinfo(get_And_right(irn));
474 z = tarval_and(l->z, r->z);
475 o = tarval_and(l->o, r->o);
480 bitinfo* const l = get_bitinfo(get_Or_left(irn));
481 bitinfo* const r = get_bitinfo(get_Or_right(irn));
482 z = tarval_or(l->z, r->z);
483 o = tarval_or(l->o, r->o);
488 bitinfo* const l = get_bitinfo(get_Eor_left(irn));
489 bitinfo* const r = get_bitinfo(get_Eor_right(irn));
490 ir_tarval* const lz = l->z;
491 ir_tarval* const lo = l->o;
492 ir_tarval* const rz = r->z;
493 ir_tarval* const ro = r->o;
494 z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
495 o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
500 bitinfo* const b = get_bitinfo(get_Not_op(irn));
501 z = tarval_not(b->o);
502 o = tarval_not(b->z);
507 bitinfo* const b = get_bitinfo(get_Conv_op(irn));
508 if (b == NULL) // Happens when converting from float values.
510 z = tarval_convert_to(b->z, m);
511 o = tarval_convert_to(b->o, m);
516 bitinfo* const bf = get_bitinfo(get_Mux_false(irn));
517 bitinfo* const bt = get_bitinfo(get_Mux_true(irn));
518 bitinfo* const c = get_bitinfo(get_Mux_sel(irn));
522 } else if (c->z == f) {
526 z = tarval_or( bf->z, bt->z);
527 o = tarval_and(bf->o, bt->o);
533 bitinfo* const l = get_bitinfo(get_Cmp_left(irn));
534 bitinfo* const r = get_bitinfo(get_Cmp_right(irn));
535 if (l == NULL || r == NULL) {
536 goto result_unknown; // Cmp compares something we cannot evaluate.
538 ir_tarval* const lz = l->z;
539 ir_tarval* const lo = l->o;
540 ir_tarval* const rz = r->z;
541 ir_tarval* const ro = r->o;
542 ir_relation const relation = get_Cmp_relation(irn);
544 case ir_relation_less_greater:
545 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
546 !tarval_is_null(tarval_andnot(lo, rz))) {
547 // At least one bit differs.
549 } else if (lz == lo && rz == ro && lz == rz) {
556 case ir_relation_equal:
557 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
558 !tarval_is_null(tarval_andnot(lo, rz))) {
559 // At least one bit differs.
561 } else if (lz == lo && rz == ro && lz == rz) {
568 case ir_relation_less_equal:
569 case ir_relation_less:
570 /* TODO handle negative values */
571 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
572 tarval_is_negative(rz) || tarval_is_negative(ro))
575 if (tarval_cmp(lz, ro) & relation) {
576 /* Left upper bound is smaller(/equal) than right lower bound. */
578 } else if (!(tarval_cmp(lo, rz) & relation)) {
579 /* Left lower bound is not smaller(/equal) than right upper bound. */
586 case ir_relation_greater_equal:
587 case ir_relation_greater:
588 /* TODO handle negative values */
589 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
590 tarval_is_negative(rz) || tarval_is_negative(ro))
593 if (!(tarval_cmp(lz, ro) & relation)) {
594 /* Left upper bound is not greater(/equal) than right lower bound. */
596 } else if (tarval_cmp(lo, rz) & relation) {
597 /* Left lower bound is greater(/equal) than right upper bound. */
613 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
615 z = get_tarval_all_one(m);
616 o = get_tarval_null(m);
625 return set_bitinfo(irn, z, o);
628 static void first_round(ir_node* const irn, void* const env)
630 pdeq* const q = (pdeq*)env;
633 if (is_Phi(irn) || is_Block(irn)) {
634 /* Only Phis (and their users) need another round, if we did not have
635 * information about all their inputs in the first round, i.e. in loops. */
636 /* TODO inserts all Phis, should only insert Phis, which did no have all
637 * predecessors available */
642 static ir_node *make_bad_block(ir_graph *irg)
644 ir_node *bad = new_r_Bad(irg, mode_BB);
645 bitinfo *bb = get_bitinfo(bad);
647 ir_tarval* const f = get_tarval_b_false();
648 ir_tarval* const t = get_tarval_b_true();
649 set_bitinfo(bad, f, t); /* Undefined. */
654 static void apply_result(ir_node* const irn, void* ctx)
656 environment_t* env = (environment_t*)ctx;
664 block_b = get_bitinfo(irn);
665 /* Trivially unreachable blocks have no info. */
666 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
667 ir_node *bad = make_bad_block(get_irn_irg(irn));
674 block = get_nodes_block(irn);
675 block_b = get_bitinfo(block);
676 /* Trivially unreachable blocks have no info. */
677 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
678 /* Unreachable blocks might be replaced before the nodes in them. */
679 ir_mode *mode = get_irn_mode(irn);
680 ir_graph *irg = get_irn_irg(irn);
681 ir_node *bad = new_r_Bad(irg, mode);
687 b = get_bitinfo(irn);
689 if (is_Const(irn)) return; // It cannot get any better than a Const.
693 // Only display information if we could find out anything about the value.
694 DEBUG_ONLY(if (!tarval_is_all_one(z) || !tarval_is_null(o)))
695 DB((dbg, LEVEL_2, "%+F: 0:%T 1:%T%s\n", irn, z, o, z == o ? " --- constant" : ""));
697 // Replace node with constant value by Const.
699 ir_mode* const m = get_irn_mode(irn);
701 if (mode_is_intb(m)) {
702 ir_graph *irg = get_irn_irg(irn);
703 n = new_r_Const(irg, z);
704 } else if (m == mode_X) {
705 ir_graph* const irg = get_Block_irg(block);
706 if (z == get_tarval_b_true()) {
707 // Might produce an endless loop, so keep the block.
708 add_End_keepalive(get_irg_end(irg), block);
709 n = new_r_Jmp(block);
711 n = new_r_Bad(irg, mode_X);
712 /* Transferring analysis information to the bad node makes it a
713 * candidate for replacement. */
725 switch (get_irn_opcode(irn)) {
727 ir_node* const l = get_And_left(irn);
728 ir_node* const r = get_And_right(irn);
729 bitinfo const* const bl = get_bitinfo(l);
730 bitinfo const* const br = get_bitinfo(r);
731 if (bl->z == bl->o) {
732 if (tarval_is_null(tarval_andnot(br->z, bl->z))) {
733 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
737 } else if (br->z == br->o) {
738 if (tarval_is_null(tarval_andnot(bl->z, br->z))) {
739 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
748 ir_node* const l = get_Or_left(irn);
749 ir_node* const r = get_Or_right(irn);
750 bitinfo const* const bl = get_bitinfo(l);
751 bitinfo const* const br = get_bitinfo(r);
752 if (bl->z == bl->o) {
753 if (tarval_is_null(tarval_andnot(bl->o, br->o))) {
754 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
758 } else if (br->z == br->o) {
759 if (tarval_is_null(tarval_andnot(br->o, bl->o))) {
760 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
770 static void queue_users(pdeq* const q, ir_node* const n)
772 if (get_irn_mode(n) == mode_X) {
773 /* When the state of a control flow node changes, not only queue its
774 * successor blocks, but also the Phis in these blocks, because the Phis
775 * must reconsider this input path. */
777 foreach_out_edge(n, e) {
778 ir_node* const src = get_edge_src_irn(e);
780 /* should always be a block */
783 for (phi = get_Block_phis(src); phi; phi = get_Phi_next(phi))
789 foreach_out_edge(n, e) {
790 ir_node* const src = get_edge_src_irn(e);
791 if (get_irn_mode(src) == mode_T) {
800 static void clear_links(ir_node *irn, void *env)
803 set_irn_link(irn, NULL);
805 set_Block_phis(irn, NULL);
808 static void build_phi_lists(ir_node *irn, void *env)
812 add_Block_phi(get_nodes_block(irn), irn);
815 void fixpoint_vrp(ir_graph* const irg)
819 FIRM_DBG_REGISTER(dbg, "firm.opt.fp-vrp");
820 DB((dbg, LEVEL_1, "===> Performing constant propagation on %+F\n", irg));
824 /* HACK: to avoid finding dead code */
825 edges_deactivate(irg);
831 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
834 pdeq* const q = new_pdeq();
836 /* We need this extra step because the dom tree does not contain
837 * unreachable blocks in Firm. Moreover build phi list. */
838 irg_walk_anchors(irg, clear_links, build_phi_lists, NULL);
841 ir_tarval* const f = get_tarval_b_false();
842 ir_tarval* const t = get_tarval_b_true();
843 set_bitinfo(get_irg_end_block(irg), t, f); /* Reachable. */
846 /* TODO Improve iteration order. Best is reverse postorder in data flow
847 * direction and respecting loop nesting for fastest convergence. */
848 irg_walk_blkwise_dom_top_down(irg, NULL, first_round, q);
850 while (!pdeq_empty(q)) {
851 ir_node* const n = (ir_node*)pdeq_getl(q);
859 DB((dbg, LEVEL_2, "---> Applying analysis results\n"));
861 irg_walk_graph(irg, NULL, apply_result, &env);
864 /* control flow might changed */
865 set_irg_extblk_inconsistent(irg);
866 set_irg_doms_inconsistent(irg);
867 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
870 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
872 obstack_free(&obst, NULL);
875 ir_graph_pass_t *fixpoint_vrp_irg_pass(const char *name)
877 return def_graph_pass(name ? name : "fixpoint_vrp", fixpoint_vrp);