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
31 #include "adt/xmalloc.h"
40 #include "iroptimize.h"
47 * - Implement cleared/set bit calculation for Add, Sub, Minus, Mul, Div, Mod, Shl, Shr, Shrs, Rotl
48 * - Implement min/max calculation for And, Eor, Or, Not, Conv, Shl, Shr, Shrs, Rotl, Mux
49 * - Implement min/max calculation for Add, Sub, Minus, Mul, Div, Mod, Conv, Shl, Shr, Shrs, Rotl, Mux
52 /* Tables of the cleared/set bit lattice
54 * Encoding of the lattice
57 * 01 - impossible state, is zero /and/ one
58 * 10 T top, may be either zero or one
72 * Half adder, half subtractor, and, xor, or, Mux
84 * Full adder, full subtractor
115 * Assume: Xmin <= Xmax and no overflow
116 * A + B = (Amin + Bmin, Amax + Bmax)
117 * -A = (-Amax, -Amin)
118 * A - B = A + -B = (Amin (-B)min, Amax + (-B)max) = (Amin - Bmax, Amax - Bmin)
121 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
123 static struct obstack obst;
125 typedef struct bitinfo
127 tarval* z; // safe zeroes, 0 = bit is zero, 1 = bit maybe is 1
128 tarval* o; // safe ones, 0 = bit maybe is zero, 1 = bit is 1
131 typedef struct environment_t {
132 unsigned modified:1; /**< Set, if the graph was modified. */
135 static inline bitinfo* get_bitinfo(ir_node const* const irn)
137 return get_irn_link(irn);
140 static int set_bitinfo(ir_node* const irn, tarval* const z, tarval* const o)
142 bitinfo* b = get_bitinfo(irn);
144 b = OALLOCZ(&obst, bitinfo);
145 set_irn_link(irn, b);
146 } else if (z == b->z && o == b->o) {
151 DB((dbg, LEVEL_3, "%+F: 0:%T 1:%T\n", irn, z, o));
155 static int mode_is_intb(ir_mode const* const m)
157 return mode_is_int(m) || m == mode_b;
160 static int transfer(ir_node* const irn)
162 ir_mode* const m = get_irn_mode(irn);
167 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
168 switch (get_irn_opcode(irn)) {
170 ir_node* const pred = get_Proj_pred(irn);
171 if (is_Start(pred)) {
172 z = get_tarval_b_true();
173 o = get_tarval_b_false();
174 } else if (is_Cond(pred)) {
175 ir_node* const selector = get_Cond_selector(pred);
176 bitinfo* const b = get_bitinfo(selector);
177 tarval* const bz = b->z;
178 tarval* const bo = b->o;
179 if (get_irn_mode(selector) == mode_b) {
181 if ((bz == get_tarval_b_true()) == get_Proj_proj(irn)) {
182 z = o = get_tarval_b_true();
184 z = o = get_tarval_b_false();
187 goto result_unknown_X;
190 long const val = get_Proj_proj(irn);
191 if (val != get_Cond_default_proj(pred)) {
192 tarval* const tv = new_tarval_from_long(val, get_irn_mode(selector));
193 if (!tarval_is_null(tarval_andnot(tv, bz)) ||
194 !tarval_is_null(tarval_andnot(bo, tv))) {
195 // At least one bit differs.
196 z = o = get_tarval_b_false();
197 #if 0 // TODO must handle default Proj
198 } else if (bz == bo && bz == tv) {
199 z = o = get_tarval_b_true();
202 goto result_unknown_X;
205 goto cannot_analyse_X;
209 goto cannot_analyse_X;
215 bitinfo* const b = get_bitinfo(get_nodes_block(irn));
223 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
225 z = get_tarval_b_true();
226 o = get_tarval_b_false();
229 } else if (is_Block(irn)) {
231 int const arity = get_Block_n_cfgpreds(irn);
234 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
235 for (i = 0; i != arity; ++i) {
236 bitinfo* const b = get_bitinfo(get_Block_cfgpred(irn, i));
237 if (b != NULL && b->z == get_tarval_b_true()) {
243 o = get_tarval_b_false();
244 z = reachable || irn == get_irg_start_block(get_irn_irg(irn)) ? get_tarval_b_true() : o;
245 } else if (mode_is_intb(m)) {
246 DB((dbg, LEVEL_3, "transfer %+F\n", irn));
247 switch (get_irn_opcode(irn)) {
249 z = o = get_Const_tarval(irn);
254 bitinfo* const l = get_bitinfo(get_Shl_left(irn));
255 bitinfo* const r = get_bitinfo(get_Shl_right(irn));
256 tarval* const rz = r->z;
258 z = tarval_shl(l->z, rz);
259 o = tarval_shl(l->o, rz);
267 bitinfo* const l = get_bitinfo(get_Shr_left(irn));
268 bitinfo* const r = get_bitinfo(get_Shr_right(irn));
269 tarval* const rz = r->z;
271 z = tarval_shr(l->z, rz);
272 o = tarval_shr(l->o, rz);
280 bitinfo* const l = get_bitinfo(get_Shrs_left(irn));
281 bitinfo* const r = get_bitinfo(get_Shrs_right(irn));
282 tarval* const rz = r->z;
284 z = tarval_shrs(l->z, rz);
285 o = tarval_shrs(l->o, rz);
293 bitinfo* const l = get_bitinfo(get_Rotl_left(irn));
294 bitinfo* const r = get_bitinfo(get_Rotl_right(irn));
295 tarval* const rz = r->z;
297 z = tarval_rotl(l->z, rz);
298 o = tarval_rotl(l->o, rz);
306 bitinfo* const l = get_bitinfo(get_Add_left(irn));
307 bitinfo* const r = get_bitinfo(get_Add_right(irn));
308 tarval* const lz = l->z;
309 tarval* const lo = l->o;
310 tarval* const rz = r->z;
311 tarval* const ro = r->o;
312 if (lz == lo && rz == ro) {
313 z = o = tarval_add(lz, rz);
315 // TODO improve: can only do lower disjoint bits
316 /* Determine where any of the operands has zero bits, i.e. where no
317 * carry out is generated if there is not carry in */
318 tarval* const no_c_in_no_c_out = tarval_and(lz, rz);
319 /* Generate a mask of the lower consecutive zeroes: x | -x. In this
320 * range the addition is disjoint and therefore Add behaves like Or.
322 tarval* const low_zero_mask = tarval_or(no_c_in_no_c_out, tarval_neg(no_c_in_no_c_out));
323 tarval* const low_one_mask = tarval_not(low_zero_mask);
324 z = tarval_or( tarval_or(lz, rz), low_zero_mask);
325 o = tarval_and(tarval_or(lo, ro), low_one_mask);
331 bitinfo* const l = get_bitinfo(get_Sub_left(irn));
332 bitinfo* const r = get_bitinfo(get_Sub_right(irn));
333 if (l != NULL && r != NULL) { // Sub might subtract pointers.
334 tarval* const lz = l->z;
335 tarval* const lo = l->o;
336 tarval* const rz = r->z;
337 tarval* const ro = r->o;
338 if (lz == lo && rz == ro) {
339 z = o = tarval_sub(lz, rz, NULL);
340 } else if (tarval_is_null(tarval_andnot(rz, lo))) {
341 /* Every possible one of the subtrahend is backed by a safe one of the
342 * minuend, i.e. there are no borrows. */
343 // TODO extend no-borrow like carry for Add above
344 z = tarval_andnot(lz, ro);
345 o = tarval_andnot(lo, rz);
356 bitinfo* const l = get_bitinfo(get_Mul_left(irn));
357 bitinfo* const r = get_bitinfo(get_Mul_right(irn));
358 tarval* const lz = l->z;
359 tarval* const lo = l->o;
360 tarval* const rz = r->z;
361 tarval* const ro = r->o;
362 if (lz == lo && rz == ro) {
363 z = o = tarval_mul(lz, rz);
366 // Determine safe lower zeroes: x | -x.
367 tarval* const lzn = tarval_or(lz, tarval_neg(lz));
368 tarval* const rzn = tarval_or(rz, tarval_neg(rz));
369 // Concatenate safe lower zeroes.
370 if (tarval_cmp(lzn, rzn) == pn_Cmp_Lt) {
371 z = tarval_mul(tarval_eor(lzn, tarval_shl(lzn, get_tarval_one(m))), rzn);
373 z = tarval_mul(tarval_eor(rzn, tarval_shl(rzn, get_tarval_one(m))), lzn);
375 o = get_tarval_null(m);
381 bitinfo* const b = get_bitinfo(get_Minus_op(irn));
383 z = o = tarval_neg(b->z);
391 bitinfo* const l = get_bitinfo(get_And_left(irn));
392 bitinfo* const r = get_bitinfo(get_And_right(irn));
393 z = tarval_and(l->z, r->z);
394 o = tarval_and(l->o, r->o);
399 bitinfo* const l = get_bitinfo(get_Or_left(irn));
400 bitinfo* const r = get_bitinfo(get_Or_right(irn));
401 z = tarval_or(l->z, r->z);
402 o = tarval_or(l->o, r->o);
407 bitinfo* const l = get_bitinfo(get_Eor_left(irn));
408 bitinfo* const r = get_bitinfo(get_Eor_right(irn));
409 tarval* const lz = l->z;
410 tarval* const lo = l->o;
411 tarval* const rz = r->z;
412 tarval* const ro = r->o;
413 z = tarval_or(tarval_andnot(lz, ro), tarval_andnot(rz, lo));
414 o = tarval_or(tarval_andnot(ro, lz), tarval_andnot(lo, rz));
419 bitinfo* const b = get_bitinfo(get_Not_op(irn));
420 z = tarval_not(b->o);
421 o = tarval_not(b->z);
426 bitinfo* const b = get_bitinfo(get_Conv_op(irn));
427 if (b == NULL) // Happens when converting from float values.
429 z = tarval_convert_to(b->z, m);
430 o = tarval_convert_to(b->o, m);
435 bitinfo* const f = get_bitinfo(get_Mux_false(irn));
436 bitinfo* const t = get_bitinfo(get_Mux_true(irn));
437 bitinfo* const c = get_bitinfo(get_Mux_sel(irn));
438 if (c->o == get_tarval_b_true()) {
441 } else if (c->z == get_tarval_b_false()) {
445 z = tarval_or( f->z, t->z);
446 o = tarval_and(f->o, t->o);
452 ir_node* const block = get_nodes_block(irn);
453 int const arity = get_Phi_n_preds(irn);
456 z = get_tarval_null(m);
457 o = get_tarval_all_one(m);
458 for (i = 0; i != arity; ++i) {
459 bitinfo* const b_cfg = get_bitinfo(get_Block_cfgpred(block, i));
460 if (b_cfg != NULL && b_cfg->z != get_tarval_b_false()) {
461 bitinfo* const b = get_bitinfo(get_Phi_pred(irn, i));
462 z = tarval_or( z, b->z);
463 o = tarval_and(o, b->o);
470 ir_node* const pred = get_Proj_pred(irn);
471 if (is_Cmp(pred)) { // TODO generalize
472 bitinfo* const l = get_bitinfo(get_Cmp_left(pred));
473 bitinfo* const r = get_bitinfo(get_Cmp_right(pred));
474 if (l == NULL || r == NULL)
475 goto result_unknown; // Cmp compares something we cannot evaluate.
476 switch (get_Proj_proj(irn)) {
478 tarval* const lz = l->z;
479 tarval* const lo = l->o;
480 tarval* const rz = r->z;
481 tarval* const ro = r->o;
482 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
483 !tarval_is_null(tarval_andnot(lo, rz))) {
484 // At least one bit differs.
485 z = o = get_tarval_b_true();
486 } else if (lz == lo && rz == ro && lz == rz) {
487 z = o = get_tarval_b_false();
495 tarval* const lz = l->z;
496 tarval* const lo = l->o;
497 tarval* const rz = r->z;
498 tarval* const ro = r->o;
499 if (!tarval_is_null(tarval_andnot(ro, lz)) ||
500 !tarval_is_null(tarval_andnot(lo, rz))) {
501 // At least one bit differs.
502 z = o = get_tarval_b_false();
503 } else if (lz == lo && rz == ro && lz == rz) {
504 z = o = get_tarval_b_true();
522 DB((dbg, LEVEL_4, "cannot analyse %+F\n", irn));
524 z = get_tarval_all_one(m);
525 o = get_tarval_null(m);
533 return set_bitinfo(irn, z, o);
536 static void first_round(ir_node* const irn, void* const env)
541 if (is_Phi(irn) || is_Block(irn)) {
542 /* Only Phis (and their users) need another round, if we did not have
543 * information about all their inputs in the first round, i.e. in loops. */
544 /* TODO inserts all Phis, should only insert Phis, which did no have all
545 * predecessors available */
550 static void apply_result(ir_node* const irn, void* ctx)
552 bitinfo* const b = get_bitinfo(irn);
555 environment_t* env = ctx;
558 if (is_Const(irn)) return; // It cannot get any better than a Const.
562 // Only display information if we could find out anything about the value.
563 DEBUG_ONLY(if (!tarval_is_all_one(z) || !tarval_is_null(o)))
564 DB((dbg, LEVEL_2, "%+F: 0:%T 1:%T%s\n", irn, z, o, z == o ? " --- constant" : ""));
566 // Replace node with constant value by Const.
568 ir_mode* const m = get_irn_mode(irn);
570 if (mode_is_intb(m)) {
572 } else if (m == mode_X) {
573 ir_node* const block = get_nodes_block(irn);
574 ir_graph* const irg = get_Block_irg(block);
575 if (z == get_tarval_b_true()) {
576 // Might produce an endless loop, so keep the block.
577 add_End_keepalive(get_irg_end(irg), block);
578 n = new_r_Jmp(block);
581 /* Transferring analysis information to the bad node makes it a
582 * candidate for replacement. */
594 switch (get_irn_opcode(irn)) {
596 ir_node* const l = get_And_left(irn);
597 ir_node* const r = get_And_right(irn);
598 bitinfo const* const bl = get_bitinfo(l);
599 bitinfo const* const br = get_bitinfo(r);
600 if (bl->z == bl->o) {
601 if (tarval_is_null(tarval_andnot(br->z, bl->z))) {
602 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
606 } else if (br->z == br->o) {
607 if (tarval_is_null(tarval_andnot(bl->z, br->z))) {
608 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
617 ir_node* const l = get_Or_left(irn);
618 ir_node* const r = get_Or_right(irn);
619 bitinfo const* const bl = get_bitinfo(l);
620 bitinfo const* const br = get_bitinfo(r);
621 if (bl->z == bl->o) {
622 if (tarval_is_null(tarval_andnot(bl->o, br->o))) {
623 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
627 } else if (br->z == br->o) {
628 if (tarval_is_null(tarval_andnot(br->o, bl->o))) {
629 DB((dbg, LEVEL_2, "%+F(%+F, %+F) is superfluous\n", irn, l, r));
639 static void queue_users(pdeq* const q, ir_node* const n)
641 if (get_irn_mode(n) == mode_X) {
642 /* When the state of a control flow node changes, not only queue its
643 * successor blocks, but also the Phis in these blocks, because the Phis
644 * must reconsider this input path. */
646 foreach_out_edge(n, e) {
647 ir_node* const src = get_edge_src_irn(e);
649 /* should always be a block */
652 for (phi = get_Block_phis(src); phi; phi = get_Phi_next(phi))
658 foreach_out_edge(n, e) {
659 ir_node* const src = get_edge_src_irn(e);
660 if (get_irn_mode(src) == mode_T) {
669 static void clear_links(ir_node *irn, void *env)
672 set_irn_link(irn, NULL);
674 set_Block_phis(irn, NULL);
677 static void build_phi_lists(ir_node *irn, void *env)
680 add_Block_phi(get_nodes_block(irn), irn);
683 void fixpoint_vrp(ir_graph* const irg)
687 FIRM_DBG_REGISTER(dbg, "firm.opt.fp-vrp");
688 DB((dbg, LEVEL_1, "===> Performing constant propagation on %+F\n", irg));
695 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
698 pdeq* const q = new_pdeq();
700 /* We need this extra step because the dom tree does not contain unreachable
701 blocks in Firm. Moreover build phi list. */
702 irg_walk_graph(irg, clear_links, build_phi_lists, NULL);
704 /* TODO Improve iteration order. Best is reverse postorder in data flow
705 * direction and respecting loop nesting for fastest convergence. */
706 irg_walk_blkwise_dom_top_down(irg, firm_clear_link, first_round, q);
708 while (!pdeq_empty(q)) {
709 ir_node* const n = pdeq_getl(q);
717 DB((dbg, LEVEL_2, "---> Applying analysis results\n"));
719 irg_walk_graph(irg, NULL, apply_result, &env);
722 /* control flow might changed */
723 set_irg_outs_inconsistent(irg);
724 set_irg_extblk_inconsistent(irg);
725 set_irg_doms_inconsistent(irg);
726 set_irg_loopinfo_inconsistent(irg);
727 set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
730 ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
732 obstack_free(&obst, NULL);
735 ir_graph_pass_t *fixpoint_vrp_irg_pass(const char *name)
737 return def_graph_pass(name ? name : "fixpoint_vrp", fixpoint_vrp);