1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 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"
27 #include "stor-layout.h"
29 #include "basic-block.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-fold.h"
34 #include "gimple-expr.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "gimple-ssa.h"
41 #include "tree-phinodes.h"
42 #include "ssa-iterators.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
50 #include "tree-pass.h"
51 #include "tree-dump.h"
52 #include "gimple-pretty-print.h"
53 #include "diagnostic-core.h"
56 #include "tree-scalar-evolution.h"
57 #include "tree-ssa-propagate.h"
58 #include "tree-chrec.h"
59 #include "tree-ssa-threadupdate.h"
62 #include "tree-ssa-threadedge.h"
67 /* Range of values that can be associated with an SSA_NAME after VRP
71 /* Lattice value represented by this range. */
72 enum value_range_type type
;
74 /* Minimum and maximum values represented by this range. These
75 values should be interpreted as follows:
77 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
80 - If TYPE == VR_RANGE then MIN holds the minimum value and
81 MAX holds the maximum value of the range [MIN, MAX].
83 - If TYPE == ANTI_RANGE the variable is known to NOT
84 take any values in the range [MIN, MAX]. */
88 /* Set of SSA names whose value ranges are equivalent to this one.
89 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
93 typedef struct value_range_d value_range_t
;
95 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
97 /* Set of SSA names found live during the RPO traversal of the function
98 for still active basic-blocks. */
101 /* Return true if the SSA name NAME is live on the edge E. */
104 live_on_edge (edge e
, tree name
)
106 return (live
[e
->dest
->index
]
107 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
110 /* Local functions. */
111 static int compare_values (tree val1
, tree val2
);
112 static int compare_values_warnv (tree val1
, tree val2
, bool *);
113 static void vrp_meet (value_range_t
*, value_range_t
*);
114 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
115 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
116 tree
, tree
, bool, bool *,
119 /* Location information for ASSERT_EXPRs. Each instance of this
120 structure describes an ASSERT_EXPR for an SSA name. Since a single
121 SSA name may have more than one assertion associated with it, these
122 locations are kept in a linked list attached to the corresponding
124 struct assert_locus_d
126 /* Basic block where the assertion would be inserted. */
129 /* Some assertions need to be inserted on an edge (e.g., assertions
130 generated by COND_EXPRs). In those cases, BB will be NULL. */
133 /* Pointer to the statement that generated this assertion. */
134 gimple_stmt_iterator si
;
136 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
137 enum tree_code comp_code
;
139 /* Value being compared against. */
142 /* Expression to compare. */
145 /* Next node in the linked list. */
146 struct assert_locus_d
*next
;
149 typedef struct assert_locus_d
*assert_locus_t
;
151 /* If bit I is present, it means that SSA name N_i has a list of
152 assertions that should be inserted in the IL. */
153 static bitmap need_assert_for
;
155 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
156 holds a list of ASSERT_LOCUS_T nodes that describe where
157 ASSERT_EXPRs for SSA name N_I should be inserted. */
158 static assert_locus_t
*asserts_for
;
160 /* Value range array. After propagation, VR_VALUE[I] holds the range
161 of values that SSA name N_I may take. */
162 static unsigned num_vr_values
;
163 static value_range_t
**vr_value
;
164 static bool values_propagated
;
166 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
167 number of executable edges we saw the last time we visited the
169 static int *vr_phi_edge_counts
;
176 static vec
<edge
> to_remove_edges
;
177 static vec
<switch_update
> to_update_switch_stmts
;
180 /* Return the maximum value for TYPE. */
183 vrp_val_max (const_tree type
)
185 if (!INTEGRAL_TYPE_P (type
))
188 return TYPE_MAX_VALUE (type
);
191 /* Return the minimum value for TYPE. */
194 vrp_val_min (const_tree type
)
196 if (!INTEGRAL_TYPE_P (type
))
199 return TYPE_MIN_VALUE (type
);
202 /* Return whether VAL is equal to the maximum value of its type. This
203 will be true for a positive overflow infinity. We can't do a
204 simple equality comparison with TYPE_MAX_VALUE because C typedefs
205 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
206 to the integer constant with the same value in the type. */
209 vrp_val_is_max (const_tree val
)
211 tree type_max
= vrp_val_max (TREE_TYPE (val
));
212 return (val
== type_max
213 || (type_max
!= NULL_TREE
214 && operand_equal_p (val
, type_max
, 0)));
217 /* Return whether VAL is equal to the minimum value of its type. This
218 will be true for a negative overflow infinity. */
221 vrp_val_is_min (const_tree val
)
223 tree type_min
= vrp_val_min (TREE_TYPE (val
));
224 return (val
== type_min
225 || (type_min
!= NULL_TREE
226 && operand_equal_p (val
, type_min
, 0)));
230 /* Return whether TYPE should use an overflow infinity distinct from
231 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
232 represent a signed overflow during VRP computations. An infinity
233 is distinct from a half-range, which will go from some number to
234 TYPE_{MIN,MAX}_VALUE. */
237 needs_overflow_infinity (const_tree type
)
239 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
242 /* Return whether TYPE can support our overflow infinity
243 representation: we use the TREE_OVERFLOW flag, which only exists
244 for constants. If TYPE doesn't support this, we don't optimize
245 cases which would require signed overflow--we drop them to
249 supports_overflow_infinity (const_tree type
)
251 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
252 #ifdef ENABLE_CHECKING
253 gcc_assert (needs_overflow_infinity (type
));
255 return (min
!= NULL_TREE
256 && CONSTANT_CLASS_P (min
)
258 && CONSTANT_CLASS_P (max
));
261 /* VAL is the maximum or minimum value of a type. Return a
262 corresponding overflow infinity. */
265 make_overflow_infinity (tree val
)
267 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
268 val
= copy_node (val
);
269 TREE_OVERFLOW (val
) = 1;
273 /* Return a negative overflow infinity for TYPE. */
276 negative_overflow_infinity (tree type
)
278 gcc_checking_assert (supports_overflow_infinity (type
));
279 return make_overflow_infinity (vrp_val_min (type
));
282 /* Return a positive overflow infinity for TYPE. */
285 positive_overflow_infinity (tree type
)
287 gcc_checking_assert (supports_overflow_infinity (type
));
288 return make_overflow_infinity (vrp_val_max (type
));
291 /* Return whether VAL is a negative overflow infinity. */
294 is_negative_overflow_infinity (const_tree val
)
296 return (TREE_OVERFLOW_P (val
)
297 && needs_overflow_infinity (TREE_TYPE (val
))
298 && vrp_val_is_min (val
));
301 /* Return whether VAL is a positive overflow infinity. */
304 is_positive_overflow_infinity (const_tree val
)
306 return (TREE_OVERFLOW_P (val
)
307 && needs_overflow_infinity (TREE_TYPE (val
))
308 && vrp_val_is_max (val
));
311 /* Return whether VAL is a positive or negative overflow infinity. */
314 is_overflow_infinity (const_tree val
)
316 return (TREE_OVERFLOW_P (val
)
317 && needs_overflow_infinity (TREE_TYPE (val
))
318 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
321 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
324 stmt_overflow_infinity (gimple stmt
)
326 if (is_gimple_assign (stmt
)
327 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
329 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
333 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
334 the same value with TREE_OVERFLOW clear. This can be used to avoid
335 confusing a regular value with an overflow value. */
338 avoid_overflow_infinity (tree val
)
340 if (!is_overflow_infinity (val
))
343 if (vrp_val_is_max (val
))
344 return vrp_val_max (TREE_TYPE (val
));
347 gcc_checking_assert (vrp_val_is_min (val
));
348 return vrp_val_min (TREE_TYPE (val
));
353 /* Return true if ARG is marked with the nonnull attribute in the
354 current function signature. */
357 nonnull_arg_p (const_tree arg
)
359 tree t
, attrs
, fntype
;
360 unsigned HOST_WIDE_INT arg_num
;
362 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
364 /* The static chain decl is always non null. */
365 if (arg
== cfun
->static_chain_decl
)
368 fntype
= TREE_TYPE (current_function_decl
);
369 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
371 attrs
= lookup_attribute ("nonnull", attrs
);
373 /* If "nonnull" wasn't specified, we know nothing about the argument. */
374 if (attrs
== NULL_TREE
)
377 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
378 if (TREE_VALUE (attrs
) == NULL_TREE
)
381 /* Get the position number for ARG in the function signature. */
382 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
384 t
= DECL_CHAIN (t
), arg_num
++)
390 gcc_assert (t
== arg
);
392 /* Now see if ARG_NUM is mentioned in the nonnull list. */
393 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
395 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
404 /* Set value range VR to VR_UNDEFINED. */
407 set_value_range_to_undefined (value_range_t
*vr
)
409 vr
->type
= VR_UNDEFINED
;
410 vr
->min
= vr
->max
= NULL_TREE
;
412 bitmap_clear (vr
->equiv
);
416 /* Set value range VR to VR_VARYING. */
419 set_value_range_to_varying (value_range_t
*vr
)
421 vr
->type
= VR_VARYING
;
422 vr
->min
= vr
->max
= NULL_TREE
;
424 bitmap_clear (vr
->equiv
);
428 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
431 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
432 tree max
, bitmap equiv
)
434 #if defined ENABLE_CHECKING
435 /* Check the validity of the range. */
436 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
440 gcc_assert (min
&& max
);
442 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
443 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
445 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
446 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
448 cmp
= compare_values (min
, max
);
449 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
451 if (needs_overflow_infinity (TREE_TYPE (min
)))
452 gcc_assert (!is_overflow_infinity (min
)
453 || !is_overflow_infinity (max
));
456 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
457 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
459 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
460 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
467 /* Since updating the equivalence set involves deep copying the
468 bitmaps, only do it if absolutely necessary. */
469 if (vr
->equiv
== NULL
471 vr
->equiv
= BITMAP_ALLOC (NULL
);
473 if (equiv
!= vr
->equiv
)
475 if (equiv
&& !bitmap_empty_p (equiv
))
476 bitmap_copy (vr
->equiv
, equiv
);
478 bitmap_clear (vr
->equiv
);
483 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
484 This means adjusting T, MIN and MAX representing the case of a
485 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
486 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
487 In corner cases where MAX+1 or MIN-1 wraps this will fall back
489 This routine exists to ease canonicalization in the case where we
490 extract ranges from var + CST op limit. */
493 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
494 tree min
, tree max
, bitmap equiv
)
496 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
497 if (t
== VR_UNDEFINED
)
499 set_value_range_to_undefined (vr
);
502 else if (t
== VR_VARYING
)
504 set_value_range_to_varying (vr
);
508 /* Nothing to canonicalize for symbolic ranges. */
509 if (TREE_CODE (min
) != INTEGER_CST
510 || TREE_CODE (max
) != INTEGER_CST
)
512 set_value_range (vr
, t
, min
, max
, equiv
);
516 /* Wrong order for min and max, to swap them and the VR type we need
518 if (tree_int_cst_lt (max
, min
))
522 /* For one bit precision if max < min, then the swapped
523 range covers all values, so for VR_RANGE it is varying and
524 for VR_ANTI_RANGE empty range, so drop to varying as well. */
525 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
527 set_value_range_to_varying (vr
);
531 one
= build_int_cst (TREE_TYPE (min
), 1);
532 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
533 max
= int_const_binop (MINUS_EXPR
, min
, one
);
536 /* There's one corner case, if we had [C+1, C] before we now have
537 that again. But this represents an empty value range, so drop
538 to varying in this case. */
539 if (tree_int_cst_lt (max
, min
))
541 set_value_range_to_varying (vr
);
545 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
548 /* Anti-ranges that can be represented as ranges should be so. */
549 if (t
== VR_ANTI_RANGE
)
551 bool is_min
= vrp_val_is_min (min
);
552 bool is_max
= vrp_val_is_max (max
);
554 if (is_min
&& is_max
)
556 /* We cannot deal with empty ranges, drop to varying.
557 ??? This could be VR_UNDEFINED instead. */
558 set_value_range_to_varying (vr
);
561 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
562 && (is_min
|| is_max
))
564 /* Non-empty boolean ranges can always be represented
565 as a singleton range. */
567 min
= max
= vrp_val_max (TREE_TYPE (min
));
569 min
= max
= vrp_val_min (TREE_TYPE (min
));
573 /* As a special exception preserve non-null ranges. */
574 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
575 && integer_zerop (max
)))
577 tree one
= build_int_cst (TREE_TYPE (max
), 1);
578 min
= int_const_binop (PLUS_EXPR
, max
, one
);
579 max
= vrp_val_max (TREE_TYPE (max
));
584 tree one
= build_int_cst (TREE_TYPE (min
), 1);
585 max
= int_const_binop (MINUS_EXPR
, min
, one
);
586 min
= vrp_val_min (TREE_TYPE (min
));
591 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
592 if (needs_overflow_infinity (TREE_TYPE (min
))
593 && is_overflow_infinity (min
)
594 && is_overflow_infinity (max
))
596 set_value_range_to_varying (vr
);
600 set_value_range (vr
, t
, min
, max
, equiv
);
603 /* Copy value range FROM into value range TO. */
606 copy_value_range (value_range_t
*to
, value_range_t
*from
)
608 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
611 /* Set value range VR to a single value. This function is only called
612 with values we get from statements, and exists to clear the
613 TREE_OVERFLOW flag so that we don't think we have an overflow
614 infinity when we shouldn't. */
617 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
619 gcc_assert (is_gimple_min_invariant (val
));
620 if (TREE_OVERFLOW_P (val
))
621 val
= drop_tree_overflow (val
);
622 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
625 /* Set value range VR to a non-negative range of type TYPE.
626 OVERFLOW_INFINITY indicates whether to use an overflow infinity
627 rather than TYPE_MAX_VALUE; this should be true if we determine
628 that the range is nonnegative based on the assumption that signed
629 overflow does not occur. */
632 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
633 bool overflow_infinity
)
637 if (overflow_infinity
&& !supports_overflow_infinity (type
))
639 set_value_range_to_varying (vr
);
643 zero
= build_int_cst (type
, 0);
644 set_value_range (vr
, VR_RANGE
, zero
,
646 ? positive_overflow_infinity (type
)
647 : TYPE_MAX_VALUE (type
)),
651 /* Set value range VR to a non-NULL range of type TYPE. */
654 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
656 tree zero
= build_int_cst (type
, 0);
657 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
661 /* Set value range VR to a NULL range of type TYPE. */
664 set_value_range_to_null (value_range_t
*vr
, tree type
)
666 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
670 /* Set value range VR to a range of a truthvalue of type TYPE. */
673 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
675 if (TYPE_PRECISION (type
) == 1)
676 set_value_range_to_varying (vr
);
678 set_value_range (vr
, VR_RANGE
,
679 build_int_cst (type
, 0), build_int_cst (type
, 1),
684 /* If abs (min) < abs (max), set VR to [-max, max], if
685 abs (min) >= abs (max), set VR to [-min, min]. */
688 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
692 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
693 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
694 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
695 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
696 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
697 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
698 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
700 set_value_range_to_varying (vr
);
703 cmp
= compare_values (min
, max
);
705 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
706 else if (cmp
== 0 || cmp
== 1)
709 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
713 set_value_range_to_varying (vr
);
716 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
720 /* Return value range information for VAR.
722 If we have no values ranges recorded (ie, VRP is not running), then
723 return NULL. Otherwise create an empty range if none existed for VAR. */
725 static value_range_t
*
726 get_value_range (const_tree var
)
728 static const struct value_range_d vr_const_varying
729 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
732 unsigned ver
= SSA_NAME_VERSION (var
);
734 /* If we have no recorded ranges, then return NULL. */
738 /* If we query the range for a new SSA name return an unmodifiable VARYING.
739 We should get here at most from the substitute-and-fold stage which
740 will never try to change values. */
741 if (ver
>= num_vr_values
)
742 return CONST_CAST (value_range_t
*, &vr_const_varying
);
748 /* After propagation finished do not allocate new value-ranges. */
749 if (values_propagated
)
750 return CONST_CAST (value_range_t
*, &vr_const_varying
);
752 /* Create a default value range. */
753 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
755 /* Defer allocating the equivalence set. */
758 /* If VAR is a default definition of a parameter, the variable can
759 take any value in VAR's type. */
760 if (SSA_NAME_IS_DEFAULT_DEF (var
))
762 sym
= SSA_NAME_VAR (var
);
763 if (TREE_CODE (sym
) == PARM_DECL
)
765 /* Try to use the "nonnull" attribute to create ~[0, 0]
766 anti-ranges for pointers. Note that this is only valid with
767 default definitions of PARM_DECLs. */
768 if (POINTER_TYPE_P (TREE_TYPE (sym
))
769 && nonnull_arg_p (sym
))
770 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
772 set_value_range_to_varying (vr
);
774 else if (TREE_CODE (sym
) == RESULT_DECL
775 && DECL_BY_REFERENCE (sym
))
776 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
782 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
785 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
789 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
791 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
794 /* Return true, if the bitmaps B1 and B2 are equal. */
797 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
800 || ((!b1
|| bitmap_empty_p (b1
))
801 && (!b2
|| bitmap_empty_p (b2
)))
803 && bitmap_equal_p (b1
, b2
)));
806 /* Update the value range and equivalence set for variable VAR to
807 NEW_VR. Return true if NEW_VR is different from VAR's previous
810 NOTE: This function assumes that NEW_VR is a temporary value range
811 object created for the sole purpose of updating VAR's range. The
812 storage used by the equivalence set from NEW_VR will be freed by
813 this function. Do not call update_value_range when NEW_VR
814 is the range object associated with another SSA name. */
817 update_value_range (const_tree var
, value_range_t
*new_vr
)
819 value_range_t
*old_vr
;
822 /* Update the value range, if necessary. */
823 old_vr
= get_value_range (var
);
824 is_new
= old_vr
->type
!= new_vr
->type
825 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
826 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
827 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
831 /* Do not allow transitions up the lattice. The following
832 is slightly more awkward than just new_vr->type < old_vr->type
833 because VR_RANGE and VR_ANTI_RANGE need to be considered
834 the same. We may not have is_new when transitioning to
835 UNDEFINED or from VARYING. */
836 if (new_vr
->type
== VR_UNDEFINED
837 || old_vr
->type
== VR_VARYING
)
838 set_value_range_to_varying (old_vr
);
840 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
844 BITMAP_FREE (new_vr
->equiv
);
850 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
851 point where equivalence processing can be turned on/off. */
854 add_equivalence (bitmap
*equiv
, const_tree var
)
856 unsigned ver
= SSA_NAME_VERSION (var
);
857 value_range_t
*vr
= vr_value
[ver
];
860 *equiv
= BITMAP_ALLOC (NULL
);
861 bitmap_set_bit (*equiv
, ver
);
863 bitmap_ior_into (*equiv
, vr
->equiv
);
867 /* Return true if VR is ~[0, 0]. */
870 range_is_nonnull (value_range_t
*vr
)
872 return vr
->type
== VR_ANTI_RANGE
873 && integer_zerop (vr
->min
)
874 && integer_zerop (vr
->max
);
878 /* Return true if VR is [0, 0]. */
881 range_is_null (value_range_t
*vr
)
883 return vr
->type
== VR_RANGE
884 && integer_zerop (vr
->min
)
885 && integer_zerop (vr
->max
);
888 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
892 range_int_cst_p (value_range_t
*vr
)
894 return (vr
->type
== VR_RANGE
895 && TREE_CODE (vr
->max
) == INTEGER_CST
896 && TREE_CODE (vr
->min
) == INTEGER_CST
);
899 /* Return true if VR is a INTEGER_CST singleton. */
902 range_int_cst_singleton_p (value_range_t
*vr
)
904 return (range_int_cst_p (vr
)
905 && !is_overflow_infinity (vr
->min
)
906 && !is_overflow_infinity (vr
->max
)
907 && tree_int_cst_equal (vr
->min
, vr
->max
));
910 /* Return true if value range VR involves at least one symbol. */
913 symbolic_range_p (value_range_t
*vr
)
915 return (!is_gimple_min_invariant (vr
->min
)
916 || !is_gimple_min_invariant (vr
->max
));
919 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
920 otherwise. We only handle additive operations and set NEG to true if the
921 symbol is negated and INV to the invariant part, if any. */
924 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
929 if (TREE_CODE (t
) == PLUS_EXPR
930 || TREE_CODE (t
) == POINTER_PLUS_EXPR
931 || TREE_CODE (t
) == MINUS_EXPR
)
933 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
935 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
936 inv_
= TREE_OPERAND (t
, 0);
937 t
= TREE_OPERAND (t
, 1);
939 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
942 inv_
= TREE_OPERAND (t
, 1);
943 t
= TREE_OPERAND (t
, 0);
954 if (TREE_CODE (t
) == NEGATE_EXPR
)
956 t
= TREE_OPERAND (t
, 0);
960 if (TREE_CODE (t
) != SSA_NAME
)
968 /* The reverse operation: build a symbolic expression with TYPE
969 from symbol SYM, negated according to NEG, and invariant INV. */
972 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
974 const bool pointer_p
= POINTER_TYPE_P (type
);
978 t
= build1 (NEGATE_EXPR
, type
, t
);
980 if (integer_zerop (inv
))
983 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
986 /* Return true if value range VR involves exactly one symbol SYM. */
989 symbolic_range_based_on_p (value_range_t
*vr
, const_tree sym
)
991 bool neg
, min_has_symbol
, max_has_symbol
;
994 if (is_gimple_min_invariant (vr
->min
))
995 min_has_symbol
= false;
996 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
997 min_has_symbol
= true;
1001 if (is_gimple_min_invariant (vr
->max
))
1002 max_has_symbol
= false;
1003 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
1004 max_has_symbol
= true;
1008 return (min_has_symbol
|| max_has_symbol
);
1011 /* Return true if value range VR uses an overflow infinity. */
1014 overflow_infinity_range_p (value_range_t
*vr
)
1016 return (vr
->type
== VR_RANGE
1017 && (is_overflow_infinity (vr
->min
)
1018 || is_overflow_infinity (vr
->max
)));
1021 /* Return false if we can not make a valid comparison based on VR;
1022 this will be the case if it uses an overflow infinity and overflow
1023 is not undefined (i.e., -fno-strict-overflow is in effect).
1024 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1025 uses an overflow infinity. */
1028 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
1030 gcc_assert (vr
->type
== VR_RANGE
);
1031 if (is_overflow_infinity (vr
->min
))
1033 *strict_overflow_p
= true;
1034 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1037 if (is_overflow_infinity (vr
->max
))
1039 *strict_overflow_p
= true;
1040 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1047 /* Return true if the result of assignment STMT is know to be non-negative.
1048 If the return value is based on the assumption that signed overflow is
1049 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1050 *STRICT_OVERFLOW_P.*/
1053 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1055 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1056 switch (get_gimple_rhs_class (code
))
1058 case GIMPLE_UNARY_RHS
:
1059 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1060 gimple_expr_type (stmt
),
1061 gimple_assign_rhs1 (stmt
),
1063 case GIMPLE_BINARY_RHS
:
1064 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1065 gimple_expr_type (stmt
),
1066 gimple_assign_rhs1 (stmt
),
1067 gimple_assign_rhs2 (stmt
),
1069 case GIMPLE_TERNARY_RHS
:
1071 case GIMPLE_SINGLE_RHS
:
1072 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
1074 case GIMPLE_INVALID_RHS
:
1081 /* Return true if return value of call STMT is know to be non-negative.
1082 If the return value is based on the assumption that signed overflow is
1083 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1084 *STRICT_OVERFLOW_P.*/
1087 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1089 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
1090 gimple_call_arg (stmt
, 0) : NULL_TREE
;
1091 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1092 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1094 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1095 gimple_call_fndecl (stmt
),
1101 /* Return true if STMT is know to to compute a non-negative value.
1102 If the return value is based on the assumption that signed overflow is
1103 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1104 *STRICT_OVERFLOW_P.*/
1107 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1109 switch (gimple_code (stmt
))
1112 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1114 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1120 /* Return true if the result of assignment STMT is know to be non-zero.
1121 If the return value is based on the assumption that signed overflow is
1122 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1123 *STRICT_OVERFLOW_P.*/
1126 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1128 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1129 switch (get_gimple_rhs_class (code
))
1131 case GIMPLE_UNARY_RHS
:
1132 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1133 gimple_expr_type (stmt
),
1134 gimple_assign_rhs1 (stmt
),
1136 case GIMPLE_BINARY_RHS
:
1137 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1138 gimple_expr_type (stmt
),
1139 gimple_assign_rhs1 (stmt
),
1140 gimple_assign_rhs2 (stmt
),
1142 case GIMPLE_TERNARY_RHS
:
1144 case GIMPLE_SINGLE_RHS
:
1145 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1147 case GIMPLE_INVALID_RHS
:
1154 /* Return true if STMT is known to compute a non-zero value.
1155 If the return value is based on the assumption that signed overflow is
1156 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1157 *STRICT_OVERFLOW_P.*/
1160 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1162 switch (gimple_code (stmt
))
1165 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1168 tree fndecl
= gimple_call_fndecl (stmt
);
1169 if (!fndecl
) return false;
1170 if (flag_delete_null_pointer_checks
&& !flag_check_new
1171 && DECL_IS_OPERATOR_NEW (fndecl
)
1172 && !TREE_NOTHROW (fndecl
))
1174 if (flag_delete_null_pointer_checks
&&
1175 lookup_attribute ("returns_nonnull",
1176 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1178 return gimple_alloca_call_p (stmt
);
1185 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1189 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1191 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1194 /* If we have an expression of the form &X->a, then the expression
1195 is nonnull if X is nonnull. */
1196 if (is_gimple_assign (stmt
)
1197 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1199 tree expr
= gimple_assign_rhs1 (stmt
);
1200 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1202 if (base
!= NULL_TREE
1203 && TREE_CODE (base
) == MEM_REF
1204 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1206 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1207 if (range_is_nonnull (vr
))
1215 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1216 a gimple invariant, or SSA_NAME +- CST. */
1219 valid_value_p (tree expr
)
1221 if (TREE_CODE (expr
) == SSA_NAME
)
1224 if (TREE_CODE (expr
) == PLUS_EXPR
1225 || TREE_CODE (expr
) == MINUS_EXPR
)
1226 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1227 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1229 return is_gimple_min_invariant (expr
);
1235 -2 if those are incomparable. */
1237 operand_less_p (tree val
, tree val2
)
1239 /* LT is folded faster than GE and others. Inline the common case. */
1240 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1241 return tree_int_cst_lt (val
, val2
);
1246 fold_defer_overflow_warnings ();
1248 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1250 fold_undefer_and_ignore_overflow_warnings ();
1253 || TREE_CODE (tcmp
) != INTEGER_CST
)
1256 if (!integer_zerop (tcmp
))
1260 /* val >= val2, not considering overflow infinity. */
1261 if (is_negative_overflow_infinity (val
))
1262 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1263 else if (is_positive_overflow_infinity (val2
))
1264 return is_positive_overflow_infinity (val
) ? 0 : 1;
1269 /* Compare two values VAL1 and VAL2. Return
1271 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1274 +1 if VAL1 > VAL2, and
1277 This is similar to tree_int_cst_compare but supports pointer values
1278 and values that cannot be compared at compile time.
1280 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1281 true if the return value is only valid if we assume that signed
1282 overflow is undefined. */
1285 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1290 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1292 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1293 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1295 /* Convert the two values into the same type. This is needed because
1296 sizetype causes sign extension even for unsigned types. */
1297 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1298 STRIP_USELESS_TYPE_CONVERSION (val2
);
1300 if ((TREE_CODE (val1
) == SSA_NAME
1301 || (TREE_CODE (val1
) == NEGATE_EXPR
1302 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1303 || TREE_CODE (val1
) == PLUS_EXPR
1304 || TREE_CODE (val1
) == MINUS_EXPR
)
1305 && (TREE_CODE (val2
) == SSA_NAME
1306 || (TREE_CODE (val2
) == NEGATE_EXPR
1307 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1308 || TREE_CODE (val2
) == PLUS_EXPR
1309 || TREE_CODE (val2
) == MINUS_EXPR
))
1311 tree n1
, c1
, n2
, c2
;
1312 enum tree_code code1
, code2
;
1314 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1315 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1316 same name, return -2. */
1317 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1325 code1
= TREE_CODE (val1
);
1326 n1
= TREE_OPERAND (val1
, 0);
1327 c1
= TREE_OPERAND (val1
, 1);
1328 if (tree_int_cst_sgn (c1
) == -1)
1330 if (is_negative_overflow_infinity (c1
))
1332 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1335 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1339 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1347 code2
= TREE_CODE (val2
);
1348 n2
= TREE_OPERAND (val2
, 0);
1349 c2
= TREE_OPERAND (val2
, 1);
1350 if (tree_int_cst_sgn (c2
) == -1)
1352 if (is_negative_overflow_infinity (c2
))
1354 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1357 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1361 /* Both values must use the same name. */
1362 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1364 n1
= TREE_OPERAND (n1
, 0);
1365 n2
= TREE_OPERAND (n2
, 0);
1370 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1374 /* If overflow is defined we cannot simplify more. */
1375 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1378 if (strict_overflow_p
!= NULL
1379 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1380 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1381 *strict_overflow_p
= true;
1383 if (code1
== SSA_NAME
)
1385 if (code2
== PLUS_EXPR
)
1386 /* NAME < NAME + CST */
1388 else if (code2
== MINUS_EXPR
)
1389 /* NAME > NAME - CST */
1392 else if (code1
== PLUS_EXPR
)
1394 if (code2
== SSA_NAME
)
1395 /* NAME + CST > NAME */
1397 else if (code2
== PLUS_EXPR
)
1398 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1399 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1400 else if (code2
== MINUS_EXPR
)
1401 /* NAME + CST1 > NAME - CST2 */
1404 else if (code1
== MINUS_EXPR
)
1406 if (code2
== SSA_NAME
)
1407 /* NAME - CST < NAME */
1409 else if (code2
== PLUS_EXPR
)
1410 /* NAME - CST1 < NAME + CST2 */
1412 else if (code2
== MINUS_EXPR
)
1413 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1414 C1 and C2 are swapped in the call to compare_values. */
1415 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1421 /* We cannot compare non-constants. */
1422 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1425 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1427 /* We cannot compare overflowed values, except for overflow
1429 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1431 if (strict_overflow_p
!= NULL
)
1432 *strict_overflow_p
= true;
1433 if (is_negative_overflow_infinity (val1
))
1434 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1435 else if (is_negative_overflow_infinity (val2
))
1437 else if (is_positive_overflow_infinity (val1
))
1438 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1439 else if (is_positive_overflow_infinity (val2
))
1444 return tree_int_cst_compare (val1
, val2
);
1450 /* First see if VAL1 and VAL2 are not the same. */
1451 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1454 /* If VAL1 is a lower address than VAL2, return -1. */
1455 if (operand_less_p (val1
, val2
) == 1)
1458 /* If VAL1 is a higher address than VAL2, return +1. */
1459 if (operand_less_p (val2
, val1
) == 1)
1462 /* If VAL1 is different than VAL2, return +2.
1463 For integer constants we either have already returned -1 or 1
1464 or they are equivalent. We still might succeed in proving
1465 something about non-trivial operands. */
1466 if (TREE_CODE (val1
) != INTEGER_CST
1467 || TREE_CODE (val2
) != INTEGER_CST
)
1469 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1470 if (t
&& integer_onep (t
))
1478 /* Compare values like compare_values_warnv, but treat comparisons of
1479 nonconstants which rely on undefined overflow as incomparable. */
1482 compare_values (tree val1
, tree val2
)
1488 ret
= compare_values_warnv (val1
, val2
, &sop
);
1490 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1496 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1497 0 if VAL is not inside [MIN, MAX],
1498 -2 if we cannot tell either way.
1500 Benchmark compile/20001226-1.c compilation time after changing this
1504 value_inside_range (tree val
, tree min
, tree max
)
1508 cmp1
= operand_less_p (val
, min
);
1514 cmp2
= operand_less_p (max
, val
);
1522 /* Return true if value ranges VR0 and VR1 have a non-empty
1525 Benchmark compile/20001226-1.c compilation time after changing this
1530 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1532 /* The value ranges do not intersect if the maximum of the first range is
1533 less than the minimum of the second range or vice versa.
1534 When those relations are unknown, we can't do any better. */
1535 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1537 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1543 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1544 include the value zero, -2 if we cannot tell. */
1547 range_includes_zero_p (tree min
, tree max
)
1549 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1550 return value_inside_range (zero
, min
, max
);
1553 /* Return true if *VR is know to only contain nonnegative values. */
1556 value_range_nonnegative_p (value_range_t
*vr
)
1558 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1559 which would return a useful value should be encoded as a
1561 if (vr
->type
== VR_RANGE
)
1563 int result
= compare_values (vr
->min
, integer_zero_node
);
1564 return (result
== 0 || result
== 1);
1570 /* If *VR has a value rante that is a single constant value return that,
1571 otherwise return NULL_TREE. */
1574 value_range_constant_singleton (value_range_t
*vr
)
1576 if (vr
->type
== VR_RANGE
1577 && operand_equal_p (vr
->min
, vr
->max
, 0)
1578 && is_gimple_min_invariant (vr
->min
))
1584 /* If OP has a value range with a single constant value return that,
1585 otherwise return NULL_TREE. This returns OP itself if OP is a
1589 op_with_constant_singleton_value_range (tree op
)
1591 if (is_gimple_min_invariant (op
))
1594 if (TREE_CODE (op
) != SSA_NAME
)
1597 return value_range_constant_singleton (get_value_range (op
));
1600 /* Return true if op is in a boolean [0, 1] value-range. */
1603 op_with_boolean_value_range_p (tree op
)
1607 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1610 if (integer_zerop (op
)
1611 || integer_onep (op
))
1614 if (TREE_CODE (op
) != SSA_NAME
)
1617 vr
= get_value_range (op
);
1618 return (vr
->type
== VR_RANGE
1619 && integer_zerop (vr
->min
)
1620 && integer_onep (vr
->max
));
1623 /* Extract value range information from an ASSERT_EXPR EXPR and store
1627 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1629 tree var
, cond
, limit
, min
, max
, type
;
1630 value_range_t
*limit_vr
;
1631 enum tree_code cond_code
;
1633 var
= ASSERT_EXPR_VAR (expr
);
1634 cond
= ASSERT_EXPR_COND (expr
);
1636 gcc_assert (COMPARISON_CLASS_P (cond
));
1638 /* Find VAR in the ASSERT_EXPR conditional. */
1639 if (var
== TREE_OPERAND (cond
, 0)
1640 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1641 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1643 /* If the predicate is of the form VAR COMP LIMIT, then we just
1644 take LIMIT from the RHS and use the same comparison code. */
1645 cond_code
= TREE_CODE (cond
);
1646 limit
= TREE_OPERAND (cond
, 1);
1647 cond
= TREE_OPERAND (cond
, 0);
1651 /* If the predicate is of the form LIMIT COMP VAR, then we need
1652 to flip around the comparison code to create the proper range
1654 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1655 limit
= TREE_OPERAND (cond
, 0);
1656 cond
= TREE_OPERAND (cond
, 1);
1659 limit
= avoid_overflow_infinity (limit
);
1661 type
= TREE_TYPE (var
);
1662 gcc_assert (limit
!= var
);
1664 /* For pointer arithmetic, we only keep track of pointer equality
1666 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1668 set_value_range_to_varying (vr_p
);
1672 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1673 try to use LIMIT's range to avoid creating symbolic ranges
1675 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1677 /* LIMIT's range is only interesting if it has any useful information. */
1679 && (limit_vr
->type
== VR_UNDEFINED
1680 || limit_vr
->type
== VR_VARYING
1681 || symbolic_range_p (limit_vr
)))
1684 /* Initially, the new range has the same set of equivalences of
1685 VAR's range. This will be revised before returning the final
1686 value. Since assertions may be chained via mutually exclusive
1687 predicates, we will need to trim the set of equivalences before
1689 gcc_assert (vr_p
->equiv
== NULL
);
1690 add_equivalence (&vr_p
->equiv
, var
);
1692 /* Extract a new range based on the asserted comparison for VAR and
1693 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1694 will only use it for equality comparisons (EQ_EXPR). For any
1695 other kind of assertion, we cannot derive a range from LIMIT's
1696 anti-range that can be used to describe the new range. For
1697 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1698 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1699 no single range for x_2 that could describe LE_EXPR, so we might
1700 as well build the range [b_4, +INF] for it.
1701 One special case we handle is extracting a range from a
1702 range test encoded as (unsigned)var + CST <= limit. */
1703 if (TREE_CODE (cond
) == NOP_EXPR
1704 || TREE_CODE (cond
) == PLUS_EXPR
)
1706 if (TREE_CODE (cond
) == PLUS_EXPR
)
1708 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1709 TREE_OPERAND (cond
, 1));
1710 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1711 cond
= TREE_OPERAND (cond
, 0);
1715 min
= build_int_cst (TREE_TYPE (var
), 0);
1719 /* Make sure to not set TREE_OVERFLOW on the final type
1720 conversion. We are willingly interpreting large positive
1721 unsigned values as negative singed values here. */
1722 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1723 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1725 /* We can transform a max, min range to an anti-range or
1726 vice-versa. Use set_and_canonicalize_value_range which does
1728 if (cond_code
== LE_EXPR
)
1729 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1730 min
, max
, vr_p
->equiv
);
1731 else if (cond_code
== GT_EXPR
)
1732 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1733 min
, max
, vr_p
->equiv
);
1737 else if (cond_code
== EQ_EXPR
)
1739 enum value_range_type range_type
;
1743 range_type
= limit_vr
->type
;
1744 min
= limit_vr
->min
;
1745 max
= limit_vr
->max
;
1749 range_type
= VR_RANGE
;
1754 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1756 /* When asserting the equality VAR == LIMIT and LIMIT is another
1757 SSA name, the new range will also inherit the equivalence set
1759 if (TREE_CODE (limit
) == SSA_NAME
)
1760 add_equivalence (&vr_p
->equiv
, limit
);
1762 else if (cond_code
== NE_EXPR
)
1764 /* As described above, when LIMIT's range is an anti-range and
1765 this assertion is an inequality (NE_EXPR), then we cannot
1766 derive anything from the anti-range. For instance, if
1767 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1768 not imply that VAR's range is [0, 0]. So, in the case of
1769 anti-ranges, we just assert the inequality using LIMIT and
1772 If LIMIT_VR is a range, we can only use it to build a new
1773 anti-range if LIMIT_VR is a single-valued range. For
1774 instance, if LIMIT_VR is [0, 1], the predicate
1775 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1776 Rather, it means that for value 0 VAR should be ~[0, 0]
1777 and for value 1, VAR should be ~[1, 1]. We cannot
1778 represent these ranges.
1780 The only situation in which we can build a valid
1781 anti-range is when LIMIT_VR is a single-valued range
1782 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1783 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1785 && limit_vr
->type
== VR_RANGE
1786 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1788 min
= limit_vr
->min
;
1789 max
= limit_vr
->max
;
1793 /* In any other case, we cannot use LIMIT's range to build a
1794 valid anti-range. */
1798 /* If MIN and MAX cover the whole range for their type, then
1799 just use the original LIMIT. */
1800 if (INTEGRAL_TYPE_P (type
)
1801 && vrp_val_is_min (min
)
1802 && vrp_val_is_max (max
))
1805 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1806 min
, max
, vr_p
->equiv
);
1808 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1810 min
= TYPE_MIN_VALUE (type
);
1812 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1816 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1817 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1819 max
= limit_vr
->max
;
1822 /* If the maximum value forces us to be out of bounds, simply punt.
1823 It would be pointless to try and do anything more since this
1824 all should be optimized away above us. */
1825 if ((cond_code
== LT_EXPR
1826 && compare_values (max
, min
) == 0)
1827 || is_overflow_infinity (max
))
1828 set_value_range_to_varying (vr_p
);
1831 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1832 if (cond_code
== LT_EXPR
)
1834 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1835 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1836 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1837 build_int_cst (TREE_TYPE (max
), -1));
1839 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1840 build_int_cst (TREE_TYPE (max
), 1));
1842 TREE_NO_WARNING (max
) = 1;
1845 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1848 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1850 max
= TYPE_MAX_VALUE (type
);
1852 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1856 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1857 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1859 min
= limit_vr
->min
;
1862 /* If the minimum value forces us to be out of bounds, simply punt.
1863 It would be pointless to try and do anything more since this
1864 all should be optimized away above us. */
1865 if ((cond_code
== GT_EXPR
1866 && compare_values (min
, max
) == 0)
1867 || is_overflow_infinity (min
))
1868 set_value_range_to_varying (vr_p
);
1871 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1872 if (cond_code
== GT_EXPR
)
1874 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1875 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1876 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1877 build_int_cst (TREE_TYPE (min
), -1));
1879 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1880 build_int_cst (TREE_TYPE (min
), 1));
1882 TREE_NO_WARNING (min
) = 1;
1885 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1891 /* Finally intersect the new range with what we already know about var. */
1892 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1896 /* Extract range information from SSA name VAR and store it in VR. If
1897 VAR has an interesting range, use it. Otherwise, create the
1898 range [VAR, VAR] and return it. This is useful in situations where
1899 we may have conditionals testing values of VARYING names. For
1906 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1910 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1912 value_range_t
*var_vr
= get_value_range (var
);
1914 if (var_vr
->type
!= VR_VARYING
)
1915 copy_value_range (vr
, var_vr
);
1917 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1919 add_equivalence (&vr
->equiv
, var
);
1923 /* Wrapper around int_const_binop. If the operation overflows and we
1924 are not using wrapping arithmetic, then adjust the result to be
1925 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1926 NULL_TREE if we need to use an overflow infinity representation but
1927 the type does not support it. */
1930 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1934 res
= int_const_binop (code
, val1
, val2
);
1936 /* If we are using unsigned arithmetic, operate symbolically
1937 on -INF and +INF as int_const_binop only handles signed overflow. */
1938 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1940 int checkz
= compare_values (res
, val1
);
1941 bool overflow
= false;
1943 /* Ensure that res = val1 [+*] val2 >= val1
1944 or that res = val1 - val2 <= val1. */
1945 if ((code
== PLUS_EXPR
1946 && !(checkz
== 1 || checkz
== 0))
1947 || (code
== MINUS_EXPR
1948 && !(checkz
== 0 || checkz
== -1)))
1952 /* Checking for multiplication overflow is done by dividing the
1953 output of the multiplication by the first input of the
1954 multiplication. If the result of that division operation is
1955 not equal to the second input of the multiplication, then the
1956 multiplication overflowed. */
1957 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1959 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1962 int check
= compare_values (tmp
, val2
);
1970 res
= copy_node (res
);
1971 TREE_OVERFLOW (res
) = 1;
1975 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1976 /* If the singed operation wraps then int_const_binop has done
1977 everything we want. */
1979 /* Signed division of -1/0 overflows and by the time it gets here
1980 returns NULL_TREE. */
1983 else if ((TREE_OVERFLOW (res
)
1984 && !TREE_OVERFLOW (val1
)
1985 && !TREE_OVERFLOW (val2
))
1986 || is_overflow_infinity (val1
)
1987 || is_overflow_infinity (val2
))
1989 /* If the operation overflowed but neither VAL1 nor VAL2 are
1990 overflown, return -INF or +INF depending on the operation
1991 and the combination of signs of the operands. */
1992 int sgn1
= tree_int_cst_sgn (val1
);
1993 int sgn2
= tree_int_cst_sgn (val2
);
1995 if (needs_overflow_infinity (TREE_TYPE (res
))
1996 && !supports_overflow_infinity (TREE_TYPE (res
)))
1999 /* We have to punt on adding infinities of different signs,
2000 since we can't tell what the sign of the result should be.
2001 Likewise for subtracting infinities of the same sign. */
2002 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
2003 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
2004 && is_overflow_infinity (val1
)
2005 && is_overflow_infinity (val2
))
2008 /* Don't try to handle division or shifting of infinities. */
2009 if ((code
== TRUNC_DIV_EXPR
2010 || code
== FLOOR_DIV_EXPR
2011 || code
== CEIL_DIV_EXPR
2012 || code
== EXACT_DIV_EXPR
2013 || code
== ROUND_DIV_EXPR
2014 || code
== RSHIFT_EXPR
)
2015 && (is_overflow_infinity (val1
)
2016 || is_overflow_infinity (val2
)))
2019 /* Notice that we only need to handle the restricted set of
2020 operations handled by extract_range_from_binary_expr.
2021 Among them, only multiplication, addition and subtraction
2022 can yield overflow without overflown operands because we
2023 are working with integral types only... except in the
2024 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2025 for division too. */
2027 /* For multiplication, the sign of the overflow is given
2028 by the comparison of the signs of the operands. */
2029 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
2030 /* For addition, the operands must be of the same sign
2031 to yield an overflow. Its sign is therefore that
2032 of one of the operands, for example the first. For
2033 infinite operands X + -INF is negative, not positive. */
2034 || (code
== PLUS_EXPR
2036 ? !is_negative_overflow_infinity (val2
)
2037 : is_positive_overflow_infinity (val2
)))
2038 /* For subtraction, non-infinite operands must be of
2039 different signs to yield an overflow. Its sign is
2040 therefore that of the first operand or the opposite of
2041 that of the second operand. A first operand of 0 counts
2042 as positive here, for the corner case 0 - (-INF), which
2043 overflows, but must yield +INF. For infinite operands 0
2044 - INF is negative, not positive. */
2045 || (code
== MINUS_EXPR
2047 ? !is_positive_overflow_infinity (val2
)
2048 : is_negative_overflow_infinity (val2
)))
2049 /* We only get in here with positive shift count, so the
2050 overflow direction is the same as the sign of val1.
2051 Actually rshift does not overflow at all, but we only
2052 handle the case of shifting overflowed -INF and +INF. */
2053 || (code
== RSHIFT_EXPR
2055 /* For division, the only case is -INF / -1 = +INF. */
2056 || code
== TRUNC_DIV_EXPR
2057 || code
== FLOOR_DIV_EXPR
2058 || code
== CEIL_DIV_EXPR
2059 || code
== EXACT_DIV_EXPR
2060 || code
== ROUND_DIV_EXPR
)
2061 return (needs_overflow_infinity (TREE_TYPE (res
))
2062 ? positive_overflow_infinity (TREE_TYPE (res
))
2063 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
2065 return (needs_overflow_infinity (TREE_TYPE (res
))
2066 ? negative_overflow_infinity (TREE_TYPE (res
))
2067 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
2074 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2075 bitmask if some bit is unset, it means for all numbers in the range
2076 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2077 bitmask if some bit is set, it means for all numbers in the range
2078 the bit is 1, otherwise it might be 0 or 1. */
2081 zero_nonzero_bits_from_vr (const tree expr_type
,
2083 wide_int
*may_be_nonzero
,
2084 wide_int
*must_be_nonzero
)
2086 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
2087 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
2088 if (!range_int_cst_p (vr
)
2089 || is_overflow_infinity (vr
->min
)
2090 || is_overflow_infinity (vr
->max
))
2093 if (range_int_cst_singleton_p (vr
))
2095 *may_be_nonzero
= vr
->min
;
2096 *must_be_nonzero
= *may_be_nonzero
;
2098 else if (tree_int_cst_sgn (vr
->min
) >= 0
2099 || tree_int_cst_sgn (vr
->max
) < 0)
2101 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2102 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2103 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2106 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2107 may_be_nonzero
->get_precision ());
2108 *may_be_nonzero
= *may_be_nonzero
| mask
;
2109 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2116 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2117 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2118 false otherwise. If *AR can be represented with a single range
2119 *VR1 will be VR_UNDEFINED. */
2122 ranges_from_anti_range (value_range_t
*ar
,
2123 value_range_t
*vr0
, value_range_t
*vr1
)
2125 tree type
= TREE_TYPE (ar
->min
);
2127 vr0
->type
= VR_UNDEFINED
;
2128 vr1
->type
= VR_UNDEFINED
;
2130 if (ar
->type
!= VR_ANTI_RANGE
2131 || TREE_CODE (ar
->min
) != INTEGER_CST
2132 || TREE_CODE (ar
->max
) != INTEGER_CST
2133 || !vrp_val_min (type
)
2134 || !vrp_val_max (type
))
2137 if (!vrp_val_is_min (ar
->min
))
2139 vr0
->type
= VR_RANGE
;
2140 vr0
->min
= vrp_val_min (type
);
2141 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2143 if (!vrp_val_is_max (ar
->max
))
2145 vr1
->type
= VR_RANGE
;
2146 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2147 vr1
->max
= vrp_val_max (type
);
2149 if (vr0
->type
== VR_UNDEFINED
)
2152 vr1
->type
= VR_UNDEFINED
;
2155 return vr0
->type
!= VR_UNDEFINED
;
2158 /* Helper to extract a value-range *VR for a multiplicative operation
2162 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2163 enum tree_code code
,
2164 value_range_t
*vr0
, value_range_t
*vr1
)
2166 enum value_range_type type
;
2173 /* Multiplications, divisions and shifts are a bit tricky to handle,
2174 depending on the mix of signs we have in the two ranges, we
2175 need to operate on different values to get the minimum and
2176 maximum values for the new range. One approach is to figure
2177 out all the variations of range combinations and do the
2180 However, this involves several calls to compare_values and it
2181 is pretty convoluted. It's simpler to do the 4 operations
2182 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2183 MAX1) and then figure the smallest and largest values to form
2185 gcc_assert (code
== MULT_EXPR
2186 || code
== TRUNC_DIV_EXPR
2187 || code
== FLOOR_DIV_EXPR
2188 || code
== CEIL_DIV_EXPR
2189 || code
== EXACT_DIV_EXPR
2190 || code
== ROUND_DIV_EXPR
2191 || code
== RSHIFT_EXPR
2192 || code
== LSHIFT_EXPR
);
2193 gcc_assert ((vr0
->type
== VR_RANGE
2194 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2195 && vr0
->type
== vr1
->type
);
2199 /* Compute the 4 cross operations. */
2201 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2202 if (val
[0] == NULL_TREE
)
2205 if (vr1
->max
== vr1
->min
)
2209 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2210 if (val
[1] == NULL_TREE
)
2214 if (vr0
->max
== vr0
->min
)
2218 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2219 if (val
[2] == NULL_TREE
)
2223 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2227 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2228 if (val
[3] == NULL_TREE
)
2234 set_value_range_to_varying (vr
);
2238 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2242 for (i
= 1; i
< 4; i
++)
2244 if (!is_gimple_min_invariant (min
)
2245 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2246 || !is_gimple_min_invariant (max
)
2247 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2252 if (!is_gimple_min_invariant (val
[i
])
2253 || (TREE_OVERFLOW (val
[i
])
2254 && !is_overflow_infinity (val
[i
])))
2256 /* If we found an overflowed value, set MIN and MAX
2257 to it so that we set the resulting range to
2263 if (compare_values (val
[i
], min
) == -1)
2266 if (compare_values (val
[i
], max
) == 1)
2271 /* If either MIN or MAX overflowed, then set the resulting range to
2272 VARYING. But we do accept an overflow infinity
2274 if (min
== NULL_TREE
2275 || !is_gimple_min_invariant (min
)
2276 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2278 || !is_gimple_min_invariant (max
)
2279 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2281 set_value_range_to_varying (vr
);
2287 2) [-INF, +-INF(OVF)]
2288 3) [+-INF(OVF), +INF]
2289 4) [+-INF(OVF), +-INF(OVF)]
2290 We learn nothing when we have INF and INF(OVF) on both sides.
2291 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2293 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2294 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2296 set_value_range_to_varying (vr
);
2300 cmp
= compare_values (min
, max
);
2301 if (cmp
== -2 || cmp
== 1)
2303 /* If the new range has its limits swapped around (MIN > MAX),
2304 then the operation caused one of them to wrap around, mark
2305 the new range VARYING. */
2306 set_value_range_to_varying (vr
);
2309 set_value_range (vr
, type
, min
, max
, NULL
);
2312 /* Extract range information from a binary operation CODE based on
2313 the ranges of each of its operands *VR0 and *VR1 with resulting
2314 type EXPR_TYPE. The resulting range is stored in *VR. */
2317 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2318 enum tree_code code
, tree expr_type
,
2319 value_range_t
*vr0_
, value_range_t
*vr1_
)
2321 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2322 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2323 enum value_range_type type
;
2324 tree min
= NULL_TREE
, max
= NULL_TREE
;
2327 if (!INTEGRAL_TYPE_P (expr_type
)
2328 && !POINTER_TYPE_P (expr_type
))
2330 set_value_range_to_varying (vr
);
2334 /* Not all binary expressions can be applied to ranges in a
2335 meaningful way. Handle only arithmetic operations. */
2336 if (code
!= PLUS_EXPR
2337 && code
!= MINUS_EXPR
2338 && code
!= POINTER_PLUS_EXPR
2339 && code
!= MULT_EXPR
2340 && code
!= TRUNC_DIV_EXPR
2341 && code
!= FLOOR_DIV_EXPR
2342 && code
!= CEIL_DIV_EXPR
2343 && code
!= EXACT_DIV_EXPR
2344 && code
!= ROUND_DIV_EXPR
2345 && code
!= TRUNC_MOD_EXPR
2346 && code
!= RSHIFT_EXPR
2347 && code
!= LSHIFT_EXPR
2350 && code
!= BIT_AND_EXPR
2351 && code
!= BIT_IOR_EXPR
2352 && code
!= BIT_XOR_EXPR
)
2354 set_value_range_to_varying (vr
);
2358 /* If both ranges are UNDEFINED, so is the result. */
2359 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2361 set_value_range_to_undefined (vr
);
2364 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2365 code. At some point we may want to special-case operations that
2366 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2368 else if (vr0
.type
== VR_UNDEFINED
)
2369 set_value_range_to_varying (&vr0
);
2370 else if (vr1
.type
== VR_UNDEFINED
)
2371 set_value_range_to_varying (&vr1
);
2373 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2374 and express ~[] op X as ([]' op X) U ([]'' op X). */
2375 if (vr0
.type
== VR_ANTI_RANGE
2376 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2378 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2379 if (vrtem1
.type
!= VR_UNDEFINED
)
2381 value_range_t vrres
= VR_INITIALIZER
;
2382 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2384 vrp_meet (vr
, &vrres
);
2388 /* Likewise for X op ~[]. */
2389 if (vr1
.type
== VR_ANTI_RANGE
2390 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2392 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2393 if (vrtem1
.type
!= VR_UNDEFINED
)
2395 value_range_t vrres
= VR_INITIALIZER
;
2396 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2398 vrp_meet (vr
, &vrres
);
2403 /* The type of the resulting value range defaults to VR0.TYPE. */
2406 /* Refuse to operate on VARYING ranges, ranges of different kinds
2407 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2408 because we may be able to derive a useful range even if one of
2409 the operands is VR_VARYING or symbolic range. Similarly for
2410 divisions, MIN/MAX and PLUS/MINUS.
2412 TODO, we may be able to derive anti-ranges in some cases. */
2413 if (code
!= BIT_AND_EXPR
2414 && code
!= BIT_IOR_EXPR
2415 && code
!= TRUNC_DIV_EXPR
2416 && code
!= FLOOR_DIV_EXPR
2417 && code
!= CEIL_DIV_EXPR
2418 && code
!= EXACT_DIV_EXPR
2419 && code
!= ROUND_DIV_EXPR
2420 && code
!= TRUNC_MOD_EXPR
2423 && code
!= PLUS_EXPR
2424 && code
!= MINUS_EXPR
2425 && (vr0
.type
== VR_VARYING
2426 || vr1
.type
== VR_VARYING
2427 || vr0
.type
!= vr1
.type
2428 || symbolic_range_p (&vr0
)
2429 || symbolic_range_p (&vr1
)))
2431 set_value_range_to_varying (vr
);
2435 /* Now evaluate the expression to determine the new range. */
2436 if (POINTER_TYPE_P (expr_type
))
2438 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2440 /* For MIN/MAX expressions with pointers, we only care about
2441 nullness, if both are non null, then the result is nonnull.
2442 If both are null, then the result is null. Otherwise they
2444 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2445 set_value_range_to_nonnull (vr
, expr_type
);
2446 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2447 set_value_range_to_null (vr
, expr_type
);
2449 set_value_range_to_varying (vr
);
2451 else if (code
== POINTER_PLUS_EXPR
)
2453 /* For pointer types, we are really only interested in asserting
2454 whether the expression evaluates to non-NULL. */
2455 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2456 set_value_range_to_nonnull (vr
, expr_type
);
2457 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2458 set_value_range_to_null (vr
, expr_type
);
2460 set_value_range_to_varying (vr
);
2462 else if (code
== BIT_AND_EXPR
)
2464 /* For pointer types, we are really only interested in asserting
2465 whether the expression evaluates to non-NULL. */
2466 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2467 set_value_range_to_nonnull (vr
, expr_type
);
2468 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2469 set_value_range_to_null (vr
, expr_type
);
2471 set_value_range_to_varying (vr
);
2474 set_value_range_to_varying (vr
);
2479 /* For integer ranges, apply the operation to each end of the
2480 range and see what we end up with. */
2481 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2483 const bool minus_p
= (code
== MINUS_EXPR
);
2484 tree min_op0
= vr0
.min
;
2485 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2486 tree max_op0
= vr0
.max
;
2487 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2488 tree sym_min_op0
= NULL_TREE
;
2489 tree sym_min_op1
= NULL_TREE
;
2490 tree sym_max_op0
= NULL_TREE
;
2491 tree sym_max_op1
= NULL_TREE
;
2492 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2494 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2495 single-symbolic ranges, try to compute the precise resulting range,
2496 but only if we know that this resulting range will also be constant
2497 or single-symbolic. */
2498 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2499 && (TREE_CODE (min_op0
) == INTEGER_CST
2501 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2502 && (TREE_CODE (min_op1
) == INTEGER_CST
2504 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2505 && (!(sym_min_op0
&& sym_min_op1
)
2506 || (sym_min_op0
== sym_min_op1
2507 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2508 && (TREE_CODE (max_op0
) == INTEGER_CST
2510 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2511 && (TREE_CODE (max_op1
) == INTEGER_CST
2513 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2514 && (!(sym_max_op0
&& sym_max_op1
)
2515 || (sym_max_op0
== sym_max_op1
2516 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2518 const signop sgn
= TYPE_SIGN (expr_type
);
2519 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2520 wide_int type_min
, type_max
, wmin
, wmax
;
2524 /* Get the lower and upper bounds of the type. */
2525 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2527 type_min
= wi::min_value (prec
, sgn
);
2528 type_max
= wi::max_value (prec
, sgn
);
2532 type_min
= vrp_val_min (expr_type
);
2533 type_max
= vrp_val_max (expr_type
);
2536 /* Combine the lower bounds, if any. */
2537 if (min_op0
&& min_op1
)
2541 wmin
= wi::sub (min_op0
, min_op1
);
2543 /* Check for overflow. */
2544 if (wi::cmp (0, min_op1
, sgn
)
2545 != wi::cmp (wmin
, min_op0
, sgn
))
2546 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2550 wmin
= wi::add (min_op0
, min_op1
);
2552 /* Check for overflow. */
2553 if (wi::cmp (min_op1
, 0, sgn
)
2554 != wi::cmp (wmin
, min_op0
, sgn
))
2555 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2561 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2563 wmin
= wi::shwi (0, prec
);
2565 /* Combine the upper bounds, if any. */
2566 if (max_op0
&& max_op1
)
2570 wmax
= wi::sub (max_op0
, max_op1
);
2572 /* Check for overflow. */
2573 if (wi::cmp (0, max_op1
, sgn
)
2574 != wi::cmp (wmax
, max_op0
, sgn
))
2575 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2579 wmax
= wi::add (max_op0
, max_op1
);
2581 if (wi::cmp (max_op1
, 0, sgn
)
2582 != wi::cmp (wmax
, max_op0
, sgn
))
2583 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2589 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2591 wmax
= wi::shwi (0, prec
);
2593 /* Check for type overflow. */
2596 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2598 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2603 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2605 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2609 /* If we have overflow for the constant part and the resulting
2610 range will be symbolic, drop to VR_VARYING. */
2611 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2612 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2614 set_value_range_to_varying (vr
);
2618 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2620 /* If overflow wraps, truncate the values and adjust the
2621 range kind and bounds appropriately. */
2622 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2623 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2624 if (min_ovf
== max_ovf
)
2626 /* No overflow or both overflow or underflow. The
2627 range kind stays VR_RANGE. */
2628 min
= wide_int_to_tree (expr_type
, tmin
);
2629 max
= wide_int_to_tree (expr_type
, tmax
);
2631 else if (min_ovf
== -1 && max_ovf
== 1)
2633 /* Underflow and overflow, drop to VR_VARYING. */
2634 set_value_range_to_varying (vr
);
2639 /* Min underflow or max overflow. The range kind
2640 changes to VR_ANTI_RANGE. */
2641 bool covers
= false;
2642 wide_int tem
= tmin
;
2643 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2644 || (max_ovf
== 1 && min_ovf
== 0));
2645 type
= VR_ANTI_RANGE
;
2647 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2650 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2652 /* If the anti-range would cover nothing, drop to varying.
2653 Likewise if the anti-range bounds are outside of the
2655 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2657 set_value_range_to_varying (vr
);
2660 min
= wide_int_to_tree (expr_type
, tmin
);
2661 max
= wide_int_to_tree (expr_type
, tmax
);
2666 /* If overflow does not wrap, saturate to the types min/max
2670 if (needs_overflow_infinity (expr_type
)
2671 && supports_overflow_infinity (expr_type
))
2672 min
= negative_overflow_infinity (expr_type
);
2674 min
= wide_int_to_tree (expr_type
, type_min
);
2676 else if (min_ovf
== 1)
2678 if (needs_overflow_infinity (expr_type
)
2679 && supports_overflow_infinity (expr_type
))
2680 min
= positive_overflow_infinity (expr_type
);
2682 min
= wide_int_to_tree (expr_type
, type_max
);
2685 min
= wide_int_to_tree (expr_type
, wmin
);
2689 if (needs_overflow_infinity (expr_type
)
2690 && supports_overflow_infinity (expr_type
))
2691 max
= negative_overflow_infinity (expr_type
);
2693 max
= wide_int_to_tree (expr_type
, type_min
);
2695 else if (max_ovf
== 1)
2697 if (needs_overflow_infinity (expr_type
)
2698 && supports_overflow_infinity (expr_type
))
2699 max
= positive_overflow_infinity (expr_type
);
2701 max
= wide_int_to_tree (expr_type
, type_max
);
2704 max
= wide_int_to_tree (expr_type
, wmax
);
2707 if (needs_overflow_infinity (expr_type
)
2708 && supports_overflow_infinity (expr_type
))
2710 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2713 ? is_positive_overflow_infinity (min_op1
)
2714 : is_negative_overflow_infinity (min_op1
))))
2715 min
= negative_overflow_infinity (expr_type
);
2716 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2719 ? is_negative_overflow_infinity (max_op1
)
2720 : is_positive_overflow_infinity (max_op1
))))
2721 max
= positive_overflow_infinity (expr_type
);
2724 /* If the result lower bound is constant, we're done;
2725 otherwise, build the symbolic lower bound. */
2726 if (sym_min_op0
== sym_min_op1
)
2728 else if (sym_min_op0
)
2729 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2731 else if (sym_min_op1
)
2732 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2733 neg_min_op1
^ minus_p
, min
);
2735 /* Likewise for the upper bound. */
2736 if (sym_max_op0
== sym_max_op1
)
2738 else if (sym_max_op0
)
2739 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2741 else if (sym_max_op1
)
2742 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2743 neg_max_op1
^ minus_p
, max
);
2747 /* For other cases, for example if we have a PLUS_EXPR with two
2748 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2749 to compute a precise range for such a case.
2750 ??? General even mixed range kind operations can be expressed
2751 by for example transforming ~[3, 5] + [1, 2] to range-only
2752 operations and a union primitive:
2753 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2754 [-INF+1, 4] U [6, +INF(OVF)]
2755 though usually the union is not exactly representable with
2756 a single range or anti-range as the above is
2757 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2758 but one could use a scheme similar to equivalences for this. */
2759 set_value_range_to_varying (vr
);
2763 else if (code
== MIN_EXPR
2764 || code
== MAX_EXPR
)
2766 if (vr0
.type
== VR_RANGE
2767 && !symbolic_range_p (&vr0
))
2770 if (vr1
.type
== VR_RANGE
2771 && !symbolic_range_p (&vr1
))
2773 /* For operations that make the resulting range directly
2774 proportional to the original ranges, apply the operation to
2775 the same end of each range. */
2776 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2777 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2779 else if (code
== MIN_EXPR
)
2781 min
= vrp_val_min (expr_type
);
2784 else if (code
== MAX_EXPR
)
2787 max
= vrp_val_max (expr_type
);
2790 else if (vr1
.type
== VR_RANGE
2791 && !symbolic_range_p (&vr1
))
2794 if (code
== MIN_EXPR
)
2796 min
= vrp_val_min (expr_type
);
2799 else if (code
== MAX_EXPR
)
2802 max
= vrp_val_max (expr_type
);
2807 set_value_range_to_varying (vr
);
2811 else if (code
== MULT_EXPR
)
2813 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2814 drop to varying. This test requires 2*prec bits if both
2815 operands are signed and 2*prec + 2 bits if either is not. */
2817 signop sign
= TYPE_SIGN (expr_type
);
2818 unsigned int prec
= TYPE_PRECISION (expr_type
);
2820 if (range_int_cst_p (&vr0
)
2821 && range_int_cst_p (&vr1
)
2822 && TYPE_OVERFLOW_WRAPS (expr_type
))
2824 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2825 typedef generic_wide_int
2826 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2827 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2828 vrp_int size
= sizem1
+ 1;
2830 /* Extend the values using the sign of the result to PREC2.
2831 From here on out, everthing is just signed math no matter
2832 what the input types were. */
2833 vrp_int min0
= vrp_int_cst (vr0
.min
);
2834 vrp_int max0
= vrp_int_cst (vr0
.max
);
2835 vrp_int min1
= vrp_int_cst (vr1
.min
);
2836 vrp_int max1
= vrp_int_cst (vr1
.max
);
2837 /* Canonicalize the intervals. */
2838 if (sign
== UNSIGNED
)
2840 if (wi::ltu_p (size
, min0
+ max0
))
2846 if (wi::ltu_p (size
, min1
+ max1
))
2853 vrp_int prod0
= min0
* min1
;
2854 vrp_int prod1
= min0
* max1
;
2855 vrp_int prod2
= max0
* min1
;
2856 vrp_int prod3
= max0
* max1
;
2858 /* Sort the 4 products so that min is in prod0 and max is in
2860 /* min0min1 > max0max1 */
2861 if (wi::gts_p (prod0
, prod3
))
2863 vrp_int tmp
= prod3
;
2868 /* min0max1 > max0min1 */
2869 if (wi::gts_p (prod1
, prod2
))
2871 vrp_int tmp
= prod2
;
2876 if (wi::gts_p (prod0
, prod1
))
2878 vrp_int tmp
= prod1
;
2883 if (wi::gts_p (prod2
, prod3
))
2885 vrp_int tmp
= prod3
;
2890 /* diff = max - min. */
2891 prod2
= prod3
- prod0
;
2892 if (wi::geu_p (prod2
, sizem1
))
2894 /* the range covers all values. */
2895 set_value_range_to_varying (vr
);
2899 /* The following should handle the wrapping and selecting
2900 VR_ANTI_RANGE for us. */
2901 min
= wide_int_to_tree (expr_type
, prod0
);
2902 max
= wide_int_to_tree (expr_type
, prod3
);
2903 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2907 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2908 drop to VR_VARYING. It would take more effort to compute a
2909 precise range for such a case. For example, if we have
2910 op0 == 65536 and op1 == 65536 with their ranges both being
2911 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2912 we cannot claim that the product is in ~[0,0]. Note that we
2913 are guaranteed to have vr0.type == vr1.type at this
2915 if (vr0
.type
== VR_ANTI_RANGE
2916 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2918 set_value_range_to_varying (vr
);
2922 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2925 else if (code
== RSHIFT_EXPR
2926 || code
== LSHIFT_EXPR
)
2928 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2929 then drop to VR_VARYING. Outside of this range we get undefined
2930 behavior from the shift operation. We cannot even trust
2931 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2932 shifts, and the operation at the tree level may be widened. */
2933 if (range_int_cst_p (&vr1
)
2934 && compare_tree_int (vr1
.min
, 0) >= 0
2935 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2937 if (code
== RSHIFT_EXPR
)
2939 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2942 /* We can map lshifts by constants to MULT_EXPR handling. */
2943 else if (code
== LSHIFT_EXPR
2944 && range_int_cst_singleton_p (&vr1
))
2946 bool saved_flag_wrapv
;
2947 value_range_t vr1p
= VR_INITIALIZER
;
2948 vr1p
.type
= VR_RANGE
;
2949 vr1p
.min
= (wide_int_to_tree
2951 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2952 TYPE_PRECISION (expr_type
))));
2953 vr1p
.max
= vr1p
.min
;
2954 /* We have to use a wrapping multiply though as signed overflow
2955 on lshifts is implementation defined in C89. */
2956 saved_flag_wrapv
= flag_wrapv
;
2958 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2960 flag_wrapv
= saved_flag_wrapv
;
2963 else if (code
== LSHIFT_EXPR
2964 && range_int_cst_p (&vr0
))
2966 int prec
= TYPE_PRECISION (expr_type
);
2967 int overflow_pos
= prec
;
2969 wide_int low_bound
, high_bound
;
2970 bool uns
= TYPE_UNSIGNED (expr_type
);
2971 bool in_bounds
= false;
2976 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2977 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2978 overflow. However, for that to happen, vr1.max needs to be
2979 zero, which means vr1 is a singleton range of zero, which
2980 means it should be handled by the previous LSHIFT_EXPR
2982 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2983 wide_int complement
= ~(bound
- 1);
2988 high_bound
= complement
;
2989 if (wi::ltu_p (vr0
.max
, low_bound
))
2991 /* [5, 6] << [1, 2] == [10, 24]. */
2992 /* We're shifting out only zeroes, the value increases
2996 else if (wi::ltu_p (high_bound
, vr0
.min
))
2998 /* [0xffffff00, 0xffffffff] << [1, 2]
2999 == [0xfffffc00, 0xfffffffe]. */
3000 /* We're shifting out only ones, the value decreases
3007 /* [-1, 1] << [1, 2] == [-4, 4]. */
3008 low_bound
= complement
;
3010 if (wi::lts_p (vr0
.max
, high_bound
)
3011 && wi::lts_p (low_bound
, vr0
.min
))
3013 /* For non-negative numbers, we're shifting out only
3014 zeroes, the value increases monotonically.
3015 For negative numbers, we're shifting out only ones, the
3016 value decreases monotomically. */
3023 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3028 set_value_range_to_varying (vr
);
3031 else if (code
== TRUNC_DIV_EXPR
3032 || code
== FLOOR_DIV_EXPR
3033 || code
== CEIL_DIV_EXPR
3034 || code
== EXACT_DIV_EXPR
3035 || code
== ROUND_DIV_EXPR
)
3037 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
3039 /* For division, if op1 has VR_RANGE but op0 does not, something
3040 can be deduced just from that range. Say [min, max] / [4, max]
3041 gives [min / 4, max / 4] range. */
3042 if (vr1
.type
== VR_RANGE
3043 && !symbolic_range_p (&vr1
)
3044 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
3046 vr0
.type
= type
= VR_RANGE
;
3047 vr0
.min
= vrp_val_min (expr_type
);
3048 vr0
.max
= vrp_val_max (expr_type
);
3052 set_value_range_to_varying (vr
);
3057 /* For divisions, if flag_non_call_exceptions is true, we must
3058 not eliminate a division by zero. */
3059 if (cfun
->can_throw_non_call_exceptions
3060 && (vr1
.type
!= VR_RANGE
3061 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3063 set_value_range_to_varying (vr
);
3067 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3068 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3070 if (vr0
.type
== VR_RANGE
3071 && (vr1
.type
!= VR_RANGE
3072 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3074 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
3079 if (TYPE_UNSIGNED (expr_type
)
3080 || value_range_nonnegative_p (&vr1
))
3082 /* For unsigned division or when divisor is known
3083 to be non-negative, the range has to cover
3084 all numbers from 0 to max for positive max
3085 and all numbers from min to 0 for negative min. */
3086 cmp
= compare_values (vr0
.max
, zero
);
3089 else if (cmp
== 0 || cmp
== 1)
3093 cmp
= compare_values (vr0
.min
, zero
);
3096 else if (cmp
== 0 || cmp
== -1)
3103 /* Otherwise the range is -max .. max or min .. -min
3104 depending on which bound is bigger in absolute value,
3105 as the division can change the sign. */
3106 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3109 if (type
== VR_VARYING
)
3111 set_value_range_to_varying (vr
);
3117 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3121 else if (code
== TRUNC_MOD_EXPR
)
3123 if (vr1
.type
!= VR_RANGE
3124 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
3125 || vrp_val_is_min (vr1
.min
))
3127 set_value_range_to_varying (vr
);
3131 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3132 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
3133 if (tree_int_cst_lt (max
, vr1
.max
))
3135 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
3136 /* If the dividend is non-negative the modulus will be
3137 non-negative as well. */
3138 if (TYPE_UNSIGNED (expr_type
)
3139 || value_range_nonnegative_p (&vr0
))
3140 min
= build_int_cst (TREE_TYPE (max
), 0);
3142 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
3144 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3146 bool int_cst_range0
, int_cst_range1
;
3147 wide_int may_be_nonzero0
, may_be_nonzero1
;
3148 wide_int must_be_nonzero0
, must_be_nonzero1
;
3150 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3153 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3158 if (code
== BIT_AND_EXPR
)
3160 min
= wide_int_to_tree (expr_type
,
3161 must_be_nonzero0
& must_be_nonzero1
);
3162 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3163 /* If both input ranges contain only negative values we can
3164 truncate the result range maximum to the minimum of the
3165 input range maxima. */
3166 if (int_cst_range0
&& int_cst_range1
3167 && tree_int_cst_sgn (vr0
.max
) < 0
3168 && tree_int_cst_sgn (vr1
.max
) < 0)
3170 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3171 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3173 /* If either input range contains only non-negative values
3174 we can truncate the result range maximum to the respective
3175 maximum of the input range. */
3176 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3177 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3178 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3179 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3180 max
= wide_int_to_tree (expr_type
, wmax
);
3182 else if (code
== BIT_IOR_EXPR
)
3184 max
= wide_int_to_tree (expr_type
,
3185 may_be_nonzero0
| may_be_nonzero1
);
3186 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3187 /* If the input ranges contain only positive values we can
3188 truncate the minimum of the result range to the maximum
3189 of the input range minima. */
3190 if (int_cst_range0
&& int_cst_range1
3191 && tree_int_cst_sgn (vr0
.min
) >= 0
3192 && tree_int_cst_sgn (vr1
.min
) >= 0)
3194 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3195 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3197 /* If either input range contains only negative values
3198 we can truncate the minimum of the result range to the
3199 respective minimum range. */
3200 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3201 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3202 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3203 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3204 min
= wide_int_to_tree (expr_type
, wmin
);
3206 else if (code
== BIT_XOR_EXPR
)
3208 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3209 | ~(may_be_nonzero0
| may_be_nonzero1
));
3210 wide_int result_one_bits
3211 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3212 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3213 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3214 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3215 /* If the range has all positive or all negative values the
3216 result is better than VARYING. */
3217 if (tree_int_cst_sgn (min
) < 0
3218 || tree_int_cst_sgn (max
) >= 0)
3221 max
= min
= NULL_TREE
;
3227 /* If either MIN or MAX overflowed, then set the resulting range to
3228 VARYING. But we do accept an overflow infinity representation. */
3229 if (min
== NULL_TREE
3230 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3232 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3234 set_value_range_to_varying (vr
);
3240 2) [-INF, +-INF(OVF)]
3241 3) [+-INF(OVF), +INF]
3242 4) [+-INF(OVF), +-INF(OVF)]
3243 We learn nothing when we have INF and INF(OVF) on both sides.
3244 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3246 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3247 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3249 set_value_range_to_varying (vr
);
3253 cmp
= compare_values (min
, max
);
3254 if (cmp
== -2 || cmp
== 1)
3256 /* If the new range has its limits swapped around (MIN > MAX),
3257 then the operation caused one of them to wrap around, mark
3258 the new range VARYING. */
3259 set_value_range_to_varying (vr
);
3262 set_value_range (vr
, type
, min
, max
, NULL
);
3265 /* Extract range information from a binary expression OP0 CODE OP1 based on
3266 the ranges of each of its operands with resulting type EXPR_TYPE.
3267 The resulting range is stored in *VR. */
3270 extract_range_from_binary_expr (value_range_t
*vr
,
3271 enum tree_code code
,
3272 tree expr_type
, tree op0
, tree op1
)
3274 value_range_t vr0
= VR_INITIALIZER
;
3275 value_range_t vr1
= VR_INITIALIZER
;
3277 /* Get value ranges for each operand. For constant operands, create
3278 a new value range with the operand to simplify processing. */
3279 if (TREE_CODE (op0
) == SSA_NAME
)
3280 vr0
= *(get_value_range (op0
));
3281 else if (is_gimple_min_invariant (op0
))
3282 set_value_range_to_value (&vr0
, op0
, NULL
);
3284 set_value_range_to_varying (&vr0
);
3286 if (TREE_CODE (op1
) == SSA_NAME
)
3287 vr1
= *(get_value_range (op1
));
3288 else if (is_gimple_min_invariant (op1
))
3289 set_value_range_to_value (&vr1
, op1
, NULL
);
3291 set_value_range_to_varying (&vr1
);
3293 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3295 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3296 and based on the other operand, for example if it was deduced from a
3297 symbolic comparison. When a bound of the range of the first operand
3298 is invariant, we set the corresponding bound of the new range to INF
3299 in order to avoid recursing on the range of the second operand. */
3300 if (vr
->type
== VR_VARYING
3301 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3302 && TREE_CODE (op1
) == SSA_NAME
3303 && vr0
.type
== VR_RANGE
3304 && symbolic_range_based_on_p (&vr0
, op1
))
3306 const bool minus_p
= (code
== MINUS_EXPR
);
3307 value_range_t n_vr1
= VR_INITIALIZER
;
3309 /* Try with VR0 and [-INF, OP1]. */
3310 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3311 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3313 /* Try with VR0 and [OP1, +INF]. */
3314 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3315 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3317 /* Try with VR0 and [OP1, OP1]. */
3319 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3321 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3324 if (vr
->type
== VR_VARYING
3325 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3326 && TREE_CODE (op0
) == SSA_NAME
3327 && vr1
.type
== VR_RANGE
3328 && symbolic_range_based_on_p (&vr1
, op0
))
3330 const bool minus_p
= (code
== MINUS_EXPR
);
3331 value_range_t n_vr0
= VR_INITIALIZER
;
3333 /* Try with [-INF, OP0] and VR1. */
3334 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3335 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3337 /* Try with [OP0, +INF] and VR1. */
3338 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3339 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3341 /* Try with [OP0, OP0] and VR1. */
3343 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3345 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3349 /* Extract range information from a unary operation CODE based on
3350 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3351 The The resulting range is stored in *VR. */
3354 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3355 enum tree_code code
, tree type
,
3356 value_range_t
*vr0_
, tree op0_type
)
3358 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3360 /* VRP only operates on integral and pointer types. */
3361 if (!(INTEGRAL_TYPE_P (op0_type
)
3362 || POINTER_TYPE_P (op0_type
))
3363 || !(INTEGRAL_TYPE_P (type
)
3364 || POINTER_TYPE_P (type
)))
3366 set_value_range_to_varying (vr
);
3370 /* If VR0 is UNDEFINED, so is the result. */
3371 if (vr0
.type
== VR_UNDEFINED
)
3373 set_value_range_to_undefined (vr
);
3377 /* Handle operations that we express in terms of others. */
3378 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3380 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3381 copy_value_range (vr
, &vr0
);
3384 else if (code
== NEGATE_EXPR
)
3386 /* -X is simply 0 - X, so re-use existing code that also handles
3387 anti-ranges fine. */
3388 value_range_t zero
= VR_INITIALIZER
;
3389 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3390 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3393 else if (code
== BIT_NOT_EXPR
)
3395 /* ~X is simply -1 - X, so re-use existing code that also handles
3396 anti-ranges fine. */
3397 value_range_t minusone
= VR_INITIALIZER
;
3398 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3399 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3400 type
, &minusone
, &vr0
);
3404 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3405 and express op ~[] as (op []') U (op []''). */
3406 if (vr0
.type
== VR_ANTI_RANGE
3407 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3409 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3410 if (vrtem1
.type
!= VR_UNDEFINED
)
3412 value_range_t vrres
= VR_INITIALIZER
;
3413 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3415 vrp_meet (vr
, &vrres
);
3420 if (CONVERT_EXPR_CODE_P (code
))
3422 tree inner_type
= op0_type
;
3423 tree outer_type
= type
;
3425 /* If the expression evaluates to a pointer, we are only interested in
3426 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3427 if (POINTER_TYPE_P (type
))
3429 if (range_is_nonnull (&vr0
))
3430 set_value_range_to_nonnull (vr
, type
);
3431 else if (range_is_null (&vr0
))
3432 set_value_range_to_null (vr
, type
);
3434 set_value_range_to_varying (vr
);
3438 /* If VR0 is varying and we increase the type precision, assume
3439 a full range for the following transformation. */
3440 if (vr0
.type
== VR_VARYING
3441 && INTEGRAL_TYPE_P (inner_type
)
3442 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3444 vr0
.type
= VR_RANGE
;
3445 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3446 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3449 /* If VR0 is a constant range or anti-range and the conversion is
3450 not truncating we can convert the min and max values and
3451 canonicalize the resulting range. Otherwise we can do the
3452 conversion if the size of the range is less than what the
3453 precision of the target type can represent and the range is
3454 not an anti-range. */
3455 if ((vr0
.type
== VR_RANGE
3456 || vr0
.type
== VR_ANTI_RANGE
)
3457 && TREE_CODE (vr0
.min
) == INTEGER_CST
3458 && TREE_CODE (vr0
.max
) == INTEGER_CST
3459 && (!is_overflow_infinity (vr0
.min
)
3460 || (vr0
.type
== VR_RANGE
3461 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3462 && needs_overflow_infinity (outer_type
)
3463 && supports_overflow_infinity (outer_type
)))
3464 && (!is_overflow_infinity (vr0
.max
)
3465 || (vr0
.type
== VR_RANGE
3466 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3467 && needs_overflow_infinity (outer_type
)
3468 && supports_overflow_infinity (outer_type
)))
3469 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3470 || (vr0
.type
== VR_RANGE
3471 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3472 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3473 size_int (TYPE_PRECISION (outer_type
)))))))
3475 tree new_min
, new_max
;
3476 if (is_overflow_infinity (vr0
.min
))
3477 new_min
= negative_overflow_infinity (outer_type
);
3479 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3481 if (is_overflow_infinity (vr0
.max
))
3482 new_max
= positive_overflow_infinity (outer_type
);
3484 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3486 set_and_canonicalize_value_range (vr
, vr0
.type
,
3487 new_min
, new_max
, NULL
);
3491 set_value_range_to_varying (vr
);
3494 else if (code
== ABS_EXPR
)
3499 /* Pass through vr0 in the easy cases. */
3500 if (TYPE_UNSIGNED (type
)
3501 || value_range_nonnegative_p (&vr0
))
3503 copy_value_range (vr
, &vr0
);
3507 /* For the remaining varying or symbolic ranges we can't do anything
3509 if (vr0
.type
== VR_VARYING
3510 || symbolic_range_p (&vr0
))
3512 set_value_range_to_varying (vr
);
3516 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3518 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3519 && ((vr0
.type
== VR_RANGE
3520 && vrp_val_is_min (vr0
.min
))
3521 || (vr0
.type
== VR_ANTI_RANGE
3522 && !vrp_val_is_min (vr0
.min
))))
3524 set_value_range_to_varying (vr
);
3528 /* ABS_EXPR may flip the range around, if the original range
3529 included negative values. */
3530 if (is_overflow_infinity (vr0
.min
))
3531 min
= positive_overflow_infinity (type
);
3532 else if (!vrp_val_is_min (vr0
.min
))
3533 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3534 else if (!needs_overflow_infinity (type
))
3535 min
= TYPE_MAX_VALUE (type
);
3536 else if (supports_overflow_infinity (type
))
3537 min
= positive_overflow_infinity (type
);
3540 set_value_range_to_varying (vr
);
3544 if (is_overflow_infinity (vr0
.max
))
3545 max
= positive_overflow_infinity (type
);
3546 else if (!vrp_val_is_min (vr0
.max
))
3547 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3548 else if (!needs_overflow_infinity (type
))
3549 max
= TYPE_MAX_VALUE (type
);
3550 else if (supports_overflow_infinity (type
)
3551 /* We shouldn't generate [+INF, +INF] as set_value_range
3552 doesn't like this and ICEs. */
3553 && !is_positive_overflow_infinity (min
))
3554 max
= positive_overflow_infinity (type
);
3557 set_value_range_to_varying (vr
);
3561 cmp
= compare_values (min
, max
);
3563 /* If a VR_ANTI_RANGEs contains zero, then we have
3564 ~[-INF, min(MIN, MAX)]. */
3565 if (vr0
.type
== VR_ANTI_RANGE
)
3567 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3569 /* Take the lower of the two values. */
3573 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3574 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3575 flag_wrapv is set and the original anti-range doesn't include
3576 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3577 if (TYPE_OVERFLOW_WRAPS (type
))
3579 tree type_min_value
= TYPE_MIN_VALUE (type
);
3581 min
= (vr0
.min
!= type_min_value
3582 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3583 build_int_cst (TREE_TYPE (type_min_value
), 1))
3588 if (overflow_infinity_range_p (&vr0
))
3589 min
= negative_overflow_infinity (type
);
3591 min
= TYPE_MIN_VALUE (type
);
3596 /* All else has failed, so create the range [0, INF], even for
3597 flag_wrapv since TYPE_MIN_VALUE is in the original
3599 vr0
.type
= VR_RANGE
;
3600 min
= build_int_cst (type
, 0);
3601 if (needs_overflow_infinity (type
))
3603 if (supports_overflow_infinity (type
))
3604 max
= positive_overflow_infinity (type
);
3607 set_value_range_to_varying (vr
);
3612 max
= TYPE_MAX_VALUE (type
);
3616 /* If the range contains zero then we know that the minimum value in the
3617 range will be zero. */
3618 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3622 min
= build_int_cst (type
, 0);
3626 /* If the range was reversed, swap MIN and MAX. */
3635 cmp
= compare_values (min
, max
);
3636 if (cmp
== -2 || cmp
== 1)
3638 /* If the new range has its limits swapped around (MIN > MAX),
3639 then the operation caused one of them to wrap around, mark
3640 the new range VARYING. */
3641 set_value_range_to_varying (vr
);
3644 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3648 /* For unhandled operations fall back to varying. */
3649 set_value_range_to_varying (vr
);
3654 /* Extract range information from a unary expression CODE OP0 based on
3655 the range of its operand with resulting type TYPE.
3656 The resulting range is stored in *VR. */
3659 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3660 tree type
, tree op0
)
3662 value_range_t vr0
= VR_INITIALIZER
;
3664 /* Get value ranges for the operand. For constant operands, create
3665 a new value range with the operand to simplify processing. */
3666 if (TREE_CODE (op0
) == SSA_NAME
)
3667 vr0
= *(get_value_range (op0
));
3668 else if (is_gimple_min_invariant (op0
))
3669 set_value_range_to_value (&vr0
, op0
, NULL
);
3671 set_value_range_to_varying (&vr0
);
3673 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3677 /* Extract range information from a conditional expression STMT based on
3678 the ranges of each of its operands and the expression code. */
3681 extract_range_from_cond_expr (value_range_t
*vr
, gimple_assign stmt
)
3684 value_range_t vr0
= VR_INITIALIZER
;
3685 value_range_t vr1
= VR_INITIALIZER
;
3687 /* Get value ranges for each operand. For constant operands, create
3688 a new value range with the operand to simplify processing. */
3689 op0
= gimple_assign_rhs2 (stmt
);
3690 if (TREE_CODE (op0
) == SSA_NAME
)
3691 vr0
= *(get_value_range (op0
));
3692 else if (is_gimple_min_invariant (op0
))
3693 set_value_range_to_value (&vr0
, op0
, NULL
);
3695 set_value_range_to_varying (&vr0
);
3697 op1
= gimple_assign_rhs3 (stmt
);
3698 if (TREE_CODE (op1
) == SSA_NAME
)
3699 vr1
= *(get_value_range (op1
));
3700 else if (is_gimple_min_invariant (op1
))
3701 set_value_range_to_value (&vr1
, op1
, NULL
);
3703 set_value_range_to_varying (&vr1
);
3705 /* The resulting value range is the union of the operand ranges */
3706 copy_value_range (vr
, &vr0
);
3707 vrp_meet (vr
, &vr1
);
3711 /* Extract range information from a comparison expression EXPR based
3712 on the range of its operand and the expression code. */
3715 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3716 tree type
, tree op0
, tree op1
)
3721 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3724 /* A disadvantage of using a special infinity as an overflow
3725 representation is that we lose the ability to record overflow
3726 when we don't have an infinity. So we have to ignore a result
3727 which relies on overflow. */
3729 if (val
&& !is_overflow_infinity (val
) && !sop
)
3731 /* Since this expression was found on the RHS of an assignment,
3732 its type may be different from _Bool. Convert VAL to EXPR's
3734 val
= fold_convert (type
, val
);
3735 if (is_gimple_min_invariant (val
))
3736 set_value_range_to_value (vr
, val
, vr
->equiv
);
3738 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3741 /* The result of a comparison is always true or false. */
3742 set_value_range_to_truthvalue (vr
, type
);
3745 /* Try to derive a nonnegative or nonzero range out of STMT relying
3746 primarily on generic routines in fold in conjunction with range data.
3747 Store the result in *VR */
3750 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3753 tree type
= gimple_expr_type (stmt
);
3755 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3757 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3758 int mini
, maxi
, zerov
= 0, prec
;
3760 switch (DECL_FUNCTION_CODE (fndecl
))
3762 case BUILT_IN_CONSTANT_P
:
3763 /* If the call is __builtin_constant_p and the argument is a
3764 function parameter resolve it to false. This avoids bogus
3765 array bound warnings.
3766 ??? We could do this as early as inlining is finished. */
3767 arg
= gimple_call_arg (stmt
, 0);
3768 if (TREE_CODE (arg
) == SSA_NAME
3769 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3770 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3772 set_value_range_to_null (vr
, type
);
3776 /* Both __builtin_ffs* and __builtin_popcount return
3778 CASE_INT_FN (BUILT_IN_FFS
):
3779 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3780 arg
= gimple_call_arg (stmt
, 0);
3781 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3784 if (TREE_CODE (arg
) == SSA_NAME
)
3786 value_range_t
*vr0
= get_value_range (arg
);
3787 /* If arg is non-zero, then ffs or popcount
3789 if (((vr0
->type
== VR_RANGE
3790 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3791 || (vr0
->type
== VR_ANTI_RANGE
3792 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3793 && !is_overflow_infinity (vr0
->min
)
3794 && !is_overflow_infinity (vr0
->max
))
3796 /* If some high bits are known to be zero,
3797 we can decrease the maximum. */
3798 if (vr0
->type
== VR_RANGE
3799 && TREE_CODE (vr0
->max
) == INTEGER_CST
3800 && !operand_less_p (vr0
->min
,
3801 build_zero_cst (TREE_TYPE (vr0
->min
)))
3802 && !is_overflow_infinity (vr0
->max
))
3803 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3806 /* __builtin_parity* returns [0, 1]. */
3807 CASE_INT_FN (BUILT_IN_PARITY
):
3811 /* __builtin_c[lt]z* return [0, prec-1], except for
3812 when the argument is 0, but that is undefined behavior.
3813 On many targets where the CLZ RTL or optab value is defined
3814 for 0 the value is prec, so include that in the range
3816 CASE_INT_FN (BUILT_IN_CLZ
):
3817 arg
= gimple_call_arg (stmt
, 0);
3818 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3821 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3823 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3825 /* Handle only the single common value. */
3827 /* Magic value to give up, unless vr0 proves
3830 if (TREE_CODE (arg
) == SSA_NAME
)
3832 value_range_t
*vr0
= get_value_range (arg
);
3833 /* From clz of VR_RANGE minimum we can compute
3835 if (vr0
->type
== VR_RANGE
3836 && TREE_CODE (vr0
->min
) == INTEGER_CST
3837 && !is_overflow_infinity (vr0
->min
))
3839 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3843 else if (vr0
->type
== VR_ANTI_RANGE
3844 && integer_zerop (vr0
->min
)
3845 && !is_overflow_infinity (vr0
->min
))
3852 /* From clz of VR_RANGE maximum we can compute
3854 if (vr0
->type
== VR_RANGE
3855 && TREE_CODE (vr0
->max
) == INTEGER_CST
3856 && !is_overflow_infinity (vr0
->max
))
3858 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3866 /* __builtin_ctz* return [0, prec-1], except for
3867 when the argument is 0, but that is undefined behavior.
3868 If there is a ctz optab for this mode and
3869 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3870 otherwise just assume 0 won't be seen. */
3871 CASE_INT_FN (BUILT_IN_CTZ
):
3872 arg
= gimple_call_arg (stmt
, 0);
3873 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3876 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3878 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3881 /* Handle only the two common values. */
3884 else if (zerov
== prec
)
3887 /* Magic value to give up, unless vr0 proves
3891 if (TREE_CODE (arg
) == SSA_NAME
)
3893 value_range_t
*vr0
= get_value_range (arg
);
3894 /* If arg is non-zero, then use [0, prec - 1]. */
3895 if (((vr0
->type
== VR_RANGE
3896 && integer_nonzerop (vr0
->min
))
3897 || (vr0
->type
== VR_ANTI_RANGE
3898 && integer_zerop (vr0
->min
)))
3899 && !is_overflow_infinity (vr0
->min
))
3904 /* If some high bits are known to be zero,
3905 we can decrease the result maximum. */
3906 if (vr0
->type
== VR_RANGE
3907 && TREE_CODE (vr0
->max
) == INTEGER_CST
3908 && !is_overflow_infinity (vr0
->max
))
3910 maxi
= tree_floor_log2 (vr0
->max
);
3911 /* For vr0 [0, 0] give up. */
3919 /* __builtin_clrsb* returns [0, prec-1]. */
3920 CASE_INT_FN (BUILT_IN_CLRSB
):
3921 arg
= gimple_call_arg (stmt
, 0);
3922 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3927 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3928 build_int_cst (type
, maxi
), NULL
);
3934 else if (is_gimple_call (stmt
)
3935 && gimple_call_internal_p (stmt
))
3937 enum tree_code subcode
= ERROR_MARK
;
3938 switch (gimple_call_internal_fn (stmt
))
3940 case IFN_UBSAN_CHECK_ADD
:
3941 subcode
= PLUS_EXPR
;
3943 case IFN_UBSAN_CHECK_SUB
:
3944 subcode
= MINUS_EXPR
;
3946 case IFN_UBSAN_CHECK_MUL
:
3947 subcode
= MULT_EXPR
;
3952 if (subcode
!= ERROR_MARK
)
3954 bool saved_flag_wrapv
= flag_wrapv
;
3955 /* Pretend the arithmetics is wrapping. If there is
3956 any overflow, we'll complain, but will actually do
3957 wrapping operation. */
3959 extract_range_from_binary_expr (vr
, subcode
, type
,
3960 gimple_call_arg (stmt
, 0),
3961 gimple_call_arg (stmt
, 1));
3962 flag_wrapv
= saved_flag_wrapv
;
3964 /* If for both arguments vrp_valueize returned non-NULL,
3965 this should have been already folded and if not, it
3966 wasn't folded because of overflow. Avoid removing the
3967 UBSAN_CHECK_* calls in that case. */
3968 if (vr
->type
== VR_RANGE
3969 && (vr
->min
== vr
->max
3970 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3971 set_value_range_to_varying (vr
);
3975 if (INTEGRAL_TYPE_P (type
)
3976 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3977 set_value_range_to_nonnegative (vr
, type
,
3978 sop
|| stmt_overflow_infinity (stmt
));
3979 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3981 set_value_range_to_nonnull (vr
, type
);
3983 set_value_range_to_varying (vr
);
3987 /* Try to compute a useful range out of assignment STMT and store it
3991 extract_range_from_assignment (value_range_t
*vr
, gimple_assign stmt
)
3993 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3995 if (code
== ASSERT_EXPR
)
3996 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3997 else if (code
== SSA_NAME
)
3998 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3999 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4000 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4001 gimple_expr_type (stmt
),
4002 gimple_assign_rhs1 (stmt
),
4003 gimple_assign_rhs2 (stmt
));
4004 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4005 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4006 gimple_expr_type (stmt
),
4007 gimple_assign_rhs1 (stmt
));
4008 else if (code
== COND_EXPR
)
4009 extract_range_from_cond_expr (vr
, stmt
);
4010 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4011 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4012 gimple_expr_type (stmt
),
4013 gimple_assign_rhs1 (stmt
),
4014 gimple_assign_rhs2 (stmt
));
4015 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4016 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4017 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4019 set_value_range_to_varying (vr
);
4021 if (vr
->type
== VR_VARYING
)
4022 extract_range_basic (vr
, stmt
);
4025 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4026 would be profitable to adjust VR using scalar evolution information
4027 for VAR. If so, update VR with the new limits. */
4030 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
4031 gimple stmt
, tree var
)
4033 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4034 enum ev_direction dir
;
4036 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4037 better opportunities than a regular range, but I'm not sure. */
4038 if (vr
->type
== VR_ANTI_RANGE
)
4041 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4043 /* Like in PR19590, scev can return a constant function. */
4044 if (is_gimple_min_invariant (chrec
))
4046 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4050 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4053 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4054 tem
= op_with_constant_singleton_value_range (init
);
4057 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4058 tem
= op_with_constant_singleton_value_range (step
);
4062 /* If STEP is symbolic, we can't know whether INIT will be the
4063 minimum or maximum value in the range. Also, unless INIT is
4064 a simple expression, compare_values and possibly other functions
4065 in tree-vrp won't be able to handle it. */
4066 if (step
== NULL_TREE
4067 || !is_gimple_min_invariant (step
)
4068 || !valid_value_p (init
))
4071 dir
= scev_direction (chrec
);
4072 if (/* Do not adjust ranges if we do not know whether the iv increases
4073 or decreases, ... */
4074 dir
== EV_DIR_UNKNOWN
4075 /* ... or if it may wrap. */
4076 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4080 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4081 negative_overflow_infinity and positive_overflow_infinity,
4082 because we have concluded that the loop probably does not
4085 type
= TREE_TYPE (var
);
4086 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4087 tmin
= lower_bound_in_type (type
, type
);
4089 tmin
= TYPE_MIN_VALUE (type
);
4090 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4091 tmax
= upper_bound_in_type (type
, type
);
4093 tmax
= TYPE_MAX_VALUE (type
);
4095 /* Try to use estimated number of iterations for the loop to constrain the
4096 final value in the evolution. */
4097 if (TREE_CODE (step
) == INTEGER_CST
4098 && is_gimple_val (init
)
4099 && (TREE_CODE (init
) != SSA_NAME
4100 || get_value_range (init
)->type
== VR_RANGE
))
4104 /* We are only entering here for loop header PHI nodes, so using
4105 the number of latch executions is the correct thing to use. */
4106 if (max_loop_iterations (loop
, &nit
))
4108 value_range_t maxvr
= VR_INITIALIZER
;
4109 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4112 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4114 /* If the multiplication overflowed we can't do a meaningful
4115 adjustment. Likewise if the result doesn't fit in the type
4116 of the induction variable. For a signed type we have to
4117 check whether the result has the expected signedness which
4118 is that of the step as number of iterations is unsigned. */
4120 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4122 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4124 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4125 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4126 TREE_TYPE (init
), init
, tem
);
4127 /* Likewise if the addition did. */
4128 if (maxvr
.type
== VR_RANGE
)
4137 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4142 /* For VARYING or UNDEFINED ranges, just about anything we get
4143 from scalar evolutions should be better. */
4145 if (dir
== EV_DIR_DECREASES
)
4150 else if (vr
->type
== VR_RANGE
)
4155 if (dir
== EV_DIR_DECREASES
)
4157 /* INIT is the maximum value. If INIT is lower than VR->MAX
4158 but no smaller than VR->MIN, set VR->MAX to INIT. */
4159 if (compare_values (init
, max
) == -1)
4162 /* According to the loop information, the variable does not
4163 overflow. If we think it does, probably because of an
4164 overflow due to arithmetic on a different INF value,
4166 if (is_negative_overflow_infinity (min
)
4167 || compare_values (min
, tmin
) == -1)
4173 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4174 if (compare_values (init
, min
) == 1)
4177 if (is_positive_overflow_infinity (max
)
4178 || compare_values (tmax
, max
) == -1)
4185 /* If we just created an invalid range with the minimum
4186 greater than the maximum, we fail conservatively.
4187 This should happen only in unreachable
4188 parts of code, or for invalid programs. */
4189 if (compare_values (min
, max
) == 1
4190 || (is_negative_overflow_infinity (min
)
4191 && is_positive_overflow_infinity (max
)))
4194 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4198 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4200 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4201 all the values in the ranges.
4203 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4205 - Return NULL_TREE if it is not always possible to determine the
4206 value of the comparison.
4208 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4209 overflow infinity was used in the test. */
4213 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4214 bool *strict_overflow_p
)
4216 /* VARYING or UNDEFINED ranges cannot be compared. */
4217 if (vr0
->type
== VR_VARYING
4218 || vr0
->type
== VR_UNDEFINED
4219 || vr1
->type
== VR_VARYING
4220 || vr1
->type
== VR_UNDEFINED
)
4223 /* Anti-ranges need to be handled separately. */
4224 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4226 /* If both are anti-ranges, then we cannot compute any
4228 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4231 /* These comparisons are never statically computable. */
4238 /* Equality can be computed only between a range and an
4239 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4240 if (vr0
->type
== VR_RANGE
)
4242 /* To simplify processing, make VR0 the anti-range. */
4243 value_range_t
*tmp
= vr0
;
4248 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4250 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4251 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4252 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4257 if (!usable_range_p (vr0
, strict_overflow_p
)
4258 || !usable_range_p (vr1
, strict_overflow_p
))
4261 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4262 operands around and change the comparison code. */
4263 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4266 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4272 if (comp
== EQ_EXPR
)
4274 /* Equality may only be computed if both ranges represent
4275 exactly one value. */
4276 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4277 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4279 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4281 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4283 if (cmp_min
== 0 && cmp_max
== 0)
4284 return boolean_true_node
;
4285 else if (cmp_min
!= -2 && cmp_max
!= -2)
4286 return boolean_false_node
;
4288 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4289 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4290 strict_overflow_p
) == 1
4291 || compare_values_warnv (vr1
->min
, vr0
->max
,
4292 strict_overflow_p
) == 1)
4293 return boolean_false_node
;
4297 else if (comp
== NE_EXPR
)
4301 /* If VR0 is completely to the left or completely to the right
4302 of VR1, they are always different. Notice that we need to
4303 make sure that both comparisons yield similar results to
4304 avoid comparing values that cannot be compared at
4306 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4307 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4308 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4309 return boolean_true_node
;
4311 /* If VR0 and VR1 represent a single value and are identical,
4313 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4314 strict_overflow_p
) == 0
4315 && compare_values_warnv (vr1
->min
, vr1
->max
,
4316 strict_overflow_p
) == 0
4317 && compare_values_warnv (vr0
->min
, vr1
->min
,
4318 strict_overflow_p
) == 0
4319 && compare_values_warnv (vr0
->max
, vr1
->max
,
4320 strict_overflow_p
) == 0)
4321 return boolean_false_node
;
4323 /* Otherwise, they may or may not be different. */
4327 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4331 /* If VR0 is to the left of VR1, return true. */
4332 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4333 if ((comp
== LT_EXPR
&& tst
== -1)
4334 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4336 if (overflow_infinity_range_p (vr0
)
4337 || overflow_infinity_range_p (vr1
))
4338 *strict_overflow_p
= true;
4339 return boolean_true_node
;
4342 /* If VR0 is to the right of VR1, return false. */
4343 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4344 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4345 || (comp
== LE_EXPR
&& tst
== 1))
4347 if (overflow_infinity_range_p (vr0
)
4348 || overflow_infinity_range_p (vr1
))
4349 *strict_overflow_p
= true;
4350 return boolean_false_node
;
4353 /* Otherwise, we don't know. */
4361 /* Given a value range VR, a value VAL and a comparison code COMP, return
4362 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4363 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4364 always returns false. Return NULL_TREE if it is not always
4365 possible to determine the value of the comparison. Also set
4366 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4367 infinity was used in the test. */
4370 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4371 bool *strict_overflow_p
)
4373 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4376 /* Anti-ranges need to be handled separately. */
4377 if (vr
->type
== VR_ANTI_RANGE
)
4379 /* For anti-ranges, the only predicates that we can compute at
4380 compile time are equality and inequality. */
4387 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4388 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4389 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4394 if (!usable_range_p (vr
, strict_overflow_p
))
4397 if (comp
== EQ_EXPR
)
4399 /* EQ_EXPR may only be computed if VR represents exactly
4401 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4403 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4405 return boolean_true_node
;
4406 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4407 return boolean_false_node
;
4409 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4410 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4411 return boolean_false_node
;
4415 else if (comp
== NE_EXPR
)
4417 /* If VAL is not inside VR, then they are always different. */
4418 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4419 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4420 return boolean_true_node
;
4422 /* If VR represents exactly one value equal to VAL, then return
4424 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4425 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4426 return boolean_false_node
;
4428 /* Otherwise, they may or may not be different. */
4431 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4435 /* If VR is to the left of VAL, return true. */
4436 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4437 if ((comp
== LT_EXPR
&& tst
== -1)
4438 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4440 if (overflow_infinity_range_p (vr
))
4441 *strict_overflow_p
= true;
4442 return boolean_true_node
;
4445 /* If VR is to the right of VAL, return false. */
4446 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4447 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4448 || (comp
== LE_EXPR
&& tst
== 1))
4450 if (overflow_infinity_range_p (vr
))
4451 *strict_overflow_p
= true;
4452 return boolean_false_node
;
4455 /* Otherwise, we don't know. */
4458 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4462 /* If VR is to the right of VAL, return true. */
4463 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4464 if ((comp
== GT_EXPR
&& tst
== 1)
4465 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4467 if (overflow_infinity_range_p (vr
))
4468 *strict_overflow_p
= true;
4469 return boolean_true_node
;
4472 /* If VR is to the left of VAL, return false. */
4473 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4474 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4475 || (comp
== GE_EXPR
&& tst
== -1))
4477 if (overflow_infinity_range_p (vr
))
4478 *strict_overflow_p
= true;
4479 return boolean_false_node
;
4482 /* Otherwise, we don't know. */
4490 /* Debugging dumps. */
4492 void dump_value_range (FILE *, value_range_t
*);
4493 void debug_value_range (value_range_t
*);
4494 void dump_all_value_ranges (FILE *);
4495 void debug_all_value_ranges (void);
4496 void dump_vr_equiv (FILE *, bitmap
);
4497 void debug_vr_equiv (bitmap
);
4500 /* Dump value range VR to FILE. */
4503 dump_value_range (FILE *file
, value_range_t
*vr
)
4506 fprintf (file
, "[]");
4507 else if (vr
->type
== VR_UNDEFINED
)
4508 fprintf (file
, "UNDEFINED");
4509 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4511 tree type
= TREE_TYPE (vr
->min
);
4513 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4515 if (is_negative_overflow_infinity (vr
->min
))
4516 fprintf (file
, "-INF(OVF)");
4517 else if (INTEGRAL_TYPE_P (type
)
4518 && !TYPE_UNSIGNED (type
)
4519 && vrp_val_is_min (vr
->min
))
4520 fprintf (file
, "-INF");
4522 print_generic_expr (file
, vr
->min
, 0);
4524 fprintf (file
, ", ");
4526 if (is_positive_overflow_infinity (vr
->max
))
4527 fprintf (file
, "+INF(OVF)");
4528 else if (INTEGRAL_TYPE_P (type
)
4529 && vrp_val_is_max (vr
->max
))
4530 fprintf (file
, "+INF");
4532 print_generic_expr (file
, vr
->max
, 0);
4534 fprintf (file
, "]");
4541 fprintf (file
, " EQUIVALENCES: { ");
4543 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4545 print_generic_expr (file
, ssa_name (i
), 0);
4546 fprintf (file
, " ");
4550 fprintf (file
, "} (%u elements)", c
);
4553 else if (vr
->type
== VR_VARYING
)
4554 fprintf (file
, "VARYING");
4556 fprintf (file
, "INVALID RANGE");
4560 /* Dump value range VR to stderr. */
4563 debug_value_range (value_range_t
*vr
)
4565 dump_value_range (stderr
, vr
);
4566 fprintf (stderr
, "\n");
4570 /* Dump value ranges of all SSA_NAMEs to FILE. */
4573 dump_all_value_ranges (FILE *file
)
4577 for (i
= 0; i
< num_vr_values
; i
++)
4581 print_generic_expr (file
, ssa_name (i
), 0);
4582 fprintf (file
, ": ");
4583 dump_value_range (file
, vr_value
[i
]);
4584 fprintf (file
, "\n");
4588 fprintf (file
, "\n");
4592 /* Dump all value ranges to stderr. */
4595 debug_all_value_ranges (void)
4597 dump_all_value_ranges (stderr
);
4601 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4602 create a new SSA name N and return the assertion assignment
4603 'N = ASSERT_EXPR <V, V OP W>'. */
4606 build_assert_expr_for (tree cond
, tree v
)
4609 gimple_assign assertion
;
4611 gcc_assert (TREE_CODE (v
) == SSA_NAME
4612 && COMPARISON_CLASS_P (cond
));
4614 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4615 assertion
= gimple_build_assign (NULL_TREE
, a
);
4617 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4618 operand of the ASSERT_EXPR. Create it so the new name and the old one
4619 are registered in the replacement table so that we can fix the SSA web
4620 after adding all the ASSERT_EXPRs. */
4621 create_new_def_for (v
, assertion
, NULL
);
4627 /* Return false if EXPR is a predicate expression involving floating
4631 fp_predicate (gimple stmt
)
4633 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4635 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4638 /* If the range of values taken by OP can be inferred after STMT executes,
4639 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4640 describes the inferred range. Return true if a range could be
4644 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4647 *comp_code_p
= ERROR_MARK
;
4649 /* Do not attempt to infer anything in names that flow through
4651 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4654 /* Similarly, don't infer anything from statements that may throw
4655 exceptions. ??? Relax this requirement? */
4656 if (stmt_could_throw_p (stmt
))
4659 /* If STMT is the last statement of a basic block with no normal
4660 successors, there is no point inferring anything about any of its
4661 operands. We would not be able to find a proper insertion point
4662 for the assertion, anyway. */
4663 if (stmt_ends_bb_p (stmt
))
4668 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4669 if (!(e
->flags
& EDGE_ABNORMAL
))
4675 if (infer_nonnull_range (stmt
, op
, true, true))
4677 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4678 *comp_code_p
= NE_EXPR
;
4686 void dump_asserts_for (FILE *, tree
);
4687 void debug_asserts_for (tree
);
4688 void dump_all_asserts (FILE *);
4689 void debug_all_asserts (void);
4691 /* Dump all the registered assertions for NAME to FILE. */
4694 dump_asserts_for (FILE *file
, tree name
)
4698 fprintf (file
, "Assertions to be inserted for ");
4699 print_generic_expr (file
, name
, 0);
4700 fprintf (file
, "\n");
4702 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4705 fprintf (file
, "\t");
4706 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4707 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4710 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4711 loc
->e
->dest
->index
);
4712 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4714 fprintf (file
, "\n\tPREDICATE: ");
4715 print_generic_expr (file
, name
, 0);
4716 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4717 print_generic_expr (file
, loc
->val
, 0);
4718 fprintf (file
, "\n\n");
4722 fprintf (file
, "\n");
4726 /* Dump all the registered assertions for NAME to stderr. */
4729 debug_asserts_for (tree name
)
4731 dump_asserts_for (stderr
, name
);
4735 /* Dump all the registered assertions for all the names to FILE. */
4738 dump_all_asserts (FILE *file
)
4743 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4744 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4745 dump_asserts_for (file
, ssa_name (i
));
4746 fprintf (file
, "\n");
4750 /* Dump all the registered assertions for all the names to stderr. */
4753 debug_all_asserts (void)
4755 dump_all_asserts (stderr
);
4759 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4760 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4761 E->DEST, then register this location as a possible insertion point
4762 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4764 BB, E and SI provide the exact insertion point for the new
4765 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4766 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4767 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4768 must not be NULL. */
4771 register_new_assert_for (tree name
, tree expr
,
4772 enum tree_code comp_code
,
4776 gimple_stmt_iterator si
)
4778 assert_locus_t n
, loc
, last_loc
;
4779 basic_block dest_bb
;
4781 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4784 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4785 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4787 /* Never build an assert comparing against an integer constant with
4788 TREE_OVERFLOW set. This confuses our undefined overflow warning
4790 if (TREE_OVERFLOW_P (val
))
4791 val
= drop_tree_overflow (val
);
4793 /* The new assertion A will be inserted at BB or E. We need to
4794 determine if the new location is dominated by a previously
4795 registered location for A. If we are doing an edge insertion,
4796 assume that A will be inserted at E->DEST. Note that this is not
4799 If E is a critical edge, it will be split. But even if E is
4800 split, the new block will dominate the same set of blocks that
4803 The reverse, however, is not true, blocks dominated by E->DEST
4804 will not be dominated by the new block created to split E. So,
4805 if the insertion location is on a critical edge, we will not use
4806 the new location to move another assertion previously registered
4807 at a block dominated by E->DEST. */
4808 dest_bb
= (bb
) ? bb
: e
->dest
;
4810 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4811 VAL at a block dominating DEST_BB, then we don't need to insert a new
4812 one. Similarly, if the same assertion already exists at a block
4813 dominated by DEST_BB and the new location is not on a critical
4814 edge, then update the existing location for the assertion (i.e.,
4815 move the assertion up in the dominance tree).
4817 Note, this is implemented as a simple linked list because there
4818 should not be more than a handful of assertions registered per
4819 name. If this becomes a performance problem, a table hashed by
4820 COMP_CODE and VAL could be implemented. */
4821 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4825 if (loc
->comp_code
== comp_code
4827 || operand_equal_p (loc
->val
, val
, 0))
4828 && (loc
->expr
== expr
4829 || operand_equal_p (loc
->expr
, expr
, 0)))
4831 /* If E is not a critical edge and DEST_BB
4832 dominates the existing location for the assertion, move
4833 the assertion up in the dominance tree by updating its
4834 location information. */
4835 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4836 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4845 /* Update the last node of the list and move to the next one. */
4850 /* If we didn't find an assertion already registered for
4851 NAME COMP_CODE VAL, add a new one at the end of the list of
4852 assertions associated with NAME. */
4853 n
= XNEW (struct assert_locus_d
);
4857 n
->comp_code
= comp_code
;
4865 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4867 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4870 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4871 Extract a suitable test code and value and store them into *CODE_P and
4872 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4874 If no extraction was possible, return FALSE, otherwise return TRUE.
4876 If INVERT is true, then we invert the result stored into *CODE_P. */
4879 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4880 tree cond_op0
, tree cond_op1
,
4881 bool invert
, enum tree_code
*code_p
,
4884 enum tree_code comp_code
;
4887 /* Otherwise, we have a comparison of the form NAME COMP VAL
4888 or VAL COMP NAME. */
4889 if (name
== cond_op1
)
4891 /* If the predicate is of the form VAL COMP NAME, flip
4892 COMP around because we need to register NAME as the
4893 first operand in the predicate. */
4894 comp_code
= swap_tree_comparison (cond_code
);
4899 /* The comparison is of the form NAME COMP VAL, so the
4900 comparison code remains unchanged. */
4901 comp_code
= cond_code
;
4905 /* Invert the comparison code as necessary. */
4907 comp_code
= invert_tree_comparison (comp_code
, 0);
4909 /* VRP does not handle float types. */
4910 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4913 /* Do not register always-false predicates.
4914 FIXME: this works around a limitation in fold() when dealing with
4915 enumerations. Given 'enum { N1, N2 } x;', fold will not
4916 fold 'if (x > N2)' to 'if (0)'. */
4917 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4918 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4920 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4921 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4923 if (comp_code
== GT_EXPR
4925 || compare_values (val
, max
) == 0))
4928 if (comp_code
== LT_EXPR
4930 || compare_values (val
, min
) == 0))
4933 *code_p
= comp_code
;
4938 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4939 (otherwise return VAL). VAL and MASK must be zero-extended for
4940 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4941 (to transform signed values into unsigned) and at the end xor
4945 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4946 const wide_int
&sgnbit
, unsigned int prec
)
4948 wide_int bit
= wi::one (prec
), res
;
4951 wide_int val
= val_in
^ sgnbit
;
4952 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4955 if ((res
& bit
) == 0)
4958 res
= (val
+ bit
).and_not (res
);
4960 if (wi::gtu_p (res
, val
))
4961 return res
^ sgnbit
;
4963 return val
^ sgnbit
;
4966 /* Try to register an edge assertion for SSA name NAME on edge E for
4967 the condition COND contributing to the conditional jump pointed to by BSI.
4968 Invert the condition COND if INVERT is true.
4969 Return true if an assertion for NAME could be registered. */
4972 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4973 enum tree_code cond_code
,
4974 tree cond_op0
, tree cond_op1
, bool invert
)
4977 enum tree_code comp_code
;
4978 bool retval
= false;
4980 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4983 invert
, &comp_code
, &val
))
4986 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4987 reachable from E. */
4988 if (live_on_edge (e
, name
)
4989 && !has_single_use (name
))
4991 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
4995 /* In the case of NAME <= CST and NAME being defined as
4996 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
4997 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
4998 This catches range and anti-range tests. */
4999 if ((comp_code
== LE_EXPR
5000 || comp_code
== GT_EXPR
)
5001 && TREE_CODE (val
) == INTEGER_CST
5002 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5004 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5005 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5007 /* Extract CST2 from the (optional) addition. */
5008 if (is_gimple_assign (def_stmt
)
5009 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5011 name2
= gimple_assign_rhs1 (def_stmt
);
5012 cst2
= gimple_assign_rhs2 (def_stmt
);
5013 if (TREE_CODE (name2
) == SSA_NAME
5014 && TREE_CODE (cst2
) == INTEGER_CST
)
5015 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5018 /* Extract NAME2 from the (optional) sign-changing cast. */
5019 if (gimple_assign_cast_p (def_stmt
))
5021 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5022 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5023 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5024 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5025 name3
= gimple_assign_rhs1 (def_stmt
);
5028 /* If name3 is used later, create an ASSERT_EXPR for it. */
5029 if (name3
!= NULL_TREE
5030 && TREE_CODE (name3
) == SSA_NAME
5031 && (cst2
== NULL_TREE
5032 || TREE_CODE (cst2
) == INTEGER_CST
)
5033 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5034 && live_on_edge (e
, name3
)
5035 && !has_single_use (name3
))
5039 /* Build an expression for the range test. */
5040 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5041 if (cst2
!= NULL_TREE
)
5042 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5046 fprintf (dump_file
, "Adding assert for ");
5047 print_generic_expr (dump_file
, name3
, 0);
5048 fprintf (dump_file
, " from ");
5049 print_generic_expr (dump_file
, tmp
, 0);
5050 fprintf (dump_file
, "\n");
5053 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5058 /* If name2 is used later, create an ASSERT_EXPR for it. */
5059 if (name2
!= NULL_TREE
5060 && TREE_CODE (name2
) == SSA_NAME
5061 && TREE_CODE (cst2
) == INTEGER_CST
5062 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5063 && live_on_edge (e
, name2
)
5064 && !has_single_use (name2
))
5068 /* Build an expression for the range test. */
5070 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5071 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5072 if (cst2
!= NULL_TREE
)
5073 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5077 fprintf (dump_file
, "Adding assert for ");
5078 print_generic_expr (dump_file
, name2
, 0);
5079 fprintf (dump_file
, " from ");
5080 print_generic_expr (dump_file
, tmp
, 0);
5081 fprintf (dump_file
, "\n");
5084 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5090 /* In the case of post-in/decrement tests like if (i++) ... and uses
5091 of the in/decremented value on the edge the extra name we want to
5092 assert for is not on the def chain of the name compared. Instead
5093 it is in the set of use stmts. */
5094 if ((comp_code
== NE_EXPR
5095 || comp_code
== EQ_EXPR
)
5096 && TREE_CODE (val
) == INTEGER_CST
)
5098 imm_use_iterator ui
;
5100 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5102 /* Cut off to use-stmts that are in the predecessor. */
5103 if (gimple_bb (use_stmt
) != e
->src
)
5106 if (!is_gimple_assign (use_stmt
))
5109 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5110 if (code
!= PLUS_EXPR
5111 && code
!= MINUS_EXPR
)
5114 tree cst
= gimple_assign_rhs2 (use_stmt
);
5115 if (TREE_CODE (cst
) != INTEGER_CST
)
5118 tree name2
= gimple_assign_lhs (use_stmt
);
5119 if (live_on_edge (e
, name2
))
5121 cst
= int_const_binop (code
, val
, cst
);
5122 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5129 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5130 && TREE_CODE (val
) == INTEGER_CST
)
5132 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5133 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5134 tree val2
= NULL_TREE
;
5135 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5136 wide_int mask
= wi::zero (prec
);
5137 unsigned int nprec
= prec
;
5138 enum tree_code rhs_code
= ERROR_MARK
;
5140 if (is_gimple_assign (def_stmt
))
5141 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5143 /* Add asserts for NAME cmp CST and NAME being defined
5144 as NAME = (int) NAME2. */
5145 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5146 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5147 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5148 && gimple_assign_cast_p (def_stmt
))
5150 name2
= gimple_assign_rhs1 (def_stmt
);
5151 if (CONVERT_EXPR_CODE_P (rhs_code
)
5152 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5153 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5154 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5155 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5156 || !tree_int_cst_equal (val
,
5157 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5158 && live_on_edge (e
, name2
)
5159 && !has_single_use (name2
))
5162 enum tree_code new_comp_code
= comp_code
;
5164 cst
= fold_convert (TREE_TYPE (name2
),
5165 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5166 /* Build an expression for the range test. */
5167 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5168 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5169 fold_convert (TREE_TYPE (name2
), val
));
5170 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5172 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5173 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5174 build_int_cst (TREE_TYPE (name2
), 1));
5179 fprintf (dump_file
, "Adding assert for ");
5180 print_generic_expr (dump_file
, name2
, 0);
5181 fprintf (dump_file
, " from ");
5182 print_generic_expr (dump_file
, tmp
, 0);
5183 fprintf (dump_file
, "\n");
5186 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5193 /* Add asserts for NAME cmp CST and NAME being defined as
5194 NAME = NAME2 >> CST2.
5196 Extract CST2 from the right shift. */
5197 if (rhs_code
== RSHIFT_EXPR
)
5199 name2
= gimple_assign_rhs1 (def_stmt
);
5200 cst2
= gimple_assign_rhs2 (def_stmt
);
5201 if (TREE_CODE (name2
) == SSA_NAME
5202 && tree_fits_uhwi_p (cst2
)
5203 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5204 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5205 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5206 && live_on_edge (e
, name2
)
5207 && !has_single_use (name2
))
5209 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5210 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5213 if (val2
!= NULL_TREE
5214 && TREE_CODE (val2
) == INTEGER_CST
5215 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5219 enum tree_code new_comp_code
= comp_code
;
5223 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5225 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5227 tree type
= build_nonstandard_integer_type (prec
, 1);
5228 tmp
= build1 (NOP_EXPR
, type
, name2
);
5229 val2
= fold_convert (type
, val2
);
5231 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5232 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5233 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5235 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5238 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5240 if (minval
== new_val
)
5241 new_val
= NULL_TREE
;
5246 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5249 new_val
= NULL_TREE
;
5251 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5258 fprintf (dump_file
, "Adding assert for ");
5259 print_generic_expr (dump_file
, name2
, 0);
5260 fprintf (dump_file
, " from ");
5261 print_generic_expr (dump_file
, tmp
, 0);
5262 fprintf (dump_file
, "\n");
5265 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5271 /* Add asserts for NAME cmp CST and NAME being defined as
5272 NAME = NAME2 & CST2.
5274 Extract CST2 from the and.
5277 NAME = (unsigned) NAME2;
5278 casts where NAME's type is unsigned and has smaller precision
5279 than NAME2's type as if it was NAME = NAME2 & MASK. */
5280 names
[0] = NULL_TREE
;
5281 names
[1] = NULL_TREE
;
5283 if (rhs_code
== BIT_AND_EXPR
5284 || (CONVERT_EXPR_CODE_P (rhs_code
)
5285 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5286 && TYPE_UNSIGNED (TREE_TYPE (val
))
5287 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5291 name2
= gimple_assign_rhs1 (def_stmt
);
5292 if (rhs_code
== BIT_AND_EXPR
)
5293 cst2
= gimple_assign_rhs2 (def_stmt
);
5296 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5297 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5299 if (TREE_CODE (name2
) == SSA_NAME
5300 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5301 && TREE_CODE (cst2
) == INTEGER_CST
5302 && !integer_zerop (cst2
)
5304 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5306 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5307 if (gimple_assign_cast_p (def_stmt2
))
5309 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5310 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5311 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5312 || (TYPE_PRECISION (TREE_TYPE (name2
))
5313 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5314 || !live_on_edge (e
, names
[1])
5315 || has_single_use (names
[1]))
5316 names
[1] = NULL_TREE
;
5318 if (live_on_edge (e
, name2
)
5319 && !has_single_use (name2
))
5323 if (names
[0] || names
[1])
5325 wide_int minv
, maxv
, valv
, cst2v
;
5326 wide_int tem
, sgnbit
;
5327 bool valid_p
= false, valn
, cst2n
;
5328 enum tree_code ccode
= comp_code
;
5330 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5331 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5332 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5333 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5334 /* If CST2 doesn't have most significant bit set,
5335 but VAL is negative, we have comparison like
5336 if ((x & 0x123) > -4) (always true). Just give up. */
5340 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5342 sgnbit
= wi::zero (nprec
);
5343 minv
= valv
& cst2v
;
5347 /* Minimum unsigned value for equality is VAL & CST2
5348 (should be equal to VAL, otherwise we probably should
5349 have folded the comparison into false) and
5350 maximum unsigned value is VAL | ~CST2. */
5351 maxv
= valv
| ~cst2v
;
5356 tem
= valv
| ~cst2v
;
5357 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5361 sgnbit
= wi::zero (nprec
);
5364 /* If (VAL | ~CST2) is all ones, handle it as
5365 (X & CST2) < VAL. */
5370 sgnbit
= wi::zero (nprec
);
5373 if (!cst2n
&& wi::neg_p (cst2v
))
5374 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5383 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5389 sgnbit
= wi::zero (nprec
);
5394 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5395 is VAL and maximum unsigned value is ~0. For signed
5396 comparison, if CST2 doesn't have most significant bit
5397 set, handle it similarly. If CST2 has MSB set,
5398 the minimum is the same, and maximum is ~0U/2. */
5401 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5403 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5407 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5413 /* Find out smallest MINV where MINV > VAL
5414 && (MINV & CST2) == MINV, if any. If VAL is signed and
5415 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5416 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5419 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5424 /* Minimum unsigned value for <= is 0 and maximum
5425 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5426 Otherwise, find smallest VAL2 where VAL2 > VAL
5427 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5429 For signed comparison, if CST2 doesn't have most
5430 significant bit set, handle it similarly. If CST2 has
5431 MSB set, the maximum is the same and minimum is INT_MIN. */
5436 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5448 /* Minimum unsigned value for < is 0 and maximum
5449 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5450 Otherwise, find smallest VAL2 where VAL2 > VAL
5451 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5453 For signed comparison, if CST2 doesn't have most
5454 significant bit set, handle it similarly. If CST2 has
5455 MSB set, the maximum is the same and minimum is INT_MIN. */
5464 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5478 && (maxv
- minv
) != -1)
5480 tree tmp
, new_val
, type
;
5483 for (i
= 0; i
< 2; i
++)
5486 wide_int maxv2
= maxv
;
5488 type
= TREE_TYPE (names
[i
]);
5489 if (!TYPE_UNSIGNED (type
))
5491 type
= build_nonstandard_integer_type (nprec
, 1);
5492 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5496 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5497 wide_int_to_tree (type
, -minv
));
5498 maxv2
= maxv
- minv
;
5500 new_val
= wide_int_to_tree (type
, maxv2
);
5504 fprintf (dump_file
, "Adding assert for ");
5505 print_generic_expr (dump_file
, names
[i
], 0);
5506 fprintf (dump_file
, " from ");
5507 print_generic_expr (dump_file
, tmp
, 0);
5508 fprintf (dump_file
, "\n");
5511 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5512 new_val
, NULL
, e
, bsi
);
5522 /* OP is an operand of a truth value expression which is known to have
5523 a particular value. Register any asserts for OP and for any
5524 operands in OP's defining statement.
5526 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5527 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5530 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5531 edge e
, gimple_stmt_iterator bsi
)
5533 bool retval
= false;
5536 enum tree_code rhs_code
;
5538 /* We only care about SSA_NAMEs. */
5539 if (TREE_CODE (op
) != SSA_NAME
)
5542 /* We know that OP will have a zero or nonzero value. If OP is used
5543 more than once go ahead and register an assert for OP. */
5544 if (live_on_edge (e
, op
)
5545 && !has_single_use (op
))
5547 val
= build_int_cst (TREE_TYPE (op
), 0);
5548 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5552 /* Now look at how OP is set. If it's set from a comparison,
5553 a truth operation or some bit operations, then we may be able
5554 to register information about the operands of that assignment. */
5555 op_def
= SSA_NAME_DEF_STMT (op
);
5556 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5559 rhs_code
= gimple_assign_rhs_code (op_def
);
5561 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5563 bool invert
= (code
== EQ_EXPR
? true : false);
5564 tree op0
= gimple_assign_rhs1 (op_def
);
5565 tree op1
= gimple_assign_rhs2 (op_def
);
5567 if (TREE_CODE (op0
) == SSA_NAME
)
5568 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5570 if (TREE_CODE (op1
) == SSA_NAME
)
5571 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5574 else if ((code
== NE_EXPR
5575 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5577 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5579 /* Recurse on each operand. */
5580 tree op0
= gimple_assign_rhs1 (op_def
);
5581 tree op1
= gimple_assign_rhs2 (op_def
);
5582 if (TREE_CODE (op0
) == SSA_NAME
5583 && has_single_use (op0
))
5584 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5585 if (TREE_CODE (op1
) == SSA_NAME
5586 && has_single_use (op1
))
5587 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5589 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5590 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5592 /* Recurse, flipping CODE. */
5593 code
= invert_tree_comparison (code
, false);
5594 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5597 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5599 /* Recurse through the copy. */
5600 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5603 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5605 /* Recurse through the type conversion, unless it is a narrowing
5606 conversion or conversion from non-integral type. */
5607 tree rhs
= gimple_assign_rhs1 (op_def
);
5608 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5609 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5610 <= TYPE_PRECISION (TREE_TYPE (op
))))
5611 retval
|= register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5617 /* Try to register an edge assertion for SSA name NAME on edge E for
5618 the condition COND contributing to the conditional jump pointed to by SI.
5619 Return true if an assertion for NAME could be registered. */
5622 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5623 enum tree_code cond_code
, tree cond_op0
,
5627 enum tree_code comp_code
;
5628 bool retval
= false;
5629 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5631 /* Do not attempt to infer anything in names that flow through
5633 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5636 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5642 /* Register ASSERT_EXPRs for name. */
5643 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5644 cond_op1
, is_else_edge
);
5647 /* If COND is effectively an equality test of an SSA_NAME against
5648 the value zero or one, then we may be able to assert values
5649 for SSA_NAMEs which flow into COND. */
5651 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5652 statement of NAME we can assert both operands of the BIT_AND_EXPR
5653 have nonzero value. */
5654 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5655 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5657 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5659 if (is_gimple_assign (def_stmt
)
5660 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5662 tree op0
= gimple_assign_rhs1 (def_stmt
);
5663 tree op1
= gimple_assign_rhs2 (def_stmt
);
5664 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5665 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5669 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5670 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5672 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5673 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5675 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5677 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5678 necessarily zero value, or if type-precision is one. */
5679 if (is_gimple_assign (def_stmt
)
5680 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5681 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5682 || comp_code
== EQ_EXPR
)))
5684 tree op0
= gimple_assign_rhs1 (def_stmt
);
5685 tree op1
= gimple_assign_rhs2 (def_stmt
);
5686 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5687 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5695 /* Determine whether the outgoing edges of BB should receive an
5696 ASSERT_EXPR for each of the operands of BB's LAST statement.
5697 The last statement of BB must be a COND_EXPR.
5699 If any of the sub-graphs rooted at BB have an interesting use of
5700 the predicate operands, an assert location node is added to the
5701 list of assertions for the corresponding operands. */
5704 find_conditional_asserts (basic_block bb
, gimple_cond last
)
5707 gimple_stmt_iterator bsi
;
5713 need_assert
= false;
5714 bsi
= gsi_for_stmt (last
);
5716 /* Look for uses of the operands in each of the sub-graphs
5717 rooted at BB. We need to check each of the outgoing edges
5718 separately, so that we know what kind of ASSERT_EXPR to
5720 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5725 /* Register the necessary assertions for each operand in the
5726 conditional predicate. */
5727 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5729 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5730 gimple_cond_code (last
),
5731 gimple_cond_lhs (last
),
5732 gimple_cond_rhs (last
));
5745 /* Compare two case labels sorting first by the destination bb index
5746 and then by the case value. */
5749 compare_case_labels (const void *p1
, const void *p2
)
5751 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5752 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5753 int idx1
= ci1
->bb
->index
;
5754 int idx2
= ci2
->bb
->index
;
5758 else if (idx1
== idx2
)
5760 /* Make sure the default label is first in a group. */
5761 if (!CASE_LOW (ci1
->expr
))
5763 else if (!CASE_LOW (ci2
->expr
))
5766 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5767 CASE_LOW (ci2
->expr
));
5773 /* Determine whether the outgoing edges of BB should receive an
5774 ASSERT_EXPR for each of the operands of BB's LAST statement.
5775 The last statement of BB must be a SWITCH_EXPR.
5777 If any of the sub-graphs rooted at BB have an interesting use of
5778 the predicate operands, an assert location node is added to the
5779 list of assertions for the corresponding operands. */
5782 find_switch_asserts (basic_block bb
, gimple_switch last
)
5785 gimple_stmt_iterator bsi
;
5788 struct case_info
*ci
;
5789 size_t n
= gimple_switch_num_labels (last
);
5790 #if GCC_VERSION >= 4000
5793 /* Work around GCC 3.4 bug (PR 37086). */
5794 volatile unsigned int idx
;
5797 need_assert
= false;
5798 bsi
= gsi_for_stmt (last
);
5799 op
= gimple_switch_index (last
);
5800 if (TREE_CODE (op
) != SSA_NAME
)
5803 /* Build a vector of case labels sorted by destination label. */
5804 ci
= XNEWVEC (struct case_info
, n
);
5805 for (idx
= 0; idx
< n
; ++idx
)
5807 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5808 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5810 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5812 for (idx
= 0; idx
< n
; ++idx
)
5815 tree cl
= ci
[idx
].expr
;
5816 basic_block cbb
= ci
[idx
].bb
;
5818 min
= CASE_LOW (cl
);
5819 max
= CASE_HIGH (cl
);
5821 /* If there are multiple case labels with the same destination
5822 we need to combine them to a single value range for the edge. */
5823 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5825 /* Skip labels until the last of the group. */
5828 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5831 /* Pick up the maximum of the case label range. */
5832 if (CASE_HIGH (ci
[idx
].expr
))
5833 max
= CASE_HIGH (ci
[idx
].expr
);
5835 max
= CASE_LOW (ci
[idx
].expr
);
5838 /* Nothing to do if the range includes the default label until we
5839 can register anti-ranges. */
5840 if (min
== NULL_TREE
)
5843 /* Find the edge to register the assert expr on. */
5844 e
= find_edge (bb
, cbb
);
5846 /* Register the necessary assertions for the operand in the
5848 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5849 max
? GE_EXPR
: EQ_EXPR
,
5851 fold_convert (TREE_TYPE (op
),
5855 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5857 fold_convert (TREE_TYPE (op
),
5867 /* Traverse all the statements in block BB looking for statements that
5868 may generate useful assertions for the SSA names in their operand.
5869 If a statement produces a useful assertion A for name N_i, then the
5870 list of assertions already generated for N_i is scanned to
5871 determine if A is actually needed.
5873 If N_i already had the assertion A at a location dominating the
5874 current location, then nothing needs to be done. Otherwise, the
5875 new location for A is recorded instead.
5877 1- For every statement S in BB, all the variables used by S are
5878 added to bitmap FOUND_IN_SUBGRAPH.
5880 2- If statement S uses an operand N in a way that exposes a known
5881 value range for N, then if N was not already generated by an
5882 ASSERT_EXPR, create a new assert location for N. For instance,
5883 if N is a pointer and the statement dereferences it, we can
5884 assume that N is not NULL.
5886 3- COND_EXPRs are a special case of #2. We can derive range
5887 information from the predicate but need to insert different
5888 ASSERT_EXPRs for each of the sub-graphs rooted at the
5889 conditional block. If the last statement of BB is a conditional
5890 expression of the form 'X op Y', then
5892 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5894 b) If the conditional is the only entry point to the sub-graph
5895 corresponding to the THEN_CLAUSE, recurse into it. On
5896 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5897 an ASSERT_EXPR is added for the corresponding variable.
5899 c) Repeat step (b) on the ELSE_CLAUSE.
5901 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5910 In this case, an assertion on the THEN clause is useful to
5911 determine that 'a' is always 9 on that edge. However, an assertion
5912 on the ELSE clause would be unnecessary.
5914 4- If BB does not end in a conditional expression, then we recurse
5915 into BB's dominator children.
5917 At the end of the recursive traversal, every SSA name will have a
5918 list of locations where ASSERT_EXPRs should be added. When a new
5919 location for name N is found, it is registered by calling
5920 register_new_assert_for. That function keeps track of all the
5921 registered assertions to prevent adding unnecessary assertions.
5922 For instance, if a pointer P_4 is dereferenced more than once in a
5923 dominator tree, only the location dominating all the dereference of
5924 P_4 will receive an ASSERT_EXPR.
5926 If this function returns true, then it means that there are names
5927 for which we need to generate ASSERT_EXPRs. Those assertions are
5928 inserted by process_assert_insertions. */
5931 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5936 need_assert
= false;
5937 last
= last_stmt (bb
);
5939 /* If BB's last statement is a conditional statement involving integer
5940 operands, determine if we need to add ASSERT_EXPRs. */
5942 && gimple_code (last
) == GIMPLE_COND
5943 && !fp_predicate (last
)
5944 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5945 need_assert
|= find_conditional_asserts (bb
, as_a
<gimple_cond
> (last
));
5947 /* If BB's last statement is a switch statement involving integer
5948 operands, determine if we need to add ASSERT_EXPRs. */
5950 && gimple_code (last
) == GIMPLE_SWITCH
5951 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5952 need_assert
|= find_switch_asserts (bb
, as_a
<gimple_switch
> (last
));
5954 /* Traverse all the statements in BB marking used names and looking
5955 for statements that may infer assertions for their used operands. */
5956 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
5963 stmt
= gsi_stmt (si
);
5965 if (is_gimple_debug (stmt
))
5968 /* See if we can derive an assertion for any of STMT's operands. */
5969 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5972 enum tree_code comp_code
;
5974 /* If op is not live beyond this stmt, do not bother to insert
5976 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5979 /* If OP is used in such a way that we can infer a value
5980 range for it, and we don't find a previous assertion for
5981 it, create a new assertion location node for OP. */
5982 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5984 /* If we are able to infer a nonzero value range for OP,
5985 then walk backwards through the use-def chain to see if OP
5986 was set via a typecast.
5988 If so, then we can also infer a nonzero value range
5989 for the operand of the NOP_EXPR. */
5990 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5993 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5995 while (is_gimple_assign (def_stmt
)
5996 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
5998 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6000 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6002 t
= gimple_assign_rhs1 (def_stmt
);
6003 def_stmt
= SSA_NAME_DEF_STMT (t
);
6005 /* Note we want to register the assert for the
6006 operand of the NOP_EXPR after SI, not after the
6008 if (! has_single_use (t
))
6010 register_new_assert_for (t
, t
, comp_code
, value
,
6017 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6023 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6024 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6025 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6026 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6029 /* Traverse all PHI nodes in BB, updating live. */
6030 for (gimple_phi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6033 use_operand_p arg_p
;
6035 gimple_phi phi
= si
.phi ();
6036 tree res
= gimple_phi_result (phi
);
6038 if (virtual_operand_p (res
))
6041 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6043 tree arg
= USE_FROM_PTR (arg_p
);
6044 if (TREE_CODE (arg
) == SSA_NAME
)
6045 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6048 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6054 /* Do an RPO walk over the function computing SSA name liveness
6055 on-the-fly and deciding on assert expressions to insert.
6056 Returns true if there are assert expressions to be inserted. */
6059 find_assert_locations (void)
6061 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6062 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6063 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6067 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6068 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6069 for (i
= 0; i
< rpo_cnt
; ++i
)
6072 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6073 the order we compute liveness and insert asserts we otherwise
6074 fail to insert asserts into the loop latch. */
6076 FOR_EACH_LOOP (loop
, 0)
6078 i
= loop
->latch
->index
;
6079 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6080 for (gimple_phi_iterator gsi
= gsi_start_phis (loop
->header
);
6081 !gsi_end_p (gsi
); gsi_next (&gsi
))
6083 gimple_phi phi
= gsi
.phi ();
6084 if (virtual_operand_p (gimple_phi_result (phi
)))
6086 tree arg
= gimple_phi_arg_def (phi
, j
);
6087 if (TREE_CODE (arg
) == SSA_NAME
)
6089 if (live
[i
] == NULL
)
6091 live
[i
] = sbitmap_alloc (num_ssa_names
);
6092 bitmap_clear (live
[i
]);
6094 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6099 need_asserts
= false;
6100 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6102 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6108 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6109 bitmap_clear (live
[rpo
[i
]]);
6112 /* Process BB and update the live information with uses in
6114 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6116 /* Merge liveness into the predecessor blocks and free it. */
6117 if (!bitmap_empty_p (live
[rpo
[i
]]))
6120 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6122 int pred
= e
->src
->index
;
6123 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6128 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6129 bitmap_clear (live
[pred
]);
6131 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6133 if (bb_rpo
[pred
] < pred_rpo
)
6134 pred_rpo
= bb_rpo
[pred
];
6137 /* Record the RPO number of the last visited block that needs
6138 live information from this block. */
6139 last_rpo
[rpo
[i
]] = pred_rpo
;
6143 sbitmap_free (live
[rpo
[i
]]);
6144 live
[rpo
[i
]] = NULL
;
6147 /* We can free all successors live bitmaps if all their
6148 predecessors have been visited already. */
6149 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6150 if (last_rpo
[e
->dest
->index
] == i
6151 && live
[e
->dest
->index
])
6153 sbitmap_free (live
[e
->dest
->index
]);
6154 live
[e
->dest
->index
] = NULL
;
6159 XDELETEVEC (bb_rpo
);
6160 XDELETEVEC (last_rpo
);
6161 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6163 sbitmap_free (live
[i
]);
6166 return need_asserts
;
6169 /* Create an ASSERT_EXPR for NAME and insert it in the location
6170 indicated by LOC. Return true if we made any edge insertions. */
6173 process_assert_insertions_for (tree name
, assert_locus_t loc
)
6175 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6182 /* If we have X <=> X do not insert an assert expr for that. */
6183 if (loc
->expr
== loc
->val
)
6186 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6187 assert_stmt
= build_assert_expr_for (cond
, name
);
6190 /* We have been asked to insert the assertion on an edge. This
6191 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6192 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6193 || (gimple_code (gsi_stmt (loc
->si
))
6196 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6200 /* Otherwise, we can insert right after LOC->SI iff the
6201 statement must not be the last statement in the block. */
6202 stmt
= gsi_stmt (loc
->si
);
6203 if (!stmt_ends_bb_p (stmt
))
6205 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6209 /* If STMT must be the last statement in BB, we can only insert new
6210 assertions on the non-abnormal edge out of BB. Note that since
6211 STMT is not control flow, there may only be one non-abnormal edge
6213 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6214 if (!(e
->flags
& EDGE_ABNORMAL
))
6216 gsi_insert_on_edge (e
, assert_stmt
);
6224 /* Process all the insertions registered for every name N_i registered
6225 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6226 found in ASSERTS_FOR[i]. */
6229 process_assert_insertions (void)
6233 bool update_edges_p
= false;
6234 int num_asserts
= 0;
6236 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6237 dump_all_asserts (dump_file
);
6239 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6241 assert_locus_t loc
= asserts_for
[i
];
6246 assert_locus_t next
= loc
->next
;
6247 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6255 gsi_commit_edge_inserts ();
6257 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6262 /* Traverse the flowgraph looking for conditional jumps to insert range
6263 expressions. These range expressions are meant to provide information
6264 to optimizations that need to reason in terms of value ranges. They
6265 will not be expanded into RTL. For instance, given:
6274 this pass will transform the code into:
6280 x = ASSERT_EXPR <x, x < y>
6285 y = ASSERT_EXPR <y, x >= y>
6289 The idea is that once copy and constant propagation have run, other
6290 optimizations will be able to determine what ranges of values can 'x'
6291 take in different paths of the code, simply by checking the reaching
6292 definition of 'x'. */
6295 insert_range_assertions (void)
6297 need_assert_for
= BITMAP_ALLOC (NULL
);
6298 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6300 calculate_dominance_info (CDI_DOMINATORS
);
6302 if (find_assert_locations ())
6304 process_assert_insertions ();
6305 update_ssa (TODO_update_ssa_no_phi
);
6308 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6310 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6311 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6315 BITMAP_FREE (need_assert_for
);
6318 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6319 and "struct" hacks. If VRP can determine that the
6320 array subscript is a constant, check if it is outside valid
6321 range. If the array subscript is a RANGE, warn if it is
6322 non-overlapping with valid range.
6323 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6326 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6328 value_range_t
* vr
= NULL
;
6329 tree low_sub
, up_sub
;
6330 tree low_bound
, up_bound
, up_bound_p1
;
6333 if (TREE_NO_WARNING (ref
))
6336 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6337 up_bound
= array_ref_up_bound (ref
);
6339 /* Can not check flexible arrays. */
6341 || TREE_CODE (up_bound
) != INTEGER_CST
)
6344 /* Accesses to trailing arrays via pointers may access storage
6345 beyond the types array bounds. */
6346 base
= get_base_address (ref
);
6347 if (base
&& TREE_CODE (base
) == MEM_REF
)
6349 tree cref
, next
= NULL_TREE
;
6351 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6354 cref
= TREE_OPERAND (ref
, 0);
6355 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6356 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6357 next
&& TREE_CODE (next
) != FIELD_DECL
;
6358 next
= DECL_CHAIN (next
))
6361 /* If this is the last field in a struct type or a field in a
6362 union type do not warn. */
6367 low_bound
= array_ref_low_bound (ref
);
6368 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6369 build_int_cst (TREE_TYPE (up_bound
), 1));
6371 if (TREE_CODE (low_sub
) == SSA_NAME
)
6373 vr
= get_value_range (low_sub
);
6374 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6376 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6377 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6381 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6383 if (TREE_CODE (up_sub
) == INTEGER_CST
6384 && tree_int_cst_lt (up_bound
, up_sub
)
6385 && TREE_CODE (low_sub
) == INTEGER_CST
6386 && tree_int_cst_lt (low_sub
, low_bound
))
6388 warning_at (location
, OPT_Warray_bounds
,
6389 "array subscript is outside array bounds");
6390 TREE_NO_WARNING (ref
) = 1;
6393 else if (TREE_CODE (up_sub
) == INTEGER_CST
6394 && (ignore_off_by_one
6395 ? (tree_int_cst_lt (up_bound
, up_sub
)
6396 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6397 : (tree_int_cst_lt (up_bound
, up_sub
)
6398 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6400 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6402 fprintf (dump_file
, "Array bound warning for ");
6403 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6404 fprintf (dump_file
, "\n");
6406 warning_at (location
, OPT_Warray_bounds
,
6407 "array subscript is above array bounds");
6408 TREE_NO_WARNING (ref
) = 1;
6410 else if (TREE_CODE (low_sub
) == INTEGER_CST
6411 && tree_int_cst_lt (low_sub
, low_bound
))
6413 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6415 fprintf (dump_file
, "Array bound warning for ");
6416 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6417 fprintf (dump_file
, "\n");
6419 warning_at (location
, OPT_Warray_bounds
,
6420 "array subscript is below array bounds");
6421 TREE_NO_WARNING (ref
) = 1;
6425 /* Searches if the expr T, located at LOCATION computes
6426 address of an ARRAY_REF, and call check_array_ref on it. */
6429 search_for_addr_array (tree t
, location_t location
)
6431 while (TREE_CODE (t
) == SSA_NAME
)
6433 gimple g
= SSA_NAME_DEF_STMT (t
);
6435 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6438 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6439 != GIMPLE_SINGLE_RHS
)
6442 t
= gimple_assign_rhs1 (g
);
6446 /* We are only interested in addresses of ARRAY_REF's. */
6447 if (TREE_CODE (t
) != ADDR_EXPR
)
6450 /* Check each ARRAY_REFs in the reference chain. */
6453 if (TREE_CODE (t
) == ARRAY_REF
)
6454 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6456 t
= TREE_OPERAND (t
, 0);
6458 while (handled_component_p (t
));
6460 if (TREE_CODE (t
) == MEM_REF
6461 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6462 && !TREE_NO_WARNING (t
))
6464 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6465 tree low_bound
, up_bound
, el_sz
;
6467 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6468 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6469 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6472 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6473 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6474 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6476 || TREE_CODE (low_bound
) != INTEGER_CST
6478 || TREE_CODE (up_bound
) != INTEGER_CST
6480 || TREE_CODE (el_sz
) != INTEGER_CST
)
6483 idx
= mem_ref_offset (t
);
6484 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6485 if (wi::lts_p (idx
, 0))
6487 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6489 fprintf (dump_file
, "Array bound warning for ");
6490 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6491 fprintf (dump_file
, "\n");
6493 warning_at (location
, OPT_Warray_bounds
,
6494 "array subscript is below array bounds");
6495 TREE_NO_WARNING (t
) = 1;
6497 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6498 - wi::to_offset (low_bound
) + 1)))
6500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6502 fprintf (dump_file
, "Array bound warning for ");
6503 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6504 fprintf (dump_file
, "\n");
6506 warning_at (location
, OPT_Warray_bounds
,
6507 "array subscript is above array bounds");
6508 TREE_NO_WARNING (t
) = 1;
6513 /* walk_tree() callback that checks if *TP is
6514 an ARRAY_REF inside an ADDR_EXPR (in which an array
6515 subscript one outside the valid range is allowed). Call
6516 check_array_ref for each ARRAY_REF found. The location is
6520 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6523 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6524 location_t location
;
6526 if (EXPR_HAS_LOCATION (t
))
6527 location
= EXPR_LOCATION (t
);
6530 location_t
*locp
= (location_t
*) wi
->info
;
6534 *walk_subtree
= TRUE
;
6536 if (TREE_CODE (t
) == ARRAY_REF
)
6537 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6539 if (TREE_CODE (t
) == MEM_REF
6540 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6541 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6543 if (TREE_CODE (t
) == ADDR_EXPR
)
6544 *walk_subtree
= FALSE
;
6549 /* Walk over all statements of all reachable BBs and call check_array_bounds
6553 check_all_array_refs (void)
6556 gimple_stmt_iterator si
;
6558 FOR_EACH_BB_FN (bb
, cfun
)
6562 bool executable
= false;
6564 /* Skip blocks that were found to be unreachable. */
6565 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6566 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6570 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6572 gimple stmt
= gsi_stmt (si
);
6573 struct walk_stmt_info wi
;
6574 if (!gimple_has_location (stmt
))
6577 if (is_gimple_call (stmt
))
6580 size_t n
= gimple_call_num_args (stmt
);
6581 for (i
= 0; i
< n
; i
++)
6583 tree arg
= gimple_call_arg (stmt
, i
);
6584 search_for_addr_array (arg
, gimple_location (stmt
));
6589 memset (&wi
, 0, sizeof (wi
));
6590 wi
.info
= CONST_CAST (void *, (const void *)
6591 gimple_location_ptr (stmt
));
6593 walk_gimple_op (gsi_stmt (si
),
6601 /* Return true if all imm uses of VAR are either in STMT, or
6602 feed (optionally through a chain of single imm uses) GIMPLE_COND
6603 in basic block COND_BB. */
6606 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6608 use_operand_p use_p
, use2_p
;
6609 imm_use_iterator iter
;
6611 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6612 if (USE_STMT (use_p
) != stmt
)
6614 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6615 if (is_gimple_debug (use_stmt
))
6617 while (is_gimple_assign (use_stmt
)
6618 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6619 && single_imm_use (gimple_assign_lhs (use_stmt
),
6620 &use2_p
, &use_stmt2
))
6621 use_stmt
= use_stmt2
;
6622 if (gimple_code (use_stmt
) != GIMPLE_COND
6623 || gimple_bb (use_stmt
) != cond_bb
)
6636 __builtin_unreachable ();
6638 x_5 = ASSERT_EXPR <x_3, ...>;
6639 If x_3 has no other immediate uses (checked by caller),
6640 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6641 from the non-zero bitmask. */
6644 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6646 edge e
= single_pred_edge (bb
);
6647 basic_block cond_bb
= e
->src
;
6648 gimple stmt
= last_stmt (cond_bb
);
6652 || gimple_code (stmt
) != GIMPLE_COND
6653 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6654 ? EQ_EXPR
: NE_EXPR
)
6655 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6656 || !integer_zerop (gimple_cond_rhs (stmt
)))
6659 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6660 if (!is_gimple_assign (stmt
)
6661 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6662 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6664 if (gimple_assign_rhs1 (stmt
) != var
)
6668 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6670 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6671 if (!gimple_assign_cast_p (stmt2
)
6672 || gimple_assign_rhs1 (stmt2
) != var
6673 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6674 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6675 != TYPE_PRECISION (TREE_TYPE (var
))))
6678 cst
= gimple_assign_rhs2 (stmt
);
6679 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6682 /* Convert range assertion expressions into the implied copies and
6683 copy propagate away the copies. Doing the trivial copy propagation
6684 here avoids the need to run the full copy propagation pass after
6687 FIXME, this will eventually lead to copy propagation removing the
6688 names that had useful range information attached to them. For
6689 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6690 then N_i will have the range [3, +INF].
6692 However, by converting the assertion into the implied copy
6693 operation N_i = N_j, we will then copy-propagate N_j into the uses
6694 of N_i and lose the range information. We may want to hold on to
6695 ASSERT_EXPRs a little while longer as the ranges could be used in
6696 things like jump threading.
6698 The problem with keeping ASSERT_EXPRs around is that passes after
6699 VRP need to handle them appropriately.
6701 Another approach would be to make the range information a first
6702 class property of the SSA_NAME so that it can be queried from
6703 any pass. This is made somewhat more complex by the need for
6704 multiple ranges to be associated with one SSA_NAME. */
6707 remove_range_assertions (void)
6710 gimple_stmt_iterator si
;
6711 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6712 a basic block preceeded by GIMPLE_COND branching to it and
6713 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6716 /* Note that the BSI iterator bump happens at the bottom of the
6717 loop and no bump is necessary if we're removing the statement
6718 referenced by the current BSI. */
6719 FOR_EACH_BB_FN (bb
, cfun
)
6720 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6722 gimple stmt
= gsi_stmt (si
);
6725 if (is_gimple_assign (stmt
)
6726 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6728 tree lhs
= gimple_assign_lhs (stmt
);
6729 tree rhs
= gimple_assign_rhs1 (stmt
);
6731 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6732 use_operand_p use_p
;
6733 imm_use_iterator iter
;
6735 gcc_assert (cond
!= boolean_false_node
);
6737 var
= ASSERT_EXPR_VAR (rhs
);
6738 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6740 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6741 && SSA_NAME_RANGE_INFO (lhs
))
6743 if (is_unreachable
== -1)
6746 if (single_pred_p (bb
)
6747 && assert_unreachable_fallthru_edge_p
6748 (single_pred_edge (bb
)))
6752 if (x_7 >= 10 && x_7 < 20)
6753 __builtin_unreachable ();
6754 x_8 = ASSERT_EXPR <x_7, ...>;
6755 if the only uses of x_7 are in the ASSERT_EXPR and
6756 in the condition. In that case, we can copy the
6757 range info from x_8 computed in this pass also
6760 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6763 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6764 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6765 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6766 maybe_set_nonzero_bits (bb
, var
);
6770 /* Propagate the RHS into every use of the LHS. */
6771 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6772 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6773 SET_USE (use_p
, var
);
6775 /* And finally, remove the copy, it is not needed. */
6776 gsi_remove (&si
, true);
6777 release_defs (stmt
);
6781 if (!is_gimple_debug (gsi_stmt (si
)))
6789 /* Return true if STMT is interesting for VRP. */
6792 stmt_interesting_for_vrp (gimple stmt
)
6794 if (gimple_code (stmt
) == GIMPLE_PHI
)
6796 tree res
= gimple_phi_result (stmt
);
6797 return (!virtual_operand_p (res
)
6798 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6799 || POINTER_TYPE_P (TREE_TYPE (res
))));
6801 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6803 tree lhs
= gimple_get_lhs (stmt
);
6805 /* In general, assignments with virtual operands are not useful
6806 for deriving ranges, with the obvious exception of calls to
6807 builtin functions. */
6808 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6809 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6810 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6811 && (is_gimple_call (stmt
)
6812 || !gimple_vuse (stmt
)))
6815 else if (gimple_code (stmt
) == GIMPLE_COND
6816 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6823 /* Initialize local data structures for VRP. */
6826 vrp_initialize (void)
6830 values_propagated
= false;
6831 num_vr_values
= num_ssa_names
;
6832 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6833 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6835 FOR_EACH_BB_FN (bb
, cfun
)
6837 for (gimple_phi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6840 gimple_phi phi
= si
.phi ();
6841 if (!stmt_interesting_for_vrp (phi
))
6843 tree lhs
= PHI_RESULT (phi
);
6844 set_value_range_to_varying (get_value_range (lhs
));
6845 prop_set_simulate_again (phi
, false);
6848 prop_set_simulate_again (phi
, true);
6851 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
6854 gimple stmt
= gsi_stmt (si
);
6856 /* If the statement is a control insn, then we do not
6857 want to avoid simulating the statement once. Failure
6858 to do so means that those edges will never get added. */
6859 if (stmt_ends_bb_p (stmt
))
6860 prop_set_simulate_again (stmt
, true);
6861 else if (!stmt_interesting_for_vrp (stmt
))
6865 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6866 set_value_range_to_varying (get_value_range (def
));
6867 prop_set_simulate_again (stmt
, false);
6870 prop_set_simulate_again (stmt
, true);
6875 /* Return the singleton value-range for NAME or NAME. */
6878 vrp_valueize (tree name
)
6880 if (TREE_CODE (name
) == SSA_NAME
)
6882 value_range_t
*vr
= get_value_range (name
);
6883 if (vr
->type
== VR_RANGE
6884 && (vr
->min
== vr
->max
6885 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6891 /* Visit assignment STMT. If it produces an interesting range, record
6892 the SSA name in *OUTPUT_P. */
6894 static enum ssa_prop_result
6895 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6899 enum gimple_code code
= gimple_code (stmt
);
6900 lhs
= gimple_get_lhs (stmt
);
6902 /* We only keep track of ranges in integral and pointer types. */
6903 if (TREE_CODE (lhs
) == SSA_NAME
6904 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6905 /* It is valid to have NULL MIN/MAX values on a type. See
6906 build_range_type. */
6907 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6908 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6909 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6911 value_range_t new_vr
= VR_INITIALIZER
;
6913 /* Try folding the statement to a constant first. */
6914 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6916 set_value_range_to_value (&new_vr
, tem
, NULL
);
6917 /* Then dispatch to value-range extracting functions. */
6918 else if (code
== GIMPLE_CALL
)
6919 extract_range_basic (&new_vr
, stmt
);
6921 extract_range_from_assignment (&new_vr
, as_a
<gimple_assign
> (stmt
));
6923 if (update_value_range (lhs
, &new_vr
))
6927 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6929 fprintf (dump_file
, "Found new range for ");
6930 print_generic_expr (dump_file
, lhs
, 0);
6931 fprintf (dump_file
, ": ");
6932 dump_value_range (dump_file
, &new_vr
);
6933 fprintf (dump_file
, "\n");
6936 if (new_vr
.type
== VR_VARYING
)
6937 return SSA_PROP_VARYING
;
6939 return SSA_PROP_INTERESTING
;
6942 return SSA_PROP_NOT_INTERESTING
;
6945 /* Every other statement produces no useful ranges. */
6946 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6947 set_value_range_to_varying (get_value_range (def
));
6949 return SSA_PROP_VARYING
;
6952 /* Helper that gets the value range of the SSA_NAME with version I
6953 or a symbolic range containing the SSA_NAME only if the value range
6954 is varying or undefined. */
6956 static inline value_range_t
6957 get_vr_for_comparison (int i
)
6959 value_range_t vr
= *get_value_range (ssa_name (i
));
6961 /* If name N_i does not have a valid range, use N_i as its own
6962 range. This allows us to compare against names that may
6963 have N_i in their ranges. */
6964 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6967 vr
.min
= ssa_name (i
);
6968 vr
.max
= ssa_name (i
);
6974 /* Compare all the value ranges for names equivalent to VAR with VAL
6975 using comparison code COMP. Return the same value returned by
6976 compare_range_with_value, including the setting of
6977 *STRICT_OVERFLOW_P. */
6980 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6981 bool *strict_overflow_p
)
6987 int used_strict_overflow
;
6989 value_range_t equiv_vr
;
6991 /* Get the set of equivalences for VAR. */
6992 e
= get_value_range (var
)->equiv
;
6994 /* Start at -1. Set it to 0 if we do a comparison without relying
6995 on overflow, or 1 if all comparisons rely on overflow. */
6996 used_strict_overflow
= -1;
6998 /* Compare vars' value range with val. */
6999 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7001 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7003 used_strict_overflow
= sop
? 1 : 0;
7005 /* If the equiv set is empty we have done all work we need to do. */
7009 && used_strict_overflow
> 0)
7010 *strict_overflow_p
= true;
7014 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7016 equiv_vr
= get_vr_for_comparison (i
);
7018 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7021 /* If we get different answers from different members
7022 of the equivalence set this check must be in a dead
7023 code region. Folding it to a trap representation
7024 would be correct here. For now just return don't-know. */
7034 used_strict_overflow
= 0;
7035 else if (used_strict_overflow
< 0)
7036 used_strict_overflow
= 1;
7041 && used_strict_overflow
> 0)
7042 *strict_overflow_p
= true;
7048 /* Given a comparison code COMP and names N1 and N2, compare all the
7049 ranges equivalent to N1 against all the ranges equivalent to N2
7050 to determine the value of N1 COMP N2. Return the same value
7051 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7052 whether we relied on an overflow infinity in the comparison. */
7056 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7057 bool *strict_overflow_p
)
7061 bitmap_iterator bi1
, bi2
;
7063 int used_strict_overflow
;
7064 static bitmap_obstack
*s_obstack
= NULL
;
7065 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7067 /* Compare the ranges of every name equivalent to N1 against the
7068 ranges of every name equivalent to N2. */
7069 e1
= get_value_range (n1
)->equiv
;
7070 e2
= get_value_range (n2
)->equiv
;
7072 /* Use the fake bitmaps if e1 or e2 are not available. */
7073 if (s_obstack
== NULL
)
7075 s_obstack
= XNEW (bitmap_obstack
);
7076 bitmap_obstack_initialize (s_obstack
);
7077 s_e1
= BITMAP_ALLOC (s_obstack
);
7078 s_e2
= BITMAP_ALLOC (s_obstack
);
7085 /* Add N1 and N2 to their own set of equivalences to avoid
7086 duplicating the body of the loop just to check N1 and N2
7088 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7089 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7091 /* If the equivalence sets have a common intersection, then the two
7092 names can be compared without checking their ranges. */
7093 if (bitmap_intersect_p (e1
, e2
))
7095 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7096 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7098 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7100 : boolean_false_node
;
7103 /* Start at -1. Set it to 0 if we do a comparison without relying
7104 on overflow, or 1 if all comparisons rely on overflow. */
7105 used_strict_overflow
= -1;
7107 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7108 N2 to their own set of equivalences to avoid duplicating the body
7109 of the loop just to check N1 and N2 ranges. */
7110 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7112 value_range_t vr1
= get_vr_for_comparison (i1
);
7114 t
= retval
= NULL_TREE
;
7115 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7119 value_range_t vr2
= get_vr_for_comparison (i2
);
7121 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7124 /* If we get different answers from different members
7125 of the equivalence set this check must be in a dead
7126 code region. Folding it to a trap representation
7127 would be correct here. For now just return don't-know. */
7131 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7132 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7138 used_strict_overflow
= 0;
7139 else if (used_strict_overflow
< 0)
7140 used_strict_overflow
= 1;
7146 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7147 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7148 if (used_strict_overflow
> 0)
7149 *strict_overflow_p
= true;
7154 /* None of the equivalent ranges are useful in computing this
7156 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7157 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7161 /* Helper function for vrp_evaluate_conditional_warnv. */
7164 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7166 bool * strict_overflow_p
)
7168 value_range_t
*vr0
, *vr1
;
7170 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7171 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7173 tree res
= NULL_TREE
;
7175 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7177 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7179 res
= (compare_range_with_value
7180 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7184 /* Helper function for vrp_evaluate_conditional_warnv. */
7187 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7188 tree op1
, bool use_equiv_p
,
7189 bool *strict_overflow_p
, bool *only_ranges
)
7193 *only_ranges
= true;
7195 /* We only deal with integral and pointer types. */
7196 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7197 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7203 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7204 (code
, op0
, op1
, strict_overflow_p
)))
7206 *only_ranges
= false;
7207 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7208 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7209 else if (TREE_CODE (op0
) == SSA_NAME
)
7210 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7211 else if (TREE_CODE (op1
) == SSA_NAME
)
7212 return (compare_name_with_value
7213 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7216 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7221 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7222 information. Return NULL if the conditional can not be evaluated.
7223 The ranges of all the names equivalent with the operands in COND
7224 will be used when trying to compute the value. If the result is
7225 based on undefined signed overflow, issue a warning if
7229 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7235 /* Some passes and foldings leak constants with overflow flag set
7236 into the IL. Avoid doing wrong things with these and bail out. */
7237 if ((TREE_CODE (op0
) == INTEGER_CST
7238 && TREE_OVERFLOW (op0
))
7239 || (TREE_CODE (op1
) == INTEGER_CST
7240 && TREE_OVERFLOW (op1
)))
7244 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7249 enum warn_strict_overflow_code wc
;
7250 const char* warnmsg
;
7252 if (is_gimple_min_invariant (ret
))
7254 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7255 warnmsg
= G_("assuming signed overflow does not occur when "
7256 "simplifying conditional to constant");
7260 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7261 warnmsg
= G_("assuming signed overflow does not occur when "
7262 "simplifying conditional");
7265 if (issue_strict_overflow_warning (wc
))
7267 location_t location
;
7269 if (!gimple_has_location (stmt
))
7270 location
= input_location
;
7272 location
= gimple_location (stmt
);
7273 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7277 if (warn_type_limits
7278 && ret
&& only_ranges
7279 && TREE_CODE_CLASS (code
) == tcc_comparison
7280 && TREE_CODE (op0
) == SSA_NAME
)
7282 /* If the comparison is being folded and the operand on the LHS
7283 is being compared against a constant value that is outside of
7284 the natural range of OP0's type, then the predicate will
7285 always fold regardless of the value of OP0. If -Wtype-limits
7286 was specified, emit a warning. */
7287 tree type
= TREE_TYPE (op0
);
7288 value_range_t
*vr0
= get_value_range (op0
);
7290 if (vr0
->type
!= VR_VARYING
7291 && INTEGRAL_TYPE_P (type
)
7292 && vrp_val_is_min (vr0
->min
)
7293 && vrp_val_is_max (vr0
->max
)
7294 && is_gimple_min_invariant (op1
))
7296 location_t location
;
7298 if (!gimple_has_location (stmt
))
7299 location
= input_location
;
7301 location
= gimple_location (stmt
);
7303 warning_at (location
, OPT_Wtype_limits
,
7305 ? G_("comparison always false "
7306 "due to limited range of data type")
7307 : G_("comparison always true "
7308 "due to limited range of data type"));
7316 /* Visit conditional statement STMT. If we can determine which edge
7317 will be taken out of STMT's basic block, record it in
7318 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7319 SSA_PROP_VARYING. */
7321 static enum ssa_prop_result
7322 vrp_visit_cond_stmt (gimple_cond stmt
, edge
*taken_edge_p
)
7327 *taken_edge_p
= NULL
;
7329 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7334 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7335 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7336 fprintf (dump_file
, "\nWith known ranges\n");
7338 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7340 fprintf (dump_file
, "\t");
7341 print_generic_expr (dump_file
, use
, 0);
7342 fprintf (dump_file
, ": ");
7343 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7346 fprintf (dump_file
, "\n");
7349 /* Compute the value of the predicate COND by checking the known
7350 ranges of each of its operands.
7352 Note that we cannot evaluate all the equivalent ranges here
7353 because those ranges may not yet be final and with the current
7354 propagation strategy, we cannot determine when the value ranges
7355 of the names in the equivalence set have changed.
7357 For instance, given the following code fragment
7361 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7365 Assume that on the first visit to i_14, i_5 has the temporary
7366 range [8, 8] because the second argument to the PHI function is
7367 not yet executable. We derive the range ~[0, 0] for i_14 and the
7368 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7369 the first time, since i_14 is equivalent to the range [8, 8], we
7370 determine that the predicate is always false.
7372 On the next round of propagation, i_13 is determined to be
7373 VARYING, which causes i_5 to drop down to VARYING. So, another
7374 visit to i_14 is scheduled. In this second visit, we compute the
7375 exact same range and equivalence set for i_14, namely ~[0, 0] and
7376 { i_5 }. But we did not have the previous range for i_5
7377 registered, so vrp_visit_assignment thinks that the range for
7378 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7379 is not visited again, which stops propagation from visiting
7380 statements in the THEN clause of that if().
7382 To properly fix this we would need to keep the previous range
7383 value for the names in the equivalence set. This way we would've
7384 discovered that from one visit to the other i_5 changed from
7385 range [8, 8] to VR_VARYING.
7387 However, fixing this apparent limitation may not be worth the
7388 additional checking. Testing on several code bases (GCC, DLV,
7389 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7390 4 more predicates folded in SPEC. */
7393 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7394 gimple_cond_lhs (stmt
),
7395 gimple_cond_rhs (stmt
),
7400 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7403 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7405 "\nIgnoring predicate evaluation because "
7406 "it assumes that signed overflow is undefined");
7411 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7413 fprintf (dump_file
, "\nPredicate evaluates to: ");
7414 if (val
== NULL_TREE
)
7415 fprintf (dump_file
, "DON'T KNOW\n");
7417 print_generic_stmt (dump_file
, val
, 0);
7420 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7423 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7424 that includes the value VAL. The search is restricted to the range
7425 [START_IDX, n - 1] where n is the size of VEC.
7427 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7430 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7431 it is placed in IDX and false is returned.
7433 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7437 find_case_label_index (gimple_switch stmt
, size_t start_idx
, tree val
, size_t *idx
)
7439 size_t n
= gimple_switch_num_labels (stmt
);
7442 /* Find case label for minimum of the value range or the next one.
7443 At each iteration we are searching in [low, high - 1]. */
7445 for (low
= start_idx
, high
= n
; high
!= low
; )
7449 /* Note that i != high, so we never ask for n. */
7450 size_t i
= (high
+ low
) / 2;
7451 t
= gimple_switch_label (stmt
, i
);
7453 /* Cache the result of comparing CASE_LOW and val. */
7454 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7458 /* Ranges cannot be empty. */
7467 if (CASE_HIGH (t
) != NULL
7468 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7480 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7481 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7482 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7483 then MAX_IDX < MIN_IDX.
7484 Returns true if the default label is not needed. */
7487 find_case_label_range (gimple_switch stmt
, tree min
, tree max
, size_t *min_idx
,
7491 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7492 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7496 && max_take_default
)
7498 /* Only the default case label reached.
7499 Return an empty range. */
7506 bool take_default
= min_take_default
|| max_take_default
;
7510 if (max_take_default
)
7513 /* If the case label range is continuous, we do not need
7514 the default case label. Verify that. */
7515 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7516 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7517 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7518 for (k
= i
+ 1; k
<= j
; ++k
)
7520 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7521 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7523 take_default
= true;
7527 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7528 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7533 return !take_default
;
7537 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7538 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7539 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7540 Returns true if the default label is not needed. */
7543 find_case_label_ranges (gimple_switch stmt
, value_range_t
*vr
, size_t *min_idx1
,
7544 size_t *max_idx1
, size_t *min_idx2
,
7548 unsigned int n
= gimple_switch_num_labels (stmt
);
7550 tree case_low
, case_high
;
7551 tree min
= vr
->min
, max
= vr
->max
;
7553 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7555 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7557 /* Set second range to emtpy. */
7561 if (vr
->type
== VR_RANGE
)
7565 return !take_default
;
7568 /* Set first range to all case labels. */
7575 /* Make sure all the values of case labels [i , j] are contained in
7576 range [MIN, MAX]. */
7577 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7578 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7579 if (tree_int_cst_compare (case_low
, min
) < 0)
7581 if (case_high
!= NULL_TREE
7582 && tree_int_cst_compare (max
, case_high
) < 0)
7588 /* If the range spans case labels [i, j], the corresponding anti-range spans
7589 the labels [1, i - 1] and [j + 1, n - 1]. */
7615 /* Visit switch statement STMT. If we can determine which edge
7616 will be taken out of STMT's basic block, record it in
7617 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7618 SSA_PROP_VARYING. */
7620 static enum ssa_prop_result
7621 vrp_visit_switch_stmt (gimple_switch stmt
, edge
*taken_edge_p
)
7625 size_t i
= 0, j
= 0, k
, l
;
7628 *taken_edge_p
= NULL
;
7629 op
= gimple_switch_index (stmt
);
7630 if (TREE_CODE (op
) != SSA_NAME
)
7631 return SSA_PROP_VARYING
;
7633 vr
= get_value_range (op
);
7634 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7636 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7637 print_generic_expr (dump_file
, op
, 0);
7638 fprintf (dump_file
, " with known range ");
7639 dump_value_range (dump_file
, vr
);
7640 fprintf (dump_file
, "\n");
7643 if ((vr
->type
!= VR_RANGE
7644 && vr
->type
!= VR_ANTI_RANGE
)
7645 || symbolic_range_p (vr
))
7646 return SSA_PROP_VARYING
;
7648 /* Find the single edge that is taken from the switch expression. */
7649 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7651 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7655 gcc_assert (take_default
);
7656 val
= gimple_switch_default_label (stmt
);
7660 /* Check if labels with index i to j and maybe the default label
7661 are all reaching the same label. */
7663 val
= gimple_switch_label (stmt
, i
);
7665 && CASE_LABEL (gimple_switch_default_label (stmt
))
7666 != CASE_LABEL (val
))
7668 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7669 fprintf (dump_file
, " not a single destination for this "
7671 return SSA_PROP_VARYING
;
7673 for (++i
; i
<= j
; ++i
)
7675 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7677 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7678 fprintf (dump_file
, " not a single destination for this "
7680 return SSA_PROP_VARYING
;
7685 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7687 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7688 fprintf (dump_file
, " not a single destination for this "
7690 return SSA_PROP_VARYING
;
7695 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7696 label_to_block (CASE_LABEL (val
)));
7698 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7700 fprintf (dump_file
, " will take edge to ");
7701 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7704 return SSA_PROP_INTERESTING
;
7708 /* Evaluate statement STMT. If the statement produces a useful range,
7709 return SSA_PROP_INTERESTING and record the SSA name with the
7710 interesting range into *OUTPUT_P.
7712 If STMT is a conditional branch and we can determine its truth
7713 value, the taken edge is recorded in *TAKEN_EDGE_P.
7715 If STMT produces a varying value, return SSA_PROP_VARYING. */
7717 static enum ssa_prop_result
7718 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7723 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7725 fprintf (dump_file
, "\nVisiting statement:\n");
7726 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7729 if (!stmt_interesting_for_vrp (stmt
))
7730 gcc_assert (stmt_ends_bb_p (stmt
));
7731 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7732 return vrp_visit_assignment_or_call (stmt
, output_p
);
7733 else if (gimple_code (stmt
) == GIMPLE_COND
)
7734 return vrp_visit_cond_stmt (as_a
<gimple_cond
> (stmt
), taken_edge_p
);
7735 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7736 return vrp_visit_switch_stmt (as_a
<gimple_switch
> (stmt
), taken_edge_p
);
7738 /* All other statements produce nothing of interest for VRP, so mark
7739 their outputs varying and prevent further simulation. */
7740 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7741 set_value_range_to_varying (get_value_range (def
));
7743 return SSA_PROP_VARYING
;
7746 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7747 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7748 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7749 possible such range. The resulting range is not canonicalized. */
7752 union_ranges (enum value_range_type
*vr0type
,
7753 tree
*vr0min
, tree
*vr0max
,
7754 enum value_range_type vr1type
,
7755 tree vr1min
, tree vr1max
)
7757 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7758 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7760 /* [] is vr0, () is vr1 in the following classification comments. */
7764 if (*vr0type
== vr1type
)
7765 /* Nothing to do for equal ranges. */
7767 else if ((*vr0type
== VR_RANGE
7768 && vr1type
== VR_ANTI_RANGE
)
7769 || (*vr0type
== VR_ANTI_RANGE
7770 && vr1type
== VR_RANGE
))
7772 /* For anti-range with range union the result is varying. */
7778 else if (operand_less_p (*vr0max
, vr1min
) == 1
7779 || operand_less_p (vr1max
, *vr0min
) == 1)
7781 /* [ ] ( ) or ( ) [ ]
7782 If the ranges have an empty intersection, result of the union
7783 operation is the anti-range or if both are anti-ranges
7785 if (*vr0type
== VR_ANTI_RANGE
7786 && vr1type
== VR_ANTI_RANGE
)
7788 else if (*vr0type
== VR_ANTI_RANGE
7789 && vr1type
== VR_RANGE
)
7791 else if (*vr0type
== VR_RANGE
7792 && vr1type
== VR_ANTI_RANGE
)
7798 else if (*vr0type
== VR_RANGE
7799 && vr1type
== VR_RANGE
)
7801 /* The result is the convex hull of both ranges. */
7802 if (operand_less_p (*vr0max
, vr1min
) == 1)
7804 /* If the result can be an anti-range, create one. */
7805 if (TREE_CODE (*vr0max
) == INTEGER_CST
7806 && TREE_CODE (vr1min
) == INTEGER_CST
7807 && vrp_val_is_min (*vr0min
)
7808 && vrp_val_is_max (vr1max
))
7810 tree min
= int_const_binop (PLUS_EXPR
,
7812 build_int_cst (TREE_TYPE (*vr0max
), 1));
7813 tree max
= int_const_binop (MINUS_EXPR
,
7815 build_int_cst (TREE_TYPE (vr1min
), 1));
7816 if (!operand_less_p (max
, min
))
7818 *vr0type
= VR_ANTI_RANGE
;
7830 /* If the result can be an anti-range, create one. */
7831 if (TREE_CODE (vr1max
) == INTEGER_CST
7832 && TREE_CODE (*vr0min
) == INTEGER_CST
7833 && vrp_val_is_min (vr1min
)
7834 && vrp_val_is_max (*vr0max
))
7836 tree min
= int_const_binop (PLUS_EXPR
,
7838 build_int_cst (TREE_TYPE (vr1max
), 1));
7839 tree max
= int_const_binop (MINUS_EXPR
,
7841 build_int_cst (TREE_TYPE (*vr0min
), 1));
7842 if (!operand_less_p (max
, min
))
7844 *vr0type
= VR_ANTI_RANGE
;
7858 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7859 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7861 /* [ ( ) ] or [( ) ] or [ ( )] */
7862 if (*vr0type
== VR_RANGE
7863 && vr1type
== VR_RANGE
)
7865 else if (*vr0type
== VR_ANTI_RANGE
7866 && vr1type
== VR_ANTI_RANGE
)
7872 else if (*vr0type
== VR_ANTI_RANGE
7873 && vr1type
== VR_RANGE
)
7875 /* Arbitrarily choose the right or left gap. */
7876 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7877 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7878 build_int_cst (TREE_TYPE (vr1min
), 1));
7879 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7880 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7881 build_int_cst (TREE_TYPE (vr1max
), 1));
7885 else if (*vr0type
== VR_RANGE
7886 && vr1type
== VR_ANTI_RANGE
)
7887 /* The result covers everything. */
7892 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7893 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7895 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7896 if (*vr0type
== VR_RANGE
7897 && vr1type
== VR_RANGE
)
7903 else if (*vr0type
== VR_ANTI_RANGE
7904 && vr1type
== VR_ANTI_RANGE
)
7906 else if (*vr0type
== VR_RANGE
7907 && vr1type
== VR_ANTI_RANGE
)
7909 *vr0type
= VR_ANTI_RANGE
;
7910 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7912 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7913 build_int_cst (TREE_TYPE (*vr0min
), 1));
7916 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7918 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7919 build_int_cst (TREE_TYPE (*vr0max
), 1));
7925 else if (*vr0type
== VR_ANTI_RANGE
7926 && vr1type
== VR_RANGE
)
7927 /* The result covers everything. */
7932 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7933 || operand_equal_p (vr1min
, *vr0max
, 0))
7934 && operand_less_p (*vr0min
, vr1min
) == 1
7935 && operand_less_p (*vr0max
, vr1max
) == 1)
7937 /* [ ( ] ) or [ ]( ) */
7938 if (*vr0type
== VR_RANGE
7939 && vr1type
== VR_RANGE
)
7941 else if (*vr0type
== VR_ANTI_RANGE
7942 && vr1type
== VR_ANTI_RANGE
)
7944 else if (*vr0type
== VR_ANTI_RANGE
7945 && vr1type
== VR_RANGE
)
7947 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7948 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7949 build_int_cst (TREE_TYPE (vr1min
), 1));
7953 else if (*vr0type
== VR_RANGE
7954 && vr1type
== VR_ANTI_RANGE
)
7956 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7959 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7960 build_int_cst (TREE_TYPE (*vr0max
), 1));
7969 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7970 || operand_equal_p (*vr0min
, vr1max
, 0))
7971 && operand_less_p (vr1min
, *vr0min
) == 1
7972 && operand_less_p (vr1max
, *vr0max
) == 1)
7974 /* ( [ ) ] or ( )[ ] */
7975 if (*vr0type
== VR_RANGE
7976 && vr1type
== VR_RANGE
)
7978 else if (*vr0type
== VR_ANTI_RANGE
7979 && vr1type
== VR_ANTI_RANGE
)
7981 else if (*vr0type
== VR_ANTI_RANGE
7982 && vr1type
== VR_RANGE
)
7984 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7985 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7986 build_int_cst (TREE_TYPE (vr1max
), 1));
7990 else if (*vr0type
== VR_RANGE
7991 && vr1type
== VR_ANTI_RANGE
)
7993 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7997 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7998 build_int_cst (TREE_TYPE (*vr0min
), 1));
8012 *vr0type
= VR_VARYING
;
8013 *vr0min
= NULL_TREE
;
8014 *vr0max
= NULL_TREE
;
8017 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8018 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8019 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8020 possible such range. The resulting range is not canonicalized. */
8023 intersect_ranges (enum value_range_type
*vr0type
,
8024 tree
*vr0min
, tree
*vr0max
,
8025 enum value_range_type vr1type
,
8026 tree vr1min
, tree vr1max
)
8028 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8029 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8031 /* [] is vr0, () is vr1 in the following classification comments. */
8035 if (*vr0type
== vr1type
)
8036 /* Nothing to do for equal ranges. */
8038 else if ((*vr0type
== VR_RANGE
8039 && vr1type
== VR_ANTI_RANGE
)
8040 || (*vr0type
== VR_ANTI_RANGE
8041 && vr1type
== VR_RANGE
))
8043 /* For anti-range with range intersection the result is empty. */
8044 *vr0type
= VR_UNDEFINED
;
8045 *vr0min
= NULL_TREE
;
8046 *vr0max
= NULL_TREE
;
8051 else if (operand_less_p (*vr0max
, vr1min
) == 1
8052 || operand_less_p (vr1max
, *vr0min
) == 1)
8054 /* [ ] ( ) or ( ) [ ]
8055 If the ranges have an empty intersection, the result of the
8056 intersect operation is the range for intersecting an
8057 anti-range with a range or empty when intersecting two ranges. */
8058 if (*vr0type
== VR_RANGE
8059 && vr1type
== VR_ANTI_RANGE
)
8061 else if (*vr0type
== VR_ANTI_RANGE
8062 && vr1type
== VR_RANGE
)
8068 else if (*vr0type
== VR_RANGE
8069 && vr1type
== VR_RANGE
)
8071 *vr0type
= VR_UNDEFINED
;
8072 *vr0min
= NULL_TREE
;
8073 *vr0max
= NULL_TREE
;
8075 else if (*vr0type
== VR_ANTI_RANGE
8076 && vr1type
== VR_ANTI_RANGE
)
8078 /* If the anti-ranges are adjacent to each other merge them. */
8079 if (TREE_CODE (*vr0max
) == INTEGER_CST
8080 && TREE_CODE (vr1min
) == INTEGER_CST
8081 && operand_less_p (*vr0max
, vr1min
) == 1
8082 && integer_onep (int_const_binop (MINUS_EXPR
,
8085 else if (TREE_CODE (vr1max
) == INTEGER_CST
8086 && TREE_CODE (*vr0min
) == INTEGER_CST
8087 && operand_less_p (vr1max
, *vr0min
) == 1
8088 && integer_onep (int_const_binop (MINUS_EXPR
,
8091 /* Else arbitrarily take VR0. */
8094 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8095 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8097 /* [ ( ) ] or [( ) ] or [ ( )] */
8098 if (*vr0type
== VR_RANGE
8099 && vr1type
== VR_RANGE
)
8101 /* If both are ranges the result is the inner one. */
8106 else if (*vr0type
== VR_RANGE
8107 && vr1type
== VR_ANTI_RANGE
)
8109 /* Choose the right gap if the left one is empty. */
8112 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8113 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8114 build_int_cst (TREE_TYPE (vr1max
), 1));
8118 /* Choose the left gap if the right one is empty. */
8121 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8122 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8123 build_int_cst (TREE_TYPE (vr1min
), 1));
8127 /* Choose the anti-range if the range is effectively varying. */
8128 else if (vrp_val_is_min (*vr0min
)
8129 && vrp_val_is_max (*vr0max
))
8135 /* Else choose the range. */
8137 else if (*vr0type
== VR_ANTI_RANGE
8138 && vr1type
== VR_ANTI_RANGE
)
8139 /* If both are anti-ranges the result is the outer one. */
8141 else if (*vr0type
== VR_ANTI_RANGE
8142 && vr1type
== VR_RANGE
)
8144 /* The intersection is empty. */
8145 *vr0type
= VR_UNDEFINED
;
8146 *vr0min
= NULL_TREE
;
8147 *vr0max
= NULL_TREE
;
8152 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8153 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8155 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8156 if (*vr0type
== VR_RANGE
8157 && vr1type
== VR_RANGE
)
8158 /* Choose the inner range. */
8160 else if (*vr0type
== VR_ANTI_RANGE
8161 && vr1type
== VR_RANGE
)
8163 /* Choose the right gap if the left is empty. */
8166 *vr0type
= VR_RANGE
;
8167 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8168 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8169 build_int_cst (TREE_TYPE (*vr0max
), 1));
8174 /* Choose the left gap if the right is empty. */
8177 *vr0type
= VR_RANGE
;
8178 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8179 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8180 build_int_cst (TREE_TYPE (*vr0min
), 1));
8185 /* Choose the anti-range if the range is effectively varying. */
8186 else if (vrp_val_is_min (vr1min
)
8187 && vrp_val_is_max (vr1max
))
8189 /* Else choose the range. */
8197 else if (*vr0type
== VR_ANTI_RANGE
8198 && vr1type
== VR_ANTI_RANGE
)
8200 /* If both are anti-ranges the result is the outer one. */
8205 else if (vr1type
== VR_ANTI_RANGE
8206 && *vr0type
== VR_RANGE
)
8208 /* The intersection is empty. */
8209 *vr0type
= VR_UNDEFINED
;
8210 *vr0min
= NULL_TREE
;
8211 *vr0max
= NULL_TREE
;
8216 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8217 || operand_equal_p (vr1min
, *vr0max
, 0))
8218 && operand_less_p (*vr0min
, vr1min
) == 1)
8220 /* [ ( ] ) or [ ]( ) */
8221 if (*vr0type
== VR_ANTI_RANGE
8222 && vr1type
== VR_ANTI_RANGE
)
8224 else if (*vr0type
== VR_RANGE
8225 && vr1type
== VR_RANGE
)
8227 else if (*vr0type
== VR_RANGE
8228 && vr1type
== VR_ANTI_RANGE
)
8230 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8231 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8232 build_int_cst (TREE_TYPE (vr1min
), 1));
8236 else if (*vr0type
== VR_ANTI_RANGE
8237 && vr1type
== VR_RANGE
)
8239 *vr0type
= VR_RANGE
;
8240 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8241 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8242 build_int_cst (TREE_TYPE (*vr0max
), 1));
8250 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8251 || operand_equal_p (*vr0min
, vr1max
, 0))
8252 && operand_less_p (vr1min
, *vr0min
) == 1)
8254 /* ( [ ) ] or ( )[ ] */
8255 if (*vr0type
== VR_ANTI_RANGE
8256 && vr1type
== VR_ANTI_RANGE
)
8258 else if (*vr0type
== VR_RANGE
8259 && vr1type
== VR_RANGE
)
8261 else if (*vr0type
== VR_RANGE
8262 && vr1type
== VR_ANTI_RANGE
)
8264 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8265 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8266 build_int_cst (TREE_TYPE (vr1max
), 1));
8270 else if (*vr0type
== VR_ANTI_RANGE
8271 && vr1type
== VR_RANGE
)
8273 *vr0type
= VR_RANGE
;
8274 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8275 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8276 build_int_cst (TREE_TYPE (*vr0min
), 1));
8285 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8286 result for the intersection. That's always a conservative
8287 correct estimate. */
8293 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8294 in *VR0. This may not be the smallest possible such range. */
8297 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8299 value_range_t saved
;
8301 /* If either range is VR_VARYING the other one wins. */
8302 if (vr1
->type
== VR_VARYING
)
8304 if (vr0
->type
== VR_VARYING
)
8306 copy_value_range (vr0
, vr1
);
8310 /* When either range is VR_UNDEFINED the resulting range is
8311 VR_UNDEFINED, too. */
8312 if (vr0
->type
== VR_UNDEFINED
)
8314 if (vr1
->type
== VR_UNDEFINED
)
8316 set_value_range_to_undefined (vr0
);
8320 /* Save the original vr0 so we can return it as conservative intersection
8321 result when our worker turns things to varying. */
8323 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8324 vr1
->type
, vr1
->min
, vr1
->max
);
8325 /* Make sure to canonicalize the result though as the inversion of a
8326 VR_RANGE can still be a VR_RANGE. */
8327 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8328 vr0
->min
, vr0
->max
, vr0
->equiv
);
8329 /* If that failed, use the saved original VR0. */
8330 if (vr0
->type
== VR_VARYING
)
8335 /* If the result is VR_UNDEFINED there is no need to mess with
8336 the equivalencies. */
8337 if (vr0
->type
== VR_UNDEFINED
)
8340 /* The resulting set of equivalences for range intersection is the union of
8342 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8343 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8344 else if (vr1
->equiv
&& !vr0
->equiv
)
8345 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8349 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8351 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8353 fprintf (dump_file
, "Intersecting\n ");
8354 dump_value_range (dump_file
, vr0
);
8355 fprintf (dump_file
, "\nand\n ");
8356 dump_value_range (dump_file
, vr1
);
8357 fprintf (dump_file
, "\n");
8359 vrp_intersect_ranges_1 (vr0
, vr1
);
8360 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8362 fprintf (dump_file
, "to\n ");
8363 dump_value_range (dump_file
, vr0
);
8364 fprintf (dump_file
, "\n");
8368 /* Meet operation for value ranges. Given two value ranges VR0 and
8369 VR1, store in VR0 a range that contains both VR0 and VR1. This
8370 may not be the smallest possible such range. */
8373 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8375 value_range_t saved
;
8377 if (vr0
->type
== VR_UNDEFINED
)
8379 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8383 if (vr1
->type
== VR_UNDEFINED
)
8385 /* VR0 already has the resulting range. */
8389 if (vr0
->type
== VR_VARYING
)
8391 /* Nothing to do. VR0 already has the resulting range. */
8395 if (vr1
->type
== VR_VARYING
)
8397 set_value_range_to_varying (vr0
);
8402 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8403 vr1
->type
, vr1
->min
, vr1
->max
);
8404 if (vr0
->type
== VR_VARYING
)
8406 /* Failed to find an efficient meet. Before giving up and setting
8407 the result to VARYING, see if we can at least derive a useful
8408 anti-range. FIXME, all this nonsense about distinguishing
8409 anti-ranges from ranges is necessary because of the odd
8410 semantics of range_includes_zero_p and friends. */
8411 if (((saved
.type
== VR_RANGE
8412 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8413 || (saved
.type
== VR_ANTI_RANGE
8414 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8415 && ((vr1
->type
== VR_RANGE
8416 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8417 || (vr1
->type
== VR_ANTI_RANGE
8418 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8420 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8422 /* Since this meet operation did not result from the meeting of
8423 two equivalent names, VR0 cannot have any equivalences. */
8425 bitmap_clear (vr0
->equiv
);
8429 set_value_range_to_varying (vr0
);
8432 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8434 if (vr0
->type
== VR_VARYING
)
8437 /* The resulting set of equivalences is always the intersection of
8439 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8440 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8441 else if (vr0
->equiv
&& !vr1
->equiv
)
8442 bitmap_clear (vr0
->equiv
);
8446 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8448 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8450 fprintf (dump_file
, "Meeting\n ");
8451 dump_value_range (dump_file
, vr0
);
8452 fprintf (dump_file
, "\nand\n ");
8453 dump_value_range (dump_file
, vr1
);
8454 fprintf (dump_file
, "\n");
8456 vrp_meet_1 (vr0
, vr1
);
8457 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8459 fprintf (dump_file
, "to\n ");
8460 dump_value_range (dump_file
, vr0
);
8461 fprintf (dump_file
, "\n");
8466 /* Visit all arguments for PHI node PHI that flow through executable
8467 edges. If a valid value range can be derived from all the incoming
8468 value ranges, set a new range for the LHS of PHI. */
8470 static enum ssa_prop_result
8471 vrp_visit_phi_node (gimple_phi phi
)
8474 tree lhs
= PHI_RESULT (phi
);
8475 value_range_t
*lhs_vr
= get_value_range (lhs
);
8476 value_range_t vr_result
= VR_INITIALIZER
;
8478 int edges
, old_edges
;
8481 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8483 fprintf (dump_file
, "\nVisiting PHI node: ");
8484 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8488 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8490 edge e
= gimple_phi_arg_edge (phi
, i
);
8492 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8495 " Argument #%d (%d -> %d %sexecutable)\n",
8496 (int) i
, e
->src
->index
, e
->dest
->index
,
8497 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8500 if (e
->flags
& EDGE_EXECUTABLE
)
8502 tree arg
= PHI_ARG_DEF (phi
, i
);
8503 value_range_t vr_arg
;
8507 if (TREE_CODE (arg
) == SSA_NAME
)
8509 vr_arg
= *(get_value_range (arg
));
8510 /* Do not allow equivalences or symbolic ranges to leak in from
8511 backedges. That creates invalid equivalencies.
8512 See PR53465 and PR54767. */
8513 if (e
->flags
& EDGE_DFS_BACK
)
8515 if (vr_arg
.type
== VR_RANGE
8516 || vr_arg
.type
== VR_ANTI_RANGE
)
8518 vr_arg
.equiv
= NULL
;
8519 if (symbolic_range_p (&vr_arg
))
8521 vr_arg
.type
= VR_VARYING
;
8522 vr_arg
.min
= NULL_TREE
;
8523 vr_arg
.max
= NULL_TREE
;
8529 /* If the non-backedge arguments range is VR_VARYING then
8530 we can still try recording a simple equivalence. */
8531 if (vr_arg
.type
== VR_VARYING
)
8533 vr_arg
.type
= VR_RANGE
;
8536 vr_arg
.equiv
= NULL
;
8542 if (TREE_OVERFLOW_P (arg
))
8543 arg
= drop_tree_overflow (arg
);
8545 vr_arg
.type
= VR_RANGE
;
8548 vr_arg
.equiv
= NULL
;
8551 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8553 fprintf (dump_file
, "\t");
8554 print_generic_expr (dump_file
, arg
, dump_flags
);
8555 fprintf (dump_file
, ": ");
8556 dump_value_range (dump_file
, &vr_arg
);
8557 fprintf (dump_file
, "\n");
8561 copy_value_range (&vr_result
, &vr_arg
);
8563 vrp_meet (&vr_result
, &vr_arg
);
8566 if (vr_result
.type
== VR_VARYING
)
8571 if (vr_result
.type
== VR_VARYING
)
8573 else if (vr_result
.type
== VR_UNDEFINED
)
8576 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8577 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8579 /* To prevent infinite iterations in the algorithm, derive ranges
8580 when the new value is slightly bigger or smaller than the
8581 previous one. We don't do this if we have seen a new executable
8582 edge; this helps us avoid an overflow infinity for conditionals
8583 which are not in a loop. If the old value-range was VR_UNDEFINED
8584 use the updated range and iterate one more time. */
8586 && gimple_phi_num_args (phi
) > 1
8587 && edges
== old_edges
8588 && lhs_vr
->type
!= VR_UNDEFINED
)
8590 /* Compare old and new ranges, fall back to varying if the
8591 values are not comparable. */
8592 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8595 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8599 /* For non VR_RANGE or for pointers fall back to varying if
8600 the range changed. */
8601 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8602 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8603 && (cmp_min
!= 0 || cmp_max
!= 0))
8606 /* If the new minimum is larger than than the previous one
8607 retain the old value. If the new minimum value is smaller
8608 than the previous one and not -INF go all the way to -INF + 1.
8609 In the first case, to avoid infinite bouncing between different
8610 minimums, and in the other case to avoid iterating millions of
8611 times to reach -INF. Going to -INF + 1 also lets the following
8612 iteration compute whether there will be any overflow, at the
8613 expense of one additional iteration. */
8615 vr_result
.min
= lhs_vr
->min
;
8616 else if (cmp_min
> 0
8617 && !vrp_val_is_min (vr_result
.min
))
8619 = int_const_binop (PLUS_EXPR
,
8620 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8621 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8623 /* Similarly for the maximum value. */
8625 vr_result
.max
= lhs_vr
->max
;
8626 else if (cmp_max
< 0
8627 && !vrp_val_is_max (vr_result
.max
))
8629 = int_const_binop (MINUS_EXPR
,
8630 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8631 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8633 /* If we dropped either bound to +-INF then if this is a loop
8634 PHI node SCEV may known more about its value-range. */
8635 if ((cmp_min
> 0 || cmp_min
< 0
8636 || cmp_max
< 0 || cmp_max
> 0)
8637 && (l
= loop_containing_stmt (phi
))
8638 && l
->header
== gimple_bb (phi
))
8639 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8641 /* If we will end up with a (-INF, +INF) range, set it to
8642 VARYING. Same if the previous max value was invalid for
8643 the type and we end up with vr_result.min > vr_result.max. */
8644 if ((vrp_val_is_max (vr_result
.max
)
8645 && vrp_val_is_min (vr_result
.min
))
8646 || compare_values (vr_result
.min
,
8651 /* If the new range is different than the previous value, keep
8654 if (update_value_range (lhs
, &vr_result
))
8656 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8658 fprintf (dump_file
, "Found new range for ");
8659 print_generic_expr (dump_file
, lhs
, 0);
8660 fprintf (dump_file
, ": ");
8661 dump_value_range (dump_file
, &vr_result
);
8662 fprintf (dump_file
, "\n");
8665 return SSA_PROP_INTERESTING
;
8668 /* Nothing changed, don't add outgoing edges. */
8669 return SSA_PROP_NOT_INTERESTING
;
8671 /* No match found. Set the LHS to VARYING. */
8673 set_value_range_to_varying (lhs_vr
);
8674 return SSA_PROP_VARYING
;
8677 /* Simplify boolean operations if the source is known
8678 to be already a boolean. */
8680 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8682 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8684 bool need_conversion
;
8686 /* We handle only !=/== case here. */
8687 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8689 op0
= gimple_assign_rhs1 (stmt
);
8690 if (!op_with_boolean_value_range_p (op0
))
8693 op1
= gimple_assign_rhs2 (stmt
);
8694 if (!op_with_boolean_value_range_p (op1
))
8697 /* Reduce number of cases to handle to NE_EXPR. As there is no
8698 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8699 if (rhs_code
== EQ_EXPR
)
8701 if (TREE_CODE (op1
) == INTEGER_CST
)
8702 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8703 build_int_cst (TREE_TYPE (op1
), 1));
8708 lhs
= gimple_assign_lhs (stmt
);
8710 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8712 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8714 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8715 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8716 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8719 /* For A != 0 we can substitute A itself. */
8720 if (integer_zerop (op1
))
8721 gimple_assign_set_rhs_with_ops (gsi
,
8723 ? NOP_EXPR
: TREE_CODE (op0
),
8725 /* For A != B we substitute A ^ B. Either with conversion. */
8726 else if (need_conversion
)
8728 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8729 gimple_assign newop
=
8730 gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8731 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8732 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8736 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8737 update_stmt (gsi_stmt (*gsi
));
8742 /* Simplify a division or modulo operator to a right shift or
8743 bitwise and if the first operand is unsigned or is greater
8744 than zero and the second operand is an exact power of two. */
8747 simplify_div_or_mod_using_ranges (gimple stmt
)
8749 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8751 tree op0
= gimple_assign_rhs1 (stmt
);
8752 tree op1
= gimple_assign_rhs2 (stmt
);
8753 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8755 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8757 val
= integer_one_node
;
8763 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8767 && integer_onep (val
)
8768 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8770 location_t location
;
8772 if (!gimple_has_location (stmt
))
8773 location
= input_location
;
8775 location
= gimple_location (stmt
);
8776 warning_at (location
, OPT_Wstrict_overflow
,
8777 "assuming signed overflow does not occur when "
8778 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8782 if (val
&& integer_onep (val
))
8786 if (rhs_code
== TRUNC_DIV_EXPR
)
8788 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8789 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8790 gimple_assign_set_rhs1 (stmt
, op0
);
8791 gimple_assign_set_rhs2 (stmt
, t
);
8795 t
= build_int_cst (TREE_TYPE (op1
), 1);
8796 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8797 t
= fold_convert (TREE_TYPE (op0
), t
);
8799 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8800 gimple_assign_set_rhs1 (stmt
, op0
);
8801 gimple_assign_set_rhs2 (stmt
, t
);
8811 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8812 ABS_EXPR. If the operand is <= 0, then simplify the
8813 ABS_EXPR into a NEGATE_EXPR. */
8816 simplify_abs_using_ranges (gimple stmt
)
8819 tree op
= gimple_assign_rhs1 (stmt
);
8820 tree type
= TREE_TYPE (op
);
8821 value_range_t
*vr
= get_value_range (op
);
8823 if (TYPE_UNSIGNED (type
))
8825 val
= integer_zero_node
;
8831 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8835 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8840 if (integer_zerop (val
))
8841 val
= integer_one_node
;
8842 else if (integer_onep (val
))
8843 val
= integer_zero_node
;
8848 && (integer_onep (val
) || integer_zerop (val
)))
8850 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8852 location_t location
;
8854 if (!gimple_has_location (stmt
))
8855 location
= input_location
;
8857 location
= gimple_location (stmt
);
8858 warning_at (location
, OPT_Wstrict_overflow
,
8859 "assuming signed overflow does not occur when "
8860 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8863 gimple_assign_set_rhs1 (stmt
, op
);
8864 if (integer_onep (val
))
8865 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8867 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8876 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8877 If all the bits that are being cleared by & are already
8878 known to be zero from VR, or all the bits that are being
8879 set by | are already known to be one from VR, the bit
8880 operation is redundant. */
8883 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8885 tree op0
= gimple_assign_rhs1 (stmt
);
8886 tree op1
= gimple_assign_rhs2 (stmt
);
8887 tree op
= NULL_TREE
;
8888 value_range_t vr0
= VR_INITIALIZER
;
8889 value_range_t vr1
= VR_INITIALIZER
;
8890 wide_int may_be_nonzero0
, may_be_nonzero1
;
8891 wide_int must_be_nonzero0
, must_be_nonzero1
;
8894 if (TREE_CODE (op0
) == SSA_NAME
)
8895 vr0
= *(get_value_range (op0
));
8896 else if (is_gimple_min_invariant (op0
))
8897 set_value_range_to_value (&vr0
, op0
, NULL
);
8901 if (TREE_CODE (op1
) == SSA_NAME
)
8902 vr1
= *(get_value_range (op1
));
8903 else if (is_gimple_min_invariant (op1
))
8904 set_value_range_to_value (&vr1
, op1
, NULL
);
8908 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
8911 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
8915 switch (gimple_assign_rhs_code (stmt
))
8918 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8924 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8932 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8938 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8949 if (op
== NULL_TREE
)
8952 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8953 update_stmt (gsi_stmt (*gsi
));
8957 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8958 a known value range VR.
8960 If there is one and only one value which will satisfy the
8961 conditional, then return that value. Else return NULL. */
8964 test_for_singularity (enum tree_code cond_code
, tree op0
,
8965 tree op1
, value_range_t
*vr
)
8970 /* Extract minimum/maximum values which satisfy the
8971 the conditional as it was written. */
8972 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8974 /* This should not be negative infinity; there is no overflow
8976 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8979 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8981 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8982 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8984 TREE_NO_WARNING (max
) = 1;
8987 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8989 /* This should not be positive infinity; there is no overflow
8991 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8994 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8996 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8997 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8999 TREE_NO_WARNING (min
) = 1;
9003 /* Now refine the minimum and maximum values using any
9004 value range information we have for op0. */
9007 if (compare_values (vr
->min
, min
) == 1)
9009 if (compare_values (vr
->max
, max
) == -1)
9012 /* If the new min/max values have converged to a single value,
9013 then there is only one value which can satisfy the condition,
9014 return that value. */
9015 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9021 /* Return whether the value range *VR fits in an integer type specified
9022 by PRECISION and UNSIGNED_P. */
9025 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
9028 unsigned src_precision
;
9032 /* We can only handle integral and pointer types. */
9033 src_type
= TREE_TYPE (vr
->min
);
9034 if (!INTEGRAL_TYPE_P (src_type
)
9035 && !POINTER_TYPE_P (src_type
))
9038 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9039 and so is an identity transform. */
9040 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9041 src_sgn
= TYPE_SIGN (src_type
);
9042 if ((src_precision
< dest_precision
9043 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9044 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9047 /* Now we can only handle ranges with constant bounds. */
9048 if (vr
->type
!= VR_RANGE
9049 || TREE_CODE (vr
->min
) != INTEGER_CST
9050 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9053 /* For sign changes, the MSB of the wide_int has to be clear.
9054 An unsigned value with its MSB set cannot be represented by
9055 a signed wide_int, while a negative value cannot be represented
9056 by an unsigned wide_int. */
9057 if (src_sgn
!= dest_sgn
9058 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9061 /* Then we can perform the conversion on both ends and compare
9062 the result for equality. */
9063 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9064 if (tem
!= wi::to_widest (vr
->min
))
9066 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9067 if (tem
!= wi::to_widest (vr
->max
))
9073 /* Simplify a conditional using a relational operator to an equality
9074 test if the range information indicates only one value can satisfy
9075 the original conditional. */
9078 simplify_cond_using_ranges (gimple_cond stmt
)
9080 tree op0
= gimple_cond_lhs (stmt
);
9081 tree op1
= gimple_cond_rhs (stmt
);
9082 enum tree_code cond_code
= gimple_cond_code (stmt
);
9084 if (cond_code
!= NE_EXPR
9085 && cond_code
!= EQ_EXPR
9086 && TREE_CODE (op0
) == SSA_NAME
9087 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9088 && is_gimple_min_invariant (op1
))
9090 value_range_t
*vr
= get_value_range (op0
);
9092 /* If we have range information for OP0, then we might be
9093 able to simplify this conditional. */
9094 if (vr
->type
== VR_RANGE
)
9096 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9102 fprintf (dump_file
, "Simplified relational ");
9103 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9104 fprintf (dump_file
, " into ");
9107 gimple_cond_set_code (stmt
, EQ_EXPR
);
9108 gimple_cond_set_lhs (stmt
, op0
);
9109 gimple_cond_set_rhs (stmt
, new_tree
);
9115 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9116 fprintf (dump_file
, "\n");
9122 /* Try again after inverting the condition. We only deal
9123 with integral types here, so no need to worry about
9124 issues with inverting FP comparisons. */
9125 cond_code
= invert_tree_comparison (cond_code
, false);
9126 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9132 fprintf (dump_file
, "Simplified relational ");
9133 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9134 fprintf (dump_file
, " into ");
9137 gimple_cond_set_code (stmt
, NE_EXPR
);
9138 gimple_cond_set_lhs (stmt
, op0
);
9139 gimple_cond_set_rhs (stmt
, new_tree
);
9145 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9146 fprintf (dump_file
, "\n");
9154 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9155 see if OP0 was set by a type conversion where the source of
9156 the conversion is another SSA_NAME with a range that fits
9157 into the range of OP0's type.
9159 If so, the conversion is redundant as the earlier SSA_NAME can be
9160 used for the comparison directly if we just massage the constant in the
9162 if (TREE_CODE (op0
) == SSA_NAME
9163 && TREE_CODE (op1
) == INTEGER_CST
)
9165 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
9168 if (!is_gimple_assign (def_stmt
)
9169 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9172 innerop
= gimple_assign_rhs1 (def_stmt
);
9174 if (TREE_CODE (innerop
) == SSA_NAME
9175 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9177 value_range_t
*vr
= get_value_range (innerop
);
9179 if (range_int_cst_p (vr
)
9180 && range_fits_type_p (vr
,
9181 TYPE_PRECISION (TREE_TYPE (op0
)),
9182 TYPE_SIGN (TREE_TYPE (op0
)))
9183 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9184 /* The range must not have overflowed, or if it did overflow
9185 we must not be wrapping/trapping overflow and optimizing
9186 with strict overflow semantics. */
9187 && ((!is_negative_overflow_infinity (vr
->min
)
9188 && !is_positive_overflow_infinity (vr
->max
))
9189 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9191 /* If the range overflowed and the user has asked for warnings
9192 when strict overflow semantics were used to optimize code,
9193 issue an appropriate warning. */
9194 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9195 && (is_negative_overflow_infinity (vr
->min
)
9196 || is_positive_overflow_infinity (vr
->max
))
9197 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9199 location_t location
;
9201 if (!gimple_has_location (stmt
))
9202 location
= input_location
;
9204 location
= gimple_location (stmt
);
9205 warning_at (location
, OPT_Wstrict_overflow
,
9206 "assuming signed overflow does not occur when "
9207 "simplifying conditional");
9210 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9211 gimple_cond_set_lhs (stmt
, innerop
);
9212 gimple_cond_set_rhs (stmt
, newconst
);
9221 /* Simplify a switch statement using the value range of the switch
9225 simplify_switch_using_ranges (gimple_switch stmt
)
9227 tree op
= gimple_switch_index (stmt
);
9232 size_t i
= 0, j
= 0, n
, n2
;
9235 size_t k
= 1, l
= 0;
9237 if (TREE_CODE (op
) == SSA_NAME
)
9239 vr
= get_value_range (op
);
9241 /* We can only handle integer ranges. */
9242 if ((vr
->type
!= VR_RANGE
9243 && vr
->type
!= VR_ANTI_RANGE
)
9244 || symbolic_range_p (vr
))
9247 /* Find case label for min/max of the value range. */
9248 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9250 else if (TREE_CODE (op
) == INTEGER_CST
)
9252 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9266 n
= gimple_switch_num_labels (stmt
);
9268 /* Bail out if this is just all edges taken. */
9274 /* Build a new vector of taken case labels. */
9275 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9278 /* Add the default edge, if necessary. */
9280 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9282 for (; i
<= j
; ++i
, ++n2
)
9283 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9285 for (; k
<= l
; ++k
, ++n2
)
9286 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9288 /* Mark needed edges. */
9289 for (i
= 0; i
< n2
; ++i
)
9291 e
= find_edge (gimple_bb (stmt
),
9292 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9293 e
->aux
= (void *)-1;
9296 /* Queue not needed edges for later removal. */
9297 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9299 if (e
->aux
== (void *)-1)
9305 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9307 fprintf (dump_file
, "removing unreachable case label\n");
9309 to_remove_edges
.safe_push (e
);
9310 e
->flags
&= ~EDGE_EXECUTABLE
;
9313 /* And queue an update for the stmt. */
9316 to_update_switch_stmts
.safe_push (su
);
9320 /* Simplify an integral conversion from an SSA name in STMT. */
9323 simplify_conversion_using_ranges (gimple stmt
)
9325 tree innerop
, middleop
, finaltype
;
9327 value_range_t
*innervr
;
9328 signop inner_sgn
, middle_sgn
, final_sgn
;
9329 unsigned inner_prec
, middle_prec
, final_prec
;
9330 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9332 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9333 if (!INTEGRAL_TYPE_P (finaltype
))
9335 middleop
= gimple_assign_rhs1 (stmt
);
9336 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9337 if (!is_gimple_assign (def_stmt
)
9338 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9340 innerop
= gimple_assign_rhs1 (def_stmt
);
9341 if (TREE_CODE (innerop
) != SSA_NAME
9342 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9345 /* Get the value-range of the inner operand. */
9346 innervr
= get_value_range (innerop
);
9347 if (innervr
->type
!= VR_RANGE
9348 || TREE_CODE (innervr
->min
) != INTEGER_CST
9349 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9352 /* Simulate the conversion chain to check if the result is equal if
9353 the middle conversion is removed. */
9354 innermin
= wi::to_widest (innervr
->min
);
9355 innermax
= wi::to_widest (innervr
->max
);
9357 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9358 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9359 final_prec
= TYPE_PRECISION (finaltype
);
9361 /* If the first conversion is not injective, the second must not
9363 if (wi::gtu_p (innermax
- innermin
,
9364 wi::mask
<widest_int
> (middle_prec
, false))
9365 && middle_prec
< final_prec
)
9367 /* We also want a medium value so that we can track the effect that
9368 narrowing conversions with sign change have. */
9369 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9370 if (inner_sgn
== UNSIGNED
)
9371 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9374 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9375 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9376 innermed
= innermin
;
9378 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9379 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9380 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9381 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9383 /* Require that the final conversion applied to both the original
9384 and the intermediate range produces the same result. */
9385 final_sgn
= TYPE_SIGN (finaltype
);
9386 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9387 != wi::ext (innermin
, final_prec
, final_sgn
)
9388 || wi::ext (middlemed
, final_prec
, final_sgn
)
9389 != wi::ext (innermed
, final_prec
, final_sgn
)
9390 || wi::ext (middlemax
, final_prec
, final_sgn
)
9391 != wi::ext (innermax
, final_prec
, final_sgn
))
9394 gimple_assign_set_rhs1 (stmt
, innerop
);
9399 /* Simplify a conversion from integral SSA name to float in STMT. */
9402 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9404 tree rhs1
= gimple_assign_rhs1 (stmt
);
9405 value_range_t
*vr
= get_value_range (rhs1
);
9406 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9407 enum machine_mode mode
;
9411 /* We can only handle constant ranges. */
9412 if (vr
->type
!= VR_RANGE
9413 || TREE_CODE (vr
->min
) != INTEGER_CST
9414 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9417 /* First check if we can use a signed type in place of an unsigned. */
9418 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9419 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9420 != CODE_FOR_nothing
)
9421 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9422 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9423 /* If we can do the conversion in the current input mode do nothing. */
9424 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9425 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9427 /* Otherwise search for a mode we can use, starting from the narrowest
9428 integer mode available. */
9431 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9434 /* If we cannot do a signed conversion to float from mode
9435 or if the value-range does not fit in the signed type
9436 try with a wider mode. */
9437 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9438 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9441 mode
= GET_MODE_WIDER_MODE (mode
);
9442 /* But do not widen the input. Instead leave that to the
9443 optabs expansion code. */
9444 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9447 while (mode
!= VOIDmode
);
9448 if (mode
== VOIDmode
)
9452 /* It works, insert a truncation or sign-change before the
9453 float conversion. */
9454 tem
= make_ssa_name (build_nonstandard_integer_type
9455 (GET_MODE_PRECISION (mode
), 0), NULL
);
9456 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9457 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9458 gimple_assign_set_rhs1 (stmt
, tem
);
9464 /* Simplify an internal fn call using ranges if possible. */
9467 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9469 enum tree_code subcode
;
9470 switch (gimple_call_internal_fn (stmt
))
9472 case IFN_UBSAN_CHECK_ADD
:
9473 subcode
= PLUS_EXPR
;
9475 case IFN_UBSAN_CHECK_SUB
:
9476 subcode
= MINUS_EXPR
;
9478 case IFN_UBSAN_CHECK_MUL
:
9479 subcode
= MULT_EXPR
;
9485 value_range_t vr0
= VR_INITIALIZER
;
9486 value_range_t vr1
= VR_INITIALIZER
;
9487 tree op0
= gimple_call_arg (stmt
, 0);
9488 tree op1
= gimple_call_arg (stmt
, 1);
9490 if (TREE_CODE (op0
) == SSA_NAME
)
9491 vr0
= *get_value_range (op0
);
9492 else if (TREE_CODE (op0
) == INTEGER_CST
)
9493 set_value_range_to_value (&vr0
, op0
, NULL
);
9495 set_value_range_to_varying (&vr0
);
9497 if (TREE_CODE (op1
) == SSA_NAME
)
9498 vr1
= *get_value_range (op1
);
9499 else if (TREE_CODE (op1
) == INTEGER_CST
)
9500 set_value_range_to_value (&vr1
, op1
, NULL
);
9502 set_value_range_to_varying (&vr1
);
9504 if (!range_int_cst_p (&vr0
))
9506 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9507 optimize at least x = y + 0; x = y - 0; x = y * 0;
9508 and x = y * 1; which never overflow. */
9509 if (!range_int_cst_p (&vr1
))
9511 if (tree_int_cst_sgn (vr1
.min
) == -1)
9513 if (compare_tree_int (vr1
.max
, subcode
== MULT_EXPR
) == 1)
9516 else if (!range_int_cst_p (&vr1
))
9518 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9519 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9520 which never overflow. */
9521 if (subcode
== MINUS_EXPR
)
9523 if (!range_int_cst_p (&vr0
))
9525 if (tree_int_cst_sgn (vr0
.min
) == -1)
9527 if (compare_tree_int (vr0
.max
, subcode
== MULT_EXPR
) == 1)
9532 tree r1
= int_const_binop (subcode
, vr0
.min
, vr1
.min
);
9533 tree r2
= int_const_binop (subcode
, vr0
.max
, vr1
.max
);
9534 if (r1
== NULL_TREE
|| TREE_OVERFLOW (r1
)
9535 || r2
== NULL_TREE
|| TREE_OVERFLOW (r2
))
9537 if (subcode
== MULT_EXPR
)
9539 tree r3
= int_const_binop (subcode
, vr0
.min
, vr1
.max
);
9540 tree r4
= int_const_binop (subcode
, vr0
.max
, vr1
.min
);
9541 if (r3
== NULL_TREE
|| TREE_OVERFLOW (r3
)
9542 || r4
== NULL_TREE
|| TREE_OVERFLOW (r4
))
9547 gimple g
= gimple_build_assign_with_ops (subcode
, gimple_call_lhs (stmt
),
9549 gsi_replace (gsi
, g
, false);
9553 /* Simplify STMT using ranges if possible. */
9556 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9558 gimple stmt
= gsi_stmt (*gsi
);
9559 if (is_gimple_assign (stmt
))
9561 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9562 tree rhs1
= gimple_assign_rhs1 (stmt
);
9568 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9569 if the RHS is zero or one, and the LHS are known to be boolean
9571 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9572 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9575 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9576 and BIT_AND_EXPR respectively if the first operand is greater
9577 than zero and the second operand is an exact power of two. */
9578 case TRUNC_DIV_EXPR
:
9579 case TRUNC_MOD_EXPR
:
9580 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9581 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9582 return simplify_div_or_mod_using_ranges (stmt
);
9585 /* Transform ABS (X) into X or -X as appropriate. */
9587 if (TREE_CODE (rhs1
) == SSA_NAME
9588 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9589 return simplify_abs_using_ranges (stmt
);
9594 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9595 if all the bits being cleared are already cleared or
9596 all the bits being set are already set. */
9597 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9598 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9602 if (TREE_CODE (rhs1
) == SSA_NAME
9603 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9604 return simplify_conversion_using_ranges (stmt
);
9608 if (TREE_CODE (rhs1
) == SSA_NAME
9609 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9610 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9617 else if (gimple_code (stmt
) == GIMPLE_COND
)
9618 return simplify_cond_using_ranges (as_a
<gimple_cond
> (stmt
));
9619 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9620 return simplify_switch_using_ranges (as_a
<gimple_switch
> (stmt
));
9621 else if (is_gimple_call (stmt
)
9622 && gimple_call_internal_p (stmt
))
9623 return simplify_internal_call_using_ranges (gsi
, stmt
);
9628 /* If the statement pointed by SI has a predicate whose value can be
9629 computed using the value range information computed by VRP, compute
9630 its value and return true. Otherwise, return false. */
9633 fold_predicate_in (gimple_stmt_iterator
*si
)
9635 bool assignment_p
= false;
9637 gimple stmt
= gsi_stmt (*si
);
9639 if (is_gimple_assign (stmt
)
9640 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9642 assignment_p
= true;
9643 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9644 gimple_assign_rhs1 (stmt
),
9645 gimple_assign_rhs2 (stmt
),
9648 else if (gimple_cond cond_stmt
= dyn_cast
<gimple_cond
> (stmt
))
9649 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
9650 gimple_cond_lhs (cond_stmt
),
9651 gimple_cond_rhs (cond_stmt
),
9659 val
= fold_convert (gimple_expr_type (stmt
), val
);
9663 fprintf (dump_file
, "Folding predicate ");
9664 print_gimple_expr (dump_file
, stmt
, 0, 0);
9665 fprintf (dump_file
, " to ");
9666 print_generic_expr (dump_file
, val
, 0);
9667 fprintf (dump_file
, "\n");
9670 if (is_gimple_assign (stmt
))
9671 gimple_assign_set_rhs_from_tree (si
, val
);
9674 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9675 gimple_cond cond_stmt
= as_a
<gimple_cond
> (stmt
);
9676 if (integer_zerop (val
))
9677 gimple_cond_make_false (cond_stmt
);
9678 else if (integer_onep (val
))
9679 gimple_cond_make_true (cond_stmt
);
9690 /* Callback for substitute_and_fold folding the stmt at *SI. */
9693 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9695 if (fold_predicate_in (si
))
9698 return simplify_stmt_using_ranges (si
);
9701 /* Stack of dest,src equivalency pairs that need to be restored after
9702 each attempt to thread a block's incoming edge to an outgoing edge.
9704 A NULL entry is used to mark the end of pairs which need to be
9706 static vec
<tree
> equiv_stack
;
9708 /* A trivial wrapper so that we can present the generic jump threading
9709 code with a simple API for simplifying statements. STMT is the
9710 statement we want to simplify, WITHIN_STMT provides the location
9711 for any overflow warnings. */
9714 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9716 if (gimple_cond cond_stmt
= dyn_cast
<gimple_cond
> (stmt
))
9717 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
9718 gimple_cond_lhs (cond_stmt
),
9719 gimple_cond_rhs (cond_stmt
),
9722 if (gimple_assign assign_stmt
= dyn_cast
<gimple_assign
> (stmt
))
9724 value_range_t new_vr
= VR_INITIALIZER
;
9725 tree lhs
= gimple_assign_lhs (assign_stmt
);
9727 if (TREE_CODE (lhs
) == SSA_NAME
9728 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9729 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9731 extract_range_from_assignment (&new_vr
, assign_stmt
);
9732 if (range_int_cst_singleton_p (&new_vr
))
9740 /* Blocks which have more than one predecessor and more than
9741 one successor present jump threading opportunities, i.e.,
9742 when the block is reached from a specific predecessor, we
9743 may be able to determine which of the outgoing edges will
9744 be traversed. When this optimization applies, we are able
9745 to avoid conditionals at runtime and we may expose secondary
9746 optimization opportunities.
9748 This routine is effectively a driver for the generic jump
9749 threading code. It basically just presents the generic code
9750 with edges that may be suitable for jump threading.
9752 Unlike DOM, we do not iterate VRP if jump threading was successful.
9753 While iterating may expose new opportunities for VRP, it is expected
9754 those opportunities would be very limited and the compile time cost
9755 to expose those opportunities would be significant.
9757 As jump threading opportunities are discovered, they are registered
9758 for later realization. */
9761 identify_jump_threads (void)
9768 /* Ugh. When substituting values earlier in this pass we can
9769 wipe the dominance information. So rebuild the dominator
9770 information as we need it within the jump threading code. */
9771 calculate_dominance_info (CDI_DOMINATORS
);
9773 /* We do not allow VRP information to be used for jump threading
9774 across a back edge in the CFG. Otherwise it becomes too
9775 difficult to avoid eliminating loop exit tests. Of course
9776 EDGE_DFS_BACK is not accurate at this time so we have to
9778 mark_dfs_back_edges ();
9780 /* Do not thread across edges we are about to remove. Just marking
9781 them as EDGE_DFS_BACK will do. */
9782 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9783 e
->flags
|= EDGE_DFS_BACK
;
9785 /* Allocate our unwinder stack to unwind any temporary equivalences
9786 that might be recorded. */
9787 equiv_stack
.create (20);
9789 /* To avoid lots of silly node creation, we create a single
9790 conditional and just modify it in-place when attempting to
9792 dummy
= gimple_build_cond (EQ_EXPR
,
9793 integer_zero_node
, integer_zero_node
,
9796 /* Walk through all the blocks finding those which present a
9797 potential jump threading opportunity. We could set this up
9798 as a dominator walker and record data during the walk, but
9799 I doubt it's worth the effort for the classes of jump
9800 threading opportunities we are trying to identify at this
9801 point in compilation. */
9802 FOR_EACH_BB_FN (bb
, cfun
)
9806 /* If the generic jump threading code does not find this block
9807 interesting, then there is nothing to do. */
9808 if (! potentially_threadable_block (bb
))
9811 /* We only care about blocks ending in a COND_EXPR. While there
9812 may be some value in handling SWITCH_EXPR here, I doubt it's
9813 terribly important. */
9814 last
= gsi_stmt (gsi_last_bb (bb
));
9816 /* We're basically looking for a switch or any kind of conditional with
9817 integral or pointer type arguments. Note the type of the second
9818 argument will be the same as the first argument, so no need to
9819 check it explicitly. */
9820 if (gimple_code (last
) == GIMPLE_SWITCH
9821 || (gimple_code (last
) == GIMPLE_COND
9822 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9823 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9824 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9825 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9826 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9830 /* We've got a block with multiple predecessors and multiple
9831 successors which also ends in a suitable conditional or
9832 switch statement. For each predecessor, see if we can thread
9833 it to a specific successor. */
9834 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9836 /* Do not thread across back edges or abnormal edges
9838 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9841 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9842 simplify_stmt_for_jump_threading
);
9847 /* We do not actually update the CFG or SSA graphs at this point as
9848 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9849 handle ASSERT_EXPRs gracefully. */
9852 /* We identified all the jump threading opportunities earlier, but could
9853 not transform the CFG at that time. This routine transforms the
9854 CFG and arranges for the dominator tree to be rebuilt if necessary.
9856 Note the SSA graph update will occur during the normal TODO
9857 processing by the pass manager. */
9859 finalize_jump_threads (void)
9861 thread_through_all_blocks (false);
9862 equiv_stack
.release ();
9866 /* Traverse all the blocks folding conditionals with known ranges. */
9873 values_propagated
= true;
9877 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9878 dump_all_value_ranges (dump_file
);
9879 fprintf (dump_file
, "\n");
9882 substitute_and_fold (op_with_constant_singleton_value_range
,
9883 vrp_fold_stmt
, false);
9885 if (warn_array_bounds
)
9886 check_all_array_refs ();
9888 /* We must identify jump threading opportunities before we release
9889 the datastructures built by VRP. */
9890 identify_jump_threads ();
9892 /* Set value range to non pointer SSA_NAMEs. */
9893 for (i
= 0; i
< num_vr_values
; i
++)
9896 tree name
= ssa_name (i
);
9899 || POINTER_TYPE_P (TREE_TYPE (name
))
9900 || (vr_value
[i
]->type
== VR_VARYING
)
9901 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9904 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9905 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
9906 && (vr_value
[i
]->type
== VR_RANGE
9907 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
9908 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
9912 /* Free allocated memory. */
9913 for (i
= 0; i
< num_vr_values
; i
++)
9916 BITMAP_FREE (vr_value
[i
]->equiv
);
9921 free (vr_phi_edge_counts
);
9923 /* So that we can distinguish between VRP data being available
9924 and not available. */
9926 vr_phi_edge_counts
= NULL
;
9930 /* Main entry point to VRP (Value Range Propagation). This pass is
9931 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9932 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9933 Programming Language Design and Implementation, pp. 67-78, 1995.
9934 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9936 This is essentially an SSA-CCP pass modified to deal with ranges
9937 instead of constants.
9939 While propagating ranges, we may find that two or more SSA name
9940 have equivalent, though distinct ranges. For instance,
9943 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9945 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9949 In the code above, pointer p_5 has range [q_2, q_2], but from the
9950 code we can also determine that p_5 cannot be NULL and, if q_2 had
9951 a non-varying range, p_5's range should also be compatible with it.
9953 These equivalences are created by two expressions: ASSERT_EXPR and
9954 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9955 result of another assertion, then we can use the fact that p_5 and
9956 p_4 are equivalent when evaluating p_5's range.
9958 Together with value ranges, we also propagate these equivalences
9959 between names so that we can take advantage of information from
9960 multiple ranges when doing final replacement. Note that this
9961 equivalency relation is transitive but not symmetric.
9963 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9964 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9965 in contexts where that assertion does not hold (e.g., in line 6).
9967 TODO, the main difference between this pass and Patterson's is that
9968 we do not propagate edge probabilities. We only compute whether
9969 edges can be taken or not. That is, instead of having a spectrum
9970 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9971 DON'T KNOW. In the future, it may be worthwhile to propagate
9972 probabilities to aid branch prediction. */
9981 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9982 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9985 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9986 Inserting assertions may split edges which will invalidate
9988 insert_range_assertions ();
9990 to_remove_edges
.create (10);
9991 to_update_switch_stmts
.create (5);
9992 threadedge_initialize_values ();
9994 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9995 mark_dfs_back_edges ();
9998 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
10001 free_numbers_of_iterations_estimates ();
10003 /* ASSERT_EXPRs must be removed before finalizing jump threads
10004 as finalizing jump threads calls the CFG cleanup code which
10005 does not properly handle ASSERT_EXPRs. */
10006 remove_range_assertions ();
10008 /* If we exposed any new variables, go ahead and put them into
10009 SSA form now, before we handle jump threading. This simplifies
10010 interactions between rewriting of _DECL nodes into SSA form
10011 and rewriting SSA_NAME nodes into SSA form after block
10012 duplication and CFG manipulation. */
10013 update_ssa (TODO_update_ssa
);
10015 finalize_jump_threads ();
10017 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10018 CFG in a broken state and requires a cfg_cleanup run. */
10019 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10021 /* Update SWITCH_EXPR case label vector. */
10022 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
10025 size_t n
= TREE_VEC_LENGTH (su
->vec
);
10027 gimple_switch_set_num_labels (su
->stmt
, n
);
10028 for (j
= 0; j
< n
; j
++)
10029 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
10030 /* As we may have replaced the default label with a regular one
10031 make sure to make it a real default label again. This ensures
10032 optimal expansion. */
10033 label
= gimple_switch_label (su
->stmt
, 0);
10034 CASE_LOW (label
) = NULL_TREE
;
10035 CASE_HIGH (label
) = NULL_TREE
;
10038 if (to_remove_edges
.length () > 0)
10040 free_dominance_info (CDI_DOMINATORS
);
10041 loops_state_set (LOOPS_NEED_FIXUP
);
10044 to_remove_edges
.release ();
10045 to_update_switch_stmts
.release ();
10046 threadedge_finalize_values ();
10049 loop_optimizer_finalize ();
10055 const pass_data pass_data_vrp
=
10057 GIMPLE_PASS
, /* type */
10059 OPTGROUP_NONE
, /* optinfo_flags */
10060 TV_TREE_VRP
, /* tv_id */
10061 PROP_ssa
, /* properties_required */
10062 0, /* properties_provided */
10063 0, /* properties_destroyed */
10064 0, /* todo_flags_start */
10065 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10068 class pass_vrp
: public gimple_opt_pass
10071 pass_vrp (gcc::context
*ctxt
)
10072 : gimple_opt_pass (pass_data_vrp
, ctxt
)
10075 /* opt_pass methods: */
10076 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10077 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10078 virtual unsigned int execute (function
*) { return execute_vrp (); }
10080 }; // class pass_vrp
10082 } // anon namespace
10085 make_pass_vrp (gcc::context
*ctxt
)
10087 return new pass_vrp (ctxt
);