1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
24 #include "coretypes.h"
29 #include "basic-block.h"
30 #include "tree-flow.h"
31 #include "tree-pass.h"
32 #include "tree-dump.h"
34 #include "diagnostic.h"
38 #include "tree-scalar-evolution.h"
39 #include "tree-ssa-propagate.h"
40 #include "tree-chrec.h"
42 /* Set of SSA names found during the dominator traversal of a
43 sub-graph in find_assert_locations. */
44 static sbitmap found_in_subgraph
;
46 /* Local functions. */
47 static int compare_values (tree val1
, tree val2
);
48 static int compare_values_warnv (tree val1
, tree val2
, bool *);
49 static tree
vrp_evaluate_conditional_warnv (tree
, bool, bool *);
51 /* Location information for ASSERT_EXPRs. Each instance of this
52 structure describes an ASSERT_EXPR for an SSA name. Since a single
53 SSA name may have more than one assertion associated with it, these
54 locations are kept in a linked list attached to the corresponding
58 /* Basic block where the assertion would be inserted. */
61 /* Some assertions need to be inserted on an edge (e.g., assertions
62 generated by COND_EXPRs). In those cases, BB will be NULL. */
65 /* Pointer to the statement that generated this assertion. */
66 block_stmt_iterator si
;
68 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
69 enum tree_code comp_code
;
71 /* Value being compared against. */
74 /* Next node in the linked list. */
75 struct assert_locus_d
*next
;
78 typedef struct assert_locus_d
*assert_locus_t
;
80 /* If bit I is present, it means that SSA name N_i has a list of
81 assertions that should be inserted in the IL. */
82 static bitmap need_assert_for
;
84 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
85 holds a list of ASSERT_LOCUS_T nodes that describe where
86 ASSERT_EXPRs for SSA name N_I should be inserted. */
87 static assert_locus_t
*asserts_for
;
89 /* Set of blocks visited in find_assert_locations. Used to avoid
90 visiting the same block more than once. */
91 static sbitmap blocks_visited
;
93 /* Value range array. After propagation, VR_VALUE[I] holds the range
94 of values that SSA name N_I may take. */
95 static value_range_t
**vr_value
;
98 /* Return whether TYPE should use an overflow infinity distinct from
99 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
100 represent a signed overflow during VRP computations. An infinity
101 is distinct from a half-range, which will go from some number to
102 TYPE_{MIN,MAX}_VALUE. */
105 needs_overflow_infinity (tree type
)
107 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
110 /* Return whether TYPE can support our overflow infinity
111 representation: we use the TREE_OVERFLOW flag, which only exists
112 for constants. If TYPE doesn't support this, we don't optimize
113 cases which would require signed overflow--we drop them to
117 supports_overflow_infinity (tree type
)
119 #ifdef ENABLE_CHECKING
120 gcc_assert (needs_overflow_infinity (type
));
122 return (TYPE_MIN_VALUE (type
) != NULL_TREE
123 && CONSTANT_CLASS_P (TYPE_MIN_VALUE (type
))
124 && TYPE_MAX_VALUE (type
) != NULL_TREE
125 && CONSTANT_CLASS_P (TYPE_MAX_VALUE (type
)));
128 /* VAL is the maximum or minimum value of a type. Return a
129 corresponding overflow infinity. */
132 make_overflow_infinity (tree val
)
134 #ifdef ENABLE_CHECKING
135 gcc_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
137 val
= copy_node (val
);
138 TREE_OVERFLOW (val
) = 1;
142 /* Return a negative overflow infinity for TYPE. */
145 negative_overflow_infinity (tree type
)
147 #ifdef ENABLE_CHECKING
148 gcc_assert (supports_overflow_infinity (type
));
150 return make_overflow_infinity (TYPE_MIN_VALUE (type
));
153 /* Return a positive overflow infinity for TYPE. */
156 positive_overflow_infinity (tree type
)
158 #ifdef ENABLE_CHECKING
159 gcc_assert (supports_overflow_infinity (type
));
161 return make_overflow_infinity (TYPE_MAX_VALUE (type
));
164 /* Return whether VAL is a negative overflow infinity. */
167 is_negative_overflow_infinity (tree val
)
169 return (needs_overflow_infinity (TREE_TYPE (val
))
170 && CONSTANT_CLASS_P (val
)
171 && TREE_OVERFLOW (val
)
172 && operand_equal_p (val
, TYPE_MIN_VALUE (TREE_TYPE (val
)), 0));
175 /* Return whether VAL is a positive overflow infinity. */
178 is_positive_overflow_infinity (tree val
)
180 return (needs_overflow_infinity (TREE_TYPE (val
))
181 && CONSTANT_CLASS_P (val
)
182 && TREE_OVERFLOW (val
)
183 && operand_equal_p (val
, TYPE_MAX_VALUE (TREE_TYPE (val
)), 0));
186 /* Return whether VAL is a positive or negative overflow infinity. */
189 is_overflow_infinity (tree val
)
191 return (needs_overflow_infinity (TREE_TYPE (val
))
192 && CONSTANT_CLASS_P (val
)
193 && TREE_OVERFLOW (val
)
194 && (operand_equal_p (val
, TYPE_MAX_VALUE (TREE_TYPE (val
)), 0)
195 || operand_equal_p (val
, TYPE_MIN_VALUE (TREE_TYPE (val
)), 0)));
198 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
199 the same value with TREE_OVERFLOW clear. This can be used to avoid
200 confusing a regular value with an overflow value. */
203 avoid_overflow_infinity (tree val
)
205 if (!is_overflow_infinity (val
))
208 if (operand_equal_p (val
, TYPE_MAX_VALUE (TREE_TYPE (val
)), 0))
209 return TYPE_MAX_VALUE (TREE_TYPE (val
));
212 #ifdef ENABLE_CHECKING
213 gcc_assert (operand_equal_p (val
, TYPE_MIN_VALUE (TREE_TYPE (val
)), 0));
215 return TYPE_MIN_VALUE (TREE_TYPE (val
));
220 /* Return whether VAL is equal to the maximum value of its type. This
221 will be true for a positive overflow infinity. We can't do a
222 simple equality comparison with TYPE_MAX_VALUE because C typedefs
223 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
224 to the integer constant with the same value in the type. */
227 vrp_val_is_max (tree val
)
229 tree type_max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
231 return (val
== type_max
232 || (type_max
!= NULL_TREE
233 && operand_equal_p (val
, type_max
, 0)));
236 /* Return whether VAL is equal to the minimum value of its type. This
237 will be true for a negative overflow infinity. */
240 vrp_val_is_min (tree val
)
242 tree type_min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
244 return (val
== type_min
245 || (type_min
!= NULL_TREE
246 && operand_equal_p (val
, type_min
, 0)));
250 /* Return true if ARG is marked with the nonnull attribute in the
251 current function signature. */
254 nonnull_arg_p (tree arg
)
256 tree t
, attrs
, fntype
;
257 unsigned HOST_WIDE_INT arg_num
;
259 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
261 /* The static chain decl is always non null. */
262 if (arg
== cfun
->static_chain_decl
)
265 fntype
= TREE_TYPE (current_function_decl
);
266 attrs
= lookup_attribute ("nonnull", TYPE_ATTRIBUTES (fntype
));
268 /* If "nonnull" wasn't specified, we know nothing about the argument. */
269 if (attrs
== NULL_TREE
)
272 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
273 if (TREE_VALUE (attrs
) == NULL_TREE
)
276 /* Get the position number for ARG in the function signature. */
277 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
279 t
= TREE_CHAIN (t
), arg_num
++)
285 gcc_assert (t
== arg
);
287 /* Now see if ARG_NUM is mentioned in the nonnull list. */
288 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
290 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
298 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
301 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
302 tree max
, bitmap equiv
)
304 #if defined ENABLE_CHECKING
305 /* Check the validity of the range. */
306 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
310 gcc_assert (min
&& max
);
312 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
313 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
315 cmp
= compare_values (min
, max
);
316 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
318 if (needs_overflow_infinity (TREE_TYPE (min
)))
319 gcc_assert (!is_overflow_infinity (min
)
320 || !is_overflow_infinity (max
));
323 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
324 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
326 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
327 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
334 /* Since updating the equivalence set involves deep copying the
335 bitmaps, only do it if absolutely necessary. */
336 if (vr
->equiv
== NULL
)
337 vr
->equiv
= BITMAP_ALLOC (NULL
);
339 if (equiv
!= vr
->equiv
)
341 if (equiv
&& !bitmap_empty_p (equiv
))
342 bitmap_copy (vr
->equiv
, equiv
);
344 bitmap_clear (vr
->equiv
);
349 /* Copy value range FROM into value range TO. */
352 copy_value_range (value_range_t
*to
, value_range_t
*from
)
354 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
358 /* Set value range VR to VR_VARYING. */
361 set_value_range_to_varying (value_range_t
*vr
)
363 vr
->type
= VR_VARYING
;
364 vr
->min
= vr
->max
= NULL_TREE
;
366 bitmap_clear (vr
->equiv
);
369 /* Set value range VR to a single value. This function is only called
370 with values we get from statements, and exists to clear the
371 TREE_OVERFLOW flag so that we don't think we have an overflow
372 infinity when we shouldn't. */
375 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
377 gcc_assert (is_gimple_min_invariant (val
));
378 val
= avoid_overflow_infinity (val
);
379 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
382 /* Set value range VR to a non-negative range of type TYPE.
383 OVERFLOW_INFINITY indicates whether to use a overflow infinity
384 rather than TYPE_MAX_VALUE; this should be true if we determine
385 that the range is nonnegative based on the assumption that signed
386 overflow does not occur. */
389 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
390 bool overflow_infinity
)
394 if (overflow_infinity
&& !supports_overflow_infinity (type
))
396 set_value_range_to_varying (vr
);
400 zero
= build_int_cst (type
, 0);
401 set_value_range (vr
, VR_RANGE
, zero
,
403 ? positive_overflow_infinity (type
)
404 : TYPE_MAX_VALUE (type
)),
408 /* Set value range VR to a non-NULL range of type TYPE. */
411 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
413 tree zero
= build_int_cst (type
, 0);
414 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
418 /* Set value range VR to a NULL range of type TYPE. */
421 set_value_range_to_null (value_range_t
*vr
, tree type
)
423 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
427 /* Set value range VR to VR_UNDEFINED. */
430 set_value_range_to_undefined (value_range_t
*vr
)
432 vr
->type
= VR_UNDEFINED
;
433 vr
->min
= vr
->max
= NULL_TREE
;
435 bitmap_clear (vr
->equiv
);
439 /* Return value range information for VAR.
441 If we have no values ranges recorded (ie, VRP is not running), then
442 return NULL. Otherwise create an empty range if none existed for VAR. */
444 static value_range_t
*
445 get_value_range (tree var
)
449 unsigned ver
= SSA_NAME_VERSION (var
);
451 /* If we have no recorded ranges, then return NULL. */
459 /* Create a default value range. */
460 vr_value
[ver
] = vr
= XNEW (value_range_t
);
461 memset (vr
, 0, sizeof (*vr
));
463 /* Allocate an equivalence set. */
464 vr
->equiv
= BITMAP_ALLOC (NULL
);
466 /* If VAR is a default definition, the variable can take any value
468 sym
= SSA_NAME_VAR (var
);
469 if (var
== default_def (sym
))
471 /* Try to use the "nonnull" attribute to create ~[0, 0]
472 anti-ranges for pointers. Note that this is only valid with
473 default definitions of PARM_DECLs. */
474 if (TREE_CODE (sym
) == PARM_DECL
475 && POINTER_TYPE_P (TREE_TYPE (sym
))
476 && nonnull_arg_p (sym
))
477 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
479 set_value_range_to_varying (vr
);
485 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
488 vrp_operand_equal_p (tree val1
, tree val2
)
492 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
494 if (is_overflow_infinity (val1
))
495 return is_overflow_infinity (val2
);
499 /* Return true, if the bitmaps B1 and B2 are equal. */
502 vrp_bitmap_equal_p (bitmap b1
, bitmap b2
)
506 && bitmap_equal_p (b1
, b2
)));
509 /* Update the value range and equivalence set for variable VAR to
510 NEW_VR. Return true if NEW_VR is different from VAR's previous
513 NOTE: This function assumes that NEW_VR is a temporary value range
514 object created for the sole purpose of updating VAR's range. The
515 storage used by the equivalence set from NEW_VR will be freed by
516 this function. Do not call update_value_range when NEW_VR
517 is the range object associated with another SSA name. */
520 update_value_range (tree var
, value_range_t
*new_vr
)
522 value_range_t
*old_vr
;
525 /* Update the value range, if necessary. */
526 old_vr
= get_value_range (var
);
527 is_new
= old_vr
->type
!= new_vr
->type
528 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
529 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
530 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
533 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
536 BITMAP_FREE (new_vr
->equiv
);
537 new_vr
->equiv
= NULL
;
543 /* Add VAR and VAR's equivalence set to EQUIV. */
546 add_equivalence (bitmap equiv
, tree var
)
548 unsigned ver
= SSA_NAME_VERSION (var
);
549 value_range_t
*vr
= vr_value
[ver
];
551 bitmap_set_bit (equiv
, ver
);
553 bitmap_ior_into (equiv
, vr
->equiv
);
557 /* Return true if VR is ~[0, 0]. */
560 range_is_nonnull (value_range_t
*vr
)
562 return vr
->type
== VR_ANTI_RANGE
563 && integer_zerop (vr
->min
)
564 && integer_zerop (vr
->max
);
568 /* Return true if VR is [0, 0]. */
571 range_is_null (value_range_t
*vr
)
573 return vr
->type
== VR_RANGE
574 && integer_zerop (vr
->min
)
575 && integer_zerop (vr
->max
);
579 /* Return true if value range VR involves at least one symbol. */
582 symbolic_range_p (value_range_t
*vr
)
584 return (!is_gimple_min_invariant (vr
->min
)
585 || !is_gimple_min_invariant (vr
->max
));
588 /* Return true if value range VR uses a overflow infinity. */
591 overflow_infinity_range_p (value_range_t
*vr
)
593 return (vr
->type
== VR_RANGE
594 && (is_overflow_infinity (vr
->min
)
595 || is_overflow_infinity (vr
->max
)));
598 /* Return false if we can not make a valid comparison based on VR;
599 this will be the case if it uses an overflow infinity and overflow
600 is not undefined (i.e., -fno-strict-overflow is in effect).
601 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
602 uses an overflow infinity. */
605 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
607 gcc_assert (vr
->type
== VR_RANGE
);
608 if (is_overflow_infinity (vr
->min
))
610 *strict_overflow_p
= true;
611 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
614 if (is_overflow_infinity (vr
->max
))
616 *strict_overflow_p
= true;
617 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
624 /* Like tree_expr_nonnegative_warnv_p, but this function uses value
625 ranges obtained so far. */
628 vrp_expr_computes_nonnegative (tree expr
, bool *strict_overflow_p
)
630 return tree_expr_nonnegative_warnv_p (expr
, strict_overflow_p
);
633 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
637 vrp_expr_computes_nonzero (tree expr
, bool *strict_overflow_p
)
639 if (tree_expr_nonzero_warnv_p (expr
, strict_overflow_p
))
642 /* If we have an expression of the form &X->a, then the expression
643 is nonnull if X is nonnull. */
644 if (TREE_CODE (expr
) == ADDR_EXPR
)
646 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
648 if (base
!= NULL_TREE
649 && TREE_CODE (base
) == INDIRECT_REF
650 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
652 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
653 if (range_is_nonnull (vr
))
661 /* Returns true if EXPR is a valid value (as expected by compare_values) --
662 a gimple invariant, or SSA_NAME +- CST. */
665 valid_value_p (tree expr
)
667 if (TREE_CODE (expr
) == SSA_NAME
)
670 if (TREE_CODE (expr
) == PLUS_EXPR
671 || TREE_CODE (expr
) == MINUS_EXPR
)
672 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
673 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
675 return is_gimple_min_invariant (expr
);
678 /* Compare two values VAL1 and VAL2. Return
680 -2 if VAL1 and VAL2 cannot be compared at compile-time,
683 +1 if VAL1 > VAL2, and
686 This is similar to tree_int_cst_compare but supports pointer values
687 and values that cannot be compared at compile time.
689 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
690 true if the return value is only valid if we assume that signed
691 overflow is undefined. */
694 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
699 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
701 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
702 == POINTER_TYPE_P (TREE_TYPE (val2
)));
704 if ((TREE_CODE (val1
) == SSA_NAME
705 || TREE_CODE (val1
) == PLUS_EXPR
706 || TREE_CODE (val1
) == MINUS_EXPR
)
707 && (TREE_CODE (val2
) == SSA_NAME
708 || TREE_CODE (val2
) == PLUS_EXPR
709 || TREE_CODE (val2
) == MINUS_EXPR
))
712 enum tree_code code1
, code2
;
714 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
715 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
716 same name, return -2. */
717 if (TREE_CODE (val1
) == SSA_NAME
)
725 code1
= TREE_CODE (val1
);
726 n1
= TREE_OPERAND (val1
, 0);
727 c1
= TREE_OPERAND (val1
, 1);
728 if (tree_int_cst_sgn (c1
) == -1)
730 if (is_negative_overflow_infinity (c1
))
732 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
735 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
739 if (TREE_CODE (val2
) == SSA_NAME
)
747 code2
= TREE_CODE (val2
);
748 n2
= TREE_OPERAND (val2
, 0);
749 c2
= TREE_OPERAND (val2
, 1);
750 if (tree_int_cst_sgn (c2
) == -1)
752 if (is_negative_overflow_infinity (c2
))
754 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
757 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
761 /* Both values must use the same name. */
765 if (code1
== SSA_NAME
766 && code2
== SSA_NAME
)
770 /* If overflow is defined we cannot simplify more. */
771 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
774 if (strict_overflow_p
!= NULL
775 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
776 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
777 *strict_overflow_p
= true;
779 if (code1
== SSA_NAME
)
781 if (code2
== PLUS_EXPR
)
782 /* NAME < NAME + CST */
784 else if (code2
== MINUS_EXPR
)
785 /* NAME > NAME - CST */
788 else if (code1
== PLUS_EXPR
)
790 if (code2
== SSA_NAME
)
791 /* NAME + CST > NAME */
793 else if (code2
== PLUS_EXPR
)
794 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
795 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
796 else if (code2
== MINUS_EXPR
)
797 /* NAME + CST1 > NAME - CST2 */
800 else if (code1
== MINUS_EXPR
)
802 if (code2
== SSA_NAME
)
803 /* NAME - CST < NAME */
805 else if (code2
== PLUS_EXPR
)
806 /* NAME - CST1 < NAME + CST2 */
808 else if (code2
== MINUS_EXPR
)
809 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
810 C1 and C2 are swapped in the call to compare_values. */
811 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
817 /* We cannot compare non-constants. */
818 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
821 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
823 /* We cannot compare overflowed values, except for overflow
825 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
827 if (strict_overflow_p
!= NULL
)
828 *strict_overflow_p
= true;
829 if (is_negative_overflow_infinity (val1
))
830 return is_negative_overflow_infinity (val2
) ? 0 : -1;
831 else if (is_negative_overflow_infinity (val2
))
833 else if (is_positive_overflow_infinity (val1
))
834 return is_positive_overflow_infinity (val2
) ? 0 : 1;
835 else if (is_positive_overflow_infinity (val2
))
840 return tree_int_cst_compare (val1
, val2
);
846 /* First see if VAL1 and VAL2 are not the same. */
847 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
850 /* If VAL1 is a lower address than VAL2, return -1. */
851 t
= fold_binary (LT_EXPR
, boolean_type_node
, val1
, val2
);
852 if (t
== boolean_true_node
)
855 /* If VAL1 is a higher address than VAL2, return +1. */
856 t
= fold_binary (GT_EXPR
, boolean_type_node
, val1
, val2
);
857 if (t
== boolean_true_node
)
860 /* If VAL1 is different than VAL2, return +2. */
861 t
= fold_binary (NE_EXPR
, boolean_type_node
, val1
, val2
);
862 if (t
== boolean_true_node
)
869 /* Compare values like compare_values_warnv, but treat comparisons of
870 nonconstants which rely on undefined overflow as incomparable. */
873 compare_values (tree val1
, tree val2
)
879 ret
= compare_values_warnv (val1
, val2
, &sop
);
881 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
887 /* Return 1 if VAL is inside value range VR (VR->MIN <= VAL <= VR->MAX),
888 0 if VAL is not inside VR,
889 -2 if we cannot tell either way.
891 FIXME, the current semantics of this functions are a bit quirky
892 when taken in the context of VRP. In here we do not care
893 about VR's type. If VR is the anti-range ~[3, 5] the call
894 value_inside_range (4, VR) will return 1.
896 This is counter-intuitive in a strict sense, but the callers
897 currently expect this. They are calling the function
898 merely to determine whether VR->MIN <= VAL <= VR->MAX. The
899 callers are applying the VR_RANGE/VR_ANTI_RANGE semantics
902 This also applies to value_ranges_intersect_p and
903 range_includes_zero_p. The semantics of VR_RANGE and
904 VR_ANTI_RANGE should be encoded here, but that also means
905 adapting the users of these functions to the new semantics. */
908 value_inside_range (tree val
, value_range_t
*vr
)
912 fold_defer_overflow_warnings ();
914 cmp1
= fold_binary_to_constant (GE_EXPR
, boolean_type_node
, val
, vr
->min
);
917 fold_undefer_and_ignore_overflow_warnings ();
921 cmp2
= fold_binary_to_constant (LE_EXPR
, boolean_type_node
, val
, vr
->max
);
923 fold_undefer_and_ignore_overflow_warnings ();
928 return cmp1
== boolean_true_node
&& cmp2
== boolean_true_node
;
932 /* Return true if value ranges VR0 and VR1 have a non-empty
936 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
938 return (value_inside_range (vr1
->min
, vr0
) == 1
939 || value_inside_range (vr1
->max
, vr0
) == 1
940 || value_inside_range (vr0
->min
, vr1
) == 1
941 || value_inside_range (vr0
->max
, vr1
) == 1);
945 /* Return true if VR includes the value zero, false otherwise. FIXME,
946 currently this will return false for an anti-range like ~[-4, 3].
947 This will be wrong when the semantics of value_inside_range are
948 modified (currently the users of this function expect these
952 range_includes_zero_p (value_range_t
*vr
)
956 gcc_assert (vr
->type
!= VR_UNDEFINED
957 && vr
->type
!= VR_VARYING
958 && !symbolic_range_p (vr
));
960 zero
= build_int_cst (TREE_TYPE (vr
->min
), 0);
961 return (value_inside_range (zero
, vr
) == 1);
964 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
965 false otherwise or if no value range information is available. */
968 ssa_name_nonnegative_p (tree t
)
970 value_range_t
*vr
= get_value_range (t
);
975 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
976 which would return a useful value should be encoded as a VR_RANGE. */
977 if (vr
->type
== VR_RANGE
)
979 int result
= compare_values (vr
->min
, integer_zero_node
);
981 return (result
== 0 || result
== 1);
986 /* Return true if T, an SSA_NAME, is known to be nonzero. Return
987 false otherwise or if no value range information is available. */
990 ssa_name_nonzero_p (tree t
)
992 value_range_t
*vr
= get_value_range (t
);
997 /* A VR_RANGE which does not include zero is a nonzero value. */
998 if (vr
->type
== VR_RANGE
&& !symbolic_range_p (vr
))
999 return ! range_includes_zero_p (vr
);
1001 /* A VR_ANTI_RANGE which does include zero is a nonzero value. */
1002 if (vr
->type
== VR_ANTI_RANGE
&& !symbolic_range_p (vr
))
1003 return range_includes_zero_p (vr
);
1009 /* Extract value range information from an ASSERT_EXPR EXPR and store
1013 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1015 tree var
, cond
, limit
, min
, max
, type
;
1016 value_range_t
*var_vr
, *limit_vr
;
1017 enum tree_code cond_code
;
1019 var
= ASSERT_EXPR_VAR (expr
);
1020 cond
= ASSERT_EXPR_COND (expr
);
1022 gcc_assert (COMPARISON_CLASS_P (cond
));
1024 /* Find VAR in the ASSERT_EXPR conditional. */
1025 if (var
== TREE_OPERAND (cond
, 0))
1027 /* If the predicate is of the form VAR COMP LIMIT, then we just
1028 take LIMIT from the RHS and use the same comparison code. */
1029 limit
= TREE_OPERAND (cond
, 1);
1030 cond_code
= TREE_CODE (cond
);
1034 /* If the predicate is of the form LIMIT COMP VAR, then we need
1035 to flip around the comparison code to create the proper range
1037 limit
= TREE_OPERAND (cond
, 0);
1038 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1041 limit
= avoid_overflow_infinity (limit
);
1043 type
= TREE_TYPE (limit
);
1044 gcc_assert (limit
!= var
);
1046 /* For pointer arithmetic, we only keep track of pointer equality
1048 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1050 set_value_range_to_varying (vr_p
);
1054 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1055 try to use LIMIT's range to avoid creating symbolic ranges
1057 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1059 /* LIMIT's range is only interesting if it has any useful information. */
1061 && (limit_vr
->type
== VR_UNDEFINED
1062 || limit_vr
->type
== VR_VARYING
1063 || symbolic_range_p (limit_vr
)))
1066 /* Initially, the new range has the same set of equivalences of
1067 VAR's range. This will be revised before returning the final
1068 value. Since assertions may be chained via mutually exclusive
1069 predicates, we will need to trim the set of equivalences before
1071 gcc_assert (vr_p
->equiv
== NULL
);
1072 vr_p
->equiv
= BITMAP_ALLOC (NULL
);
1073 add_equivalence (vr_p
->equiv
, var
);
1075 /* Extract a new range based on the asserted comparison for VAR and
1076 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1077 will only use it for equality comparisons (EQ_EXPR). For any
1078 other kind of assertion, we cannot derive a range from LIMIT's
1079 anti-range that can be used to describe the new range. For
1080 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1081 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1082 no single range for x_2 that could describe LE_EXPR, so we might
1083 as well build the range [b_4, +INF] for it. */
1084 if (cond_code
== EQ_EXPR
)
1086 enum value_range_type range_type
;
1090 range_type
= limit_vr
->type
;
1091 min
= limit_vr
->min
;
1092 max
= limit_vr
->max
;
1096 range_type
= VR_RANGE
;
1101 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1103 /* When asserting the equality VAR == LIMIT and LIMIT is another
1104 SSA name, the new range will also inherit the equivalence set
1106 if (TREE_CODE (limit
) == SSA_NAME
)
1107 add_equivalence (vr_p
->equiv
, limit
);
1109 else if (cond_code
== NE_EXPR
)
1111 /* As described above, when LIMIT's range is an anti-range and
1112 this assertion is an inequality (NE_EXPR), then we cannot
1113 derive anything from the anti-range. For instance, if
1114 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1115 not imply that VAR's range is [0, 0]. So, in the case of
1116 anti-ranges, we just assert the inequality using LIMIT and
1119 If LIMIT_VR is a range, we can only use it to build a new
1120 anti-range if LIMIT_VR is a single-valued range. For
1121 instance, if LIMIT_VR is [0, 1], the predicate
1122 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1123 Rather, it means that for value 0 VAR should be ~[0, 0]
1124 and for value 1, VAR should be ~[1, 1]. We cannot
1125 represent these ranges.
1127 The only situation in which we can build a valid
1128 anti-range is when LIMIT_VR is a single-valued range
1129 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1130 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1132 && limit_vr
->type
== VR_RANGE
1133 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1135 min
= limit_vr
->min
;
1136 max
= limit_vr
->max
;
1140 /* In any other case, we cannot use LIMIT's range to build a
1141 valid anti-range. */
1145 /* If MIN and MAX cover the whole range for their type, then
1146 just use the original LIMIT. */
1147 if (INTEGRAL_TYPE_P (type
)
1148 && vrp_val_is_min (min
)
1149 && vrp_val_is_max (max
))
1152 set_value_range (vr_p
, VR_ANTI_RANGE
, min
, max
, vr_p
->equiv
);
1154 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1156 min
= TYPE_MIN_VALUE (type
);
1158 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1162 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1163 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1165 max
= limit_vr
->max
;
1168 /* If the maximum value forces us to be out of bounds, simply punt.
1169 It would be pointless to try and do anything more since this
1170 all should be optimized away above us. */
1171 if ((cond_code
== LT_EXPR
1172 && compare_values (max
, min
) == 0)
1173 || is_overflow_infinity (max
))
1174 set_value_range_to_varying (vr_p
);
1177 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1178 if (cond_code
== LT_EXPR
)
1180 tree one
= build_int_cst (type
, 1);
1181 max
= fold_build2 (MINUS_EXPR
, type
, max
, one
);
1183 TREE_NO_WARNING (max
) = 1;
1186 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1189 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1191 max
= TYPE_MAX_VALUE (type
);
1193 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1197 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1198 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1200 min
= limit_vr
->min
;
1203 /* If the minimum value forces us to be out of bounds, simply punt.
1204 It would be pointless to try and do anything more since this
1205 all should be optimized away above us. */
1206 if ((cond_code
== GT_EXPR
1207 && compare_values (min
, max
) == 0)
1208 || is_overflow_infinity (min
))
1209 set_value_range_to_varying (vr_p
);
1212 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1213 if (cond_code
== GT_EXPR
)
1215 tree one
= build_int_cst (type
, 1);
1216 min
= fold_build2 (PLUS_EXPR
, type
, min
, one
);
1218 TREE_NO_WARNING (min
) = 1;
1221 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1227 /* If VAR already had a known range, it may happen that the new
1228 range we have computed and VAR's range are not compatible. For
1232 p_6 = ASSERT_EXPR <p_5, p_5 == NULL>;
1234 p_8 = ASSERT_EXPR <p_6, p_6 != NULL>;
1236 While the above comes from a faulty program, it will cause an ICE
1237 later because p_8 and p_6 will have incompatible ranges and at
1238 the same time will be considered equivalent. A similar situation
1242 i_6 = ASSERT_EXPR <i_5, i_5 > 10>;
1244 i_7 = ASSERT_EXPR <i_6, i_6 < 5>;
1246 Again i_6 and i_7 will have incompatible ranges. It would be
1247 pointless to try and do anything with i_7's range because
1248 anything dominated by 'if (i_5 < 5)' will be optimized away.
1249 Note, due to the wa in which simulation proceeds, the statement
1250 i_7 = ASSERT_EXPR <...> we would never be visited because the
1251 conditional 'if (i_5 < 5)' always evaluates to false. However,
1252 this extra check does not hurt and may protect against future
1253 changes to VRP that may get into a situation similar to the
1254 NULL pointer dereference example.
1256 Note that these compatibility tests are only needed when dealing
1257 with ranges or a mix of range and anti-range. If VAR_VR and VR_P
1258 are both anti-ranges, they will always be compatible, because two
1259 anti-ranges will always have a non-empty intersection. */
1261 var_vr
= get_value_range (var
);
1263 /* We may need to make adjustments when VR_P and VAR_VR are numeric
1264 ranges or anti-ranges. */
1265 if (vr_p
->type
== VR_VARYING
1266 || vr_p
->type
== VR_UNDEFINED
1267 || var_vr
->type
== VR_VARYING
1268 || var_vr
->type
== VR_UNDEFINED
1269 || symbolic_range_p (vr_p
)
1270 || symbolic_range_p (var_vr
))
1273 if (var_vr
->type
== VR_RANGE
&& vr_p
->type
== VR_RANGE
)
1275 /* If the two ranges have a non-empty intersection, we can
1276 refine the resulting range. Since the assert expression
1277 creates an equivalency and at the same time it asserts a
1278 predicate, we can take the intersection of the two ranges to
1279 get better precision. */
1280 if (value_ranges_intersect_p (var_vr
, vr_p
))
1282 /* Use the larger of the two minimums. */
1283 if (compare_values (vr_p
->min
, var_vr
->min
) == -1)
1288 /* Use the smaller of the two maximums. */
1289 if (compare_values (vr_p
->max
, var_vr
->max
) == 1)
1294 set_value_range (vr_p
, vr_p
->type
, min
, max
, vr_p
->equiv
);
1298 /* The two ranges do not intersect, set the new range to
1299 VARYING, because we will not be able to do anything
1300 meaningful with it. */
1301 set_value_range_to_varying (vr_p
);
1304 else if ((var_vr
->type
== VR_RANGE
&& vr_p
->type
== VR_ANTI_RANGE
)
1305 || (var_vr
->type
== VR_ANTI_RANGE
&& vr_p
->type
== VR_RANGE
))
1307 /* A range and an anti-range will cancel each other only if
1308 their ends are the same. For instance, in the example above,
1309 p_8's range ~[0, 0] and p_6's range [0, 0] are incompatible,
1310 so VR_P should be set to VR_VARYING. */
1311 if (compare_values (var_vr
->min
, vr_p
->min
) == 0
1312 && compare_values (var_vr
->max
, vr_p
->max
) == 0)
1313 set_value_range_to_varying (vr_p
);
1316 tree min
, max
, anti_min
, anti_max
, real_min
, real_max
;
1318 /* We want to compute the logical AND of the two ranges;
1319 there are three cases to consider.
1322 1. The VR_ANTI_RANGE range is completely within the
1323 VR_RANGE and the endpoints of the ranges are
1324 different. In that case the resulting range
1325 should be whichever range is more precise.
1326 Typically that will be the VR_RANGE.
1328 2. The VR_ANTI_RANGE is completely disjoint from
1329 the VR_RANGE. In this case the resulting range
1330 should be the VR_RANGE.
1332 3. There is some overlap between the VR_ANTI_RANGE
1335 3a. If the high limit of the VR_ANTI_RANGE resides
1336 within the VR_RANGE, then the result is a new
1337 VR_RANGE starting at the high limit of the
1338 the VR_ANTI_RANGE + 1 and extending to the
1339 high limit of the original VR_RANGE.
1341 3b. If the low limit of the VR_ANTI_RANGE resides
1342 within the VR_RANGE, then the result is a new
1343 VR_RANGE starting at the low limit of the original
1344 VR_RANGE and extending to the low limit of the
1345 VR_ANTI_RANGE - 1. */
1346 if (vr_p
->type
== VR_ANTI_RANGE
)
1348 anti_min
= vr_p
->min
;
1349 anti_max
= vr_p
->max
;
1350 real_min
= var_vr
->min
;
1351 real_max
= var_vr
->max
;
1355 anti_min
= var_vr
->min
;
1356 anti_max
= var_vr
->max
;
1357 real_min
= vr_p
->min
;
1358 real_max
= vr_p
->max
;
1362 /* Case 1, VR_ANTI_RANGE completely within VR_RANGE,
1363 not including any endpoints. */
1364 if (compare_values (anti_max
, real_max
) == -1
1365 && compare_values (anti_min
, real_min
) == 1)
1367 set_value_range (vr_p
, VR_RANGE
, real_min
,
1368 real_max
, vr_p
->equiv
);
1370 /* Case 2, VR_ANTI_RANGE completely disjoint from
1372 else if (compare_values (anti_min
, real_max
) == 1
1373 || compare_values (anti_max
, real_min
) == -1)
1375 set_value_range (vr_p
, VR_RANGE
, real_min
,
1376 real_max
, vr_p
->equiv
);
1378 /* Case 3a, the anti-range extends into the low
1379 part of the real range. Thus creating a new
1380 low for the real range. */
1381 else if ((compare_values (anti_max
, real_min
) == 1
1382 || compare_values (anti_max
, real_min
) == 0)
1383 && compare_values (anti_max
, real_max
) == -1)
1385 gcc_assert (!is_positive_overflow_infinity (anti_max
));
1386 if (needs_overflow_infinity (TREE_TYPE (anti_max
))
1387 && vrp_val_is_max (anti_max
))
1389 if (!supports_overflow_infinity (TREE_TYPE (var_vr
->min
)))
1391 set_value_range_to_varying (vr_p
);
1394 min
= positive_overflow_infinity (TREE_TYPE (var_vr
->min
));
1397 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (var_vr
->min
),
1399 build_int_cst (TREE_TYPE (var_vr
->min
), 1));
1401 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1403 /* Case 3b, the anti-range extends into the high
1404 part of the real range. Thus creating a new
1405 higher for the real range. */
1406 else if (compare_values (anti_min
, real_min
) == 1
1407 && (compare_values (anti_min
, real_max
) == -1
1408 || compare_values (anti_min
, real_max
) == 0))
1410 gcc_assert (!is_negative_overflow_infinity (anti_min
));
1411 if (needs_overflow_infinity (TREE_TYPE (anti_min
))
1412 && vrp_val_is_min (anti_min
))
1414 if (!supports_overflow_infinity (TREE_TYPE (var_vr
->min
)))
1416 set_value_range_to_varying (vr_p
);
1419 max
= negative_overflow_infinity (TREE_TYPE (var_vr
->min
));
1422 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (var_vr
->min
),
1424 build_int_cst (TREE_TYPE (var_vr
->min
), 1));
1426 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1433 /* Extract range information from SSA name VAR and store it in VR. If
1434 VAR has an interesting range, use it. Otherwise, create the
1435 range [VAR, VAR] and return it. This is useful in situations where
1436 we may have conditionals testing values of VARYING names. For
1443 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1447 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1449 value_range_t
*var_vr
= get_value_range (var
);
1451 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1452 copy_value_range (vr
, var_vr
);
1454 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1456 add_equivalence (vr
->equiv
, var
);
1460 /* Wrapper around int_const_binop. If the operation overflows and we
1461 are not using wrapping arithmetic, then adjust the result to be
1462 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1463 NULL_TREE if we need to use an overflow infinity representation but
1464 the type does not support it. */
1467 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1471 res
= int_const_binop (code
, val1
, val2
, 0);
1473 /* If we are not using wrapping arithmetic, operate symbolically
1474 on -INF and +INF. */
1475 if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1477 int checkz
= compare_values (res
, val1
);
1478 bool overflow
= false;
1480 /* Ensure that res = val1 [+*] val2 >= val1
1481 or that res = val1 - val2 <= val1. */
1482 if ((code
== PLUS_EXPR
1483 && !(checkz
== 1 || checkz
== 0))
1484 || (code
== MINUS_EXPR
1485 && !(checkz
== 0 || checkz
== -1)))
1489 /* Checking for multiplication overflow is done by dividing the
1490 output of the multiplication by the first input of the
1491 multiplication. If the result of that division operation is
1492 not equal to the second input of the multiplication, then the
1493 multiplication overflowed. */
1494 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1496 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1499 int check
= compare_values (tmp
, val2
);
1507 res
= copy_node (res
);
1508 TREE_OVERFLOW (res
) = 1;
1512 else if ((TREE_OVERFLOW (res
)
1513 && !TREE_OVERFLOW (val1
)
1514 && !TREE_OVERFLOW (val2
))
1515 || is_overflow_infinity (val1
)
1516 || is_overflow_infinity (val2
))
1518 /* If the operation overflowed but neither VAL1 nor VAL2 are
1519 overflown, return -INF or +INF depending on the operation
1520 and the combination of signs of the operands. */
1521 int sgn1
= tree_int_cst_sgn (val1
);
1522 int sgn2
= tree_int_cst_sgn (val2
);
1524 if (needs_overflow_infinity (TREE_TYPE (res
))
1525 && !supports_overflow_infinity (TREE_TYPE (res
)))
1528 /* We have to punt on adding infinities of different signs,
1529 since we can't tell what the sign of the result should be.
1530 Likewise for subtracting infinities of the same sign. */
1531 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1532 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1533 && is_overflow_infinity (val1
)
1534 && is_overflow_infinity (val2
))
1537 /* Don't try to handle division or shifting of infinities. */
1538 if ((code
== TRUNC_DIV_EXPR
1539 || code
== FLOOR_DIV_EXPR
1540 || code
== CEIL_DIV_EXPR
1541 || code
== EXACT_DIV_EXPR
1542 || code
== ROUND_DIV_EXPR
1543 || code
== RSHIFT_EXPR
)
1544 && (is_overflow_infinity (val1
)
1545 || is_overflow_infinity (val2
)))
1548 /* Notice that we only need to handle the restricted set of
1549 operations handled by extract_range_from_binary_expr.
1550 Among them, only multiplication, addition and subtraction
1551 can yield overflow without overflown operands because we
1552 are working with integral types only... except in the
1553 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1554 for division too. */
1556 /* For multiplication, the sign of the overflow is given
1557 by the comparison of the signs of the operands. */
1558 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1559 /* For addition, the operands must be of the same sign
1560 to yield an overflow. Its sign is therefore that
1561 of one of the operands, for example the first. For
1562 infinite operands X + -INF is negative, not positive. */
1563 || (code
== PLUS_EXPR
1565 ? !is_negative_overflow_infinity (val2
)
1566 : is_positive_overflow_infinity (val2
)))
1567 /* For subtraction, non-infinite operands must be of
1568 different signs to yield an overflow. Its sign is
1569 therefore that of the first operand or the opposite of
1570 that of the second operand. A first operand of 0 counts
1571 as positive here, for the corner case 0 - (-INF), which
1572 overflows, but must yield +INF. For infinite operands 0
1573 - INF is negative, not positive. */
1574 || (code
== MINUS_EXPR
1576 ? !is_positive_overflow_infinity (val2
)
1577 : is_negative_overflow_infinity (val2
)))
1578 /* For division, the only case is -INF / -1 = +INF. */
1579 || code
== TRUNC_DIV_EXPR
1580 || code
== FLOOR_DIV_EXPR
1581 || code
== CEIL_DIV_EXPR
1582 || code
== EXACT_DIV_EXPR
1583 || code
== ROUND_DIV_EXPR
)
1584 return (needs_overflow_infinity (TREE_TYPE (res
))
1585 ? positive_overflow_infinity (TREE_TYPE (res
))
1586 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1588 return (needs_overflow_infinity (TREE_TYPE (res
))
1589 ? negative_overflow_infinity (TREE_TYPE (res
))
1590 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1597 /* Extract range information from a binary expression EXPR based on
1598 the ranges of each of its operands and the expression code. */
1601 extract_range_from_binary_expr (value_range_t
*vr
, tree expr
)
1603 enum tree_code code
= TREE_CODE (expr
);
1604 enum value_range_type type
;
1605 tree op0
, op1
, min
, max
;
1607 value_range_t vr0
= { VR_UNDEFINED
, NULL_TREE
, NULL_TREE
, NULL
};
1608 value_range_t vr1
= { VR_UNDEFINED
, NULL_TREE
, NULL_TREE
, NULL
};
1610 /* Not all binary expressions can be applied to ranges in a
1611 meaningful way. Handle only arithmetic operations. */
1612 if (code
!= PLUS_EXPR
1613 && code
!= MINUS_EXPR
1614 && code
!= MULT_EXPR
1615 && code
!= TRUNC_DIV_EXPR
1616 && code
!= FLOOR_DIV_EXPR
1617 && code
!= CEIL_DIV_EXPR
1618 && code
!= EXACT_DIV_EXPR
1619 && code
!= ROUND_DIV_EXPR
1622 && code
!= BIT_AND_EXPR
1623 && code
!= TRUTH_ANDIF_EXPR
1624 && code
!= TRUTH_ORIF_EXPR
1625 && code
!= TRUTH_AND_EXPR
1626 && code
!= TRUTH_OR_EXPR
)
1628 set_value_range_to_varying (vr
);
1632 /* Get value ranges for each operand. For constant operands, create
1633 a new value range with the operand to simplify processing. */
1634 op0
= TREE_OPERAND (expr
, 0);
1635 if (TREE_CODE (op0
) == SSA_NAME
)
1636 vr0
= *(get_value_range (op0
));
1637 else if (is_gimple_min_invariant (op0
))
1638 set_value_range_to_value (&vr0
, op0
, NULL
);
1640 set_value_range_to_varying (&vr0
);
1642 op1
= TREE_OPERAND (expr
, 1);
1643 if (TREE_CODE (op1
) == SSA_NAME
)
1644 vr1
= *(get_value_range (op1
));
1645 else if (is_gimple_min_invariant (op1
))
1646 set_value_range_to_value (&vr1
, op1
, NULL
);
1648 set_value_range_to_varying (&vr1
);
1650 /* If either range is UNDEFINED, so is the result. */
1651 if (vr0
.type
== VR_UNDEFINED
|| vr1
.type
== VR_UNDEFINED
)
1653 set_value_range_to_undefined (vr
);
1657 /* The type of the resulting value range defaults to VR0.TYPE. */
1660 /* Refuse to operate on VARYING ranges, ranges of different kinds
1661 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
1662 because we may be able to derive a useful range even if one of
1663 the operands is VR_VARYING or symbolic range. TODO, we may be
1664 able to derive anti-ranges in some cases. */
1665 if (code
!= BIT_AND_EXPR
1666 && code
!= TRUTH_AND_EXPR
1667 && code
!= TRUTH_OR_EXPR
1668 && (vr0
.type
== VR_VARYING
1669 || vr1
.type
== VR_VARYING
1670 || vr0
.type
!= vr1
.type
1671 || symbolic_range_p (&vr0
)
1672 || symbolic_range_p (&vr1
)))
1674 set_value_range_to_varying (vr
);
1678 /* Now evaluate the expression to determine the new range. */
1679 if (POINTER_TYPE_P (TREE_TYPE (expr
))
1680 || POINTER_TYPE_P (TREE_TYPE (op0
))
1681 || POINTER_TYPE_P (TREE_TYPE (op1
)))
1683 /* For pointer types, we are really only interested in asserting
1684 whether the expression evaluates to non-NULL. FIXME, we used
1685 to gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR), but
1686 ivopts is generating expressions with pointer multiplication
1688 if (code
== PLUS_EXPR
)
1690 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
1691 set_value_range_to_nonnull (vr
, TREE_TYPE (expr
));
1692 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
1693 set_value_range_to_null (vr
, TREE_TYPE (expr
));
1695 set_value_range_to_varying (vr
);
1699 /* Subtracting from a pointer, may yield 0, so just drop the
1700 resulting range to varying. */
1701 set_value_range_to_varying (vr
);
1707 /* For integer ranges, apply the operation to each end of the
1708 range and see what we end up with. */
1709 if (code
== TRUTH_ANDIF_EXPR
1710 || code
== TRUTH_ORIF_EXPR
1711 || code
== TRUTH_AND_EXPR
1712 || code
== TRUTH_OR_EXPR
)
1714 /* If one of the operands is zero, we know that the whole
1715 expression evaluates zero. */
1716 if (code
== TRUTH_AND_EXPR
1717 && ((vr0
.type
== VR_RANGE
1718 && integer_zerop (vr0
.min
)
1719 && integer_zerop (vr0
.max
))
1720 || (vr1
.type
== VR_RANGE
1721 && integer_zerop (vr1
.min
)
1722 && integer_zerop (vr1
.max
))))
1725 min
= max
= build_int_cst (TREE_TYPE (expr
), 0);
1727 /* If one of the operands is one, we know that the whole
1728 expression evaluates one. */
1729 else if (code
== TRUTH_OR_EXPR
1730 && ((vr0
.type
== VR_RANGE
1731 && integer_onep (vr0
.min
)
1732 && integer_onep (vr0
.max
))
1733 || (vr1
.type
== VR_RANGE
1734 && integer_onep (vr1
.min
)
1735 && integer_onep (vr1
.max
))))
1738 min
= max
= build_int_cst (TREE_TYPE (expr
), 1);
1740 else if (vr0
.type
!= VR_VARYING
1741 && vr1
.type
!= VR_VARYING
1742 && vr0
.type
== vr1
.type
1743 && !symbolic_range_p (&vr0
)
1744 && !overflow_infinity_range_p (&vr0
)
1745 && !symbolic_range_p (&vr1
)
1746 && !overflow_infinity_range_p (&vr1
))
1748 /* Boolean expressions cannot be folded with int_const_binop. */
1749 min
= fold_binary (code
, TREE_TYPE (expr
), vr0
.min
, vr1
.min
);
1750 max
= fold_binary (code
, TREE_TYPE (expr
), vr0
.max
, vr1
.max
);
1754 set_value_range_to_varying (vr
);
1758 else if (code
== PLUS_EXPR
1760 || code
== MAX_EXPR
)
1762 /* If we have a PLUS_EXPR with two VR_ANTI_RANGEs, drop to
1763 VR_VARYING. It would take more effort to compute a precise
1764 range for such a case. For example, if we have op0 == 1 and
1765 op1 == -1 with their ranges both being ~[0,0], we would have
1766 op0 + op1 == 0, so we cannot claim that the sum is in ~[0,0].
1767 Note that we are guaranteed to have vr0.type == vr1.type at
1769 if (code
== PLUS_EXPR
&& vr0
.type
== VR_ANTI_RANGE
)
1771 set_value_range_to_varying (vr
);
1775 /* For operations that make the resulting range directly
1776 proportional to the original ranges, apply the operation to
1777 the same end of each range. */
1778 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
1779 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
1781 else if (code
== MULT_EXPR
1782 || code
== TRUNC_DIV_EXPR
1783 || code
== FLOOR_DIV_EXPR
1784 || code
== CEIL_DIV_EXPR
1785 || code
== EXACT_DIV_EXPR
1786 || code
== ROUND_DIV_EXPR
)
1792 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
1793 drop to VR_VARYING. It would take more effort to compute a
1794 precise range for such a case. For example, if we have
1795 op0 == 65536 and op1 == 65536 with their ranges both being
1796 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
1797 we cannot claim that the product is in ~[0,0]. Note that we
1798 are guaranteed to have vr0.type == vr1.type at this
1800 if (code
== MULT_EXPR
1801 && vr0
.type
== VR_ANTI_RANGE
1802 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
1804 set_value_range_to_varying (vr
);
1808 /* Multiplications and divisions are a bit tricky to handle,
1809 depending on the mix of signs we have in the two ranges, we
1810 need to operate on different values to get the minimum and
1811 maximum values for the new range. One approach is to figure
1812 out all the variations of range combinations and do the
1815 However, this involves several calls to compare_values and it
1816 is pretty convoluted. It's simpler to do the 4 operations
1817 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1818 MAX1) and then figure the smallest and largest values to form
1821 /* Divisions by zero result in a VARYING value. */
1822 if (code
!= MULT_EXPR
1823 && (vr0
.type
== VR_ANTI_RANGE
|| range_includes_zero_p (&vr1
)))
1825 set_value_range_to_varying (vr
);
1829 /* Compute the 4 cross operations. */
1831 val
[0] = vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
1832 if (val
[0] == NULL_TREE
)
1835 if (vr1
.max
== vr1
.min
)
1839 val
[1] = vrp_int_const_binop (code
, vr0
.min
, vr1
.max
);
1840 if (val
[1] == NULL_TREE
)
1844 if (vr0
.max
== vr0
.min
)
1848 val
[2] = vrp_int_const_binop (code
, vr0
.max
, vr1
.min
);
1849 if (val
[2] == NULL_TREE
)
1853 if (vr0
.min
== vr0
.max
|| vr1
.min
== vr1
.max
)
1857 val
[3] = vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
1858 if (val
[3] == NULL_TREE
)
1864 set_value_range_to_varying (vr
);
1868 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1872 for (i
= 1; i
< 4; i
++)
1874 if (!is_gimple_min_invariant (min
)
1875 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
1876 || !is_gimple_min_invariant (max
)
1877 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
1882 if (!is_gimple_min_invariant (val
[i
])
1883 || (TREE_OVERFLOW (val
[i
])
1884 && !is_overflow_infinity (val
[i
])))
1886 /* If we found an overflowed value, set MIN and MAX
1887 to it so that we set the resulting range to
1893 if (compare_values (val
[i
], min
) == -1)
1896 if (compare_values (val
[i
], max
) == 1)
1901 else if (code
== MINUS_EXPR
)
1903 /* If we have a MINUS_EXPR with two VR_ANTI_RANGEs, drop to
1904 VR_VARYING. It would take more effort to compute a precise
1905 range for such a case. For example, if we have op0 == 1 and
1906 op1 == 1 with their ranges both being ~[0,0], we would have
1907 op0 - op1 == 0, so we cannot claim that the difference is in
1908 ~[0,0]. Note that we are guaranteed to have
1909 vr0.type == vr1.type at this point. */
1910 if (vr0
.type
== VR_ANTI_RANGE
)
1912 set_value_range_to_varying (vr
);
1916 /* For MINUS_EXPR, apply the operation to the opposite ends of
1918 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.max
);
1919 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.min
);
1921 else if (code
== BIT_AND_EXPR
)
1923 if (vr0
.type
== VR_RANGE
1924 && vr0
.min
== vr0
.max
1925 && TREE_CODE (vr0
.max
) == INTEGER_CST
1926 && !TREE_OVERFLOW (vr0
.max
)
1927 && tree_int_cst_sgn (vr0
.max
) >= 0)
1929 min
= build_int_cst (TREE_TYPE (expr
), 0);
1932 else if (vr1
.type
== VR_RANGE
1933 && vr1
.min
== vr1
.max
1934 && TREE_CODE (vr1
.max
) == INTEGER_CST
1935 && !TREE_OVERFLOW (vr1
.max
)
1936 && tree_int_cst_sgn (vr1
.max
) >= 0)
1939 min
= build_int_cst (TREE_TYPE (expr
), 0);
1944 set_value_range_to_varying (vr
);
1951 /* If either MIN or MAX overflowed, then set the resulting range to
1952 VARYING. But we do accept an overflow infinity
1954 if (min
== NULL_TREE
1955 || !is_gimple_min_invariant (min
)
1956 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
1958 || !is_gimple_min_invariant (max
)
1959 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
1961 set_value_range_to_varying (vr
);
1967 2) [-INF, +-INF(OVF)]
1968 3) [+-INF(OVF), +INF]
1969 4) [+-INF(OVF), +-INF(OVF)]
1970 We learn nothing when we have INF and INF(OVF) on both sides.
1971 Note that we do accept [-INF, -INF] and [+INF, +INF] without
1973 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
1974 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
1976 set_value_range_to_varying (vr
);
1980 cmp
= compare_values (min
, max
);
1981 if (cmp
== -2 || cmp
== 1)
1983 /* If the new range has its limits swapped around (MIN > MAX),
1984 then the operation caused one of them to wrap around, mark
1985 the new range VARYING. */
1986 set_value_range_to_varying (vr
);
1989 set_value_range (vr
, type
, min
, max
, NULL
);
1993 /* Extract range information from a unary expression EXPR based on
1994 the range of its operand and the expression code. */
1997 extract_range_from_unary_expr (value_range_t
*vr
, tree expr
)
1999 enum tree_code code
= TREE_CODE (expr
);
2002 value_range_t vr0
= { VR_UNDEFINED
, NULL_TREE
, NULL_TREE
, NULL
};
2004 /* Refuse to operate on certain unary expressions for which we
2005 cannot easily determine a resulting range. */
2006 if (code
== FIX_TRUNC_EXPR
2007 || code
== FIX_CEIL_EXPR
2008 || code
== FIX_FLOOR_EXPR
2009 || code
== FIX_ROUND_EXPR
2010 || code
== FLOAT_EXPR
2011 || code
== BIT_NOT_EXPR
2012 || code
== NON_LVALUE_EXPR
2013 || code
== CONJ_EXPR
)
2015 set_value_range_to_varying (vr
);
2019 /* Get value ranges for the operand. For constant operands, create
2020 a new value range with the operand to simplify processing. */
2021 op0
= TREE_OPERAND (expr
, 0);
2022 if (TREE_CODE (op0
) == SSA_NAME
)
2023 vr0
= *(get_value_range (op0
));
2024 else if (is_gimple_min_invariant (op0
))
2025 set_value_range_to_value (&vr0
, op0
, NULL
);
2027 set_value_range_to_varying (&vr0
);
2029 /* If VR0 is UNDEFINED, so is the result. */
2030 if (vr0
.type
== VR_UNDEFINED
)
2032 set_value_range_to_undefined (vr
);
2036 /* Refuse to operate on symbolic ranges, or if neither operand is
2037 a pointer or integral type. */
2038 if ((!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
2039 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
2040 || (vr0
.type
!= VR_VARYING
2041 && symbolic_range_p (&vr0
)))
2043 set_value_range_to_varying (vr
);
2047 /* If the expression involves pointers, we are only interested in
2048 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
2049 if (POINTER_TYPE_P (TREE_TYPE (expr
)) || POINTER_TYPE_P (TREE_TYPE (op0
)))
2054 if (range_is_nonnull (&vr0
)
2055 || (tree_expr_nonzero_warnv_p (expr
, &sop
)
2057 set_value_range_to_nonnull (vr
, TREE_TYPE (expr
));
2058 else if (range_is_null (&vr0
))
2059 set_value_range_to_null (vr
, TREE_TYPE (expr
));
2061 set_value_range_to_varying (vr
);
2066 /* Handle unary expressions on integer ranges. */
2067 if (code
== NOP_EXPR
|| code
== CONVERT_EXPR
)
2069 tree inner_type
= TREE_TYPE (op0
);
2070 tree outer_type
= TREE_TYPE (expr
);
2072 /* If VR0 represents a simple range, then try to convert
2073 the min and max values for the range to the same type
2074 as OUTER_TYPE. If the results compare equal to VR0's
2075 min and max values and the new min is still less than
2076 or equal to the new max, then we can safely use the newly
2077 computed range for EXPR. This allows us to compute
2078 accurate ranges through many casts. */
2079 if ((vr0
.type
== VR_RANGE
2080 && !overflow_infinity_range_p (&vr0
))
2081 || (vr0
.type
== VR_VARYING
2082 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)))
2084 tree new_min
, new_max
, orig_min
, orig_max
;
2086 /* Convert the input operand min/max to OUTER_TYPE. If
2087 the input has no range information, then use the min/max
2088 for the input's type. */
2089 if (vr0
.type
== VR_RANGE
)
2096 orig_min
= TYPE_MIN_VALUE (inner_type
);
2097 orig_max
= TYPE_MAX_VALUE (inner_type
);
2100 new_min
= fold_convert (outer_type
, orig_min
);
2101 new_max
= fold_convert (outer_type
, orig_max
);
2103 /* Verify the new min/max values are gimple values and
2104 that they compare equal to the original input's
2106 if (is_gimple_val (new_min
)
2107 && is_gimple_val (new_max
)
2108 && tree_int_cst_equal (new_min
, orig_min
)
2109 && tree_int_cst_equal (new_max
, orig_max
)
2110 && (!is_overflow_infinity (new_min
)
2111 || !is_overflow_infinity (new_max
))
2112 && compare_values (new_min
, new_max
) <= 0
2113 && compare_values (new_min
, new_max
) >= -1)
2115 set_value_range (vr
, VR_RANGE
, new_min
, new_max
, vr
->equiv
);
2120 /* When converting types of different sizes, set the result to
2121 VARYING. Things like sign extensions and precision loss may
2122 change the range. For instance, if x_3 is of type 'long long
2123 int' and 'y_5 = (unsigned short) x_3', if x_3 is ~[0, 0], it
2124 is impossible to know at compile time whether y_5 will be
2126 if (TYPE_SIZE (inner_type
) != TYPE_SIZE (outer_type
)
2127 || TYPE_PRECISION (inner_type
) != TYPE_PRECISION (outer_type
))
2129 set_value_range_to_varying (vr
);
2134 /* Conversion of a VR_VARYING value to a wider type can result
2135 in a usable range. So wait until after we've handled conversions
2136 before dropping the result to VR_VARYING if we had a source
2137 operand that is VR_VARYING. */
2138 if (vr0
.type
== VR_VARYING
)
2140 set_value_range_to_varying (vr
);
2144 /* Apply the operation to each end of the range and see what we end
2146 if (code
== NEGATE_EXPR
2147 && !TYPE_UNSIGNED (TREE_TYPE (expr
)))
2149 /* NEGATE_EXPR flips the range around. We need to treat
2150 TYPE_MIN_VALUE specially. */
2151 if (is_positive_overflow_infinity (vr0
.max
))
2152 min
= negative_overflow_infinity (TREE_TYPE (expr
));
2153 else if (is_negative_overflow_infinity (vr0
.max
))
2154 min
= positive_overflow_infinity (TREE_TYPE (expr
));
2155 else if (!vrp_val_is_min (vr0
.max
))
2156 min
= fold_unary_to_constant (code
, TREE_TYPE (expr
), vr0
.max
);
2157 else if (needs_overflow_infinity (TREE_TYPE (expr
)))
2159 if (supports_overflow_infinity (TREE_TYPE (expr
))
2160 && !is_overflow_infinity (vr0
.min
)
2161 && !vrp_val_is_min (vr0
.min
))
2162 min
= positive_overflow_infinity (TREE_TYPE (expr
));
2165 set_value_range_to_varying (vr
);
2170 min
= TYPE_MIN_VALUE (TREE_TYPE (expr
));
2172 if (is_positive_overflow_infinity (vr0
.min
))
2173 max
= negative_overflow_infinity (TREE_TYPE (expr
));
2174 else if (is_negative_overflow_infinity (vr0
.min
))
2175 max
= positive_overflow_infinity (TREE_TYPE (expr
));
2176 else if (!vrp_val_is_min (vr0
.min
))
2177 max
= fold_unary_to_constant (code
, TREE_TYPE (expr
), vr0
.min
);
2178 else if (needs_overflow_infinity (TREE_TYPE (expr
)))
2180 if (supports_overflow_infinity (TREE_TYPE (expr
)))
2181 max
= positive_overflow_infinity (TREE_TYPE (expr
));
2184 set_value_range_to_varying (vr
);
2189 max
= TYPE_MIN_VALUE (TREE_TYPE (expr
));
2191 else if (code
== NEGATE_EXPR
2192 && TYPE_UNSIGNED (TREE_TYPE (expr
)))
2194 if (!range_includes_zero_p (&vr0
))
2196 max
= fold_unary_to_constant (code
, TREE_TYPE (expr
), vr0
.min
);
2197 min
= fold_unary_to_constant (code
, TREE_TYPE (expr
), vr0
.max
);
2201 if (range_is_null (&vr0
))
2202 set_value_range_to_null (vr
, TREE_TYPE (expr
));
2204 set_value_range_to_varying (vr
);
2208 else if (code
== ABS_EXPR
2209 && !TYPE_UNSIGNED (TREE_TYPE (expr
)))
2211 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
2213 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (expr
))
2214 && ((vr0
.type
== VR_RANGE
2215 && vrp_val_is_min (vr0
.min
))
2216 || (vr0
.type
== VR_ANTI_RANGE
2217 && !vrp_val_is_min (vr0
.min
)
2218 && !range_includes_zero_p (&vr0
))))
2220 set_value_range_to_varying (vr
);
2224 /* ABS_EXPR may flip the range around, if the original range
2225 included negative values. */
2226 if (is_overflow_infinity (vr0
.min
))
2227 min
= positive_overflow_infinity (TREE_TYPE (expr
));
2228 else if (!vrp_val_is_min (vr0
.min
))
2229 min
= fold_unary_to_constant (code
, TREE_TYPE (expr
), vr0
.min
);
2230 else if (!needs_overflow_infinity (TREE_TYPE (expr
)))
2231 min
= TYPE_MAX_VALUE (TREE_TYPE (expr
));
2232 else if (supports_overflow_infinity (TREE_TYPE (expr
)))
2233 min
= positive_overflow_infinity (TREE_TYPE (expr
));
2236 set_value_range_to_varying (vr
);
2240 if (is_overflow_infinity (vr0
.max
))
2241 max
= positive_overflow_infinity (TREE_TYPE (expr
));
2242 else if (!vrp_val_is_min (vr0
.max
))
2243 max
= fold_unary_to_constant (code
, TREE_TYPE (expr
), vr0
.max
);
2244 else if (!needs_overflow_infinity (TREE_TYPE (expr
)))
2245 max
= TYPE_MAX_VALUE (TREE_TYPE (expr
));
2246 else if (supports_overflow_infinity (TREE_TYPE (expr
)))
2247 max
= positive_overflow_infinity (TREE_TYPE (expr
));
2250 set_value_range_to_varying (vr
);
2254 cmp
= compare_values (min
, max
);
2256 /* If a VR_ANTI_RANGEs contains zero, then we have
2257 ~[-INF, min(MIN, MAX)]. */
2258 if (vr0
.type
== VR_ANTI_RANGE
)
2260 if (range_includes_zero_p (&vr0
))
2262 /* Take the lower of the two values. */
2266 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
2267 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
2268 flag_wrapv is set and the original anti-range doesn't include
2269 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
2270 if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
)))
2272 tree type_min_value
= TYPE_MIN_VALUE (TREE_TYPE (expr
));
2274 min
= (vr0
.min
!= type_min_value
2275 ? int_const_binop (PLUS_EXPR
, type_min_value
,
2276 integer_one_node
, 0)
2281 if (overflow_infinity_range_p (&vr0
))
2282 min
= negative_overflow_infinity (TREE_TYPE (expr
));
2284 min
= TYPE_MIN_VALUE (TREE_TYPE (expr
));
2289 /* All else has failed, so create the range [0, INF], even for
2290 flag_wrapv since TYPE_MIN_VALUE is in the original
2292 vr0
.type
= VR_RANGE
;
2293 min
= build_int_cst (TREE_TYPE (expr
), 0);
2294 if (needs_overflow_infinity (TREE_TYPE (expr
)))
2296 if (supports_overflow_infinity (TREE_TYPE (expr
)))
2297 max
= positive_overflow_infinity (TREE_TYPE (expr
));
2300 set_value_range_to_varying (vr
);
2305 max
= TYPE_MAX_VALUE (TREE_TYPE (expr
));
2309 /* If the range contains zero then we know that the minimum value in the
2310 range will be zero. */
2311 else if (range_includes_zero_p (&vr0
))
2315 min
= build_int_cst (TREE_TYPE (expr
), 0);
2319 /* If the range was reversed, swap MIN and MAX. */
2330 /* Otherwise, operate on each end of the range. */
2331 min
= fold_unary_to_constant (code
, TREE_TYPE (expr
), vr0
.min
);
2332 max
= fold_unary_to_constant (code
, TREE_TYPE (expr
), vr0
.max
);
2334 if (needs_overflow_infinity (TREE_TYPE (expr
)))
2336 gcc_assert (code
!= NEGATE_EXPR
&& code
!= ABS_EXPR
);
2338 /* If both sides have overflowed, we don't know
2340 if ((is_overflow_infinity (vr0
.min
)
2341 || TREE_OVERFLOW (min
))
2342 && (is_overflow_infinity (vr0
.max
)
2343 || TREE_OVERFLOW (max
)))
2345 set_value_range_to_varying (vr
);
2349 if (is_overflow_infinity (vr0
.min
))
2351 else if (TREE_OVERFLOW (min
))
2353 if (supports_overflow_infinity (TREE_TYPE (expr
)))
2354 min
= (tree_int_cst_sgn (min
) >= 0
2355 ? positive_overflow_infinity (TREE_TYPE (min
))
2356 : negative_overflow_infinity (TREE_TYPE (min
)));
2359 set_value_range_to_varying (vr
);
2364 if (is_overflow_infinity (vr0
.max
))
2366 else if (TREE_OVERFLOW (max
))
2368 if (supports_overflow_infinity (TREE_TYPE (expr
)))
2369 max
= (tree_int_cst_sgn (max
) >= 0
2370 ? positive_overflow_infinity (TREE_TYPE (max
))
2371 : negative_overflow_infinity (TREE_TYPE (max
)));
2374 set_value_range_to_varying (vr
);
2381 cmp
= compare_values (min
, max
);
2382 if (cmp
== -2 || cmp
== 1)
2384 /* If the new range has its limits swapped around (MIN > MAX),
2385 then the operation caused one of them to wrap around, mark
2386 the new range VARYING. */
2387 set_value_range_to_varying (vr
);
2390 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
2394 /* Extract range information from a comparison expression EXPR based
2395 on the range of its operand and the expression code. */
2398 extract_range_from_comparison (value_range_t
*vr
, tree expr
)
2401 tree val
= vrp_evaluate_conditional_warnv (expr
, false, &sop
);
2403 /* A disadvantage of using a special infinity as an overflow
2404 representation is that we lose the ability to record overflow
2405 when we don't have an infinity. So we have to ignore a result
2406 which relies on overflow. */
2408 if (val
&& !is_overflow_infinity (val
) && !sop
)
2410 /* Since this expression was found on the RHS of an assignment,
2411 its type may be different from _Bool. Convert VAL to EXPR's
2413 val
= fold_convert (TREE_TYPE (expr
), val
);
2414 if (is_gimple_min_invariant (val
))
2415 set_value_range_to_value (vr
, val
, vr
->equiv
);
2417 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
2420 set_value_range_to_varying (vr
);
2424 /* Try to compute a useful range out of expression EXPR and store it
2428 extract_range_from_expr (value_range_t
*vr
, tree expr
)
2430 enum tree_code code
= TREE_CODE (expr
);
2432 if (code
== ASSERT_EXPR
)
2433 extract_range_from_assert (vr
, expr
);
2434 else if (code
== SSA_NAME
)
2435 extract_range_from_ssa_name (vr
, expr
);
2436 else if (TREE_CODE_CLASS (code
) == tcc_binary
2437 || code
== TRUTH_ANDIF_EXPR
2438 || code
== TRUTH_ORIF_EXPR
2439 || code
== TRUTH_AND_EXPR
2440 || code
== TRUTH_OR_EXPR
2441 || code
== TRUTH_XOR_EXPR
)
2442 extract_range_from_binary_expr (vr
, expr
);
2443 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
2444 extract_range_from_unary_expr (vr
, expr
);
2445 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
2446 extract_range_from_comparison (vr
, expr
);
2447 else if (is_gimple_min_invariant (expr
))
2448 set_value_range_to_value (vr
, expr
, NULL
);
2450 set_value_range_to_varying (vr
);
2452 /* If we got a varying range from the tests above, try a final
2453 time to derive a nonnegative or nonzero range. This time
2454 relying primarily on generic routines in fold in conjunction
2456 if (vr
->type
== VR_VARYING
)
2460 if (INTEGRAL_TYPE_P (TREE_TYPE (expr
))
2461 && vrp_expr_computes_nonnegative (expr
, &sop
))
2462 set_value_range_to_nonnegative (vr
, TREE_TYPE (expr
),
2463 sop
|| is_overflow_infinity (expr
));
2464 else if (vrp_expr_computes_nonzero (expr
, &sop
)
2466 set_value_range_to_nonnull (vr
, TREE_TYPE (expr
));
2470 /* Given a range VR, a LOOP and a variable VAR, determine whether it
2471 would be profitable to adjust VR using scalar evolution information
2472 for VAR. If so, update VR with the new limits. */
2475 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
, tree stmt
,
2478 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
;
2479 enum ev_direction dir
;
2481 /* TODO. Don't adjust anti-ranges. An anti-range may provide
2482 better opportunities than a regular range, but I'm not sure. */
2483 if (vr
->type
== VR_ANTI_RANGE
)
2486 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
2487 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
2490 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
2491 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
2493 /* If STEP is symbolic, we can't know whether INIT will be the
2494 minimum or maximum value in the range. Also, unless INIT is
2495 a simple expression, compare_values and possibly other functions
2496 in tree-vrp won't be able to handle it. */
2497 if (step
== NULL_TREE
2498 || !is_gimple_min_invariant (step
)
2499 || !valid_value_p (init
))
2502 dir
= scev_direction (chrec
);
2503 if (/* Do not adjust ranges if we do not know whether the iv increases
2504 or decreases, ... */
2505 dir
== EV_DIR_UNKNOWN
2506 /* ... or if it may wrap. */
2507 || scev_probably_wraps_p (init
, step
, stmt
,
2508 current_loops
->parray
[CHREC_VARIABLE (chrec
)],
2512 type
= TREE_TYPE (var
);
2514 /* If we see a pointer type starting at a constant, then we have an
2515 unusual ivopt. It may legitimately wrap. */
2516 if (POINTER_TYPE_P (type
) && is_gimple_min_invariant (init
))
2519 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
2520 negative_overflow_infinity and positive_overflow_infinity,
2521 because we have concluded that the loop probably does not
2524 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
2525 tmin
= lower_bound_in_type (type
, type
);
2527 tmin
= TYPE_MIN_VALUE (type
);
2528 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
2529 tmax
= upper_bound_in_type (type
, type
);
2531 tmax
= TYPE_MAX_VALUE (type
);
2533 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
2538 /* For VARYING or UNDEFINED ranges, just about anything we get
2539 from scalar evolutions should be better. */
2541 if (dir
== EV_DIR_DECREASES
)
2546 /* If we would create an invalid range, then just assume we
2547 know absolutely nothing. This may be over-conservative,
2548 but it's clearly safe, and should happen only in unreachable
2549 parts of code, or for invalid programs. */
2550 if (compare_values (min
, max
) == 1)
2553 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
2555 else if (vr
->type
== VR_RANGE
)
2560 if (dir
== EV_DIR_DECREASES
)
2562 /* INIT is the maximum value. If INIT is lower than VR->MAX
2563 but no smaller than VR->MIN, set VR->MAX to INIT. */
2564 if (compare_values (init
, max
) == -1)
2568 /* If we just created an invalid range with the minimum
2569 greater than the maximum, we fail conservatively.
2570 This should happen only in unreachable
2571 parts of code, or for invalid programs. */
2572 if (compare_values (min
, max
) == 1)
2576 /* According to the loop information, the variable does not
2577 overflow. If we think it does, probably because of an
2578 overflow due to arithmetic on a different INF value,
2580 if (is_negative_overflow_infinity (min
))
2585 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
2586 if (compare_values (init
, min
) == 1)
2590 /* Again, avoid creating invalid range by failing. */
2591 if (compare_values (min
, max
) == 1)
2595 if (is_positive_overflow_infinity (max
))
2599 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
2603 /* Return true if VAR may overflow at STMT. This checks any available
2604 loop information to see if we can determine that VAR does not
2608 vrp_var_may_overflow (tree var
, tree stmt
)
2611 tree chrec
, init
, step
;
2613 if (current_loops
== NULL
)
2616 l
= loop_containing_stmt (stmt
);
2620 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
2621 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
2624 init
= initial_condition_in_loop_num (chrec
, l
->num
);
2625 step
= evolution_part_in_loop_num (chrec
, l
->num
);
2627 if (step
== NULL_TREE
2628 || !is_gimple_min_invariant (step
)
2629 || !valid_value_p (init
))
2632 /* If we get here, we know something useful about VAR based on the
2633 loop information. If it wraps, it may overflow. */
2635 if (scev_probably_wraps_p (init
, step
, stmt
,
2636 current_loops
->parray
[CHREC_VARIABLE (chrec
)],
2640 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
2642 print_generic_expr (dump_file
, var
, 0);
2643 fprintf (dump_file
, ": loop information indicates does not overflow\n");
2650 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
2652 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
2653 all the values in the ranges.
2655 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
2657 - Return NULL_TREE if it is not always possible to determine the
2658 value of the comparison.
2660 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
2661 overflow infinity was used in the test. */
2665 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
2666 bool *strict_overflow_p
)
2668 /* VARYING or UNDEFINED ranges cannot be compared. */
2669 if (vr0
->type
== VR_VARYING
2670 || vr0
->type
== VR_UNDEFINED
2671 || vr1
->type
== VR_VARYING
2672 || vr1
->type
== VR_UNDEFINED
)
2675 /* Anti-ranges need to be handled separately. */
2676 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
2678 /* If both are anti-ranges, then we cannot compute any
2680 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
2683 /* These comparisons are never statically computable. */
2690 /* Equality can be computed only between a range and an
2691 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
2692 if (vr0
->type
== VR_RANGE
)
2694 /* To simplify processing, make VR0 the anti-range. */
2695 value_range_t
*tmp
= vr0
;
2700 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
2702 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
2703 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
2704 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
2709 if (!usable_range_p (vr0
, strict_overflow_p
)
2710 || !usable_range_p (vr1
, strict_overflow_p
))
2713 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
2714 operands around and change the comparison code. */
2715 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
2718 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
2724 if (comp
== EQ_EXPR
)
2726 /* Equality may only be computed if both ranges represent
2727 exactly one value. */
2728 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
2729 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
2731 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
2733 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
2735 if (cmp_min
== 0 && cmp_max
== 0)
2736 return boolean_true_node
;
2737 else if (cmp_min
!= -2 && cmp_max
!= -2)
2738 return boolean_false_node
;
2740 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
2741 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
2742 strict_overflow_p
) == 1
2743 || compare_values_warnv (vr1
->min
, vr0
->max
,
2744 strict_overflow_p
) == 1)
2745 return boolean_false_node
;
2749 else if (comp
== NE_EXPR
)
2753 /* If VR0 is completely to the left or completely to the right
2754 of VR1, they are always different. Notice that we need to
2755 make sure that both comparisons yield similar results to
2756 avoid comparing values that cannot be compared at
2758 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
2759 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
2760 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
2761 return boolean_true_node
;
2763 /* If VR0 and VR1 represent a single value and are identical,
2765 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
2766 strict_overflow_p
) == 0
2767 && compare_values_warnv (vr1
->min
, vr1
->max
,
2768 strict_overflow_p
) == 0
2769 && compare_values_warnv (vr0
->min
, vr1
->min
,
2770 strict_overflow_p
) == 0
2771 && compare_values_warnv (vr0
->max
, vr1
->max
,
2772 strict_overflow_p
) == 0)
2773 return boolean_false_node
;
2775 /* Otherwise, they may or may not be different. */
2779 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
2783 /* If VR0 is to the left of VR1, return true. */
2784 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
2785 if ((comp
== LT_EXPR
&& tst
== -1)
2786 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
2788 if (overflow_infinity_range_p (vr0
)
2789 || overflow_infinity_range_p (vr1
))
2790 *strict_overflow_p
= true;
2791 return boolean_true_node
;
2794 /* If VR0 is to the right of VR1, return false. */
2795 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
2796 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
2797 || (comp
== LE_EXPR
&& tst
== 1))
2799 if (overflow_infinity_range_p (vr0
)
2800 || overflow_infinity_range_p (vr1
))
2801 *strict_overflow_p
= true;
2802 return boolean_false_node
;
2805 /* Otherwise, we don't know. */
2813 /* Given a value range VR, a value VAL and a comparison code COMP, return
2814 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
2815 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
2816 always returns false. Return NULL_TREE if it is not always
2817 possible to determine the value of the comparison. Also set
2818 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
2819 infinity was used in the test. */
2822 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
2823 bool *strict_overflow_p
)
2825 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
2828 /* Anti-ranges need to be handled separately. */
2829 if (vr
->type
== VR_ANTI_RANGE
)
2831 /* For anti-ranges, the only predicates that we can compute at
2832 compile time are equality and inequality. */
2839 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
2840 if (value_inside_range (val
, vr
) == 1)
2841 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
2846 if (!usable_range_p (vr
, strict_overflow_p
))
2849 if (comp
== EQ_EXPR
)
2851 /* EQ_EXPR may only be computed if VR represents exactly
2853 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
2855 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
2857 return boolean_true_node
;
2858 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
2859 return boolean_false_node
;
2861 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
2862 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
2863 return boolean_false_node
;
2867 else if (comp
== NE_EXPR
)
2869 /* If VAL is not inside VR, then they are always different. */
2870 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
2871 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
2872 return boolean_true_node
;
2874 /* If VR represents exactly one value equal to VAL, then return
2876 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
2877 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
2878 return boolean_false_node
;
2880 /* Otherwise, they may or may not be different. */
2883 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
2887 /* If VR is to the left of VAL, return true. */
2888 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
2889 if ((comp
== LT_EXPR
&& tst
== -1)
2890 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
2892 if (overflow_infinity_range_p (vr
))
2893 *strict_overflow_p
= true;
2894 return boolean_true_node
;
2897 /* If VR is to the right of VAL, return false. */
2898 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
2899 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
2900 || (comp
== LE_EXPR
&& tst
== 1))
2902 if (overflow_infinity_range_p (vr
))
2903 *strict_overflow_p
= true;
2904 return boolean_false_node
;
2907 /* Otherwise, we don't know. */
2910 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
2914 /* If VR is to the right of VAL, return true. */
2915 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
2916 if ((comp
== GT_EXPR
&& tst
== 1)
2917 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
2919 if (overflow_infinity_range_p (vr
))
2920 *strict_overflow_p
= true;
2921 return boolean_true_node
;
2924 /* If VR is to the left of VAL, return false. */
2925 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
2926 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
2927 || (comp
== GE_EXPR
&& tst
== -1))
2929 if (overflow_infinity_range_p (vr
))
2930 *strict_overflow_p
= true;
2931 return boolean_false_node
;
2934 /* Otherwise, we don't know. */
2942 /* Debugging dumps. */
2944 void dump_value_range (FILE *, value_range_t
*);
2945 void debug_value_range (value_range_t
*);
2946 void dump_all_value_ranges (FILE *);
2947 void debug_all_value_ranges (void);
2948 void dump_vr_equiv (FILE *, bitmap
);
2949 void debug_vr_equiv (bitmap
);
2952 /* Dump value range VR to FILE. */
2955 dump_value_range (FILE *file
, value_range_t
*vr
)
2958 fprintf (file
, "[]");
2959 else if (vr
->type
== VR_UNDEFINED
)
2960 fprintf (file
, "UNDEFINED");
2961 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
2963 tree type
= TREE_TYPE (vr
->min
);
2965 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
2967 if (is_negative_overflow_infinity (vr
->min
))
2968 fprintf (file
, "-INF(OVF)");
2969 else if (INTEGRAL_TYPE_P (type
)
2970 && !TYPE_UNSIGNED (type
)
2971 && vrp_val_is_min (vr
->min
))
2972 fprintf (file
, "-INF");
2974 print_generic_expr (file
, vr
->min
, 0);
2976 fprintf (file
, ", ");
2978 if (is_positive_overflow_infinity (vr
->max
))
2979 fprintf (file
, "+INF(OVF)");
2980 else if (INTEGRAL_TYPE_P (type
)
2981 && vrp_val_is_max (vr
->max
))
2982 fprintf (file
, "+INF");
2984 print_generic_expr (file
, vr
->max
, 0);
2986 fprintf (file
, "]");
2993 fprintf (file
, " EQUIVALENCES: { ");
2995 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
2997 print_generic_expr (file
, ssa_name (i
), 0);
2998 fprintf (file
, " ");
3002 fprintf (file
, "} (%u elements)", c
);
3005 else if (vr
->type
== VR_VARYING
)
3006 fprintf (file
, "VARYING");
3008 fprintf (file
, "INVALID RANGE");
3012 /* Dump value range VR to stderr. */
3015 debug_value_range (value_range_t
*vr
)
3017 dump_value_range (stderr
, vr
);
3018 fprintf (stderr
, "\n");
3022 /* Dump value ranges of all SSA_NAMEs to FILE. */
3025 dump_all_value_ranges (FILE *file
)
3029 for (i
= 0; i
< num_ssa_names
; i
++)
3033 print_generic_expr (file
, ssa_name (i
), 0);
3034 fprintf (file
, ": ");
3035 dump_value_range (file
, vr_value
[i
]);
3036 fprintf (file
, "\n");
3040 fprintf (file
, "\n");
3044 /* Dump all value ranges to stderr. */
3047 debug_all_value_ranges (void)
3049 dump_all_value_ranges (stderr
);
3053 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
3054 create a new SSA name N and return the assertion assignment
3055 'V = ASSERT_EXPR <V, V OP W>'. */
3058 build_assert_expr_for (tree cond
, tree v
)
3062 gcc_assert (TREE_CODE (v
) == SSA_NAME
);
3063 n
= duplicate_ssa_name (v
, NULL_TREE
);
3065 if (COMPARISON_CLASS_P (cond
))
3067 tree a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
3068 assertion
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), n
, a
);
3070 else if (TREE_CODE (cond
) == TRUTH_NOT_EXPR
)
3072 /* Given !V, build the assignment N = false. */
3073 tree op0
= TREE_OPERAND (cond
, 0);
3074 gcc_assert (op0
== v
);
3075 assertion
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), n
, boolean_false_node
);
3077 else if (TREE_CODE (cond
) == SSA_NAME
)
3079 /* Given V, build the assignment N = true. */
3080 gcc_assert (v
== cond
);
3081 assertion
= build2 (MODIFY_EXPR
, TREE_TYPE (v
), n
, boolean_true_node
);
3086 SSA_NAME_DEF_STMT (n
) = assertion
;
3088 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
3089 operand of the ASSERT_EXPR. Register the new name and the old one
3090 in the replacement table so that we can fix the SSA web after
3091 adding all the ASSERT_EXPRs. */
3092 register_new_name_mapping (n
, v
);
3098 /* Return false if EXPR is a predicate expression involving floating
3102 fp_predicate (tree expr
)
3104 return (COMPARISON_CLASS_P (expr
)
3105 && FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (expr
, 0))));
3109 /* If the range of values taken by OP can be inferred after STMT executes,
3110 return the comparison code (COMP_CODE_P) and value (VAL_P) that
3111 describes the inferred range. Return true if a range could be
3115 infer_value_range (tree stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
3118 *comp_code_p
= ERROR_MARK
;
3120 /* Do not attempt to infer anything in names that flow through
3122 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
3125 /* Similarly, don't infer anything from statements that may throw
3127 if (tree_could_throw_p (stmt
))
3130 /* If STMT is the last statement of a basic block with no
3131 successors, there is no point inferring anything about any of its
3132 operands. We would not be able to find a proper insertion point
3133 for the assertion, anyway. */
3134 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (bb_for_stmt (stmt
)->succs
) == 0)
3137 /* We can only assume that a pointer dereference will yield
3138 non-NULL if -fdelete-null-pointer-checks is enabled. */
3139 if (flag_delete_null_pointer_checks
&& POINTER_TYPE_P (TREE_TYPE (op
)))
3142 unsigned num_uses
, num_derefs
;
3144 count_uses_and_derefs (op
, stmt
, &num_uses
, &num_derefs
, &is_store
);
3147 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
3148 *comp_code_p
= NE_EXPR
;
3157 void dump_asserts_for (FILE *, tree
);
3158 void debug_asserts_for (tree
);
3159 void dump_all_asserts (FILE *);
3160 void debug_all_asserts (void);
3162 /* Dump all the registered assertions for NAME to FILE. */
3165 dump_asserts_for (FILE *file
, tree name
)
3169 fprintf (file
, "Assertions to be inserted for ");
3170 print_generic_expr (file
, name
, 0);
3171 fprintf (file
, "\n");
3173 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
3176 fprintf (file
, "\t");
3177 print_generic_expr (file
, bsi_stmt (loc
->si
), 0);
3178 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
3181 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
3182 loc
->e
->dest
->index
);
3183 dump_edge_info (file
, loc
->e
, 0);
3185 fprintf (file
, "\n\tPREDICATE: ");
3186 print_generic_expr (file
, name
, 0);
3187 fprintf (file
, " %s ", tree_code_name
[(int)loc
->comp_code
]);
3188 print_generic_expr (file
, loc
->val
, 0);
3189 fprintf (file
, "\n\n");
3193 fprintf (file
, "\n");
3197 /* Dump all the registered assertions for NAME to stderr. */
3200 debug_asserts_for (tree name
)
3202 dump_asserts_for (stderr
, name
);
3206 /* Dump all the registered assertions for all the names to FILE. */
3209 dump_all_asserts (FILE *file
)
3214 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
3215 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
3216 dump_asserts_for (file
, ssa_name (i
));
3217 fprintf (file
, "\n");
3221 /* Dump all the registered assertions for all the names to stderr. */
3224 debug_all_asserts (void)
3226 dump_all_asserts (stderr
);
3230 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
3231 'NAME COMP_CODE VAL' at a location that dominates block BB or
3232 E->DEST, then register this location as a possible insertion point
3233 for ASSERT_EXPR <NAME, NAME COMP_CODE VAL>.
3235 BB, E and SI provide the exact insertion point for the new
3236 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
3237 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
3238 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
3239 must not be NULL. */
3242 register_new_assert_for (tree name
,
3243 enum tree_code comp_code
,
3247 block_stmt_iterator si
)
3249 assert_locus_t n
, loc
, last_loc
;
3251 basic_block dest_bb
;
3253 #if defined ENABLE_CHECKING
3254 gcc_assert (bb
== NULL
|| e
== NULL
);
3257 gcc_assert (TREE_CODE (bsi_stmt (si
)) != COND_EXPR
3258 && TREE_CODE (bsi_stmt (si
)) != SWITCH_EXPR
);
3261 /* The new assertion A will be inserted at BB or E. We need to
3262 determine if the new location is dominated by a previously
3263 registered location for A. If we are doing an edge insertion,
3264 assume that A will be inserted at E->DEST. Note that this is not
3267 If E is a critical edge, it will be split. But even if E is
3268 split, the new block will dominate the same set of blocks that
3271 The reverse, however, is not true, blocks dominated by E->DEST
3272 will not be dominated by the new block created to split E. So,
3273 if the insertion location is on a critical edge, we will not use
3274 the new location to move another assertion previously registered
3275 at a block dominated by E->DEST. */
3276 dest_bb
= (bb
) ? bb
: e
->dest
;
3278 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
3279 VAL at a block dominating DEST_BB, then we don't need to insert a new
3280 one. Similarly, if the same assertion already exists at a block
3281 dominated by DEST_BB and the new location is not on a critical
3282 edge, then update the existing location for the assertion (i.e.,
3283 move the assertion up in the dominance tree).
3285 Note, this is implemented as a simple linked list because there
3286 should not be more than a handful of assertions registered per
3287 name. If this becomes a performance problem, a table hashed by
3288 COMP_CODE and VAL could be implemented. */
3289 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
3294 if (loc
->comp_code
== comp_code
3296 || operand_equal_p (loc
->val
, val
, 0)))
3298 /* If the assertion NAME COMP_CODE VAL has already been
3299 registered at a basic block that dominates DEST_BB, then
3300 we don't need to insert the same assertion again. Note
3301 that we don't check strict dominance here to avoid
3302 replicating the same assertion inside the same basic
3303 block more than once (e.g., when a pointer is
3304 dereferenced several times inside a block).
3306 An exception to this rule are edge insertions. If the
3307 new assertion is to be inserted on edge E, then it will
3308 dominate all the other insertions that we may want to
3309 insert in DEST_BB. So, if we are doing an edge
3310 insertion, don't do this dominance check. */
3312 && dominated_by_p (CDI_DOMINATORS
, dest_bb
, loc
->bb
))
3315 /* Otherwise, if E is not a critical edge and DEST_BB
3316 dominates the existing location for the assertion, move
3317 the assertion up in the dominance tree by updating its
3318 location information. */
3319 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
3320 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
3329 /* Update the last node of the list and move to the next one. */
3334 /* If we didn't find an assertion already registered for
3335 NAME COMP_CODE VAL, add a new one at the end of the list of
3336 assertions associated with NAME. */
3337 n
= XNEW (struct assert_locus_d
);
3341 n
->comp_code
= comp_code
;
3348 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
3350 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
3354 /* Try to register an edge assertion for SSA name NAME on edge E for
3355 the conditional jump pointed to by SI. Return true if an assertion
3356 for NAME could be registered. */
3359 register_edge_assert_for (tree name
, edge e
, block_stmt_iterator si
)
3362 enum tree_code comp_code
;
3364 stmt
= bsi_stmt (si
);
3366 /* Do not attempt to infer anything in names that flow through
3368 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
3371 /* If NAME was not found in the sub-graph reachable from E, then
3372 there's nothing to do. */
3373 if (!TEST_BIT (found_in_subgraph
, SSA_NAME_VERSION (name
)))
3376 /* We found a use of NAME in the sub-graph rooted at E->DEST.
3377 Register an assertion for NAME according to the value that NAME
3379 if (TREE_CODE (stmt
) == COND_EXPR
)
3381 /* If BB ends in a COND_EXPR then NAME then we should insert
3382 the original predicate on EDGE_TRUE_VALUE and the
3383 opposite predicate on EDGE_FALSE_VALUE. */
3384 tree cond
= COND_EXPR_COND (stmt
);
3385 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
3387 /* Predicates may be a single SSA name or NAME OP VAL. */
3390 /* If the predicate is a name, it must be NAME, in which
3391 case we create the predicate NAME == true or
3392 NAME == false accordingly. */
3393 comp_code
= EQ_EXPR
;
3394 val
= (is_else_edge
) ? boolean_false_node
: boolean_true_node
;
3398 /* Otherwise, we have a comparison of the form NAME COMP VAL
3399 or VAL COMP NAME. */
3400 if (name
== TREE_OPERAND (cond
, 1))
3402 /* If the predicate is of the form VAL COMP NAME, flip
3403 COMP around because we need to register NAME as the
3404 first operand in the predicate. */
3405 comp_code
= swap_tree_comparison (TREE_CODE (cond
));
3406 val
= TREE_OPERAND (cond
, 0);
3410 /* The comparison is of the form NAME COMP VAL, so the
3411 comparison code remains unchanged. */
3412 comp_code
= TREE_CODE (cond
);
3413 val
= TREE_OPERAND (cond
, 1);
3416 /* If we are inserting the assertion on the ELSE edge, we
3417 need to invert the sign comparison. */
3419 comp_code
= invert_tree_comparison (comp_code
, 0);
3421 /* Do not register always-false predicates. FIXME, this
3422 works around a limitation in fold() when dealing with
3423 enumerations. Given 'enum { N1, N2 } x;', fold will not
3424 fold 'if (x > N2)' to 'if (0)'. */
3425 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
3426 && (INTEGRAL_TYPE_P (TREE_TYPE (val
))
3427 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
))))
3429 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
3430 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
3432 if (comp_code
== GT_EXPR
&& compare_values (val
, max
) == 0)
3435 if (comp_code
== LT_EXPR
&& compare_values (val
, min
) == 0)
3442 /* FIXME. Handle SWITCH_EXPR. */
3446 register_new_assert_for (name
, comp_code
, val
, NULL
, e
, si
);
3451 static bool find_assert_locations (basic_block bb
);
3453 /* Determine whether the outgoing edges of BB should receive an
3454 ASSERT_EXPR for each of the operands of BB's last statement. The
3455 last statement of BB must be a COND_EXPR or a SWITCH_EXPR.
3457 If any of the sub-graphs rooted at BB have an interesting use of
3458 the predicate operands, an assert location node is added to the
3459 list of assertions for the corresponding operands. */
3462 find_conditional_asserts (basic_block bb
)
3465 block_stmt_iterator last_si
;
3471 need_assert
= false;
3472 last_si
= bsi_last (bb
);
3473 last
= bsi_stmt (last_si
);
3475 /* Look for uses of the operands in each of the sub-graphs
3476 rooted at BB. We need to check each of the outgoing edges
3477 separately, so that we know what kind of ASSERT_EXPR to
3479 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3484 /* Remove the COND_EXPR operands from the FOUND_IN_SUBGRAPH bitmap.
3485 Otherwise, when we finish traversing each of the sub-graphs, we
3486 won't know whether the variables were found in the sub-graphs or
3487 if they had been found in a block upstream from BB.
3489 This is actually a bad idea is some cases, particularly jump
3490 threading. Consider a CFG like the following:
3500 Assume that one or more operands in the conditional at the
3501 end of block 0 are used in a conditional in block 2, but not
3502 anywhere in block 1. In this case we will not insert any
3503 assert statements in block 1, which may cause us to miss
3504 opportunities to optimize, particularly for jump threading. */
3505 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
3506 RESET_BIT (found_in_subgraph
, SSA_NAME_VERSION (op
));
3508 /* Traverse the strictly dominated sub-graph rooted at E->DEST
3509 to determine if any of the operands in the conditional
3510 predicate are used. */
3512 need_assert
|= find_assert_locations (e
->dest
);
3514 /* Register the necessary assertions for each operand in the
3515 conditional predicate. */
3516 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
3517 need_assert
|= register_edge_assert_for (op
, e
, last_si
);
3520 /* Finally, indicate that we have found the operands in the
3522 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
3523 SET_BIT (found_in_subgraph
, SSA_NAME_VERSION (op
));
3529 /* Traverse all the statements in block BB looking for statements that
3530 may generate useful assertions for the SSA names in their operand.
3531 If a statement produces a useful assertion A for name N_i, then the
3532 list of assertions already generated for N_i is scanned to
3533 determine if A is actually needed.
3535 If N_i already had the assertion A at a location dominating the
3536 current location, then nothing needs to be done. Otherwise, the
3537 new location for A is recorded instead.
3539 1- For every statement S in BB, all the variables used by S are
3540 added to bitmap FOUND_IN_SUBGRAPH.
3542 2- If statement S uses an operand N in a way that exposes a known
3543 value range for N, then if N was not already generated by an
3544 ASSERT_EXPR, create a new assert location for N. For instance,
3545 if N is a pointer and the statement dereferences it, we can
3546 assume that N is not NULL.
3548 3- COND_EXPRs are a special case of #2. We can derive range
3549 information from the predicate but need to insert different
3550 ASSERT_EXPRs for each of the sub-graphs rooted at the
3551 conditional block. If the last statement of BB is a conditional
3552 expression of the form 'X op Y', then
3554 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3556 b) If the conditional is the only entry point to the sub-graph
3557 corresponding to the THEN_CLAUSE, recurse into it. On
3558 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3559 an ASSERT_EXPR is added for the corresponding variable.
3561 c) Repeat step (b) on the ELSE_CLAUSE.
3563 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3572 In this case, an assertion on the THEN clause is useful to
3573 determine that 'a' is always 9 on that edge. However, an assertion
3574 on the ELSE clause would be unnecessary.
3576 4- If BB does not end in a conditional expression, then we recurse
3577 into BB's dominator children.
3579 At the end of the recursive traversal, every SSA name will have a
3580 list of locations where ASSERT_EXPRs should be added. When a new
3581 location for name N is found, it is registered by calling
3582 register_new_assert_for. That function keeps track of all the
3583 registered assertions to prevent adding unnecessary assertions.
3584 For instance, if a pointer P_4 is dereferenced more than once in a
3585 dominator tree, only the location dominating all the dereference of
3586 P_4 will receive an ASSERT_EXPR.
3588 If this function returns true, then it means that there are names
3589 for which we need to generate ASSERT_EXPRs. Those assertions are
3590 inserted by process_assert_insertions.
3592 TODO. Handle SWITCH_EXPR. */
3595 find_assert_locations (basic_block bb
)
3597 block_stmt_iterator si
;
3602 if (TEST_BIT (blocks_visited
, bb
->index
))
3605 SET_BIT (blocks_visited
, bb
->index
);
3607 need_assert
= false;
3609 /* Traverse all PHI nodes in BB marking used operands. */
3610 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
3612 use_operand_p arg_p
;
3615 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
3617 tree arg
= USE_FROM_PTR (arg_p
);
3618 if (TREE_CODE (arg
) == SSA_NAME
)
3620 gcc_assert (is_gimple_reg (PHI_RESULT (phi
)));
3621 SET_BIT (found_in_subgraph
, SSA_NAME_VERSION (arg
));
3626 /* Traverse all the statements in BB marking used names and looking
3627 for statements that may infer assertions for their used operands. */
3629 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
3634 stmt
= bsi_stmt (si
);
3636 /* See if we can derive an assertion for any of STMT's operands. */
3637 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
3640 enum tree_code comp_code
;
3642 /* Mark OP in bitmap FOUND_IN_SUBGRAPH. If STMT is inside
3643 the sub-graph of a conditional block, when we return from
3644 this recursive walk, our parent will use the
3645 FOUND_IN_SUBGRAPH bitset to determine if one of the
3646 operands it was looking for was present in the sub-graph. */
3647 SET_BIT (found_in_subgraph
, SSA_NAME_VERSION (op
));
3649 /* If OP is used in such a way that we can infer a value
3650 range for it, and we don't find a previous assertion for
3651 it, create a new assertion location node for OP. */
3652 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
3654 /* If we are able to infer a nonzero value range for OP,
3655 then walk backwards through the use-def chain to see if OP
3656 was set via a typecast.
3658 If so, then we can also infer a nonzero value range
3659 for the operand of the NOP_EXPR. */
3660 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
3663 tree def_stmt
= SSA_NAME_DEF_STMT (t
);
3665 while (TREE_CODE (def_stmt
) == MODIFY_EXPR
3666 && TREE_CODE (TREE_OPERAND (def_stmt
, 1)) == NOP_EXPR
3667 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (def_stmt
, 1), 0)) == SSA_NAME
3668 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (def_stmt
, 1), 0))))
3670 t
= TREE_OPERAND (TREE_OPERAND (def_stmt
, 1), 0);
3671 def_stmt
= SSA_NAME_DEF_STMT (t
);
3673 /* Note we want to register the assert for the
3674 operand of the NOP_EXPR after SI, not after the
3676 if (! has_single_use (t
))
3678 register_new_assert_for (t
, comp_code
, value
,
3685 /* If OP is used only once, namely in this STMT, don't
3686 bother creating an ASSERT_EXPR for it. Such an
3687 ASSERT_EXPR would do nothing but increase compile time. */
3688 if (!has_single_use (op
))
3690 register_new_assert_for (op
, comp_code
, value
, bb
, NULL
, si
);
3696 /* Remember the last statement of the block. */
3700 /* If BB's last statement is a conditional expression
3701 involving integer operands, recurse into each of the sub-graphs
3702 rooted at BB to determine if we need to add ASSERT_EXPRs. */
3704 && TREE_CODE (last
) == COND_EXPR
3705 && !fp_predicate (COND_EXPR_COND (last
))
3706 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
3707 need_assert
|= find_conditional_asserts (bb
);
3709 /* Recurse into the dominator children of BB. */
3710 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
3712 son
= next_dom_son (CDI_DOMINATORS
, son
))
3713 need_assert
|= find_assert_locations (son
);
3719 /* Create an ASSERT_EXPR for NAME and insert it in the location
3720 indicated by LOC. Return true if we made any edge insertions. */
3723 process_assert_insertions_for (tree name
, assert_locus_t loc
)
3725 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3726 tree stmt
, cond
, assert_expr
;
3730 cond
= build2 (loc
->comp_code
, boolean_type_node
, name
, loc
->val
);
3731 assert_expr
= build_assert_expr_for (cond
, name
);
3735 /* We have been asked to insert the assertion on an edge. This
3736 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3737 #if defined ENABLE_CHECKING
3738 gcc_assert (TREE_CODE (bsi_stmt (loc
->si
)) == COND_EXPR
3739 || TREE_CODE (bsi_stmt (loc
->si
)) == SWITCH_EXPR
);
3742 bsi_insert_on_edge (loc
->e
, assert_expr
);
3746 /* Otherwise, we can insert right after LOC->SI iff the
3747 statement must not be the last statement in the block. */
3748 stmt
= bsi_stmt (loc
->si
);
3749 if (!stmt_ends_bb_p (stmt
))
3751 bsi_insert_after (&loc
->si
, assert_expr
, BSI_SAME_STMT
);
3755 /* If STMT must be the last statement in BB, we can only insert new
3756 assertions on the non-abnormal edge out of BB. Note that since
3757 STMT is not control flow, there may only be one non-abnormal edge
3759 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
3760 if (!(e
->flags
& EDGE_ABNORMAL
))
3762 bsi_insert_on_edge (e
, assert_expr
);
3770 /* Process all the insertions registered for every name N_i registered
3771 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3772 found in ASSERTS_FOR[i]. */
3775 process_assert_insertions (void)
3779 bool update_edges_p
= false;
3780 int num_asserts
= 0;
3782 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3783 dump_all_asserts (dump_file
);
3785 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
3787 assert_locus_t loc
= asserts_for
[i
];
3792 assert_locus_t next
= loc
->next
;
3793 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
3801 bsi_commit_edge_inserts ();
3803 if (dump_file
&& (dump_flags
& TDF_STATS
))
3804 fprintf (dump_file
, "\nNumber of ASSERT_EXPR expressions inserted: %d\n\n",
3809 /* Traverse the flowgraph looking for conditional jumps to insert range
3810 expressions. These range expressions are meant to provide information
3811 to optimizations that need to reason in terms of value ranges. They
3812 will not be expanded into RTL. For instance, given:
3821 this pass will transform the code into:
3827 x = ASSERT_EXPR <x, x < y>
3832 y = ASSERT_EXPR <y, x <= y>
3836 The idea is that once copy and constant propagation have run, other
3837 optimizations will be able to determine what ranges of values can 'x'
3838 take in different paths of the code, simply by checking the reaching
3839 definition of 'x'. */
3842 insert_range_assertions (void)
3848 found_in_subgraph
= sbitmap_alloc (num_ssa_names
);
3849 sbitmap_zero (found_in_subgraph
);
3851 blocks_visited
= sbitmap_alloc (last_basic_block
);
3852 sbitmap_zero (blocks_visited
);
3854 need_assert_for
= BITMAP_ALLOC (NULL
);
3855 asserts_for
= XNEWVEC (assert_locus_t
, num_ssa_names
);
3856 memset (asserts_for
, 0, num_ssa_names
* sizeof (assert_locus_t
));
3858 calculate_dominance_info (CDI_DOMINATORS
);
3860 update_ssa_p
= false;
3861 FOR_EACH_EDGE (e
, ei
, ENTRY_BLOCK_PTR
->succs
)
3862 if (find_assert_locations (e
->dest
))
3863 update_ssa_p
= true;
3867 process_assert_insertions ();
3868 update_ssa (TODO_update_ssa_no_phi
);
3871 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3873 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
3874 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
3877 sbitmap_free (found_in_subgraph
);
3879 BITMAP_FREE (need_assert_for
);
3883 /* Convert range assertion expressions into the implied copies and
3884 copy propagate away the copies. Doing the trivial copy propagation
3885 here avoids the need to run the full copy propagation pass after
3888 FIXME, this will eventually lead to copy propagation removing the
3889 names that had useful range information attached to them. For
3890 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
3891 then N_i will have the range [3, +INF].
3893 However, by converting the assertion into the implied copy
3894 operation N_i = N_j, we will then copy-propagate N_j into the uses
3895 of N_i and lose the range information. We may want to hold on to
3896 ASSERT_EXPRs a little while longer as the ranges could be used in
3897 things like jump threading.
3899 The problem with keeping ASSERT_EXPRs around is that passes after
3900 VRP need to handle them appropriately.
3902 Another approach would be to make the range information a first
3903 class property of the SSA_NAME so that it can be queried from
3904 any pass. This is made somewhat more complex by the need for
3905 multiple ranges to be associated with one SSA_NAME. */
3908 remove_range_assertions (void)
3911 block_stmt_iterator si
;
3913 /* Note that the BSI iterator bump happens at the bottom of the
3914 loop and no bump is necessary if we're removing the statement
3915 referenced by the current BSI. */
3917 for (si
= bsi_start (bb
); !bsi_end_p (si
);)
3919 tree stmt
= bsi_stmt (si
);
3922 if (TREE_CODE (stmt
) == MODIFY_EXPR
3923 && TREE_CODE (TREE_OPERAND (stmt
, 1)) == ASSERT_EXPR
)
3925 tree rhs
= TREE_OPERAND (stmt
, 1), var
;
3926 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
3927 use_operand_p use_p
;
3928 imm_use_iterator iter
;
3930 gcc_assert (cond
!= boolean_false_node
);
3932 /* Propagate the RHS into every use of the LHS. */
3933 var
= ASSERT_EXPR_VAR (rhs
);
3934 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, TREE_OPERAND (stmt
, 0))
3935 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3937 SET_USE (use_p
, var
);
3938 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
3941 /* And finally, remove the copy, it is not needed. */
3942 bsi_remove (&si
, true);
3948 sbitmap_free (blocks_visited
);
3952 /* Return true if STMT is interesting for VRP. */
3955 stmt_interesting_for_vrp (tree stmt
)
3957 if (TREE_CODE (stmt
) == PHI_NODE
3958 && is_gimple_reg (PHI_RESULT (stmt
))
3959 && (INTEGRAL_TYPE_P (TREE_TYPE (PHI_RESULT (stmt
)))
3960 || POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (stmt
)))))
3962 else if (TREE_CODE (stmt
) == MODIFY_EXPR
)
3964 tree lhs
= TREE_OPERAND (stmt
, 0);
3965 tree rhs
= TREE_OPERAND (stmt
, 1);
3967 /* In general, assignments with virtual operands are not useful
3968 for deriving ranges, with the obvious exception of calls to
3969 builtin functions. */
3970 if (TREE_CODE (lhs
) == SSA_NAME
3971 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
3972 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
3973 && ((TREE_CODE (rhs
) == CALL_EXPR
3974 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == ADDR_EXPR
3975 && DECL_P (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 0))
3976 && DECL_IS_BUILTIN (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 0)))
3977 || ZERO_SSA_OPERANDS (stmt
, SSA_OP_ALL_VIRTUALS
)))
3980 else if (TREE_CODE (stmt
) == COND_EXPR
|| TREE_CODE (stmt
) == SWITCH_EXPR
)
3987 /* Initialize local data structures for VRP. */
3990 vrp_initialize (void)
3994 vr_value
= XNEWVEC (value_range_t
*, num_ssa_names
);
3995 memset (vr_value
, 0, num_ssa_names
* sizeof (value_range_t
*));
3999 block_stmt_iterator si
;
4002 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
4004 if (!stmt_interesting_for_vrp (phi
))
4006 tree lhs
= PHI_RESULT (phi
);
4007 set_value_range_to_varying (get_value_range (lhs
));
4008 DONT_SIMULATE_AGAIN (phi
) = true;
4011 DONT_SIMULATE_AGAIN (phi
) = false;
4014 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
4016 tree stmt
= bsi_stmt (si
);
4018 if (!stmt_interesting_for_vrp (stmt
))
4022 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
4023 set_value_range_to_varying (get_value_range (def
));
4024 DONT_SIMULATE_AGAIN (stmt
) = true;
4028 DONT_SIMULATE_AGAIN (stmt
) = false;
4035 /* Visit assignment STMT. If it produces an interesting range, record
4036 the SSA name in *OUTPUT_P. */
4038 static enum ssa_prop_result
4039 vrp_visit_assignment (tree stmt
, tree
*output_p
)
4044 lhs
= TREE_OPERAND (stmt
, 0);
4045 rhs
= TREE_OPERAND (stmt
, 1);
4047 /* We only keep track of ranges in integral and pointer types. */
4048 if (TREE_CODE (lhs
) == SSA_NAME
4049 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
4050 /* It is valid to have NULL MIN/MAX values on a type. See
4051 build_range_type. */
4052 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
4053 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
4054 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
4057 value_range_t new_vr
= { VR_UNDEFINED
, NULL_TREE
, NULL_TREE
, NULL
};
4059 extract_range_from_expr (&new_vr
, rhs
);
4061 /* If STMT is inside a loop, we may be able to know something
4062 else about the range of LHS by examining scalar evolution
4064 if (current_loops
&& (l
= loop_containing_stmt (stmt
)))
4065 adjust_range_with_scev (&new_vr
, l
, stmt
, lhs
);
4067 if (update_value_range (lhs
, &new_vr
))
4071 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4073 fprintf (dump_file
, "Found new range for ");
4074 print_generic_expr (dump_file
, lhs
, 0);
4075 fprintf (dump_file
, ": ");
4076 dump_value_range (dump_file
, &new_vr
);
4077 fprintf (dump_file
, "\n\n");
4080 if (new_vr
.type
== VR_VARYING
)
4081 return SSA_PROP_VARYING
;
4083 return SSA_PROP_INTERESTING
;
4086 return SSA_PROP_NOT_INTERESTING
;
4089 /* Every other statement produces no useful ranges. */
4090 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
4091 set_value_range_to_varying (get_value_range (def
));
4093 return SSA_PROP_VARYING
;
4097 /* Compare all the value ranges for names equivalent to VAR with VAL
4098 using comparison code COMP. Return the same value returned by
4099 compare_range_with_value, including the setting of
4100 *STRICT_OVERFLOW_P. */
4103 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
4104 bool *strict_overflow_p
)
4110 int used_strict_overflow
;
4112 t
= retval
= NULL_TREE
;
4114 /* Get the set of equivalences for VAR. */
4115 e
= get_value_range (var
)->equiv
;
4117 /* Add VAR to its own set of equivalences so that VAR's value range
4118 is processed by this loop (otherwise, we would have to replicate
4119 the body of the loop just to check VAR's value range). */
4120 bitmap_set_bit (e
, SSA_NAME_VERSION (var
));
4122 /* Start at -1. Set it to 0 if we do a comparison without relying
4123 on overflow, or 1 if all comparisons rely on overflow. */
4124 used_strict_overflow
= -1;
4126 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
4130 value_range_t equiv_vr
= *(vr_value
[i
]);
4132 /* If name N_i does not have a valid range, use N_i as its own
4133 range. This allows us to compare against names that may
4134 have N_i in their ranges. */
4135 if (equiv_vr
.type
== VR_VARYING
|| equiv_vr
.type
== VR_UNDEFINED
)
4137 equiv_vr
.type
= VR_RANGE
;
4138 equiv_vr
.min
= ssa_name (i
);
4139 equiv_vr
.max
= ssa_name (i
);
4143 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
4146 /* If we get different answers from different members
4147 of the equivalence set this check must be in a dead
4148 code region. Folding it to a trap representation
4149 would be correct here. For now just return don't-know. */
4159 used_strict_overflow
= 0;
4160 else if (used_strict_overflow
< 0)
4161 used_strict_overflow
= 1;
4165 /* Remove VAR from its own equivalence set. */
4166 bitmap_clear_bit (e
, SSA_NAME_VERSION (var
));
4170 if (used_strict_overflow
> 0)
4171 *strict_overflow_p
= true;
4175 /* We couldn't find a non-NULL value for the predicate. */
4180 /* Given a comparison code COMP and names N1 and N2, compare all the
4181 ranges equivalent to N1 against all the ranges equivalent to N2
4182 to determine the value of N1 COMP N2. Return the same value
4183 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
4184 whether we relied on an overflow infinity in the comparison. */
4188 compare_names (enum tree_code comp
, tree n1
, tree n2
,
4189 bool *strict_overflow_p
)
4193 bitmap_iterator bi1
, bi2
;
4195 int used_strict_overflow
;
4197 /* Compare the ranges of every name equivalent to N1 against the
4198 ranges of every name equivalent to N2. */
4199 e1
= get_value_range (n1
)->equiv
;
4200 e2
= get_value_range (n2
)->equiv
;
4202 /* Add N1 and N2 to their own set of equivalences to avoid
4203 duplicating the body of the loop just to check N1 and N2
4205 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
4206 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
4208 /* If the equivalence sets have a common intersection, then the two
4209 names can be compared without checking their ranges. */
4210 if (bitmap_intersect_p (e1
, e2
))
4212 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
4213 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
4215 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
4217 : boolean_false_node
;
4220 /* Start at -1. Set it to 0 if we do a comparison without relying
4221 on overflow, or 1 if all comparisons rely on overflow. */
4222 used_strict_overflow
= -1;
4224 /* Otherwise, compare all the equivalent ranges. First, add N1 and
4225 N2 to their own set of equivalences to avoid duplicating the body
4226 of the loop just to check N1 and N2 ranges. */
4227 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
4229 value_range_t vr1
= *(vr_value
[i1
]);
4231 /* If the range is VARYING or UNDEFINED, use the name itself. */
4232 if (vr1
.type
== VR_VARYING
|| vr1
.type
== VR_UNDEFINED
)
4234 vr1
.type
= VR_RANGE
;
4235 vr1
.min
= ssa_name (i1
);
4236 vr1
.max
= ssa_name (i1
);
4239 t
= retval
= NULL_TREE
;
4240 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
4244 value_range_t vr2
= *(vr_value
[i2
]);
4246 if (vr2
.type
== VR_VARYING
|| vr2
.type
== VR_UNDEFINED
)
4248 vr2
.type
= VR_RANGE
;
4249 vr2
.min
= ssa_name (i2
);
4250 vr2
.max
= ssa_name (i2
);
4253 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
4256 /* If we get different answers from different members
4257 of the equivalence set this check must be in a dead
4258 code region. Folding it to a trap representation
4259 would be correct here. For now just return don't-know. */
4263 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
4264 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
4270 used_strict_overflow
= 0;
4271 else if (used_strict_overflow
< 0)
4272 used_strict_overflow
= 1;
4278 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
4279 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
4280 if (used_strict_overflow
> 0)
4281 *strict_overflow_p
= true;
4286 /* None of the equivalent ranges are useful in computing this
4288 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
4289 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
4294 /* Given a conditional predicate COND, try to determine if COND yields
4295 true or false based on the value ranges of its operands. Return
4296 BOOLEAN_TRUE_NODE if the conditional always evaluates to true,
4297 BOOLEAN_FALSE_NODE if the conditional always evaluates to false, and,
4298 NULL if the conditional cannot be evaluated at compile time.
4300 If USE_EQUIV_P is true, the ranges of all the names equivalent with
4301 the operands in COND are used when trying to compute its value.
4302 This is only used during final substitution. During propagation,
4303 we only check the range of each variable and not its equivalents.
4305 Set *STRICT_OVERFLOW_P to indicate whether we relied on an overflow
4306 infinity to produce the result. */
4309 vrp_evaluate_conditional_warnv (tree cond
, bool use_equiv_p
,
4310 bool *strict_overflow_p
)
4312 gcc_assert (TREE_CODE (cond
) == SSA_NAME
4313 || TREE_CODE_CLASS (TREE_CODE (cond
)) == tcc_comparison
);
4315 if (TREE_CODE (cond
) == SSA_NAME
)
4321 retval
= compare_name_with_value (NE_EXPR
, cond
, boolean_false_node
,
4325 value_range_t
*vr
= get_value_range (cond
);
4326 retval
= compare_range_with_value (NE_EXPR
, vr
, boolean_false_node
,
4330 /* If COND has a known boolean range, return it. */
4334 /* Otherwise, if COND has a symbolic range of exactly one value,
4336 vr
= get_value_range (cond
);
4337 if (vr
->type
== VR_RANGE
&& vr
->min
== vr
->max
)
4342 tree op0
= TREE_OPERAND (cond
, 0);
4343 tree op1
= TREE_OPERAND (cond
, 1);
4345 /* We only deal with integral and pointer types. */
4346 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
4347 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
4352 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
4353 return compare_names (TREE_CODE (cond
), op0
, op1
,
4355 else if (TREE_CODE (op0
) == SSA_NAME
)
4356 return compare_name_with_value (TREE_CODE (cond
), op0
, op1
,
4358 else if (TREE_CODE (op1
) == SSA_NAME
)
4359 return (compare_name_with_value
4360 (swap_tree_comparison (TREE_CODE (cond
)), op1
, op0
,
4361 strict_overflow_p
));
4365 value_range_t
*vr0
, *vr1
;
4367 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
4368 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
4371 return compare_ranges (TREE_CODE (cond
), vr0
, vr1
,
4373 else if (vr0
&& vr1
== NULL
)
4374 return compare_range_with_value (TREE_CODE (cond
), vr0
, op1
,
4376 else if (vr0
== NULL
&& vr1
)
4377 return (compare_range_with_value
4378 (swap_tree_comparison (TREE_CODE (cond
)), vr1
, op0
,
4379 strict_overflow_p
));
4383 /* Anything else cannot be computed statically. */
4387 /* Given COND within STMT, try to simplify it based on value range
4388 information. Return NULL if the conditional can not be evaluated.
4389 The ranges of all the names equivalent with the operands in COND
4390 will be used when trying to compute the value. If the result is
4391 based on undefined signed overflow, issue a warning if
4395 vrp_evaluate_conditional (tree cond
, tree stmt
)
4401 ret
= vrp_evaluate_conditional_warnv (cond
, true, &sop
);
4405 enum warn_strict_overflow_code wc
;
4406 const char* warnmsg
;
4408 if (is_gimple_min_invariant (ret
))
4410 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
4411 warnmsg
= G_("assuming signed overflow does not occur when "
4412 "simplifying conditional to constant");
4416 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
4417 warnmsg
= G_("assuming signed overflow does not occur when "
4418 "simplifying conditional");
4421 if (issue_strict_overflow_warning (wc
))
4425 if (!EXPR_HAS_LOCATION (stmt
))
4426 locus
= input_location
;
4428 locus
= EXPR_LOCATION (stmt
);
4429 warning (OPT_Wstrict_overflow
, "%H%s", &locus
, warnmsg
);
4437 /* Visit conditional statement STMT. If we can determine which edge
4438 will be taken out of STMT's basic block, record it in
4439 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
4440 SSA_PROP_VARYING. */
4442 static enum ssa_prop_result
4443 vrp_visit_cond_stmt (tree stmt
, edge
*taken_edge_p
)
4448 *taken_edge_p
= NULL
;
4450 /* FIXME. Handle SWITCH_EXPRs. But first, the assert pass needs to
4451 add ASSERT_EXPRs for them. */
4452 if (TREE_CODE (stmt
) == SWITCH_EXPR
)
4453 return SSA_PROP_VARYING
;
4455 cond
= COND_EXPR_COND (stmt
);
4457 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4462 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
4463 print_generic_expr (dump_file
, cond
, 0);
4464 fprintf (dump_file
, "\nWith known ranges\n");
4466 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
4468 fprintf (dump_file
, "\t");
4469 print_generic_expr (dump_file
, use
, 0);
4470 fprintf (dump_file
, ": ");
4471 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
4474 fprintf (dump_file
, "\n");
4477 /* Compute the value of the predicate COND by checking the known
4478 ranges of each of its operands.
4480 Note that we cannot evaluate all the equivalent ranges here
4481 because those ranges may not yet be final and with the current
4482 propagation strategy, we cannot determine when the value ranges
4483 of the names in the equivalence set have changed.
4485 For instance, given the following code fragment
4489 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
4493 Assume that on the first visit to i_14, i_5 has the temporary
4494 range [8, 8] because the second argument to the PHI function is
4495 not yet executable. We derive the range ~[0, 0] for i_14 and the
4496 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
4497 the first time, since i_14 is equivalent to the range [8, 8], we
4498 determine that the predicate is always false.
4500 On the next round of propagation, i_13 is determined to be
4501 VARYING, which causes i_5 to drop down to VARYING. So, another
4502 visit to i_14 is scheduled. In this second visit, we compute the
4503 exact same range and equivalence set for i_14, namely ~[0, 0] and
4504 { i_5 }. But we did not have the previous range for i_5
4505 registered, so vrp_visit_assignment thinks that the range for
4506 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
4507 is not visited again, which stops propagation from visiting
4508 statements in the THEN clause of that if().
4510 To properly fix this we would need to keep the previous range
4511 value for the names in the equivalence set. This way we would've
4512 discovered that from one visit to the other i_5 changed from
4513 range [8, 8] to VR_VARYING.
4515 However, fixing this apparent limitation may not be worth the
4516 additional checking. Testing on several code bases (GCC, DLV,
4517 MICO, TRAMP3D and SPEC2000) showed that doing this results in
4518 4 more predicates folded in SPEC. */
4520 val
= vrp_evaluate_conditional_warnv (cond
, false, &sop
);
4524 *taken_edge_p
= find_taken_edge (bb_for_stmt (stmt
), val
);
4527 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4529 "\nIgnoring predicate evaluation because "
4530 "it assumes that signed overflow is undefined");
4535 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4537 fprintf (dump_file
, "\nPredicate evaluates to: ");
4538 if (val
== NULL_TREE
)
4539 fprintf (dump_file
, "DON'T KNOW\n");
4541 print_generic_stmt (dump_file
, val
, 0);
4544 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
4548 /* Evaluate statement STMT. If the statement produces a useful range,
4549 return SSA_PROP_INTERESTING and record the SSA name with the
4550 interesting range into *OUTPUT_P.
4552 If STMT is a conditional branch and we can determine its truth
4553 value, the taken edge is recorded in *TAKEN_EDGE_P.
4555 If STMT produces a varying value, return SSA_PROP_VARYING. */
4557 static enum ssa_prop_result
4558 vrp_visit_stmt (tree stmt
, edge
*taken_edge_p
, tree
*output_p
)
4564 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4566 fprintf (dump_file
, "\nVisiting statement:\n");
4567 print_generic_stmt (dump_file
, stmt
, dump_flags
);
4568 fprintf (dump_file
, "\n");
4571 ann
= stmt_ann (stmt
);
4572 if (TREE_CODE (stmt
) == MODIFY_EXPR
)
4574 tree rhs
= TREE_OPERAND (stmt
, 1);
4576 /* In general, assignments with virtual operands are not useful
4577 for deriving ranges, with the obvious exception of calls to
4578 builtin functions. */
4579 if ((TREE_CODE (rhs
) == CALL_EXPR
4580 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == ADDR_EXPR
4581 && DECL_P (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 0))
4582 && DECL_IS_BUILTIN (TREE_OPERAND (TREE_OPERAND (rhs
, 0), 0)))
4583 || ZERO_SSA_OPERANDS (stmt
, SSA_OP_ALL_VIRTUALS
))
4584 return vrp_visit_assignment (stmt
, output_p
);
4586 else if (TREE_CODE (stmt
) == COND_EXPR
|| TREE_CODE (stmt
) == SWITCH_EXPR
)
4587 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
4589 /* All other statements produce nothing of interest for VRP, so mark
4590 their outputs varying and prevent further simulation. */
4591 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
4592 set_value_range_to_varying (get_value_range (def
));
4594 return SSA_PROP_VARYING
;
4598 /* Meet operation for value ranges. Given two value ranges VR0 and
4599 VR1, store in VR0 the result of meeting VR0 and VR1.
4601 The meeting rules are as follows:
4603 1- If VR0 and VR1 have an empty intersection, set VR0 to VR_VARYING.
4605 2- If VR0 and VR1 have a non-empty intersection, set VR0 to the
4606 union of VR0 and VR1. */
4609 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
4611 if (vr0
->type
== VR_UNDEFINED
)
4613 copy_value_range (vr0
, vr1
);
4617 if (vr1
->type
== VR_UNDEFINED
)
4619 /* Nothing to do. VR0 already has the resulting range. */
4623 if (vr0
->type
== VR_VARYING
)
4625 /* Nothing to do. VR0 already has the resulting range. */
4629 if (vr1
->type
== VR_VARYING
)
4631 set_value_range_to_varying (vr0
);
4635 if (vr0
->type
== VR_RANGE
&& vr1
->type
== VR_RANGE
)
4637 /* If VR0 and VR1 have a non-empty intersection, compute the
4638 union of both ranges. */
4639 if (value_ranges_intersect_p (vr0
, vr1
))
4644 /* The lower limit of the new range is the minimum of the
4645 two ranges. If they cannot be compared, the result is
4647 cmp
= compare_values (vr0
->min
, vr1
->min
);
4648 if (cmp
== 0 || cmp
== 1)
4654 set_value_range_to_varying (vr0
);
4658 /* Similarly, the upper limit of the new range is the
4659 maximum of the two ranges. If they cannot be compared,
4660 the result is VARYING. */
4661 cmp
= compare_values (vr0
->max
, vr1
->max
);
4662 if (cmp
== 0 || cmp
== -1)
4668 set_value_range_to_varying (vr0
);
4672 /* Check for useless ranges. */
4673 if (INTEGRAL_TYPE_P (TREE_TYPE (min
))
4674 && ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
4675 && (vrp_val_is_max (max
) || is_overflow_infinity (max
))))
4677 set_value_range_to_varying (vr0
);
4681 /* The resulting set of equivalences is the intersection of
4683 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
4684 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
4685 else if (vr0
->equiv
&& !vr1
->equiv
)
4686 bitmap_clear (vr0
->equiv
);
4688 set_value_range (vr0
, vr0
->type
, min
, max
, vr0
->equiv
);
4693 else if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4695 /* Two anti-ranges meet only if they are both identical. */
4696 if (compare_values (vr0
->min
, vr1
->min
) == 0
4697 && compare_values (vr0
->max
, vr1
->max
) == 0
4698 && compare_values (vr0
->min
, vr0
->max
) == 0)
4700 /* The resulting set of equivalences is the intersection of
4702 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
4703 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
4704 else if (vr0
->equiv
&& !vr1
->equiv
)
4705 bitmap_clear (vr0
->equiv
);
4710 else if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4712 /* A numeric range [VAL1, VAL2] and an anti-range ~[VAL3, VAL4]
4713 meet only if the ranges have an empty intersection. The
4714 result of the meet operation is the anti-range. */
4715 if (!symbolic_range_p (vr0
)
4716 && !symbolic_range_p (vr1
)
4717 && !value_ranges_intersect_p (vr0
, vr1
))
4719 /* Copy most of VR1 into VR0. Don't copy VR1's equivalence
4720 set. We need to compute the intersection of the two
4721 equivalence sets. */
4722 if (vr1
->type
== VR_ANTI_RANGE
)
4723 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr0
->equiv
);
4725 /* The resulting set of equivalences is the intersection of
4727 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
4728 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
4729 else if (vr0
->equiv
&& !vr1
->equiv
)
4730 bitmap_clear (vr0
->equiv
);
4741 /* The two range VR0 and VR1 do not meet. Before giving up and
4742 setting the result to VARYING, see if we can at least derive a
4743 useful anti-range. FIXME, all this nonsense about distinguishing
4744 anti-ranges from ranges is necessary because of the odd
4745 semantics of range_includes_zero_p and friends. */
4746 if (!symbolic_range_p (vr0
)
4747 && ((vr0
->type
== VR_RANGE
&& !range_includes_zero_p (vr0
))
4748 || (vr0
->type
== VR_ANTI_RANGE
&& range_includes_zero_p (vr0
)))
4749 && !symbolic_range_p (vr1
)
4750 && ((vr1
->type
== VR_RANGE
&& !range_includes_zero_p (vr1
))
4751 || (vr1
->type
== VR_ANTI_RANGE
&& range_includes_zero_p (vr1
))))
4753 set_value_range_to_nonnull (vr0
, TREE_TYPE (vr0
->min
));
4755 /* Since this meet operation did not result from the meeting of
4756 two equivalent names, VR0 cannot have any equivalences. */
4758 bitmap_clear (vr0
->equiv
);
4761 set_value_range_to_varying (vr0
);
4765 /* Visit all arguments for PHI node PHI that flow through executable
4766 edges. If a valid value range can be derived from all the incoming
4767 value ranges, set a new range for the LHS of PHI. */
4769 static enum ssa_prop_result
4770 vrp_visit_phi_node (tree phi
)
4773 tree lhs
= PHI_RESULT (phi
);
4774 value_range_t
*lhs_vr
= get_value_range (lhs
);
4775 value_range_t vr_result
= { VR_UNDEFINED
, NULL_TREE
, NULL_TREE
, NULL
};
4777 copy_value_range (&vr_result
, lhs_vr
);
4779 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4781 fprintf (dump_file
, "\nVisiting PHI node: ");
4782 print_generic_expr (dump_file
, phi
, dump_flags
);
4785 for (i
= 0; i
< PHI_NUM_ARGS (phi
); i
++)
4787 edge e
= PHI_ARG_EDGE (phi
, i
);
4789 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4792 "\n Argument #%d (%d -> %d %sexecutable)\n",
4793 i
, e
->src
->index
, e
->dest
->index
,
4794 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
4797 if (e
->flags
& EDGE_EXECUTABLE
)
4799 tree arg
= PHI_ARG_DEF (phi
, i
);
4800 value_range_t vr_arg
;
4802 if (TREE_CODE (arg
) == SSA_NAME
)
4803 vr_arg
= *(get_value_range (arg
));
4806 if (is_overflow_infinity (arg
))
4808 arg
= copy_node (arg
);
4809 TREE_OVERFLOW (arg
) = 0;
4812 vr_arg
.type
= VR_RANGE
;
4815 vr_arg
.equiv
= NULL
;
4818 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4820 fprintf (dump_file
, "\t");
4821 print_generic_expr (dump_file
, arg
, dump_flags
);
4822 fprintf (dump_file
, "\n\tValue: ");
4823 dump_value_range (dump_file
, &vr_arg
);
4824 fprintf (dump_file
, "\n");
4827 vrp_meet (&vr_result
, &vr_arg
);
4829 if (vr_result
.type
== VR_VARYING
)
4834 if (vr_result
.type
== VR_VARYING
)
4837 /* To prevent infinite iterations in the algorithm, derive ranges
4838 when the new value is slightly bigger or smaller than the
4840 if (lhs_vr
->type
== VR_RANGE
&& vr_result
.type
== VR_RANGE
)
4842 if (!POINTER_TYPE_P (TREE_TYPE (lhs
)))
4844 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
4845 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
4847 /* If the new minimum is smaller or larger than the previous
4848 one, go all the way to -INF. In the first case, to avoid
4849 iterating millions of times to reach -INF, and in the
4850 other case to avoid infinite bouncing between different
4852 if (cmp_min
> 0 || cmp_min
< 0)
4854 /* If we will end up with a (-INF, +INF) range, set it
4856 if (vrp_val_is_max (vr_result
.max
))
4859 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
4860 || !vrp_var_may_overflow (lhs
, phi
))
4861 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
4862 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
4864 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
4869 /* Similarly, if the new maximum is smaller or larger than
4870 the previous one, go all the way to +INF. */
4871 if (cmp_max
< 0 || cmp_max
> 0)
4873 /* If we will end up with a (-INF, +INF) range, set it
4875 if (vrp_val_is_min (vr_result
.min
))
4878 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
4879 || !vrp_var_may_overflow (lhs
, phi
))
4880 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
4881 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
4883 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
4890 /* If the new range is different than the previous value, keep
4892 if (update_value_range (lhs
, &vr_result
))
4893 return SSA_PROP_INTERESTING
;
4895 /* Nothing changed, don't add outgoing edges. */
4896 return SSA_PROP_NOT_INTERESTING
;
4898 /* No match found. Set the LHS to VARYING. */
4900 set_value_range_to_varying (lhs_vr
);
4901 return SSA_PROP_VARYING
;
4904 /* Simplify a division or modulo operator to a right shift or
4905 bitwise and if the first operand is unsigned or is greater
4906 than zero and the second operand is an exact power of two. */
4909 simplify_div_or_mod_using_ranges (tree stmt
, tree rhs
, enum tree_code rhs_code
)
4912 tree op
= TREE_OPERAND (rhs
, 0);
4913 value_range_t
*vr
= get_value_range (TREE_OPERAND (rhs
, 0));
4915 if (TYPE_UNSIGNED (TREE_TYPE (op
)))
4917 val
= integer_one_node
;
4923 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
4927 && integer_onep (val
)
4928 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
4932 if (!EXPR_HAS_LOCATION (stmt
))
4933 locus
= input_location
;
4935 locus
= EXPR_LOCATION (stmt
);
4936 warning (OPT_Wstrict_overflow
,
4937 ("%Hassuming signed overflow does not occur when "
4938 "simplifying / or %% to >> or &"),
4943 if (val
&& integer_onep (val
))
4946 tree op0
= TREE_OPERAND (rhs
, 0);
4947 tree op1
= TREE_OPERAND (rhs
, 1);
4949 if (rhs_code
== TRUNC_DIV_EXPR
)
4951 t
= build_int_cst (NULL_TREE
, tree_log2 (op1
));
4952 t
= build2 (RSHIFT_EXPR
, TREE_TYPE (op0
), op0
, t
);
4956 t
= build_int_cst (TREE_TYPE (op1
), 1);
4957 t
= int_const_binop (MINUS_EXPR
, op1
, t
, 0);
4958 t
= fold_convert (TREE_TYPE (op0
), t
);
4959 t
= build2 (BIT_AND_EXPR
, TREE_TYPE (op0
), op0
, t
);
4962 TREE_OPERAND (stmt
, 1) = t
;
4967 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
4968 ABS_EXPR. If the operand is <= 0, then simplify the
4969 ABS_EXPR into a NEGATE_EXPR. */
4972 simplify_abs_using_ranges (tree stmt
, tree rhs
)
4975 tree op
= TREE_OPERAND (rhs
, 0);
4976 tree type
= TREE_TYPE (op
);
4977 value_range_t
*vr
= get_value_range (TREE_OPERAND (rhs
, 0));
4979 if (TYPE_UNSIGNED (type
))
4981 val
= integer_zero_node
;
4987 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
4991 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
4996 if (integer_zerop (val
))
4997 val
= integer_one_node
;
4998 else if (integer_onep (val
))
4999 val
= integer_zero_node
;
5004 && (integer_onep (val
) || integer_zerop (val
)))
5008 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
5012 if (!EXPR_HAS_LOCATION (stmt
))
5013 locus
= input_location
;
5015 locus
= EXPR_LOCATION (stmt
);
5016 warning (OPT_Wstrict_overflow
,
5017 ("%Hassuming signed overflow does not occur when "
5018 "simplifying abs (X) to X or -X"),
5022 if (integer_onep (val
))
5023 t
= build1 (NEGATE_EXPR
, TREE_TYPE (op
), op
);
5027 TREE_OPERAND (stmt
, 1) = t
;
5033 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
5034 a known value range VR.
5036 If there is one and only one value which will satisfy the
5037 conditional, then return that value. Else return NULL. */
5040 test_for_singularity (enum tree_code cond_code
, tree op0
,
5041 tree op1
, value_range_t
*vr
)
5046 /* Extract minimum/maximum values which satisfy the
5047 the conditional as it was written. */
5048 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
5050 /* This should not be negative infinity; there is no overflow
5052 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
5055 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
5057 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
5058 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
5060 TREE_NO_WARNING (max
) = 1;
5063 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
5065 /* This should not be positive infinity; there is no overflow
5067 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
5070 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
5072 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
5073 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
5075 TREE_NO_WARNING (min
) = 1;
5079 /* Now refine the minimum and maximum values using any
5080 value range information we have for op0. */
5083 if (compare_values (vr
->min
, min
) == -1)
5087 if (compare_values (vr
->max
, max
) == 1)
5092 /* If the new min/max values have converged to a single value,
5093 then there is only one value which can satisfy the condition,
5094 return that value. */
5095 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
5101 /* Simplify a conditional using a relational operator to an equality
5102 test if the range information indicates only one value can satisfy
5103 the original conditional. */
5106 simplify_cond_using_ranges (tree stmt
)
5108 tree cond
= COND_EXPR_COND (stmt
);
5109 tree op0
= TREE_OPERAND (cond
, 0);
5110 tree op1
= TREE_OPERAND (cond
, 1);
5111 enum tree_code cond_code
= TREE_CODE (cond
);
5113 if (cond_code
!= NE_EXPR
5114 && cond_code
!= EQ_EXPR
5115 && TREE_CODE (op0
) == SSA_NAME
5116 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
5117 && is_gimple_min_invariant (op1
))
5119 value_range_t
*vr
= get_value_range (op0
);
5121 /* If we have range information for OP0, then we might be
5122 able to simplify this conditional. */
5123 if (vr
->type
== VR_RANGE
)
5125 tree
new = test_for_singularity (cond_code
, op0
, op1
, vr
);
5131 fprintf (dump_file
, "Simplified relational ");
5132 print_generic_expr (dump_file
, cond
, 0);
5133 fprintf (dump_file
, " into ");
5136 COND_EXPR_COND (stmt
)
5137 = build2 (EQ_EXPR
, boolean_type_node
, op0
, new);
5142 print_generic_expr (dump_file
, COND_EXPR_COND (stmt
), 0);
5143 fprintf (dump_file
, "\n");
5149 /* Try again after inverting the condition. We only deal
5150 with integral types here, so no need to worry about
5151 issues with inverting FP comparisons. */
5152 cond_code
= invert_tree_comparison (cond_code
, false);
5153 new = test_for_singularity (cond_code
, op0
, op1
, vr
);
5159 fprintf (dump_file
, "Simplified relational ");
5160 print_generic_expr (dump_file
, cond
, 0);
5161 fprintf (dump_file
, " into ");
5164 COND_EXPR_COND (stmt
)
5165 = build2 (NE_EXPR
, boolean_type_node
, op0
, new);
5170 print_generic_expr (dump_file
, COND_EXPR_COND (stmt
), 0);
5171 fprintf (dump_file
, "\n");
5180 /* Simplify STMT using ranges if possible. */
5183 simplify_stmt_using_ranges (tree stmt
)
5185 if (TREE_CODE (stmt
) == MODIFY_EXPR
)
5187 tree rhs
= TREE_OPERAND (stmt
, 1);
5188 enum tree_code rhs_code
= TREE_CODE (rhs
);
5190 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
5191 and BIT_AND_EXPR respectively if the first operand is greater
5192 than zero and the second operand is an exact power of two. */
5193 if ((rhs_code
== TRUNC_DIV_EXPR
|| rhs_code
== TRUNC_MOD_EXPR
)
5194 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs
, 0)))
5195 && integer_pow2p (TREE_OPERAND (rhs
, 1)))
5196 simplify_div_or_mod_using_ranges (stmt
, rhs
, rhs_code
);
5198 /* Transform ABS (X) into X or -X as appropriate. */
5199 if (rhs_code
== ABS_EXPR
5200 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == SSA_NAME
5201 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs
, 0))))
5202 simplify_abs_using_ranges (stmt
, rhs
);
5204 else if (TREE_CODE (stmt
) == COND_EXPR
5205 && COMPARISON_CLASS_P (COND_EXPR_COND (stmt
)))
5207 simplify_cond_using_ranges (stmt
);
5211 /* Stack of dest,src equivalency pairs that need to be restored after
5212 each attempt to thread a block's incoming edge to an outgoing edge.
5214 A NULL entry is used to mark the end of pairs which need to be
5216 static VEC(tree
,heap
) *stack
;
5218 /* A trivial wrapper so that we can present the generic jump threading
5219 code with a simple API for simplifying statements. STMT is the
5220 statement we want to simplify, WITHIN_STMT provides the location
5221 for any overflow warnings. */
5224 simplify_stmt_for_jump_threading (tree stmt
, tree within_stmt
)
5226 /* We only use VRP information to simplify conditionals. This is
5227 overly conservative, but it's unclear if doing more would be
5228 worth the compile time cost. */
5229 if (TREE_CODE (stmt
) != COND_EXPR
)
5232 return vrp_evaluate_conditional (COND_EXPR_COND (stmt
), within_stmt
);
5235 /* Blocks which have more than one predecessor and more than
5236 one successor present jump threading opportunities. ie,
5237 when the block is reached from a specific predecessor, we
5238 may be able to determine which of the outgoing edges will
5239 be traversed. When this optimization applies, we are able
5240 to avoid conditionals at runtime and we may expose secondary
5241 optimization opportunities.
5243 This routine is effectively a driver for the generic jump
5244 threading code. It basically just presents the generic code
5245 with edges that may be suitable for jump threading.
5247 Unlike DOM, we do not iterate VRP if jump threading was successful.
5248 While iterating may expose new opportunities for VRP, it is expected
5249 those opportunities would be very limited and the compile time cost
5250 to expose those opportunities would be significant.
5252 As jump threading opportunities are discovered, they are registered
5253 for later realization. */
5256 identify_jump_threads (void)
5261 /* Ugh. When substituting values earlier in this pass we can
5262 wipe the dominance information. So rebuild the dominator
5263 information as we need it within the jump threading code. */
5264 calculate_dominance_info (CDI_DOMINATORS
);
5266 /* We do not allow VRP information to be used for jump threading
5267 across a back edge in the CFG. Otherwise it becomes too
5268 difficult to avoid eliminating loop exit tests. Of course
5269 EDGE_DFS_BACK is not accurate at this time so we have to
5271 mark_dfs_back_edges ();
5273 /* Allocate our unwinder stack to unwind any temporary equivalences
5274 that might be recorded. */
5275 stack
= VEC_alloc (tree
, heap
, 20);
5277 /* To avoid lots of silly node creation, we create a single
5278 conditional and just modify it in-place when attempting to
5280 dummy
= build2 (EQ_EXPR
, boolean_type_node
, NULL
, NULL
);
5281 dummy
= build3 (COND_EXPR
, void_type_node
, dummy
, NULL
, NULL
);
5283 /* Walk through all the blocks finding those which present a
5284 potential jump threading opportunity. We could set this up
5285 as a dominator walker and record data during the walk, but
5286 I doubt it's worth the effort for the classes of jump
5287 threading opportunities we are trying to identify at this
5288 point in compilation. */
5293 /* If the generic jump threading code does not find this block
5294 interesting, then there is nothing to do. */
5295 if (! potentially_threadable_block (bb
))
5298 /* We only care about blocks ending in a COND_EXPR. While there
5299 may be some value in handling SWITCH_EXPR here, I doubt it's
5300 terribly important. */
5301 last
= bsi_stmt (bsi_last (bb
));
5302 if (TREE_CODE (last
) != COND_EXPR
)
5305 /* We're basically looking for any kind of conditional with
5306 integral type arguments. */
5307 cond
= COND_EXPR_COND (last
);
5308 if ((TREE_CODE (cond
) == SSA_NAME
5309 && INTEGRAL_TYPE_P (TREE_TYPE (cond
)))
5310 || (COMPARISON_CLASS_P (cond
)
5311 && TREE_CODE (TREE_OPERAND (cond
, 0)) == SSA_NAME
5312 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond
, 0)))
5313 && (TREE_CODE (TREE_OPERAND (cond
, 1)) == SSA_NAME
5314 || is_gimple_min_invariant (TREE_OPERAND (cond
, 1)))
5315 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (cond
, 1)))))
5320 /* We've got a block with multiple predecessors and multiple
5321 successors which also ends in a suitable conditional. For
5322 each predecessor, see if we can thread it to a specific
5324 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5326 /* Do not thread across back edges or abnormal edges
5328 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
5331 thread_across_edge (dummy
, e
, true,
5333 simplify_stmt_for_jump_threading
);
5338 /* We do not actually update the CFG or SSA graphs at this point as
5339 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
5340 handle ASSERT_EXPRs gracefully. */
5343 /* We identified all the jump threading opportunities earlier, but could
5344 not transform the CFG at that time. This routine transforms the
5345 CFG and arranges for the dominator tree to be rebuilt if necessary.
5347 Note the SSA graph update will occur during the normal TODO
5348 processing by the pass manager. */
5350 finalize_jump_threads (void)
5352 bool cfg_altered
= false;
5353 cfg_altered
= thread_through_all_blocks ();
5355 /* If we threaded jumps, then we need to recompute the dominance
5356 information, to safely do that we must clean up the CFG first. */
5359 free_dominance_info (CDI_DOMINATORS
);
5360 cleanup_tree_cfg ();
5361 calculate_dominance_info (CDI_DOMINATORS
);
5363 VEC_free (tree
, heap
, stack
);
5367 /* Traverse all the blocks folding conditionals with known ranges. */
5373 prop_value_t
*single_val_range
;
5374 bool do_value_subst_p
;
5378 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
5379 dump_all_value_ranges (dump_file
);
5380 fprintf (dump_file
, "\n");
5383 /* We may have ended with ranges that have exactly one value. Those
5384 values can be substituted as any other copy/const propagated
5385 value using substitute_and_fold. */
5386 single_val_range
= XNEWVEC (prop_value_t
, num_ssa_names
);
5387 memset (single_val_range
, 0, num_ssa_names
* sizeof (*single_val_range
));
5389 do_value_subst_p
= false;
5390 for (i
= 0; i
< num_ssa_names
; i
++)
5392 && vr_value
[i
]->type
== VR_RANGE
5393 && vr_value
[i
]->min
== vr_value
[i
]->max
)
5395 single_val_range
[i
].value
= vr_value
[i
]->min
;
5396 do_value_subst_p
= true;
5399 if (!do_value_subst_p
)
5401 /* We found no single-valued ranges, don't waste time trying to
5402 do single value substitution in substitute_and_fold. */
5403 free (single_val_range
);
5404 single_val_range
= NULL
;
5407 substitute_and_fold (single_val_range
, true);
5409 /* We must identify jump threading opportunities before we release
5410 the datastructures built by VRP. */
5411 identify_jump_threads ();
5413 /* Free allocated memory. */
5414 for (i
= 0; i
< num_ssa_names
; i
++)
5417 BITMAP_FREE (vr_value
[i
]->equiv
);
5421 free (single_val_range
);
5424 /* So that we can distinguish between VRP data being available
5425 and not available. */
5430 /* Main entry point to VRP (Value Range Propagation). This pass is
5431 loosely based on J. R. C. Patterson, ``Accurate Static Branch
5432 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
5433 Programming Language Design and Implementation, pp. 67-78, 1995.
5434 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
5436 This is essentially an SSA-CCP pass modified to deal with ranges
5437 instead of constants.
5439 While propagating ranges, we may find that two or more SSA name
5440 have equivalent, though distinct ranges. For instance,
5443 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
5445 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
5449 In the code above, pointer p_5 has range [q_2, q_2], but from the
5450 code we can also determine that p_5 cannot be NULL and, if q_2 had
5451 a non-varying range, p_5's range should also be compatible with it.
5453 These equivalences are created by two expressions: ASSERT_EXPR and
5454 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
5455 result of another assertion, then we can use the fact that p_5 and
5456 p_4 are equivalent when evaluating p_5's range.
5458 Together with value ranges, we also propagate these equivalences
5459 between names so that we can take advantage of information from
5460 multiple ranges when doing final replacement. Note that this
5461 equivalency relation is transitive but not symmetric.
5463 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
5464 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
5465 in contexts where that assertion does not hold (e.g., in line 6).
5467 TODO, the main difference between this pass and Patterson's is that
5468 we do not propagate edge probabilities. We only compute whether
5469 edges can be taken or not. That is, instead of having a spectrum
5470 of jump probabilities between 0 and 1, we only deal with 0, 1 and
5471 DON'T KNOW. In the future, it may be worthwhile to propagate
5472 probabilities to aid branch prediction. */
5477 insert_range_assertions ();
5479 current_loops
= loop_optimizer_init (LOOPS_NORMAL
);
5481 scev_initialize (current_loops
);
5484 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
5490 loop_optimizer_finalize (current_loops
);
5491 current_loops
= NULL
;
5494 /* ASSERT_EXPRs must be removed before finalizing jump threads
5495 as finalizing jump threads calls the CFG cleanup code which
5496 does not properly handle ASSERT_EXPRs. */
5497 remove_range_assertions ();
5499 /* If we exposed any new variables, go ahead and put them into
5500 SSA form now, before we handle jump threading. This simplifies
5501 interactions between rewriting of _DECL nodes into SSA form
5502 and rewriting SSA_NAME nodes into SSA form after block
5503 duplication and CFG manipulation. */
5504 update_ssa (TODO_update_ssa
);
5506 finalize_jump_threads ();
5513 return flag_tree_vrp
!= 0;
5516 struct tree_opt_pass pass_vrp
=
5519 gate_vrp
, /* gate */
5520 execute_vrp
, /* execute */
5523 0, /* static_pass_number */
5524 TV_TREE_VRP
, /* tv_id */
5525 PROP_ssa
| PROP_alias
, /* properties_required */
5526 0, /* properties_provided */
5527 PROP_smt_usage
, /* properties_destroyed */
5528 0, /* todo_flags_start */
5534 | TODO_update_smt_usage
, /* todo_flags_finish */