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 analyze graph to provide value range information
31 #include "irgraph_t.h"
39 #include "irphase_t.h"
43 static void *VISITED = &v;
49 static vrp_attr *get_vrp_attr(const ir_node *node)
51 return (vrp_attr*) get_or_set_irn_phase_info(node, PHASE_VRP);
54 static int vrp_update_node(ir_node *node)
56 tarval *new_bits_set = get_tarval_bad();
57 tarval *new_bits_not_set = get_tarval_bad();
58 tarval *new_range_bottom = get_tarval_bad();
59 tarval *new_range_top = get_tarval_bad();
60 enum range_types new_range_type = VRP_UNDEFINED;
61 int something_changed = 0;
64 if (!mode_is_int(get_irn_mode(node))) {
65 return 0; /* we don't optimize for non-int-nodes*/
68 ir_printf("update_vrp for %d called\n", get_irn_node_nr(node));
69 vrp = get_vrp_attr(node);
71 /* TODO: Check if all predecessors have valid VRP information*/
73 switch (get_irn_opcode(node)) {
75 tarval *tv = get_Const_tarval(node);
77 new_bits_not_set = tarval_not(tv);
78 new_range_bottom = tv;
80 new_range_type = VRP_RANGE;
85 vrp_attr *vrp_left, *vrp_right;
86 ir_node *left, *right;
88 left = get_And_left(node);
89 right = get_And_right(node);
90 vrp_left = get_vrp_attr(left);
91 vrp_right = get_vrp_attr(right);
92 new_bits_set = tarval_and(vrp_left->bits_set, vrp_right->bits_set);
93 new_bits_not_set = tarval_or(vrp_left->bits_not_set, vrp_right->bits_not_set);
99 int overflow_top, overflow_bottom;
100 tarval *new_top, *new_bottom;
101 vrp_attr *vrp_left, *vrp_right;
102 vrp_left = get_vrp_attr(get_Add_left(node));
103 vrp_right = get_vrp_attr(get_Add_right(node));
105 if (vrp_left->range_type == VRP_UNDEFINED || vrp_right->range_type ==
106 VRP_UNDEFINED || vrp_left->range_type == VRP_VARYING ||
107 vrp_right->range_type == VRP_VARYING) {
111 new_top = tarval_add(vrp_left->range_top, vrp_right->range_top);
112 overflow_top = tarval_carry();
113 new_bottom = tarval_add(vrp_left->range_bottom, vrp_right->range_bottom);
114 overflow_bottom = tarval_carry();
116 if (!overflow_top && !overflow_bottom && vrp_left->range_type == VRP_RANGE
117 &&vrp_right->range_type == VRP_RANGE) {
118 new_range_bottom = new_bottom;
119 new_range_top = new_top;
120 new_range_type = VRP_RANGE;
123 if (overflow_top || overflow_bottom) {
124 /* TODO Implement overflow handling*/
125 new_range_type = VRP_UNDEFINED;
131 int overflow_top, overflow_bottom;
132 tarval *new_top, *new_bottom;
133 vrp_attr *vrp_left, *vrp_right;
134 vrp_left = get_vrp_attr(get_Sub_left(node));
135 vrp_right = get_vrp_attr(get_Sub_right(node));
137 if (vrp_left->range_type == VRP_UNDEFINED || vrp_right->range_type ==
142 new_top = tarval_sub(vrp_left->range_top, vrp_right->range_top, NULL);
143 overflow_top = tarval_carry();
144 new_bottom = tarval_sub(vrp_left->range_bottom, vrp_right->range_bottom, NULL);
145 overflow_bottom = tarval_carry();
147 if (!overflow_top && !overflow_bottom && vrp_left->range_type == VRP_RANGE
148 &&vrp_right->range_type == VRP_RANGE) {
149 new_range_bottom = new_bottom;
150 new_range_top = new_top;
151 new_range_type = VRP_RANGE;
154 if (overflow_top || overflow_bottom) {
155 /* TODO Implement overflow handling*/
161 vrp_attr *vrp_left, *vrp_right;
163 vrp_left = get_vrp_attr(get_Or_left(node));
164 vrp_right = get_vrp_attr(get_Or_right(node));
166 new_bits_set = tarval_or(vrp_left->bits_set, vrp_right->bits_set);
167 new_bits_not_set = tarval_and(vrp_left->bits_not_set, vrp_right->bits_not_set);
173 vrp_attr *vrp_left, *vrp_right;
174 ir_node *right = get_Rotl_right(node);
176 vrp_left = get_vrp_attr(get_Rotl_left(node));
177 vrp_right = get_vrp_attr(get_Rotl_right(node));
179 /* We can only compute this if the right value is a constant*/
180 if (is_Const(right)) {
181 tarval *bits_set, *bits_not_set;
182 bits_set = tarval_rotl(vrp_left->bits_set, get_Const_tarval(right));
183 bits_not_set = tarval_rotl(vrp_left->bits_not_set, get_Const_tarval(right));
185 new_bits_set = tarval_or(bits_set, vrp->bits_set);
186 new_bits_not_set = tarval_or(bits_not_set, vrp->bits_not_set);
192 vrp_attr *vrp_left, *vrp_right;
193 ir_node *right = get_Shl_right(node);
194 vrp_left = get_vrp_attr(get_Shl_left(node));
195 vrp_right = get_vrp_attr(get_Shl_right(node));
197 /* We can only compute this if the right value is a constant*/
198 if (is_Const(right)) {
199 tarval *bits_set, *bits_not_set;
200 ir_mode *m = get_tarval_mode(vrp->bits_not_set);
201 bits_set = tarval_shl(vrp_left->bits_set, get_Const_tarval(right));
202 bits_not_set = tarval_shl(vrp_left->bits_not_set, get_Const_tarval(right));
204 new_bits_set = tarval_or(bits_set, vrp->bits_set);
205 new_bits_not_set = tarval_or(bits_not_set, vrp->bits_not_set);
207 bits_not_set = tarval_not( tarval_shl(
209 get_Const_tarval(right)));
210 new_bits_not_set = tarval_or(bits_not_set, new_bits_not_set);
217 vrp_attr *vrp_left, *vrp_right;
218 ir_node *right = get_Shr_right(node);
220 vrp_left = get_vrp_attr(get_Shr_left(node));
221 vrp_right = get_vrp_attr(get_Shr_right(node));
223 /* We can only compute this if the right value is a constant*/
224 if (is_Const(right)) {
225 tarval *bits_set, *bits_not_set;
226 ir_mode *m = get_tarval_mode(vrp->bits_not_set);
227 bits_set = tarval_shr(vrp_left->bits_set, get_Const_tarval(right));
228 bits_not_set = tarval_shr(vrp_left->bits_not_set, get_Const_tarval(right));
230 new_bits_set = tarval_or(bits_set, vrp->bits_set);
231 new_bits_not_set = tarval_or(bits_not_set, vrp->bits_not_set);
233 bits_not_set = tarval_not( tarval_shr(
235 get_Const_tarval(right)));
236 new_bits_not_set = tarval_or(bits_not_set, new_bits_not_set);
242 vrp_attr *vrp_left, *vrp_right;
243 ir_node *right = get_Shrs_right(node);
245 vrp_left = get_vrp_attr(get_Shrs_left(node));
246 vrp_right = get_vrp_attr(get_Shrs_right(node));
248 /* We can only compute this if the right value is a constant*/
249 if (is_Const(right)) {
250 tarval *bits_set, *bits_not_set;
251 ir_mode *m = get_tarval_mode(vrp->bits_not_set);
252 bits_set = tarval_shrs(vrp_left->bits_set, get_Const_tarval(right));
253 bits_not_set = tarval_shrs(vrp_left->bits_not_set, get_Const_tarval(right));
255 new_bits_set = tarval_or(bits_set, vrp->bits_set);
256 new_bits_not_set = tarval_or(bits_not_set, vrp->bits_not_set);
258 bits_not_set = tarval_not( tarval_shrs(
260 get_Const_tarval(right)));
261 new_bits_not_set = tarval_or(bits_not_set, new_bits_not_set);
267 tarval *bits_set, *bits_not_set;
268 vrp_attr *vrp_left, *vrp_right;
270 vrp_left = get_vrp_attr(get_Eor_left(node));
271 vrp_right = get_vrp_attr(get_Eor_right(node));
273 bits_not_set = tarval_or(
274 tarval_and(vrp_left->bits_set, vrp_right->bits_set),
275 tarval_and(vrp_left->bits_not_set,
276 vrp_right->bits_not_set));
278 bits_set = tarval_or(
279 tarval_and(vrp_left->bits_set, vrp_right->bits_not_set),
280 tarval_and(vrp_left->bits_not_set, vrp_right->bits_set));
282 new_bits_set = tarval_or(bits_set, vrp->bits_set);
283 new_bits_not_set = tarval_or(bits_not_set, vrp->bits_not_set);
288 vrp_attr *vrp_pred = get_vrp_attr(get_Id_pred(node));
289 new_bits_set = vrp_pred->bits_set;
290 new_bits_not_set = vrp_pred->bits_not_set;
291 new_range_top = vrp_pred->range_top;
292 new_range_bottom = vrp_pred->range_bottom;
293 new_range_type = vrp_pred->range_type;
298 vrp_attr *vrp_pred = get_vrp_attr(get_Not_op(node));
299 new_bits_set = tarval_or(vrp_pred->bits_not_set, vrp->bits_set);
300 new_bits_not_set = tarval_or(vrp_pred->bits_set, vrp->bits_not_set);
305 ir_node *pred = get_Conv_op(node);
306 ir_mode *old_mode = get_irn_mode(pred);
307 vrp_attr *vrp_pred = get_vrp_attr(pred);
310 tarval *bits_not_set;
312 if (!mode_is_int(old_mode))
315 new_mode = get_irn_mode(node);
317 /* The second and is needed if target type is smaller*/
318 bits_not_set = tarval_not(
319 tarval_convert_to(get_mode_all_one(old_mode),
322 bits_not_set = tarval_or(bits_not_set, tarval_convert_to(vrp_pred->bits_not_set, new_mode));
323 new_bits_not_set = tarval_or(bits_not_set, vrp->bits_not_set);
324 new_bits_set = tarval_and(
325 tarval_not(bits_not_set), tarval_convert_to(vrp_pred->bits_set, new_mode));
327 if (tarval_cmp(vrp_pred->range_top, get_mode_max(new_mode)) == pn_Cmp_Le) {
328 vrp->range_top = vrp_pred->range_top;
331 if (tarval_cmp(vrp_pred->range_bottom, get_mode_min(new_mode)) == pn_Cmp_Ge) {
332 vrp->range_bottom = vrp_pred->range_bottom;
338 pn_Cmp cmp = get_Confirm_cmp(node);
339 ir_node *bound = get_Confirm_bound(node);
341 /** @todo: Handle non-Const bounds */
343 if (cmp == pn_Cmp_Lg) {
344 /** @todo: Is there some way to preserve the information? */
345 new_range_type = VRP_ANTIRANGE;
346 if (is_Const(bound)) {
347 new_range_top = get_Const_tarval(bound);
348 new_range_bottom = get_Const_tarval(bound);
350 } else if (cmp == pn_Cmp_Le) {
351 if (vrp->range_type == VRP_UNDEFINED) {
352 new_range_type = VRP_RANGE;
353 if (is_Const(bound)) {
354 new_range_top = get_Const_tarval(bound);
356 new_range_bottom = get_tarval_min(get_irn_mode(node));
357 } else if (vrp->range_type == VRP_RANGE) {
358 if (is_Const(bound)) {
359 if (tarval_cmp(vrp->range_top,
360 get_Const_tarval(bound)) == pn_Cmp_Le) {
361 new_range_top = get_Const_tarval(bound);
363 new_range_bottom = get_tarval_min(get_irn_mode(node));
365 } else if (vrp->range_type == VRP_ANTIRANGE) {
366 /** @todo: How do we manage not to get a never ending loop? */
374 /* combine all ranges*/
376 int num = get_Phi_n_preds(node);
380 ir_node *pred = get_Phi_pred(node,0);
381 vrp_attr *vrp_pred = get_vrp_attr(pred);
382 new_range_top = vrp_pred->range_top;
383 new_range_bottom = vrp_pred->range_bottom;
384 new_range_type = vrp_pred->range_type;
385 new_bits_set = vrp_pred->bits_set;
386 new_bits_not_set = vrp_pred->bits_not_set;
391 for (i = 1; i < num; i++) {
392 pred = get_Phi_pred(node, i);
393 vrp_pred = get_vrp_attr(pred);
394 if (new_range_type == VRP_RANGE && vrp_pred->range_type ==
396 cmp = tarval_cmp(new_range_top, vrp_pred->range_top);
397 if (cmp == pn_Cmp_Lt) {
398 new_range_top = vrp_pred->range_top;
400 cmp = tarval_cmp(new_range_bottom, vrp_pred->range_bottom);
401 if (cmp == pn_Cmp_Gt) {
402 new_range_bottom = vrp_pred->range_bottom;
405 new_range_type = VRP_VARYING;
413 /* unhandled, therefore never updated */
419 /* TODO: Check, if there can be information derived from any of these:
420 is_Abs(node) is_Alloc(node) is_Anchor(node) is_Borrow(node) is_Bound(node)
421 is_Break(node) is_Builtin(node) is_Call(node) is_CallBegin(node)
422 is_Carry(node) is_Cast(node) is_Cmp(node) is_Cond(node)
423 is_CopyB(node) is_Div(node) is_DivMod(node) is_Dummy(node)
424 is_End(node) is_EndExcept(node) is_EndReg(node) is_Filter(node) is_Free(node)
425 is_IJmp(node) is_InstOf(node) is_Jmp(node) is_Load(node) is_Minus(node)
426 is_Mod(node) is_Mul(node) is_Mulh(node) is_Mux(node) is_NoMem(node)
427 is_Pin(node) is_Proj(node) is_Quot(node)
428 is_Raise(node) is_Return(node) is_Sel(node) is_Start(node) is_Store(node)
429 is_SymConst(node) is_Sync(node) is_Tuple(node)
432 /* Merge the newly calculated values with those that might already exist*/
433 if (new_bits_set != tarval_bad) {
434 new_bits_set = tarval_or(new_bits_set, vrp->bits_set);
435 if (tarval_cmp(new_bits_set, vrp->bits_set) != pn_Cmp_Eq) {
436 something_changed = 1;
437 vrp->bits_set = new_bits_set;
441 if (new_bits_not_set != tarval_bad) {
442 new_bits_not_set = tarval_or(new_bits_not_set, vrp->bits_not_set);
444 if (tarval_cmp(new_bits_not_set, vrp->bits_not_set) != pn_Cmp_Eq) {
445 something_changed = 1;
446 vrp->bits_not_set = new_bits_not_set;
450 if (vrp->range_type == VRP_UNDEFINED &&
451 new_range_type != VRP_UNDEFINED) {
452 something_changed = 1;
453 vrp->range_type = new_range_type;
454 vrp->range_bottom = new_range_bottom;
455 vrp->range_top = new_range_top;
457 } else if (vrp->range_type == VRP_RANGE) {
458 if (new_range_type == VRP_RANGE) {
459 if (tarval_cmp(vrp->range_bottom, new_range_bottom) == pn_Cmp_Lt) {
460 something_changed = 1;
461 vrp->range_bottom = new_range_bottom;
463 if (tarval_cmp(vrp->range_top, new_range_top) == pn_Cmp_Gt) {
464 something_changed = 1;
465 vrp->range_top = new_range_top;
469 if (new_range_type == VRP_ANTIRANGE) {
470 /* if they are overlapping, cut the range.*/
471 /* TODO: Maybe we can preserve more information here*/
472 if (tarval_cmp(vrp->range_bottom, new_range_top) == pn_Cmp_Gt &&
473 tarval_cmp(vrp->range_bottom, new_range_bottom) == pn_Cmp_Gt) {
474 something_changed = 1;
475 vrp->range_bottom = new_range_top;
477 } else if (tarval_cmp(vrp->range_top, new_range_bottom) == pn_Cmp_Gt &&
478 tarval_cmp(vrp->range_top, new_range_top) == pn_Cmp_Lt) {
479 something_changed = 1;
480 vrp->range_top = new_range_bottom;
483 /* We can not handle the case where the anti range is in the*/
487 } else if (vrp->range_type == VRP_ANTIRANGE) {
488 if (new_range_type == VRP_ANTIRANGE) {
489 if (tarval_cmp(vrp->range_bottom, new_range_bottom) == pn_Cmp_Gt) {
490 something_changed = 1;
491 vrp->range_bottom = new_range_bottom;
493 if (tarval_cmp(vrp->range_top, new_range_top) == pn_Cmp_Lt) {
494 something_changed = 1;
495 vrp->range_top = new_range_top;
499 if (new_range_type == VRP_RANGE) {
500 if (tarval_cmp(vrp->range_bottom, new_range_top) == pn_Cmp_Gt) {
501 something_changed = 1;
502 vrp->range_bottom = new_range_top;
504 if (tarval_cmp(vrp->range_top, new_range_bottom) == pn_Cmp_Lt) {
505 something_changed = 1;
506 vrp->range_top = new_range_bottom;
511 assert(tarval_is_null(
512 tarval_and(vrp->bits_set, vrp->bits_not_set)));
513 return something_changed;
516 static void vrp_first_pass(ir_node *n, void *e)
520 struct vrp_env_t *env = e;
525 set_irn_link(n, VISITED);
529 for (i = get_irn_n_outs(n) - 1; i >=0; --i) {
530 succ = get_irn_out(n, i);
531 if (get_irn_link(succ) == VISITED) {
533 waitq_put(env->workqueue, n);
538 static void *vrp_init_node(ir_phase *phase, const ir_node *n, void *old)
543 ir_printf("initialized node nr: %d\n", get_irn_node_nr(n));
545 assert(1==0 && "init called for node already initialized");
547 vrp = phase_alloc(phase, sizeof(vrp_attr));
549 memset(vrp, 0, sizeof(vrp_attr));
550 /* Initialize the vrp information to default */
552 mode = get_irn_mode(n);
554 vrp->range_type = VRP_UNDEFINED;
556 /* TODO: We might be able to optimize space usage if we do not allocate
557 * vrp space for non-int nodes. (currently caught by vrp_update_node)
559 if (mode_is_int(mode)) {
560 /* We are assuming that 0 is always represented as this modes null */
562 vrp->bits_set = get_mode_null(mode);
563 vrp->bits_not_set = get_mode_null(mode);
564 vrp->range_bottom = get_tarval_top();
565 vrp->range_top = get_tarval_top();
568 vrp->bits_set = get_tarval_bad();
569 vrp->bits_not_set = get_tarval_bad();
570 vrp->range_bottom = get_tarval_bad();
571 vrp->range_top = get_tarval_bad();
574 /* TODO: We might be able to set better vrp info at this time, if this is
575 * a node which is newly created in an already initialized irg
577 * maybe just call vrp_update_node and if it returns one, iterate over
583 void set_vrp_data(ir_graph *irg)
585 ir_node *succ, *node;
587 struct vrp_env_t *env;
590 assure_irg_outs(irg); /* ensure that out edges are consistent*/
591 phase = irg_get_phase(irg, PHASE_VRP);
593 /* this is our first run */
594 phase = new_phase(irg, vrp_init_node);
595 irg_register_phase(irg, PHASE_VRP, phase);
596 env = phase_alloc(phase, sizeof(*env));
602 env->workqueue = new_waitq();
604 irg_walk_graph(irg, NULL, vrp_first_pass, env);
606 /* while there are entries in the worklist, continue*/
607 while (!waitq_empty(env->workqueue)) {
608 node = waitq_get(env->workqueue);
610 if (vrp_update_node(node)) {
611 /* if something changed, add successors to worklist*/
612 for (i = get_irn_n_outs(node) - 1; i >=0; --i) {
613 succ = get_irn_out(node, i);
614 waitq_put(env->workqueue, node);
618 del_waitq(env->workqueue);
622 ir_graph_pass_t *set_vrp_pass(const char *name)
624 return def_graph_pass(name ? name : "set_vrp", set_vrp_data);
627 pn_Cmp vrp_cmp(const ir_node *left, const ir_node *right)
629 vrp_attr *vrp_left, *vrp_right;
631 vrp_left = vrp_get_info(left);
632 vrp_right = vrp_get_info(right);
634 if (!vrp_left || !vrp_right) {
638 if (vrp_left->range_type == VRP_RANGE && vrp_right->range_type == VRP_RANGE) {
639 if (tarval_cmp(vrp_left->range_top, vrp_right->range_bottom) == pn_Cmp_Lt) {
642 if (tarval_cmp(vrp_left->range_bottom, vrp_right->range_top) == pn_Cmp_Gt) {
647 if (!tarval_is_null(tarval_and(vrp_left->bits_set, vrp_right->bits_not_set)) ||
648 !tarval_is_null(tarval_and(vrp_left->bits_not_set, vrp_right->bits_set))) {
651 /* TODO: We can get way more information here*/
656 vrp_attr *vrp_get_info(const ir_node *node)
658 const ir_graph *irg = get_irn_irg(node);
659 const ir_phase *phase = irg_get_phase(irg, PHASE_VRP);
662 /* phase has not yet been initialized */
666 return phase_get_irn_data(phase, node);