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
32 #include "adt/xmalloc.h"
38 #include "irgraph_t.h"
41 #include "iroptimize.h"
48 * - Implement cleared/set bit calculation for Add, Sub, Minus, Mul, Div, Mod, Shl, Shr, Shrs, Rotl
49 * - Implement min/max calculation for And, Eor, Or, Not, Conv, Shl, Shr, Shrs, Rotl, Mux
50 * - Implement min/max calculation for Add, Sub, Minus, Mul, Div, Mod, Conv, Shl, Shr, Shrs, Rotl, Mux
53 /* Tables of the cleared/set bit lattice
55 * Encoding of the lattice
58 * 01 - impossible state, is zero /and/ one
59 * 10 T top, may be either zero or one
73 * Half adder, half subtractor, and, xor, or, Mux
85 * Full adder, full subtractor
116 * Assume: Xmin <= Xmax and no overflow
117 * A + B = (Amin + Bmin, Amax + Bmax)
118 * -A = (-Amax, -Amin)
119 * A - B = A + -B = (Amin (-B)min, Amax + (-B)max) = (Amin - Bmax, Amax - Bmin)
122 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
124 static struct obstack obst;
126 typedef struct bitinfo
128 ir_tarval* z; // safe zeroes, 0 = bit is zero, 1 = bit maybe is 1
129 ir_tarval* o; // safe ones, 0 = bit maybe is zero, 1 = bit is 1
132 typedef struct environment_t {
133 unsigned modified:1; /**< Set, if the graph was modified. */
136 static bool is_undefined(bitinfo const* const b)
138 return tarval_is_null(b->z) && tarval_is_all_one(b->o);
141 static inline bitinfo* get_bitinfo(ir_node const* const irn)
143 return (bitinfo*)get_irn_link(irn);
146 static int set_bitinfo(ir_node* const irn, ir_tarval* const z, ir_tarval* const o)
148 bitinfo* b = get_bitinfo(irn);
150 b = OALLOCZ(&obst, bitinfo);
151 set_irn_link(irn, b);
152 } else if (z == b->z && o == b->o) {
155 /* Assert monotonicity. */
156 assert(tarval_is_null(tarval_andnot(b->z, z)));
157 assert(tarval_is_null(tarval_andnot(o, b->o)));
161 DB((dbg, LEVEL_3, "%+F: 0:%T 1:%T\n", irn, z, o));
165 static int mode_is_intb(ir_mode const* const m)
167 return mode_is_int(m) || m == mode_b;
170 static int transfer(ir_node* const irn)
172 ir_tarval* const f = get_tarval_b_false();
173 ir_tarval* const t = get_tarval_b_true();
174 ir_mode* const m = get_irn_mode(irn);
178 if (is_Bad(irn)) return 0;
181 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
183 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
185 /* Unreachble blocks might have no bitinfo. */
186 if (b == NULL || b->z == f) {
190 } else switch (get_irn_opcode(irn)) {
192 ir_node* const pred = get_Proj_pred(irn);
193 if (is_Start(pred)) {
194 goto result_unknown_X;
195 } else if (is_Cond(pred)) {
196 ir_node* const selector = get_Cond_selector(pred);
197 bitinfo* const b = get_bitinfo(selector);
201 if ((b->z == t) == get_Proj_proj(irn)) {
207 goto result_unknown_X;
209 } else if (is_Switch(pred)) {
210 ir_node* const selector = get_Switch_selector(pred);
211 bitinfo* const b = get_bitinfo(selector);
215 goto cannot_analyse_X;
217 goto cannot_analyse_X;
223 goto result_unknown_X;
227 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
233 } else if (is_Block(irn)) {
235 int const arity = get_Block_n_cfgpreds(irn);
238 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
239 for (i = 0; i != arity; ++i) {
240 bitinfo* const b = get_bitinfo(get_Block_cfgpred(irn, i));
241 if (b != NULL && b->z == t) {
248 ir_graph *const irg = get_Block_irg(irn);
250 irn == get_irg_start_block(irg) ||
251 irn == get_irg_end_block(irg);
261 } else if (mode_is_intb(m)) {
262 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
264 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
266 if (b == NULL || b->z == f) {
268 z = get_tarval_null(m);
269 o = get_tarval_all_one(m);
270 } else if (is_Phi(irn)) {
271 ir_node* const block = get_nodes_block(irn);
272 int const arity = get_Phi_n_preds(irn);
275 z = get_tarval_null(m);
276 o = get_tarval_all_one(m);
277 for (i = 0; i != arity; ++i) {
278 bitinfo* const b_cfg = get_bitinfo(get_Block_cfgpred(block, i));
279 if (b_cfg != NULL && b_cfg->z != f) {
280 bitinfo* const b = get_bitinfo(get_Phi_pred(irn, i));
281 /* Only use input if it's not undefined. */
282 if (!is_undefined(b)) {
283 z = tarval_or( z, b->z);
284 o = tarval_and(o, b->o);
289 int const arity = get_irn_arity(irn);
292 /* Undefined if any input is undefined. */
293 for (i = 0; i != arity; ++i) {
294 ir_node* const pred = get_irn_n(irn, i);
295 bitinfo* const pred_b = get_bitinfo(pred);
296 if (pred_b != NULL && is_undefined(pred_b))
300 switch (get_irn_opcode(irn)) {
302 z = o = get_Const_tarval(irn);
307 ir_node* const v = get_Confirm_value(irn);
308 bitinfo* const b = get_bitinfo(v);
309 /* TODO Use bound and relation. */
312 if ((get_Confirm_relation(irn) & ~ir_relation_unordered) == ir_relation_equal) {
313 bitinfo* const bound_b = get_bitinfo(get_Confirm_bound(irn));
314 z = tarval_and(z, bound_b->z);
315 o = tarval_or( o, bound_b->o);
321 bitinfo* const l = get_bitinfo(get_Shl_left(irn));
322 bitinfo* const r = get_bitinfo(get_Shl_right(irn));
323 ir_tarval* const rz = r->z;
325 z = tarval_shl(l->z, rz);
326 o = tarval_shl(l->o, rz);
334 bitinfo* const l = get_bitinfo(get_Shr_left(irn));
335 bitinfo* const r = get_bitinfo(get_Shr_right(irn));
336 ir_tarval* const rz = r->z;
338 z = tarval_shr(l->z, rz);
339 o = tarval_shr(l->o, rz);
347 bitinfo* const l = get_bitinfo(get_Shrs_left(irn));
348 bitinfo* const r = get_bitinfo(get_Shrs_right(irn));
349 ir_tarval* const rz = r->z;
351 z = tarval_shrs(l->z, rz);
352 o = tarval_shrs(l->o, rz);
360 bitinfo* const l = get_bitinfo(get_Rotl_left(irn));
361 bitinfo* const r = get_bitinfo(get_Rotl_right(irn));
362 ir_tarval* const rz = r->z;
364 z = tarval_rotl(l->z, rz);
365 o = tarval_rotl(l->o, rz);
373 bitinfo* const l = get_bitinfo(get_Add_left(irn));
374 bitinfo* const r = get_bitinfo(get_Add_right(irn));
375 ir_tarval* const lz = l->z;
376 ir_tarval* const lo = l->o;
377 ir_tarval* const rz = r->z;
378 ir_tarval* const ro = r->o;
379 if (lz == lo && rz == ro) {
380 z = o = tarval_add(lz, rz);
382 // TODO improve: can only do lower disjoint bits
383 /* Determine where any of the operands has zero bits, i.e. where no
384 * carry out is generated if there is not carry in */
385 ir_tarval* const no_c_in_no_c_out = tarval_and(lz, rz);
386 /* Generate a mask of the lower consecutive zeroes: x | -x. In this
387 * range the addition is disjoint and therefore Add behaves like Or.
389 ir_tarval* const low_zero_mask = tarval_or(no_c_in_no_c_out, tarval_neg(no_c_in_no_c_out));
390 ir_tarval* const low_one_mask = tarval_not(low_zero_mask);
391 z = tarval_or( tarval_or(lz, rz), low_zero_mask);
392 o = tarval_and(tarval_or(lo, ro), low_one_mask);
398 bitinfo* const l = get_bitinfo(get_Sub_left(irn));
399 bitinfo* const r = get_bitinfo(get_Sub_right(irn));
400 if (l != NULL && r != NULL) { // Sub might subtract pointers.
401 ir_tarval* const lz = l->z;
402 ir_tarval* const lo = l->o;
403 ir_tarval* const rz = r->z;
404 ir_tarval* const ro = r->o;
405 if (lz == lo && rz == ro) {
406 z = o = tarval_sub(lz, rz, NULL);
407 } else if (tarval_is_null(tarval_andnot(rz, lo))) {
408 /* Every possible one of the subtrahend is backed by a safe one of the
409 * minuend, i.e. there are no borrows. */
410 // TODO extend no-borrow like carry for Add above
411 z = tarval_andnot(lz, ro);
412 o = tarval_andnot(lo, rz);
423 bitinfo* const l = get_bitinfo(get_Mul_left(irn));
424 bitinfo* const r = get_bitinfo(get_Mul_right(irn));
425 ir_tarval* const lz = l->z;
426 ir_tarval* const lo = l->o;
427 ir_tarval* const rz = r->z;
428 ir_tarval* const ro = r->o;
429 if (lz == lo && rz == ro) {
430 z = o = tarval_mul(lz, rz);
433 // Determine safe lower zeroes: x | -x.
434 ir_tarval* const lzn = tarval_or(lz, tarval_neg(lz));
435 ir_tarval* const rzn = tarval_or(rz, tarval_neg(rz));
436 // Concatenate safe lower zeroes.
437 if (tarval_cmp(lzn, rzn) == ir_relation_less) {
438 z = tarval_mul(tarval_eor(lzn, tarval_shl(lzn, get_tarval_one(m))), rzn);
440 z = tarval_mul(tarval_eor(rzn, tarval_shl(rzn, get_tarval_one(m))), lzn);
442 o = get_tarval_null(m);
448 bitinfo* const b = get_bitinfo(get_Minus_op(irn));
450 z = o = tarval_neg(b->z);
458 bitinfo* const l = get_bitinfo(get_And_left(irn));
459 bitinfo* const r = get_bitinfo(get_And_right(irn));
460 z = tarval_and(l->z, r->z);
461 o = tarval_and(l->o, r->o);
466 bitinfo* const l = get_bitinfo(get_Or_left(irn));
467 bitinfo* const r = get_bitinfo(get_Or_right(irn));
468 z = tarval_or(l->z, r->z);
469 o = tarval_or(l->o, r->o);
474 bitinfo* const l = get_bitinfo(get_Eor_left(irn));
475 bitinfo* const r = get_bitinfo(get_Eor_right(irn));
476 ir_tarval* const lz = l->z;
477 ir_tarval* const lo = l->o;
478 ir_tarval* const rz = r->z;
479 ir_tarval* const ro = r->o;
480 z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
481 o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
486 bitinfo* const b = get_bitinfo(get_Not_op(irn));
487 z = tarval_not(b->o);
488 o = tarval_not(b->z);
493 bitinfo* const b = get_bitinfo(get_Conv_op(irn));
494 if (b == NULL) // Happens when converting from float values.
496 z = tarval_convert_to(b->z, m);
497 o = tarval_convert_to(b->o, m);
502 bitinfo* const bf = get_bitinfo(get_Mux_false(irn));
503 bitinfo* const bt = get_bitinfo(get_Mux_true(irn));
504 bitinfo* const c = get_bitinfo(get_Mux_sel(irn));
508 } else if (c->z == f) {
512 z = tarval_or( bf->z, bt->z);
513 o = tarval_and(bf->o, bt->o);
519 bitinfo* const l = get_bitinfo(get_Cmp_left(irn));
520 bitinfo* const r = get_bitinfo(get_Cmp_right(irn));
521 if (l == NULL || r == NULL) {
522 goto result_unknown; // Cmp compares something we cannot evaluate.
524 ir_tarval* const lz = l->z;
525 ir_tarval* const lo = l->o;
526 ir_tarval* const rz = r->z;
527 ir_tarval* const ro = r->o;
528 ir_relation const relation = get_Cmp_relation(irn);
530 case ir_relation_less_greater:
531 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
532 !tarval_is_null(tarval_andnot(lo, rz))) {
533 // At least one bit differs.
535 } else if (lz == lo && rz == ro && lz == rz) {
542 case ir_relation_equal:
543 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
544 !tarval_is_null(tarval_andnot(lo, rz))) {
545 // At least one bit differs.
547 } else if (lz == lo && rz == ro && lz == rz) {
554 case ir_relation_less_equal:
555 case ir_relation_less:
556 /* TODO handle negative values */
557 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
558 tarval_is_negative(rz) || tarval_is_negative(ro))
561 if (tarval_cmp(lz, ro) & relation) {
562 /* Left upper bound is smaller(/equal) than right lower bound. */
564 } else if (!(tarval_cmp(lo, rz) & relation)) {
565 /* Left lower bound is not smaller(/equal) than right upper bound. */
572 case ir_relation_greater_equal:
573 case ir_relation_greater:
574 /* TODO handle negative values */
575 if (tarval_is_negative(lz) || tarval_is_negative(lo) ||
576 tarval_is_negative(rz) || tarval_is_negative(ro))
579 if (!(tarval_cmp(lz, ro) & relation)) {
580 /* Left upper bound is not greater(/equal) than right lower bound. */
582 } else if (tarval_cmp(lo, rz) & relation) {
583 /* Left lower bound is greater(/equal) than right upper bound. */
599 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
601 z = get_tarval_all_one(m);
602 o = get_tarval_null(m);
611 return set_bitinfo(irn, z, o);
614 static void first_round(ir_node* const irn, void* const env)
616 pdeq* const q = (pdeq*)env;
619 if (is_Phi(irn) || is_Block(irn)) {
620 /* Only Phis (and their users) need another round, if we did not have
621 * information about all their inputs in the first round, i.e. in loops. */
622 /* TODO inserts all Phis, should only insert Phis, which did no have all
623 * predecessors available */
628 static ir_node *make_bad_block(ir_graph *irg)
630 ir_node *bad = new_r_Bad(irg, mode_BB);
631 bitinfo *bb = get_bitinfo(bad);
633 ir_tarval* const f = get_tarval_b_false();
634 ir_tarval* const t = get_tarval_b_true();
635 set_bitinfo(bad, f, t); /* Undefined. */
640 static void apply_result(ir_node* const irn, void* ctx)
642 environment_t* env = (environment_t*)ctx;
650 block_b = get_bitinfo(irn);
651 /* Trivially unreachable blocks have no info. */
652 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
653 ir_node *bad = make_bad_block(get_irn_irg(irn));
660 block = get_nodes_block(irn);
661 block_b = get_bitinfo(block);
662 /* Trivially unreachable blocks have no info. */
663 if (block_b == NULL || block_b->z == get_tarval_b_false()) {
664 /* Unreachable blocks might be replaced before the nodes in them. */
665 ir_mode *mode = get_irn_mode(irn);
666 ir_graph *irg = get_irn_irg(irn);
667 ir_node *bad = new_r_Bad(irg, mode);
673 b = get_bitinfo(irn);
675 if (is_Const(irn)) return; // It cannot get any better than a Const.
679 // Only display information if we could find out anything about the value.
680 DEBUG_ONLY(if (!tarval_is_all_one(z) || !tarval_is_null(o)))
681 DB((dbg, LEVEL_2, "%+F: 0:%T 1:%T%s\n", irn, z, o, z == o ? " --- constant" : ""));
683 // Replace node with constant value by Const.
685 ir_mode* const m = get_irn_mode(irn);
687 if (mode_is_intb(m)) {
688 ir_graph *irg = get_irn_irg(irn);
689 n = new_r_Const(irg, z);
690 } else if (m == mode_X) {
691 ir_graph* const irg = get_Block_irg(block);
692 if (z == get_tarval_b_true()) {
693 // Might produce an endless loop, so keep the block.
694 add_End_keepalive(get_irg_end(irg), block);
695 n = new_r_Jmp(block);
697 n = new_r_Bad(irg, mode_X);
698 /* Transferring analysis information to the bad node makes it a
699 * candidate for replacement. */
711 switch (get_irn_opcode(irn)) {
713 ir_node* const l = get_And_left(irn);
714 ir_node* const r = get_And_right(irn);
715 bitinfo const* const bl = get_bitinfo(l);
716 bitinfo const* const br = get_bitinfo(r);
717 if (tarval_is_null(tarval_andnot(br->z, bl->o))) {
718 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
721 } else if (tarval_is_null(tarval_andnot(bl->z, br->o))) {
722 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
730 ir_node* const l = get_Eor_left(irn);
731 ir_node* const r = get_Eor_right(irn);
732 bitinfo const* const bl = get_bitinfo(l);
733 bitinfo const* const br = get_bitinfo(r);
734 /* if each bit is guaranteed to be zero on either the left or right
735 * then an Add will have the same effect as the Eor. Change it for
737 if (tarval_is_null(tarval_and(bl->z, br->z))) {
738 dbg_info *dbgi = get_irn_dbg_info(irn);
739 ir_node *block = get_nodes_block(irn);
740 ir_mode *mode = get_irn_mode(irn);
741 ir_node *new_node = new_rd_Add(dbgi, block, l, r, mode);
742 bitinfo const *bi = get_bitinfo(irn);
743 DB((dbg, LEVEL_2, "%+F(%+F, %+F) normalised to Add\n", irn, l, r));
744 set_bitinfo(new_node, bi->z, bi->o);
745 exchange(irn, new_node);
752 ir_node* const l = get_Or_left(irn);
753 ir_node* const r = get_Or_right(irn);
754 bitinfo const* const bl = get_bitinfo(l);
755 bitinfo const* const br = get_bitinfo(r);
756 if (tarval_is_null(tarval_andnot(bl->z, br->o))) {
757 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
760 } else if (tarval_is_null(tarval_andnot(br->z, bl->o))) {
761 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
766 /* if each bit is guaranteed to be zero on either the left or right
767 * then an Add will have the same effect as the Or. Change it for
769 if (tarval_is_null(tarval_and(bl->z, br->z))) {
770 dbg_info *dbgi = get_irn_dbg_info(irn);
771 ir_node *block = get_nodes_block(irn);
772 ir_mode *mode = get_irn_mode(irn);
773 ir_node *new_node = new_rd_Add(dbgi, block, l, r, mode);
774 bitinfo const *bi = get_bitinfo(irn);
775 DB((dbg, LEVEL_2, "%+F(%+F, %+F) normalised to Add\n", irn, l, r));
776 set_bitinfo(new_node, bi->z, bi->o);
777 exchange(irn, new_node);
786 static void queue_users(pdeq* const q, ir_node* const n)
788 if (get_irn_mode(n) == mode_X) {
789 /* When the state of a control flow node changes, not only queue its
790 * successor blocks, but also the Phis in these blocks, because the Phis
791 * must reconsider this input path. */
792 foreach_out_edge(n, e) {
793 ir_node* const src = get_edge_src_irn(e);
795 /* should always be a block */
798 for (phi = get_Block_phis(src); phi; phi = get_Phi_next(phi))
803 foreach_out_edge(n, e) {
804 ir_node* const src = get_edge_src_irn(e);
805 if (get_irn_mode(src) == mode_T) {
814 static void clear_links(ir_node *irn, void *env)
817 set_irn_link(irn, NULL);
819 set_Block_phis(irn, NULL);
822 static void build_phi_lists(ir_node *irn, void *env)
826 add_Block_phi(get_nodes_block(irn), irn);
829 void fixpoint_vrp(ir_graph* const irg)
833 FIRM_DBG_REGISTER(dbg, "firm.opt.fp-vrp");
834 DB((dbg, LEVEL_1, "===> Performing constant propagation on %+F\n", irg));
836 assure_irg_properties(irg,
837 IR_GRAPH_PROPERTY_NO_BADS
838 | IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
839 | IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE
840 | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES);
844 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
847 pdeq* const q = new_pdeq();
849 /* We need this extra step because the dom tree does not contain
850 * unreachable blocks in Firm. Moreover build phi list. */
851 irg_walk_anchors(irg, clear_links, build_phi_lists, NULL);
854 ir_tarval* const f = get_tarval_b_false();
855 ir_tarval* const t = get_tarval_b_true();
856 set_bitinfo(get_irg_end_block(irg), t, f); /* Reachable. */
859 /* TODO Improve iteration order. Best is reverse postorder in data flow
860 * direction and respecting loop nesting for fastest convergence. */
861 irg_walk_blkwise_dom_top_down(irg, NULL, first_round, q);
863 while (!pdeq_empty(q)) {
864 ir_node* const n = (ir_node*)pdeq_getl(q);
872 DB((dbg, LEVEL_2, "---> Applying analysis results\n"));
874 irg_walk_graph(irg, NULL, apply_result, &env);
876 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
878 obstack_free(&obst, NULL);
879 confirm_irg_properties(irg,
880 env.modified ? IR_GRAPH_PROPERTIES_NONE : IR_GRAPH_PROPERTIES_ALL);
883 ir_graph_pass_t *fixpoint_vrp_irg_pass(const char *name)
885 return def_graph_pass(name ? name : "fixpoint_vrp", fixpoint_vrp);