1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2013 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "basic-block.h"
30 #include "gimple-ssa.h"
32 #include "tree-phinodes.h"
33 #include "ssa-iterators.h"
34 #include "tree-ssanames.h"
35 #include "tree-ssa-loop-manip.h"
36 #include "tree-ssa-loop-niter.h"
37 #include "tree-ssa-loop.h"
38 #include "tree-into-ssa.h"
40 #include "tree-pass.h"
41 #include "tree-dump.h"
42 #include "gimple-pretty-print.h"
43 #include "diagnostic-core.h"
46 #include "tree-scalar-evolution.h"
47 #include "tree-ssa-propagate.h"
48 #include "tree-chrec.h"
49 #include "tree-ssa-threadupdate.h"
52 #include "tree-ssa-threadedge.h"
56 /* Range of values that can be associated with an SSA_NAME after VRP
60 /* Lattice value represented by this range. */
61 enum value_range_type type
;
63 /* Minimum and maximum values represented by this range. These
64 values should be interpreted as follows:
66 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
69 - If TYPE == VR_RANGE then MIN holds the minimum value and
70 MAX holds the maximum value of the range [MIN, MAX].
72 - If TYPE == ANTI_RANGE the variable is known to NOT
73 take any values in the range [MIN, MAX]. */
77 /* Set of SSA names whose value ranges are equivalent to this one.
78 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
82 typedef struct value_range_d value_range_t
;
84 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
86 /* Set of SSA names found live during the RPO traversal of the function
87 for still active basic-blocks. */
90 /* Return true if the SSA name NAME is live on the edge E. */
93 live_on_edge (edge e
, tree name
)
95 return (live
[e
->dest
->index
]
96 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
99 /* Local functions. */
100 static int compare_values (tree val1
, tree val2
);
101 static int compare_values_warnv (tree val1
, tree val2
, bool *);
102 static void vrp_meet (value_range_t
*, value_range_t
*);
103 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
104 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
105 tree
, tree
, bool, bool *,
108 /* Location information for ASSERT_EXPRs. Each instance of this
109 structure describes an ASSERT_EXPR for an SSA name. Since a single
110 SSA name may have more than one assertion associated with it, these
111 locations are kept in a linked list attached to the corresponding
113 struct assert_locus_d
115 /* Basic block where the assertion would be inserted. */
118 /* Some assertions need to be inserted on an edge (e.g., assertions
119 generated by COND_EXPRs). In those cases, BB will be NULL. */
122 /* Pointer to the statement that generated this assertion. */
123 gimple_stmt_iterator si
;
125 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
126 enum tree_code comp_code
;
128 /* Value being compared against. */
131 /* Expression to compare. */
134 /* Next node in the linked list. */
135 struct assert_locus_d
*next
;
138 typedef struct assert_locus_d
*assert_locus_t
;
140 /* If bit I is present, it means that SSA name N_i has a list of
141 assertions that should be inserted in the IL. */
142 static bitmap need_assert_for
;
144 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
145 holds a list of ASSERT_LOCUS_T nodes that describe where
146 ASSERT_EXPRs for SSA name N_I should be inserted. */
147 static assert_locus_t
*asserts_for
;
149 /* Value range array. After propagation, VR_VALUE[I] holds the range
150 of values that SSA name N_I may take. */
151 static unsigned num_vr_values
;
152 static value_range_t
**vr_value
;
153 static bool values_propagated
;
155 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
156 number of executable edges we saw the last time we visited the
158 static int *vr_phi_edge_counts
;
165 static vec
<edge
> to_remove_edges
;
166 static vec
<switch_update
> to_update_switch_stmts
;
169 /* Return the maximum value for TYPE. */
172 vrp_val_max (const_tree type
)
174 if (!INTEGRAL_TYPE_P (type
))
177 return TYPE_MAX_VALUE (type
);
180 /* Return the minimum value for TYPE. */
183 vrp_val_min (const_tree type
)
185 if (!INTEGRAL_TYPE_P (type
))
188 return TYPE_MIN_VALUE (type
);
191 /* Return whether VAL is equal to the maximum value of its type. This
192 will be true for a positive overflow infinity. We can't do a
193 simple equality comparison with TYPE_MAX_VALUE because C typedefs
194 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
195 to the integer constant with the same value in the type. */
198 vrp_val_is_max (const_tree val
)
200 tree type_max
= vrp_val_max (TREE_TYPE (val
));
201 return (val
== type_max
202 || (type_max
!= NULL_TREE
203 && operand_equal_p (val
, type_max
, 0)));
206 /* Return whether VAL is equal to the minimum value of its type. This
207 will be true for a negative overflow infinity. */
210 vrp_val_is_min (const_tree val
)
212 tree type_min
= vrp_val_min (TREE_TYPE (val
));
213 return (val
== type_min
214 || (type_min
!= NULL_TREE
215 && operand_equal_p (val
, type_min
, 0)));
219 /* Return whether TYPE should use an overflow infinity distinct from
220 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
221 represent a signed overflow during VRP computations. An infinity
222 is distinct from a half-range, which will go from some number to
223 TYPE_{MIN,MAX}_VALUE. */
226 needs_overflow_infinity (const_tree type
)
228 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
231 /* Return whether TYPE can support our overflow infinity
232 representation: we use the TREE_OVERFLOW flag, which only exists
233 for constants. If TYPE doesn't support this, we don't optimize
234 cases which would require signed overflow--we drop them to
238 supports_overflow_infinity (const_tree type
)
240 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
241 #ifdef ENABLE_CHECKING
242 gcc_assert (needs_overflow_infinity (type
));
244 return (min
!= NULL_TREE
245 && CONSTANT_CLASS_P (min
)
247 && CONSTANT_CLASS_P (max
));
250 /* VAL is the maximum or minimum value of a type. Return a
251 corresponding overflow infinity. */
254 make_overflow_infinity (tree val
)
256 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
257 val
= copy_node (val
);
258 TREE_OVERFLOW (val
) = 1;
262 /* Return a negative overflow infinity for TYPE. */
265 negative_overflow_infinity (tree type
)
267 gcc_checking_assert (supports_overflow_infinity (type
));
268 return make_overflow_infinity (vrp_val_min (type
));
271 /* Return a positive overflow infinity for TYPE. */
274 positive_overflow_infinity (tree type
)
276 gcc_checking_assert (supports_overflow_infinity (type
));
277 return make_overflow_infinity (vrp_val_max (type
));
280 /* Return whether VAL is a negative overflow infinity. */
283 is_negative_overflow_infinity (const_tree val
)
285 return (needs_overflow_infinity (TREE_TYPE (val
))
286 && CONSTANT_CLASS_P (val
)
287 && TREE_OVERFLOW (val
)
288 && vrp_val_is_min (val
));
291 /* Return whether VAL is a positive overflow infinity. */
294 is_positive_overflow_infinity (const_tree val
)
296 return (needs_overflow_infinity (TREE_TYPE (val
))
297 && CONSTANT_CLASS_P (val
)
298 && TREE_OVERFLOW (val
)
299 && vrp_val_is_max (val
));
302 /* Return whether VAL is a positive or negative overflow infinity. */
305 is_overflow_infinity (const_tree val
)
307 return (needs_overflow_infinity (TREE_TYPE (val
))
308 && CONSTANT_CLASS_P (val
)
309 && TREE_OVERFLOW (val
)
310 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
313 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
316 stmt_overflow_infinity (gimple stmt
)
318 if (is_gimple_assign (stmt
)
319 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
321 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
325 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
326 the same value with TREE_OVERFLOW clear. This can be used to avoid
327 confusing a regular value with an overflow value. */
330 avoid_overflow_infinity (tree val
)
332 if (!is_overflow_infinity (val
))
335 if (vrp_val_is_max (val
))
336 return vrp_val_max (TREE_TYPE (val
));
339 gcc_checking_assert (vrp_val_is_min (val
));
340 return vrp_val_min (TREE_TYPE (val
));
345 /* Return true if ARG is marked with the nonnull attribute in the
346 current function signature. */
349 nonnull_arg_p (const_tree arg
)
351 tree t
, attrs
, fntype
;
352 unsigned HOST_WIDE_INT arg_num
;
354 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
356 /* The static chain decl is always non null. */
357 if (arg
== cfun
->static_chain_decl
)
360 fntype
= TREE_TYPE (current_function_decl
);
361 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
363 attrs
= lookup_attribute ("nonnull", attrs
);
365 /* If "nonnull" wasn't specified, we know nothing about the argument. */
366 if (attrs
== NULL_TREE
)
369 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
370 if (TREE_VALUE (attrs
) == NULL_TREE
)
373 /* Get the position number for ARG in the function signature. */
374 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
376 t
= DECL_CHAIN (t
), arg_num
++)
382 gcc_assert (t
== arg
);
384 /* Now see if ARG_NUM is mentioned in the nonnull list. */
385 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
387 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
396 /* Set value range VR to VR_UNDEFINED. */
399 set_value_range_to_undefined (value_range_t
*vr
)
401 vr
->type
= VR_UNDEFINED
;
402 vr
->min
= vr
->max
= NULL_TREE
;
404 bitmap_clear (vr
->equiv
);
408 /* Set value range VR to VR_VARYING. */
411 set_value_range_to_varying (value_range_t
*vr
)
413 vr
->type
= VR_VARYING
;
414 vr
->min
= vr
->max
= NULL_TREE
;
416 bitmap_clear (vr
->equiv
);
420 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
423 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
424 tree max
, bitmap equiv
)
426 #if defined ENABLE_CHECKING
427 /* Check the validity of the range. */
428 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
432 gcc_assert (min
&& max
);
434 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
435 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
437 cmp
= compare_values (min
, max
);
438 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
440 if (needs_overflow_infinity (TREE_TYPE (min
)))
441 gcc_assert (!is_overflow_infinity (min
)
442 || !is_overflow_infinity (max
));
445 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
446 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
448 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
449 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
456 /* Since updating the equivalence set involves deep copying the
457 bitmaps, only do it if absolutely necessary. */
458 if (vr
->equiv
== NULL
460 vr
->equiv
= BITMAP_ALLOC (NULL
);
462 if (equiv
!= vr
->equiv
)
464 if (equiv
&& !bitmap_empty_p (equiv
))
465 bitmap_copy (vr
->equiv
, equiv
);
467 bitmap_clear (vr
->equiv
);
472 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
473 This means adjusting T, MIN and MAX representing the case of a
474 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
475 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
476 In corner cases where MAX+1 or MIN-1 wraps this will fall back
478 This routine exists to ease canonicalization in the case where we
479 extract ranges from var + CST op limit. */
482 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
483 tree min
, tree max
, bitmap equiv
)
485 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
486 if (t
== VR_UNDEFINED
)
488 set_value_range_to_undefined (vr
);
491 else if (t
== VR_VARYING
)
493 set_value_range_to_varying (vr
);
497 /* Nothing to canonicalize for symbolic ranges. */
498 if (TREE_CODE (min
) != INTEGER_CST
499 || TREE_CODE (max
) != INTEGER_CST
)
501 set_value_range (vr
, t
, min
, max
, equiv
);
505 /* Wrong order for min and max, to swap them and the VR type we need
507 if (tree_int_cst_lt (max
, min
))
511 /* For one bit precision if max < min, then the swapped
512 range covers all values, so for VR_RANGE it is varying and
513 for VR_ANTI_RANGE empty range, so drop to varying as well. */
514 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
516 set_value_range_to_varying (vr
);
520 one
= build_int_cst (TREE_TYPE (min
), 1);
521 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
522 max
= int_const_binop (MINUS_EXPR
, min
, one
);
525 /* There's one corner case, if we had [C+1, C] before we now have
526 that again. But this represents an empty value range, so drop
527 to varying in this case. */
528 if (tree_int_cst_lt (max
, min
))
530 set_value_range_to_varying (vr
);
534 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
537 /* Anti-ranges that can be represented as ranges should be so. */
538 if (t
== VR_ANTI_RANGE
)
540 bool is_min
= vrp_val_is_min (min
);
541 bool is_max
= vrp_val_is_max (max
);
543 if (is_min
&& is_max
)
545 /* We cannot deal with empty ranges, drop to varying.
546 ??? This could be VR_UNDEFINED instead. */
547 set_value_range_to_varying (vr
);
550 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
551 && (is_min
|| is_max
))
553 /* Non-empty boolean ranges can always be represented
554 as a singleton range. */
556 min
= max
= vrp_val_max (TREE_TYPE (min
));
558 min
= max
= vrp_val_min (TREE_TYPE (min
));
562 /* As a special exception preserve non-null ranges. */
563 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
564 && integer_zerop (max
)))
566 tree one
= build_int_cst (TREE_TYPE (max
), 1);
567 min
= int_const_binop (PLUS_EXPR
, max
, one
);
568 max
= vrp_val_max (TREE_TYPE (max
));
573 tree one
= build_int_cst (TREE_TYPE (min
), 1);
574 max
= int_const_binop (MINUS_EXPR
, min
, one
);
575 min
= vrp_val_min (TREE_TYPE (min
));
580 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
581 if (needs_overflow_infinity (TREE_TYPE (min
))
582 && is_overflow_infinity (min
)
583 && is_overflow_infinity (max
))
585 set_value_range_to_varying (vr
);
589 set_value_range (vr
, t
, min
, max
, equiv
);
592 /* Copy value range FROM into value range TO. */
595 copy_value_range (value_range_t
*to
, value_range_t
*from
)
597 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
600 /* Set value range VR to a single value. This function is only called
601 with values we get from statements, and exists to clear the
602 TREE_OVERFLOW flag so that we don't think we have an overflow
603 infinity when we shouldn't. */
606 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
608 gcc_assert (is_gimple_min_invariant (val
));
609 val
= avoid_overflow_infinity (val
);
610 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
613 /* Set value range VR to a non-negative range of type TYPE.
614 OVERFLOW_INFINITY indicates whether to use an overflow infinity
615 rather than TYPE_MAX_VALUE; this should be true if we determine
616 that the range is nonnegative based on the assumption that signed
617 overflow does not occur. */
620 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
621 bool overflow_infinity
)
625 if (overflow_infinity
&& !supports_overflow_infinity (type
))
627 set_value_range_to_varying (vr
);
631 zero
= build_int_cst (type
, 0);
632 set_value_range (vr
, VR_RANGE
, zero
,
634 ? positive_overflow_infinity (type
)
635 : TYPE_MAX_VALUE (type
)),
639 /* Set value range VR to a non-NULL range of type TYPE. */
642 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
644 tree zero
= build_int_cst (type
, 0);
645 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
649 /* Set value range VR to a NULL range of type TYPE. */
652 set_value_range_to_null (value_range_t
*vr
, tree type
)
654 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
658 /* Set value range VR to a range of a truthvalue of type TYPE. */
661 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
663 if (TYPE_PRECISION (type
) == 1)
664 set_value_range_to_varying (vr
);
666 set_value_range (vr
, VR_RANGE
,
667 build_int_cst (type
, 0), build_int_cst (type
, 1),
672 /* If abs (min) < abs (max), set VR to [-max, max], if
673 abs (min) >= abs (max), set VR to [-min, min]. */
676 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
680 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
681 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
682 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
683 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
684 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
685 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
686 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
688 set_value_range_to_varying (vr
);
691 cmp
= compare_values (min
, max
);
693 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
694 else if (cmp
== 0 || cmp
== 1)
697 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
701 set_value_range_to_varying (vr
);
704 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
708 /* Return value range information for VAR.
710 If we have no values ranges recorded (ie, VRP is not running), then
711 return NULL. Otherwise create an empty range if none existed for VAR. */
713 static value_range_t
*
714 get_value_range (const_tree var
)
716 static const struct value_range_d vr_const_varying
717 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
720 unsigned ver
= SSA_NAME_VERSION (var
);
722 /* If we have no recorded ranges, then return NULL. */
726 /* If we query the range for a new SSA name return an unmodifiable VARYING.
727 We should get here at most from the substitute-and-fold stage which
728 will never try to change values. */
729 if (ver
>= num_vr_values
)
730 return CONST_CAST (value_range_t
*, &vr_const_varying
);
736 /* After propagation finished do not allocate new value-ranges. */
737 if (values_propagated
)
738 return CONST_CAST (value_range_t
*, &vr_const_varying
);
740 /* Create a default value range. */
741 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
743 /* Defer allocating the equivalence set. */
746 /* If VAR is a default definition of a parameter, the variable can
747 take any value in VAR's type. */
748 if (SSA_NAME_IS_DEFAULT_DEF (var
))
750 sym
= SSA_NAME_VAR (var
);
751 if (TREE_CODE (sym
) == PARM_DECL
)
753 /* Try to use the "nonnull" attribute to create ~[0, 0]
754 anti-ranges for pointers. Note that this is only valid with
755 default definitions of PARM_DECLs. */
756 if (POINTER_TYPE_P (TREE_TYPE (sym
))
757 && nonnull_arg_p (sym
))
758 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
760 set_value_range_to_varying (vr
);
762 else if (TREE_CODE (sym
) == RESULT_DECL
763 && DECL_BY_REFERENCE (sym
))
764 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
770 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
773 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
777 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
779 if (is_overflow_infinity (val1
))
780 return is_overflow_infinity (val2
);
784 /* Return true, if the bitmaps B1 and B2 are equal. */
787 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
790 || ((!b1
|| bitmap_empty_p (b1
))
791 && (!b2
|| bitmap_empty_p (b2
)))
793 && bitmap_equal_p (b1
, b2
)));
796 /* Update the value range and equivalence set for variable VAR to
797 NEW_VR. Return true if NEW_VR is different from VAR's previous
800 NOTE: This function assumes that NEW_VR is a temporary value range
801 object created for the sole purpose of updating VAR's range. The
802 storage used by the equivalence set from NEW_VR will be freed by
803 this function. Do not call update_value_range when NEW_VR
804 is the range object associated with another SSA name. */
807 update_value_range (const_tree var
, value_range_t
*new_vr
)
809 value_range_t
*old_vr
;
812 /* Update the value range, if necessary. */
813 old_vr
= get_value_range (var
);
814 is_new
= old_vr
->type
!= new_vr
->type
815 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
816 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
817 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
821 /* Do not allow transitions up the lattice. The following
822 is slightly more awkward than just new_vr->type < old_vr->type
823 because VR_RANGE and VR_ANTI_RANGE need to be considered
824 the same. We may not have is_new when transitioning to
825 UNDEFINED or from VARYING. */
826 if (new_vr
->type
== VR_UNDEFINED
827 || old_vr
->type
== VR_VARYING
)
828 set_value_range_to_varying (old_vr
);
830 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
834 BITMAP_FREE (new_vr
->equiv
);
840 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
841 point where equivalence processing can be turned on/off. */
844 add_equivalence (bitmap
*equiv
, const_tree var
)
846 unsigned ver
= SSA_NAME_VERSION (var
);
847 value_range_t
*vr
= vr_value
[ver
];
850 *equiv
= BITMAP_ALLOC (NULL
);
851 bitmap_set_bit (*equiv
, ver
);
853 bitmap_ior_into (*equiv
, vr
->equiv
);
857 /* Return true if VR is ~[0, 0]. */
860 range_is_nonnull (value_range_t
*vr
)
862 return vr
->type
== VR_ANTI_RANGE
863 && integer_zerop (vr
->min
)
864 && integer_zerop (vr
->max
);
868 /* Return true if VR is [0, 0]. */
871 range_is_null (value_range_t
*vr
)
873 return vr
->type
== VR_RANGE
874 && integer_zerop (vr
->min
)
875 && integer_zerop (vr
->max
);
878 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
882 range_int_cst_p (value_range_t
*vr
)
884 return (vr
->type
== VR_RANGE
885 && TREE_CODE (vr
->max
) == INTEGER_CST
886 && TREE_CODE (vr
->min
) == INTEGER_CST
);
889 /* Return true if VR is a INTEGER_CST singleton. */
892 range_int_cst_singleton_p (value_range_t
*vr
)
894 return (range_int_cst_p (vr
)
895 && !is_overflow_infinity (vr
->min
)
896 && !is_overflow_infinity (vr
->max
)
897 && tree_int_cst_equal (vr
->min
, vr
->max
));
900 /* Return true if value range VR involves at least one symbol. */
903 symbolic_range_p (value_range_t
*vr
)
905 return (!is_gimple_min_invariant (vr
->min
)
906 || !is_gimple_min_invariant (vr
->max
));
909 /* Return true if value range VR uses an overflow infinity. */
912 overflow_infinity_range_p (value_range_t
*vr
)
914 return (vr
->type
== VR_RANGE
915 && (is_overflow_infinity (vr
->min
)
916 || is_overflow_infinity (vr
->max
)));
919 /* Return false if we can not make a valid comparison based on VR;
920 this will be the case if it uses an overflow infinity and overflow
921 is not undefined (i.e., -fno-strict-overflow is in effect).
922 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
923 uses an overflow infinity. */
926 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
928 gcc_assert (vr
->type
== VR_RANGE
);
929 if (is_overflow_infinity (vr
->min
))
931 *strict_overflow_p
= true;
932 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
935 if (is_overflow_infinity (vr
->max
))
937 *strict_overflow_p
= true;
938 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
945 /* Return true if the result of assignment STMT is know to be non-negative.
946 If the return value is based on the assumption that signed overflow is
947 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
948 *STRICT_OVERFLOW_P.*/
951 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
953 enum tree_code code
= gimple_assign_rhs_code (stmt
);
954 switch (get_gimple_rhs_class (code
))
956 case GIMPLE_UNARY_RHS
:
957 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
958 gimple_expr_type (stmt
),
959 gimple_assign_rhs1 (stmt
),
961 case GIMPLE_BINARY_RHS
:
962 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
963 gimple_expr_type (stmt
),
964 gimple_assign_rhs1 (stmt
),
965 gimple_assign_rhs2 (stmt
),
967 case GIMPLE_TERNARY_RHS
:
969 case GIMPLE_SINGLE_RHS
:
970 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
972 case GIMPLE_INVALID_RHS
:
979 /* Return true if return value of call STMT is know to be non-negative.
980 If the return value is based on the assumption that signed overflow is
981 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
982 *STRICT_OVERFLOW_P.*/
985 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
987 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
988 gimple_call_arg (stmt
, 0) : NULL_TREE
;
989 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
990 gimple_call_arg (stmt
, 1) : NULL_TREE
;
992 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
993 gimple_call_fndecl (stmt
),
999 /* Return true if STMT is know to to compute a non-negative value.
1000 If the return value is based on the assumption that signed overflow is
1001 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1002 *STRICT_OVERFLOW_P.*/
1005 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1007 switch (gimple_code (stmt
))
1010 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1012 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1018 /* Return true if the result of assignment STMT is know to be non-zero.
1019 If the return value is based on the assumption that signed overflow is
1020 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1021 *STRICT_OVERFLOW_P.*/
1024 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1026 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1027 switch (get_gimple_rhs_class (code
))
1029 case GIMPLE_UNARY_RHS
:
1030 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1031 gimple_expr_type (stmt
),
1032 gimple_assign_rhs1 (stmt
),
1034 case GIMPLE_BINARY_RHS
:
1035 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1036 gimple_expr_type (stmt
),
1037 gimple_assign_rhs1 (stmt
),
1038 gimple_assign_rhs2 (stmt
),
1040 case GIMPLE_TERNARY_RHS
:
1042 case GIMPLE_SINGLE_RHS
:
1043 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1045 case GIMPLE_INVALID_RHS
:
1052 /* Return true if STMT is known to compute a non-zero value.
1053 If the return value is based on the assumption that signed overflow is
1054 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1055 *STRICT_OVERFLOW_P.*/
1058 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1060 switch (gimple_code (stmt
))
1063 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1066 tree fndecl
= gimple_call_fndecl (stmt
);
1067 if (!fndecl
) return false;
1068 if (flag_delete_null_pointer_checks
&& !flag_check_new
1069 && DECL_IS_OPERATOR_NEW (fndecl
)
1070 && !TREE_NOTHROW (fndecl
))
1072 if (flag_delete_null_pointer_checks
&&
1073 lookup_attribute ("returns_nonnull",
1074 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1076 return gimple_alloca_call_p (stmt
);
1083 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1087 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1089 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1092 /* If we have an expression of the form &X->a, then the expression
1093 is nonnull if X is nonnull. */
1094 if (is_gimple_assign (stmt
)
1095 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1097 tree expr
= gimple_assign_rhs1 (stmt
);
1098 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1100 if (base
!= NULL_TREE
1101 && TREE_CODE (base
) == MEM_REF
1102 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1104 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1105 if (range_is_nonnull (vr
))
1113 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1114 a gimple invariant, or SSA_NAME +- CST. */
1117 valid_value_p (tree expr
)
1119 if (TREE_CODE (expr
) == SSA_NAME
)
1122 if (TREE_CODE (expr
) == PLUS_EXPR
1123 || TREE_CODE (expr
) == MINUS_EXPR
)
1124 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1125 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1127 return is_gimple_min_invariant (expr
);
1133 -2 if those are incomparable. */
1135 operand_less_p (tree val
, tree val2
)
1137 /* LT is folded faster than GE and others. Inline the common case. */
1138 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1140 if (TYPE_UNSIGNED (TREE_TYPE (val
)))
1141 return INT_CST_LT_UNSIGNED (val
, val2
);
1144 if (INT_CST_LT (val
, val2
))
1152 fold_defer_overflow_warnings ();
1154 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1156 fold_undefer_and_ignore_overflow_warnings ();
1159 || TREE_CODE (tcmp
) != INTEGER_CST
)
1162 if (!integer_zerop (tcmp
))
1166 /* val >= val2, not considering overflow infinity. */
1167 if (is_negative_overflow_infinity (val
))
1168 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1169 else if (is_positive_overflow_infinity (val2
))
1170 return is_positive_overflow_infinity (val
) ? 0 : 1;
1175 /* Compare two values VAL1 and VAL2. Return
1177 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1180 +1 if VAL1 > VAL2, and
1183 This is similar to tree_int_cst_compare but supports pointer values
1184 and values that cannot be compared at compile time.
1186 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1187 true if the return value is only valid if we assume that signed
1188 overflow is undefined. */
1191 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1196 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1198 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1199 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1200 /* Convert the two values into the same type. This is needed because
1201 sizetype causes sign extension even for unsigned types. */
1202 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1203 STRIP_USELESS_TYPE_CONVERSION (val2
);
1205 if ((TREE_CODE (val1
) == SSA_NAME
1206 || TREE_CODE (val1
) == PLUS_EXPR
1207 || TREE_CODE (val1
) == MINUS_EXPR
)
1208 && (TREE_CODE (val2
) == SSA_NAME
1209 || TREE_CODE (val2
) == PLUS_EXPR
1210 || TREE_CODE (val2
) == MINUS_EXPR
))
1212 tree n1
, c1
, n2
, c2
;
1213 enum tree_code code1
, code2
;
1215 /* If VAL1 and VAL2 are of the form 'NAME [+-] CST' or 'NAME',
1216 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1217 same name, return -2. */
1218 if (TREE_CODE (val1
) == SSA_NAME
)
1226 code1
= TREE_CODE (val1
);
1227 n1
= TREE_OPERAND (val1
, 0);
1228 c1
= TREE_OPERAND (val1
, 1);
1229 if (tree_int_cst_sgn (c1
) == -1)
1231 if (is_negative_overflow_infinity (c1
))
1233 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1236 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1240 if (TREE_CODE (val2
) == SSA_NAME
)
1248 code2
= TREE_CODE (val2
);
1249 n2
= TREE_OPERAND (val2
, 0);
1250 c2
= TREE_OPERAND (val2
, 1);
1251 if (tree_int_cst_sgn (c2
) == -1)
1253 if (is_negative_overflow_infinity (c2
))
1255 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1258 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1262 /* Both values must use the same name. */
1266 if (code1
== SSA_NAME
1267 && code2
== SSA_NAME
)
1271 /* If overflow is defined we cannot simplify more. */
1272 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1275 if (strict_overflow_p
!= NULL
1276 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1277 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1278 *strict_overflow_p
= true;
1280 if (code1
== SSA_NAME
)
1282 if (code2
== PLUS_EXPR
)
1283 /* NAME < NAME + CST */
1285 else if (code2
== MINUS_EXPR
)
1286 /* NAME > NAME - CST */
1289 else if (code1
== PLUS_EXPR
)
1291 if (code2
== SSA_NAME
)
1292 /* NAME + CST > NAME */
1294 else if (code2
== PLUS_EXPR
)
1295 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1296 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1297 else if (code2
== MINUS_EXPR
)
1298 /* NAME + CST1 > NAME - CST2 */
1301 else if (code1
== MINUS_EXPR
)
1303 if (code2
== SSA_NAME
)
1304 /* NAME - CST < NAME */
1306 else if (code2
== PLUS_EXPR
)
1307 /* NAME - CST1 < NAME + CST2 */
1309 else if (code2
== MINUS_EXPR
)
1310 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1311 C1 and C2 are swapped in the call to compare_values. */
1312 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1318 /* We cannot compare non-constants. */
1319 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1322 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1324 /* We cannot compare overflowed values, except for overflow
1326 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1328 if (strict_overflow_p
!= NULL
)
1329 *strict_overflow_p
= true;
1330 if (is_negative_overflow_infinity (val1
))
1331 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1332 else if (is_negative_overflow_infinity (val2
))
1334 else if (is_positive_overflow_infinity (val1
))
1335 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1336 else if (is_positive_overflow_infinity (val2
))
1341 return tree_int_cst_compare (val1
, val2
);
1347 /* First see if VAL1 and VAL2 are not the same. */
1348 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1351 /* If VAL1 is a lower address than VAL2, return -1. */
1352 if (operand_less_p (val1
, val2
) == 1)
1355 /* If VAL1 is a higher address than VAL2, return +1. */
1356 if (operand_less_p (val2
, val1
) == 1)
1359 /* If VAL1 is different than VAL2, return +2.
1360 For integer constants we either have already returned -1 or 1
1361 or they are equivalent. We still might succeed in proving
1362 something about non-trivial operands. */
1363 if (TREE_CODE (val1
) != INTEGER_CST
1364 || TREE_CODE (val2
) != INTEGER_CST
)
1366 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1367 if (t
&& integer_onep (t
))
1375 /* Compare values like compare_values_warnv, but treat comparisons of
1376 nonconstants which rely on undefined overflow as incomparable. */
1379 compare_values (tree val1
, tree val2
)
1385 ret
= compare_values_warnv (val1
, val2
, &sop
);
1387 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1393 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1394 0 if VAL is not inside [MIN, MAX],
1395 -2 if we cannot tell either way.
1397 Benchmark compile/20001226-1.c compilation time after changing this
1401 value_inside_range (tree val
, tree min
, tree max
)
1405 cmp1
= operand_less_p (val
, min
);
1411 cmp2
= operand_less_p (max
, val
);
1419 /* Return true if value ranges VR0 and VR1 have a non-empty
1422 Benchmark compile/20001226-1.c compilation time after changing this
1427 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1429 /* The value ranges do not intersect if the maximum of the first range is
1430 less than the minimum of the second range or vice versa.
1431 When those relations are unknown, we can't do any better. */
1432 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1434 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1440 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1441 include the value zero, -2 if we cannot tell. */
1444 range_includes_zero_p (tree min
, tree max
)
1446 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1447 return value_inside_range (zero
, min
, max
);
1450 /* Return true if *VR is know to only contain nonnegative values. */
1453 value_range_nonnegative_p (value_range_t
*vr
)
1455 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1456 which would return a useful value should be encoded as a
1458 if (vr
->type
== VR_RANGE
)
1460 int result
= compare_values (vr
->min
, integer_zero_node
);
1461 return (result
== 0 || result
== 1);
1467 /* Return true if T, an SSA_NAME, is known to be nonnegative. Return
1468 false otherwise or if no value range information is available. */
1471 ssa_name_nonnegative_p (const_tree t
)
1473 value_range_t
*vr
= get_value_range (t
);
1475 if (INTEGRAL_TYPE_P (t
)
1476 && TYPE_UNSIGNED (t
))
1482 return value_range_nonnegative_p (vr
);
1485 /* If *VR has a value rante that is a single constant value return that,
1486 otherwise return NULL_TREE. */
1489 value_range_constant_singleton (value_range_t
*vr
)
1491 if (vr
->type
== VR_RANGE
1492 && operand_equal_p (vr
->min
, vr
->max
, 0)
1493 && is_gimple_min_invariant (vr
->min
))
1499 /* If OP has a value range with a single constant value return that,
1500 otherwise return NULL_TREE. This returns OP itself if OP is a
1504 op_with_constant_singleton_value_range (tree op
)
1506 if (is_gimple_min_invariant (op
))
1509 if (TREE_CODE (op
) != SSA_NAME
)
1512 return value_range_constant_singleton (get_value_range (op
));
1515 /* Return true if op is in a boolean [0, 1] value-range. */
1518 op_with_boolean_value_range_p (tree op
)
1522 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1525 if (integer_zerop (op
)
1526 || integer_onep (op
))
1529 if (TREE_CODE (op
) != SSA_NAME
)
1532 vr
= get_value_range (op
);
1533 return (vr
->type
== VR_RANGE
1534 && integer_zerop (vr
->min
)
1535 && integer_onep (vr
->max
));
1538 /* Extract value range information from an ASSERT_EXPR EXPR and store
1542 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1544 tree var
, cond
, limit
, min
, max
, type
;
1545 value_range_t
*limit_vr
;
1546 enum tree_code cond_code
;
1548 var
= ASSERT_EXPR_VAR (expr
);
1549 cond
= ASSERT_EXPR_COND (expr
);
1551 gcc_assert (COMPARISON_CLASS_P (cond
));
1553 /* Find VAR in the ASSERT_EXPR conditional. */
1554 if (var
== TREE_OPERAND (cond
, 0)
1555 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1556 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1558 /* If the predicate is of the form VAR COMP LIMIT, then we just
1559 take LIMIT from the RHS and use the same comparison code. */
1560 cond_code
= TREE_CODE (cond
);
1561 limit
= TREE_OPERAND (cond
, 1);
1562 cond
= TREE_OPERAND (cond
, 0);
1566 /* If the predicate is of the form LIMIT COMP VAR, then we need
1567 to flip around the comparison code to create the proper range
1569 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1570 limit
= TREE_OPERAND (cond
, 0);
1571 cond
= TREE_OPERAND (cond
, 1);
1574 limit
= avoid_overflow_infinity (limit
);
1576 type
= TREE_TYPE (var
);
1577 gcc_assert (limit
!= var
);
1579 /* For pointer arithmetic, we only keep track of pointer equality
1581 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1583 set_value_range_to_varying (vr_p
);
1587 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1588 try to use LIMIT's range to avoid creating symbolic ranges
1590 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1592 /* LIMIT's range is only interesting if it has any useful information. */
1594 && (limit_vr
->type
== VR_UNDEFINED
1595 || limit_vr
->type
== VR_VARYING
1596 || symbolic_range_p (limit_vr
)))
1599 /* Initially, the new range has the same set of equivalences of
1600 VAR's range. This will be revised before returning the final
1601 value. Since assertions may be chained via mutually exclusive
1602 predicates, we will need to trim the set of equivalences before
1604 gcc_assert (vr_p
->equiv
== NULL
);
1605 add_equivalence (&vr_p
->equiv
, var
);
1607 /* Extract a new range based on the asserted comparison for VAR and
1608 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1609 will only use it for equality comparisons (EQ_EXPR). For any
1610 other kind of assertion, we cannot derive a range from LIMIT's
1611 anti-range that can be used to describe the new range. For
1612 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1613 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1614 no single range for x_2 that could describe LE_EXPR, so we might
1615 as well build the range [b_4, +INF] for it.
1616 One special case we handle is extracting a range from a
1617 range test encoded as (unsigned)var + CST <= limit. */
1618 if (TREE_CODE (cond
) == NOP_EXPR
1619 || TREE_CODE (cond
) == PLUS_EXPR
)
1621 if (TREE_CODE (cond
) == PLUS_EXPR
)
1623 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1624 TREE_OPERAND (cond
, 1));
1625 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1626 cond
= TREE_OPERAND (cond
, 0);
1630 min
= build_int_cst (TREE_TYPE (var
), 0);
1634 /* Make sure to not set TREE_OVERFLOW on the final type
1635 conversion. We are willingly interpreting large positive
1636 unsigned values as negative singed values here. */
1637 min
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (min
),
1639 max
= force_fit_type_double (TREE_TYPE (var
), tree_to_double_int (max
),
1642 /* We can transform a max, min range to an anti-range or
1643 vice-versa. Use set_and_canonicalize_value_range which does
1645 if (cond_code
== LE_EXPR
)
1646 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1647 min
, max
, vr_p
->equiv
);
1648 else if (cond_code
== GT_EXPR
)
1649 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1650 min
, max
, vr_p
->equiv
);
1654 else if (cond_code
== EQ_EXPR
)
1656 enum value_range_type range_type
;
1660 range_type
= limit_vr
->type
;
1661 min
= limit_vr
->min
;
1662 max
= limit_vr
->max
;
1666 range_type
= VR_RANGE
;
1671 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1673 /* When asserting the equality VAR == LIMIT and LIMIT is another
1674 SSA name, the new range will also inherit the equivalence set
1676 if (TREE_CODE (limit
) == SSA_NAME
)
1677 add_equivalence (&vr_p
->equiv
, limit
);
1679 else if (cond_code
== NE_EXPR
)
1681 /* As described above, when LIMIT's range is an anti-range and
1682 this assertion is an inequality (NE_EXPR), then we cannot
1683 derive anything from the anti-range. For instance, if
1684 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1685 not imply that VAR's range is [0, 0]. So, in the case of
1686 anti-ranges, we just assert the inequality using LIMIT and
1689 If LIMIT_VR is a range, we can only use it to build a new
1690 anti-range if LIMIT_VR is a single-valued range. For
1691 instance, if LIMIT_VR is [0, 1], the predicate
1692 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1693 Rather, it means that for value 0 VAR should be ~[0, 0]
1694 and for value 1, VAR should be ~[1, 1]. We cannot
1695 represent these ranges.
1697 The only situation in which we can build a valid
1698 anti-range is when LIMIT_VR is a single-valued range
1699 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1700 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1702 && limit_vr
->type
== VR_RANGE
1703 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1705 min
= limit_vr
->min
;
1706 max
= limit_vr
->max
;
1710 /* In any other case, we cannot use LIMIT's range to build a
1711 valid anti-range. */
1715 /* If MIN and MAX cover the whole range for their type, then
1716 just use the original LIMIT. */
1717 if (INTEGRAL_TYPE_P (type
)
1718 && vrp_val_is_min (min
)
1719 && vrp_val_is_max (max
))
1722 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1723 min
, max
, vr_p
->equiv
);
1725 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1727 min
= TYPE_MIN_VALUE (type
);
1729 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1733 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1734 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1736 max
= limit_vr
->max
;
1739 /* If the maximum value forces us to be out of bounds, simply punt.
1740 It would be pointless to try and do anything more since this
1741 all should be optimized away above us. */
1742 if ((cond_code
== LT_EXPR
1743 && compare_values (max
, min
) == 0)
1744 || is_overflow_infinity (max
))
1745 set_value_range_to_varying (vr_p
);
1748 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1749 if (cond_code
== LT_EXPR
)
1751 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1752 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1753 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1754 build_int_cst (TREE_TYPE (max
), -1));
1756 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1757 build_int_cst (TREE_TYPE (max
), 1));
1759 TREE_NO_WARNING (max
) = 1;
1762 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1765 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1767 max
= TYPE_MAX_VALUE (type
);
1769 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1773 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1774 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1776 min
= limit_vr
->min
;
1779 /* If the minimum value forces us to be out of bounds, simply punt.
1780 It would be pointless to try and do anything more since this
1781 all should be optimized away above us. */
1782 if ((cond_code
== GT_EXPR
1783 && compare_values (min
, max
) == 0)
1784 || is_overflow_infinity (min
))
1785 set_value_range_to_varying (vr_p
);
1788 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1789 if (cond_code
== GT_EXPR
)
1791 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1792 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1793 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1794 build_int_cst (TREE_TYPE (min
), -1));
1796 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1797 build_int_cst (TREE_TYPE (min
), 1));
1799 TREE_NO_WARNING (min
) = 1;
1802 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1808 /* Finally intersect the new range with what we already know about var. */
1809 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1813 /* Extract range information from SSA name VAR and store it in VR. If
1814 VAR has an interesting range, use it. Otherwise, create the
1815 range [VAR, VAR] and return it. This is useful in situations where
1816 we may have conditionals testing values of VARYING names. For
1823 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1827 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1829 value_range_t
*var_vr
= get_value_range (var
);
1831 if (var_vr
->type
!= VR_UNDEFINED
&& var_vr
->type
!= VR_VARYING
)
1832 copy_value_range (vr
, var_vr
);
1834 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1836 add_equivalence (&vr
->equiv
, var
);
1840 /* Wrapper around int_const_binop. If the operation overflows and we
1841 are not using wrapping arithmetic, then adjust the result to be
1842 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1843 NULL_TREE if we need to use an overflow infinity representation but
1844 the type does not support it. */
1847 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1851 res
= int_const_binop (code
, val1
, val2
);
1853 /* If we are using unsigned arithmetic, operate symbolically
1854 on -INF and +INF as int_const_binop only handles signed overflow. */
1855 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1857 int checkz
= compare_values (res
, val1
);
1858 bool overflow
= false;
1860 /* Ensure that res = val1 [+*] val2 >= val1
1861 or that res = val1 - val2 <= val1. */
1862 if ((code
== PLUS_EXPR
1863 && !(checkz
== 1 || checkz
== 0))
1864 || (code
== MINUS_EXPR
1865 && !(checkz
== 0 || checkz
== -1)))
1869 /* Checking for multiplication overflow is done by dividing the
1870 output of the multiplication by the first input of the
1871 multiplication. If the result of that division operation is
1872 not equal to the second input of the multiplication, then the
1873 multiplication overflowed. */
1874 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1876 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1879 int check
= compare_values (tmp
, val2
);
1887 res
= copy_node (res
);
1888 TREE_OVERFLOW (res
) = 1;
1892 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1893 /* If the singed operation wraps then int_const_binop has done
1894 everything we want. */
1896 else if ((TREE_OVERFLOW (res
)
1897 && !TREE_OVERFLOW (val1
)
1898 && !TREE_OVERFLOW (val2
))
1899 || is_overflow_infinity (val1
)
1900 || is_overflow_infinity (val2
))
1902 /* If the operation overflowed but neither VAL1 nor VAL2 are
1903 overflown, return -INF or +INF depending on the operation
1904 and the combination of signs of the operands. */
1905 int sgn1
= tree_int_cst_sgn (val1
);
1906 int sgn2
= tree_int_cst_sgn (val2
);
1908 if (needs_overflow_infinity (TREE_TYPE (res
))
1909 && !supports_overflow_infinity (TREE_TYPE (res
)))
1912 /* We have to punt on adding infinities of different signs,
1913 since we can't tell what the sign of the result should be.
1914 Likewise for subtracting infinities of the same sign. */
1915 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1916 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1917 && is_overflow_infinity (val1
)
1918 && is_overflow_infinity (val2
))
1921 /* Don't try to handle division or shifting of infinities. */
1922 if ((code
== TRUNC_DIV_EXPR
1923 || code
== FLOOR_DIV_EXPR
1924 || code
== CEIL_DIV_EXPR
1925 || code
== EXACT_DIV_EXPR
1926 || code
== ROUND_DIV_EXPR
1927 || code
== RSHIFT_EXPR
)
1928 && (is_overflow_infinity (val1
)
1929 || is_overflow_infinity (val2
)))
1932 /* Notice that we only need to handle the restricted set of
1933 operations handled by extract_range_from_binary_expr.
1934 Among them, only multiplication, addition and subtraction
1935 can yield overflow without overflown operands because we
1936 are working with integral types only... except in the
1937 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1938 for division too. */
1940 /* For multiplication, the sign of the overflow is given
1941 by the comparison of the signs of the operands. */
1942 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1943 /* For addition, the operands must be of the same sign
1944 to yield an overflow. Its sign is therefore that
1945 of one of the operands, for example the first. For
1946 infinite operands X + -INF is negative, not positive. */
1947 || (code
== PLUS_EXPR
1949 ? !is_negative_overflow_infinity (val2
)
1950 : is_positive_overflow_infinity (val2
)))
1951 /* For subtraction, non-infinite operands must be of
1952 different signs to yield an overflow. Its sign is
1953 therefore that of the first operand or the opposite of
1954 that of the second operand. A first operand of 0 counts
1955 as positive here, for the corner case 0 - (-INF), which
1956 overflows, but must yield +INF. For infinite operands 0
1957 - INF is negative, not positive. */
1958 || (code
== MINUS_EXPR
1960 ? !is_positive_overflow_infinity (val2
)
1961 : is_negative_overflow_infinity (val2
)))
1962 /* We only get in here with positive shift count, so the
1963 overflow direction is the same as the sign of val1.
1964 Actually rshift does not overflow at all, but we only
1965 handle the case of shifting overflowed -INF and +INF. */
1966 || (code
== RSHIFT_EXPR
1968 /* For division, the only case is -INF / -1 = +INF. */
1969 || code
== TRUNC_DIV_EXPR
1970 || code
== FLOOR_DIV_EXPR
1971 || code
== CEIL_DIV_EXPR
1972 || code
== EXACT_DIV_EXPR
1973 || code
== ROUND_DIV_EXPR
)
1974 return (needs_overflow_infinity (TREE_TYPE (res
))
1975 ? positive_overflow_infinity (TREE_TYPE (res
))
1976 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1978 return (needs_overflow_infinity (TREE_TYPE (res
))
1979 ? negative_overflow_infinity (TREE_TYPE (res
))
1980 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1987 /* For range VR compute two double_int bitmasks. In *MAY_BE_NONZERO
1988 bitmask if some bit is unset, it means for all numbers in the range
1989 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1990 bitmask if some bit is set, it means for all numbers in the range
1991 the bit is 1, otherwise it might be 0 or 1. */
1994 zero_nonzero_bits_from_vr (value_range_t
*vr
,
1995 double_int
*may_be_nonzero
,
1996 double_int
*must_be_nonzero
)
1998 *may_be_nonzero
= double_int_minus_one
;
1999 *must_be_nonzero
= double_int_zero
;
2000 if (!range_int_cst_p (vr
)
2001 || is_overflow_infinity (vr
->min
)
2002 || is_overflow_infinity (vr
->max
))
2005 if (range_int_cst_singleton_p (vr
))
2007 *may_be_nonzero
= tree_to_double_int (vr
->min
);
2008 *must_be_nonzero
= *may_be_nonzero
;
2010 else if (tree_int_cst_sgn (vr
->min
) >= 0
2011 || tree_int_cst_sgn (vr
->max
) < 0)
2013 double_int dmin
= tree_to_double_int (vr
->min
);
2014 double_int dmax
= tree_to_double_int (vr
->max
);
2015 double_int xor_mask
= dmin
^ dmax
;
2016 *may_be_nonzero
= dmin
| dmax
;
2017 *must_be_nonzero
= dmin
& dmax
;
2018 if (xor_mask
.high
!= 0)
2020 unsigned HOST_WIDE_INT mask
2021 = ((unsigned HOST_WIDE_INT
) 1
2022 << floor_log2 (xor_mask
.high
)) - 1;
2023 may_be_nonzero
->low
= ALL_ONES
;
2024 may_be_nonzero
->high
|= mask
;
2025 must_be_nonzero
->low
= 0;
2026 must_be_nonzero
->high
&= ~mask
;
2028 else if (xor_mask
.low
!= 0)
2030 unsigned HOST_WIDE_INT mask
2031 = ((unsigned HOST_WIDE_INT
) 1
2032 << floor_log2 (xor_mask
.low
)) - 1;
2033 may_be_nonzero
->low
|= mask
;
2034 must_be_nonzero
->low
&= ~mask
;
2041 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2042 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2043 false otherwise. If *AR can be represented with a single range
2044 *VR1 will be VR_UNDEFINED. */
2047 ranges_from_anti_range (value_range_t
*ar
,
2048 value_range_t
*vr0
, value_range_t
*vr1
)
2050 tree type
= TREE_TYPE (ar
->min
);
2052 vr0
->type
= VR_UNDEFINED
;
2053 vr1
->type
= VR_UNDEFINED
;
2055 if (ar
->type
!= VR_ANTI_RANGE
2056 || TREE_CODE (ar
->min
) != INTEGER_CST
2057 || TREE_CODE (ar
->max
) != INTEGER_CST
2058 || !vrp_val_min (type
)
2059 || !vrp_val_max (type
))
2062 if (!vrp_val_is_min (ar
->min
))
2064 vr0
->type
= VR_RANGE
;
2065 vr0
->min
= vrp_val_min (type
);
2067 = double_int_to_tree (type
,
2068 tree_to_double_int (ar
->min
) - double_int_one
);
2070 if (!vrp_val_is_max (ar
->max
))
2072 vr1
->type
= VR_RANGE
;
2074 = double_int_to_tree (type
,
2075 tree_to_double_int (ar
->max
) + double_int_one
);
2076 vr1
->max
= vrp_val_max (type
);
2078 if (vr0
->type
== VR_UNDEFINED
)
2081 vr1
->type
= VR_UNDEFINED
;
2084 return vr0
->type
!= VR_UNDEFINED
;
2087 /* Helper to extract a value-range *VR for a multiplicative operation
2091 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2092 enum tree_code code
,
2093 value_range_t
*vr0
, value_range_t
*vr1
)
2095 enum value_range_type type
;
2102 /* Multiplications, divisions and shifts are a bit tricky to handle,
2103 depending on the mix of signs we have in the two ranges, we
2104 need to operate on different values to get the minimum and
2105 maximum values for the new range. One approach is to figure
2106 out all the variations of range combinations and do the
2109 However, this involves several calls to compare_values and it
2110 is pretty convoluted. It's simpler to do the 4 operations
2111 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2112 MAX1) and then figure the smallest and largest values to form
2114 gcc_assert (code
== MULT_EXPR
2115 || code
== TRUNC_DIV_EXPR
2116 || code
== FLOOR_DIV_EXPR
2117 || code
== CEIL_DIV_EXPR
2118 || code
== EXACT_DIV_EXPR
2119 || code
== ROUND_DIV_EXPR
2120 || code
== RSHIFT_EXPR
2121 || code
== LSHIFT_EXPR
);
2122 gcc_assert ((vr0
->type
== VR_RANGE
2123 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2124 && vr0
->type
== vr1
->type
);
2128 /* Compute the 4 cross operations. */
2130 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2131 if (val
[0] == NULL_TREE
)
2134 if (vr1
->max
== vr1
->min
)
2138 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2139 if (val
[1] == NULL_TREE
)
2143 if (vr0
->max
== vr0
->min
)
2147 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2148 if (val
[2] == NULL_TREE
)
2152 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2156 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2157 if (val
[3] == NULL_TREE
)
2163 set_value_range_to_varying (vr
);
2167 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2171 for (i
= 1; i
< 4; i
++)
2173 if (!is_gimple_min_invariant (min
)
2174 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2175 || !is_gimple_min_invariant (max
)
2176 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2181 if (!is_gimple_min_invariant (val
[i
])
2182 || (TREE_OVERFLOW (val
[i
])
2183 && !is_overflow_infinity (val
[i
])))
2185 /* If we found an overflowed value, set MIN and MAX
2186 to it so that we set the resulting range to
2192 if (compare_values (val
[i
], min
) == -1)
2195 if (compare_values (val
[i
], max
) == 1)
2200 /* If either MIN or MAX overflowed, then set the resulting range to
2201 VARYING. But we do accept an overflow infinity
2203 if (min
== NULL_TREE
2204 || !is_gimple_min_invariant (min
)
2205 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2207 || !is_gimple_min_invariant (max
)
2208 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2210 set_value_range_to_varying (vr
);
2216 2) [-INF, +-INF(OVF)]
2217 3) [+-INF(OVF), +INF]
2218 4) [+-INF(OVF), +-INF(OVF)]
2219 We learn nothing when we have INF and INF(OVF) on both sides.
2220 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2222 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2223 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2225 set_value_range_to_varying (vr
);
2229 cmp
= compare_values (min
, max
);
2230 if (cmp
== -2 || cmp
== 1)
2232 /* If the new range has its limits swapped around (MIN > MAX),
2233 then the operation caused one of them to wrap around, mark
2234 the new range VARYING. */
2235 set_value_range_to_varying (vr
);
2238 set_value_range (vr
, type
, min
, max
, NULL
);
2241 /* Some quadruple precision helpers. */
2243 quad_int_cmp (double_int l0
, double_int h0
,
2244 double_int l1
, double_int h1
, bool uns
)
2246 int c
= h0
.cmp (h1
, uns
);
2247 if (c
!= 0) return c
;
2248 return l0
.ucmp (l1
);
2252 quad_int_pair_sort (double_int
*l0
, double_int
*h0
,
2253 double_int
*l1
, double_int
*h1
, bool uns
)
2255 if (quad_int_cmp (*l0
, *h0
, *l1
, *h1
, uns
) > 0)
2258 tmp
= *l0
; *l0
= *l1
; *l1
= tmp
;
2259 tmp
= *h0
; *h0
= *h1
; *h1
= tmp
;
2263 /* Extract range information from a binary operation CODE based on
2264 the ranges of each of its operands, *VR0 and *VR1 with resulting
2265 type EXPR_TYPE. The resulting range is stored in *VR. */
2268 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2269 enum tree_code code
, tree expr_type
,
2270 value_range_t
*vr0_
, value_range_t
*vr1_
)
2272 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2273 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2274 enum value_range_type type
;
2275 tree min
= NULL_TREE
, max
= NULL_TREE
;
2278 if (!INTEGRAL_TYPE_P (expr_type
)
2279 && !POINTER_TYPE_P (expr_type
))
2281 set_value_range_to_varying (vr
);
2285 /* Not all binary expressions can be applied to ranges in a
2286 meaningful way. Handle only arithmetic operations. */
2287 if (code
!= PLUS_EXPR
2288 && code
!= MINUS_EXPR
2289 && code
!= POINTER_PLUS_EXPR
2290 && code
!= MULT_EXPR
2291 && code
!= TRUNC_DIV_EXPR
2292 && code
!= FLOOR_DIV_EXPR
2293 && code
!= CEIL_DIV_EXPR
2294 && code
!= EXACT_DIV_EXPR
2295 && code
!= ROUND_DIV_EXPR
2296 && code
!= TRUNC_MOD_EXPR
2297 && code
!= RSHIFT_EXPR
2298 && code
!= LSHIFT_EXPR
2301 && code
!= BIT_AND_EXPR
2302 && code
!= BIT_IOR_EXPR
2303 && code
!= BIT_XOR_EXPR
)
2305 set_value_range_to_varying (vr
);
2309 /* If both ranges are UNDEFINED, so is the result. */
2310 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2312 set_value_range_to_undefined (vr
);
2315 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2316 code. At some point we may want to special-case operations that
2317 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2319 else if (vr0
.type
== VR_UNDEFINED
)
2320 set_value_range_to_varying (&vr0
);
2321 else if (vr1
.type
== VR_UNDEFINED
)
2322 set_value_range_to_varying (&vr1
);
2324 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2325 and express ~[] op X as ([]' op X) U ([]'' op X). */
2326 if (vr0
.type
== VR_ANTI_RANGE
2327 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2329 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2330 if (vrtem1
.type
!= VR_UNDEFINED
)
2332 value_range_t vrres
= VR_INITIALIZER
;
2333 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2335 vrp_meet (vr
, &vrres
);
2339 /* Likewise for X op ~[]. */
2340 if (vr1
.type
== VR_ANTI_RANGE
2341 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2343 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2344 if (vrtem1
.type
!= VR_UNDEFINED
)
2346 value_range_t vrres
= VR_INITIALIZER
;
2347 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2349 vrp_meet (vr
, &vrres
);
2354 /* The type of the resulting value range defaults to VR0.TYPE. */
2357 /* Refuse to operate on VARYING ranges, ranges of different kinds
2358 and symbolic ranges. As an exception, we allow BIT_AND_EXPR
2359 because we may be able to derive a useful range even if one of
2360 the operands is VR_VARYING or symbolic range. Similarly for
2361 divisions. TODO, we may be able to derive anti-ranges in
2363 if (code
!= BIT_AND_EXPR
2364 && code
!= BIT_IOR_EXPR
2365 && code
!= TRUNC_DIV_EXPR
2366 && code
!= FLOOR_DIV_EXPR
2367 && code
!= CEIL_DIV_EXPR
2368 && code
!= EXACT_DIV_EXPR
2369 && code
!= ROUND_DIV_EXPR
2370 && code
!= TRUNC_MOD_EXPR
2373 && (vr0
.type
== VR_VARYING
2374 || vr1
.type
== VR_VARYING
2375 || vr0
.type
!= vr1
.type
2376 || symbolic_range_p (&vr0
)
2377 || symbolic_range_p (&vr1
)))
2379 set_value_range_to_varying (vr
);
2383 /* Now evaluate the expression to determine the new range. */
2384 if (POINTER_TYPE_P (expr_type
))
2386 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2388 /* For MIN/MAX expressions with pointers, we only care about
2389 nullness, if both are non null, then the result is nonnull.
2390 If both are null, then the result is null. Otherwise they
2392 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2393 set_value_range_to_nonnull (vr
, expr_type
);
2394 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2395 set_value_range_to_null (vr
, expr_type
);
2397 set_value_range_to_varying (vr
);
2399 else if (code
== POINTER_PLUS_EXPR
)
2401 /* For pointer types, we are really only interested in asserting
2402 whether the expression evaluates to non-NULL. */
2403 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2404 set_value_range_to_nonnull (vr
, expr_type
);
2405 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2406 set_value_range_to_null (vr
, expr_type
);
2408 set_value_range_to_varying (vr
);
2410 else if (code
== BIT_AND_EXPR
)
2412 /* For pointer types, we are really only interested in asserting
2413 whether the expression evaluates to non-NULL. */
2414 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2415 set_value_range_to_nonnull (vr
, expr_type
);
2416 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2417 set_value_range_to_null (vr
, expr_type
);
2419 set_value_range_to_varying (vr
);
2422 set_value_range_to_varying (vr
);
2427 /* For integer ranges, apply the operation to each end of the
2428 range and see what we end up with. */
2429 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2431 /* If we have a PLUS_EXPR with two VR_RANGE integer constant
2432 ranges compute the precise range for such case if possible. */
2433 if (range_int_cst_p (&vr0
)
2434 && range_int_cst_p (&vr1
)
2435 /* We need as many bits as the possibly unsigned inputs. */
2436 && TYPE_PRECISION (expr_type
) <= HOST_BITS_PER_DOUBLE_INT
)
2438 double_int min0
= tree_to_double_int (vr0
.min
);
2439 double_int max0
= tree_to_double_int (vr0
.max
);
2440 double_int min1
= tree_to_double_int (vr1
.min
);
2441 double_int max1
= tree_to_double_int (vr1
.max
);
2442 bool uns
= TYPE_UNSIGNED (expr_type
);
2444 = double_int::min_value (TYPE_PRECISION (expr_type
), uns
);
2446 = double_int::max_value (TYPE_PRECISION (expr_type
), uns
);
2447 double_int dmin
, dmax
;
2451 if (code
== PLUS_EXPR
)
2456 /* Check for overflow in double_int. */
2457 if (min1
.cmp (double_int_zero
, uns
) != dmin
.cmp (min0
, uns
))
2458 min_ovf
= min0
.cmp (dmin
, uns
);
2459 if (max1
.cmp (double_int_zero
, uns
) != dmax
.cmp (max0
, uns
))
2460 max_ovf
= max0
.cmp (dmax
, uns
);
2462 else /* if (code == MINUS_EXPR) */
2467 if (double_int_zero
.cmp (max1
, uns
) != dmin
.cmp (min0
, uns
))
2468 min_ovf
= min0
.cmp (max1
, uns
);
2469 if (double_int_zero
.cmp (min1
, uns
) != dmax
.cmp (max0
, uns
))
2470 max_ovf
= max0
.cmp (min1
, uns
);
2473 /* For non-wrapping arithmetic look at possibly smaller
2474 value-ranges of the type. */
2475 if (!TYPE_OVERFLOW_WRAPS (expr_type
))
2477 if (vrp_val_min (expr_type
))
2478 type_min
= tree_to_double_int (vrp_val_min (expr_type
));
2479 if (vrp_val_max (expr_type
))
2480 type_max
= tree_to_double_int (vrp_val_max (expr_type
));
2483 /* Check for type overflow. */
2486 if (dmin
.cmp (type_min
, uns
) == -1)
2488 else if (dmin
.cmp (type_max
, uns
) == 1)
2493 if (dmax
.cmp (type_min
, uns
) == -1)
2495 else if (dmax
.cmp (type_max
, uns
) == 1)
2499 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2501 /* If overflow wraps, truncate the values and adjust the
2502 range kind and bounds appropriately. */
2504 = dmin
.ext (TYPE_PRECISION (expr_type
), uns
);
2506 = dmax
.ext (TYPE_PRECISION (expr_type
), uns
);
2507 if (min_ovf
== max_ovf
)
2509 /* No overflow or both overflow or underflow. The
2510 range kind stays VR_RANGE. */
2511 min
= double_int_to_tree (expr_type
, tmin
);
2512 max
= double_int_to_tree (expr_type
, tmax
);
2514 else if (min_ovf
== -1
2517 /* Underflow and overflow, drop to VR_VARYING. */
2518 set_value_range_to_varying (vr
);
2523 /* Min underflow or max overflow. The range kind
2524 changes to VR_ANTI_RANGE. */
2525 bool covers
= false;
2526 double_int tem
= tmin
;
2527 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2528 || (max_ovf
== 1 && min_ovf
== 0));
2529 type
= VR_ANTI_RANGE
;
2530 tmin
= tmax
+ double_int_one
;
2531 if (tmin
.cmp (tmax
, uns
) < 0)
2533 tmax
= tem
+ double_int_minus_one
;
2534 if (tmax
.cmp (tem
, uns
) > 0)
2536 /* If the anti-range would cover nothing, drop to varying.
2537 Likewise if the anti-range bounds are outside of the
2539 if (covers
|| tmin
.cmp (tmax
, uns
) > 0)
2541 set_value_range_to_varying (vr
);
2544 min
= double_int_to_tree (expr_type
, tmin
);
2545 max
= double_int_to_tree (expr_type
, tmax
);
2550 /* If overflow does not wrap, saturate to the types min/max
2554 if (needs_overflow_infinity (expr_type
)
2555 && supports_overflow_infinity (expr_type
))
2556 min
= negative_overflow_infinity (expr_type
);
2558 min
= double_int_to_tree (expr_type
, type_min
);
2560 else if (min_ovf
== 1)
2562 if (needs_overflow_infinity (expr_type
)
2563 && supports_overflow_infinity (expr_type
))
2564 min
= positive_overflow_infinity (expr_type
);
2566 min
= double_int_to_tree (expr_type
, type_max
);
2569 min
= double_int_to_tree (expr_type
, dmin
);
2573 if (needs_overflow_infinity (expr_type
)
2574 && supports_overflow_infinity (expr_type
))
2575 max
= negative_overflow_infinity (expr_type
);
2577 max
= double_int_to_tree (expr_type
, type_min
);
2579 else if (max_ovf
== 1)
2581 if (needs_overflow_infinity (expr_type
)
2582 && supports_overflow_infinity (expr_type
))
2583 max
= positive_overflow_infinity (expr_type
);
2585 max
= double_int_to_tree (expr_type
, type_max
);
2588 max
= double_int_to_tree (expr_type
, dmax
);
2590 if (needs_overflow_infinity (expr_type
)
2591 && supports_overflow_infinity (expr_type
))
2593 if (is_negative_overflow_infinity (vr0
.min
)
2594 || (code
== PLUS_EXPR
2595 ? is_negative_overflow_infinity (vr1
.min
)
2596 : is_positive_overflow_infinity (vr1
.max
)))
2597 min
= negative_overflow_infinity (expr_type
);
2598 if (is_positive_overflow_infinity (vr0
.max
)
2599 || (code
== PLUS_EXPR
2600 ? is_positive_overflow_infinity (vr1
.max
)
2601 : is_negative_overflow_infinity (vr1
.min
)))
2602 max
= positive_overflow_infinity (expr_type
);
2607 /* For other cases, for example if we have a PLUS_EXPR with two
2608 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2609 to compute a precise range for such a case.
2610 ??? General even mixed range kind operations can be expressed
2611 by for example transforming ~[3, 5] + [1, 2] to range-only
2612 operations and a union primitive:
2613 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2614 [-INF+1, 4] U [6, +INF(OVF)]
2615 though usually the union is not exactly representable with
2616 a single range or anti-range as the above is
2617 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2618 but one could use a scheme similar to equivalences for this. */
2619 set_value_range_to_varying (vr
);
2623 else if (code
== MIN_EXPR
2624 || code
== MAX_EXPR
)
2626 if (vr0
.type
== VR_RANGE
2627 && !symbolic_range_p (&vr0
))
2630 if (vr1
.type
== VR_RANGE
2631 && !symbolic_range_p (&vr1
))
2633 /* For operations that make the resulting range directly
2634 proportional to the original ranges, apply the operation to
2635 the same end of each range. */
2636 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2637 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2639 else if (code
== MIN_EXPR
)
2641 min
= vrp_val_min (expr_type
);
2644 else if (code
== MAX_EXPR
)
2647 max
= vrp_val_max (expr_type
);
2650 else if (vr1
.type
== VR_RANGE
2651 && !symbolic_range_p (&vr1
))
2654 if (code
== MIN_EXPR
)
2656 min
= vrp_val_min (expr_type
);
2659 else if (code
== MAX_EXPR
)
2662 max
= vrp_val_max (expr_type
);
2667 set_value_range_to_varying (vr
);
2671 else if (code
== MULT_EXPR
)
2673 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2675 if (range_int_cst_p (&vr0
)
2676 && range_int_cst_p (&vr1
)
2677 && TYPE_OVERFLOW_WRAPS (expr_type
))
2679 double_int min0
, max0
, min1
, max1
, sizem1
, size
;
2680 double_int prod0l
, prod0h
, prod1l
, prod1h
,
2681 prod2l
, prod2h
, prod3l
, prod3h
;
2682 bool uns0
, uns1
, uns
;
2684 sizem1
= double_int::max_value (TYPE_PRECISION (expr_type
), true);
2685 size
= sizem1
+ double_int_one
;
2687 min0
= tree_to_double_int (vr0
.min
);
2688 max0
= tree_to_double_int (vr0
.max
);
2689 min1
= tree_to_double_int (vr1
.min
);
2690 max1
= tree_to_double_int (vr1
.max
);
2692 uns0
= TYPE_UNSIGNED (expr_type
);
2695 /* Canonicalize the intervals. */
2696 if (TYPE_UNSIGNED (expr_type
))
2698 double_int min2
= size
- min0
;
2699 if (!min2
.is_zero () && min2
.cmp (max0
, true) < 0)
2707 if (!min2
.is_zero () && min2
.cmp (max1
, true) < 0)
2717 prod0l
= min0
.wide_mul_with_sign (min1
, true, &prod0h
, &overflow
);
2718 if (!uns0
&& min0
.is_negative ())
2720 if (!uns1
&& min1
.is_negative ())
2723 prod1l
= min0
.wide_mul_with_sign (max1
, true, &prod1h
, &overflow
);
2724 if (!uns0
&& min0
.is_negative ())
2726 if (!uns1
&& max1
.is_negative ())
2729 prod2l
= max0
.wide_mul_with_sign (min1
, true, &prod2h
, &overflow
);
2730 if (!uns0
&& max0
.is_negative ())
2732 if (!uns1
&& min1
.is_negative ())
2735 prod3l
= max0
.wide_mul_with_sign (max1
, true, &prod3h
, &overflow
);
2736 if (!uns0
&& max0
.is_negative ())
2738 if (!uns1
&& max1
.is_negative ())
2741 /* Sort the 4 products. */
2742 quad_int_pair_sort (&prod0l
, &prod0h
, &prod3l
, &prod3h
, uns
);
2743 quad_int_pair_sort (&prod1l
, &prod1h
, &prod2l
, &prod2h
, uns
);
2744 quad_int_pair_sort (&prod0l
, &prod0h
, &prod1l
, &prod1h
, uns
);
2745 quad_int_pair_sort (&prod2l
, &prod2h
, &prod3l
, &prod3h
, uns
);
2748 if (prod0l
.is_zero ())
2750 prod1l
= double_int_zero
;
2758 prod2l
= prod3l
+ prod1l
;
2759 prod2h
= prod3h
+ prod1h
;
2760 if (prod2l
.ult (prod3l
))
2761 prod2h
+= double_int_one
; /* carry */
2763 if (!prod2h
.is_zero ()
2764 || prod2l
.cmp (sizem1
, true) >= 0)
2766 /* the range covers all values. */
2767 set_value_range_to_varying (vr
);
2771 /* The following should handle the wrapping and selecting
2772 VR_ANTI_RANGE for us. */
2773 min
= double_int_to_tree (expr_type
, prod0l
);
2774 max
= double_int_to_tree (expr_type
, prod3l
);
2775 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2779 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2780 drop to VR_VARYING. It would take more effort to compute a
2781 precise range for such a case. For example, if we have
2782 op0 == 65536 and op1 == 65536 with their ranges both being
2783 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2784 we cannot claim that the product is in ~[0,0]. Note that we
2785 are guaranteed to have vr0.type == vr1.type at this
2787 if (vr0
.type
== VR_ANTI_RANGE
2788 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2790 set_value_range_to_varying (vr
);
2794 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2797 else if (code
== RSHIFT_EXPR
2798 || code
== LSHIFT_EXPR
)
2800 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2801 then drop to VR_VARYING. Outside of this range we get undefined
2802 behavior from the shift operation. We cannot even trust
2803 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2804 shifts, and the operation at the tree level may be widened. */
2805 if (range_int_cst_p (&vr1
)
2806 && compare_tree_int (vr1
.min
, 0) >= 0
2807 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2809 if (code
== RSHIFT_EXPR
)
2811 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2814 /* We can map lshifts by constants to MULT_EXPR handling. */
2815 else if (code
== LSHIFT_EXPR
2816 && range_int_cst_singleton_p (&vr1
))
2818 bool saved_flag_wrapv
;
2819 value_range_t vr1p
= VR_INITIALIZER
;
2820 vr1p
.type
= VR_RANGE
;
2822 = double_int_to_tree (expr_type
,
2824 .llshift (TREE_INT_CST_LOW (vr1
.min
),
2825 TYPE_PRECISION (expr_type
)));
2826 vr1p
.max
= vr1p
.min
;
2827 /* We have to use a wrapping multiply though as signed overflow
2828 on lshifts is implementation defined in C89. */
2829 saved_flag_wrapv
= flag_wrapv
;
2831 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2833 flag_wrapv
= saved_flag_wrapv
;
2836 else if (code
== LSHIFT_EXPR
2837 && range_int_cst_p (&vr0
))
2839 int prec
= TYPE_PRECISION (expr_type
);
2840 int overflow_pos
= prec
;
2842 double_int bound
, complement
, low_bound
, high_bound
;
2843 bool uns
= TYPE_UNSIGNED (expr_type
);
2844 bool in_bounds
= false;
2849 bound_shift
= overflow_pos
- TREE_INT_CST_LOW (vr1
.max
);
2850 /* If bound_shift == HOST_BITS_PER_DOUBLE_INT, the llshift can
2851 overflow. However, for that to happen, vr1.max needs to be
2852 zero, which means vr1 is a singleton range of zero, which
2853 means it should be handled by the previous LSHIFT_EXPR
2855 bound
= double_int_one
.llshift (bound_shift
, prec
);
2856 complement
= ~(bound
- double_int_one
);
2860 low_bound
= bound
.zext (prec
);
2861 high_bound
= complement
.zext (prec
);
2862 if (tree_to_double_int (vr0
.max
).ult (low_bound
))
2864 /* [5, 6] << [1, 2] == [10, 24]. */
2865 /* We're shifting out only zeroes, the value increases
2869 else if (high_bound
.ult (tree_to_double_int (vr0
.min
)))
2871 /* [0xffffff00, 0xffffffff] << [1, 2]
2872 == [0xfffffc00, 0xfffffffe]. */
2873 /* We're shifting out only ones, the value decreases
2880 /* [-1, 1] << [1, 2] == [-4, 4]. */
2881 low_bound
= complement
.sext (prec
);
2883 if (tree_to_double_int (vr0
.max
).slt (high_bound
)
2884 && low_bound
.slt (tree_to_double_int (vr0
.min
)))
2886 /* For non-negative numbers, we're shifting out only
2887 zeroes, the value increases monotonically.
2888 For negative numbers, we're shifting out only ones, the
2889 value decreases monotomically. */
2896 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2901 set_value_range_to_varying (vr
);
2904 else if (code
== TRUNC_DIV_EXPR
2905 || code
== FLOOR_DIV_EXPR
2906 || code
== CEIL_DIV_EXPR
2907 || code
== EXACT_DIV_EXPR
2908 || code
== ROUND_DIV_EXPR
)
2910 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2912 /* For division, if op1 has VR_RANGE but op0 does not, something
2913 can be deduced just from that range. Say [min, max] / [4, max]
2914 gives [min / 4, max / 4] range. */
2915 if (vr1
.type
== VR_RANGE
2916 && !symbolic_range_p (&vr1
)
2917 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2919 vr0
.type
= type
= VR_RANGE
;
2920 vr0
.min
= vrp_val_min (expr_type
);
2921 vr0
.max
= vrp_val_max (expr_type
);
2925 set_value_range_to_varying (vr
);
2930 /* For divisions, if flag_non_call_exceptions is true, we must
2931 not eliminate a division by zero. */
2932 if (cfun
->can_throw_non_call_exceptions
2933 && (vr1
.type
!= VR_RANGE
2934 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2936 set_value_range_to_varying (vr
);
2940 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2941 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2943 if (vr0
.type
== VR_RANGE
2944 && (vr1
.type
!= VR_RANGE
2945 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2947 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2952 if (TYPE_UNSIGNED (expr_type
)
2953 || value_range_nonnegative_p (&vr1
))
2955 /* For unsigned division or when divisor is known
2956 to be non-negative, the range has to cover
2957 all numbers from 0 to max for positive max
2958 and all numbers from min to 0 for negative min. */
2959 cmp
= compare_values (vr0
.max
, zero
);
2962 else if (cmp
== 0 || cmp
== 1)
2966 cmp
= compare_values (vr0
.min
, zero
);
2969 else if (cmp
== 0 || cmp
== -1)
2976 /* Otherwise the range is -max .. max or min .. -min
2977 depending on which bound is bigger in absolute value,
2978 as the division can change the sign. */
2979 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2982 if (type
== VR_VARYING
)
2984 set_value_range_to_varying (vr
);
2990 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2994 else if (code
== TRUNC_MOD_EXPR
)
2996 if (vr1
.type
!= VR_RANGE
2997 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
2998 || vrp_val_is_min (vr1
.min
))
3000 set_value_range_to_varying (vr
);
3004 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3005 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
3006 if (tree_int_cst_lt (max
, vr1
.max
))
3008 max
= int_const_binop (MINUS_EXPR
, max
, integer_one_node
);
3009 /* If the dividend is non-negative the modulus will be
3010 non-negative as well. */
3011 if (TYPE_UNSIGNED (expr_type
)
3012 || value_range_nonnegative_p (&vr0
))
3013 min
= build_int_cst (TREE_TYPE (max
), 0);
3015 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
3017 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3019 bool int_cst_range0
, int_cst_range1
;
3020 double_int may_be_nonzero0
, may_be_nonzero1
;
3021 double_int must_be_nonzero0
, must_be_nonzero1
;
3023 int_cst_range0
= zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
,
3025 int_cst_range1
= zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
,
3029 if (code
== BIT_AND_EXPR
)
3032 min
= double_int_to_tree (expr_type
,
3033 must_be_nonzero0
& must_be_nonzero1
);
3034 dmax
= may_be_nonzero0
& may_be_nonzero1
;
3035 /* If both input ranges contain only negative values we can
3036 truncate the result range maximum to the minimum of the
3037 input range maxima. */
3038 if (int_cst_range0
&& int_cst_range1
3039 && tree_int_cst_sgn (vr0
.max
) < 0
3040 && tree_int_cst_sgn (vr1
.max
) < 0)
3042 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
3043 TYPE_UNSIGNED (expr_type
));
3044 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
3045 TYPE_UNSIGNED (expr_type
));
3047 /* If either input range contains only non-negative values
3048 we can truncate the result range maximum to the respective
3049 maximum of the input range. */
3050 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3051 dmax
= dmax
.min (tree_to_double_int (vr0
.max
),
3052 TYPE_UNSIGNED (expr_type
));
3053 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3054 dmax
= dmax
.min (tree_to_double_int (vr1
.max
),
3055 TYPE_UNSIGNED (expr_type
));
3056 max
= double_int_to_tree (expr_type
, dmax
);
3058 else if (code
== BIT_IOR_EXPR
)
3061 max
= double_int_to_tree (expr_type
,
3062 may_be_nonzero0
| may_be_nonzero1
);
3063 dmin
= must_be_nonzero0
| must_be_nonzero1
;
3064 /* If the input ranges contain only positive values we can
3065 truncate the minimum of the result range to the maximum
3066 of the input range minima. */
3067 if (int_cst_range0
&& int_cst_range1
3068 && tree_int_cst_sgn (vr0
.min
) >= 0
3069 && tree_int_cst_sgn (vr1
.min
) >= 0)
3071 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3072 TYPE_UNSIGNED (expr_type
));
3073 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3074 TYPE_UNSIGNED (expr_type
));
3076 /* If either input range contains only negative values
3077 we can truncate the minimum of the result range to the
3078 respective minimum range. */
3079 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3080 dmin
= dmin
.max (tree_to_double_int (vr0
.min
),
3081 TYPE_UNSIGNED (expr_type
));
3082 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3083 dmin
= dmin
.max (tree_to_double_int (vr1
.min
),
3084 TYPE_UNSIGNED (expr_type
));
3085 min
= double_int_to_tree (expr_type
, dmin
);
3087 else if (code
== BIT_XOR_EXPR
)
3089 double_int result_zero_bits
, result_one_bits
;
3090 result_zero_bits
= (must_be_nonzero0
& must_be_nonzero1
)
3091 | ~(may_be_nonzero0
| may_be_nonzero1
);
3092 result_one_bits
= must_be_nonzero0
.and_not (may_be_nonzero1
)
3093 | must_be_nonzero1
.and_not (may_be_nonzero0
);
3094 max
= double_int_to_tree (expr_type
, ~result_zero_bits
);
3095 min
= double_int_to_tree (expr_type
, result_one_bits
);
3096 /* If the range has all positive or all negative values the
3097 result is better than VARYING. */
3098 if (tree_int_cst_sgn (min
) < 0
3099 || tree_int_cst_sgn (max
) >= 0)
3102 max
= min
= NULL_TREE
;
3108 /* If either MIN or MAX overflowed, then set the resulting range to
3109 VARYING. But we do accept an overflow infinity
3111 if (min
== NULL_TREE
3112 || !is_gimple_min_invariant (min
)
3113 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
3115 || !is_gimple_min_invariant (max
)
3116 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
3118 set_value_range_to_varying (vr
);
3124 2) [-INF, +-INF(OVF)]
3125 3) [+-INF(OVF), +INF]
3126 4) [+-INF(OVF), +-INF(OVF)]
3127 We learn nothing when we have INF and INF(OVF) on both sides.
3128 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3130 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3131 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3133 set_value_range_to_varying (vr
);
3137 cmp
= compare_values (min
, max
);
3138 if (cmp
== -2 || cmp
== 1)
3140 /* If the new range has its limits swapped around (MIN > MAX),
3141 then the operation caused one of them to wrap around, mark
3142 the new range VARYING. */
3143 set_value_range_to_varying (vr
);
3146 set_value_range (vr
, type
, min
, max
, NULL
);
3149 /* Extract range information from a binary expression OP0 CODE OP1 based on
3150 the ranges of each of its operands with resulting type EXPR_TYPE.
3151 The resulting range is stored in *VR. */
3154 extract_range_from_binary_expr (value_range_t
*vr
,
3155 enum tree_code code
,
3156 tree expr_type
, tree op0
, tree op1
)
3158 value_range_t vr0
= VR_INITIALIZER
;
3159 value_range_t vr1
= VR_INITIALIZER
;
3161 /* Get value ranges for each operand. For constant operands, create
3162 a new value range with the operand to simplify processing. */
3163 if (TREE_CODE (op0
) == SSA_NAME
)
3164 vr0
= *(get_value_range (op0
));
3165 else if (is_gimple_min_invariant (op0
))
3166 set_value_range_to_value (&vr0
, op0
, NULL
);
3168 set_value_range_to_varying (&vr0
);
3170 if (TREE_CODE (op1
) == SSA_NAME
)
3171 vr1
= *(get_value_range (op1
));
3172 else if (is_gimple_min_invariant (op1
))
3173 set_value_range_to_value (&vr1
, op1
, NULL
);
3175 set_value_range_to_varying (&vr1
);
3177 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3180 /* Extract range information from a unary operation CODE based on
3181 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3182 The The resulting range is stored in *VR. */
3185 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3186 enum tree_code code
, tree type
,
3187 value_range_t
*vr0_
, tree op0_type
)
3189 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3191 /* VRP only operates on integral and pointer types. */
3192 if (!(INTEGRAL_TYPE_P (op0_type
)
3193 || POINTER_TYPE_P (op0_type
))
3194 || !(INTEGRAL_TYPE_P (type
)
3195 || POINTER_TYPE_P (type
)))
3197 set_value_range_to_varying (vr
);
3201 /* If VR0 is UNDEFINED, so is the result. */
3202 if (vr0
.type
== VR_UNDEFINED
)
3204 set_value_range_to_undefined (vr
);
3208 /* Handle operations that we express in terms of others. */
3209 if (code
== PAREN_EXPR
)
3211 /* PAREN_EXPR is a simple copy. */
3212 copy_value_range (vr
, &vr0
);
3215 else if (code
== NEGATE_EXPR
)
3217 /* -X is simply 0 - X, so re-use existing code that also handles
3218 anti-ranges fine. */
3219 value_range_t zero
= VR_INITIALIZER
;
3220 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3221 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3224 else if (code
== BIT_NOT_EXPR
)
3226 /* ~X is simply -1 - X, so re-use existing code that also handles
3227 anti-ranges fine. */
3228 value_range_t minusone
= VR_INITIALIZER
;
3229 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3230 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3231 type
, &minusone
, &vr0
);
3235 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3236 and express op ~[] as (op []') U (op []''). */
3237 if (vr0
.type
== VR_ANTI_RANGE
3238 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3240 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3241 if (vrtem1
.type
!= VR_UNDEFINED
)
3243 value_range_t vrres
= VR_INITIALIZER
;
3244 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3246 vrp_meet (vr
, &vrres
);
3251 if (CONVERT_EXPR_CODE_P (code
))
3253 tree inner_type
= op0_type
;
3254 tree outer_type
= type
;
3256 /* If the expression evaluates to a pointer, we are only interested in
3257 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3258 if (POINTER_TYPE_P (type
))
3260 if (range_is_nonnull (&vr0
))
3261 set_value_range_to_nonnull (vr
, type
);
3262 else if (range_is_null (&vr0
))
3263 set_value_range_to_null (vr
, type
);
3265 set_value_range_to_varying (vr
);
3269 /* If VR0 is varying and we increase the type precision, assume
3270 a full range for the following transformation. */
3271 if (vr0
.type
== VR_VARYING
3272 && INTEGRAL_TYPE_P (inner_type
)
3273 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3275 vr0
.type
= VR_RANGE
;
3276 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3277 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3280 /* If VR0 is a constant range or anti-range and the conversion is
3281 not truncating we can convert the min and max values and
3282 canonicalize the resulting range. Otherwise we can do the
3283 conversion if the size of the range is less than what the
3284 precision of the target type can represent and the range is
3285 not an anti-range. */
3286 if ((vr0
.type
== VR_RANGE
3287 || vr0
.type
== VR_ANTI_RANGE
)
3288 && TREE_CODE (vr0
.min
) == INTEGER_CST
3289 && TREE_CODE (vr0
.max
) == INTEGER_CST
3290 && (!is_overflow_infinity (vr0
.min
)
3291 || (vr0
.type
== VR_RANGE
3292 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3293 && needs_overflow_infinity (outer_type
)
3294 && supports_overflow_infinity (outer_type
)))
3295 && (!is_overflow_infinity (vr0
.max
)
3296 || (vr0
.type
== VR_RANGE
3297 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3298 && needs_overflow_infinity (outer_type
)
3299 && supports_overflow_infinity (outer_type
)))
3300 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3301 || (vr0
.type
== VR_RANGE
3302 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3303 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3304 size_int (TYPE_PRECISION (outer_type
)))))))
3306 tree new_min
, new_max
;
3307 if (is_overflow_infinity (vr0
.min
))
3308 new_min
= negative_overflow_infinity (outer_type
);
3310 new_min
= force_fit_type_double (outer_type
,
3311 tree_to_double_int (vr0
.min
),
3313 if (is_overflow_infinity (vr0
.max
))
3314 new_max
= positive_overflow_infinity (outer_type
);
3316 new_max
= force_fit_type_double (outer_type
,
3317 tree_to_double_int (vr0
.max
),
3319 set_and_canonicalize_value_range (vr
, vr0
.type
,
3320 new_min
, new_max
, NULL
);
3324 set_value_range_to_varying (vr
);
3327 else if (code
== ABS_EXPR
)
3332 /* Pass through vr0 in the easy cases. */
3333 if (TYPE_UNSIGNED (type
)
3334 || value_range_nonnegative_p (&vr0
))
3336 copy_value_range (vr
, &vr0
);
3340 /* For the remaining varying or symbolic ranges we can't do anything
3342 if (vr0
.type
== VR_VARYING
3343 || symbolic_range_p (&vr0
))
3345 set_value_range_to_varying (vr
);
3349 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3351 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3352 && ((vr0
.type
== VR_RANGE
3353 && vrp_val_is_min (vr0
.min
))
3354 || (vr0
.type
== VR_ANTI_RANGE
3355 && !vrp_val_is_min (vr0
.min
))))
3357 set_value_range_to_varying (vr
);
3361 /* ABS_EXPR may flip the range around, if the original range
3362 included negative values. */
3363 if (is_overflow_infinity (vr0
.min
))
3364 min
= positive_overflow_infinity (type
);
3365 else if (!vrp_val_is_min (vr0
.min
))
3366 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3367 else if (!needs_overflow_infinity (type
))
3368 min
= TYPE_MAX_VALUE (type
);
3369 else if (supports_overflow_infinity (type
))
3370 min
= positive_overflow_infinity (type
);
3373 set_value_range_to_varying (vr
);
3377 if (is_overflow_infinity (vr0
.max
))
3378 max
= positive_overflow_infinity (type
);
3379 else if (!vrp_val_is_min (vr0
.max
))
3380 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3381 else if (!needs_overflow_infinity (type
))
3382 max
= TYPE_MAX_VALUE (type
);
3383 else if (supports_overflow_infinity (type
)
3384 /* We shouldn't generate [+INF, +INF] as set_value_range
3385 doesn't like this and ICEs. */
3386 && !is_positive_overflow_infinity (min
))
3387 max
= positive_overflow_infinity (type
);
3390 set_value_range_to_varying (vr
);
3394 cmp
= compare_values (min
, max
);
3396 /* If a VR_ANTI_RANGEs contains zero, then we have
3397 ~[-INF, min(MIN, MAX)]. */
3398 if (vr0
.type
== VR_ANTI_RANGE
)
3400 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3402 /* Take the lower of the two values. */
3406 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3407 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3408 flag_wrapv is set and the original anti-range doesn't include
3409 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3410 if (TYPE_OVERFLOW_WRAPS (type
))
3412 tree type_min_value
= TYPE_MIN_VALUE (type
);
3414 min
= (vr0
.min
!= type_min_value
3415 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3421 if (overflow_infinity_range_p (&vr0
))
3422 min
= negative_overflow_infinity (type
);
3424 min
= TYPE_MIN_VALUE (type
);
3429 /* All else has failed, so create the range [0, INF], even for
3430 flag_wrapv since TYPE_MIN_VALUE is in the original
3432 vr0
.type
= VR_RANGE
;
3433 min
= build_int_cst (type
, 0);
3434 if (needs_overflow_infinity (type
))
3436 if (supports_overflow_infinity (type
))
3437 max
= positive_overflow_infinity (type
);
3440 set_value_range_to_varying (vr
);
3445 max
= TYPE_MAX_VALUE (type
);
3449 /* If the range contains zero then we know that the minimum value in the
3450 range will be zero. */
3451 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3455 min
= build_int_cst (type
, 0);
3459 /* If the range was reversed, swap MIN and MAX. */
3468 cmp
= compare_values (min
, max
);
3469 if (cmp
== -2 || cmp
== 1)
3471 /* If the new range has its limits swapped around (MIN > MAX),
3472 then the operation caused one of them to wrap around, mark
3473 the new range VARYING. */
3474 set_value_range_to_varying (vr
);
3477 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3481 /* For unhandled operations fall back to varying. */
3482 set_value_range_to_varying (vr
);
3487 /* Extract range information from a unary expression CODE OP0 based on
3488 the range of its operand with resulting type TYPE.
3489 The resulting range is stored in *VR. */
3492 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3493 tree type
, tree op0
)
3495 value_range_t vr0
= VR_INITIALIZER
;
3497 /* Get value ranges for the operand. For constant operands, create
3498 a new value range with the operand to simplify processing. */
3499 if (TREE_CODE (op0
) == SSA_NAME
)
3500 vr0
= *(get_value_range (op0
));
3501 else if (is_gimple_min_invariant (op0
))
3502 set_value_range_to_value (&vr0
, op0
, NULL
);
3504 set_value_range_to_varying (&vr0
);
3506 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3510 /* Extract range information from a conditional expression STMT based on
3511 the ranges of each of its operands and the expression code. */
3514 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3517 value_range_t vr0
= VR_INITIALIZER
;
3518 value_range_t vr1
= VR_INITIALIZER
;
3520 /* Get value ranges for each operand. For constant operands, create
3521 a new value range with the operand to simplify processing. */
3522 op0
= gimple_assign_rhs2 (stmt
);
3523 if (TREE_CODE (op0
) == SSA_NAME
)
3524 vr0
= *(get_value_range (op0
));
3525 else if (is_gimple_min_invariant (op0
))
3526 set_value_range_to_value (&vr0
, op0
, NULL
);
3528 set_value_range_to_varying (&vr0
);
3530 op1
= gimple_assign_rhs3 (stmt
);
3531 if (TREE_CODE (op1
) == SSA_NAME
)
3532 vr1
= *(get_value_range (op1
));
3533 else if (is_gimple_min_invariant (op1
))
3534 set_value_range_to_value (&vr1
, op1
, NULL
);
3536 set_value_range_to_varying (&vr1
);
3538 /* The resulting value range is the union of the operand ranges */
3539 copy_value_range (vr
, &vr0
);
3540 vrp_meet (vr
, &vr1
);
3544 /* Extract range information from a comparison expression EXPR based
3545 on the range of its operand and the expression code. */
3548 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3549 tree type
, tree op0
, tree op1
)
3554 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3557 /* A disadvantage of using a special infinity as an overflow
3558 representation is that we lose the ability to record overflow
3559 when we don't have an infinity. So we have to ignore a result
3560 which relies on overflow. */
3562 if (val
&& !is_overflow_infinity (val
) && !sop
)
3564 /* Since this expression was found on the RHS of an assignment,
3565 its type may be different from _Bool. Convert VAL to EXPR's
3567 val
= fold_convert (type
, val
);
3568 if (is_gimple_min_invariant (val
))
3569 set_value_range_to_value (vr
, val
, vr
->equiv
);
3571 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3574 /* The result of a comparison is always true or false. */
3575 set_value_range_to_truthvalue (vr
, type
);
3578 /* Try to derive a nonnegative or nonzero range out of STMT relying
3579 primarily on generic routines in fold in conjunction with range data.
3580 Store the result in *VR */
3583 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3586 tree type
= gimple_expr_type (stmt
);
3588 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3590 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3591 int mini
, maxi
, zerov
= 0, prec
;
3593 switch (DECL_FUNCTION_CODE (fndecl
))
3595 case BUILT_IN_CONSTANT_P
:
3596 /* If the call is __builtin_constant_p and the argument is a
3597 function parameter resolve it to false. This avoids bogus
3598 array bound warnings.
3599 ??? We could do this as early as inlining is finished. */
3600 arg
= gimple_call_arg (stmt
, 0);
3601 if (TREE_CODE (arg
) == SSA_NAME
3602 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3603 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3605 set_value_range_to_null (vr
, type
);
3609 /* Both __builtin_ffs* and __builtin_popcount return
3611 CASE_INT_FN (BUILT_IN_FFS
):
3612 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3613 arg
= gimple_call_arg (stmt
, 0);
3614 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3617 if (TREE_CODE (arg
) == SSA_NAME
)
3619 value_range_t
*vr0
= get_value_range (arg
);
3620 /* If arg is non-zero, then ffs or popcount
3622 if (((vr0
->type
== VR_RANGE
3623 && integer_nonzerop (vr0
->min
))
3624 || (vr0
->type
== VR_ANTI_RANGE
3625 && integer_zerop (vr0
->min
)))
3626 && !is_overflow_infinity (vr0
->min
))
3628 /* If some high bits are known to be zero,
3629 we can decrease the maximum. */
3630 if (vr0
->type
== VR_RANGE
3631 && TREE_CODE (vr0
->max
) == INTEGER_CST
3632 && !is_overflow_infinity (vr0
->max
))
3633 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3636 /* __builtin_parity* returns [0, 1]. */
3637 CASE_INT_FN (BUILT_IN_PARITY
):
3641 /* __builtin_c[lt]z* return [0, prec-1], except for
3642 when the argument is 0, but that is undefined behavior.
3643 On many targets where the CLZ RTL or optab value is defined
3644 for 0 the value is prec, so include that in the range
3646 CASE_INT_FN (BUILT_IN_CLZ
):
3647 arg
= gimple_call_arg (stmt
, 0);
3648 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3651 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3653 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3655 /* Handle only the single common value. */
3657 /* Magic value to give up, unless vr0 proves
3660 if (TREE_CODE (arg
) == SSA_NAME
)
3662 value_range_t
*vr0
= get_value_range (arg
);
3663 /* From clz of VR_RANGE minimum we can compute
3665 if (vr0
->type
== VR_RANGE
3666 && TREE_CODE (vr0
->min
) == INTEGER_CST
3667 && !is_overflow_infinity (vr0
->min
))
3669 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3673 else if (vr0
->type
== VR_ANTI_RANGE
3674 && integer_zerop (vr0
->min
)
3675 && !is_overflow_infinity (vr0
->min
))
3682 /* From clz of VR_RANGE maximum we can compute
3684 if (vr0
->type
== VR_RANGE
3685 && TREE_CODE (vr0
->max
) == INTEGER_CST
3686 && !is_overflow_infinity (vr0
->max
))
3688 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3696 /* __builtin_ctz* return [0, prec-1], except for
3697 when the argument is 0, but that is undefined behavior.
3698 If there is a ctz optab for this mode and
3699 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3700 otherwise just assume 0 won't be seen. */
3701 CASE_INT_FN (BUILT_IN_CTZ
):
3702 arg
= gimple_call_arg (stmt
, 0);
3703 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3706 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3708 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3711 /* Handle only the two common values. */
3714 else if (zerov
== prec
)
3717 /* Magic value to give up, unless vr0 proves
3721 if (TREE_CODE (arg
) == SSA_NAME
)
3723 value_range_t
*vr0
= get_value_range (arg
);
3724 /* If arg is non-zero, then use [0, prec - 1]. */
3725 if (((vr0
->type
== VR_RANGE
3726 && integer_nonzerop (vr0
->min
))
3727 || (vr0
->type
== VR_ANTI_RANGE
3728 && integer_zerop (vr0
->min
)))
3729 && !is_overflow_infinity (vr0
->min
))
3734 /* If some high bits are known to be zero,
3735 we can decrease the result maximum. */
3736 if (vr0
->type
== VR_RANGE
3737 && TREE_CODE (vr0
->max
) == INTEGER_CST
3738 && !is_overflow_infinity (vr0
->max
))
3740 maxi
= tree_floor_log2 (vr0
->max
);
3741 /* For vr0 [0, 0] give up. */
3749 /* __builtin_clrsb* returns [0, prec-1]. */
3750 CASE_INT_FN (BUILT_IN_CLRSB
):
3751 arg
= gimple_call_arg (stmt
, 0);
3752 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3757 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3758 build_int_cst (type
, maxi
), NULL
);
3764 if (INTEGRAL_TYPE_P (type
)
3765 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3766 set_value_range_to_nonnegative (vr
, type
,
3767 sop
|| stmt_overflow_infinity (stmt
));
3768 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3770 set_value_range_to_nonnull (vr
, type
);
3772 set_value_range_to_varying (vr
);
3776 /* Try to compute a useful range out of assignment STMT and store it
3780 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
3782 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3784 if (code
== ASSERT_EXPR
)
3785 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3786 else if (code
== SSA_NAME
)
3787 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3788 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3789 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3790 gimple_expr_type (stmt
),
3791 gimple_assign_rhs1 (stmt
),
3792 gimple_assign_rhs2 (stmt
));
3793 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3794 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3795 gimple_expr_type (stmt
),
3796 gimple_assign_rhs1 (stmt
));
3797 else if (code
== COND_EXPR
)
3798 extract_range_from_cond_expr (vr
, stmt
);
3799 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3800 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3801 gimple_expr_type (stmt
),
3802 gimple_assign_rhs1 (stmt
),
3803 gimple_assign_rhs2 (stmt
));
3804 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3805 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3806 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3808 set_value_range_to_varying (vr
);
3810 if (vr
->type
== VR_VARYING
)
3811 extract_range_basic (vr
, stmt
);
3814 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3815 would be profitable to adjust VR using scalar evolution information
3816 for VAR. If so, update VR with the new limits. */
3819 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
3820 gimple stmt
, tree var
)
3822 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3823 enum ev_direction dir
;
3825 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3826 better opportunities than a regular range, but I'm not sure. */
3827 if (vr
->type
== VR_ANTI_RANGE
)
3830 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3832 /* Like in PR19590, scev can return a constant function. */
3833 if (is_gimple_min_invariant (chrec
))
3835 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3839 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3842 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3843 tem
= op_with_constant_singleton_value_range (init
);
3846 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3847 tem
= op_with_constant_singleton_value_range (step
);
3851 /* If STEP is symbolic, we can't know whether INIT will be the
3852 minimum or maximum value in the range. Also, unless INIT is
3853 a simple expression, compare_values and possibly other functions
3854 in tree-vrp won't be able to handle it. */
3855 if (step
== NULL_TREE
3856 || !is_gimple_min_invariant (step
)
3857 || !valid_value_p (init
))
3860 dir
= scev_direction (chrec
);
3861 if (/* Do not adjust ranges if we do not know whether the iv increases
3862 or decreases, ... */
3863 dir
== EV_DIR_UNKNOWN
3864 /* ... or if it may wrap. */
3865 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
3869 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3870 negative_overflow_infinity and positive_overflow_infinity,
3871 because we have concluded that the loop probably does not
3874 type
= TREE_TYPE (var
);
3875 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3876 tmin
= lower_bound_in_type (type
, type
);
3878 tmin
= TYPE_MIN_VALUE (type
);
3879 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3880 tmax
= upper_bound_in_type (type
, type
);
3882 tmax
= TYPE_MAX_VALUE (type
);
3884 /* Try to use estimated number of iterations for the loop to constrain the
3885 final value in the evolution. */
3886 if (TREE_CODE (step
) == INTEGER_CST
3887 && is_gimple_val (init
)
3888 && (TREE_CODE (init
) != SSA_NAME
3889 || get_value_range (init
)->type
== VR_RANGE
))
3893 /* We are only entering here for loop header PHI nodes, so using
3894 the number of latch executions is the correct thing to use. */
3895 if (max_loop_iterations (loop
, &nit
))
3897 value_range_t maxvr
= VR_INITIALIZER
;
3899 bool unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (step
));
3900 bool overflow
= false;
3902 dtmp
= tree_to_double_int (step
)
3903 .mul_with_sign (nit
, unsigned_p
, &overflow
);
3904 /* If the multiplication overflowed we can't do a meaningful
3905 adjustment. Likewise if the result doesn't fit in the type
3906 of the induction variable. For a signed type we have to
3907 check whether the result has the expected signedness which
3908 is that of the step as number of iterations is unsigned. */
3910 && double_int_fits_to_tree_p (TREE_TYPE (init
), dtmp
)
3912 || ((dtmp
.high
^ TREE_INT_CST_HIGH (step
)) >= 0)))
3914 tem
= double_int_to_tree (TREE_TYPE (init
), dtmp
);
3915 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
3916 TREE_TYPE (init
), init
, tem
);
3917 /* Likewise if the addition did. */
3918 if (maxvr
.type
== VR_RANGE
)
3927 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
3932 /* For VARYING or UNDEFINED ranges, just about anything we get
3933 from scalar evolutions should be better. */
3935 if (dir
== EV_DIR_DECREASES
)
3940 /* If we would create an invalid range, then just assume we
3941 know absolutely nothing. This may be over-conservative,
3942 but it's clearly safe, and should happen only in unreachable
3943 parts of code, or for invalid programs. */
3944 if (compare_values (min
, max
) == 1)
3947 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3949 else if (vr
->type
== VR_RANGE
)
3954 if (dir
== EV_DIR_DECREASES
)
3956 /* INIT is the maximum value. If INIT is lower than VR->MAX
3957 but no smaller than VR->MIN, set VR->MAX to INIT. */
3958 if (compare_values (init
, max
) == -1)
3961 /* According to the loop information, the variable does not
3962 overflow. If we think it does, probably because of an
3963 overflow due to arithmetic on a different INF value,
3965 if (is_negative_overflow_infinity (min
)
3966 || compare_values (min
, tmin
) == -1)
3972 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
3973 if (compare_values (init
, min
) == 1)
3976 if (is_positive_overflow_infinity (max
)
3977 || compare_values (tmax
, max
) == -1)
3981 /* If we just created an invalid range with the minimum
3982 greater than the maximum, we fail conservatively.
3983 This should happen only in unreachable
3984 parts of code, or for invalid programs. */
3985 if (compare_values (min
, max
) == 1)
3988 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
3992 /* Return true if VAR may overflow at STMT. This checks any available
3993 loop information to see if we can determine that VAR does not
3997 vrp_var_may_overflow (tree var
, gimple stmt
)
4000 tree chrec
, init
, step
;
4002 if (current_loops
== NULL
)
4005 l
= loop_containing_stmt (stmt
);
4010 chrec
= instantiate_parameters (l
, analyze_scalar_evolution (l
, var
));
4011 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4014 init
= initial_condition_in_loop_num (chrec
, l
->num
);
4015 step
= evolution_part_in_loop_num (chrec
, l
->num
);
4017 if (step
== NULL_TREE
4018 || !is_gimple_min_invariant (step
)
4019 || !valid_value_p (init
))
4022 /* If we get here, we know something useful about VAR based on the
4023 loop information. If it wraps, it may overflow. */
4025 if (scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4029 if (dump_file
&& (dump_flags
& TDF_DETAILS
) != 0)
4031 print_generic_expr (dump_file
, var
, 0);
4032 fprintf (dump_file
, ": loop information indicates does not overflow\n");
4039 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4041 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4042 all the values in the ranges.
4044 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4046 - Return NULL_TREE if it is not always possible to determine the
4047 value of the comparison.
4049 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4050 overflow infinity was used in the test. */
4054 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4055 bool *strict_overflow_p
)
4057 /* VARYING or UNDEFINED ranges cannot be compared. */
4058 if (vr0
->type
== VR_VARYING
4059 || vr0
->type
== VR_UNDEFINED
4060 || vr1
->type
== VR_VARYING
4061 || vr1
->type
== VR_UNDEFINED
)
4064 /* Anti-ranges need to be handled separately. */
4065 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4067 /* If both are anti-ranges, then we cannot compute any
4069 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4072 /* These comparisons are never statically computable. */
4079 /* Equality can be computed only between a range and an
4080 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4081 if (vr0
->type
== VR_RANGE
)
4083 /* To simplify processing, make VR0 the anti-range. */
4084 value_range_t
*tmp
= vr0
;
4089 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4091 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4092 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4093 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4098 if (!usable_range_p (vr0
, strict_overflow_p
)
4099 || !usable_range_p (vr1
, strict_overflow_p
))
4102 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4103 operands around and change the comparison code. */
4104 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4107 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4113 if (comp
== EQ_EXPR
)
4115 /* Equality may only be computed if both ranges represent
4116 exactly one value. */
4117 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4118 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4120 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4122 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4124 if (cmp_min
== 0 && cmp_max
== 0)
4125 return boolean_true_node
;
4126 else if (cmp_min
!= -2 && cmp_max
!= -2)
4127 return boolean_false_node
;
4129 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4130 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4131 strict_overflow_p
) == 1
4132 || compare_values_warnv (vr1
->min
, vr0
->max
,
4133 strict_overflow_p
) == 1)
4134 return boolean_false_node
;
4138 else if (comp
== NE_EXPR
)
4142 /* If VR0 is completely to the left or completely to the right
4143 of VR1, they are always different. Notice that we need to
4144 make sure that both comparisons yield similar results to
4145 avoid comparing values that cannot be compared at
4147 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4148 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4149 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4150 return boolean_true_node
;
4152 /* If VR0 and VR1 represent a single value and are identical,
4154 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4155 strict_overflow_p
) == 0
4156 && compare_values_warnv (vr1
->min
, vr1
->max
,
4157 strict_overflow_p
) == 0
4158 && compare_values_warnv (vr0
->min
, vr1
->min
,
4159 strict_overflow_p
) == 0
4160 && compare_values_warnv (vr0
->max
, vr1
->max
,
4161 strict_overflow_p
) == 0)
4162 return boolean_false_node
;
4164 /* Otherwise, they may or may not be different. */
4168 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4172 /* If VR0 is to the left of VR1, return true. */
4173 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4174 if ((comp
== LT_EXPR
&& tst
== -1)
4175 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4177 if (overflow_infinity_range_p (vr0
)
4178 || overflow_infinity_range_p (vr1
))
4179 *strict_overflow_p
= true;
4180 return boolean_true_node
;
4183 /* If VR0 is to the right of VR1, return false. */
4184 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4185 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4186 || (comp
== LE_EXPR
&& tst
== 1))
4188 if (overflow_infinity_range_p (vr0
)
4189 || overflow_infinity_range_p (vr1
))
4190 *strict_overflow_p
= true;
4191 return boolean_false_node
;
4194 /* Otherwise, we don't know. */
4202 /* Given a value range VR, a value VAL and a comparison code COMP, return
4203 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4204 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4205 always returns false. Return NULL_TREE if it is not always
4206 possible to determine the value of the comparison. Also set
4207 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4208 infinity was used in the test. */
4211 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4212 bool *strict_overflow_p
)
4214 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4217 /* Anti-ranges need to be handled separately. */
4218 if (vr
->type
== VR_ANTI_RANGE
)
4220 /* For anti-ranges, the only predicates that we can compute at
4221 compile time are equality and inequality. */
4228 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4229 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4230 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4235 if (!usable_range_p (vr
, strict_overflow_p
))
4238 if (comp
== EQ_EXPR
)
4240 /* EQ_EXPR may only be computed if VR represents exactly
4242 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4244 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4246 return boolean_true_node
;
4247 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4248 return boolean_false_node
;
4250 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4251 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4252 return boolean_false_node
;
4256 else if (comp
== NE_EXPR
)
4258 /* If VAL is not inside VR, then they are always different. */
4259 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4260 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4261 return boolean_true_node
;
4263 /* If VR represents exactly one value equal to VAL, then return
4265 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4266 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4267 return boolean_false_node
;
4269 /* Otherwise, they may or may not be different. */
4272 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4276 /* If VR is to the left of VAL, return true. */
4277 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4278 if ((comp
== LT_EXPR
&& tst
== -1)
4279 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4281 if (overflow_infinity_range_p (vr
))
4282 *strict_overflow_p
= true;
4283 return boolean_true_node
;
4286 /* If VR is to the right of VAL, return false. */
4287 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4288 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4289 || (comp
== LE_EXPR
&& tst
== 1))
4291 if (overflow_infinity_range_p (vr
))
4292 *strict_overflow_p
= true;
4293 return boolean_false_node
;
4296 /* Otherwise, we don't know. */
4299 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4303 /* If VR is to the right of VAL, return true. */
4304 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4305 if ((comp
== GT_EXPR
&& tst
== 1)
4306 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4308 if (overflow_infinity_range_p (vr
))
4309 *strict_overflow_p
= true;
4310 return boolean_true_node
;
4313 /* If VR is to the left of VAL, return false. */
4314 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4315 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4316 || (comp
== GE_EXPR
&& tst
== -1))
4318 if (overflow_infinity_range_p (vr
))
4319 *strict_overflow_p
= true;
4320 return boolean_false_node
;
4323 /* Otherwise, we don't know. */
4331 /* Debugging dumps. */
4333 void dump_value_range (FILE *, value_range_t
*);
4334 void debug_value_range (value_range_t
*);
4335 void dump_all_value_ranges (FILE *);
4336 void debug_all_value_ranges (void);
4337 void dump_vr_equiv (FILE *, bitmap
);
4338 void debug_vr_equiv (bitmap
);
4341 /* Dump value range VR to FILE. */
4344 dump_value_range (FILE *file
, value_range_t
*vr
)
4347 fprintf (file
, "[]");
4348 else if (vr
->type
== VR_UNDEFINED
)
4349 fprintf (file
, "UNDEFINED");
4350 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4352 tree type
= TREE_TYPE (vr
->min
);
4354 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4356 if (is_negative_overflow_infinity (vr
->min
))
4357 fprintf (file
, "-INF(OVF)");
4358 else if (INTEGRAL_TYPE_P (type
)
4359 && !TYPE_UNSIGNED (type
)
4360 && vrp_val_is_min (vr
->min
))
4361 fprintf (file
, "-INF");
4363 print_generic_expr (file
, vr
->min
, 0);
4365 fprintf (file
, ", ");
4367 if (is_positive_overflow_infinity (vr
->max
))
4368 fprintf (file
, "+INF(OVF)");
4369 else if (INTEGRAL_TYPE_P (type
)
4370 && vrp_val_is_max (vr
->max
))
4371 fprintf (file
, "+INF");
4373 print_generic_expr (file
, vr
->max
, 0);
4375 fprintf (file
, "]");
4382 fprintf (file
, " EQUIVALENCES: { ");
4384 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4386 print_generic_expr (file
, ssa_name (i
), 0);
4387 fprintf (file
, " ");
4391 fprintf (file
, "} (%u elements)", c
);
4394 else if (vr
->type
== VR_VARYING
)
4395 fprintf (file
, "VARYING");
4397 fprintf (file
, "INVALID RANGE");
4401 /* Dump value range VR to stderr. */
4404 debug_value_range (value_range_t
*vr
)
4406 dump_value_range (stderr
, vr
);
4407 fprintf (stderr
, "\n");
4411 /* Dump value ranges of all SSA_NAMEs to FILE. */
4414 dump_all_value_ranges (FILE *file
)
4418 for (i
= 0; i
< num_vr_values
; i
++)
4422 print_generic_expr (file
, ssa_name (i
), 0);
4423 fprintf (file
, ": ");
4424 dump_value_range (file
, vr_value
[i
]);
4425 fprintf (file
, "\n");
4429 fprintf (file
, "\n");
4433 /* Dump all value ranges to stderr. */
4436 debug_all_value_ranges (void)
4438 dump_all_value_ranges (stderr
);
4442 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4443 create a new SSA name N and return the assertion assignment
4444 'V = ASSERT_EXPR <V, V OP W>'. */
4447 build_assert_expr_for (tree cond
, tree v
)
4452 gcc_assert (TREE_CODE (v
) == SSA_NAME
4453 && COMPARISON_CLASS_P (cond
));
4455 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4456 assertion
= gimple_build_assign (NULL_TREE
, a
);
4458 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4459 operand of the ASSERT_EXPR. Create it so the new name and the old one
4460 are registered in the replacement table so that we can fix the SSA web
4461 after adding all the ASSERT_EXPRs. */
4462 create_new_def_for (v
, assertion
, NULL
);
4468 /* Return false if EXPR is a predicate expression involving floating
4472 fp_predicate (gimple stmt
)
4474 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4476 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4479 /* If the range of values taken by OP can be inferred after STMT executes,
4480 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4481 describes the inferred range. Return true if a range could be
4485 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4488 *comp_code_p
= ERROR_MARK
;
4490 /* Do not attempt to infer anything in names that flow through
4492 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4495 /* Similarly, don't infer anything from statements that may throw
4496 exceptions. ??? Relax this requirement? */
4497 if (stmt_could_throw_p (stmt
))
4500 /* If STMT is the last statement of a basic block with no
4501 successors, there is no point inferring anything about any of its
4502 operands. We would not be able to find a proper insertion point
4503 for the assertion, anyway. */
4504 if (stmt_ends_bb_p (stmt
) && EDGE_COUNT (gimple_bb (stmt
)->succs
) == 0)
4507 if (infer_nonnull_range (stmt
, op
))
4509 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4510 *comp_code_p
= NE_EXPR
;
4518 void dump_asserts_for (FILE *, tree
);
4519 void debug_asserts_for (tree
);
4520 void dump_all_asserts (FILE *);
4521 void debug_all_asserts (void);
4523 /* Dump all the registered assertions for NAME to FILE. */
4526 dump_asserts_for (FILE *file
, tree name
)
4530 fprintf (file
, "Assertions to be inserted for ");
4531 print_generic_expr (file
, name
, 0);
4532 fprintf (file
, "\n");
4534 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4537 fprintf (file
, "\t");
4538 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4539 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4542 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4543 loc
->e
->dest
->index
);
4544 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4546 fprintf (file
, "\n\tPREDICATE: ");
4547 print_generic_expr (file
, name
, 0);
4548 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4549 print_generic_expr (file
, loc
->val
, 0);
4550 fprintf (file
, "\n\n");
4554 fprintf (file
, "\n");
4558 /* Dump all the registered assertions for NAME to stderr. */
4561 debug_asserts_for (tree name
)
4563 dump_asserts_for (stderr
, name
);
4567 /* Dump all the registered assertions for all the names to FILE. */
4570 dump_all_asserts (FILE *file
)
4575 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4576 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4577 dump_asserts_for (file
, ssa_name (i
));
4578 fprintf (file
, "\n");
4582 /* Dump all the registered assertions for all the names to stderr. */
4585 debug_all_asserts (void)
4587 dump_all_asserts (stderr
);
4591 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4592 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4593 E->DEST, then register this location as a possible insertion point
4594 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4596 BB, E and SI provide the exact insertion point for the new
4597 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4598 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4599 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4600 must not be NULL. */
4603 register_new_assert_for (tree name
, tree expr
,
4604 enum tree_code comp_code
,
4608 gimple_stmt_iterator si
)
4610 assert_locus_t n
, loc
, last_loc
;
4611 basic_block dest_bb
;
4613 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4616 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4617 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4619 /* Never build an assert comparing against an integer constant with
4620 TREE_OVERFLOW set. This confuses our undefined overflow warning
4622 if (TREE_OVERFLOW_P (val
))
4623 val
= drop_tree_overflow (val
);
4625 /* The new assertion A will be inserted at BB or E. We need to
4626 determine if the new location is dominated by a previously
4627 registered location for A. If we are doing an edge insertion,
4628 assume that A will be inserted at E->DEST. Note that this is not
4631 If E is a critical edge, it will be split. But even if E is
4632 split, the new block will dominate the same set of blocks that
4635 The reverse, however, is not true, blocks dominated by E->DEST
4636 will not be dominated by the new block created to split E. So,
4637 if the insertion location is on a critical edge, we will not use
4638 the new location to move another assertion previously registered
4639 at a block dominated by E->DEST. */
4640 dest_bb
= (bb
) ? bb
: e
->dest
;
4642 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4643 VAL at a block dominating DEST_BB, then we don't need to insert a new
4644 one. Similarly, if the same assertion already exists at a block
4645 dominated by DEST_BB and the new location is not on a critical
4646 edge, then update the existing location for the assertion (i.e.,
4647 move the assertion up in the dominance tree).
4649 Note, this is implemented as a simple linked list because there
4650 should not be more than a handful of assertions registered per
4651 name. If this becomes a performance problem, a table hashed by
4652 COMP_CODE and VAL could be implemented. */
4653 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4657 if (loc
->comp_code
== comp_code
4659 || operand_equal_p (loc
->val
, val
, 0))
4660 && (loc
->expr
== expr
4661 || operand_equal_p (loc
->expr
, expr
, 0)))
4663 /* If E is not a critical edge and DEST_BB
4664 dominates the existing location for the assertion, move
4665 the assertion up in the dominance tree by updating its
4666 location information. */
4667 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4668 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4677 /* Update the last node of the list and move to the next one. */
4682 /* If we didn't find an assertion already registered for
4683 NAME COMP_CODE VAL, add a new one at the end of the list of
4684 assertions associated with NAME. */
4685 n
= XNEW (struct assert_locus_d
);
4689 n
->comp_code
= comp_code
;
4697 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4699 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4702 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4703 Extract a suitable test code and value and store them into *CODE_P and
4704 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4706 If no extraction was possible, return FALSE, otherwise return TRUE.
4708 If INVERT is true, then we invert the result stored into *CODE_P. */
4711 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4712 tree cond_op0
, tree cond_op1
,
4713 bool invert
, enum tree_code
*code_p
,
4716 enum tree_code comp_code
;
4719 /* Otherwise, we have a comparison of the form NAME COMP VAL
4720 or VAL COMP NAME. */
4721 if (name
== cond_op1
)
4723 /* If the predicate is of the form VAL COMP NAME, flip
4724 COMP around because we need to register NAME as the
4725 first operand in the predicate. */
4726 comp_code
= swap_tree_comparison (cond_code
);
4731 /* The comparison is of the form NAME COMP VAL, so the
4732 comparison code remains unchanged. */
4733 comp_code
= cond_code
;
4737 /* Invert the comparison code as necessary. */
4739 comp_code
= invert_tree_comparison (comp_code
, 0);
4741 /* VRP does not handle float types. */
4742 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4745 /* Do not register always-false predicates.
4746 FIXME: this works around a limitation in fold() when dealing with
4747 enumerations. Given 'enum { N1, N2 } x;', fold will not
4748 fold 'if (x > N2)' to 'if (0)'. */
4749 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4750 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4752 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4753 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4755 if (comp_code
== GT_EXPR
4757 || compare_values (val
, max
) == 0))
4760 if (comp_code
== LT_EXPR
4762 || compare_values (val
, min
) == 0))
4765 *code_p
= comp_code
;
4770 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4771 (otherwise return VAL). VAL and MASK must be zero-extended for
4772 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4773 (to transform signed values into unsigned) and at the end xor
4777 masked_increment (double_int val
, double_int mask
, double_int sgnbit
,
4780 double_int bit
= double_int_one
, res
;
4784 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4787 if ((res
& bit
).is_zero ())
4789 res
= bit
- double_int_one
;
4790 res
= (val
+ bit
).and_not (res
);
4793 return res
^ sgnbit
;
4795 return val
^ sgnbit
;
4798 /* Try to register an edge assertion for SSA name NAME on edge E for
4799 the condition COND contributing to the conditional jump pointed to by BSI.
4800 Invert the condition COND if INVERT is true.
4801 Return true if an assertion for NAME could be registered. */
4804 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4805 enum tree_code cond_code
,
4806 tree cond_op0
, tree cond_op1
, bool invert
)
4809 enum tree_code comp_code
;
4810 bool retval
= false;
4812 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4815 invert
, &comp_code
, &val
))
4818 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4819 reachable from E. */
4820 if (live_on_edge (e
, name
)
4821 && !has_single_use (name
))
4823 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4827 /* In the case of NAME <= CST and NAME being defined as
4828 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4829 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4830 This catches range and anti-range tests. */
4831 if ((comp_code
== LE_EXPR
4832 || comp_code
== GT_EXPR
)
4833 && TREE_CODE (val
) == INTEGER_CST
4834 && TYPE_UNSIGNED (TREE_TYPE (val
)))
4836 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4837 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
4839 /* Extract CST2 from the (optional) addition. */
4840 if (is_gimple_assign (def_stmt
)
4841 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
4843 name2
= gimple_assign_rhs1 (def_stmt
);
4844 cst2
= gimple_assign_rhs2 (def_stmt
);
4845 if (TREE_CODE (name2
) == SSA_NAME
4846 && TREE_CODE (cst2
) == INTEGER_CST
)
4847 def_stmt
= SSA_NAME_DEF_STMT (name2
);
4850 /* Extract NAME2 from the (optional) sign-changing cast. */
4851 if (gimple_assign_cast_p (def_stmt
))
4853 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
4854 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
4855 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
4856 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
4857 name3
= gimple_assign_rhs1 (def_stmt
);
4860 /* If name3 is used later, create an ASSERT_EXPR for it. */
4861 if (name3
!= NULL_TREE
4862 && TREE_CODE (name3
) == SSA_NAME
4863 && (cst2
== NULL_TREE
4864 || TREE_CODE (cst2
) == INTEGER_CST
)
4865 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
4866 && live_on_edge (e
, name3
)
4867 && !has_single_use (name3
))
4871 /* Build an expression for the range test. */
4872 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
4873 if (cst2
!= NULL_TREE
)
4874 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4878 fprintf (dump_file
, "Adding assert for ");
4879 print_generic_expr (dump_file
, name3
, 0);
4880 fprintf (dump_file
, " from ");
4881 print_generic_expr (dump_file
, tmp
, 0);
4882 fprintf (dump_file
, "\n");
4885 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4890 /* If name2 is used later, create an ASSERT_EXPR for it. */
4891 if (name2
!= NULL_TREE
4892 && TREE_CODE (name2
) == SSA_NAME
4893 && TREE_CODE (cst2
) == INTEGER_CST
4894 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4895 && live_on_edge (e
, name2
)
4896 && !has_single_use (name2
))
4900 /* Build an expression for the range test. */
4902 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
4903 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
4904 if (cst2
!= NULL_TREE
)
4905 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
4909 fprintf (dump_file
, "Adding assert for ");
4910 print_generic_expr (dump_file
, name2
, 0);
4911 fprintf (dump_file
, " from ");
4912 print_generic_expr (dump_file
, tmp
, 0);
4913 fprintf (dump_file
, "\n");
4916 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
4922 /* In the case of post-in/decrement tests like if (i++) ... and uses
4923 of the in/decremented value on the edge the extra name we want to
4924 assert for is not on the def chain of the name compared. Instead
4925 it is in the set of use stmts. */
4926 if ((comp_code
== NE_EXPR
4927 || comp_code
== EQ_EXPR
)
4928 && TREE_CODE (val
) == INTEGER_CST
)
4930 imm_use_iterator ui
;
4932 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
4934 /* Cut off to use-stmts that are in the predecessor. */
4935 if (gimple_bb (use_stmt
) != e
->src
)
4938 if (!is_gimple_assign (use_stmt
))
4941 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
4942 if (code
!= PLUS_EXPR
4943 && code
!= MINUS_EXPR
)
4946 tree cst
= gimple_assign_rhs2 (use_stmt
);
4947 if (TREE_CODE (cst
) != INTEGER_CST
)
4950 tree name2
= gimple_assign_lhs (use_stmt
);
4951 if (live_on_edge (e
, name2
))
4953 cst
= int_const_binop (code
, val
, cst
);
4954 register_new_assert_for (name2
, name2
, comp_code
, cst
,
4961 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
4962 && TREE_CODE (val
) == INTEGER_CST
)
4964 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
4965 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
4966 tree val2
= NULL_TREE
;
4967 double_int mask
= double_int_zero
;
4968 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
4969 unsigned int nprec
= prec
;
4970 enum tree_code rhs_code
= ERROR_MARK
;
4972 if (is_gimple_assign (def_stmt
))
4973 rhs_code
= gimple_assign_rhs_code (def_stmt
);
4975 /* Add asserts for NAME cmp CST and NAME being defined
4976 as NAME = (int) NAME2. */
4977 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
4978 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
4979 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4980 && gimple_assign_cast_p (def_stmt
))
4982 name2
= gimple_assign_rhs1 (def_stmt
);
4983 if (CONVERT_EXPR_CODE_P (rhs_code
)
4984 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
4985 && TYPE_UNSIGNED (TREE_TYPE (name2
))
4986 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
4987 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
4988 || !tree_int_cst_equal (val
,
4989 TYPE_MIN_VALUE (TREE_TYPE (val
))))
4990 && live_on_edge (e
, name2
)
4991 && !has_single_use (name2
))
4994 enum tree_code new_comp_code
= comp_code
;
4996 cst
= fold_convert (TREE_TYPE (name2
),
4997 TYPE_MIN_VALUE (TREE_TYPE (val
)));
4998 /* Build an expression for the range test. */
4999 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5000 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5001 fold_convert (TREE_TYPE (name2
), val
));
5002 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5004 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5005 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5006 build_int_cst (TREE_TYPE (name2
), 1));
5011 fprintf (dump_file
, "Adding assert for ");
5012 print_generic_expr (dump_file
, name2
, 0);
5013 fprintf (dump_file
, " from ");
5014 print_generic_expr (dump_file
, tmp
, 0);
5015 fprintf (dump_file
, "\n");
5018 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5025 /* Add asserts for NAME cmp CST and NAME being defined as
5026 NAME = NAME2 >> CST2.
5028 Extract CST2 from the right shift. */
5029 if (rhs_code
== RSHIFT_EXPR
)
5031 name2
= gimple_assign_rhs1 (def_stmt
);
5032 cst2
= gimple_assign_rhs2 (def_stmt
);
5033 if (TREE_CODE (name2
) == SSA_NAME
5034 && host_integerp (cst2
, 1)
5035 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5036 && IN_RANGE (tree_low_cst (cst2
, 1), 1, prec
- 1)
5037 && prec
<= HOST_BITS_PER_DOUBLE_INT
5038 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5039 && live_on_edge (e
, name2
)
5040 && !has_single_use (name2
))
5042 mask
= double_int::mask (tree_low_cst (cst2
, 1));
5043 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5046 if (val2
!= NULL_TREE
5047 && TREE_CODE (val2
) == INTEGER_CST
5048 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5052 enum tree_code new_comp_code
= comp_code
;
5056 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5058 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5060 tree type
= build_nonstandard_integer_type (prec
, 1);
5061 tmp
= build1 (NOP_EXPR
, type
, name2
);
5062 val2
= fold_convert (type
, val2
);
5064 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5065 new_val
= double_int_to_tree (TREE_TYPE (tmp
), mask
);
5066 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5068 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5071 = double_int::min_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
5073 if (minval
== tree_to_double_int (new_val
))
5074 new_val
= NULL_TREE
;
5079 = double_int::max_value (prec
, TYPE_UNSIGNED (TREE_TYPE (val
)));
5080 mask
|= tree_to_double_int (val2
);
5082 new_val
= NULL_TREE
;
5084 new_val
= double_int_to_tree (TREE_TYPE (val2
), mask
);
5091 fprintf (dump_file
, "Adding assert for ");
5092 print_generic_expr (dump_file
, name2
, 0);
5093 fprintf (dump_file
, " from ");
5094 print_generic_expr (dump_file
, tmp
, 0);
5095 fprintf (dump_file
, "\n");
5098 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5104 /* Add asserts for NAME cmp CST and NAME being defined as
5105 NAME = NAME2 & CST2.
5107 Extract CST2 from the and.
5110 NAME = (unsigned) NAME2;
5111 casts where NAME's type is unsigned and has smaller precision
5112 than NAME2's type as if it was NAME = NAME2 & MASK. */
5113 names
[0] = NULL_TREE
;
5114 names
[1] = NULL_TREE
;
5116 if (rhs_code
== BIT_AND_EXPR
5117 || (CONVERT_EXPR_CODE_P (rhs_code
)
5118 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5119 && TYPE_UNSIGNED (TREE_TYPE (val
))
5120 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5124 name2
= gimple_assign_rhs1 (def_stmt
);
5125 if (rhs_code
== BIT_AND_EXPR
)
5126 cst2
= gimple_assign_rhs2 (def_stmt
);
5129 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5130 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5132 if (TREE_CODE (name2
) == SSA_NAME
5133 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5134 && TREE_CODE (cst2
) == INTEGER_CST
5135 && !integer_zerop (cst2
)
5136 && nprec
<= HOST_BITS_PER_DOUBLE_INT
5138 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5140 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5141 if (gimple_assign_cast_p (def_stmt2
))
5143 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5144 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5145 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5146 || (TYPE_PRECISION (TREE_TYPE (name2
))
5147 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5148 || !live_on_edge (e
, names
[1])
5149 || has_single_use (names
[1]))
5150 names
[1] = NULL_TREE
;
5152 if (live_on_edge (e
, name2
)
5153 && !has_single_use (name2
))
5157 if (names
[0] || names
[1])
5159 double_int minv
, maxv
= double_int_zero
, valv
, cst2v
;
5160 double_int tem
, sgnbit
;
5161 bool valid_p
= false, valn
= false, cst2n
= false;
5162 enum tree_code ccode
= comp_code
;
5164 valv
= tree_to_double_int (val
).zext (nprec
);
5165 cst2v
= tree_to_double_int (cst2
).zext (nprec
);
5166 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5168 valn
= valv
.sext (nprec
).is_negative ();
5169 cst2n
= cst2v
.sext (nprec
).is_negative ();
5171 /* If CST2 doesn't have most significant bit set,
5172 but VAL is negative, we have comparison like
5173 if ((x & 0x123) > -4) (always true). Just give up. */
5177 sgnbit
= double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
5179 sgnbit
= double_int_zero
;
5180 minv
= valv
& cst2v
;
5184 /* Minimum unsigned value for equality is VAL & CST2
5185 (should be equal to VAL, otherwise we probably should
5186 have folded the comparison into false) and
5187 maximum unsigned value is VAL | ~CST2. */
5188 maxv
= valv
| ~cst2v
;
5189 maxv
= maxv
.zext (nprec
);
5193 tem
= valv
| ~cst2v
;
5194 tem
= tem
.zext (nprec
);
5195 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5196 if (valv
.is_zero ())
5199 sgnbit
= double_int_zero
;
5202 /* If (VAL | ~CST2) is all ones, handle it as
5203 (X & CST2) < VAL. */
5204 if (tem
== double_int::mask (nprec
))
5208 sgnbit
= double_int_zero
;
5212 && cst2v
.sext (nprec
).is_negative ())
5214 = double_int_one
.llshift (nprec
- 1, nprec
).zext (nprec
);
5215 if (!sgnbit
.is_zero ())
5223 if (tem
== double_int::mask (nprec
- 1))
5229 sgnbit
= double_int_zero
;
5233 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5234 is VAL and maximum unsigned value is ~0. For signed
5235 comparison, if CST2 doesn't have most significant bit
5236 set, handle it similarly. If CST2 has MSB set,
5237 the minimum is the same, and maximum is ~0U/2. */
5240 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5242 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5246 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
5251 /* Find out smallest MINV where MINV > VAL
5252 && (MINV & CST2) == MINV, if any. If VAL is signed and
5253 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5254 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5257 maxv
= double_int::mask (nprec
- (cst2n
? 1 : 0));
5261 /* Minimum unsigned value for <= is 0 and maximum
5262 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5263 Otherwise, find smallest VAL2 where VAL2 > VAL
5264 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5266 For signed comparison, if CST2 doesn't have most
5267 significant bit set, handle it similarly. If CST2 has
5268 MSB set, the maximum is the same and minimum is INT_MIN. */
5273 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5276 maxv
-= double_int_one
;
5279 maxv
= maxv
.zext (nprec
);
5285 /* Minimum unsigned value for < is 0 and maximum
5286 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5287 Otherwise, find smallest VAL2 where VAL2 > VAL
5288 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5290 For signed comparison, if CST2 doesn't have most
5291 significant bit set, handle it similarly. If CST2 has
5292 MSB set, the maximum is the same and minimum is INT_MIN. */
5301 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5305 maxv
-= double_int_one
;
5307 maxv
= maxv
.zext (nprec
);
5315 && (maxv
- minv
).zext (nprec
) != double_int::mask (nprec
))
5317 tree tmp
, new_val
, type
;
5320 for (i
= 0; i
< 2; i
++)
5323 double_int maxv2
= maxv
;
5325 type
= TREE_TYPE (names
[i
]);
5326 if (!TYPE_UNSIGNED (type
))
5328 type
= build_nonstandard_integer_type (nprec
, 1);
5329 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5331 if (!minv
.is_zero ())
5333 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5334 double_int_to_tree (type
, -minv
));
5335 maxv2
= maxv
- minv
;
5337 new_val
= double_int_to_tree (type
, maxv2
);
5341 fprintf (dump_file
, "Adding assert for ");
5342 print_generic_expr (dump_file
, names
[i
], 0);
5343 fprintf (dump_file
, " from ");
5344 print_generic_expr (dump_file
, tmp
, 0);
5345 fprintf (dump_file
, "\n");
5348 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5349 new_val
, NULL
, e
, bsi
);
5359 /* OP is an operand of a truth value expression which is known to have
5360 a particular value. Register any asserts for OP and for any
5361 operands in OP's defining statement.
5363 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5364 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5367 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5368 edge e
, gimple_stmt_iterator bsi
)
5370 bool retval
= false;
5373 enum tree_code rhs_code
;
5375 /* We only care about SSA_NAMEs. */
5376 if (TREE_CODE (op
) != SSA_NAME
)
5379 /* We know that OP will have a zero or nonzero value. If OP is used
5380 more than once go ahead and register an assert for OP.
5382 The FOUND_IN_SUBGRAPH support is not helpful in this situation as
5383 it will always be set for OP (because OP is used in a COND_EXPR in
5385 if (!has_single_use (op
))
5387 val
= build_int_cst (TREE_TYPE (op
), 0);
5388 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5392 /* Now look at how OP is set. If it's set from a comparison,
5393 a truth operation or some bit operations, then we may be able
5394 to register information about the operands of that assignment. */
5395 op_def
= SSA_NAME_DEF_STMT (op
);
5396 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5399 rhs_code
= gimple_assign_rhs_code (op_def
);
5401 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5403 bool invert
= (code
== EQ_EXPR
? true : false);
5404 tree op0
= gimple_assign_rhs1 (op_def
);
5405 tree op1
= gimple_assign_rhs2 (op_def
);
5407 if (TREE_CODE (op0
) == SSA_NAME
)
5408 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5410 if (TREE_CODE (op1
) == SSA_NAME
)
5411 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5414 else if ((code
== NE_EXPR
5415 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5417 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5419 /* Recurse on each operand. */
5420 tree op0
= gimple_assign_rhs1 (op_def
);
5421 tree op1
= gimple_assign_rhs2 (op_def
);
5422 if (TREE_CODE (op0
) == SSA_NAME
5423 && has_single_use (op0
))
5424 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5425 if (TREE_CODE (op1
) == SSA_NAME
5426 && has_single_use (op1
))
5427 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5429 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5430 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5432 /* Recurse, flipping CODE. */
5433 code
= invert_tree_comparison (code
, false);
5434 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5437 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5439 /* Recurse through the copy. */
5440 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5443 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5445 /* Recurse through the type conversion. */
5446 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5453 /* Try to register an edge assertion for SSA name NAME on edge E for
5454 the condition COND contributing to the conditional jump pointed to by SI.
5455 Return true if an assertion for NAME could be registered. */
5458 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5459 enum tree_code cond_code
, tree cond_op0
,
5463 enum tree_code comp_code
;
5464 bool retval
= false;
5465 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5467 /* Do not attempt to infer anything in names that flow through
5469 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5472 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5478 /* Register ASSERT_EXPRs for name. */
5479 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5480 cond_op1
, is_else_edge
);
5483 /* If COND is effectively an equality test of an SSA_NAME against
5484 the value zero or one, then we may be able to assert values
5485 for SSA_NAMEs which flow into COND. */
5487 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5488 statement of NAME we can assert both operands of the BIT_AND_EXPR
5489 have nonzero value. */
5490 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5491 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5493 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5495 if (is_gimple_assign (def_stmt
)
5496 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5498 tree op0
= gimple_assign_rhs1 (def_stmt
);
5499 tree op1
= gimple_assign_rhs2 (def_stmt
);
5500 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5501 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5505 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5506 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5508 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5509 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5511 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5513 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5514 necessarily zero value, or if type-precision is one. */
5515 if (is_gimple_assign (def_stmt
)
5516 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5517 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5518 || comp_code
== EQ_EXPR
)))
5520 tree op0
= gimple_assign_rhs1 (def_stmt
);
5521 tree op1
= gimple_assign_rhs2 (def_stmt
);
5522 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5523 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5531 /* Determine whether the outgoing edges of BB should receive an
5532 ASSERT_EXPR for each of the operands of BB's LAST statement.
5533 The last statement of BB must be a COND_EXPR.
5535 If any of the sub-graphs rooted at BB have an interesting use of
5536 the predicate operands, an assert location node is added to the
5537 list of assertions for the corresponding operands. */
5540 find_conditional_asserts (basic_block bb
, gimple last
)
5543 gimple_stmt_iterator bsi
;
5549 need_assert
= false;
5550 bsi
= gsi_for_stmt (last
);
5552 /* Look for uses of the operands in each of the sub-graphs
5553 rooted at BB. We need to check each of the outgoing edges
5554 separately, so that we know what kind of ASSERT_EXPR to
5556 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5561 /* Register the necessary assertions for each operand in the
5562 conditional predicate. */
5563 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5565 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5566 gimple_cond_code (last
),
5567 gimple_cond_lhs (last
),
5568 gimple_cond_rhs (last
));
5581 /* Compare two case labels sorting first by the destination bb index
5582 and then by the case value. */
5585 compare_case_labels (const void *p1
, const void *p2
)
5587 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5588 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5589 int idx1
= ci1
->bb
->index
;
5590 int idx2
= ci2
->bb
->index
;
5594 else if (idx1
== idx2
)
5596 /* Make sure the default label is first in a group. */
5597 if (!CASE_LOW (ci1
->expr
))
5599 else if (!CASE_LOW (ci2
->expr
))
5602 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5603 CASE_LOW (ci2
->expr
));
5609 /* Determine whether the outgoing edges of BB should receive an
5610 ASSERT_EXPR for each of the operands of BB's LAST statement.
5611 The last statement of BB must be a SWITCH_EXPR.
5613 If any of the sub-graphs rooted at BB have an interesting use of
5614 the predicate operands, an assert location node is added to the
5615 list of assertions for the corresponding operands. */
5618 find_switch_asserts (basic_block bb
, gimple last
)
5621 gimple_stmt_iterator bsi
;
5624 struct case_info
*ci
;
5625 size_t n
= gimple_switch_num_labels (last
);
5626 #if GCC_VERSION >= 4000
5629 /* Work around GCC 3.4 bug (PR 37086). */
5630 volatile unsigned int idx
;
5633 need_assert
= false;
5634 bsi
= gsi_for_stmt (last
);
5635 op
= gimple_switch_index (last
);
5636 if (TREE_CODE (op
) != SSA_NAME
)
5639 /* Build a vector of case labels sorted by destination label. */
5640 ci
= XNEWVEC (struct case_info
, n
);
5641 for (idx
= 0; idx
< n
; ++idx
)
5643 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5644 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5646 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5648 for (idx
= 0; idx
< n
; ++idx
)
5651 tree cl
= ci
[idx
].expr
;
5652 basic_block cbb
= ci
[idx
].bb
;
5654 min
= CASE_LOW (cl
);
5655 max
= CASE_HIGH (cl
);
5657 /* If there are multiple case labels with the same destination
5658 we need to combine them to a single value range for the edge. */
5659 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5661 /* Skip labels until the last of the group. */
5664 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5667 /* Pick up the maximum of the case label range. */
5668 if (CASE_HIGH (ci
[idx
].expr
))
5669 max
= CASE_HIGH (ci
[idx
].expr
);
5671 max
= CASE_LOW (ci
[idx
].expr
);
5674 /* Nothing to do if the range includes the default label until we
5675 can register anti-ranges. */
5676 if (min
== NULL_TREE
)
5679 /* Find the edge to register the assert expr on. */
5680 e
= find_edge (bb
, cbb
);
5682 /* Register the necessary assertions for the operand in the
5684 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5685 max
? GE_EXPR
: EQ_EXPR
,
5687 fold_convert (TREE_TYPE (op
),
5691 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5693 fold_convert (TREE_TYPE (op
),
5703 /* Traverse all the statements in block BB looking for statements that
5704 may generate useful assertions for the SSA names in their operand.
5705 If a statement produces a useful assertion A for name N_i, then the
5706 list of assertions already generated for N_i is scanned to
5707 determine if A is actually needed.
5709 If N_i already had the assertion A at a location dominating the
5710 current location, then nothing needs to be done. Otherwise, the
5711 new location for A is recorded instead.
5713 1- For every statement S in BB, all the variables used by S are
5714 added to bitmap FOUND_IN_SUBGRAPH.
5716 2- If statement S uses an operand N in a way that exposes a known
5717 value range for N, then if N was not already generated by an
5718 ASSERT_EXPR, create a new assert location for N. For instance,
5719 if N is a pointer and the statement dereferences it, we can
5720 assume that N is not NULL.
5722 3- COND_EXPRs are a special case of #2. We can derive range
5723 information from the predicate but need to insert different
5724 ASSERT_EXPRs for each of the sub-graphs rooted at the
5725 conditional block. If the last statement of BB is a conditional
5726 expression of the form 'X op Y', then
5728 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5730 b) If the conditional is the only entry point to the sub-graph
5731 corresponding to the THEN_CLAUSE, recurse into it. On
5732 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5733 an ASSERT_EXPR is added for the corresponding variable.
5735 c) Repeat step (b) on the ELSE_CLAUSE.
5737 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5746 In this case, an assertion on the THEN clause is useful to
5747 determine that 'a' is always 9 on that edge. However, an assertion
5748 on the ELSE clause would be unnecessary.
5750 4- If BB does not end in a conditional expression, then we recurse
5751 into BB's dominator children.
5753 At the end of the recursive traversal, every SSA name will have a
5754 list of locations where ASSERT_EXPRs should be added. When a new
5755 location for name N is found, it is registered by calling
5756 register_new_assert_for. That function keeps track of all the
5757 registered assertions to prevent adding unnecessary assertions.
5758 For instance, if a pointer P_4 is dereferenced more than once in a
5759 dominator tree, only the location dominating all the dereference of
5760 P_4 will receive an ASSERT_EXPR.
5762 If this function returns true, then it means that there are names
5763 for which we need to generate ASSERT_EXPRs. Those assertions are
5764 inserted by process_assert_insertions. */
5767 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5769 gimple_stmt_iterator si
;
5773 need_assert
= false;
5774 last
= last_stmt (bb
);
5776 /* If BB's last statement is a conditional statement involving integer
5777 operands, determine if we need to add ASSERT_EXPRs. */
5779 && gimple_code (last
) == GIMPLE_COND
5780 && !fp_predicate (last
)
5781 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5782 need_assert
|= find_conditional_asserts (bb
, last
);
5784 /* If BB's last statement is a switch statement involving integer
5785 operands, determine if we need to add ASSERT_EXPRs. */
5787 && gimple_code (last
) == GIMPLE_SWITCH
5788 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5789 need_assert
|= find_switch_asserts (bb
, last
);
5791 /* Traverse all the statements in BB marking used names and looking
5792 for statements that may infer assertions for their used operands. */
5793 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5799 stmt
= gsi_stmt (si
);
5801 if (is_gimple_debug (stmt
))
5804 /* See if we can derive an assertion for any of STMT's operands. */
5805 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5808 enum tree_code comp_code
;
5810 /* If op is not live beyond this stmt, do not bother to insert
5812 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5815 /* If OP is used in such a way that we can infer a value
5816 range for it, and we don't find a previous assertion for
5817 it, create a new assertion location node for OP. */
5818 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5820 /* If we are able to infer a nonzero value range for OP,
5821 then walk backwards through the use-def chain to see if OP
5822 was set via a typecast.
5824 If so, then we can also infer a nonzero value range
5825 for the operand of the NOP_EXPR. */
5826 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5829 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5831 while (is_gimple_assign (def_stmt
)
5832 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5834 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5836 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5838 t
= gimple_assign_rhs1 (def_stmt
);
5839 def_stmt
= SSA_NAME_DEF_STMT (t
);
5841 /* Note we want to register the assert for the
5842 operand of the NOP_EXPR after SI, not after the
5844 if (! has_single_use (t
))
5846 register_new_assert_for (t
, t
, comp_code
, value
,
5853 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5859 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5860 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5861 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5862 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5865 /* Traverse all PHI nodes in BB, updating live. */
5866 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
5868 use_operand_p arg_p
;
5870 gimple phi
= gsi_stmt (si
);
5871 tree res
= gimple_phi_result (phi
);
5873 if (virtual_operand_p (res
))
5876 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
5878 tree arg
= USE_FROM_PTR (arg_p
);
5879 if (TREE_CODE (arg
) == SSA_NAME
)
5880 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
5883 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
5889 /* Do an RPO walk over the function computing SSA name liveness
5890 on-the-fly and deciding on assert expressions to insert.
5891 Returns true if there are assert expressions to be inserted. */
5894 find_assert_locations (void)
5896 int *rpo
= XNEWVEC (int, last_basic_block
);
5897 int *bb_rpo
= XNEWVEC (int, last_basic_block
);
5898 int *last_rpo
= XCNEWVEC (int, last_basic_block
);
5902 live
= XCNEWVEC (sbitmap
, last_basic_block
);
5903 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
5904 for (i
= 0; i
< rpo_cnt
; ++i
)
5907 need_asserts
= false;
5908 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
5910 basic_block bb
= BASIC_BLOCK (rpo
[i
]);
5916 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
5917 bitmap_clear (live
[rpo
[i
]]);
5920 /* Process BB and update the live information with uses in
5922 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
5924 /* Merge liveness into the predecessor blocks and free it. */
5925 if (!bitmap_empty_p (live
[rpo
[i
]]))
5928 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
5930 int pred
= e
->src
->index
;
5931 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
5936 live
[pred
] = sbitmap_alloc (num_ssa_names
);
5937 bitmap_clear (live
[pred
]);
5939 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
5941 if (bb_rpo
[pred
] < pred_rpo
)
5942 pred_rpo
= bb_rpo
[pred
];
5945 /* Record the RPO number of the last visited block that needs
5946 live information from this block. */
5947 last_rpo
[rpo
[i
]] = pred_rpo
;
5951 sbitmap_free (live
[rpo
[i
]]);
5952 live
[rpo
[i
]] = NULL
;
5955 /* We can free all successors live bitmaps if all their
5956 predecessors have been visited already. */
5957 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5958 if (last_rpo
[e
->dest
->index
] == i
5959 && live
[e
->dest
->index
])
5961 sbitmap_free (live
[e
->dest
->index
]);
5962 live
[e
->dest
->index
] = NULL
;
5967 XDELETEVEC (bb_rpo
);
5968 XDELETEVEC (last_rpo
);
5969 for (i
= 0; i
< last_basic_block
; ++i
)
5971 sbitmap_free (live
[i
]);
5974 return need_asserts
;
5977 /* Create an ASSERT_EXPR for NAME and insert it in the location
5978 indicated by LOC. Return true if we made any edge insertions. */
5981 process_assert_insertions_for (tree name
, assert_locus_t loc
)
5983 /* Build the comparison expression NAME_i COMP_CODE VAL. */
5990 /* If we have X <=> X do not insert an assert expr for that. */
5991 if (loc
->expr
== loc
->val
)
5994 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
5995 assert_stmt
= build_assert_expr_for (cond
, name
);
5998 /* We have been asked to insert the assertion on an edge. This
5999 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6000 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6001 || (gimple_code (gsi_stmt (loc
->si
))
6004 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6008 /* Otherwise, we can insert right after LOC->SI iff the
6009 statement must not be the last statement in the block. */
6010 stmt
= gsi_stmt (loc
->si
);
6011 if (!stmt_ends_bb_p (stmt
))
6013 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6017 /* If STMT must be the last statement in BB, we can only insert new
6018 assertions on the non-abnormal edge out of BB. Note that since
6019 STMT is not control flow, there may only be one non-abnormal edge
6021 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6022 if (!(e
->flags
& EDGE_ABNORMAL
))
6024 gsi_insert_on_edge (e
, assert_stmt
);
6032 /* Process all the insertions registered for every name N_i registered
6033 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6034 found in ASSERTS_FOR[i]. */
6037 process_assert_insertions (void)
6041 bool update_edges_p
= false;
6042 int num_asserts
= 0;
6044 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6045 dump_all_asserts (dump_file
);
6047 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6049 assert_locus_t loc
= asserts_for
[i
];
6054 assert_locus_t next
= loc
->next
;
6055 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6063 gsi_commit_edge_inserts ();
6065 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6070 /* Traverse the flowgraph looking for conditional jumps to insert range
6071 expressions. These range expressions are meant to provide information
6072 to optimizations that need to reason in terms of value ranges. They
6073 will not be expanded into RTL. For instance, given:
6082 this pass will transform the code into:
6088 x = ASSERT_EXPR <x, x < y>
6093 y = ASSERT_EXPR <y, x <= y>
6097 The idea is that once copy and constant propagation have run, other
6098 optimizations will be able to determine what ranges of values can 'x'
6099 take in different paths of the code, simply by checking the reaching
6100 definition of 'x'. */
6103 insert_range_assertions (void)
6105 need_assert_for
= BITMAP_ALLOC (NULL
);
6106 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6108 calculate_dominance_info (CDI_DOMINATORS
);
6110 if (find_assert_locations ())
6112 process_assert_insertions ();
6113 update_ssa (TODO_update_ssa_no_phi
);
6116 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6118 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6119 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6123 BITMAP_FREE (need_assert_for
);
6126 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6127 and "struct" hacks. If VRP can determine that the
6128 array subscript is a constant, check if it is outside valid
6129 range. If the array subscript is a RANGE, warn if it is
6130 non-overlapping with valid range.
6131 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6134 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6136 value_range_t
* vr
= NULL
;
6137 tree low_sub
, up_sub
;
6138 tree low_bound
, up_bound
, up_bound_p1
;
6141 if (TREE_NO_WARNING (ref
))
6144 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6145 up_bound
= array_ref_up_bound (ref
);
6147 /* Can not check flexible arrays. */
6149 || TREE_CODE (up_bound
) != INTEGER_CST
)
6152 /* Accesses to trailing arrays via pointers may access storage
6153 beyond the types array bounds. */
6154 base
= get_base_address (ref
);
6155 if (base
&& TREE_CODE (base
) == MEM_REF
)
6157 tree cref
, next
= NULL_TREE
;
6159 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6162 cref
= TREE_OPERAND (ref
, 0);
6163 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6164 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6165 next
&& TREE_CODE (next
) != FIELD_DECL
;
6166 next
= DECL_CHAIN (next
))
6169 /* If this is the last field in a struct type or a field in a
6170 union type do not warn. */
6175 low_bound
= array_ref_low_bound (ref
);
6176 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
, integer_one_node
);
6178 if (TREE_CODE (low_sub
) == SSA_NAME
)
6180 vr
= get_value_range (low_sub
);
6181 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6183 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6184 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6188 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6190 if (TREE_CODE (up_sub
) == INTEGER_CST
6191 && tree_int_cst_lt (up_bound
, up_sub
)
6192 && TREE_CODE (low_sub
) == INTEGER_CST
6193 && tree_int_cst_lt (low_sub
, low_bound
))
6195 warning_at (location
, OPT_Warray_bounds
,
6196 "array subscript is outside array bounds");
6197 TREE_NO_WARNING (ref
) = 1;
6200 else if (TREE_CODE (up_sub
) == INTEGER_CST
6201 && (ignore_off_by_one
6202 ? (tree_int_cst_lt (up_bound
, up_sub
)
6203 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6204 : (tree_int_cst_lt (up_bound
, up_sub
)
6205 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6207 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6209 fprintf (dump_file
, "Array bound warning for ");
6210 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6211 fprintf (dump_file
, "\n");
6213 warning_at (location
, OPT_Warray_bounds
,
6214 "array subscript is above array bounds");
6215 TREE_NO_WARNING (ref
) = 1;
6217 else if (TREE_CODE (low_sub
) == INTEGER_CST
6218 && tree_int_cst_lt (low_sub
, low_bound
))
6220 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6222 fprintf (dump_file
, "Array bound warning for ");
6223 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6224 fprintf (dump_file
, "\n");
6226 warning_at (location
, OPT_Warray_bounds
,
6227 "array subscript is below array bounds");
6228 TREE_NO_WARNING (ref
) = 1;
6232 /* Searches if the expr T, located at LOCATION computes
6233 address of an ARRAY_REF, and call check_array_ref on it. */
6236 search_for_addr_array (tree t
, location_t location
)
6238 while (TREE_CODE (t
) == SSA_NAME
)
6240 gimple g
= SSA_NAME_DEF_STMT (t
);
6242 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6245 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6246 != GIMPLE_SINGLE_RHS
)
6249 t
= gimple_assign_rhs1 (g
);
6253 /* We are only interested in addresses of ARRAY_REF's. */
6254 if (TREE_CODE (t
) != ADDR_EXPR
)
6257 /* Check each ARRAY_REFs in the reference chain. */
6260 if (TREE_CODE (t
) == ARRAY_REF
)
6261 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6263 t
= TREE_OPERAND (t
, 0);
6265 while (handled_component_p (t
));
6267 if (TREE_CODE (t
) == MEM_REF
6268 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6269 && !TREE_NO_WARNING (t
))
6271 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6272 tree low_bound
, up_bound
, el_sz
;
6274 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6275 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6276 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6279 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6280 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6281 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6283 || TREE_CODE (low_bound
) != INTEGER_CST
6285 || TREE_CODE (up_bound
) != INTEGER_CST
6287 || TREE_CODE (el_sz
) != INTEGER_CST
)
6290 idx
= mem_ref_offset (t
);
6291 idx
= idx
.sdiv (tree_to_double_int (el_sz
), TRUNC_DIV_EXPR
);
6292 if (idx
.slt (double_int_zero
))
6294 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6296 fprintf (dump_file
, "Array bound warning for ");
6297 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6298 fprintf (dump_file
, "\n");
6300 warning_at (location
, OPT_Warray_bounds
,
6301 "array subscript is below array bounds");
6302 TREE_NO_WARNING (t
) = 1;
6304 else if (idx
.sgt (tree_to_double_int (up_bound
)
6305 - tree_to_double_int (low_bound
)
6308 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6310 fprintf (dump_file
, "Array bound warning for ");
6311 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6312 fprintf (dump_file
, "\n");
6314 warning_at (location
, OPT_Warray_bounds
,
6315 "array subscript is above array bounds");
6316 TREE_NO_WARNING (t
) = 1;
6321 /* walk_tree() callback that checks if *TP is
6322 an ARRAY_REF inside an ADDR_EXPR (in which an array
6323 subscript one outside the valid range is allowed). Call
6324 check_array_ref for each ARRAY_REF found. The location is
6328 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6331 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6332 location_t location
;
6334 if (EXPR_HAS_LOCATION (t
))
6335 location
= EXPR_LOCATION (t
);
6338 location_t
*locp
= (location_t
*) wi
->info
;
6342 *walk_subtree
= TRUE
;
6344 if (TREE_CODE (t
) == ARRAY_REF
)
6345 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6347 if (TREE_CODE (t
) == MEM_REF
6348 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6349 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6351 if (TREE_CODE (t
) == ADDR_EXPR
)
6352 *walk_subtree
= FALSE
;
6357 /* Walk over all statements of all reachable BBs and call check_array_bounds
6361 check_all_array_refs (void)
6364 gimple_stmt_iterator si
;
6370 bool executable
= false;
6372 /* Skip blocks that were found to be unreachable. */
6373 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6374 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6378 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6380 gimple stmt
= gsi_stmt (si
);
6381 struct walk_stmt_info wi
;
6382 if (!gimple_has_location (stmt
))
6385 if (is_gimple_call (stmt
))
6388 size_t n
= gimple_call_num_args (stmt
);
6389 for (i
= 0; i
< n
; i
++)
6391 tree arg
= gimple_call_arg (stmt
, i
);
6392 search_for_addr_array (arg
, gimple_location (stmt
));
6397 memset (&wi
, 0, sizeof (wi
));
6398 wi
.info
= CONST_CAST (void *, (const void *)
6399 gimple_location_ptr (stmt
));
6401 walk_gimple_op (gsi_stmt (si
),
6409 /* Return true if all imm uses of VAR are either in STMT, or
6410 feed (optionally through a chain of single imm uses) GIMPLE_COND
6411 in basic block COND_BB. */
6414 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6416 use_operand_p use_p
, use2_p
;
6417 imm_use_iterator iter
;
6419 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6420 if (USE_STMT (use_p
) != stmt
)
6422 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6423 if (is_gimple_debug (use_stmt
))
6425 while (is_gimple_assign (use_stmt
)
6426 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6427 && single_imm_use (gimple_assign_lhs (use_stmt
),
6428 &use2_p
, &use_stmt2
))
6429 use_stmt
= use_stmt2
;
6430 if (gimple_code (use_stmt
) != GIMPLE_COND
6431 || gimple_bb (use_stmt
) != cond_bb
)
6444 __builtin_unreachable ();
6446 x_5 = ASSERT_EXPR <x_3, ...>;
6447 If x_3 has no other immediate uses (checked by caller),
6448 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6449 from the non-zero bitmask. */
6452 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6454 edge e
= single_pred_edge (bb
);
6455 basic_block cond_bb
= e
->src
;
6456 gimple stmt
= last_stmt (cond_bb
);
6460 || gimple_code (stmt
) != GIMPLE_COND
6461 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6462 ? EQ_EXPR
: NE_EXPR
)
6463 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6464 || !integer_zerop (gimple_cond_rhs (stmt
)))
6467 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6468 if (!is_gimple_assign (stmt
)
6469 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6470 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6472 if (gimple_assign_rhs1 (stmt
) != var
)
6476 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6478 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6479 if (!gimple_assign_cast_p (stmt2
)
6480 || gimple_assign_rhs1 (stmt2
) != var
6481 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6482 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6483 != TYPE_PRECISION (TREE_TYPE (var
))))
6486 cst
= gimple_assign_rhs2 (stmt
);
6487 set_nonzero_bits (var
, (get_nonzero_bits (var
)
6488 & ~tree_to_double_int (cst
)));
6491 /* Convert range assertion expressions into the implied copies and
6492 copy propagate away the copies. Doing the trivial copy propagation
6493 here avoids the need to run the full copy propagation pass after
6496 FIXME, this will eventually lead to copy propagation removing the
6497 names that had useful range information attached to them. For
6498 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6499 then N_i will have the range [3, +INF].
6501 However, by converting the assertion into the implied copy
6502 operation N_i = N_j, we will then copy-propagate N_j into the uses
6503 of N_i and lose the range information. We may want to hold on to
6504 ASSERT_EXPRs a little while longer as the ranges could be used in
6505 things like jump threading.
6507 The problem with keeping ASSERT_EXPRs around is that passes after
6508 VRP need to handle them appropriately.
6510 Another approach would be to make the range information a first
6511 class property of the SSA_NAME so that it can be queried from
6512 any pass. This is made somewhat more complex by the need for
6513 multiple ranges to be associated with one SSA_NAME. */
6516 remove_range_assertions (void)
6519 gimple_stmt_iterator si
;
6520 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6521 a basic block preceeded by GIMPLE_COND branching to it and
6522 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6525 /* Note that the BSI iterator bump happens at the bottom of the
6526 loop and no bump is necessary if we're removing the statement
6527 referenced by the current BSI. */
6529 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6531 gimple stmt
= gsi_stmt (si
);
6534 if (is_gimple_assign (stmt
)
6535 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6537 tree lhs
= gimple_assign_lhs (stmt
);
6538 tree rhs
= gimple_assign_rhs1 (stmt
);
6540 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6541 use_operand_p use_p
;
6542 imm_use_iterator iter
;
6544 gcc_assert (cond
!= boolean_false_node
);
6546 var
= ASSERT_EXPR_VAR (rhs
);
6547 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6549 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6550 && SSA_NAME_RANGE_INFO (lhs
))
6552 if (is_unreachable
== -1)
6555 if (single_pred_p (bb
)
6556 && assert_unreachable_fallthru_edge_p
6557 (single_pred_edge (bb
)))
6561 if (x_7 >= 10 && x_7 < 20)
6562 __builtin_unreachable ();
6563 x_8 = ASSERT_EXPR <x_7, ...>;
6564 if the only uses of x_7 are in the ASSERT_EXPR and
6565 in the condition. In that case, we can copy the
6566 range info from x_8 computed in this pass also
6569 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6572 set_range_info (var
, SSA_NAME_RANGE_INFO (lhs
)->min
,
6573 SSA_NAME_RANGE_INFO (lhs
)->max
);
6574 maybe_set_nonzero_bits (bb
, var
);
6578 /* Propagate the RHS into every use of the LHS. */
6579 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6580 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6581 SET_USE (use_p
, var
);
6583 /* And finally, remove the copy, it is not needed. */
6584 gsi_remove (&si
, true);
6585 release_defs (stmt
);
6596 /* Return true if STMT is interesting for VRP. */
6599 stmt_interesting_for_vrp (gimple stmt
)
6601 if (gimple_code (stmt
) == GIMPLE_PHI
)
6603 tree res
= gimple_phi_result (stmt
);
6604 return (!virtual_operand_p (res
)
6605 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6606 || POINTER_TYPE_P (TREE_TYPE (res
))));
6608 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6610 tree lhs
= gimple_get_lhs (stmt
);
6612 /* In general, assignments with virtual operands are not useful
6613 for deriving ranges, with the obvious exception of calls to
6614 builtin functions. */
6615 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6616 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6617 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6618 && (is_gimple_call (stmt
)
6619 || !gimple_vuse (stmt
)))
6622 else if (gimple_code (stmt
) == GIMPLE_COND
6623 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6630 /* Initialize local data structures for VRP. */
6633 vrp_initialize (void)
6637 values_propagated
= false;
6638 num_vr_values
= num_ssa_names
;
6639 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6640 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6644 gimple_stmt_iterator si
;
6646 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6648 gimple phi
= gsi_stmt (si
);
6649 if (!stmt_interesting_for_vrp (phi
))
6651 tree lhs
= PHI_RESULT (phi
);
6652 set_value_range_to_varying (get_value_range (lhs
));
6653 prop_set_simulate_again (phi
, false);
6656 prop_set_simulate_again (phi
, true);
6659 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6661 gimple stmt
= gsi_stmt (si
);
6663 /* If the statement is a control insn, then we do not
6664 want to avoid simulating the statement once. Failure
6665 to do so means that those edges will never get added. */
6666 if (stmt_ends_bb_p (stmt
))
6667 prop_set_simulate_again (stmt
, true);
6668 else if (!stmt_interesting_for_vrp (stmt
))
6672 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6673 set_value_range_to_varying (get_value_range (def
));
6674 prop_set_simulate_again (stmt
, false);
6677 prop_set_simulate_again (stmt
, true);
6682 /* Return the singleton value-range for NAME or NAME. */
6685 vrp_valueize (tree name
)
6687 if (TREE_CODE (name
) == SSA_NAME
)
6689 value_range_t
*vr
= get_value_range (name
);
6690 if (vr
->type
== VR_RANGE
6691 && (vr
->min
== vr
->max
6692 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6698 /* Visit assignment STMT. If it produces an interesting range, record
6699 the SSA name in *OUTPUT_P. */
6701 static enum ssa_prop_result
6702 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6706 enum gimple_code code
= gimple_code (stmt
);
6707 lhs
= gimple_get_lhs (stmt
);
6709 /* We only keep track of ranges in integral and pointer types. */
6710 if (TREE_CODE (lhs
) == SSA_NAME
6711 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6712 /* It is valid to have NULL MIN/MAX values on a type. See
6713 build_range_type. */
6714 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6715 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6716 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6718 value_range_t new_vr
= VR_INITIALIZER
;
6720 /* Try folding the statement to a constant first. */
6721 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6722 if (tem
&& !is_overflow_infinity (tem
))
6723 set_value_range (&new_vr
, VR_RANGE
, tem
, tem
, NULL
);
6724 /* Then dispatch to value-range extracting functions. */
6725 else if (code
== GIMPLE_CALL
)
6726 extract_range_basic (&new_vr
, stmt
);
6728 extract_range_from_assignment (&new_vr
, stmt
);
6730 if (update_value_range (lhs
, &new_vr
))
6734 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6736 fprintf (dump_file
, "Found new range for ");
6737 print_generic_expr (dump_file
, lhs
, 0);
6738 fprintf (dump_file
, ": ");
6739 dump_value_range (dump_file
, &new_vr
);
6740 fprintf (dump_file
, "\n\n");
6743 if (new_vr
.type
== VR_VARYING
)
6744 return SSA_PROP_VARYING
;
6746 return SSA_PROP_INTERESTING
;
6749 return SSA_PROP_NOT_INTERESTING
;
6752 /* Every other statement produces no useful ranges. */
6753 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6754 set_value_range_to_varying (get_value_range (def
));
6756 return SSA_PROP_VARYING
;
6759 /* Helper that gets the value range of the SSA_NAME with version I
6760 or a symbolic range containing the SSA_NAME only if the value range
6761 is varying or undefined. */
6763 static inline value_range_t
6764 get_vr_for_comparison (int i
)
6766 value_range_t vr
= *get_value_range (ssa_name (i
));
6768 /* If name N_i does not have a valid range, use N_i as its own
6769 range. This allows us to compare against names that may
6770 have N_i in their ranges. */
6771 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6774 vr
.min
= ssa_name (i
);
6775 vr
.max
= ssa_name (i
);
6781 /* Compare all the value ranges for names equivalent to VAR with VAL
6782 using comparison code COMP. Return the same value returned by
6783 compare_range_with_value, including the setting of
6784 *STRICT_OVERFLOW_P. */
6787 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6788 bool *strict_overflow_p
)
6794 int used_strict_overflow
;
6796 value_range_t equiv_vr
;
6798 /* Get the set of equivalences for VAR. */
6799 e
= get_value_range (var
)->equiv
;
6801 /* Start at -1. Set it to 0 if we do a comparison without relying
6802 on overflow, or 1 if all comparisons rely on overflow. */
6803 used_strict_overflow
= -1;
6805 /* Compare vars' value range with val. */
6806 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6808 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6810 used_strict_overflow
= sop
? 1 : 0;
6812 /* If the equiv set is empty we have done all work we need to do. */
6816 && used_strict_overflow
> 0)
6817 *strict_overflow_p
= true;
6821 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6823 equiv_vr
= get_vr_for_comparison (i
);
6825 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6828 /* If we get different answers from different members
6829 of the equivalence set this check must be in a dead
6830 code region. Folding it to a trap representation
6831 would be correct here. For now just return don't-know. */
6841 used_strict_overflow
= 0;
6842 else if (used_strict_overflow
< 0)
6843 used_strict_overflow
= 1;
6848 && used_strict_overflow
> 0)
6849 *strict_overflow_p
= true;
6855 /* Given a comparison code COMP and names N1 and N2, compare all the
6856 ranges equivalent to N1 against all the ranges equivalent to N2
6857 to determine the value of N1 COMP N2. Return the same value
6858 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
6859 whether we relied on an overflow infinity in the comparison. */
6863 compare_names (enum tree_code comp
, tree n1
, tree n2
,
6864 bool *strict_overflow_p
)
6868 bitmap_iterator bi1
, bi2
;
6870 int used_strict_overflow
;
6871 static bitmap_obstack
*s_obstack
= NULL
;
6872 static bitmap s_e1
= NULL
, s_e2
= NULL
;
6874 /* Compare the ranges of every name equivalent to N1 against the
6875 ranges of every name equivalent to N2. */
6876 e1
= get_value_range (n1
)->equiv
;
6877 e2
= get_value_range (n2
)->equiv
;
6879 /* Use the fake bitmaps if e1 or e2 are not available. */
6880 if (s_obstack
== NULL
)
6882 s_obstack
= XNEW (bitmap_obstack
);
6883 bitmap_obstack_initialize (s_obstack
);
6884 s_e1
= BITMAP_ALLOC (s_obstack
);
6885 s_e2
= BITMAP_ALLOC (s_obstack
);
6892 /* Add N1 and N2 to their own set of equivalences to avoid
6893 duplicating the body of the loop just to check N1 and N2
6895 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
6896 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
6898 /* If the equivalence sets have a common intersection, then the two
6899 names can be compared without checking their ranges. */
6900 if (bitmap_intersect_p (e1
, e2
))
6902 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6903 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6905 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
6907 : boolean_false_node
;
6910 /* Start at -1. Set it to 0 if we do a comparison without relying
6911 on overflow, or 1 if all comparisons rely on overflow. */
6912 used_strict_overflow
= -1;
6914 /* Otherwise, compare all the equivalent ranges. First, add N1 and
6915 N2 to their own set of equivalences to avoid duplicating the body
6916 of the loop just to check N1 and N2 ranges. */
6917 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
6919 value_range_t vr1
= get_vr_for_comparison (i1
);
6921 t
= retval
= NULL_TREE
;
6922 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
6926 value_range_t vr2
= get_vr_for_comparison (i2
);
6928 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
6931 /* If we get different answers from different members
6932 of the equivalence set this check must be in a dead
6933 code region. Folding it to a trap representation
6934 would be correct here. For now just return don't-know. */
6938 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6939 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6945 used_strict_overflow
= 0;
6946 else if (used_strict_overflow
< 0)
6947 used_strict_overflow
= 1;
6953 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6954 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6955 if (used_strict_overflow
> 0)
6956 *strict_overflow_p
= true;
6961 /* None of the equivalent ranges are useful in computing this
6963 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
6964 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
6968 /* Helper function for vrp_evaluate_conditional_warnv. */
6971 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
6973 bool * strict_overflow_p
)
6975 value_range_t
*vr0
, *vr1
;
6977 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
6978 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
6981 return compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
6982 else if (vr0
&& vr1
== NULL
)
6983 return compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
6984 else if (vr0
== NULL
&& vr1
)
6985 return (compare_range_with_value
6986 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
6990 /* Helper function for vrp_evaluate_conditional_warnv. */
6993 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
6994 tree op1
, bool use_equiv_p
,
6995 bool *strict_overflow_p
, bool *only_ranges
)
6999 *only_ranges
= true;
7001 /* We only deal with integral and pointer types. */
7002 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7003 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7009 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7010 (code
, op0
, op1
, strict_overflow_p
)))
7012 *only_ranges
= false;
7013 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7014 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7015 else if (TREE_CODE (op0
) == SSA_NAME
)
7016 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7017 else if (TREE_CODE (op1
) == SSA_NAME
)
7018 return (compare_name_with_value
7019 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7022 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7027 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7028 information. Return NULL if the conditional can not be evaluated.
7029 The ranges of all the names equivalent with the operands in COND
7030 will be used when trying to compute the value. If the result is
7031 based on undefined signed overflow, issue a warning if
7035 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7041 /* Some passes and foldings leak constants with overflow flag set
7042 into the IL. Avoid doing wrong things with these and bail out. */
7043 if ((TREE_CODE (op0
) == INTEGER_CST
7044 && TREE_OVERFLOW (op0
))
7045 || (TREE_CODE (op1
) == INTEGER_CST
7046 && TREE_OVERFLOW (op1
)))
7050 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7055 enum warn_strict_overflow_code wc
;
7056 const char* warnmsg
;
7058 if (is_gimple_min_invariant (ret
))
7060 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7061 warnmsg
= G_("assuming signed overflow does not occur when "
7062 "simplifying conditional to constant");
7066 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7067 warnmsg
= G_("assuming signed overflow does not occur when "
7068 "simplifying conditional");
7071 if (issue_strict_overflow_warning (wc
))
7073 location_t location
;
7075 if (!gimple_has_location (stmt
))
7076 location
= input_location
;
7078 location
= gimple_location (stmt
);
7079 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7083 if (warn_type_limits
7084 && ret
&& only_ranges
7085 && TREE_CODE_CLASS (code
) == tcc_comparison
7086 && TREE_CODE (op0
) == SSA_NAME
)
7088 /* If the comparison is being folded and the operand on the LHS
7089 is being compared against a constant value that is outside of
7090 the natural range of OP0's type, then the predicate will
7091 always fold regardless of the value of OP0. If -Wtype-limits
7092 was specified, emit a warning. */
7093 tree type
= TREE_TYPE (op0
);
7094 value_range_t
*vr0
= get_value_range (op0
);
7096 if (vr0
->type
!= VR_VARYING
7097 && INTEGRAL_TYPE_P (type
)
7098 && vrp_val_is_min (vr0
->min
)
7099 && vrp_val_is_max (vr0
->max
)
7100 && is_gimple_min_invariant (op1
))
7102 location_t location
;
7104 if (!gimple_has_location (stmt
))
7105 location
= input_location
;
7107 location
= gimple_location (stmt
);
7109 warning_at (location
, OPT_Wtype_limits
,
7111 ? G_("comparison always false "
7112 "due to limited range of data type")
7113 : G_("comparison always true "
7114 "due to limited range of data type"));
7122 /* Visit conditional statement STMT. If we can determine which edge
7123 will be taken out of STMT's basic block, record it in
7124 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7125 SSA_PROP_VARYING. */
7127 static enum ssa_prop_result
7128 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
7133 *taken_edge_p
= NULL
;
7135 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7140 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7141 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7142 fprintf (dump_file
, "\nWith known ranges\n");
7144 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7146 fprintf (dump_file
, "\t");
7147 print_generic_expr (dump_file
, use
, 0);
7148 fprintf (dump_file
, ": ");
7149 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7152 fprintf (dump_file
, "\n");
7155 /* Compute the value of the predicate COND by checking the known
7156 ranges of each of its operands.
7158 Note that we cannot evaluate all the equivalent ranges here
7159 because those ranges may not yet be final and with the current
7160 propagation strategy, we cannot determine when the value ranges
7161 of the names in the equivalence set have changed.
7163 For instance, given the following code fragment
7167 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7171 Assume that on the first visit to i_14, i_5 has the temporary
7172 range [8, 8] because the second argument to the PHI function is
7173 not yet executable. We derive the range ~[0, 0] for i_14 and the
7174 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7175 the first time, since i_14 is equivalent to the range [8, 8], we
7176 determine that the predicate is always false.
7178 On the next round of propagation, i_13 is determined to be
7179 VARYING, which causes i_5 to drop down to VARYING. So, another
7180 visit to i_14 is scheduled. In this second visit, we compute the
7181 exact same range and equivalence set for i_14, namely ~[0, 0] and
7182 { i_5 }. But we did not have the previous range for i_5
7183 registered, so vrp_visit_assignment thinks that the range for
7184 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7185 is not visited again, which stops propagation from visiting
7186 statements in the THEN clause of that if().
7188 To properly fix this we would need to keep the previous range
7189 value for the names in the equivalence set. This way we would've
7190 discovered that from one visit to the other i_5 changed from
7191 range [8, 8] to VR_VARYING.
7193 However, fixing this apparent limitation may not be worth the
7194 additional checking. Testing on several code bases (GCC, DLV,
7195 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7196 4 more predicates folded in SPEC. */
7199 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7200 gimple_cond_lhs (stmt
),
7201 gimple_cond_rhs (stmt
),
7206 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7209 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7211 "\nIgnoring predicate evaluation because "
7212 "it assumes that signed overflow is undefined");
7217 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7219 fprintf (dump_file
, "\nPredicate evaluates to: ");
7220 if (val
== NULL_TREE
)
7221 fprintf (dump_file
, "DON'T KNOW\n");
7223 print_generic_stmt (dump_file
, val
, 0);
7226 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7229 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7230 that includes the value VAL. The search is restricted to the range
7231 [START_IDX, n - 1] where n is the size of VEC.
7233 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7236 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7237 it is placed in IDX and false is returned.
7239 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7243 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
7245 size_t n
= gimple_switch_num_labels (stmt
);
7248 /* Find case label for minimum of the value range or the next one.
7249 At each iteration we are searching in [low, high - 1]. */
7251 for (low
= start_idx
, high
= n
; high
!= low
; )
7255 /* Note that i != high, so we never ask for n. */
7256 size_t i
= (high
+ low
) / 2;
7257 t
= gimple_switch_label (stmt
, i
);
7259 /* Cache the result of comparing CASE_LOW and val. */
7260 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7264 /* Ranges cannot be empty. */
7273 if (CASE_HIGH (t
) != NULL
7274 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7286 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7287 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7288 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7289 then MAX_IDX < MIN_IDX.
7290 Returns true if the default label is not needed. */
7293 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
7297 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7298 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7302 && max_take_default
)
7304 /* Only the default case label reached.
7305 Return an empty range. */
7312 bool take_default
= min_take_default
|| max_take_default
;
7316 if (max_take_default
)
7319 /* If the case label range is continuous, we do not need
7320 the default case label. Verify that. */
7321 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7322 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7323 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7324 for (k
= i
+ 1; k
<= j
; ++k
)
7326 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7327 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7329 take_default
= true;
7333 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7334 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7339 return !take_default
;
7343 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7344 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7345 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7346 Returns true if the default label is not needed. */
7349 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7350 size_t *max_idx1
, size_t *min_idx2
,
7354 unsigned int n
= gimple_switch_num_labels (stmt
);
7356 tree case_low
, case_high
;
7357 tree min
= vr
->min
, max
= vr
->max
;
7359 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7361 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7363 /* Set second range to emtpy. */
7367 if (vr
->type
== VR_RANGE
)
7371 return !take_default
;
7374 /* Set first range to all case labels. */
7381 /* Make sure all the values of case labels [i , j] are contained in
7382 range [MIN, MAX]. */
7383 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7384 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7385 if (tree_int_cst_compare (case_low
, min
) < 0)
7387 if (case_high
!= NULL_TREE
7388 && tree_int_cst_compare (max
, case_high
) < 0)
7394 /* If the range spans case labels [i, j], the corresponding anti-range spans
7395 the labels [1, i - 1] and [j + 1, n - 1]. */
7421 /* Visit switch statement STMT. If we can determine which edge
7422 will be taken out of STMT's basic block, record it in
7423 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7424 SSA_PROP_VARYING. */
7426 static enum ssa_prop_result
7427 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7431 size_t i
= 0, j
= 0, k
, l
;
7434 *taken_edge_p
= NULL
;
7435 op
= gimple_switch_index (stmt
);
7436 if (TREE_CODE (op
) != SSA_NAME
)
7437 return SSA_PROP_VARYING
;
7439 vr
= get_value_range (op
);
7440 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7442 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7443 print_generic_expr (dump_file
, op
, 0);
7444 fprintf (dump_file
, " with known range ");
7445 dump_value_range (dump_file
, vr
);
7446 fprintf (dump_file
, "\n");
7449 if ((vr
->type
!= VR_RANGE
7450 && vr
->type
!= VR_ANTI_RANGE
)
7451 || symbolic_range_p (vr
))
7452 return SSA_PROP_VARYING
;
7454 /* Find the single edge that is taken from the switch expression. */
7455 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7457 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7461 gcc_assert (take_default
);
7462 val
= gimple_switch_default_label (stmt
);
7466 /* Check if labels with index i to j and maybe the default label
7467 are all reaching the same label. */
7469 val
= gimple_switch_label (stmt
, i
);
7471 && CASE_LABEL (gimple_switch_default_label (stmt
))
7472 != CASE_LABEL (val
))
7474 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7475 fprintf (dump_file
, " not a single destination for this "
7477 return SSA_PROP_VARYING
;
7479 for (++i
; i
<= j
; ++i
)
7481 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7483 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7484 fprintf (dump_file
, " not a single destination for this "
7486 return SSA_PROP_VARYING
;
7491 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7493 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7494 fprintf (dump_file
, " not a single destination for this "
7496 return SSA_PROP_VARYING
;
7501 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7502 label_to_block (CASE_LABEL (val
)));
7504 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7506 fprintf (dump_file
, " will take edge to ");
7507 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7510 return SSA_PROP_INTERESTING
;
7514 /* Evaluate statement STMT. If the statement produces a useful range,
7515 return SSA_PROP_INTERESTING and record the SSA name with the
7516 interesting range into *OUTPUT_P.
7518 If STMT is a conditional branch and we can determine its truth
7519 value, the taken edge is recorded in *TAKEN_EDGE_P.
7521 If STMT produces a varying value, return SSA_PROP_VARYING. */
7523 static enum ssa_prop_result
7524 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7529 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7531 fprintf (dump_file
, "\nVisiting statement:\n");
7532 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7533 fprintf (dump_file
, "\n");
7536 if (!stmt_interesting_for_vrp (stmt
))
7537 gcc_assert (stmt_ends_bb_p (stmt
));
7538 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7539 return vrp_visit_assignment_or_call (stmt
, output_p
);
7540 else if (gimple_code (stmt
) == GIMPLE_COND
)
7541 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7542 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7543 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7545 /* All other statements produce nothing of interest for VRP, so mark
7546 their outputs varying and prevent further simulation. */
7547 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7548 set_value_range_to_varying (get_value_range (def
));
7550 return SSA_PROP_VARYING
;
7553 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7554 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7555 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7556 possible such range. The resulting range is not canonicalized. */
7559 union_ranges (enum value_range_type
*vr0type
,
7560 tree
*vr0min
, tree
*vr0max
,
7561 enum value_range_type vr1type
,
7562 tree vr1min
, tree vr1max
)
7564 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7565 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7567 /* [] is vr0, () is vr1 in the following classification comments. */
7571 if (*vr0type
== vr1type
)
7572 /* Nothing to do for equal ranges. */
7574 else if ((*vr0type
== VR_RANGE
7575 && vr1type
== VR_ANTI_RANGE
)
7576 || (*vr0type
== VR_ANTI_RANGE
7577 && vr1type
== VR_RANGE
))
7579 /* For anti-range with range union the result is varying. */
7585 else if (operand_less_p (*vr0max
, vr1min
) == 1
7586 || operand_less_p (vr1max
, *vr0min
) == 1)
7588 /* [ ] ( ) or ( ) [ ]
7589 If the ranges have an empty intersection, result of the union
7590 operation is the anti-range or if both are anti-ranges
7592 if (*vr0type
== VR_ANTI_RANGE
7593 && vr1type
== VR_ANTI_RANGE
)
7595 else if (*vr0type
== VR_ANTI_RANGE
7596 && vr1type
== VR_RANGE
)
7598 else if (*vr0type
== VR_RANGE
7599 && vr1type
== VR_ANTI_RANGE
)
7605 else if (*vr0type
== VR_RANGE
7606 && vr1type
== VR_RANGE
)
7608 /* The result is the convex hull of both ranges. */
7609 if (operand_less_p (*vr0max
, vr1min
) == 1)
7611 /* If the result can be an anti-range, create one. */
7612 if (TREE_CODE (*vr0max
) == INTEGER_CST
7613 && TREE_CODE (vr1min
) == INTEGER_CST
7614 && vrp_val_is_min (*vr0min
)
7615 && vrp_val_is_max (vr1max
))
7617 tree min
= int_const_binop (PLUS_EXPR
,
7618 *vr0max
, integer_one_node
);
7619 tree max
= int_const_binop (MINUS_EXPR
,
7620 vr1min
, integer_one_node
);
7621 if (!operand_less_p (max
, min
))
7623 *vr0type
= VR_ANTI_RANGE
;
7635 /* If the result can be an anti-range, create one. */
7636 if (TREE_CODE (vr1max
) == INTEGER_CST
7637 && TREE_CODE (*vr0min
) == INTEGER_CST
7638 && vrp_val_is_min (vr1min
)
7639 && vrp_val_is_max (*vr0max
))
7641 tree min
= int_const_binop (PLUS_EXPR
,
7642 vr1max
, integer_one_node
);
7643 tree max
= int_const_binop (MINUS_EXPR
,
7644 *vr0min
, integer_one_node
);
7645 if (!operand_less_p (max
, min
))
7647 *vr0type
= VR_ANTI_RANGE
;
7661 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7662 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7664 /* [ ( ) ] or [( ) ] or [ ( )] */
7665 if (*vr0type
== VR_RANGE
7666 && vr1type
== VR_RANGE
)
7668 else if (*vr0type
== VR_ANTI_RANGE
7669 && vr1type
== VR_ANTI_RANGE
)
7675 else if (*vr0type
== VR_ANTI_RANGE
7676 && vr1type
== VR_RANGE
)
7678 /* Arbitrarily choose the right or left gap. */
7679 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7680 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7681 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7682 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7686 else if (*vr0type
== VR_RANGE
7687 && vr1type
== VR_ANTI_RANGE
)
7688 /* The result covers everything. */
7693 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7694 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7696 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7697 if (*vr0type
== VR_RANGE
7698 && vr1type
== VR_RANGE
)
7704 else if (*vr0type
== VR_ANTI_RANGE
7705 && vr1type
== VR_ANTI_RANGE
)
7707 else if (*vr0type
== VR_RANGE
7708 && vr1type
== VR_ANTI_RANGE
)
7710 *vr0type
= VR_ANTI_RANGE
;
7711 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7713 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7716 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7718 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7724 else if (*vr0type
== VR_ANTI_RANGE
7725 && vr1type
== VR_RANGE
)
7726 /* The result covers everything. */
7731 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7732 || operand_equal_p (vr1min
, *vr0max
, 0))
7733 && operand_less_p (*vr0min
, vr1min
) == 1)
7735 /* [ ( ] ) or [ ]( ) */
7736 if (*vr0type
== VR_RANGE
7737 && vr1type
== VR_RANGE
)
7739 else if (*vr0type
== VR_ANTI_RANGE
7740 && vr1type
== VR_ANTI_RANGE
)
7742 else if (*vr0type
== VR_ANTI_RANGE
7743 && vr1type
== VR_RANGE
)
7745 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7746 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
, integer_one_node
);
7750 else if (*vr0type
== VR_RANGE
7751 && vr1type
== VR_ANTI_RANGE
)
7753 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7756 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
, integer_one_node
);
7765 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7766 || operand_equal_p (*vr0min
, vr1max
, 0))
7767 && operand_less_p (vr1min
, *vr0min
) == 1)
7769 /* ( [ ) ] or ( )[ ] */
7770 if (*vr0type
== VR_RANGE
7771 && vr1type
== VR_RANGE
)
7773 else if (*vr0type
== VR_ANTI_RANGE
7774 && vr1type
== VR_ANTI_RANGE
)
7776 else if (*vr0type
== VR_ANTI_RANGE
7777 && vr1type
== VR_RANGE
)
7779 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7780 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7784 else if (*vr0type
== VR_RANGE
7785 && vr1type
== VR_ANTI_RANGE
)
7787 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7791 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
, integer_one_node
);
7805 *vr0type
= VR_VARYING
;
7806 *vr0min
= NULL_TREE
;
7807 *vr0max
= NULL_TREE
;
7810 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7811 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7812 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7813 possible such range. The resulting range is not canonicalized. */
7816 intersect_ranges (enum value_range_type
*vr0type
,
7817 tree
*vr0min
, tree
*vr0max
,
7818 enum value_range_type vr1type
,
7819 tree vr1min
, tree vr1max
)
7821 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7822 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7824 /* [] is vr0, () is vr1 in the following classification comments. */
7828 if (*vr0type
== vr1type
)
7829 /* Nothing to do for equal ranges. */
7831 else if ((*vr0type
== VR_RANGE
7832 && vr1type
== VR_ANTI_RANGE
)
7833 || (*vr0type
== VR_ANTI_RANGE
7834 && vr1type
== VR_RANGE
))
7836 /* For anti-range with range intersection the result is empty. */
7837 *vr0type
= VR_UNDEFINED
;
7838 *vr0min
= NULL_TREE
;
7839 *vr0max
= NULL_TREE
;
7844 else if (operand_less_p (*vr0max
, vr1min
) == 1
7845 || operand_less_p (vr1max
, *vr0min
) == 1)
7847 /* [ ] ( ) or ( ) [ ]
7848 If the ranges have an empty intersection, the result of the
7849 intersect operation is the range for intersecting an
7850 anti-range with a range or empty when intersecting two ranges. */
7851 if (*vr0type
== VR_RANGE
7852 && vr1type
== VR_ANTI_RANGE
)
7854 else if (*vr0type
== VR_ANTI_RANGE
7855 && vr1type
== VR_RANGE
)
7861 else if (*vr0type
== VR_RANGE
7862 && vr1type
== VR_RANGE
)
7864 *vr0type
= VR_UNDEFINED
;
7865 *vr0min
= NULL_TREE
;
7866 *vr0max
= NULL_TREE
;
7868 else if (*vr0type
== VR_ANTI_RANGE
7869 && vr1type
== VR_ANTI_RANGE
)
7871 /* If the anti-ranges are adjacent to each other merge them. */
7872 if (TREE_CODE (*vr0max
) == INTEGER_CST
7873 && TREE_CODE (vr1min
) == INTEGER_CST
7874 && operand_less_p (*vr0max
, vr1min
) == 1
7875 && integer_onep (int_const_binop (MINUS_EXPR
,
7878 else if (TREE_CODE (vr1max
) == INTEGER_CST
7879 && TREE_CODE (*vr0min
) == INTEGER_CST
7880 && operand_less_p (vr1max
, *vr0min
) == 1
7881 && integer_onep (int_const_binop (MINUS_EXPR
,
7884 /* Else arbitrarily take VR0. */
7887 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7888 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7890 /* [ ( ) ] or [( ) ] or [ ( )] */
7891 if (*vr0type
== VR_RANGE
7892 && vr1type
== VR_RANGE
)
7894 /* If both are ranges the result is the inner one. */
7899 else if (*vr0type
== VR_RANGE
7900 && vr1type
== VR_ANTI_RANGE
)
7902 /* Choose the right gap if the left one is empty. */
7905 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7906 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
, integer_one_node
);
7910 /* Choose the left gap if the right one is empty. */
7913 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7914 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7919 /* Choose the anti-range if the range is effectively varying. */
7920 else if (vrp_val_is_min (*vr0min
)
7921 && vrp_val_is_max (*vr0max
))
7927 /* Else choose the range. */
7929 else if (*vr0type
== VR_ANTI_RANGE
7930 && vr1type
== VR_ANTI_RANGE
)
7931 /* If both are anti-ranges the result is the outer one. */
7933 else if (*vr0type
== VR_ANTI_RANGE
7934 && vr1type
== VR_RANGE
)
7936 /* The intersection is empty. */
7937 *vr0type
= VR_UNDEFINED
;
7938 *vr0min
= NULL_TREE
;
7939 *vr0max
= NULL_TREE
;
7944 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7945 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7947 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7948 if (*vr0type
== VR_RANGE
7949 && vr1type
== VR_RANGE
)
7950 /* Choose the inner range. */
7952 else if (*vr0type
== VR_ANTI_RANGE
7953 && vr1type
== VR_RANGE
)
7955 /* Choose the right gap if the left is empty. */
7958 *vr0type
= VR_RANGE
;
7959 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7960 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7966 /* Choose the left gap if the right is empty. */
7969 *vr0type
= VR_RANGE
;
7970 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7971 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7977 /* Choose the anti-range if the range is effectively varying. */
7978 else if (vrp_val_is_min (vr1min
)
7979 && vrp_val_is_max (vr1max
))
7981 /* Else choose the range. */
7989 else if (*vr0type
== VR_ANTI_RANGE
7990 && vr1type
== VR_ANTI_RANGE
)
7992 /* If both are anti-ranges the result is the outer one. */
7997 else if (vr1type
== VR_ANTI_RANGE
7998 && *vr0type
== VR_RANGE
)
8000 /* The intersection is empty. */
8001 *vr0type
= VR_UNDEFINED
;
8002 *vr0min
= NULL_TREE
;
8003 *vr0max
= NULL_TREE
;
8008 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8009 || operand_equal_p (vr1min
, *vr0max
, 0))
8010 && operand_less_p (*vr0min
, vr1min
) == 1)
8012 /* [ ( ] ) or [ ]( ) */
8013 if (*vr0type
== VR_ANTI_RANGE
8014 && vr1type
== VR_ANTI_RANGE
)
8016 else if (*vr0type
== VR_RANGE
8017 && vr1type
== VR_RANGE
)
8019 else if (*vr0type
== VR_RANGE
8020 && vr1type
== VR_ANTI_RANGE
)
8022 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8023 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8028 else if (*vr0type
== VR_ANTI_RANGE
8029 && vr1type
== VR_RANGE
)
8031 *vr0type
= VR_RANGE
;
8032 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8033 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8042 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8043 || operand_equal_p (*vr0min
, vr1max
, 0))
8044 && operand_less_p (vr1min
, *vr0min
) == 1)
8046 /* ( [ ) ] or ( )[ ] */
8047 if (*vr0type
== VR_ANTI_RANGE
8048 && vr1type
== VR_ANTI_RANGE
)
8050 else if (*vr0type
== VR_RANGE
8051 && vr1type
== VR_RANGE
)
8053 else if (*vr0type
== VR_RANGE
8054 && vr1type
== VR_ANTI_RANGE
)
8056 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8057 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8062 else if (*vr0type
== VR_ANTI_RANGE
8063 && vr1type
== VR_RANGE
)
8065 *vr0type
= VR_RANGE
;
8066 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8067 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8077 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8078 result for the intersection. That's always a conservative
8079 correct estimate. */
8085 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8086 in *VR0. This may not be the smallest possible such range. */
8089 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8091 value_range_t saved
;
8093 /* If either range is VR_VARYING the other one wins. */
8094 if (vr1
->type
== VR_VARYING
)
8096 if (vr0
->type
== VR_VARYING
)
8098 copy_value_range (vr0
, vr1
);
8102 /* When either range is VR_UNDEFINED the resulting range is
8103 VR_UNDEFINED, too. */
8104 if (vr0
->type
== VR_UNDEFINED
)
8106 if (vr1
->type
== VR_UNDEFINED
)
8108 set_value_range_to_undefined (vr0
);
8112 /* Save the original vr0 so we can return it as conservative intersection
8113 result when our worker turns things to varying. */
8115 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8116 vr1
->type
, vr1
->min
, vr1
->max
);
8117 /* Make sure to canonicalize the result though as the inversion of a
8118 VR_RANGE can still be a VR_RANGE. */
8119 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8120 vr0
->min
, vr0
->max
, vr0
->equiv
);
8121 /* If that failed, use the saved original VR0. */
8122 if (vr0
->type
== VR_VARYING
)
8127 /* If the result is VR_UNDEFINED there is no need to mess with
8128 the equivalencies. */
8129 if (vr0
->type
== VR_UNDEFINED
)
8132 /* The resulting set of equivalences for range intersection is the union of
8134 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8135 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8136 else if (vr1
->equiv
&& !vr0
->equiv
)
8137 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8141 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8143 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8145 fprintf (dump_file
, "Intersecting\n ");
8146 dump_value_range (dump_file
, vr0
);
8147 fprintf (dump_file
, "\nand\n ");
8148 dump_value_range (dump_file
, vr1
);
8149 fprintf (dump_file
, "\n");
8151 vrp_intersect_ranges_1 (vr0
, vr1
);
8152 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8154 fprintf (dump_file
, "to\n ");
8155 dump_value_range (dump_file
, vr0
);
8156 fprintf (dump_file
, "\n");
8160 /* Meet operation for value ranges. Given two value ranges VR0 and
8161 VR1, store in VR0 a range that contains both VR0 and VR1. This
8162 may not be the smallest possible such range. */
8165 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8167 value_range_t saved
;
8169 if (vr0
->type
== VR_UNDEFINED
)
8171 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8175 if (vr1
->type
== VR_UNDEFINED
)
8177 /* VR0 already has the resulting range. */
8181 if (vr0
->type
== VR_VARYING
)
8183 /* Nothing to do. VR0 already has the resulting range. */
8187 if (vr1
->type
== VR_VARYING
)
8189 set_value_range_to_varying (vr0
);
8194 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8195 vr1
->type
, vr1
->min
, vr1
->max
);
8196 if (vr0
->type
== VR_VARYING
)
8198 /* Failed to find an efficient meet. Before giving up and setting
8199 the result to VARYING, see if we can at least derive a useful
8200 anti-range. FIXME, all this nonsense about distinguishing
8201 anti-ranges from ranges is necessary because of the odd
8202 semantics of range_includes_zero_p and friends. */
8203 if (((saved
.type
== VR_RANGE
8204 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8205 || (saved
.type
== VR_ANTI_RANGE
8206 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8207 && ((vr1
->type
== VR_RANGE
8208 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8209 || (vr1
->type
== VR_ANTI_RANGE
8210 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8212 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8214 /* Since this meet operation did not result from the meeting of
8215 two equivalent names, VR0 cannot have any equivalences. */
8217 bitmap_clear (vr0
->equiv
);
8221 set_value_range_to_varying (vr0
);
8224 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8226 if (vr0
->type
== VR_VARYING
)
8229 /* The resulting set of equivalences is always the intersection of
8231 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8232 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8233 else if (vr0
->equiv
&& !vr1
->equiv
)
8234 bitmap_clear (vr0
->equiv
);
8238 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8240 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8242 fprintf (dump_file
, "Meeting\n ");
8243 dump_value_range (dump_file
, vr0
);
8244 fprintf (dump_file
, "\nand\n ");
8245 dump_value_range (dump_file
, vr1
);
8246 fprintf (dump_file
, "\n");
8248 vrp_meet_1 (vr0
, vr1
);
8249 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8251 fprintf (dump_file
, "to\n ");
8252 dump_value_range (dump_file
, vr0
);
8253 fprintf (dump_file
, "\n");
8258 /* Visit all arguments for PHI node PHI that flow through executable
8259 edges. If a valid value range can be derived from all the incoming
8260 value ranges, set a new range for the LHS of PHI. */
8262 static enum ssa_prop_result
8263 vrp_visit_phi_node (gimple phi
)
8266 tree lhs
= PHI_RESULT (phi
);
8267 value_range_t
*lhs_vr
= get_value_range (lhs
);
8268 value_range_t vr_result
= VR_INITIALIZER
;
8270 int edges
, old_edges
;
8273 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8275 fprintf (dump_file
, "\nVisiting PHI node: ");
8276 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8280 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8282 edge e
= gimple_phi_arg_edge (phi
, i
);
8284 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8287 "\n Argument #%d (%d -> %d %sexecutable)\n",
8288 (int) i
, e
->src
->index
, e
->dest
->index
,
8289 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8292 if (e
->flags
& EDGE_EXECUTABLE
)
8294 tree arg
= PHI_ARG_DEF (phi
, i
);
8295 value_range_t vr_arg
;
8299 if (TREE_CODE (arg
) == SSA_NAME
)
8301 vr_arg
= *(get_value_range (arg
));
8302 /* Do not allow equivalences or symbolic ranges to leak in from
8303 backedges. That creates invalid equivalencies.
8304 See PR53465 and PR54767. */
8305 if (e
->flags
& EDGE_DFS_BACK
8306 && (vr_arg
.type
== VR_RANGE
8307 || vr_arg
.type
== VR_ANTI_RANGE
))
8309 vr_arg
.equiv
= NULL
;
8310 if (symbolic_range_p (&vr_arg
))
8312 vr_arg
.type
= VR_VARYING
;
8313 vr_arg
.min
= NULL_TREE
;
8314 vr_arg
.max
= NULL_TREE
;
8320 if (is_overflow_infinity (arg
))
8321 arg
= drop_tree_overflow (arg
);
8323 vr_arg
.type
= VR_RANGE
;
8326 vr_arg
.equiv
= NULL
;
8329 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8331 fprintf (dump_file
, "\t");
8332 print_generic_expr (dump_file
, arg
, dump_flags
);
8333 fprintf (dump_file
, "\n\tValue: ");
8334 dump_value_range (dump_file
, &vr_arg
);
8335 fprintf (dump_file
, "\n");
8339 copy_value_range (&vr_result
, &vr_arg
);
8341 vrp_meet (&vr_result
, &vr_arg
);
8344 if (vr_result
.type
== VR_VARYING
)
8349 if (vr_result
.type
== VR_VARYING
)
8351 else if (vr_result
.type
== VR_UNDEFINED
)
8354 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8355 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8357 /* To prevent infinite iterations in the algorithm, derive ranges
8358 when the new value is slightly bigger or smaller than the
8359 previous one. We don't do this if we have seen a new executable
8360 edge; this helps us avoid an overflow infinity for conditionals
8361 which are not in a loop. If the old value-range was VR_UNDEFINED
8362 use the updated range and iterate one more time. */
8364 && gimple_phi_num_args (phi
) > 1
8365 && edges
== old_edges
8366 && lhs_vr
->type
!= VR_UNDEFINED
)
8368 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8369 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8371 /* For non VR_RANGE or for pointers fall back to varying if
8372 the range changed. */
8373 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8374 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8375 && (cmp_min
!= 0 || cmp_max
!= 0))
8378 /* If the new minimum is smaller or larger than the previous
8379 one, go all the way to -INF. In the first case, to avoid
8380 iterating millions of times to reach -INF, and in the
8381 other case to avoid infinite bouncing between different
8383 if (cmp_min
> 0 || cmp_min
< 0)
8385 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.min
))
8386 || !vrp_var_may_overflow (lhs
, phi
))
8387 vr_result
.min
= TYPE_MIN_VALUE (TREE_TYPE (vr_result
.min
));
8388 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.min
)))
8390 negative_overflow_infinity (TREE_TYPE (vr_result
.min
));
8393 /* Similarly, if the new maximum is smaller or larger than
8394 the previous one, go all the way to +INF. */
8395 if (cmp_max
< 0 || cmp_max
> 0)
8397 if (!needs_overflow_infinity (TREE_TYPE (vr_result
.max
))
8398 || !vrp_var_may_overflow (lhs
, phi
))
8399 vr_result
.max
= TYPE_MAX_VALUE (TREE_TYPE (vr_result
.max
));
8400 else if (supports_overflow_infinity (TREE_TYPE (vr_result
.max
)))
8402 positive_overflow_infinity (TREE_TYPE (vr_result
.max
));
8405 /* If we dropped either bound to +-INF then if this is a loop
8406 PHI node SCEV may known more about its value-range. */
8407 if ((cmp_min
> 0 || cmp_min
< 0
8408 || cmp_max
< 0 || cmp_max
> 0)
8410 && (l
= loop_containing_stmt (phi
))
8411 && l
->header
== gimple_bb (phi
))
8412 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8414 /* If we will end up with a (-INF, +INF) range, set it to
8415 VARYING. Same if the previous max value was invalid for
8416 the type and we end up with vr_result.min > vr_result.max. */
8417 if ((vrp_val_is_max (vr_result
.max
)
8418 && vrp_val_is_min (vr_result
.min
))
8419 || compare_values (vr_result
.min
,
8424 /* If the new range is different than the previous value, keep
8427 if (update_value_range (lhs
, &vr_result
))
8429 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8431 fprintf (dump_file
, "Found new range for ");
8432 print_generic_expr (dump_file
, lhs
, 0);
8433 fprintf (dump_file
, ": ");
8434 dump_value_range (dump_file
, &vr_result
);
8435 fprintf (dump_file
, "\n\n");
8438 return SSA_PROP_INTERESTING
;
8441 /* Nothing changed, don't add outgoing edges. */
8442 return SSA_PROP_NOT_INTERESTING
;
8444 /* No match found. Set the LHS to VARYING. */
8446 set_value_range_to_varying (lhs_vr
);
8447 return SSA_PROP_VARYING
;
8450 /* Simplify boolean operations if the source is known
8451 to be already a boolean. */
8453 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8455 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8457 bool need_conversion
;
8459 /* We handle only !=/== case here. */
8460 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8462 op0
= gimple_assign_rhs1 (stmt
);
8463 if (!op_with_boolean_value_range_p (op0
))
8466 op1
= gimple_assign_rhs2 (stmt
);
8467 if (!op_with_boolean_value_range_p (op1
))
8470 /* Reduce number of cases to handle to NE_EXPR. As there is no
8471 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8472 if (rhs_code
== EQ_EXPR
)
8474 if (TREE_CODE (op1
) == INTEGER_CST
)
8475 op1
= int_const_binop (BIT_XOR_EXPR
, op1
, integer_one_node
);
8480 lhs
= gimple_assign_lhs (stmt
);
8482 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8484 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8486 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8487 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8488 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8491 /* For A != 0 we can substitute A itself. */
8492 if (integer_zerop (op1
))
8493 gimple_assign_set_rhs_with_ops (gsi
,
8495 ? NOP_EXPR
: TREE_CODE (op0
),
8497 /* For A != B we substitute A ^ B. Either with conversion. */
8498 else if (need_conversion
)
8500 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8501 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8502 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8503 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8507 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8508 update_stmt (gsi_stmt (*gsi
));
8513 /* Simplify a division or modulo operator to a right shift or
8514 bitwise and if the first operand is unsigned or is greater
8515 than zero and the second operand is an exact power of two. */
8518 simplify_div_or_mod_using_ranges (gimple stmt
)
8520 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8522 tree op0
= gimple_assign_rhs1 (stmt
);
8523 tree op1
= gimple_assign_rhs2 (stmt
);
8524 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8526 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8528 val
= integer_one_node
;
8534 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8538 && integer_onep (val
)
8539 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8541 location_t location
;
8543 if (!gimple_has_location (stmt
))
8544 location
= input_location
;
8546 location
= gimple_location (stmt
);
8547 warning_at (location
, OPT_Wstrict_overflow
,
8548 "assuming signed overflow does not occur when "
8549 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8553 if (val
&& integer_onep (val
))
8557 if (rhs_code
== TRUNC_DIV_EXPR
)
8559 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8560 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8561 gimple_assign_set_rhs1 (stmt
, op0
);
8562 gimple_assign_set_rhs2 (stmt
, t
);
8566 t
= build_int_cst (TREE_TYPE (op1
), 1);
8567 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8568 t
= fold_convert (TREE_TYPE (op0
), t
);
8570 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8571 gimple_assign_set_rhs1 (stmt
, op0
);
8572 gimple_assign_set_rhs2 (stmt
, t
);
8582 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8583 ABS_EXPR. If the operand is <= 0, then simplify the
8584 ABS_EXPR into a NEGATE_EXPR. */
8587 simplify_abs_using_ranges (gimple stmt
)
8590 tree op
= gimple_assign_rhs1 (stmt
);
8591 tree type
= TREE_TYPE (op
);
8592 value_range_t
*vr
= get_value_range (op
);
8594 if (TYPE_UNSIGNED (type
))
8596 val
= integer_zero_node
;
8602 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8606 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8611 if (integer_zerop (val
))
8612 val
= integer_one_node
;
8613 else if (integer_onep (val
))
8614 val
= integer_zero_node
;
8619 && (integer_onep (val
) || integer_zerop (val
)))
8621 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8623 location_t location
;
8625 if (!gimple_has_location (stmt
))
8626 location
= input_location
;
8628 location
= gimple_location (stmt
);
8629 warning_at (location
, OPT_Wstrict_overflow
,
8630 "assuming signed overflow does not occur when "
8631 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8634 gimple_assign_set_rhs1 (stmt
, op
);
8635 if (integer_onep (val
))
8636 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8638 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8647 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8648 If all the bits that are being cleared by & are already
8649 known to be zero from VR, or all the bits that are being
8650 set by | are already known to be one from VR, the bit
8651 operation is redundant. */
8654 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8656 tree op0
= gimple_assign_rhs1 (stmt
);
8657 tree op1
= gimple_assign_rhs2 (stmt
);
8658 tree op
= NULL_TREE
;
8659 value_range_t vr0
= VR_INITIALIZER
;
8660 value_range_t vr1
= VR_INITIALIZER
;
8661 double_int may_be_nonzero0
, may_be_nonzero1
;
8662 double_int must_be_nonzero0
, must_be_nonzero1
;
8665 if (TREE_CODE (op0
) == SSA_NAME
)
8666 vr0
= *(get_value_range (op0
));
8667 else if (is_gimple_min_invariant (op0
))
8668 set_value_range_to_value (&vr0
, op0
, NULL
);
8672 if (TREE_CODE (op1
) == SSA_NAME
)
8673 vr1
= *(get_value_range (op1
));
8674 else if (is_gimple_min_invariant (op1
))
8675 set_value_range_to_value (&vr1
, op1
, NULL
);
8679 if (!zero_nonzero_bits_from_vr (&vr0
, &may_be_nonzero0
, &must_be_nonzero0
))
8681 if (!zero_nonzero_bits_from_vr (&vr1
, &may_be_nonzero1
, &must_be_nonzero1
))
8684 switch (gimple_assign_rhs_code (stmt
))
8687 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8688 if (mask
.is_zero ())
8693 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8694 if (mask
.is_zero ())
8701 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8702 if (mask
.is_zero ())
8707 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8708 if (mask
.is_zero ())
8718 if (op
== NULL_TREE
)
8721 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8722 update_stmt (gsi_stmt (*gsi
));
8726 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8727 a known value range VR.
8729 If there is one and only one value which will satisfy the
8730 conditional, then return that value. Else return NULL. */
8733 test_for_singularity (enum tree_code cond_code
, tree op0
,
8734 tree op1
, value_range_t
*vr
)
8739 /* Extract minimum/maximum values which satisfy the
8740 the conditional as it was written. */
8741 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8743 /* This should not be negative infinity; there is no overflow
8745 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8748 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8750 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8751 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8753 TREE_NO_WARNING (max
) = 1;
8756 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8758 /* This should not be positive infinity; there is no overflow
8760 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8763 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8765 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8766 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8768 TREE_NO_WARNING (min
) = 1;
8772 /* Now refine the minimum and maximum values using any
8773 value range information we have for op0. */
8776 if (compare_values (vr
->min
, min
) == 1)
8778 if (compare_values (vr
->max
, max
) == -1)
8781 /* If the new min/max values have converged to a single value,
8782 then there is only one value which can satisfy the condition,
8783 return that value. */
8784 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8790 /* Return whether the value range *VR fits in an integer type specified
8791 by PRECISION and UNSIGNED_P. */
8794 range_fits_type_p (value_range_t
*vr
, unsigned precision
, bool unsigned_p
)
8797 unsigned src_precision
;
8800 /* We can only handle integral and pointer types. */
8801 src_type
= TREE_TYPE (vr
->min
);
8802 if (!INTEGRAL_TYPE_P (src_type
)
8803 && !POINTER_TYPE_P (src_type
))
8806 /* An extension is fine unless VR is signed and unsigned_p,
8807 and so is an identity transform. */
8808 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8809 if ((src_precision
< precision
8810 && !(unsigned_p
&& !TYPE_UNSIGNED (src_type
)))
8811 || (src_precision
== precision
8812 && TYPE_UNSIGNED (src_type
) == unsigned_p
))
8815 /* Now we can only handle ranges with constant bounds. */
8816 if (vr
->type
!= VR_RANGE
8817 || TREE_CODE (vr
->min
) != INTEGER_CST
8818 || TREE_CODE (vr
->max
) != INTEGER_CST
)
8821 /* For sign changes, the MSB of the double_int has to be clear.
8822 An unsigned value with its MSB set cannot be represented by
8823 a signed double_int, while a negative value cannot be represented
8824 by an unsigned double_int. */
8825 if (TYPE_UNSIGNED (src_type
) != unsigned_p
8826 && (TREE_INT_CST_HIGH (vr
->min
) | TREE_INT_CST_HIGH (vr
->max
)) < 0)
8829 /* Then we can perform the conversion on both ends and compare
8830 the result for equality. */
8831 tem
= tree_to_double_int (vr
->min
).ext (precision
, unsigned_p
);
8832 if (tree_to_double_int (vr
->min
) != tem
)
8834 tem
= tree_to_double_int (vr
->max
).ext (precision
, unsigned_p
);
8835 if (tree_to_double_int (vr
->max
) != tem
)
8841 /* Simplify a conditional using a relational operator to an equality
8842 test if the range information indicates only one value can satisfy
8843 the original conditional. */
8846 simplify_cond_using_ranges (gimple stmt
)
8848 tree op0
= gimple_cond_lhs (stmt
);
8849 tree op1
= gimple_cond_rhs (stmt
);
8850 enum tree_code cond_code
= gimple_cond_code (stmt
);
8852 if (cond_code
!= NE_EXPR
8853 && cond_code
!= EQ_EXPR
8854 && TREE_CODE (op0
) == SSA_NAME
8855 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
8856 && is_gimple_min_invariant (op1
))
8858 value_range_t
*vr
= get_value_range (op0
);
8860 /* If we have range information for OP0, then we might be
8861 able to simplify this conditional. */
8862 if (vr
->type
== VR_RANGE
)
8864 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8870 fprintf (dump_file
, "Simplified relational ");
8871 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8872 fprintf (dump_file
, " into ");
8875 gimple_cond_set_code (stmt
, EQ_EXPR
);
8876 gimple_cond_set_lhs (stmt
, op0
);
8877 gimple_cond_set_rhs (stmt
, new_tree
);
8883 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8884 fprintf (dump_file
, "\n");
8890 /* Try again after inverting the condition. We only deal
8891 with integral types here, so no need to worry about
8892 issues with inverting FP comparisons. */
8893 cond_code
= invert_tree_comparison (cond_code
, false);
8894 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
8900 fprintf (dump_file
, "Simplified relational ");
8901 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8902 fprintf (dump_file
, " into ");
8905 gimple_cond_set_code (stmt
, NE_EXPR
);
8906 gimple_cond_set_lhs (stmt
, op0
);
8907 gimple_cond_set_rhs (stmt
, new_tree
);
8913 print_gimple_stmt (dump_file
, stmt
, 0, 0);
8914 fprintf (dump_file
, "\n");
8922 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
8923 see if OP0 was set by a type conversion where the source of
8924 the conversion is another SSA_NAME with a range that fits
8925 into the range of OP0's type.
8927 If so, the conversion is redundant as the earlier SSA_NAME can be
8928 used for the comparison directly if we just massage the constant in the
8930 if (TREE_CODE (op0
) == SSA_NAME
8931 && TREE_CODE (op1
) == INTEGER_CST
)
8933 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
8936 if (!is_gimple_assign (def_stmt
)
8937 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
8940 innerop
= gimple_assign_rhs1 (def_stmt
);
8942 if (TREE_CODE (innerop
) == SSA_NAME
8943 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
8945 value_range_t
*vr
= get_value_range (innerop
);
8947 if (range_int_cst_p (vr
)
8948 && range_fits_type_p (vr
,
8949 TYPE_PRECISION (TREE_TYPE (op0
)),
8950 TYPE_UNSIGNED (TREE_TYPE (op0
)))
8951 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
8952 /* The range must not have overflowed, or if it did overflow
8953 we must not be wrapping/trapping overflow and optimizing
8954 with strict overflow semantics. */
8955 && ((!is_negative_overflow_infinity (vr
->min
)
8956 && !is_positive_overflow_infinity (vr
->max
))
8957 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
8959 /* If the range overflowed and the user has asked for warnings
8960 when strict overflow semantics were used to optimize code,
8961 issue an appropriate warning. */
8962 if ((is_negative_overflow_infinity (vr
->min
)
8963 || is_positive_overflow_infinity (vr
->max
))
8964 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
8966 location_t location
;
8968 if (!gimple_has_location (stmt
))
8969 location
= input_location
;
8971 location
= gimple_location (stmt
);
8972 warning_at (location
, OPT_Wstrict_overflow
,
8973 "assuming signed overflow does not occur when "
8974 "simplifying conditional");
8977 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
8978 gimple_cond_set_lhs (stmt
, innerop
);
8979 gimple_cond_set_rhs (stmt
, newconst
);
8988 /* Simplify a switch statement using the value range of the switch
8992 simplify_switch_using_ranges (gimple stmt
)
8994 tree op
= gimple_switch_index (stmt
);
8999 size_t i
= 0, j
= 0, n
, n2
;
9002 size_t k
= 1, l
= 0;
9004 if (TREE_CODE (op
) == SSA_NAME
)
9006 vr
= get_value_range (op
);
9008 /* We can only handle integer ranges. */
9009 if ((vr
->type
!= VR_RANGE
9010 && vr
->type
!= VR_ANTI_RANGE
)
9011 || symbolic_range_p (vr
))
9014 /* Find case label for min/max of the value range. */
9015 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9017 else if (TREE_CODE (op
) == INTEGER_CST
)
9019 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9033 n
= gimple_switch_num_labels (stmt
);
9035 /* Bail out if this is just all edges taken. */
9041 /* Build a new vector of taken case labels. */
9042 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9045 /* Add the default edge, if necessary. */
9047 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9049 for (; i
<= j
; ++i
, ++n2
)
9050 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9052 for (; k
<= l
; ++k
, ++n2
)
9053 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9055 /* Mark needed edges. */
9056 for (i
= 0; i
< n2
; ++i
)
9058 e
= find_edge (gimple_bb (stmt
),
9059 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9060 e
->aux
= (void *)-1;
9063 /* Queue not needed edges for later removal. */
9064 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9066 if (e
->aux
== (void *)-1)
9072 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9074 fprintf (dump_file
, "removing unreachable case label\n");
9076 to_remove_edges
.safe_push (e
);
9077 e
->flags
&= ~EDGE_EXECUTABLE
;
9080 /* And queue an update for the stmt. */
9083 to_update_switch_stmts
.safe_push (su
);
9087 /* Simplify an integral conversion from an SSA name in STMT. */
9090 simplify_conversion_using_ranges (gimple stmt
)
9092 tree innerop
, middleop
, finaltype
;
9094 value_range_t
*innervr
;
9095 bool inner_unsigned_p
, middle_unsigned_p
, final_unsigned_p
;
9096 unsigned inner_prec
, middle_prec
, final_prec
;
9097 double_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9099 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9100 if (!INTEGRAL_TYPE_P (finaltype
))
9102 middleop
= gimple_assign_rhs1 (stmt
);
9103 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9104 if (!is_gimple_assign (def_stmt
)
9105 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9107 innerop
= gimple_assign_rhs1 (def_stmt
);
9108 if (TREE_CODE (innerop
) != SSA_NAME
9109 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9112 /* Get the value-range of the inner operand. */
9113 innervr
= get_value_range (innerop
);
9114 if (innervr
->type
!= VR_RANGE
9115 || TREE_CODE (innervr
->min
) != INTEGER_CST
9116 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9119 /* Simulate the conversion chain to check if the result is equal if
9120 the middle conversion is removed. */
9121 innermin
= tree_to_double_int (innervr
->min
);
9122 innermax
= tree_to_double_int (innervr
->max
);
9124 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9125 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9126 final_prec
= TYPE_PRECISION (finaltype
);
9128 /* If the first conversion is not injective, the second must not
9130 if ((innermax
- innermin
).ugt (double_int::mask (middle_prec
))
9131 && middle_prec
< final_prec
)
9133 /* We also want a medium value so that we can track the effect that
9134 narrowing conversions with sign change have. */
9135 inner_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (innerop
));
9136 if (inner_unsigned_p
)
9137 innermed
= double_int::mask (inner_prec
).lrshift (1, inner_prec
);
9139 innermed
= double_int_zero
;
9140 if (innermin
.cmp (innermed
, inner_unsigned_p
) >= 0
9141 || innermed
.cmp (innermax
, inner_unsigned_p
) >= 0)
9142 innermed
= innermin
;
9144 middle_unsigned_p
= TYPE_UNSIGNED (TREE_TYPE (middleop
));
9145 middlemin
= innermin
.ext (middle_prec
, middle_unsigned_p
);
9146 middlemed
= innermed
.ext (middle_prec
, middle_unsigned_p
);
9147 middlemax
= innermax
.ext (middle_prec
, middle_unsigned_p
);
9149 /* Require that the final conversion applied to both the original
9150 and the intermediate range produces the same result. */
9151 final_unsigned_p
= TYPE_UNSIGNED (finaltype
);
9152 if (middlemin
.ext (final_prec
, final_unsigned_p
)
9153 != innermin
.ext (final_prec
, final_unsigned_p
)
9154 || middlemed
.ext (final_prec
, final_unsigned_p
)
9155 != innermed
.ext (final_prec
, final_unsigned_p
)
9156 || middlemax
.ext (final_prec
, final_unsigned_p
)
9157 != innermax
.ext (final_prec
, final_unsigned_p
))
9160 gimple_assign_set_rhs1 (stmt
, innerop
);
9165 /* Simplify a conversion from integral SSA name to float in STMT. */
9168 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9170 tree rhs1
= gimple_assign_rhs1 (stmt
);
9171 value_range_t
*vr
= get_value_range (rhs1
);
9172 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9173 enum machine_mode mode
;
9177 /* We can only handle constant ranges. */
9178 if (vr
->type
!= VR_RANGE
9179 || TREE_CODE (vr
->min
) != INTEGER_CST
9180 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9183 /* First check if we can use a signed type in place of an unsigned. */
9184 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9185 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9186 != CODE_FOR_nothing
)
9187 && range_fits_type_p (vr
, GET_MODE_PRECISION
9188 (TYPE_MODE (TREE_TYPE (rhs1
))), 0))
9189 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9190 /* If we can do the conversion in the current input mode do nothing. */
9191 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9192 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9194 /* Otherwise search for a mode we can use, starting from the narrowest
9195 integer mode available. */
9198 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9201 /* If we cannot do a signed conversion to float from mode
9202 or if the value-range does not fit in the signed type
9203 try with a wider mode. */
9204 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9205 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), 0))
9208 mode
= GET_MODE_WIDER_MODE (mode
);
9209 /* But do not widen the input. Instead leave that to the
9210 optabs expansion code. */
9211 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9214 while (mode
!= VOIDmode
);
9215 if (mode
== VOIDmode
)
9219 /* It works, insert a truncation or sign-change before the
9220 float conversion. */
9221 tem
= make_ssa_name (build_nonstandard_integer_type
9222 (GET_MODE_PRECISION (mode
), 0), NULL
);
9223 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9224 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9225 gimple_assign_set_rhs1 (stmt
, tem
);
9231 /* Simplify STMT using ranges if possible. */
9234 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9236 gimple stmt
= gsi_stmt (*gsi
);
9237 if (is_gimple_assign (stmt
))
9239 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9240 tree rhs1
= gimple_assign_rhs1 (stmt
);
9246 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9247 if the RHS is zero or one, and the LHS are known to be boolean
9249 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9250 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9253 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9254 and BIT_AND_EXPR respectively if the first operand is greater
9255 than zero and the second operand is an exact power of two. */
9256 case TRUNC_DIV_EXPR
:
9257 case TRUNC_MOD_EXPR
:
9258 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9259 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9260 return simplify_div_or_mod_using_ranges (stmt
);
9263 /* Transform ABS (X) into X or -X as appropriate. */
9265 if (TREE_CODE (rhs1
) == SSA_NAME
9266 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9267 return simplify_abs_using_ranges (stmt
);
9272 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9273 if all the bits being cleared are already cleared or
9274 all the bits being set are already set. */
9275 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9276 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9280 if (TREE_CODE (rhs1
) == SSA_NAME
9281 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9282 return simplify_conversion_using_ranges (stmt
);
9286 if (TREE_CODE (rhs1
) == SSA_NAME
9287 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9288 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9295 else if (gimple_code (stmt
) == GIMPLE_COND
)
9296 return simplify_cond_using_ranges (stmt
);
9297 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9298 return simplify_switch_using_ranges (stmt
);
9303 /* If the statement pointed by SI has a predicate whose value can be
9304 computed using the value range information computed by VRP, compute
9305 its value and return true. Otherwise, return false. */
9308 fold_predicate_in (gimple_stmt_iterator
*si
)
9310 bool assignment_p
= false;
9312 gimple stmt
= gsi_stmt (*si
);
9314 if (is_gimple_assign (stmt
)
9315 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9317 assignment_p
= true;
9318 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9319 gimple_assign_rhs1 (stmt
),
9320 gimple_assign_rhs2 (stmt
),
9323 else if (gimple_code (stmt
) == GIMPLE_COND
)
9324 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
9325 gimple_cond_lhs (stmt
),
9326 gimple_cond_rhs (stmt
),
9334 val
= fold_convert (gimple_expr_type (stmt
), val
);
9338 fprintf (dump_file
, "Folding predicate ");
9339 print_gimple_expr (dump_file
, stmt
, 0, 0);
9340 fprintf (dump_file
, " to ");
9341 print_generic_expr (dump_file
, val
, 0);
9342 fprintf (dump_file
, "\n");
9345 if (is_gimple_assign (stmt
))
9346 gimple_assign_set_rhs_from_tree (si
, val
);
9349 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9350 if (integer_zerop (val
))
9351 gimple_cond_make_false (stmt
);
9352 else if (integer_onep (val
))
9353 gimple_cond_make_true (stmt
);
9364 /* Callback for substitute_and_fold folding the stmt at *SI. */
9367 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9369 if (fold_predicate_in (si
))
9372 return simplify_stmt_using_ranges (si
);
9375 /* Stack of dest,src equivalency pairs that need to be restored after
9376 each attempt to thread a block's incoming edge to an outgoing edge.
9378 A NULL entry is used to mark the end of pairs which need to be
9380 static vec
<tree
> equiv_stack
;
9382 /* A trivial wrapper so that we can present the generic jump threading
9383 code with a simple API for simplifying statements. STMT is the
9384 statement we want to simplify, WITHIN_STMT provides the location
9385 for any overflow warnings. */
9388 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9390 if (gimple_code (stmt
) == GIMPLE_COND
)
9391 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9392 gimple_cond_lhs (stmt
),
9393 gimple_cond_rhs (stmt
), within_stmt
);
9395 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
9397 value_range_t new_vr
= VR_INITIALIZER
;
9398 tree lhs
= gimple_assign_lhs (stmt
);
9400 if (TREE_CODE (lhs
) == SSA_NAME
9401 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9402 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9404 extract_range_from_assignment (&new_vr
, stmt
);
9405 if (range_int_cst_singleton_p (&new_vr
))
9413 /* Blocks which have more than one predecessor and more than
9414 one successor present jump threading opportunities, i.e.,
9415 when the block is reached from a specific predecessor, we
9416 may be able to determine which of the outgoing edges will
9417 be traversed. When this optimization applies, we are able
9418 to avoid conditionals at runtime and we may expose secondary
9419 optimization opportunities.
9421 This routine is effectively a driver for the generic jump
9422 threading code. It basically just presents the generic code
9423 with edges that may be suitable for jump threading.
9425 Unlike DOM, we do not iterate VRP if jump threading was successful.
9426 While iterating may expose new opportunities for VRP, it is expected
9427 those opportunities would be very limited and the compile time cost
9428 to expose those opportunities would be significant.
9430 As jump threading opportunities are discovered, they are registered
9431 for later realization. */
9434 identify_jump_threads (void)
9441 /* Ugh. When substituting values earlier in this pass we can
9442 wipe the dominance information. So rebuild the dominator
9443 information as we need it within the jump threading code. */
9444 calculate_dominance_info (CDI_DOMINATORS
);
9446 /* We do not allow VRP information to be used for jump threading
9447 across a back edge in the CFG. Otherwise it becomes too
9448 difficult to avoid eliminating loop exit tests. Of course
9449 EDGE_DFS_BACK is not accurate at this time so we have to
9451 mark_dfs_back_edges ();
9453 /* Do not thread across edges we are about to remove. Just marking
9454 them as EDGE_DFS_BACK will do. */
9455 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9456 e
->flags
|= EDGE_DFS_BACK
;
9458 /* Allocate our unwinder stack to unwind any temporary equivalences
9459 that might be recorded. */
9460 equiv_stack
.create (20);
9462 /* To avoid lots of silly node creation, we create a single
9463 conditional and just modify it in-place when attempting to
9465 dummy
= gimple_build_cond (EQ_EXPR
,
9466 integer_zero_node
, integer_zero_node
,
9469 /* Walk through all the blocks finding those which present a
9470 potential jump threading opportunity. We could set this up
9471 as a dominator walker and record data during the walk, but
9472 I doubt it's worth the effort for the classes of jump
9473 threading opportunities we are trying to identify at this
9474 point in compilation. */
9479 /* If the generic jump threading code does not find this block
9480 interesting, then there is nothing to do. */
9481 if (! potentially_threadable_block (bb
))
9484 /* We only care about blocks ending in a COND_EXPR. While there
9485 may be some value in handling SWITCH_EXPR here, I doubt it's
9486 terribly important. */
9487 last
= gsi_stmt (gsi_last_bb (bb
));
9489 /* We're basically looking for a switch or any kind of conditional with
9490 integral or pointer type arguments. Note the type of the second
9491 argument will be the same as the first argument, so no need to
9492 check it explicitly. */
9493 if (gimple_code (last
) == GIMPLE_SWITCH
9494 || (gimple_code (last
) == GIMPLE_COND
9495 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9496 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9497 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9498 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9499 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9503 /* We've got a block with multiple predecessors and multiple
9504 successors which also ends in a suitable conditional or
9505 switch statement. For each predecessor, see if we can thread
9506 it to a specific successor. */
9507 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9509 /* Do not thread across back edges or abnormal edges
9511 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9514 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9515 simplify_stmt_for_jump_threading
);
9520 /* We do not actually update the CFG or SSA graphs at this point as
9521 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9522 handle ASSERT_EXPRs gracefully. */
9525 /* We identified all the jump threading opportunities earlier, but could
9526 not transform the CFG at that time. This routine transforms the
9527 CFG and arranges for the dominator tree to be rebuilt if necessary.
9529 Note the SSA graph update will occur during the normal TODO
9530 processing by the pass manager. */
9532 finalize_jump_threads (void)
9534 thread_through_all_blocks (false);
9535 equiv_stack
.release ();
9539 /* Traverse all the blocks folding conditionals with known ranges. */
9546 values_propagated
= true;
9550 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9551 dump_all_value_ranges (dump_file
);
9552 fprintf (dump_file
, "\n");
9555 substitute_and_fold (op_with_constant_singleton_value_range
,
9556 vrp_fold_stmt
, false);
9558 if (warn_array_bounds
)
9559 check_all_array_refs ();
9561 /* We must identify jump threading opportunities before we release
9562 the datastructures built by VRP. */
9563 identify_jump_threads ();
9565 /* Set value range to non pointer SSA_NAMEs. */
9566 for (i
= 0; i
< num_vr_values
; i
++)
9569 tree name
= ssa_name (i
);
9572 || POINTER_TYPE_P (TREE_TYPE (name
))
9573 || (vr_value
[i
]->type
== VR_VARYING
)
9574 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9577 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9578 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
))
9580 if (vr_value
[i
]->type
== VR_RANGE
)
9581 set_range_info (name
,
9582 tree_to_double_int (vr_value
[i
]->min
),
9583 tree_to_double_int (vr_value
[i
]->max
));
9584 else if (vr_value
[i
]->type
== VR_ANTI_RANGE
)
9586 /* VR_ANTI_RANGE ~[min, max] is encoded compactly as
9587 [max + 1, min - 1] without additional attributes.
9588 When min value > max value, we know that it is
9589 VR_ANTI_RANGE; it is VR_RANGE otherwise. */
9591 /* ~[0,0] anti-range is represented as
9593 if (TYPE_UNSIGNED (TREE_TYPE (name
))
9594 && integer_zerop (vr_value
[i
]->min
)
9595 && integer_zerop (vr_value
[i
]->max
))
9596 set_range_info (name
,
9598 double_int::max_value
9599 (TYPE_PRECISION (TREE_TYPE (name
)), true));
9601 set_range_info (name
,
9602 tree_to_double_int (vr_value
[i
]->max
)
9604 tree_to_double_int (vr_value
[i
]->min
)
9610 /* Free allocated memory. */
9611 for (i
= 0; i
< num_vr_values
; i
++)
9614 BITMAP_FREE (vr_value
[i
]->equiv
);
9619 free (vr_phi_edge_counts
);
9621 /* So that we can distinguish between VRP data being available
9622 and not available. */
9624 vr_phi_edge_counts
= NULL
;
9628 /* Main entry point to VRP (Value Range Propagation). This pass is
9629 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9630 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9631 Programming Language Design and Implementation, pp. 67-78, 1995.
9632 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9634 This is essentially an SSA-CCP pass modified to deal with ranges
9635 instead of constants.
9637 While propagating ranges, we may find that two or more SSA name
9638 have equivalent, though distinct ranges. For instance,
9641 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9643 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9647 In the code above, pointer p_5 has range [q_2, q_2], but from the
9648 code we can also determine that p_5 cannot be NULL and, if q_2 had
9649 a non-varying range, p_5's range should also be compatible with it.
9651 These equivalences are created by two expressions: ASSERT_EXPR and
9652 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9653 result of another assertion, then we can use the fact that p_5 and
9654 p_4 are equivalent when evaluating p_5's range.
9656 Together with value ranges, we also propagate these equivalences
9657 between names so that we can take advantage of information from
9658 multiple ranges when doing final replacement. Note that this
9659 equivalency relation is transitive but not symmetric.
9661 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9662 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9663 in contexts where that assertion does not hold (e.g., in line 6).
9665 TODO, the main difference between this pass and Patterson's is that
9666 we do not propagate edge probabilities. We only compute whether
9667 edges can be taken or not. That is, instead of having a spectrum
9668 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9669 DON'T KNOW. In the future, it may be worthwhile to propagate
9670 probabilities to aid branch prediction. */
9679 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9680 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9683 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9684 Inserting assertions may split edges which will invalidate
9686 insert_range_assertions ();
9688 to_remove_edges
.create (10);
9689 to_update_switch_stmts
.create (5);
9690 threadedge_initialize_values ();
9692 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9693 mark_dfs_back_edges ();
9696 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9699 free_numbers_of_iterations_estimates ();
9701 /* ASSERT_EXPRs must be removed before finalizing jump threads
9702 as finalizing jump threads calls the CFG cleanup code which
9703 does not properly handle ASSERT_EXPRs. */
9704 remove_range_assertions ();
9706 /* If we exposed any new variables, go ahead and put them into
9707 SSA form now, before we handle jump threading. This simplifies
9708 interactions between rewriting of _DECL nodes into SSA form
9709 and rewriting SSA_NAME nodes into SSA form after block
9710 duplication and CFG manipulation. */
9711 update_ssa (TODO_update_ssa
);
9713 finalize_jump_threads ();
9715 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9716 CFG in a broken state and requires a cfg_cleanup run. */
9717 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9719 /* Update SWITCH_EXPR case label vector. */
9720 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
9723 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9725 gimple_switch_set_num_labels (su
->stmt
, n
);
9726 for (j
= 0; j
< n
; j
++)
9727 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9728 /* As we may have replaced the default label with a regular one
9729 make sure to make it a real default label again. This ensures
9730 optimal expansion. */
9731 label
= gimple_switch_label (su
->stmt
, 0);
9732 CASE_LOW (label
) = NULL_TREE
;
9733 CASE_HIGH (label
) = NULL_TREE
;
9736 if (to_remove_edges
.length () > 0)
9738 free_dominance_info (CDI_DOMINATORS
);
9740 loops_state_set (LOOPS_NEED_FIXUP
);
9743 to_remove_edges
.release ();
9744 to_update_switch_stmts
.release ();
9745 threadedge_finalize_values ();
9748 loop_optimizer_finalize ();
9755 return flag_tree_vrp
!= 0;
9760 const pass_data pass_data_vrp
=
9762 GIMPLE_PASS
, /* type */
9764 OPTGROUP_NONE
, /* optinfo_flags */
9765 true, /* has_gate */
9766 true, /* has_execute */
9767 TV_TREE_VRP
, /* tv_id */
9768 PROP_ssa
, /* properties_required */
9769 0, /* properties_provided */
9770 0, /* properties_destroyed */
9771 0, /* todo_flags_start */
9772 ( TODO_cleanup_cfg
| TODO_update_ssa
9774 | TODO_verify_flow
), /* todo_flags_finish */
9777 class pass_vrp
: public gimple_opt_pass
9780 pass_vrp (gcc::context
*ctxt
)
9781 : gimple_opt_pass (pass_data_vrp
, ctxt
)
9784 /* opt_pass methods: */
9785 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
9786 bool gate () { return gate_vrp (); }
9787 unsigned int execute () { return execute_vrp (); }
9789 }; // class pass_vrp
9794 make_pass_vrp (gcc::context
*ctxt
)
9796 return new pass_vrp (ctxt
);