1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2017 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"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-fold.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.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"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
58 #include "omp-general.h"
60 #include "case-cfn-macros.h"
62 #include "alloc-pool.h"
64 #include "tree-cfgcleanup.h"
66 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
68 /* Allocation pools for tree-vrp allocations. */
69 static object_allocator
<value_range
> vrp_value_range_pool ("Tree VRP value ranges");
70 static bitmap_obstack vrp_equiv_obstack
;
72 /* Set of SSA names found live during the RPO traversal of the function
73 for still active basic-blocks. */
76 /* Return true if the SSA name NAME is live on the edge E. */
79 live_on_edge (edge e
, tree name
)
81 return (live
[e
->dest
->index
]
82 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
85 /* Local functions. */
86 static int compare_values (tree val1
, tree val2
);
87 static int compare_values_warnv (tree val1
, tree val2
, bool *);
88 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
89 tree
, tree
, bool, bool *,
94 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
95 enum tree_code comp_code
;
97 /* Name to register the assert for. */
100 /* Value being compared against. */
103 /* Expression to compare. */
107 /* Location information for ASSERT_EXPRs. Each instance of this
108 structure describes an ASSERT_EXPR for an SSA name. Since a single
109 SSA name may have more than one assertion associated with it, these
110 locations are kept in a linked list attached to the corresponding
114 /* Basic block where the assertion would be inserted. */
117 /* Some assertions need to be inserted on an edge (e.g., assertions
118 generated by COND_EXPRs). In those cases, BB will be NULL. */
121 /* Pointer to the statement that generated this assertion. */
122 gimple_stmt_iterator si
;
124 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
125 enum tree_code comp_code
;
127 /* Value being compared against. */
130 /* Expression to compare. */
133 /* Next node in the linked list. */
137 /* If bit I is present, it means that SSA name N_i has a list of
138 assertions that should be inserted in the IL. */
139 static bitmap need_assert_for
;
141 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
142 holds a list of ASSERT_LOCUS_T nodes that describe where
143 ASSERT_EXPRs for SSA name N_I should be inserted. */
144 static assert_locus
**asserts_for
;
146 /* Value range array. After propagation, VR_VALUE[I] holds the range
147 of values that SSA name N_I may take. */
148 static unsigned num_vr_values
;
149 static value_range
**vr_value
;
150 static bool values_propagated
;
152 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
153 number of executable edges we saw the last time we visited the
155 static int *vr_phi_edge_counts
;
157 struct switch_update
{
162 static vec
<edge
> to_remove_edges
;
163 static vec
<switch_update
> to_update_switch_stmts
;
166 /* Return the maximum value for TYPE. */
169 vrp_val_max (const_tree type
)
171 if (!INTEGRAL_TYPE_P (type
))
174 return TYPE_MAX_VALUE (type
);
177 /* Return the minimum value for TYPE. */
180 vrp_val_min (const_tree type
)
182 if (!INTEGRAL_TYPE_P (type
))
185 return TYPE_MIN_VALUE (type
);
188 /* Return whether VAL is equal to the maximum value of its type. This
189 will be true for a positive overflow infinity. We can't do a
190 simple equality comparison with TYPE_MAX_VALUE because C typedefs
191 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
192 to the integer constant with the same value in the type. */
195 vrp_val_is_max (const_tree val
)
197 tree type_max
= vrp_val_max (TREE_TYPE (val
));
198 return (val
== type_max
199 || (type_max
!= NULL_TREE
200 && operand_equal_p (val
, type_max
, 0)));
203 /* Return whether VAL is equal to the minimum value of its type. This
204 will be true for a negative overflow infinity. */
207 vrp_val_is_min (const_tree val
)
209 tree type_min
= vrp_val_min (TREE_TYPE (val
));
210 return (val
== type_min
211 || (type_min
!= NULL_TREE
212 && operand_equal_p (val
, type_min
, 0)));
216 /* Set value range VR to VR_UNDEFINED. */
219 set_value_range_to_undefined (value_range
*vr
)
221 vr
->type
= VR_UNDEFINED
;
222 vr
->min
= vr
->max
= NULL_TREE
;
224 bitmap_clear (vr
->equiv
);
228 /* Set value range VR to VR_VARYING. */
231 set_value_range_to_varying (value_range
*vr
)
233 vr
->type
= VR_VARYING
;
234 vr
->min
= vr
->max
= NULL_TREE
;
236 bitmap_clear (vr
->equiv
);
240 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
243 set_value_range (value_range
*vr
, enum value_range_type t
, tree min
,
244 tree max
, bitmap equiv
)
246 /* Check the validity of the range. */
248 && (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
))
252 gcc_assert (min
&& max
);
254 gcc_assert (!TREE_OVERFLOW_P (min
) && !TREE_OVERFLOW_P (max
));
256 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
257 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
259 cmp
= compare_values (min
, max
);
260 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
264 && (t
== VR_UNDEFINED
|| t
== VR_VARYING
))
266 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
267 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
274 /* Since updating the equivalence set involves deep copying the
275 bitmaps, only do it if absolutely necessary. */
276 if (vr
->equiv
== NULL
278 vr
->equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
280 if (equiv
!= vr
->equiv
)
282 if (equiv
&& !bitmap_empty_p (equiv
))
283 bitmap_copy (vr
->equiv
, equiv
);
285 bitmap_clear (vr
->equiv
);
290 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
291 This means adjusting T, MIN and MAX representing the case of a
292 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
293 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
294 In corner cases where MAX+1 or MIN-1 wraps this will fall back
296 This routine exists to ease canonicalization in the case where we
297 extract ranges from var + CST op limit. */
300 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
301 tree min
, tree max
, bitmap equiv
)
303 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
304 if (t
== VR_UNDEFINED
)
306 set_value_range_to_undefined (vr
);
309 else if (t
== VR_VARYING
)
311 set_value_range_to_varying (vr
);
315 /* Nothing to canonicalize for symbolic ranges. */
316 if (TREE_CODE (min
) != INTEGER_CST
317 || TREE_CODE (max
) != INTEGER_CST
)
319 set_value_range (vr
, t
, min
, max
, equiv
);
323 /* Wrong order for min and max, to swap them and the VR type we need
325 if (tree_int_cst_lt (max
, min
))
329 /* For one bit precision if max < min, then the swapped
330 range covers all values, so for VR_RANGE it is varying and
331 for VR_ANTI_RANGE empty range, so drop to varying as well. */
332 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
334 set_value_range_to_varying (vr
);
338 one
= build_int_cst (TREE_TYPE (min
), 1);
339 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
340 max
= int_const_binop (MINUS_EXPR
, min
, one
);
343 /* There's one corner case, if we had [C+1, C] before we now have
344 that again. But this represents an empty value range, so drop
345 to varying in this case. */
346 if (tree_int_cst_lt (max
, min
))
348 set_value_range_to_varying (vr
);
352 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
355 /* Anti-ranges that can be represented as ranges should be so. */
356 if (t
== VR_ANTI_RANGE
)
358 bool is_min
= vrp_val_is_min (min
);
359 bool is_max
= vrp_val_is_max (max
);
361 if (is_min
&& is_max
)
363 /* We cannot deal with empty ranges, drop to varying.
364 ??? This could be VR_UNDEFINED instead. */
365 set_value_range_to_varying (vr
);
368 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
369 && (is_min
|| is_max
))
371 /* Non-empty boolean ranges can always be represented
372 as a singleton range. */
374 min
= max
= vrp_val_max (TREE_TYPE (min
));
376 min
= max
= vrp_val_min (TREE_TYPE (min
));
380 /* As a special exception preserve non-null ranges. */
381 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
382 && integer_zerop (max
)))
384 tree one
= build_int_cst (TREE_TYPE (max
), 1);
385 min
= int_const_binop (PLUS_EXPR
, max
, one
);
386 max
= vrp_val_max (TREE_TYPE (max
));
391 tree one
= build_int_cst (TREE_TYPE (min
), 1);
392 max
= int_const_binop (MINUS_EXPR
, min
, one
);
393 min
= vrp_val_min (TREE_TYPE (min
));
398 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
399 to make sure VRP iteration terminates, otherwise we can get into
402 set_value_range (vr
, t
, min
, max
, equiv
);
405 /* Copy value range FROM into value range TO. */
408 copy_value_range (value_range
*to
, value_range
*from
)
410 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
413 /* Set value range VR to a single value. This function is only called
414 with values we get from statements, and exists to clear the
415 TREE_OVERFLOW flag so that we don't think we have an overflow
416 infinity when we shouldn't. */
419 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
421 gcc_assert (is_gimple_min_invariant (val
));
422 if (TREE_OVERFLOW_P (val
))
423 val
= drop_tree_overflow (val
);
424 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
427 /* Set value range VR to a non-negative range of type TYPE.
428 OVERFLOW_INFINITY indicates whether to use an overflow infinity
429 rather than TYPE_MAX_VALUE; this should be true if we determine
430 that the range is nonnegative based on the assumption that signed
431 overflow does not occur. */
434 set_value_range_to_nonnegative (value_range
*vr
, tree type
)
436 tree zero
= build_int_cst (type
, 0);
437 set_value_range (vr
, VR_RANGE
, zero
, vrp_val_max (type
), vr
->equiv
);
440 /* Set value range VR to a non-NULL range of type TYPE. */
443 set_value_range_to_nonnull (value_range
*vr
, tree type
)
445 tree zero
= build_int_cst (type
, 0);
446 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
450 /* Set value range VR to a NULL range of type TYPE. */
453 set_value_range_to_null (value_range
*vr
, tree type
)
455 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
459 /* Set value range VR to a range of a truthvalue of type TYPE. */
462 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
464 if (TYPE_PRECISION (type
) == 1)
465 set_value_range_to_varying (vr
);
467 set_value_range (vr
, VR_RANGE
,
468 build_int_cst (type
, 0), build_int_cst (type
, 1),
473 /* If abs (min) < abs (max), set VR to [-max, max], if
474 abs (min) >= abs (max), set VR to [-min, min]. */
477 abs_extent_range (value_range
*vr
, tree min
, tree max
)
481 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
482 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
483 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
484 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
485 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
486 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
487 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
489 set_value_range_to_varying (vr
);
492 cmp
= compare_values (min
, max
);
494 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
495 else if (cmp
== 0 || cmp
== 1)
498 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
502 set_value_range_to_varying (vr
);
505 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
509 /* Return value range information for VAR.
511 If we have no values ranges recorded (ie, VRP is not running), then
512 return NULL. Otherwise create an empty range if none existed for VAR. */
515 get_value_range (const_tree var
)
517 static const value_range vr_const_varying
518 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
521 unsigned ver
= SSA_NAME_VERSION (var
);
523 /* If we have no recorded ranges, then return NULL. */
527 /* If we query the range for a new SSA name return an unmodifiable VARYING.
528 We should get here at most from the substitute-and-fold stage which
529 will never try to change values. */
530 if (ver
>= num_vr_values
)
531 return CONST_CAST (value_range
*, &vr_const_varying
);
537 /* After propagation finished do not allocate new value-ranges. */
538 if (values_propagated
)
539 return CONST_CAST (value_range
*, &vr_const_varying
);
541 /* Create a default value range. */
542 vr_value
[ver
] = vr
= vrp_value_range_pool
.allocate ();
543 memset (vr
, 0, sizeof (*vr
));
545 /* Defer allocating the equivalence set. */
548 /* If VAR is a default definition of a parameter, the variable can
549 take any value in VAR's type. */
550 if (SSA_NAME_IS_DEFAULT_DEF (var
))
552 sym
= SSA_NAME_VAR (var
);
553 if (TREE_CODE (sym
) == PARM_DECL
)
555 /* Try to use the "nonnull" attribute to create ~[0, 0]
556 anti-ranges for pointers. Note that this is only valid with
557 default definitions of PARM_DECLs. */
558 if (POINTER_TYPE_P (TREE_TYPE (sym
))
559 && (nonnull_arg_p (sym
)
560 || get_ptr_nonnull (var
)))
561 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
562 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym
)))
565 value_range_type rtype
= get_range_info (var
, &min
, &max
);
566 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
567 set_value_range (vr
, rtype
,
568 wide_int_to_tree (TREE_TYPE (var
), min
),
569 wide_int_to_tree (TREE_TYPE (var
), max
),
572 set_value_range_to_varying (vr
);
575 set_value_range_to_varying (vr
);
577 else if (TREE_CODE (sym
) == RESULT_DECL
578 && DECL_BY_REFERENCE (sym
))
579 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
585 /* Set value-ranges of all SSA names defined by STMT to varying. */
588 set_defs_to_varying (gimple
*stmt
)
592 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
594 value_range
*vr
= get_value_range (def
);
595 /* Avoid writing to vr_const_varying get_value_range may return. */
596 if (vr
->type
!= VR_VARYING
)
597 set_value_range_to_varying (vr
);
602 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
605 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
609 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
614 /* Return true, if the bitmaps B1 and B2 are equal. */
617 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
620 || ((!b1
|| bitmap_empty_p (b1
))
621 && (!b2
|| bitmap_empty_p (b2
)))
623 && bitmap_equal_p (b1
, b2
)));
626 /* Update the value range and equivalence set for variable VAR to
627 NEW_VR. Return true if NEW_VR is different from VAR's previous
630 NOTE: This function assumes that NEW_VR is a temporary value range
631 object created for the sole purpose of updating VAR's range. The
632 storage used by the equivalence set from NEW_VR will be freed by
633 this function. Do not call update_value_range when NEW_VR
634 is the range object associated with another SSA name. */
637 update_value_range (const_tree var
, value_range
*new_vr
)
642 /* If there is a value-range on the SSA name from earlier analysis
644 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
647 value_range_type rtype
= get_range_info (var
, &min
, &max
);
648 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
651 nr_min
= wide_int_to_tree (TREE_TYPE (var
), min
);
652 nr_max
= wide_int_to_tree (TREE_TYPE (var
), max
);
653 value_range nr
= VR_INITIALIZER
;
654 set_and_canonicalize_value_range (&nr
, rtype
, nr_min
, nr_max
, NULL
);
655 vrp_intersect_ranges (new_vr
, &nr
);
659 /* Update the value range, if necessary. */
660 old_vr
= get_value_range (var
);
661 is_new
= old_vr
->type
!= new_vr
->type
662 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
663 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
664 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
668 /* Do not allow transitions up the lattice. The following
669 is slightly more awkward than just new_vr->type < old_vr->type
670 because VR_RANGE and VR_ANTI_RANGE need to be considered
671 the same. We may not have is_new when transitioning to
672 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
674 if (new_vr
->type
== VR_UNDEFINED
)
676 BITMAP_FREE (new_vr
->equiv
);
677 set_value_range_to_varying (old_vr
);
678 set_value_range_to_varying (new_vr
);
682 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
686 BITMAP_FREE (new_vr
->equiv
);
692 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
693 point where equivalence processing can be turned on/off. */
696 add_equivalence (bitmap
*equiv
, const_tree var
)
698 unsigned ver
= SSA_NAME_VERSION (var
);
699 value_range
*vr
= get_value_range (var
);
702 *equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
703 bitmap_set_bit (*equiv
, ver
);
705 bitmap_ior_into (*equiv
, vr
->equiv
);
709 /* Return true if VR is ~[0, 0]. */
712 range_is_nonnull (value_range
*vr
)
714 return vr
->type
== VR_ANTI_RANGE
715 && integer_zerop (vr
->min
)
716 && integer_zerop (vr
->max
);
720 /* Return true if VR is [0, 0]. */
723 range_is_null (value_range
*vr
)
725 return vr
->type
== VR_RANGE
726 && integer_zerop (vr
->min
)
727 && integer_zerop (vr
->max
);
730 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
734 range_int_cst_p (value_range
*vr
)
736 return (vr
->type
== VR_RANGE
737 && TREE_CODE (vr
->max
) == INTEGER_CST
738 && TREE_CODE (vr
->min
) == INTEGER_CST
);
741 /* Return true if VR is a INTEGER_CST singleton. */
744 range_int_cst_singleton_p (value_range
*vr
)
746 return (range_int_cst_p (vr
)
747 && tree_int_cst_equal (vr
->min
, vr
->max
));
750 /* Return true if value range VR involves at least one symbol. */
753 symbolic_range_p (value_range
*vr
)
755 return (!is_gimple_min_invariant (vr
->min
)
756 || !is_gimple_min_invariant (vr
->max
));
759 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
760 otherwise. We only handle additive operations and set NEG to true if the
761 symbol is negated and INV to the invariant part, if any. */
764 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
772 if (TREE_CODE (t
) == PLUS_EXPR
773 || TREE_CODE (t
) == POINTER_PLUS_EXPR
774 || TREE_CODE (t
) == MINUS_EXPR
)
776 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
778 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
779 inv_
= TREE_OPERAND (t
, 0);
780 t
= TREE_OPERAND (t
, 1);
782 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
785 inv_
= TREE_OPERAND (t
, 1);
786 t
= TREE_OPERAND (t
, 0);
797 if (TREE_CODE (t
) == NEGATE_EXPR
)
799 t
= TREE_OPERAND (t
, 0);
803 if (TREE_CODE (t
) != SSA_NAME
)
811 /* The reverse operation: build a symbolic expression with TYPE
812 from symbol SYM, negated according to NEG, and invariant INV. */
815 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
817 const bool pointer_p
= POINTER_TYPE_P (type
);
821 t
= build1 (NEGATE_EXPR
, type
, t
);
823 if (integer_zerop (inv
))
826 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
829 /* Return true if value range VR involves exactly one symbol SYM. */
832 symbolic_range_based_on_p (value_range
*vr
, const_tree sym
)
834 bool neg
, min_has_symbol
, max_has_symbol
;
837 if (is_gimple_min_invariant (vr
->min
))
838 min_has_symbol
= false;
839 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
840 min_has_symbol
= true;
844 if (is_gimple_min_invariant (vr
->max
))
845 max_has_symbol
= false;
846 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
847 max_has_symbol
= true;
851 return (min_has_symbol
|| max_has_symbol
);
854 /* Return true if the result of assignment STMT is know to be non-zero.
855 If the return value is based on the assumption that signed overflow is
856 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
857 *STRICT_OVERFLOW_P.*/
860 gimple_assign_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
862 enum tree_code code
= gimple_assign_rhs_code (stmt
);
863 switch (get_gimple_rhs_class (code
))
865 case GIMPLE_UNARY_RHS
:
866 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
867 gimple_expr_type (stmt
),
868 gimple_assign_rhs1 (stmt
),
870 case GIMPLE_BINARY_RHS
:
871 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
872 gimple_expr_type (stmt
),
873 gimple_assign_rhs1 (stmt
),
874 gimple_assign_rhs2 (stmt
),
876 case GIMPLE_TERNARY_RHS
:
878 case GIMPLE_SINGLE_RHS
:
879 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
881 case GIMPLE_INVALID_RHS
:
888 /* Return true if STMT is known to compute a non-zero value.
889 If the return value is based on the assumption that signed overflow is
890 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
891 *STRICT_OVERFLOW_P.*/
894 gimple_stmt_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
896 switch (gimple_code (stmt
))
899 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
902 tree fndecl
= gimple_call_fndecl (stmt
);
903 if (!fndecl
) return false;
904 if (flag_delete_null_pointer_checks
&& !flag_check_new
905 && DECL_IS_OPERATOR_NEW (fndecl
)
906 && !TREE_NOTHROW (fndecl
))
908 /* References are always non-NULL. */
909 if (flag_delete_null_pointer_checks
910 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
912 if (flag_delete_null_pointer_checks
&&
913 lookup_attribute ("returns_nonnull",
914 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
917 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
918 unsigned rf
= gimple_call_return_flags (call_stmt
);
919 if (rf
& ERF_RETURNS_ARG
)
921 unsigned argnum
= rf
& ERF_RETURN_ARG_MASK
;
922 if (argnum
< gimple_call_num_args (call_stmt
))
924 tree arg
= gimple_call_arg (call_stmt
, argnum
);
926 && infer_nonnull_range_by_attribute (stmt
, arg
))
930 return gimple_alloca_call_p (stmt
);
937 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
941 vrp_stmt_computes_nonzero (gimple
*stmt
, bool *strict_overflow_p
)
943 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
946 /* If we have an expression of the form &X->a, then the expression
947 is nonnull if X is nonnull. */
948 if (is_gimple_assign (stmt
)
949 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
951 tree expr
= gimple_assign_rhs1 (stmt
);
952 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
954 if (base
!= NULL_TREE
955 && TREE_CODE (base
) == MEM_REF
956 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
958 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
959 if (range_is_nonnull (vr
))
967 /* Returns true if EXPR is a valid value (as expected by compare_values) --
968 a gimple invariant, or SSA_NAME +- CST. */
971 valid_value_p (tree expr
)
973 if (TREE_CODE (expr
) == SSA_NAME
)
976 if (TREE_CODE (expr
) == PLUS_EXPR
977 || TREE_CODE (expr
) == MINUS_EXPR
)
978 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
979 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
981 return is_gimple_min_invariant (expr
);
987 -2 if those are incomparable. */
989 operand_less_p (tree val
, tree val2
)
991 /* LT is folded faster than GE and others. Inline the common case. */
992 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
993 return tree_int_cst_lt (val
, val2
);
998 fold_defer_overflow_warnings ();
1000 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1002 fold_undefer_and_ignore_overflow_warnings ();
1005 || TREE_CODE (tcmp
) != INTEGER_CST
)
1008 if (!integer_zerop (tcmp
))
1015 /* Compare two values VAL1 and VAL2. Return
1017 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1020 +1 if VAL1 > VAL2, and
1023 This is similar to tree_int_cst_compare but supports pointer values
1024 and values that cannot be compared at compile time.
1026 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1027 true if the return value is only valid if we assume that signed
1028 overflow is undefined. */
1031 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1036 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1038 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1039 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1041 /* Convert the two values into the same type. This is needed because
1042 sizetype causes sign extension even for unsigned types. */
1043 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1044 STRIP_USELESS_TYPE_CONVERSION (val2
);
1046 const bool overflow_undefined
1047 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
1048 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
1051 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
1052 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
1054 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1055 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1058 /* Both values must use the same name with the same sign. */
1059 if (sym1
!= sym2
|| neg1
!= neg2
)
1062 /* [-]NAME + CST == [-]NAME + CST. */
1066 /* If overflow is defined we cannot simplify more. */
1067 if (!overflow_undefined
)
1070 if (strict_overflow_p
!= NULL
1071 && (!inv1
|| !TREE_NO_WARNING (val1
))
1072 && (!inv2
|| !TREE_NO_WARNING (val2
)))
1073 *strict_overflow_p
= true;
1076 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
1078 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
1080 return compare_values_warnv (inv1
, inv2
, strict_overflow_p
);
1083 const bool cst1
= is_gimple_min_invariant (val1
);
1084 const bool cst2
= is_gimple_min_invariant (val2
);
1086 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1087 it might be possible to say something depending on the constants. */
1088 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
1090 if (!overflow_undefined
)
1093 if (strict_overflow_p
!= NULL
1094 && (!sym1
|| !TREE_NO_WARNING (val1
))
1095 && (!sym2
|| !TREE_NO_WARNING (val2
)))
1096 *strict_overflow_p
= true;
1098 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
1099 tree cst
= cst1
? val1
: val2
;
1100 tree inv
= cst1
? inv2
: inv1
;
1102 /* Compute the difference between the constants. If it overflows or
1103 underflows, this means that we can trivially compare the NAME with
1104 it and, consequently, the two values with each other. */
1105 wide_int diff
= wi::sub (cst
, inv
);
1106 if (wi::cmp (0, inv
, sgn
) != wi::cmp (diff
, cst
, sgn
))
1108 const int res
= wi::cmp (cst
, inv
, sgn
);
1109 return cst1
? res
: -res
;
1115 /* We cannot say anything more for non-constants. */
1119 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1121 /* We cannot compare overflowed values, except for overflow
1123 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1125 if (strict_overflow_p
!= NULL
)
1126 *strict_overflow_p
= true;
1130 return tree_int_cst_compare (val1
, val2
);
1136 /* First see if VAL1 and VAL2 are not the same. */
1137 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1140 /* If VAL1 is a lower address than VAL2, return -1. */
1141 if (operand_less_p (val1
, val2
) == 1)
1144 /* If VAL1 is a higher address than VAL2, return +1. */
1145 if (operand_less_p (val2
, val1
) == 1)
1148 /* If VAL1 is different than VAL2, return +2.
1149 For integer constants we either have already returned -1 or 1
1150 or they are equivalent. We still might succeed in proving
1151 something about non-trivial operands. */
1152 if (TREE_CODE (val1
) != INTEGER_CST
1153 || TREE_CODE (val2
) != INTEGER_CST
)
1155 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1156 if (t
&& integer_onep (t
))
1164 /* Compare values like compare_values_warnv, but treat comparisons of
1165 nonconstants which rely on undefined overflow as incomparable. */
1168 compare_values (tree val1
, tree val2
)
1174 ret
= compare_values_warnv (val1
, val2
, &sop
);
1176 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1182 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1183 0 if VAL is not inside [MIN, MAX],
1184 -2 if we cannot tell either way.
1186 Benchmark compile/20001226-1.c compilation time after changing this
1190 value_inside_range (tree val
, tree min
, tree max
)
1194 cmp1
= operand_less_p (val
, min
);
1200 cmp2
= operand_less_p (max
, val
);
1208 /* Return true if value ranges VR0 and VR1 have a non-empty
1211 Benchmark compile/20001226-1.c compilation time after changing this
1216 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
1218 /* The value ranges do not intersect if the maximum of the first range is
1219 less than the minimum of the second range or vice versa.
1220 When those relations are unknown, we can't do any better. */
1221 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1223 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1229 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1230 include the value zero, -2 if we cannot tell. */
1233 range_includes_zero_p (tree min
, tree max
)
1235 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1236 return value_inside_range (zero
, min
, max
);
1239 /* Return true if *VR is know to only contain nonnegative values. */
1242 value_range_nonnegative_p (value_range
*vr
)
1244 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1245 which would return a useful value should be encoded as a
1247 if (vr
->type
== VR_RANGE
)
1249 int result
= compare_values (vr
->min
, integer_zero_node
);
1250 return (result
== 0 || result
== 1);
1256 /* If *VR has a value rante that is a single constant value return that,
1257 otherwise return NULL_TREE. */
1260 value_range_constant_singleton (value_range
*vr
)
1262 if (vr
->type
== VR_RANGE
1263 && vrp_operand_equal_p (vr
->min
, vr
->max
)
1264 && is_gimple_min_invariant (vr
->min
))
1270 /* If OP has a value range with a single constant value return that,
1271 otherwise return NULL_TREE. This returns OP itself if OP is a
1275 op_with_constant_singleton_value_range (tree op
)
1277 if (is_gimple_min_invariant (op
))
1280 if (TREE_CODE (op
) != SSA_NAME
)
1283 return value_range_constant_singleton (get_value_range (op
));
1286 /* Return true if op is in a boolean [0, 1] value-range. */
1289 op_with_boolean_value_range_p (tree op
)
1293 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1296 if (integer_zerop (op
)
1297 || integer_onep (op
))
1300 if (TREE_CODE (op
) != SSA_NAME
)
1303 vr
= get_value_range (op
);
1304 return (vr
->type
== VR_RANGE
1305 && integer_zerop (vr
->min
)
1306 && integer_onep (vr
->max
));
1309 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1310 true and store it in *VR_P. */
1313 extract_range_for_var_from_comparison_expr (tree var
, enum tree_code cond_code
,
1314 tree op
, tree limit
,
1317 tree min
, max
, type
;
1318 value_range
*limit_vr
;
1319 type
= TREE_TYPE (var
);
1320 gcc_assert (limit
!= var
);
1322 /* For pointer arithmetic, we only keep track of pointer equality
1324 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1326 set_value_range_to_varying (vr_p
);
1330 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1331 try to use LIMIT's range to avoid creating symbolic ranges
1333 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1335 /* LIMIT's range is only interesting if it has any useful information. */
1337 || limit_vr
->type
== VR_UNDEFINED
1338 || limit_vr
->type
== VR_VARYING
1339 || (symbolic_range_p (limit_vr
)
1340 && ! (limit_vr
->type
== VR_RANGE
1341 && (limit_vr
->min
== limit_vr
->max
1342 || operand_equal_p (limit_vr
->min
, limit_vr
->max
, 0)))))
1345 /* Initially, the new range has the same set of equivalences of
1346 VAR's range. This will be revised before returning the final
1347 value. Since assertions may be chained via mutually exclusive
1348 predicates, we will need to trim the set of equivalences before
1350 gcc_assert (vr_p
->equiv
== NULL
);
1351 add_equivalence (&vr_p
->equiv
, var
);
1353 /* Extract a new range based on the asserted comparison for VAR and
1354 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1355 will only use it for equality comparisons (EQ_EXPR). For any
1356 other kind of assertion, we cannot derive a range from LIMIT's
1357 anti-range that can be used to describe the new range. For
1358 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1359 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1360 no single range for x_2 that could describe LE_EXPR, so we might
1361 as well build the range [b_4, +INF] for it.
1362 One special case we handle is extracting a range from a
1363 range test encoded as (unsigned)var + CST <= limit. */
1364 if (TREE_CODE (op
) == NOP_EXPR
1365 || TREE_CODE (op
) == PLUS_EXPR
)
1367 if (TREE_CODE (op
) == PLUS_EXPR
)
1369 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (op
, 1)),
1370 TREE_OPERAND (op
, 1));
1371 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1372 op
= TREE_OPERAND (op
, 0);
1376 min
= build_int_cst (TREE_TYPE (var
), 0);
1380 /* Make sure to not set TREE_OVERFLOW on the final type
1381 conversion. We are willingly interpreting large positive
1382 unsigned values as negative signed values here. */
1383 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1384 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1386 /* We can transform a max, min range to an anti-range or
1387 vice-versa. Use set_and_canonicalize_value_range which does
1389 if (cond_code
== LE_EXPR
)
1390 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1391 min
, max
, vr_p
->equiv
);
1392 else if (cond_code
== GT_EXPR
)
1393 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1394 min
, max
, vr_p
->equiv
);
1398 else if (cond_code
== EQ_EXPR
)
1400 enum value_range_type range_type
;
1404 range_type
= limit_vr
->type
;
1405 min
= limit_vr
->min
;
1406 max
= limit_vr
->max
;
1410 range_type
= VR_RANGE
;
1415 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1417 /* When asserting the equality VAR == LIMIT and LIMIT is another
1418 SSA name, the new range will also inherit the equivalence set
1420 if (TREE_CODE (limit
) == SSA_NAME
)
1421 add_equivalence (&vr_p
->equiv
, limit
);
1423 else if (cond_code
== NE_EXPR
)
1425 /* As described above, when LIMIT's range is an anti-range and
1426 this assertion is an inequality (NE_EXPR), then we cannot
1427 derive anything from the anti-range. For instance, if
1428 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1429 not imply that VAR's range is [0, 0]. So, in the case of
1430 anti-ranges, we just assert the inequality using LIMIT and
1433 If LIMIT_VR is a range, we can only use it to build a new
1434 anti-range if LIMIT_VR is a single-valued range. For
1435 instance, if LIMIT_VR is [0, 1], the predicate
1436 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1437 Rather, it means that for value 0 VAR should be ~[0, 0]
1438 and for value 1, VAR should be ~[1, 1]. We cannot
1439 represent these ranges.
1441 The only situation in which we can build a valid
1442 anti-range is when LIMIT_VR is a single-valued range
1443 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1444 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1446 && limit_vr
->type
== VR_RANGE
1447 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1449 min
= limit_vr
->min
;
1450 max
= limit_vr
->max
;
1454 /* In any other case, we cannot use LIMIT's range to build a
1455 valid anti-range. */
1459 /* If MIN and MAX cover the whole range for their type, then
1460 just use the original LIMIT. */
1461 if (INTEGRAL_TYPE_P (type
)
1462 && vrp_val_is_min (min
)
1463 && vrp_val_is_max (max
))
1466 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1467 min
, max
, vr_p
->equiv
);
1469 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1471 min
= TYPE_MIN_VALUE (type
);
1473 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1477 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1478 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1480 max
= limit_vr
->max
;
1483 /* If the maximum value forces us to be out of bounds, simply punt.
1484 It would be pointless to try and do anything more since this
1485 all should be optimized away above us. */
1486 if (cond_code
== LT_EXPR
1487 && compare_values (max
, min
) == 0)
1488 set_value_range_to_varying (vr_p
);
1491 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1492 if (cond_code
== LT_EXPR
)
1494 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1495 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1496 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1497 build_int_cst (TREE_TYPE (max
), -1));
1499 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1500 build_int_cst (TREE_TYPE (max
), 1));
1502 TREE_NO_WARNING (max
) = 1;
1505 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1508 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1510 max
= TYPE_MAX_VALUE (type
);
1512 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1516 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1517 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1519 min
= limit_vr
->min
;
1522 /* If the minimum value forces us to be out of bounds, simply punt.
1523 It would be pointless to try and do anything more since this
1524 all should be optimized away above us. */
1525 if (cond_code
== GT_EXPR
1526 && compare_values (min
, max
) == 0)
1527 set_value_range_to_varying (vr_p
);
1530 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1531 if (cond_code
== GT_EXPR
)
1533 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1534 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1535 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1536 build_int_cst (TREE_TYPE (min
), -1));
1538 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1539 build_int_cst (TREE_TYPE (min
), 1));
1541 TREE_NO_WARNING (min
) = 1;
1544 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1550 /* Finally intersect the new range with what we already know about var. */
1551 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1554 /* Extract value range information from an ASSERT_EXPR EXPR and store
1558 extract_range_from_assert (value_range
*vr_p
, tree expr
)
1560 tree var
= ASSERT_EXPR_VAR (expr
);
1561 tree cond
= ASSERT_EXPR_COND (expr
);
1563 enum tree_code cond_code
;
1564 gcc_assert (COMPARISON_CLASS_P (cond
));
1566 /* Find VAR in the ASSERT_EXPR conditional. */
1567 if (var
== TREE_OPERAND (cond
, 0)
1568 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1569 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1571 /* If the predicate is of the form VAR COMP LIMIT, then we just
1572 take LIMIT from the RHS and use the same comparison code. */
1573 cond_code
= TREE_CODE (cond
);
1574 limit
= TREE_OPERAND (cond
, 1);
1575 op
= TREE_OPERAND (cond
, 0);
1579 /* If the predicate is of the form LIMIT COMP VAR, then we need
1580 to flip around the comparison code to create the proper range
1582 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1583 limit
= TREE_OPERAND (cond
, 0);
1584 op
= TREE_OPERAND (cond
, 1);
1586 extract_range_for_var_from_comparison_expr (var
, cond_code
, op
,
1590 /* Extract range information from SSA name VAR and store it in VR. If
1591 VAR has an interesting range, use it. Otherwise, create the
1592 range [VAR, VAR] and return it. This is useful in situations where
1593 we may have conditionals testing values of VARYING names. For
1600 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1604 extract_range_from_ssa_name (value_range
*vr
, tree var
)
1606 value_range
*var_vr
= get_value_range (var
);
1608 if (var_vr
->type
!= VR_VARYING
)
1609 copy_value_range (vr
, var_vr
);
1611 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1613 add_equivalence (&vr
->equiv
, var
);
1617 /* Wrapper around int_const_binop. If the operation overflows and we
1618 are not using wrapping arithmetic, then adjust the result to be
1619 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1620 NULL_TREE if we need to use an overflow infinity representation but
1621 the type does not support it. */
1624 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1628 res
= int_const_binop (code
, val1
, val2
);
1630 /* If we are using unsigned arithmetic, operate symbolically
1631 on -INF and +INF as int_const_binop only handles signed overflow. */
1632 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1634 int checkz
= compare_values (res
, val1
);
1635 bool overflow
= false;
1637 /* Ensure that res = val1 [+*] val2 >= val1
1638 or that res = val1 - val2 <= val1. */
1639 if ((code
== PLUS_EXPR
1640 && !(checkz
== 1 || checkz
== 0))
1641 || (code
== MINUS_EXPR
1642 && !(checkz
== 0 || checkz
== -1)))
1646 /* Checking for multiplication overflow is done by dividing the
1647 output of the multiplication by the first input of the
1648 multiplication. If the result of that division operation is
1649 not equal to the second input of the multiplication, then the
1650 multiplication overflowed. */
1651 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1653 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1656 int check
= compare_values (tmp
, val2
);
1664 res
= copy_node (res
);
1665 TREE_OVERFLOW (res
) = 1;
1669 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1670 /* If the singed operation wraps then int_const_binop has done
1671 everything we want. */
1673 /* Signed division of -1/0 overflows and by the time it gets here
1674 returns NULL_TREE. */
1677 else if (TREE_OVERFLOW (res
)
1678 && ! TREE_OVERFLOW (val1
)
1679 && ! TREE_OVERFLOW (val2
))
1681 /* If the operation overflowed but neither VAL1 nor VAL2 are
1682 overflown, return -INF or +INF depending on the operation
1683 and the combination of signs of the operands. */
1684 int sgn1
= tree_int_cst_sgn (val1
);
1685 int sgn2
= tree_int_cst_sgn (val2
);
1687 /* Notice that we only need to handle the restricted set of
1688 operations handled by extract_range_from_binary_expr.
1689 Among them, only multiplication, addition and subtraction
1690 can yield overflow without overflown operands because we
1691 are working with integral types only... except in the
1692 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1693 for division too. */
1695 /* For multiplication, the sign of the overflow is given
1696 by the comparison of the signs of the operands. */
1697 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1698 /* For addition, the operands must be of the same sign
1699 to yield an overflow. Its sign is therefore that
1700 of one of the operands, for example the first. */
1701 || (code
== PLUS_EXPR
&& sgn1
>= 0)
1702 /* For subtraction, operands must be of
1703 different signs to yield an overflow. Its sign is
1704 therefore that of the first operand or the opposite of
1705 that of the second operand. A first operand of 0 counts
1706 as positive here, for the corner case 0 - (-INF), which
1707 overflows, but must yield +INF. */
1708 || (code
== MINUS_EXPR
&& sgn1
>= 0)
1709 /* We only get in here with positive shift count, so the
1710 overflow direction is the same as the sign of val1.
1711 Actually rshift does not overflow at all, but we only
1712 handle the case of shifting overflowed -INF and +INF. */
1713 || (code
== RSHIFT_EXPR
1715 /* For division, the only case is -INF / -1 = +INF. */
1716 || code
== TRUNC_DIV_EXPR
1717 || code
== FLOOR_DIV_EXPR
1718 || code
== CEIL_DIV_EXPR
1719 || code
== EXACT_DIV_EXPR
1720 || code
== ROUND_DIV_EXPR
)
1721 return TYPE_MAX_VALUE (TREE_TYPE (res
));
1723 return TYPE_MIN_VALUE (TREE_TYPE (res
));
1730 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1731 bitmask if some bit is unset, it means for all numbers in the range
1732 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1733 bitmask if some bit is set, it means for all numbers in the range
1734 the bit is 1, otherwise it might be 0 or 1. */
1737 zero_nonzero_bits_from_vr (const tree expr_type
,
1739 wide_int
*may_be_nonzero
,
1740 wide_int
*must_be_nonzero
)
1742 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1743 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1744 if (!range_int_cst_p (vr
))
1747 if (range_int_cst_singleton_p (vr
))
1749 *may_be_nonzero
= vr
->min
;
1750 *must_be_nonzero
= *may_be_nonzero
;
1752 else if (tree_int_cst_sgn (vr
->min
) >= 0
1753 || tree_int_cst_sgn (vr
->max
) < 0)
1755 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
1756 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
1757 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
1760 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1761 may_be_nonzero
->get_precision ());
1762 *may_be_nonzero
= *may_be_nonzero
| mask
;
1763 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
1770 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1771 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1772 false otherwise. If *AR can be represented with a single range
1773 *VR1 will be VR_UNDEFINED. */
1776 ranges_from_anti_range (value_range
*ar
,
1777 value_range
*vr0
, value_range
*vr1
)
1779 tree type
= TREE_TYPE (ar
->min
);
1781 vr0
->type
= VR_UNDEFINED
;
1782 vr1
->type
= VR_UNDEFINED
;
1784 if (ar
->type
!= VR_ANTI_RANGE
1785 || TREE_CODE (ar
->min
) != INTEGER_CST
1786 || TREE_CODE (ar
->max
) != INTEGER_CST
1787 || !vrp_val_min (type
)
1788 || !vrp_val_max (type
))
1791 if (!vrp_val_is_min (ar
->min
))
1793 vr0
->type
= VR_RANGE
;
1794 vr0
->min
= vrp_val_min (type
);
1795 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
1797 if (!vrp_val_is_max (ar
->max
))
1799 vr1
->type
= VR_RANGE
;
1800 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
1801 vr1
->max
= vrp_val_max (type
);
1803 if (vr0
->type
== VR_UNDEFINED
)
1806 vr1
->type
= VR_UNDEFINED
;
1809 return vr0
->type
!= VR_UNDEFINED
;
1812 /* Helper to extract a value-range *VR for a multiplicative operation
1816 extract_range_from_multiplicative_op_1 (value_range
*vr
,
1817 enum tree_code code
,
1818 value_range
*vr0
, value_range
*vr1
)
1820 enum value_range_type type
;
1827 /* Multiplications, divisions and shifts are a bit tricky to handle,
1828 depending on the mix of signs we have in the two ranges, we
1829 need to operate on different values to get the minimum and
1830 maximum values for the new range. One approach is to figure
1831 out all the variations of range combinations and do the
1834 However, this involves several calls to compare_values and it
1835 is pretty convoluted. It's simpler to do the 4 operations
1836 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1837 MAX1) and then figure the smallest and largest values to form
1839 gcc_assert (code
== MULT_EXPR
1840 || code
== TRUNC_DIV_EXPR
1841 || code
== FLOOR_DIV_EXPR
1842 || code
== CEIL_DIV_EXPR
1843 || code
== EXACT_DIV_EXPR
1844 || code
== ROUND_DIV_EXPR
1845 || code
== RSHIFT_EXPR
1846 || code
== LSHIFT_EXPR
);
1847 gcc_assert ((vr0
->type
== VR_RANGE
1848 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
1849 && vr0
->type
== vr1
->type
);
1853 /* Compute the 4 cross operations. */
1855 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
1856 if (val
[0] == NULL_TREE
)
1859 if (vr1
->max
== vr1
->min
)
1863 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
1864 if (val
[1] == NULL_TREE
)
1868 if (vr0
->max
== vr0
->min
)
1872 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
1873 if (val
[2] == NULL_TREE
)
1877 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
1881 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
1882 if (val
[3] == NULL_TREE
)
1888 set_value_range_to_varying (vr
);
1892 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
1896 for (i
= 1; i
< 4; i
++)
1898 if (!is_gimple_min_invariant (min
)
1899 || TREE_OVERFLOW (min
)
1900 || !is_gimple_min_invariant (max
)
1901 || TREE_OVERFLOW (max
))
1906 if (!is_gimple_min_invariant (val
[i
])
1907 || TREE_OVERFLOW (val
[i
]))
1909 /* If we found an overflowed value, set MIN and MAX
1910 to it so that we set the resulting range to
1916 if (compare_values (val
[i
], min
) == -1)
1919 if (compare_values (val
[i
], max
) == 1)
1924 /* If either MIN or MAX overflowed, then set the resulting range to
1925 VARYING. But we do accept an overflow infinity
1927 if (min
== NULL_TREE
1928 || !is_gimple_min_invariant (min
)
1929 || TREE_OVERFLOW (min
)
1931 || !is_gimple_min_invariant (max
)
1932 || TREE_OVERFLOW (max
))
1934 set_value_range_to_varying (vr
);
1938 /* We punt for [-INF, +INF].
1939 We learn nothing when we have INF on both sides.
1940 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1941 if (vrp_val_is_min (min
)
1942 && vrp_val_is_max (max
))
1944 set_value_range_to_varying (vr
);
1948 cmp
= compare_values (min
, max
);
1949 if (cmp
== -2 || cmp
== 1)
1951 /* If the new range has its limits swapped around (MIN > MAX),
1952 then the operation caused one of them to wrap around, mark
1953 the new range VARYING. */
1954 set_value_range_to_varying (vr
);
1957 set_value_range (vr
, type
, min
, max
, NULL
);
1960 /* Extract range information from a binary operation CODE based on
1961 the ranges of each of its operands *VR0 and *VR1 with resulting
1962 type EXPR_TYPE. The resulting range is stored in *VR. */
1965 extract_range_from_binary_expr_1 (value_range
*vr
,
1966 enum tree_code code
, tree expr_type
,
1967 value_range
*vr0_
, value_range
*vr1_
)
1969 value_range vr0
= *vr0_
, vr1
= *vr1_
;
1970 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
1971 enum value_range_type type
;
1972 tree min
= NULL_TREE
, max
= NULL_TREE
;
1975 if (!INTEGRAL_TYPE_P (expr_type
)
1976 && !POINTER_TYPE_P (expr_type
))
1978 set_value_range_to_varying (vr
);
1982 /* Not all binary expressions can be applied to ranges in a
1983 meaningful way. Handle only arithmetic operations. */
1984 if (code
!= PLUS_EXPR
1985 && code
!= MINUS_EXPR
1986 && code
!= POINTER_PLUS_EXPR
1987 && code
!= MULT_EXPR
1988 && code
!= TRUNC_DIV_EXPR
1989 && code
!= FLOOR_DIV_EXPR
1990 && code
!= CEIL_DIV_EXPR
1991 && code
!= EXACT_DIV_EXPR
1992 && code
!= ROUND_DIV_EXPR
1993 && code
!= TRUNC_MOD_EXPR
1994 && code
!= RSHIFT_EXPR
1995 && code
!= LSHIFT_EXPR
1998 && code
!= BIT_AND_EXPR
1999 && code
!= BIT_IOR_EXPR
2000 && code
!= BIT_XOR_EXPR
)
2002 set_value_range_to_varying (vr
);
2006 /* If both ranges are UNDEFINED, so is the result. */
2007 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2009 set_value_range_to_undefined (vr
);
2012 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2013 code. At some point we may want to special-case operations that
2014 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2016 else if (vr0
.type
== VR_UNDEFINED
)
2017 set_value_range_to_varying (&vr0
);
2018 else if (vr1
.type
== VR_UNDEFINED
)
2019 set_value_range_to_varying (&vr1
);
2021 /* We get imprecise results from ranges_from_anti_range when
2022 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
2023 range, but then we also need to hack up vrp_meet. It's just
2024 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
2025 if (code
== EXACT_DIV_EXPR
2026 && vr0
.type
== VR_ANTI_RANGE
2027 && vr0
.min
== vr0
.max
2028 && integer_zerop (vr0
.min
))
2030 set_value_range_to_nonnull (vr
, expr_type
);
2034 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2035 and express ~[] op X as ([]' op X) U ([]'' op X). */
2036 if (vr0
.type
== VR_ANTI_RANGE
2037 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2039 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2040 if (vrtem1
.type
!= VR_UNDEFINED
)
2042 value_range vrres
= VR_INITIALIZER
;
2043 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2045 vrp_meet (vr
, &vrres
);
2049 /* Likewise for X op ~[]. */
2050 if (vr1
.type
== VR_ANTI_RANGE
2051 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2053 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2054 if (vrtem1
.type
!= VR_UNDEFINED
)
2056 value_range vrres
= VR_INITIALIZER
;
2057 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2059 vrp_meet (vr
, &vrres
);
2064 /* The type of the resulting value range defaults to VR0.TYPE. */
2067 /* Refuse to operate on VARYING ranges, ranges of different kinds
2068 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2069 because we may be able to derive a useful range even if one of
2070 the operands is VR_VARYING or symbolic range. Similarly for
2071 divisions, MIN/MAX and PLUS/MINUS.
2073 TODO, we may be able to derive anti-ranges in some cases. */
2074 if (code
!= BIT_AND_EXPR
2075 && code
!= BIT_IOR_EXPR
2076 && code
!= TRUNC_DIV_EXPR
2077 && code
!= FLOOR_DIV_EXPR
2078 && code
!= CEIL_DIV_EXPR
2079 && code
!= EXACT_DIV_EXPR
2080 && code
!= ROUND_DIV_EXPR
2081 && code
!= TRUNC_MOD_EXPR
2084 && code
!= PLUS_EXPR
2085 && code
!= MINUS_EXPR
2086 && code
!= RSHIFT_EXPR
2087 && (vr0
.type
== VR_VARYING
2088 || vr1
.type
== VR_VARYING
2089 || vr0
.type
!= vr1
.type
2090 || symbolic_range_p (&vr0
)
2091 || symbolic_range_p (&vr1
)))
2093 set_value_range_to_varying (vr
);
2097 /* Now evaluate the expression to determine the new range. */
2098 if (POINTER_TYPE_P (expr_type
))
2100 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2102 /* For MIN/MAX expressions with pointers, we only care about
2103 nullness, if both are non null, then the result is nonnull.
2104 If both are null, then the result is null. Otherwise they
2106 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2107 set_value_range_to_nonnull (vr
, expr_type
);
2108 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2109 set_value_range_to_null (vr
, expr_type
);
2111 set_value_range_to_varying (vr
);
2113 else if (code
== POINTER_PLUS_EXPR
)
2115 /* For pointer types, we are really only interested in asserting
2116 whether the expression evaluates to non-NULL. */
2117 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2118 set_value_range_to_nonnull (vr
, expr_type
);
2119 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2120 set_value_range_to_null (vr
, expr_type
);
2122 set_value_range_to_varying (vr
);
2124 else if (code
== BIT_AND_EXPR
)
2126 /* For pointer types, we are really only interested in asserting
2127 whether the expression evaluates to non-NULL. */
2128 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2129 set_value_range_to_nonnull (vr
, expr_type
);
2130 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2131 set_value_range_to_null (vr
, expr_type
);
2133 set_value_range_to_varying (vr
);
2136 set_value_range_to_varying (vr
);
2141 /* For integer ranges, apply the operation to each end of the
2142 range and see what we end up with. */
2143 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2145 const bool minus_p
= (code
== MINUS_EXPR
);
2146 tree min_op0
= vr0
.min
;
2147 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2148 tree max_op0
= vr0
.max
;
2149 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2150 tree sym_min_op0
= NULL_TREE
;
2151 tree sym_min_op1
= NULL_TREE
;
2152 tree sym_max_op0
= NULL_TREE
;
2153 tree sym_max_op1
= NULL_TREE
;
2154 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2156 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2157 single-symbolic ranges, try to compute the precise resulting range,
2158 but only if we know that this resulting range will also be constant
2159 or single-symbolic. */
2160 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2161 && (TREE_CODE (min_op0
) == INTEGER_CST
2163 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2164 && (TREE_CODE (min_op1
) == INTEGER_CST
2166 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2167 && (!(sym_min_op0
&& sym_min_op1
)
2168 || (sym_min_op0
== sym_min_op1
2169 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2170 && (TREE_CODE (max_op0
) == INTEGER_CST
2172 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2173 && (TREE_CODE (max_op1
) == INTEGER_CST
2175 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2176 && (!(sym_max_op0
&& sym_max_op1
)
2177 || (sym_max_op0
== sym_max_op1
2178 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2180 const signop sgn
= TYPE_SIGN (expr_type
);
2181 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2182 wide_int type_min
, type_max
, wmin
, wmax
;
2186 /* Get the lower and upper bounds of the type. */
2187 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2189 type_min
= wi::min_value (prec
, sgn
);
2190 type_max
= wi::max_value (prec
, sgn
);
2194 type_min
= vrp_val_min (expr_type
);
2195 type_max
= vrp_val_max (expr_type
);
2198 /* Combine the lower bounds, if any. */
2199 if (min_op0
&& min_op1
)
2203 wmin
= wi::sub (min_op0
, min_op1
);
2205 /* Check for overflow. */
2206 if (wi::cmp (0, min_op1
, sgn
)
2207 != wi::cmp (wmin
, min_op0
, sgn
))
2208 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2212 wmin
= wi::add (min_op0
, min_op1
);
2214 /* Check for overflow. */
2215 if (wi::cmp (min_op1
, 0, sgn
)
2216 != wi::cmp (wmin
, min_op0
, sgn
))
2217 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2226 wmin
= wi::neg (min_op1
);
2228 /* Check for overflow. */
2229 if (sgn
== SIGNED
&& wi::neg_p (min_op1
) && wi::neg_p (wmin
))
2231 else if (sgn
== UNSIGNED
&& wi::ne_p (min_op1
, 0))
2238 wmin
= wi::shwi (0, prec
);
2240 /* Combine the upper bounds, if any. */
2241 if (max_op0
&& max_op1
)
2245 wmax
= wi::sub (max_op0
, max_op1
);
2247 /* Check for overflow. */
2248 if (wi::cmp (0, max_op1
, sgn
)
2249 != wi::cmp (wmax
, max_op0
, sgn
))
2250 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2254 wmax
= wi::add (max_op0
, max_op1
);
2256 if (wi::cmp (max_op1
, 0, sgn
)
2257 != wi::cmp (wmax
, max_op0
, sgn
))
2258 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2267 wmax
= wi::neg (max_op1
);
2269 /* Check for overflow. */
2270 if (sgn
== SIGNED
&& wi::neg_p (max_op1
) && wi::neg_p (wmax
))
2272 else if (sgn
== UNSIGNED
&& wi::ne_p (max_op1
, 0))
2279 wmax
= wi::shwi (0, prec
);
2281 /* Check for type overflow. */
2284 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2286 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2291 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2293 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2297 /* If we have overflow for the constant part and the resulting
2298 range will be symbolic, drop to VR_VARYING. */
2299 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2300 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2302 set_value_range_to_varying (vr
);
2306 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2308 /* If overflow wraps, truncate the values and adjust the
2309 range kind and bounds appropriately. */
2310 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2311 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2312 if (min_ovf
== max_ovf
)
2314 /* No overflow or both overflow or underflow. The
2315 range kind stays VR_RANGE. */
2316 min
= wide_int_to_tree (expr_type
, tmin
);
2317 max
= wide_int_to_tree (expr_type
, tmax
);
2319 else if ((min_ovf
== -1 && max_ovf
== 0)
2320 || (max_ovf
== 1 && min_ovf
== 0))
2322 /* Min underflow or max overflow. The range kind
2323 changes to VR_ANTI_RANGE. */
2324 bool covers
= false;
2325 wide_int tem
= tmin
;
2326 type
= VR_ANTI_RANGE
;
2328 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2331 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2333 /* If the anti-range would cover nothing, drop to varying.
2334 Likewise if the anti-range bounds are outside of the
2336 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2338 set_value_range_to_varying (vr
);
2341 min
= wide_int_to_tree (expr_type
, tmin
);
2342 max
= wide_int_to_tree (expr_type
, tmax
);
2346 /* Other underflow and/or overflow, drop to VR_VARYING. */
2347 set_value_range_to_varying (vr
);
2353 /* If overflow does not wrap, saturate to the types min/max
2356 min
= wide_int_to_tree (expr_type
, type_min
);
2357 else if (min_ovf
== 1)
2358 min
= wide_int_to_tree (expr_type
, type_max
);
2360 min
= wide_int_to_tree (expr_type
, wmin
);
2363 max
= wide_int_to_tree (expr_type
, type_min
);
2364 else if (max_ovf
== 1)
2365 max
= wide_int_to_tree (expr_type
, type_max
);
2367 max
= wide_int_to_tree (expr_type
, wmax
);
2370 /* If the result lower bound is constant, we're done;
2371 otherwise, build the symbolic lower bound. */
2372 if (sym_min_op0
== sym_min_op1
)
2374 else if (sym_min_op0
)
2375 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2377 else if (sym_min_op1
)
2379 /* We may not negate if that might introduce
2380 undefined overflow. */
2383 || TYPE_OVERFLOW_WRAPS (expr_type
))
2384 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2385 neg_min_op1
^ minus_p
, min
);
2390 /* Likewise for the upper bound. */
2391 if (sym_max_op0
== sym_max_op1
)
2393 else if (sym_max_op0
)
2394 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2396 else if (sym_max_op1
)
2398 /* We may not negate if that might introduce
2399 undefined overflow. */
2402 || TYPE_OVERFLOW_WRAPS (expr_type
))
2403 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2404 neg_max_op1
^ minus_p
, max
);
2411 /* For other cases, for example if we have a PLUS_EXPR with two
2412 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2413 to compute a precise range for such a case.
2414 ??? General even mixed range kind operations can be expressed
2415 by for example transforming ~[3, 5] + [1, 2] to range-only
2416 operations and a union primitive:
2417 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2418 [-INF+1, 4] U [6, +INF(OVF)]
2419 though usually the union is not exactly representable with
2420 a single range or anti-range as the above is
2421 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2422 but one could use a scheme similar to equivalences for this. */
2423 set_value_range_to_varying (vr
);
2427 else if (code
== MIN_EXPR
2428 || code
== MAX_EXPR
)
2430 if (vr0
.type
== VR_RANGE
2431 && !symbolic_range_p (&vr0
))
2434 if (vr1
.type
== VR_RANGE
2435 && !symbolic_range_p (&vr1
))
2437 /* For operations that make the resulting range directly
2438 proportional to the original ranges, apply the operation to
2439 the same end of each range. */
2440 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2441 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2443 else if (code
== MIN_EXPR
)
2445 min
= vrp_val_min (expr_type
);
2448 else if (code
== MAX_EXPR
)
2451 max
= vrp_val_max (expr_type
);
2454 else if (vr1
.type
== VR_RANGE
2455 && !symbolic_range_p (&vr1
))
2458 if (code
== MIN_EXPR
)
2460 min
= vrp_val_min (expr_type
);
2463 else if (code
== MAX_EXPR
)
2466 max
= vrp_val_max (expr_type
);
2471 set_value_range_to_varying (vr
);
2475 else if (code
== MULT_EXPR
)
2477 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2478 drop to varying. This test requires 2*prec bits if both
2479 operands are signed and 2*prec + 2 bits if either is not. */
2481 signop sign
= TYPE_SIGN (expr_type
);
2482 unsigned int prec
= TYPE_PRECISION (expr_type
);
2484 if (range_int_cst_p (&vr0
)
2485 && range_int_cst_p (&vr1
)
2486 && TYPE_OVERFLOW_WRAPS (expr_type
))
2488 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2489 typedef generic_wide_int
2490 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2491 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2492 vrp_int size
= sizem1
+ 1;
2494 /* Extend the values using the sign of the result to PREC2.
2495 From here on out, everthing is just signed math no matter
2496 what the input types were. */
2497 vrp_int min0
= vrp_int_cst (vr0
.min
);
2498 vrp_int max0
= vrp_int_cst (vr0
.max
);
2499 vrp_int min1
= vrp_int_cst (vr1
.min
);
2500 vrp_int max1
= vrp_int_cst (vr1
.max
);
2501 /* Canonicalize the intervals. */
2502 if (sign
== UNSIGNED
)
2504 if (wi::ltu_p (size
, min0
+ max0
))
2510 if (wi::ltu_p (size
, min1
+ max1
))
2517 vrp_int prod0
= min0
* min1
;
2518 vrp_int prod1
= min0
* max1
;
2519 vrp_int prod2
= max0
* min1
;
2520 vrp_int prod3
= max0
* max1
;
2522 /* Sort the 4 products so that min is in prod0 and max is in
2524 /* min0min1 > max0max1 */
2526 std::swap (prod0
, prod3
);
2528 /* min0max1 > max0min1 */
2530 std::swap (prod1
, prod2
);
2533 std::swap (prod0
, prod1
);
2536 std::swap (prod2
, prod3
);
2538 /* diff = max - min. */
2539 prod2
= prod3
- prod0
;
2540 if (wi::geu_p (prod2
, sizem1
))
2542 /* the range covers all values. */
2543 set_value_range_to_varying (vr
);
2547 /* The following should handle the wrapping and selecting
2548 VR_ANTI_RANGE for us. */
2549 min
= wide_int_to_tree (expr_type
, prod0
);
2550 max
= wide_int_to_tree (expr_type
, prod3
);
2551 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2555 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2556 drop to VR_VARYING. It would take more effort to compute a
2557 precise range for such a case. For example, if we have
2558 op0 == 65536 and op1 == 65536 with their ranges both being
2559 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2560 we cannot claim that the product is in ~[0,0]. Note that we
2561 are guaranteed to have vr0.type == vr1.type at this
2563 if (vr0
.type
== VR_ANTI_RANGE
2564 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2566 set_value_range_to_varying (vr
);
2570 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2573 else if (code
== RSHIFT_EXPR
2574 || code
== LSHIFT_EXPR
)
2576 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2577 then drop to VR_VARYING. Outside of this range we get undefined
2578 behavior from the shift operation. We cannot even trust
2579 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2580 shifts, and the operation at the tree level may be widened. */
2581 if (range_int_cst_p (&vr1
)
2582 && compare_tree_int (vr1
.min
, 0) >= 0
2583 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2585 if (code
== RSHIFT_EXPR
)
2587 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2588 useful ranges just from the shift count. E.g.
2589 x >> 63 for signed 64-bit x is always [-1, 0]. */
2590 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2592 vr0
.type
= type
= VR_RANGE
;
2593 vr0
.min
= vrp_val_min (expr_type
);
2594 vr0
.max
= vrp_val_max (expr_type
);
2596 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2599 /* We can map lshifts by constants to MULT_EXPR handling. */
2600 else if (code
== LSHIFT_EXPR
2601 && range_int_cst_singleton_p (&vr1
))
2603 bool saved_flag_wrapv
;
2604 value_range vr1p
= VR_INITIALIZER
;
2605 vr1p
.type
= VR_RANGE
;
2606 vr1p
.min
= (wide_int_to_tree
2608 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2609 TYPE_PRECISION (expr_type
))));
2610 vr1p
.max
= vr1p
.min
;
2611 /* We have to use a wrapping multiply though as signed overflow
2612 on lshifts is implementation defined in C89. */
2613 saved_flag_wrapv
= flag_wrapv
;
2615 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2617 flag_wrapv
= saved_flag_wrapv
;
2620 else if (code
== LSHIFT_EXPR
2621 && range_int_cst_p (&vr0
))
2623 int prec
= TYPE_PRECISION (expr_type
);
2624 int overflow_pos
= prec
;
2626 wide_int low_bound
, high_bound
;
2627 bool uns
= TYPE_UNSIGNED (expr_type
);
2628 bool in_bounds
= false;
2633 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2634 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2635 overflow. However, for that to happen, vr1.max needs to be
2636 zero, which means vr1 is a singleton range of zero, which
2637 means it should be handled by the previous LSHIFT_EXPR
2639 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2640 wide_int complement
= ~(bound
- 1);
2645 high_bound
= complement
;
2646 if (wi::ltu_p (vr0
.max
, low_bound
))
2648 /* [5, 6] << [1, 2] == [10, 24]. */
2649 /* We're shifting out only zeroes, the value increases
2653 else if (wi::ltu_p (high_bound
, vr0
.min
))
2655 /* [0xffffff00, 0xffffffff] << [1, 2]
2656 == [0xfffffc00, 0xfffffffe]. */
2657 /* We're shifting out only ones, the value decreases
2664 /* [-1, 1] << [1, 2] == [-4, 4]. */
2665 low_bound
= complement
;
2667 if (wi::lts_p (vr0
.max
, high_bound
)
2668 && wi::lts_p (low_bound
, vr0
.min
))
2670 /* For non-negative numbers, we're shifting out only
2671 zeroes, the value increases monotonically.
2672 For negative numbers, we're shifting out only ones, the
2673 value decreases monotomically. */
2680 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2685 set_value_range_to_varying (vr
);
2688 else if (code
== TRUNC_DIV_EXPR
2689 || code
== FLOOR_DIV_EXPR
2690 || code
== CEIL_DIV_EXPR
2691 || code
== EXACT_DIV_EXPR
2692 || code
== ROUND_DIV_EXPR
)
2694 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2696 /* For division, if op1 has VR_RANGE but op0 does not, something
2697 can be deduced just from that range. Say [min, max] / [4, max]
2698 gives [min / 4, max / 4] range. */
2699 if (vr1
.type
== VR_RANGE
2700 && !symbolic_range_p (&vr1
)
2701 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2703 vr0
.type
= type
= VR_RANGE
;
2704 vr0
.min
= vrp_val_min (expr_type
);
2705 vr0
.max
= vrp_val_max (expr_type
);
2709 set_value_range_to_varying (vr
);
2714 /* For divisions, if flag_non_call_exceptions is true, we must
2715 not eliminate a division by zero. */
2716 if (cfun
->can_throw_non_call_exceptions
2717 && (vr1
.type
!= VR_RANGE
2718 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2720 set_value_range_to_varying (vr
);
2724 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2725 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2727 if (vr0
.type
== VR_RANGE
2728 && (vr1
.type
!= VR_RANGE
2729 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2731 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2736 if (TYPE_UNSIGNED (expr_type
)
2737 || value_range_nonnegative_p (&vr1
))
2739 /* For unsigned division or when divisor is known
2740 to be non-negative, the range has to cover
2741 all numbers from 0 to max for positive max
2742 and all numbers from min to 0 for negative min. */
2743 cmp
= compare_values (vr0
.max
, zero
);
2746 /* When vr0.max < 0, vr1.min != 0 and value
2747 ranges for dividend and divisor are available. */
2748 if (vr1
.type
== VR_RANGE
2749 && !symbolic_range_p (&vr0
)
2750 && !symbolic_range_p (&vr1
)
2751 && compare_values (vr1
.min
, zero
) != 0)
2752 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
2756 else if (cmp
== 0 || cmp
== 1)
2760 cmp
= compare_values (vr0
.min
, zero
);
2763 /* For unsigned division when value ranges for dividend
2764 and divisor are available. */
2765 if (vr1
.type
== VR_RANGE
2766 && !symbolic_range_p (&vr0
)
2767 && !symbolic_range_p (&vr1
)
2768 && compare_values (vr1
.max
, zero
) != 0)
2769 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
2773 else if (cmp
== 0 || cmp
== -1)
2780 /* Otherwise the range is -max .. max or min .. -min
2781 depending on which bound is bigger in absolute value,
2782 as the division can change the sign. */
2783 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2786 if (type
== VR_VARYING
)
2788 set_value_range_to_varying (vr
);
2792 else if (!symbolic_range_p (&vr0
) && !symbolic_range_p (&vr1
))
2794 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2798 else if (code
== TRUNC_MOD_EXPR
)
2800 if (range_is_null (&vr1
))
2802 set_value_range_to_undefined (vr
);
2805 /* ABS (A % B) < ABS (B) and either
2806 0 <= A % B <= A or A <= A % B <= 0. */
2808 signop sgn
= TYPE_SIGN (expr_type
);
2809 unsigned int prec
= TYPE_PRECISION (expr_type
);
2810 wide_int wmin
, wmax
, tmp
;
2811 wide_int zero
= wi::zero (prec
);
2812 wide_int one
= wi::one (prec
);
2813 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
2815 wmax
= wi::sub (vr1
.max
, one
);
2818 tmp
= wi::sub (wi::minus_one (prec
), vr1
.min
);
2819 wmax
= wi::smax (wmax
, tmp
);
2824 wmax
= wi::max_value (prec
, sgn
);
2825 /* X % INT_MIN may be INT_MAX. */
2826 if (sgn
== UNSIGNED
)
2830 if (sgn
== UNSIGNED
)
2835 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
2838 if (wi::gts_p (tmp
, zero
))
2840 wmin
= wi::smax (wmin
, tmp
);
2844 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
2847 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
2849 wmax
= wi::min (wmax
, tmp
, sgn
);
2852 min
= wide_int_to_tree (expr_type
, wmin
);
2853 max
= wide_int_to_tree (expr_type
, wmax
);
2855 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2857 bool int_cst_range0
, int_cst_range1
;
2858 wide_int may_be_nonzero0
, may_be_nonzero1
;
2859 wide_int must_be_nonzero0
, must_be_nonzero1
;
2861 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2864 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2869 if (code
== BIT_AND_EXPR
)
2871 min
= wide_int_to_tree (expr_type
,
2872 must_be_nonzero0
& must_be_nonzero1
);
2873 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2874 /* If both input ranges contain only negative values we can
2875 truncate the result range maximum to the minimum of the
2876 input range maxima. */
2877 if (int_cst_range0
&& int_cst_range1
2878 && tree_int_cst_sgn (vr0
.max
) < 0
2879 && tree_int_cst_sgn (vr1
.max
) < 0)
2881 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2882 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2884 /* If either input range contains only non-negative values
2885 we can truncate the result range maximum to the respective
2886 maximum of the input range. */
2887 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2888 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2889 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2890 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2891 max
= wide_int_to_tree (expr_type
, wmax
);
2892 cmp
= compare_values (min
, max
);
2893 /* PR68217: In case of signed & sign-bit-CST should
2894 result in [-INF, 0] instead of [-INF, INF]. */
2895 if (cmp
== -2 || cmp
== 1)
2898 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type
) - 1,
2899 TYPE_PRECISION (expr_type
));
2900 if (!TYPE_UNSIGNED (expr_type
)
2901 && ((value_range_constant_singleton (&vr0
)
2902 && !wi::cmps (vr0
.min
, sign_bit
))
2903 || (value_range_constant_singleton (&vr1
)
2904 && !wi::cmps (vr1
.min
, sign_bit
))))
2906 min
= TYPE_MIN_VALUE (expr_type
);
2907 max
= build_int_cst (expr_type
, 0);
2911 else if (code
== BIT_IOR_EXPR
)
2913 max
= wide_int_to_tree (expr_type
,
2914 may_be_nonzero0
| may_be_nonzero1
);
2915 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2916 /* If the input ranges contain only positive values we can
2917 truncate the minimum of the result range to the maximum
2918 of the input range minima. */
2919 if (int_cst_range0
&& int_cst_range1
2920 && tree_int_cst_sgn (vr0
.min
) >= 0
2921 && tree_int_cst_sgn (vr1
.min
) >= 0)
2923 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2924 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2926 /* If either input range contains only negative values
2927 we can truncate the minimum of the result range to the
2928 respective minimum range. */
2929 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2930 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2931 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2932 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2933 min
= wide_int_to_tree (expr_type
, wmin
);
2935 else if (code
== BIT_XOR_EXPR
)
2937 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
2938 | ~(may_be_nonzero0
| may_be_nonzero1
));
2939 wide_int result_one_bits
2940 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
2941 | must_be_nonzero1
.and_not (may_be_nonzero0
));
2942 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
2943 min
= wide_int_to_tree (expr_type
, result_one_bits
);
2944 /* If the range has all positive or all negative values the
2945 result is better than VARYING. */
2946 if (tree_int_cst_sgn (min
) < 0
2947 || tree_int_cst_sgn (max
) >= 0)
2950 max
= min
= NULL_TREE
;
2956 /* If either MIN or MAX overflowed, then set the resulting range to
2958 if (min
== NULL_TREE
2959 || TREE_OVERFLOW_P (min
)
2961 || TREE_OVERFLOW_P (max
))
2963 set_value_range_to_varying (vr
);
2967 /* We punt for [-INF, +INF].
2968 We learn nothing when we have INF on both sides.
2969 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
2970 if (vrp_val_is_min (min
) && vrp_val_is_max (max
))
2972 set_value_range_to_varying (vr
);
2976 cmp
= compare_values (min
, max
);
2977 if (cmp
== -2 || cmp
== 1)
2979 /* If the new range has its limits swapped around (MIN > MAX),
2980 then the operation caused one of them to wrap around, mark
2981 the new range VARYING. */
2982 set_value_range_to_varying (vr
);
2985 set_value_range (vr
, type
, min
, max
, NULL
);
2988 /* Extract range information from a binary expression OP0 CODE OP1 based on
2989 the ranges of each of its operands with resulting type EXPR_TYPE.
2990 The resulting range is stored in *VR. */
2993 extract_range_from_binary_expr (value_range
*vr
,
2994 enum tree_code code
,
2995 tree expr_type
, tree op0
, tree op1
)
2997 value_range vr0
= VR_INITIALIZER
;
2998 value_range vr1
= VR_INITIALIZER
;
3000 /* Get value ranges for each operand. For constant operands, create
3001 a new value range with the operand to simplify processing. */
3002 if (TREE_CODE (op0
) == SSA_NAME
)
3003 vr0
= *(get_value_range (op0
));
3004 else if (is_gimple_min_invariant (op0
))
3005 set_value_range_to_value (&vr0
, op0
, NULL
);
3007 set_value_range_to_varying (&vr0
);
3009 if (TREE_CODE (op1
) == SSA_NAME
)
3010 vr1
= *(get_value_range (op1
));
3011 else if (is_gimple_min_invariant (op1
))
3012 set_value_range_to_value (&vr1
, op1
, NULL
);
3014 set_value_range_to_varying (&vr1
);
3016 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3018 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3019 and based on the other operand, for example if it was deduced from a
3020 symbolic comparison. When a bound of the range of the first operand
3021 is invariant, we set the corresponding bound of the new range to INF
3022 in order to avoid recursing on the range of the second operand. */
3023 if (vr
->type
== VR_VARYING
3024 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3025 && TREE_CODE (op1
) == SSA_NAME
3026 && vr0
.type
== VR_RANGE
3027 && symbolic_range_based_on_p (&vr0
, op1
))
3029 const bool minus_p
= (code
== MINUS_EXPR
);
3030 value_range n_vr1
= VR_INITIALIZER
;
3032 /* Try with VR0 and [-INF, OP1]. */
3033 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3034 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3036 /* Try with VR0 and [OP1, +INF]. */
3037 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3038 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3040 /* Try with VR0 and [OP1, OP1]. */
3042 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3044 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3047 if (vr
->type
== VR_VARYING
3048 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3049 && TREE_CODE (op0
) == SSA_NAME
3050 && vr1
.type
== VR_RANGE
3051 && symbolic_range_based_on_p (&vr1
, op0
))
3053 const bool minus_p
= (code
== MINUS_EXPR
);
3054 value_range n_vr0
= VR_INITIALIZER
;
3056 /* Try with [-INF, OP0] and VR1. */
3057 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3058 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3060 /* Try with [OP0, +INF] and VR1. */
3061 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3062 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3064 /* Try with [OP0, OP0] and VR1. */
3066 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3068 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3071 /* If we didn't derive a range for MINUS_EXPR, and
3072 op1's range is ~[op0,op0] or vice-versa, then we
3073 can derive a non-null range. This happens often for
3074 pointer subtraction. */
3075 if (vr
->type
== VR_VARYING
3076 && code
== MINUS_EXPR
3077 && TREE_CODE (op0
) == SSA_NAME
3078 && ((vr0
.type
== VR_ANTI_RANGE
3080 && vr0
.min
== vr0
.max
)
3081 || (vr1
.type
== VR_ANTI_RANGE
3083 && vr1
.min
== vr1
.max
)))
3084 set_value_range_to_nonnull (vr
, TREE_TYPE (op0
));
3087 /* Extract range information from a unary operation CODE based on
3088 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3089 The resulting range is stored in *VR. */
3092 extract_range_from_unary_expr (value_range
*vr
,
3093 enum tree_code code
, tree type
,
3094 value_range
*vr0_
, tree op0_type
)
3096 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3098 /* VRP only operates on integral and pointer types. */
3099 if (!(INTEGRAL_TYPE_P (op0_type
)
3100 || POINTER_TYPE_P (op0_type
))
3101 || !(INTEGRAL_TYPE_P (type
)
3102 || POINTER_TYPE_P (type
)))
3104 set_value_range_to_varying (vr
);
3108 /* If VR0 is UNDEFINED, so is the result. */
3109 if (vr0
.type
== VR_UNDEFINED
)
3111 set_value_range_to_undefined (vr
);
3115 /* Handle operations that we express in terms of others. */
3116 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3118 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3119 copy_value_range (vr
, &vr0
);
3122 else if (code
== NEGATE_EXPR
)
3124 /* -X is simply 0 - X, so re-use existing code that also handles
3125 anti-ranges fine. */
3126 value_range zero
= VR_INITIALIZER
;
3127 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3128 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3131 else if (code
== BIT_NOT_EXPR
)
3133 /* ~X is simply -1 - X, so re-use existing code that also handles
3134 anti-ranges fine. */
3135 value_range minusone
= VR_INITIALIZER
;
3136 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3137 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3138 type
, &minusone
, &vr0
);
3142 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3143 and express op ~[] as (op []') U (op []''). */
3144 if (vr0
.type
== VR_ANTI_RANGE
3145 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3147 extract_range_from_unary_expr (vr
, code
, type
, &vrtem0
, op0_type
);
3148 if (vrtem1
.type
!= VR_UNDEFINED
)
3150 value_range vrres
= VR_INITIALIZER
;
3151 extract_range_from_unary_expr (&vrres
, code
, type
,
3153 vrp_meet (vr
, &vrres
);
3158 if (CONVERT_EXPR_CODE_P (code
))
3160 tree inner_type
= op0_type
;
3161 tree outer_type
= type
;
3163 /* If the expression evaluates to a pointer, we are only interested in
3164 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3165 if (POINTER_TYPE_P (type
))
3167 if (range_is_nonnull (&vr0
))
3168 set_value_range_to_nonnull (vr
, type
);
3169 else if (range_is_null (&vr0
))
3170 set_value_range_to_null (vr
, type
);
3172 set_value_range_to_varying (vr
);
3176 /* If VR0 is varying and we increase the type precision, assume
3177 a full range for the following transformation. */
3178 if (vr0
.type
== VR_VARYING
3179 && INTEGRAL_TYPE_P (inner_type
)
3180 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3182 vr0
.type
= VR_RANGE
;
3183 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3184 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3187 /* If VR0 is a constant range or anti-range and the conversion is
3188 not truncating we can convert the min and max values and
3189 canonicalize the resulting range. Otherwise we can do the
3190 conversion if the size of the range is less than what the
3191 precision of the target type can represent and the range is
3192 not an anti-range. */
3193 if ((vr0
.type
== VR_RANGE
3194 || vr0
.type
== VR_ANTI_RANGE
)
3195 && TREE_CODE (vr0
.min
) == INTEGER_CST
3196 && TREE_CODE (vr0
.max
) == INTEGER_CST
3197 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3198 || (vr0
.type
== VR_RANGE
3199 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3200 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3201 size_int (TYPE_PRECISION (outer_type
)))))))
3203 tree new_min
, new_max
;
3204 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3206 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3208 set_and_canonicalize_value_range (vr
, vr0
.type
,
3209 new_min
, new_max
, NULL
);
3213 set_value_range_to_varying (vr
);
3216 else if (code
== ABS_EXPR
)
3221 /* Pass through vr0 in the easy cases. */
3222 if (TYPE_UNSIGNED (type
)
3223 || value_range_nonnegative_p (&vr0
))
3225 copy_value_range (vr
, &vr0
);
3229 /* For the remaining varying or symbolic ranges we can't do anything
3231 if (vr0
.type
== VR_VARYING
3232 || symbolic_range_p (&vr0
))
3234 set_value_range_to_varying (vr
);
3238 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3240 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3241 && ((vr0
.type
== VR_RANGE
3242 && vrp_val_is_min (vr0
.min
))
3243 || (vr0
.type
== VR_ANTI_RANGE
3244 && !vrp_val_is_min (vr0
.min
))))
3246 set_value_range_to_varying (vr
);
3250 /* ABS_EXPR may flip the range around, if the original range
3251 included negative values. */
3252 if (!vrp_val_is_min (vr0
.min
))
3253 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3255 min
= TYPE_MAX_VALUE (type
);
3257 if (!vrp_val_is_min (vr0
.max
))
3258 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3260 max
= TYPE_MAX_VALUE (type
);
3262 cmp
= compare_values (min
, max
);
3264 /* If a VR_ANTI_RANGEs contains zero, then we have
3265 ~[-INF, min(MIN, MAX)]. */
3266 if (vr0
.type
== VR_ANTI_RANGE
)
3268 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3270 /* Take the lower of the two values. */
3274 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3275 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3276 flag_wrapv is set and the original anti-range doesn't include
3277 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3278 if (TYPE_OVERFLOW_WRAPS (type
))
3280 tree type_min_value
= TYPE_MIN_VALUE (type
);
3282 min
= (vr0
.min
!= type_min_value
3283 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3284 build_int_cst (TREE_TYPE (type_min_value
), 1))
3288 min
= TYPE_MIN_VALUE (type
);
3292 /* All else has failed, so create the range [0, INF], even for
3293 flag_wrapv since TYPE_MIN_VALUE is in the original
3295 vr0
.type
= VR_RANGE
;
3296 min
= build_int_cst (type
, 0);
3297 max
= TYPE_MAX_VALUE (type
);
3301 /* If the range contains zero then we know that the minimum value in the
3302 range will be zero. */
3303 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3307 min
= build_int_cst (type
, 0);
3311 /* If the range was reversed, swap MIN and MAX. */
3313 std::swap (min
, max
);
3316 cmp
= compare_values (min
, max
);
3317 if (cmp
== -2 || cmp
== 1)
3319 /* If the new range has its limits swapped around (MIN > MAX),
3320 then the operation caused one of them to wrap around, mark
3321 the new range VARYING. */
3322 set_value_range_to_varying (vr
);
3325 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3329 /* For unhandled operations fall back to varying. */
3330 set_value_range_to_varying (vr
);
3335 /* Extract range information from a unary expression CODE OP0 based on
3336 the range of its operand with resulting type TYPE.
3337 The resulting range is stored in *VR. */
3340 extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
3341 tree type
, tree op0
)
3343 value_range vr0
= VR_INITIALIZER
;
3345 /* Get value ranges for the operand. For constant operands, create
3346 a new value range with the operand to simplify processing. */
3347 if (TREE_CODE (op0
) == SSA_NAME
)
3348 vr0
= *(get_value_range (op0
));
3349 else if (is_gimple_min_invariant (op0
))
3350 set_value_range_to_value (&vr0
, op0
, NULL
);
3352 set_value_range_to_varying (&vr0
);
3354 extract_range_from_unary_expr (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3358 /* Extract range information from a conditional expression STMT based on
3359 the ranges of each of its operands and the expression code. */
3362 extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
3365 value_range vr0
= VR_INITIALIZER
;
3366 value_range vr1
= VR_INITIALIZER
;
3368 /* Get value ranges for each operand. For constant operands, create
3369 a new value range with the operand to simplify processing. */
3370 op0
= gimple_assign_rhs2 (stmt
);
3371 if (TREE_CODE (op0
) == SSA_NAME
)
3372 vr0
= *(get_value_range (op0
));
3373 else if (is_gimple_min_invariant (op0
))
3374 set_value_range_to_value (&vr0
, op0
, NULL
);
3376 set_value_range_to_varying (&vr0
);
3378 op1
= gimple_assign_rhs3 (stmt
);
3379 if (TREE_CODE (op1
) == SSA_NAME
)
3380 vr1
= *(get_value_range (op1
));
3381 else if (is_gimple_min_invariant (op1
))
3382 set_value_range_to_value (&vr1
, op1
, NULL
);
3384 set_value_range_to_varying (&vr1
);
3386 /* The resulting value range is the union of the operand ranges */
3387 copy_value_range (vr
, &vr0
);
3388 vrp_meet (vr
, &vr1
);
3392 /* Extract range information from a comparison expression EXPR based
3393 on the range of its operand and the expression code. */
3396 extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
3397 tree type
, tree op0
, tree op1
)
3402 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3405 /* A disadvantage of using a special infinity as an overflow
3406 representation is that we lose the ability to record overflow
3407 when we don't have an infinity. So we have to ignore a result
3408 which relies on overflow. */
3412 /* Since this expression was found on the RHS of an assignment,
3413 its type may be different from _Bool. Convert VAL to EXPR's
3415 val
= fold_convert (type
, val
);
3416 if (is_gimple_min_invariant (val
))
3417 set_value_range_to_value (vr
, val
, vr
->equiv
);
3419 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3422 /* The result of a comparison is always true or false. */
3423 set_value_range_to_truthvalue (vr
, type
);
3426 /* Helper function for simplify_internal_call_using_ranges and
3427 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3428 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3429 always overflow. Set *OVF to true if it is known to always
3433 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3434 tree op0
, tree op1
, bool *ovf
)
3436 value_range vr0
= VR_INITIALIZER
;
3437 value_range vr1
= VR_INITIALIZER
;
3438 if (TREE_CODE (op0
) == SSA_NAME
)
3439 vr0
= *get_value_range (op0
);
3440 else if (TREE_CODE (op0
) == INTEGER_CST
)
3441 set_value_range_to_value (&vr0
, op0
, NULL
);
3443 set_value_range_to_varying (&vr0
);
3445 if (TREE_CODE (op1
) == SSA_NAME
)
3446 vr1
= *get_value_range (op1
);
3447 else if (TREE_CODE (op1
) == INTEGER_CST
)
3448 set_value_range_to_value (&vr1
, op1
, NULL
);
3450 set_value_range_to_varying (&vr1
);
3452 if (!range_int_cst_p (&vr0
)
3453 || TREE_OVERFLOW (vr0
.min
)
3454 || TREE_OVERFLOW (vr0
.max
))
3456 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3457 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3459 if (!range_int_cst_p (&vr1
)
3460 || TREE_OVERFLOW (vr1
.min
)
3461 || TREE_OVERFLOW (vr1
.max
))
3463 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3464 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3466 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3467 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3468 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3469 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3471 if (subcode
== MULT_EXPR
)
3473 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3474 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3479 /* So far we found that there is an overflow on the boundaries.
3480 That doesn't prove that there is an overflow even for all values
3481 in between the boundaries. For that compute widest_int range
3482 of the result and see if it doesn't overlap the range of
3484 widest_int wmin
, wmax
;
3487 w
[0] = wi::to_widest (vr0
.min
);
3488 w
[1] = wi::to_widest (vr0
.max
);
3489 w
[2] = wi::to_widest (vr1
.min
);
3490 w
[3] = wi::to_widest (vr1
.max
);
3491 for (i
= 0; i
< 4; i
++)
3497 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3500 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3503 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3515 wmin
= wi::smin (wmin
, wt
);
3516 wmax
= wi::smax (wmax
, wt
);
3519 /* The result of op0 CODE op1 is known to be in range
3521 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3522 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3523 /* If all values in [wmin, wmax] are smaller than
3524 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3525 the arithmetic operation will always overflow. */
3526 if (wmax
< wtmin
|| wmin
> wtmax
)
3533 /* Try to derive a nonnegative or nonzero range out of STMT relying
3534 primarily on generic routines in fold in conjunction with range data.
3535 Store the result in *VR */
3538 extract_range_basic (value_range
*vr
, gimple
*stmt
)
3541 tree type
= gimple_expr_type (stmt
);
3543 if (is_gimple_call (stmt
))
3546 int mini
, maxi
, zerov
= 0, prec
;
3547 enum tree_code subcode
= ERROR_MARK
;
3548 combined_fn cfn
= gimple_call_combined_fn (stmt
);
3552 case CFN_BUILT_IN_CONSTANT_P
:
3553 /* If the call is __builtin_constant_p and the argument is a
3554 function parameter resolve it to false. This avoids bogus
3555 array bound warnings.
3556 ??? We could do this as early as inlining is finished. */
3557 arg
= gimple_call_arg (stmt
, 0);
3558 if (TREE_CODE (arg
) == SSA_NAME
3559 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3560 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
3561 && cfun
->after_inlining
)
3563 set_value_range_to_null (vr
, type
);
3567 /* Both __builtin_ffs* and __builtin_popcount return
3571 arg
= gimple_call_arg (stmt
, 0);
3572 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3575 if (TREE_CODE (arg
) == SSA_NAME
)
3577 value_range
*vr0
= get_value_range (arg
);
3578 /* If arg is non-zero, then ffs or popcount
3580 if ((vr0
->type
== VR_RANGE
3581 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3582 || (vr0
->type
== VR_ANTI_RANGE
3583 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3585 /* If some high bits are known to be zero,
3586 we can decrease the maximum. */
3587 if (vr0
->type
== VR_RANGE
3588 && TREE_CODE (vr0
->max
) == INTEGER_CST
3589 && !operand_less_p (vr0
->min
,
3590 build_zero_cst (TREE_TYPE (vr0
->min
))))
3591 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3594 /* __builtin_parity* returns [0, 1]. */
3599 /* __builtin_c[lt]z* return [0, prec-1], except for
3600 when the argument is 0, but that is undefined behavior.
3601 On many targets where the CLZ RTL or optab value is defined
3602 for 0 the value is prec, so include that in the range
3605 arg
= gimple_call_arg (stmt
, 0);
3606 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3609 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3611 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3613 /* Handle only the single common value. */
3615 /* Magic value to give up, unless vr0 proves
3618 if (TREE_CODE (arg
) == SSA_NAME
)
3620 value_range
*vr0
= get_value_range (arg
);
3621 /* From clz of VR_RANGE minimum we can compute
3623 if (vr0
->type
== VR_RANGE
3624 && TREE_CODE (vr0
->min
) == INTEGER_CST
)
3626 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3630 else if (vr0
->type
== VR_ANTI_RANGE
3631 && integer_zerop (vr0
->min
))
3638 /* From clz of VR_RANGE maximum we can compute
3640 if (vr0
->type
== VR_RANGE
3641 && TREE_CODE (vr0
->max
) == INTEGER_CST
)
3643 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3651 /* __builtin_ctz* return [0, prec-1], except for
3652 when the argument is 0, but that is undefined behavior.
3653 If there is a ctz optab for this mode and
3654 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3655 otherwise just assume 0 won't be seen. */
3657 arg
= gimple_call_arg (stmt
, 0);
3658 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3661 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3663 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3666 /* Handle only the two common values. */
3669 else if (zerov
== prec
)
3672 /* Magic value to give up, unless vr0 proves
3676 if (TREE_CODE (arg
) == SSA_NAME
)
3678 value_range
*vr0
= get_value_range (arg
);
3679 /* If arg is non-zero, then use [0, prec - 1]. */
3680 if ((vr0
->type
== VR_RANGE
3681 && integer_nonzerop (vr0
->min
))
3682 || (vr0
->type
== VR_ANTI_RANGE
3683 && integer_zerop (vr0
->min
)))
3688 /* If some high bits are known to be zero,
3689 we can decrease the result maximum. */
3690 if (vr0
->type
== VR_RANGE
3691 && TREE_CODE (vr0
->max
) == INTEGER_CST
)
3693 maxi
= tree_floor_log2 (vr0
->max
);
3694 /* For vr0 [0, 0] give up. */
3702 /* __builtin_clrsb* returns [0, prec-1]. */
3704 arg
= gimple_call_arg (stmt
, 0);
3705 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3710 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3711 build_int_cst (type
, maxi
), NULL
);
3713 case CFN_UBSAN_CHECK_ADD
:
3714 subcode
= PLUS_EXPR
;
3716 case CFN_UBSAN_CHECK_SUB
:
3717 subcode
= MINUS_EXPR
;
3719 case CFN_UBSAN_CHECK_MUL
:
3720 subcode
= MULT_EXPR
;
3722 case CFN_GOACC_DIM_SIZE
:
3723 case CFN_GOACC_DIM_POS
:
3724 /* Optimizing these two internal functions helps the loop
3725 optimizer eliminate outer comparisons. Size is [1,N]
3726 and pos is [0,N-1]. */
3728 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
3729 int axis
= oacc_get_ifn_dim_arg (stmt
);
3730 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
3733 /* If it's dynamic, the backend might know a hardware
3735 size
= targetm
.goacc
.dim_limit (axis
);
3737 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
3738 set_value_range (vr
, VR_RANGE
,
3739 build_int_cst (type
, is_pos
? 0 : 1),
3740 size
? build_int_cst (type
, size
- is_pos
)
3741 : vrp_val_max (type
), NULL
);
3744 case CFN_BUILT_IN_STRLEN
:
3745 if (tree lhs
= gimple_call_lhs (stmt
))
3746 if (ptrdiff_type_node
3747 && (TYPE_PRECISION (ptrdiff_type_node
)
3748 == TYPE_PRECISION (TREE_TYPE (lhs
))))
3750 tree type
= TREE_TYPE (lhs
);
3751 tree max
= vrp_val_max (ptrdiff_type_node
);
3752 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
3753 tree range_min
= build_zero_cst (type
);
3754 tree range_max
= wide_int_to_tree (type
, wmax
- 1);
3755 set_value_range (vr
, VR_RANGE
, range_min
, range_max
, NULL
);
3762 if (subcode
!= ERROR_MARK
)
3764 bool saved_flag_wrapv
= flag_wrapv
;
3765 /* Pretend the arithmetics is wrapping. If there is
3766 any overflow, we'll complain, but will actually do
3767 wrapping operation. */
3769 extract_range_from_binary_expr (vr
, subcode
, type
,
3770 gimple_call_arg (stmt
, 0),
3771 gimple_call_arg (stmt
, 1));
3772 flag_wrapv
= saved_flag_wrapv
;
3774 /* If for both arguments vrp_valueize returned non-NULL,
3775 this should have been already folded and if not, it
3776 wasn't folded because of overflow. Avoid removing the
3777 UBSAN_CHECK_* calls in that case. */
3778 if (vr
->type
== VR_RANGE
3779 && (vr
->min
== vr
->max
3780 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3781 set_value_range_to_varying (vr
);
3785 /* Handle extraction of the two results (result of arithmetics and
3786 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
3787 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
3788 else if (is_gimple_assign (stmt
)
3789 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
3790 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
3791 && INTEGRAL_TYPE_P (type
))
3793 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3794 tree op
= gimple_assign_rhs1 (stmt
);
3795 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
3797 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
3798 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
3800 enum tree_code subcode
= ERROR_MARK
;
3801 switch (gimple_call_internal_fn (g
))
3803 case IFN_ADD_OVERFLOW
:
3804 subcode
= PLUS_EXPR
;
3806 case IFN_SUB_OVERFLOW
:
3807 subcode
= MINUS_EXPR
;
3809 case IFN_MUL_OVERFLOW
:
3810 subcode
= MULT_EXPR
;
3812 case IFN_ATOMIC_COMPARE_EXCHANGE
:
3813 if (code
== IMAGPART_EXPR
)
3815 /* This is the boolean return value whether compare and
3816 exchange changed anything or not. */
3817 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
3818 build_int_cst (type
, 1), NULL
);
3825 if (subcode
!= ERROR_MARK
)
3827 tree op0
= gimple_call_arg (g
, 0);
3828 tree op1
= gimple_call_arg (g
, 1);
3829 if (code
== IMAGPART_EXPR
)
3832 if (check_for_binary_op_overflow (subcode
, type
,
3834 set_value_range_to_value (vr
,
3835 build_int_cst (type
, ovf
),
3837 else if (TYPE_PRECISION (type
) == 1
3838 && !TYPE_UNSIGNED (type
))
3839 set_value_range_to_varying (vr
);
3841 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
3842 build_int_cst (type
, 1), NULL
);
3844 else if (types_compatible_p (type
, TREE_TYPE (op0
))
3845 && types_compatible_p (type
, TREE_TYPE (op1
)))
3847 bool saved_flag_wrapv
= flag_wrapv
;
3848 /* Pretend the arithmetics is wrapping. If there is
3849 any overflow, IMAGPART_EXPR will be set. */
3851 extract_range_from_binary_expr (vr
, subcode
, type
,
3853 flag_wrapv
= saved_flag_wrapv
;
3857 value_range vr0
= VR_INITIALIZER
;
3858 value_range vr1
= VR_INITIALIZER
;
3859 bool saved_flag_wrapv
= flag_wrapv
;
3860 /* Pretend the arithmetics is wrapping. If there is
3861 any overflow, IMAGPART_EXPR will be set. */
3863 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
3865 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
3867 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
3869 flag_wrapv
= saved_flag_wrapv
;
3876 if (INTEGRAL_TYPE_P (type
)
3877 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3878 set_value_range_to_nonnegative (vr
, type
);
3879 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3881 set_value_range_to_nonnull (vr
, type
);
3883 set_value_range_to_varying (vr
);
3887 /* Try to compute a useful range out of assignment STMT and store it
3891 extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
3893 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3895 if (code
== ASSERT_EXPR
)
3896 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3897 else if (code
== SSA_NAME
)
3898 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3899 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3900 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3901 gimple_expr_type (stmt
),
3902 gimple_assign_rhs1 (stmt
),
3903 gimple_assign_rhs2 (stmt
));
3904 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3905 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3906 gimple_expr_type (stmt
),
3907 gimple_assign_rhs1 (stmt
));
3908 else if (code
== COND_EXPR
)
3909 extract_range_from_cond_expr (vr
, stmt
);
3910 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3911 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3912 gimple_expr_type (stmt
),
3913 gimple_assign_rhs1 (stmt
),
3914 gimple_assign_rhs2 (stmt
));
3915 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3916 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3917 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3919 set_value_range_to_varying (vr
);
3921 if (vr
->type
== VR_VARYING
)
3922 extract_range_basic (vr
, stmt
);
3925 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3926 would be profitable to adjust VR using scalar evolution information
3927 for VAR. If so, update VR with the new limits. */
3930 adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
3931 gimple
*stmt
, tree var
)
3933 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3934 enum ev_direction dir
;
3936 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3937 better opportunities than a regular range, but I'm not sure. */
3938 if (vr
->type
== VR_ANTI_RANGE
)
3941 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3943 /* Like in PR19590, scev can return a constant function. */
3944 if (is_gimple_min_invariant (chrec
))
3946 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3950 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3953 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3954 tem
= op_with_constant_singleton_value_range (init
);
3957 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3958 tem
= op_with_constant_singleton_value_range (step
);
3962 /* If STEP is symbolic, we can't know whether INIT will be the
3963 minimum or maximum value in the range. Also, unless INIT is
3964 a simple expression, compare_values and possibly other functions
3965 in tree-vrp won't be able to handle it. */
3966 if (step
== NULL_TREE
3967 || !is_gimple_min_invariant (step
)
3968 || !valid_value_p (init
))
3971 dir
= scev_direction (chrec
);
3972 if (/* Do not adjust ranges if we do not know whether the iv increases
3973 or decreases, ... */
3974 dir
== EV_DIR_UNKNOWN
3975 /* ... or if it may wrap. */
3976 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
3977 get_chrec_loop (chrec
), true))
3980 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
3981 negative_overflow_infinity and positive_overflow_infinity,
3982 because we have concluded that the loop probably does not
3985 type
= TREE_TYPE (var
);
3986 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
3987 tmin
= lower_bound_in_type (type
, type
);
3989 tmin
= TYPE_MIN_VALUE (type
);
3990 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
3991 tmax
= upper_bound_in_type (type
, type
);
3993 tmax
= TYPE_MAX_VALUE (type
);
3995 /* Try to use estimated number of iterations for the loop to constrain the
3996 final value in the evolution. */
3997 if (TREE_CODE (step
) == INTEGER_CST
3998 && is_gimple_val (init
)
3999 && (TREE_CODE (init
) != SSA_NAME
4000 || get_value_range (init
)->type
== VR_RANGE
))
4004 /* We are only entering here for loop header PHI nodes, so using
4005 the number of latch executions is the correct thing to use. */
4006 if (max_loop_iterations (loop
, &nit
))
4008 value_range maxvr
= VR_INITIALIZER
;
4009 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4012 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4014 /* If the multiplication overflowed we can't do a meaningful
4015 adjustment. Likewise if the result doesn't fit in the type
4016 of the induction variable. For a signed type we have to
4017 check whether the result has the expected signedness which
4018 is that of the step as number of iterations is unsigned. */
4020 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4022 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4024 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4025 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4026 TREE_TYPE (init
), init
, tem
);
4027 /* Likewise if the addition did. */
4028 if (maxvr
.type
== VR_RANGE
)
4030 value_range initvr
= VR_INITIALIZER
;
4032 if (TREE_CODE (init
) == SSA_NAME
)
4033 initvr
= *(get_value_range (init
));
4034 else if (is_gimple_min_invariant (init
))
4035 set_value_range_to_value (&initvr
, init
, NULL
);
4039 /* Check if init + nit * step overflows. Though we checked
4040 scev {init, step}_loop doesn't wrap, it is not enough
4041 because the loop may exit immediately. Overflow could
4042 happen in the plus expression in this case. */
4043 if ((dir
== EV_DIR_DECREASES
4044 && compare_values (maxvr
.min
, initvr
.min
) != -1)
4045 || (dir
== EV_DIR_GROWS
4046 && compare_values (maxvr
.max
, initvr
.max
) != 1))
4056 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4061 /* For VARYING or UNDEFINED ranges, just about anything we get
4062 from scalar evolutions should be better. */
4064 if (dir
== EV_DIR_DECREASES
)
4069 else if (vr
->type
== VR_RANGE
)
4074 if (dir
== EV_DIR_DECREASES
)
4076 /* INIT is the maximum value. If INIT is lower than VR->MAX
4077 but no smaller than VR->MIN, set VR->MAX to INIT. */
4078 if (compare_values (init
, max
) == -1)
4081 /* According to the loop information, the variable does not
4083 if (compare_values (min
, tmin
) == -1)
4089 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4090 if (compare_values (init
, min
) == 1)
4093 if (compare_values (tmax
, max
) == -1)
4100 /* If we just created an invalid range with the minimum
4101 greater than the maximum, we fail conservatively.
4102 This should happen only in unreachable
4103 parts of code, or for invalid programs. */
4104 if (compare_values (min
, max
) == 1)
4107 /* Even for valid range info, sometimes overflow flag will leak in.
4108 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4110 if (TREE_OVERFLOW_P (min
))
4111 min
= drop_tree_overflow (min
);
4112 if (TREE_OVERFLOW_P (max
))
4113 max
= drop_tree_overflow (max
);
4115 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4119 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4121 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4122 all the values in the ranges.
4124 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4126 - Return NULL_TREE if it is not always possible to determine the
4127 value of the comparison.
4129 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4130 overflow infinity was used in the test. */
4134 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
4135 bool *strict_overflow_p
)
4137 /* VARYING or UNDEFINED ranges cannot be compared. */
4138 if (vr0
->type
== VR_VARYING
4139 || vr0
->type
== VR_UNDEFINED
4140 || vr1
->type
== VR_VARYING
4141 || vr1
->type
== VR_UNDEFINED
)
4144 /* Anti-ranges need to be handled separately. */
4145 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4147 /* If both are anti-ranges, then we cannot compute any
4149 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4152 /* These comparisons are never statically computable. */
4159 /* Equality can be computed only between a range and an
4160 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4161 if (vr0
->type
== VR_RANGE
)
4163 /* To simplify processing, make VR0 the anti-range. */
4164 value_range
*tmp
= vr0
;
4169 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4171 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4172 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4173 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4178 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4179 operands around and change the comparison code. */
4180 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4182 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4183 std::swap (vr0
, vr1
);
4186 if (comp
== EQ_EXPR
)
4188 /* Equality may only be computed if both ranges represent
4189 exactly one value. */
4190 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4191 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4193 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4195 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4197 if (cmp_min
== 0 && cmp_max
== 0)
4198 return boolean_true_node
;
4199 else if (cmp_min
!= -2 && cmp_max
!= -2)
4200 return boolean_false_node
;
4202 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4203 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4204 strict_overflow_p
) == 1
4205 || compare_values_warnv (vr1
->min
, vr0
->max
,
4206 strict_overflow_p
) == 1)
4207 return boolean_false_node
;
4211 else if (comp
== NE_EXPR
)
4215 /* If VR0 is completely to the left or completely to the right
4216 of VR1, they are always different. Notice that we need to
4217 make sure that both comparisons yield similar results to
4218 avoid comparing values that cannot be compared at
4220 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4221 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4222 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4223 return boolean_true_node
;
4225 /* If VR0 and VR1 represent a single value and are identical,
4227 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4228 strict_overflow_p
) == 0
4229 && compare_values_warnv (vr1
->min
, vr1
->max
,
4230 strict_overflow_p
) == 0
4231 && compare_values_warnv (vr0
->min
, vr1
->min
,
4232 strict_overflow_p
) == 0
4233 && compare_values_warnv (vr0
->max
, vr1
->max
,
4234 strict_overflow_p
) == 0)
4235 return boolean_false_node
;
4237 /* Otherwise, they may or may not be different. */
4241 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4245 /* If VR0 is to the left of VR1, return true. */
4246 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4247 if ((comp
== LT_EXPR
&& tst
== -1)
4248 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4249 return boolean_true_node
;
4251 /* If VR0 is to the right of VR1, return false. */
4252 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4253 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4254 || (comp
== LE_EXPR
&& tst
== 1))
4255 return boolean_false_node
;
4257 /* Otherwise, we don't know. */
4265 /* Given a value range VR, a value VAL and a comparison code COMP, return
4266 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4267 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4268 always returns false. Return NULL_TREE if it is not always
4269 possible to determine the value of the comparison. Also set
4270 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4271 infinity was used in the test. */
4274 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
4275 bool *strict_overflow_p
)
4277 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4280 /* Anti-ranges need to be handled separately. */
4281 if (vr
->type
== VR_ANTI_RANGE
)
4283 /* For anti-ranges, the only predicates that we can compute at
4284 compile time are equality and inequality. */
4291 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4292 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4293 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4298 if (comp
== EQ_EXPR
)
4300 /* EQ_EXPR may only be computed if VR represents exactly
4302 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4304 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4306 return boolean_true_node
;
4307 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4308 return boolean_false_node
;
4310 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4311 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4312 return boolean_false_node
;
4316 else if (comp
== NE_EXPR
)
4318 /* If VAL is not inside VR, then they are always different. */
4319 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4320 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4321 return boolean_true_node
;
4323 /* If VR represents exactly one value equal to VAL, then return
4325 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4326 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4327 return boolean_false_node
;
4329 /* Otherwise, they may or may not be different. */
4332 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4336 /* If VR is to the left of VAL, return true. */
4337 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4338 if ((comp
== LT_EXPR
&& tst
== -1)
4339 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4340 return boolean_true_node
;
4342 /* If VR is to the right of VAL, return false. */
4343 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4344 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4345 || (comp
== LE_EXPR
&& tst
== 1))
4346 return boolean_false_node
;
4348 /* Otherwise, we don't know. */
4351 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4355 /* If VR is to the right of VAL, return true. */
4356 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4357 if ((comp
== GT_EXPR
&& tst
== 1)
4358 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4359 return boolean_true_node
;
4361 /* If VR is to the left of VAL, return false. */
4362 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4363 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4364 || (comp
== GE_EXPR
&& tst
== -1))
4365 return boolean_false_node
;
4367 /* Otherwise, we don't know. */
4375 /* Debugging dumps. */
4377 void dump_value_range (FILE *, const value_range
*);
4378 void debug_value_range (value_range
*);
4379 void dump_all_value_ranges (FILE *);
4380 void debug_all_value_ranges (void);
4381 void dump_vr_equiv (FILE *, bitmap
);
4382 void debug_vr_equiv (bitmap
);
4385 /* Dump value range VR to FILE. */
4388 dump_value_range (FILE *file
, const value_range
*vr
)
4391 fprintf (file
, "[]");
4392 else if (vr
->type
== VR_UNDEFINED
)
4393 fprintf (file
, "UNDEFINED");
4394 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4396 tree type
= TREE_TYPE (vr
->min
);
4398 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4400 if (INTEGRAL_TYPE_P (type
)
4401 && !TYPE_UNSIGNED (type
)
4402 && vrp_val_is_min (vr
->min
))
4403 fprintf (file
, "-INF");
4405 print_generic_expr (file
, vr
->min
, 0);
4407 fprintf (file
, ", ");
4409 if (INTEGRAL_TYPE_P (type
)
4410 && vrp_val_is_max (vr
->max
))
4411 fprintf (file
, "+INF");
4413 print_generic_expr (file
, vr
->max
, 0);
4415 fprintf (file
, "]");
4422 fprintf (file
, " EQUIVALENCES: { ");
4424 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4426 print_generic_expr (file
, ssa_name (i
), 0);
4427 fprintf (file
, " ");
4431 fprintf (file
, "} (%u elements)", c
);
4434 else if (vr
->type
== VR_VARYING
)
4435 fprintf (file
, "VARYING");
4437 fprintf (file
, "INVALID RANGE");
4441 /* Dump value range VR to stderr. */
4444 debug_value_range (value_range
*vr
)
4446 dump_value_range (stderr
, vr
);
4447 fprintf (stderr
, "\n");
4451 /* Dump value ranges of all SSA_NAMEs to FILE. */
4454 dump_all_value_ranges (FILE *file
)
4458 for (i
= 0; i
< num_vr_values
; i
++)
4462 print_generic_expr (file
, ssa_name (i
), 0);
4463 fprintf (file
, ": ");
4464 dump_value_range (file
, vr_value
[i
]);
4465 fprintf (file
, "\n");
4469 fprintf (file
, "\n");
4473 /* Dump all value ranges to stderr. */
4476 debug_all_value_ranges (void)
4478 dump_all_value_ranges (stderr
);
4482 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4483 create a new SSA name N and return the assertion assignment
4484 'N = ASSERT_EXPR <V, V OP W>'. */
4487 build_assert_expr_for (tree cond
, tree v
)
4492 gcc_assert (TREE_CODE (v
) == SSA_NAME
4493 && COMPARISON_CLASS_P (cond
));
4495 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4496 assertion
= gimple_build_assign (NULL_TREE
, a
);
4498 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4499 operand of the ASSERT_EXPR. Create it so the new name and the old one
4500 are registered in the replacement table so that we can fix the SSA web
4501 after adding all the ASSERT_EXPRs. */
4502 create_new_def_for (v
, assertion
, NULL
);
4508 /* Return false if EXPR is a predicate expression involving floating
4512 fp_predicate (gimple
*stmt
)
4514 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4516 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4519 /* If the range of values taken by OP can be inferred after STMT executes,
4520 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4521 describes the inferred range. Return true if a range could be
4525 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
4528 *comp_code_p
= ERROR_MARK
;
4530 /* Do not attempt to infer anything in names that flow through
4532 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4535 /* If STMT is the last statement of a basic block with no normal
4536 successors, there is no point inferring anything about any of its
4537 operands. We would not be able to find a proper insertion point
4538 for the assertion, anyway. */
4539 if (stmt_ends_bb_p (stmt
))
4544 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4545 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
4551 if (infer_nonnull_range (stmt
, op
))
4553 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4554 *comp_code_p
= NE_EXPR
;
4562 void dump_asserts_for (FILE *, tree
);
4563 void debug_asserts_for (tree
);
4564 void dump_all_asserts (FILE *);
4565 void debug_all_asserts (void);
4567 /* Dump all the registered assertions for NAME to FILE. */
4570 dump_asserts_for (FILE *file
, tree name
)
4574 fprintf (file
, "Assertions to be inserted for ");
4575 print_generic_expr (file
, name
, 0);
4576 fprintf (file
, "\n");
4578 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4581 fprintf (file
, "\t");
4582 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4583 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4586 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4587 loc
->e
->dest
->index
);
4588 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4590 fprintf (file
, "\n\tPREDICATE: ");
4591 print_generic_expr (file
, loc
->expr
, 0);
4592 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4593 print_generic_expr (file
, loc
->val
, 0);
4594 fprintf (file
, "\n\n");
4598 fprintf (file
, "\n");
4602 /* Dump all the registered assertions for NAME to stderr. */
4605 debug_asserts_for (tree name
)
4607 dump_asserts_for (stderr
, name
);
4611 /* Dump all the registered assertions for all the names to FILE. */
4614 dump_all_asserts (FILE *file
)
4619 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4620 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4621 dump_asserts_for (file
, ssa_name (i
));
4622 fprintf (file
, "\n");
4626 /* Dump all the registered assertions for all the names to stderr. */
4629 debug_all_asserts (void)
4631 dump_all_asserts (stderr
);
4634 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
4637 add_assert_info (vec
<assert_info
> &asserts
,
4638 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
4641 info
.comp_code
= comp_code
;
4645 asserts
.safe_push (info
);
4648 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4649 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4650 E->DEST, then register this location as a possible insertion point
4651 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4653 BB, E and SI provide the exact insertion point for the new
4654 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4655 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4656 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4657 must not be NULL. */
4660 register_new_assert_for (tree name
, tree expr
,
4661 enum tree_code comp_code
,
4665 gimple_stmt_iterator si
)
4667 assert_locus
*n
, *loc
, *last_loc
;
4668 basic_block dest_bb
;
4670 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4673 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4674 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4676 /* Never build an assert comparing against an integer constant with
4677 TREE_OVERFLOW set. This confuses our undefined overflow warning
4679 if (TREE_OVERFLOW_P (val
))
4680 val
= drop_tree_overflow (val
);
4682 /* The new assertion A will be inserted at BB or E. We need to
4683 determine if the new location is dominated by a previously
4684 registered location for A. If we are doing an edge insertion,
4685 assume that A will be inserted at E->DEST. Note that this is not
4688 If E is a critical edge, it will be split. But even if E is
4689 split, the new block will dominate the same set of blocks that
4692 The reverse, however, is not true, blocks dominated by E->DEST
4693 will not be dominated by the new block created to split E. So,
4694 if the insertion location is on a critical edge, we will not use
4695 the new location to move another assertion previously registered
4696 at a block dominated by E->DEST. */
4697 dest_bb
= (bb
) ? bb
: e
->dest
;
4699 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4700 VAL at a block dominating DEST_BB, then we don't need to insert a new
4701 one. Similarly, if the same assertion already exists at a block
4702 dominated by DEST_BB and the new location is not on a critical
4703 edge, then update the existing location for the assertion (i.e.,
4704 move the assertion up in the dominance tree).
4706 Note, this is implemented as a simple linked list because there
4707 should not be more than a handful of assertions registered per
4708 name. If this becomes a performance problem, a table hashed by
4709 COMP_CODE and VAL could be implemented. */
4710 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4714 if (loc
->comp_code
== comp_code
4716 || operand_equal_p (loc
->val
, val
, 0))
4717 && (loc
->expr
== expr
4718 || operand_equal_p (loc
->expr
, expr
, 0)))
4720 /* If E is not a critical edge and DEST_BB
4721 dominates the existing location for the assertion, move
4722 the assertion up in the dominance tree by updating its
4723 location information. */
4724 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4725 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4734 /* Update the last node of the list and move to the next one. */
4739 /* If we didn't find an assertion already registered for
4740 NAME COMP_CODE VAL, add a new one at the end of the list of
4741 assertions associated with NAME. */
4742 n
= XNEW (struct assert_locus
);
4746 n
->comp_code
= comp_code
;
4754 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4756 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4759 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4760 Extract a suitable test code and value and store them into *CODE_P and
4761 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4763 If no extraction was possible, return FALSE, otherwise return TRUE.
4765 If INVERT is true, then we invert the result stored into *CODE_P. */
4768 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4769 tree cond_op0
, tree cond_op1
,
4770 bool invert
, enum tree_code
*code_p
,
4773 enum tree_code comp_code
;
4776 /* Otherwise, we have a comparison of the form NAME COMP VAL
4777 or VAL COMP NAME. */
4778 if (name
== cond_op1
)
4780 /* If the predicate is of the form VAL COMP NAME, flip
4781 COMP around because we need to register NAME as the
4782 first operand in the predicate. */
4783 comp_code
= swap_tree_comparison (cond_code
);
4786 else if (name
== cond_op0
)
4788 /* The comparison is of the form NAME COMP VAL, so the
4789 comparison code remains unchanged. */
4790 comp_code
= cond_code
;
4796 /* Invert the comparison code as necessary. */
4798 comp_code
= invert_tree_comparison (comp_code
, 0);
4800 /* VRP only handles integral and pointer types. */
4801 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
4802 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
4805 /* Do not register always-false predicates.
4806 FIXME: this works around a limitation in fold() when dealing with
4807 enumerations. Given 'enum { N1, N2 } x;', fold will not
4808 fold 'if (x > N2)' to 'if (0)'. */
4809 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4810 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4812 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4813 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4815 if (comp_code
== GT_EXPR
4817 || compare_values (val
, max
) == 0))
4820 if (comp_code
== LT_EXPR
4822 || compare_values (val
, min
) == 0))
4825 *code_p
= comp_code
;
4830 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4831 (otherwise return VAL). VAL and MASK must be zero-extended for
4832 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4833 (to transform signed values into unsigned) and at the end xor
4837 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4838 const wide_int
&sgnbit
, unsigned int prec
)
4840 wide_int bit
= wi::one (prec
), res
;
4843 wide_int val
= val_in
^ sgnbit
;
4844 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4847 if ((res
& bit
) == 0)
4850 res
= (val
+ bit
).and_not (res
);
4852 if (wi::gtu_p (res
, val
))
4853 return res
^ sgnbit
;
4855 return val
^ sgnbit
;
4858 /* Helper for overflow_comparison_p
4860 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4861 OP1's defining statement to see if it ultimately has the form
4862 OP0 CODE (OP0 PLUS INTEGER_CST)
4864 If so, return TRUE indicating this is an overflow test and store into
4865 *NEW_CST an updated constant that can be used in a narrowed range test.
4867 REVERSED indicates if the comparison was originally:
4871 This affects how we build the updated constant. */
4874 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
4875 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
4877 /* See if this is a relational operation between two SSA_NAMES with
4878 unsigned, overflow wrapping values. If so, check it more deeply. */
4879 if ((code
== LT_EXPR
|| code
== LE_EXPR
4880 || code
== GE_EXPR
|| code
== GT_EXPR
)
4881 && TREE_CODE (op0
) == SSA_NAME
4882 && TREE_CODE (op1
) == SSA_NAME
4883 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
4884 && TYPE_UNSIGNED (TREE_TYPE (op0
))
4885 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
4887 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
4889 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
4890 if (follow_assert_exprs
)
4892 while (gimple_assign_single_p (op1_def
)
4893 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
4895 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
4896 if (TREE_CODE (op1
) != SSA_NAME
)
4898 op1_def
= SSA_NAME_DEF_STMT (op1
);
4902 /* Now look at the defining statement of OP1 to see if it adds
4903 or subtracts a nonzero constant from another operand. */
4905 && is_gimple_assign (op1_def
)
4906 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
4907 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
4908 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
4910 tree target
= gimple_assign_rhs1 (op1_def
);
4912 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
4913 for one where TARGET appears on the RHS. */
4914 if (follow_assert_exprs
)
4916 /* Now see if that "other operand" is op0, following the chain
4917 of ASSERT_EXPRs if necessary. */
4918 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
4919 while (op0
!= target
4920 && gimple_assign_single_p (op0_def
)
4921 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
4923 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
4924 if (TREE_CODE (op0
) != SSA_NAME
)
4926 op0_def
= SSA_NAME_DEF_STMT (op0
);
4930 /* If we did not find our target SSA_NAME, then this is not
4931 an overflow test. */
4935 tree type
= TREE_TYPE (op0
);
4936 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
4937 tree inc
= gimple_assign_rhs2 (op1_def
);
4939 *new_cst
= wide_int_to_tree (type
, max
+ inc
);
4941 *new_cst
= wide_int_to_tree (type
, max
- inc
);
4948 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4949 OP1's defining statement to see if it ultimately has the form
4950 OP0 CODE (OP0 PLUS INTEGER_CST)
4952 If so, return TRUE indicating this is an overflow test and store into
4953 *NEW_CST an updated constant that can be used in a narrowed range test.
4955 These statements are left as-is in the IL to facilitate discovery of
4956 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
4957 the alternate range representation is often useful within VRP. */
4960 overflow_comparison_p (tree_code code
, tree name
, tree val
,
4961 bool use_equiv_p
, tree
*new_cst
)
4963 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
4965 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
4966 use_equiv_p
, true, new_cst
);
4970 /* Try to register an edge assertion for SSA name NAME on edge E for
4971 the condition COND contributing to the conditional jump pointed to by BSI.
4972 Invert the condition COND if INVERT is true. */
4975 register_edge_assert_for_2 (tree name
, edge e
,
4976 enum tree_code cond_code
,
4977 tree cond_op0
, tree cond_op1
, bool invert
,
4978 vec
<assert_info
> &asserts
)
4981 enum tree_code comp_code
;
4983 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4986 invert
, &comp_code
, &val
))
4989 /* Queue the assert. */
4991 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
4993 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
4994 ? GT_EXPR
: LE_EXPR
);
4995 add_assert_info (asserts
, name
, name
, new_code
, x
);
4997 add_assert_info (asserts
, name
, name
, comp_code
, val
);
4999 /* In the case of NAME <= CST and NAME being defined as
5000 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5001 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5002 This catches range and anti-range tests. */
5003 if ((comp_code
== LE_EXPR
5004 || comp_code
== GT_EXPR
)
5005 && TREE_CODE (val
) == INTEGER_CST
5006 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5008 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5009 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5011 /* Extract CST2 from the (optional) addition. */
5012 if (is_gimple_assign (def_stmt
)
5013 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5015 name2
= gimple_assign_rhs1 (def_stmt
);
5016 cst2
= gimple_assign_rhs2 (def_stmt
);
5017 if (TREE_CODE (name2
) == SSA_NAME
5018 && TREE_CODE (cst2
) == INTEGER_CST
)
5019 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5022 /* Extract NAME2 from the (optional) sign-changing cast. */
5023 if (gimple_assign_cast_p (def_stmt
))
5025 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5026 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5027 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5028 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5029 name3
= gimple_assign_rhs1 (def_stmt
);
5032 /* If name3 is used later, create an ASSERT_EXPR for it. */
5033 if (name3
!= NULL_TREE
5034 && TREE_CODE (name3
) == SSA_NAME
5035 && (cst2
== NULL_TREE
5036 || TREE_CODE (cst2
) == INTEGER_CST
)
5037 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
5041 /* Build an expression for the range test. */
5042 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5043 if (cst2
!= NULL_TREE
)
5044 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5048 fprintf (dump_file
, "Adding assert for ");
5049 print_generic_expr (dump_file
, name3
, 0);
5050 fprintf (dump_file
, " from ");
5051 print_generic_expr (dump_file
, tmp
, 0);
5052 fprintf (dump_file
, "\n");
5055 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
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
)))
5066 /* Build an expression for the range test. */
5068 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5069 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5070 if (cst2
!= NULL_TREE
)
5071 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5075 fprintf (dump_file
, "Adding assert for ");
5076 print_generic_expr (dump_file
, name2
, 0);
5077 fprintf (dump_file
, " from ");
5078 print_generic_expr (dump_file
, tmp
, 0);
5079 fprintf (dump_file
, "\n");
5082 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
5086 /* In the case of post-in/decrement tests like if (i++) ... and uses
5087 of the in/decremented value on the edge the extra name we want to
5088 assert for is not on the def chain of the name compared. Instead
5089 it is in the set of use stmts.
5090 Similar cases happen for conversions that were simplified through
5091 fold_{sign_changed,widened}_comparison. */
5092 if ((comp_code
== NE_EXPR
5093 || comp_code
== EQ_EXPR
)
5094 && TREE_CODE (val
) == INTEGER_CST
)
5096 imm_use_iterator ui
;
5098 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5100 if (!is_gimple_assign (use_stmt
))
5103 /* Cut off to use-stmts that are dominating the predecessor. */
5104 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
5107 tree name2
= gimple_assign_lhs (use_stmt
);
5108 if (TREE_CODE (name2
) != SSA_NAME
)
5111 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5113 if (code
== PLUS_EXPR
5114 || code
== MINUS_EXPR
)
5116 cst
= gimple_assign_rhs2 (use_stmt
);
5117 if (TREE_CODE (cst
) != INTEGER_CST
)
5119 cst
= int_const_binop (code
, val
, cst
);
5121 else if (CONVERT_EXPR_CODE_P (code
))
5123 /* For truncating conversions we cannot record
5125 if (comp_code
== NE_EXPR
5126 && (TYPE_PRECISION (TREE_TYPE (name2
))
5127 < TYPE_PRECISION (TREE_TYPE (name
))))
5129 cst
= fold_convert (TREE_TYPE (name2
), val
);
5134 if (TREE_OVERFLOW_P (cst
))
5135 cst
= drop_tree_overflow (cst
);
5136 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
5140 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5141 && TREE_CODE (val
) == INTEGER_CST
)
5143 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5144 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5145 tree val2
= NULL_TREE
;
5146 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5147 wide_int mask
= wi::zero (prec
);
5148 unsigned int nprec
= prec
;
5149 enum tree_code rhs_code
= ERROR_MARK
;
5151 if (is_gimple_assign (def_stmt
))
5152 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5154 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5155 assert that A != CST1 -+ CST2. */
5156 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5157 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
5159 tree op0
= gimple_assign_rhs1 (def_stmt
);
5160 tree op1
= gimple_assign_rhs2 (def_stmt
);
5161 if (TREE_CODE (op0
) == SSA_NAME
5162 && TREE_CODE (op1
) == INTEGER_CST
)
5164 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
5165 ? MINUS_EXPR
: PLUS_EXPR
);
5166 op1
= int_const_binop (reverse_op
, val
, op1
);
5167 if (TREE_OVERFLOW (op1
))
5168 op1
= drop_tree_overflow (op1
);
5169 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
5173 /* Add asserts for NAME cmp CST and NAME being defined
5174 as NAME = (int) NAME2. */
5175 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5176 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5177 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5178 && gimple_assign_cast_p (def_stmt
))
5180 name2
= gimple_assign_rhs1 (def_stmt
);
5181 if (CONVERT_EXPR_CODE_P (rhs_code
)
5182 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5183 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5184 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5185 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5186 || !tree_int_cst_equal (val
,
5187 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
5190 enum tree_code new_comp_code
= comp_code
;
5192 cst
= fold_convert (TREE_TYPE (name2
),
5193 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5194 /* Build an expression for the range test. */
5195 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5196 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5197 fold_convert (TREE_TYPE (name2
), val
));
5198 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5200 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5201 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5202 build_int_cst (TREE_TYPE (name2
), 1));
5207 fprintf (dump_file
, "Adding assert for ");
5208 print_generic_expr (dump_file
, name2
, 0);
5209 fprintf (dump_file
, " from ");
5210 print_generic_expr (dump_file
, tmp
, 0);
5211 fprintf (dump_file
, "\n");
5214 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
5218 /* Add asserts for NAME cmp CST and NAME being defined as
5219 NAME = NAME2 >> CST2.
5221 Extract CST2 from the right shift. */
5222 if (rhs_code
== RSHIFT_EXPR
)
5224 name2
= gimple_assign_rhs1 (def_stmt
);
5225 cst2
= gimple_assign_rhs2 (def_stmt
);
5226 if (TREE_CODE (name2
) == SSA_NAME
5227 && tree_fits_uhwi_p (cst2
)
5228 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5229 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5230 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
))))
5232 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5233 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5236 if (val2
!= NULL_TREE
5237 && TREE_CODE (val2
) == INTEGER_CST
5238 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5242 enum tree_code new_comp_code
= comp_code
;
5246 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5248 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5250 tree type
= build_nonstandard_integer_type (prec
, 1);
5251 tmp
= build1 (NOP_EXPR
, type
, name2
);
5252 val2
= fold_convert (type
, val2
);
5254 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5255 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5256 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5258 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5261 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5263 if (minval
== new_val
)
5264 new_val
= NULL_TREE
;
5269 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5272 new_val
= NULL_TREE
;
5274 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5281 fprintf (dump_file
, "Adding assert for ");
5282 print_generic_expr (dump_file
, name2
, 0);
5283 fprintf (dump_file
, " from ");
5284 print_generic_expr (dump_file
, tmp
, 0);
5285 fprintf (dump_file
, "\n");
5288 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
5292 /* Add asserts for NAME cmp CST and NAME being defined as
5293 NAME = NAME2 & CST2.
5295 Extract CST2 from the and.
5298 NAME = (unsigned) NAME2;
5299 casts where NAME's type is unsigned and has smaller precision
5300 than NAME2's type as if it was NAME = NAME2 & MASK. */
5301 names
[0] = NULL_TREE
;
5302 names
[1] = NULL_TREE
;
5304 if (rhs_code
== BIT_AND_EXPR
5305 || (CONVERT_EXPR_CODE_P (rhs_code
)
5306 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
5307 && TYPE_UNSIGNED (TREE_TYPE (val
))
5308 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5311 name2
= gimple_assign_rhs1 (def_stmt
);
5312 if (rhs_code
== BIT_AND_EXPR
)
5313 cst2
= gimple_assign_rhs2 (def_stmt
);
5316 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5317 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5319 if (TREE_CODE (name2
) == SSA_NAME
5320 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5321 && TREE_CODE (cst2
) == INTEGER_CST
5322 && !integer_zerop (cst2
)
5324 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5326 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5327 if (gimple_assign_cast_p (def_stmt2
))
5329 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5330 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5331 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5332 || (TYPE_PRECISION (TREE_TYPE (name2
))
5333 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
5334 names
[1] = NULL_TREE
;
5339 if (names
[0] || names
[1])
5341 wide_int minv
, maxv
, valv
, cst2v
;
5342 wide_int tem
, sgnbit
;
5343 bool valid_p
= false, valn
, cst2n
;
5344 enum tree_code ccode
= comp_code
;
5346 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5347 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5348 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5349 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5350 /* If CST2 doesn't have most significant bit set,
5351 but VAL is negative, we have comparison like
5352 if ((x & 0x123) > -4) (always true). Just give up. */
5356 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5358 sgnbit
= wi::zero (nprec
);
5359 minv
= valv
& cst2v
;
5363 /* Minimum unsigned value for equality is VAL & CST2
5364 (should be equal to VAL, otherwise we probably should
5365 have folded the comparison into false) and
5366 maximum unsigned value is VAL | ~CST2. */
5367 maxv
= valv
| ~cst2v
;
5372 tem
= valv
| ~cst2v
;
5373 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5377 sgnbit
= wi::zero (nprec
);
5380 /* If (VAL | ~CST2) is all ones, handle it as
5381 (X & CST2) < VAL. */
5386 sgnbit
= wi::zero (nprec
);
5389 if (!cst2n
&& wi::neg_p (cst2v
))
5390 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5399 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5405 sgnbit
= wi::zero (nprec
);
5410 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5411 is VAL and maximum unsigned value is ~0. For signed
5412 comparison, if CST2 doesn't have most significant bit
5413 set, handle it similarly. If CST2 has MSB set,
5414 the minimum is the same, and maximum is ~0U/2. */
5417 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5419 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5423 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5429 /* Find out smallest MINV where MINV > VAL
5430 && (MINV & CST2) == MINV, if any. If VAL is signed and
5431 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5432 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5435 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5440 /* Minimum unsigned value for <= is 0 and maximum
5441 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5442 Otherwise, find smallest VAL2 where VAL2 > VAL
5443 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5445 For signed comparison, if CST2 doesn't have most
5446 significant bit set, handle it similarly. If CST2 has
5447 MSB set, the maximum is the same and minimum is INT_MIN. */
5452 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5464 /* Minimum unsigned value for < is 0 and maximum
5465 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5466 Otherwise, find smallest VAL2 where VAL2 > VAL
5467 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5469 For signed comparison, if CST2 doesn't have most
5470 significant bit set, handle it similarly. If CST2 has
5471 MSB set, the maximum is the same and minimum is INT_MIN. */
5480 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5494 && (maxv
- minv
) != -1)
5496 tree tmp
, new_val
, type
;
5499 for (i
= 0; i
< 2; i
++)
5502 wide_int maxv2
= maxv
;
5504 type
= TREE_TYPE (names
[i
]);
5505 if (!TYPE_UNSIGNED (type
))
5507 type
= build_nonstandard_integer_type (nprec
, 1);
5508 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5512 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5513 wide_int_to_tree (type
, -minv
));
5514 maxv2
= maxv
- minv
;
5516 new_val
= wide_int_to_tree (type
, maxv2
);
5520 fprintf (dump_file
, "Adding assert for ");
5521 print_generic_expr (dump_file
, names
[i
], 0);
5522 fprintf (dump_file
, " from ");
5523 print_generic_expr (dump_file
, tmp
, 0);
5524 fprintf (dump_file
, "\n");
5527 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
5534 /* OP is an operand of a truth value expression which is known to have
5535 a particular value. Register any asserts for OP and for any
5536 operands in OP's defining statement.
5538 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5539 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5542 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5543 edge e
, vec
<assert_info
> &asserts
)
5547 enum tree_code rhs_code
;
5549 /* We only care about SSA_NAMEs. */
5550 if (TREE_CODE (op
) != SSA_NAME
)
5553 /* We know that OP will have a zero or nonzero value. */
5554 val
= build_int_cst (TREE_TYPE (op
), 0);
5555 add_assert_info (asserts
, op
, op
, code
, val
);
5557 /* Now look at how OP is set. If it's set from a comparison,
5558 a truth operation or some bit operations, then we may be able
5559 to register information about the operands of that assignment. */
5560 op_def
= SSA_NAME_DEF_STMT (op
);
5561 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5564 rhs_code
= gimple_assign_rhs_code (op_def
);
5566 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5568 bool invert
= (code
== EQ_EXPR
? true : false);
5569 tree op0
= gimple_assign_rhs1 (op_def
);
5570 tree op1
= gimple_assign_rhs2 (op_def
);
5572 if (TREE_CODE (op0
) == SSA_NAME
)
5573 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
5574 if (TREE_CODE (op1
) == SSA_NAME
)
5575 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
5577 else if ((code
== NE_EXPR
5578 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5580 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5582 /* Recurse on each operand. */
5583 tree op0
= gimple_assign_rhs1 (op_def
);
5584 tree op1
= gimple_assign_rhs2 (op_def
);
5585 if (TREE_CODE (op0
) == SSA_NAME
5586 && has_single_use (op0
))
5587 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
5588 if (TREE_CODE (op1
) == SSA_NAME
5589 && has_single_use (op1
))
5590 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
5592 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5593 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5595 /* Recurse, flipping CODE. */
5596 code
= invert_tree_comparison (code
, false);
5597 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
5599 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5601 /* Recurse through the copy. */
5602 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
5604 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5606 /* Recurse through the type conversion, unless it is a narrowing
5607 conversion or conversion from non-integral type. */
5608 tree rhs
= gimple_assign_rhs1 (op_def
);
5609 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5610 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5611 <= TYPE_PRECISION (TREE_TYPE (op
))))
5612 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
5616 /* Try to register an edge assertion for SSA name NAME on edge E for
5617 the condition COND contributing to the conditional jump pointed to by
5621 register_edge_assert_for (tree name
, edge e
,
5622 enum tree_code cond_code
, tree cond_op0
,
5623 tree cond_op1
, vec
<assert_info
> &asserts
)
5626 enum tree_code comp_code
;
5627 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5629 /* Do not attempt to infer anything in names that flow through
5631 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5634 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5640 /* Register ASSERT_EXPRs for name. */
5641 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
5642 cond_op1
, is_else_edge
, asserts
);
5645 /* If COND is effectively an equality test of an SSA_NAME against
5646 the value zero or one, then we may be able to assert values
5647 for SSA_NAMEs which flow into COND. */
5649 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5650 statement of NAME we can assert both operands of the BIT_AND_EXPR
5651 have nonzero value. */
5652 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5653 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5655 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5657 if (is_gimple_assign (def_stmt
)
5658 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5660 tree op0
= gimple_assign_rhs1 (def_stmt
);
5661 tree op1
= gimple_assign_rhs2 (def_stmt
);
5662 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
5663 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
5667 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5668 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5670 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5671 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5673 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5675 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5676 necessarily zero value, or if type-precision is one. */
5677 if (is_gimple_assign (def_stmt
)
5678 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5679 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5680 || comp_code
== EQ_EXPR
)))
5682 tree op0
= gimple_assign_rhs1 (def_stmt
);
5683 tree op1
= gimple_assign_rhs2 (def_stmt
);
5684 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
5685 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
5690 /* Finish found ASSERTS for E and register them at GSI. */
5693 finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
5694 vec
<assert_info
> &asserts
)
5696 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
5697 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5698 reachable from E. */
5699 if (live_on_edge (e
, asserts
[i
].name
))
5700 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
5701 asserts
[i
].comp_code
, asserts
[i
].val
,
5707 /* Determine whether the outgoing edges of BB should receive an
5708 ASSERT_EXPR for each of the operands of BB's LAST statement.
5709 The last statement of BB must be a COND_EXPR.
5711 If any of the sub-graphs rooted at BB have an interesting use of
5712 the predicate operands, an assert location node is added to the
5713 list of assertions for the corresponding operands. */
5716 find_conditional_asserts (basic_block bb
, gcond
*last
)
5718 gimple_stmt_iterator bsi
;
5724 bsi
= gsi_for_stmt (last
);
5726 /* Look for uses of the operands in each of the sub-graphs
5727 rooted at BB. We need to check each of the outgoing edges
5728 separately, so that we know what kind of ASSERT_EXPR to
5730 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5735 /* Register the necessary assertions for each operand in the
5736 conditional predicate. */
5737 auto_vec
<assert_info
, 8> asserts
;
5738 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5739 register_edge_assert_for (op
, e
,
5740 gimple_cond_code (last
),
5741 gimple_cond_lhs (last
),
5742 gimple_cond_rhs (last
), asserts
);
5743 finish_register_edge_assert_for (e
, bsi
, asserts
);
5753 /* Compare two case labels sorting first by the destination bb index
5754 and then by the case value. */
5757 compare_case_labels (const void *p1
, const void *p2
)
5759 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5760 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5761 int idx1
= ci1
->bb
->index
;
5762 int idx2
= ci2
->bb
->index
;
5766 else if (idx1
== idx2
)
5768 /* Make sure the default label is first in a group. */
5769 if (!CASE_LOW (ci1
->expr
))
5771 else if (!CASE_LOW (ci2
->expr
))
5774 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5775 CASE_LOW (ci2
->expr
));
5781 /* Determine whether the outgoing edges of BB should receive an
5782 ASSERT_EXPR for each of the operands of BB's LAST statement.
5783 The last statement of BB must be a SWITCH_EXPR.
5785 If any of the sub-graphs rooted at BB have an interesting use of
5786 the predicate operands, an assert location node is added to the
5787 list of assertions for the corresponding operands. */
5790 find_switch_asserts (basic_block bb
, gswitch
*last
)
5792 gimple_stmt_iterator bsi
;
5795 struct case_info
*ci
;
5796 size_t n
= gimple_switch_num_labels (last
);
5797 #if GCC_VERSION >= 4000
5800 /* Work around GCC 3.4 bug (PR 37086). */
5801 volatile unsigned int idx
;
5804 bsi
= gsi_for_stmt (last
);
5805 op
= gimple_switch_index (last
);
5806 if (TREE_CODE (op
) != SSA_NAME
)
5809 /* Build a vector of case labels sorted by destination label. */
5810 ci
= XNEWVEC (struct case_info
, n
);
5811 for (idx
= 0; idx
< n
; ++idx
)
5813 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5814 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5816 edge default_edge
= find_edge (bb
, ci
[0].bb
);
5817 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5819 for (idx
= 0; idx
< n
; ++idx
)
5822 tree cl
= ci
[idx
].expr
;
5823 basic_block cbb
= ci
[idx
].bb
;
5825 min
= CASE_LOW (cl
);
5826 max
= CASE_HIGH (cl
);
5828 /* If there are multiple case labels with the same destination
5829 we need to combine them to a single value range for the edge. */
5830 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5832 /* Skip labels until the last of the group. */
5835 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5838 /* Pick up the maximum of the case label range. */
5839 if (CASE_HIGH (ci
[idx
].expr
))
5840 max
= CASE_HIGH (ci
[idx
].expr
);
5842 max
= CASE_LOW (ci
[idx
].expr
);
5845 /* Can't extract a useful assertion out of a range that includes the
5847 if (min
== NULL_TREE
)
5850 /* Find the edge to register the assert expr on. */
5851 e
= find_edge (bb
, cbb
);
5853 /* Register the necessary assertions for the operand in the
5855 auto_vec
<assert_info
, 8> asserts
;
5856 register_edge_assert_for (op
, e
,
5857 max
? GE_EXPR
: EQ_EXPR
,
5858 op
, fold_convert (TREE_TYPE (op
), min
),
5861 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
5862 fold_convert (TREE_TYPE (op
), max
),
5864 finish_register_edge_assert_for (e
, bsi
, asserts
);
5869 if (!live_on_edge (default_edge
, op
))
5872 /* Now register along the default label assertions that correspond to the
5873 anti-range of each label. */
5874 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
5875 if (insertion_limit
== 0)
5878 /* We can't do this if the default case shares a label with another case. */
5879 tree default_cl
= gimple_switch_default_label (last
);
5880 for (idx
= 1; idx
< n
; idx
++)
5883 tree cl
= gimple_switch_label (last
, idx
);
5884 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
5887 min
= CASE_LOW (cl
);
5888 max
= CASE_HIGH (cl
);
5890 /* Combine contiguous case ranges to reduce the number of assertions
5892 for (idx
= idx
+ 1; idx
< n
; idx
++)
5894 tree next_min
, next_max
;
5895 tree next_cl
= gimple_switch_label (last
, idx
);
5896 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
5899 next_min
= CASE_LOW (next_cl
);
5900 next_max
= CASE_HIGH (next_cl
);
5902 wide_int difference
= wi::sub (next_min
, max
? max
: min
);
5903 if (wi::eq_p (difference
, 1))
5904 max
= next_max
? next_max
: next_min
;
5910 if (max
== NULL_TREE
)
5912 /* Register the assertion OP != MIN. */
5913 auto_vec
<assert_info
, 8> asserts
;
5914 min
= fold_convert (TREE_TYPE (op
), min
);
5915 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
5917 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
5921 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
5922 which will give OP the anti-range ~[MIN,MAX]. */
5923 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
5924 min
= fold_convert (TREE_TYPE (uop
), min
);
5925 max
= fold_convert (TREE_TYPE (uop
), max
);
5927 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
5928 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
5929 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
5930 NULL
, default_edge
, bsi
);
5933 if (--insertion_limit
== 0)
5939 /* Traverse all the statements in block BB looking for statements that
5940 may generate useful assertions for the SSA names in their operand.
5941 If a statement produces a useful assertion A for name N_i, then the
5942 list of assertions already generated for N_i is scanned to
5943 determine if A is actually needed.
5945 If N_i already had the assertion A at a location dominating the
5946 current location, then nothing needs to be done. Otherwise, the
5947 new location for A is recorded instead.
5949 1- For every statement S in BB, all the variables used by S are
5950 added to bitmap FOUND_IN_SUBGRAPH.
5952 2- If statement S uses an operand N in a way that exposes a known
5953 value range for N, then if N was not already generated by an
5954 ASSERT_EXPR, create a new assert location for N. For instance,
5955 if N is a pointer and the statement dereferences it, we can
5956 assume that N is not NULL.
5958 3- COND_EXPRs are a special case of #2. We can derive range
5959 information from the predicate but need to insert different
5960 ASSERT_EXPRs for each of the sub-graphs rooted at the
5961 conditional block. If the last statement of BB is a conditional
5962 expression of the form 'X op Y', then
5964 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5966 b) If the conditional is the only entry point to the sub-graph
5967 corresponding to the THEN_CLAUSE, recurse into it. On
5968 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5969 an ASSERT_EXPR is added for the corresponding variable.
5971 c) Repeat step (b) on the ELSE_CLAUSE.
5973 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5982 In this case, an assertion on the THEN clause is useful to
5983 determine that 'a' is always 9 on that edge. However, an assertion
5984 on the ELSE clause would be unnecessary.
5986 4- If BB does not end in a conditional expression, then we recurse
5987 into BB's dominator children.
5989 At the end of the recursive traversal, every SSA name will have a
5990 list of locations where ASSERT_EXPRs should be added. When a new
5991 location for name N is found, it is registered by calling
5992 register_new_assert_for. That function keeps track of all the
5993 registered assertions to prevent adding unnecessary assertions.
5994 For instance, if a pointer P_4 is dereferenced more than once in a
5995 dominator tree, only the location dominating all the dereference of
5996 P_4 will receive an ASSERT_EXPR. */
5999 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6003 last
= last_stmt (bb
);
6005 /* If BB's last statement is a conditional statement involving integer
6006 operands, determine if we need to add ASSERT_EXPRs. */
6008 && gimple_code (last
) == GIMPLE_COND
6009 && !fp_predicate (last
)
6010 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6011 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6013 /* If BB's last statement is a switch statement involving integer
6014 operands, determine if we need to add ASSERT_EXPRs. */
6016 && gimple_code (last
) == GIMPLE_SWITCH
6017 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6018 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6020 /* Traverse all the statements in BB marking used names and looking
6021 for statements that may infer assertions for their used operands. */
6022 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6029 stmt
= gsi_stmt (si
);
6031 if (is_gimple_debug (stmt
))
6034 /* See if we can derive an assertion for any of STMT's operands. */
6035 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6038 enum tree_code comp_code
;
6040 /* If op is not live beyond this stmt, do not bother to insert
6042 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6045 /* If OP is used in such a way that we can infer a value
6046 range for it, and we don't find a previous assertion for
6047 it, create a new assertion location node for OP. */
6048 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6050 /* If we are able to infer a nonzero value range for OP,
6051 then walk backwards through the use-def chain to see if OP
6052 was set via a typecast.
6054 If so, then we can also infer a nonzero value range
6055 for the operand of the NOP_EXPR. */
6056 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6059 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
6061 while (is_gimple_assign (def_stmt
)
6062 && CONVERT_EXPR_CODE_P
6063 (gimple_assign_rhs_code (def_stmt
))
6065 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6067 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6069 t
= gimple_assign_rhs1 (def_stmt
);
6070 def_stmt
= SSA_NAME_DEF_STMT (t
);
6072 /* Note we want to register the assert for the
6073 operand of the NOP_EXPR after SI, not after the
6075 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
6076 register_new_assert_for (t
, t
, comp_code
, value
,
6081 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6086 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6087 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6088 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6089 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6092 /* Traverse all PHI nodes in BB, updating live. */
6093 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6096 use_operand_p arg_p
;
6098 gphi
*phi
= si
.phi ();
6099 tree res
= gimple_phi_result (phi
);
6101 if (virtual_operand_p (res
))
6104 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6106 tree arg
= USE_FROM_PTR (arg_p
);
6107 if (TREE_CODE (arg
) == SSA_NAME
)
6108 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6111 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6115 /* Do an RPO walk over the function computing SSA name liveness
6116 on-the-fly and deciding on assert expressions to insert. */
6119 find_assert_locations (void)
6121 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6122 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6123 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6126 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6127 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6128 for (i
= 0; i
< rpo_cnt
; ++i
)
6131 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6132 the order we compute liveness and insert asserts we otherwise
6133 fail to insert asserts into the loop latch. */
6135 FOR_EACH_LOOP (loop
, 0)
6137 i
= loop
->latch
->index
;
6138 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6139 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6140 !gsi_end_p (gsi
); gsi_next (&gsi
))
6142 gphi
*phi
= gsi
.phi ();
6143 if (virtual_operand_p (gimple_phi_result (phi
)))
6145 tree arg
= gimple_phi_arg_def (phi
, j
);
6146 if (TREE_CODE (arg
) == SSA_NAME
)
6148 if (live
[i
] == NULL
)
6150 live
[i
] = sbitmap_alloc (num_ssa_names
);
6151 bitmap_clear (live
[i
]);
6153 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6158 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6160 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6166 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6167 bitmap_clear (live
[rpo
[i
]]);
6170 /* Process BB and update the live information with uses in
6172 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6174 /* Merge liveness into the predecessor blocks and free it. */
6175 if (!bitmap_empty_p (live
[rpo
[i
]]))
6178 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6180 int pred
= e
->src
->index
;
6181 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6186 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6187 bitmap_clear (live
[pred
]);
6189 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6191 if (bb_rpo
[pred
] < pred_rpo
)
6192 pred_rpo
= bb_rpo
[pred
];
6195 /* Record the RPO number of the last visited block that needs
6196 live information from this block. */
6197 last_rpo
[rpo
[i
]] = pred_rpo
;
6201 sbitmap_free (live
[rpo
[i
]]);
6202 live
[rpo
[i
]] = NULL
;
6205 /* We can free all successors live bitmaps if all their
6206 predecessors have been visited already. */
6207 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6208 if (last_rpo
[e
->dest
->index
] == i
6209 && live
[e
->dest
->index
])
6211 sbitmap_free (live
[e
->dest
->index
]);
6212 live
[e
->dest
->index
] = NULL
;
6217 XDELETEVEC (bb_rpo
);
6218 XDELETEVEC (last_rpo
);
6219 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6221 sbitmap_free (live
[i
]);
6225 /* Create an ASSERT_EXPR for NAME and insert it in the location
6226 indicated by LOC. Return true if we made any edge insertions. */
6229 process_assert_insertions_for (tree name
, assert_locus
*loc
)
6231 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6234 gimple
*assert_stmt
;
6238 /* If we have X <=> X do not insert an assert expr for that. */
6239 if (loc
->expr
== loc
->val
)
6242 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6243 assert_stmt
= build_assert_expr_for (cond
, name
);
6246 /* We have been asked to insert the assertion on an edge. This
6247 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6248 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6249 || (gimple_code (gsi_stmt (loc
->si
))
6252 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6256 /* If the stmt iterator points at the end then this is an insertion
6257 at the beginning of a block. */
6258 if (gsi_end_p (loc
->si
))
6260 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
6261 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
6265 /* Otherwise, we can insert right after LOC->SI iff the
6266 statement must not be the last statement in the block. */
6267 stmt
= gsi_stmt (loc
->si
);
6268 if (!stmt_ends_bb_p (stmt
))
6270 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6274 /* If STMT must be the last statement in BB, we can only insert new
6275 assertions on the non-abnormal edge out of BB. Note that since
6276 STMT is not control flow, there may only be one non-abnormal/eh edge
6278 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6279 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
6281 gsi_insert_on_edge (e
, assert_stmt
);
6288 /* Qsort helper for sorting assert locations. */
6291 compare_assert_loc (const void *pa
, const void *pb
)
6293 assert_locus
* const a
= *(assert_locus
* const *)pa
;
6294 assert_locus
* const b
= *(assert_locus
* const *)pb
;
6297 else if (a
->e
&& ! b
->e
)
6300 /* Sort after destination index. */
6301 if (! a
->e
&& ! b
->e
)
6303 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
6305 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
6308 /* Sort after comp_code. */
6309 if (a
->comp_code
> b
->comp_code
)
6311 else if (a
->comp_code
< b
->comp_code
)
6314 /* Break the tie using hashing and source/bb index. */
6315 hashval_t ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
6316 hashval_t hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
6318 return (a
->e
&& b
->e
6319 ? a
->e
->src
->index
- b
->e
->src
->index
6320 : a
->bb
->index
- b
->bb
->index
);
6324 /* Process all the insertions registered for every name N_i registered
6325 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6326 found in ASSERTS_FOR[i]. */
6329 process_assert_insertions (void)
6333 bool update_edges_p
= false;
6334 int num_asserts
= 0;
6336 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6337 dump_all_asserts (dump_file
);
6339 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6341 assert_locus
*loc
= asserts_for
[i
];
6344 auto_vec
<assert_locus
*, 16> asserts
;
6345 for (; loc
; loc
= loc
->next
)
6346 asserts
.safe_push (loc
);
6347 asserts
.qsort (compare_assert_loc
);
6349 /* Push down common asserts to successors and remove redundant ones. */
6351 assert_locus
*common
= NULL
;
6352 unsigned commonj
= 0;
6353 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
6359 || loc
->e
->dest
!= common
->e
->dest
6360 || loc
->comp_code
!= common
->comp_code
6361 || ! operand_equal_p (loc
->val
, common
->val
, 0)
6362 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
6368 else if (loc
->e
== asserts
[j
-1]->e
)
6370 /* Remove duplicate asserts. */
6371 if (commonj
== j
- 1)
6376 free (asserts
[j
-1]);
6377 asserts
[j
-1] = NULL
;
6382 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
6384 /* We have the same assertion on all incoming edges of a BB.
6385 Insert it at the beginning of that block. */
6386 loc
->bb
= loc
->e
->dest
;
6388 loc
->si
= gsi_none ();
6390 /* Clear asserts commoned. */
6391 for (; commonj
!= j
; ++commonj
)
6392 if (asserts
[commonj
])
6394 free (asserts
[commonj
]);
6395 asserts
[commonj
] = NULL
;
6401 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
6406 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6413 gsi_commit_edge_inserts ();
6415 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6420 /* Traverse the flowgraph looking for conditional jumps to insert range
6421 expressions. These range expressions are meant to provide information
6422 to optimizations that need to reason in terms of value ranges. They
6423 will not be expanded into RTL. For instance, given:
6432 this pass will transform the code into:
6438 x = ASSERT_EXPR <x, x < y>
6443 y = ASSERT_EXPR <y, x >= y>
6447 The idea is that once copy and constant propagation have run, other
6448 optimizations will be able to determine what ranges of values can 'x'
6449 take in different paths of the code, simply by checking the reaching
6450 definition of 'x'. */
6453 insert_range_assertions (void)
6455 need_assert_for
= BITMAP_ALLOC (NULL
);
6456 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
6458 calculate_dominance_info (CDI_DOMINATORS
);
6460 find_assert_locations ();
6461 if (!bitmap_empty_p (need_assert_for
))
6463 process_assert_insertions ();
6464 update_ssa (TODO_update_ssa_no_phi
);
6467 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6469 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6470 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6474 BITMAP_FREE (need_assert_for
);
6477 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6478 and "struct" hacks. If VRP can determine that the
6479 array subscript is a constant, check if it is outside valid
6480 range. If the array subscript is a RANGE, warn if it is
6481 non-overlapping with valid range.
6482 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6485 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6487 value_range
*vr
= NULL
;
6488 tree low_sub
, up_sub
;
6489 tree low_bound
, up_bound
, up_bound_p1
;
6491 if (TREE_NO_WARNING (ref
))
6494 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6495 up_bound
= array_ref_up_bound (ref
);
6497 /* Can not check flexible arrays. */
6499 || TREE_CODE (up_bound
) != INTEGER_CST
)
6502 /* Accesses to trailing arrays via pointers may access storage
6503 beyond the types array bounds. */
6504 if (warn_array_bounds
< 2
6505 && array_at_struct_end_p (ref
))
6508 low_bound
= array_ref_low_bound (ref
);
6509 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6510 build_int_cst (TREE_TYPE (up_bound
), 1));
6513 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6515 warning_at (location
, OPT_Warray_bounds
,
6516 "array subscript is above array bounds");
6517 TREE_NO_WARNING (ref
) = 1;
6520 if (TREE_CODE (low_sub
) == SSA_NAME
)
6522 vr
= get_value_range (low_sub
);
6523 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6525 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6526 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6530 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6532 if (TREE_CODE (up_sub
) == INTEGER_CST
6533 && (ignore_off_by_one
6534 ? tree_int_cst_lt (up_bound
, up_sub
)
6535 : tree_int_cst_le (up_bound
, up_sub
))
6536 && TREE_CODE (low_sub
) == INTEGER_CST
6537 && tree_int_cst_le (low_sub
, low_bound
))
6539 warning_at (location
, OPT_Warray_bounds
,
6540 "array subscript is outside array bounds");
6541 TREE_NO_WARNING (ref
) = 1;
6544 else if (TREE_CODE (up_sub
) == INTEGER_CST
6545 && (ignore_off_by_one
6546 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6547 : !tree_int_cst_le (up_sub
, up_bound
)))
6549 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6551 fprintf (dump_file
, "Array bound warning for ");
6552 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6553 fprintf (dump_file
, "\n");
6555 warning_at (location
, OPT_Warray_bounds
,
6556 "array subscript is above array bounds");
6557 TREE_NO_WARNING (ref
) = 1;
6559 else if (TREE_CODE (low_sub
) == INTEGER_CST
6560 && tree_int_cst_lt (low_sub
, low_bound
))
6562 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6564 fprintf (dump_file
, "Array bound warning for ");
6565 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6566 fprintf (dump_file
, "\n");
6568 warning_at (location
, OPT_Warray_bounds
,
6569 "array subscript is below array bounds");
6570 TREE_NO_WARNING (ref
) = 1;
6574 /* Searches if the expr T, located at LOCATION computes
6575 address of an ARRAY_REF, and call check_array_ref on it. */
6578 search_for_addr_array (tree t
, location_t location
)
6580 /* Check each ARRAY_REFs in the reference chain. */
6583 if (TREE_CODE (t
) == ARRAY_REF
)
6584 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6586 t
= TREE_OPERAND (t
, 0);
6588 while (handled_component_p (t
));
6590 if (TREE_CODE (t
) == MEM_REF
6591 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6592 && !TREE_NO_WARNING (t
))
6594 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6595 tree low_bound
, up_bound
, el_sz
;
6597 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6598 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6599 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6602 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6603 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6604 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6606 || TREE_CODE (low_bound
) != INTEGER_CST
6608 || TREE_CODE (up_bound
) != INTEGER_CST
6610 || TREE_CODE (el_sz
) != INTEGER_CST
)
6613 idx
= mem_ref_offset (t
);
6614 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6617 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6619 fprintf (dump_file
, "Array bound warning for ");
6620 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6621 fprintf (dump_file
, "\n");
6623 warning_at (location
, OPT_Warray_bounds
,
6624 "array subscript is below array bounds");
6625 TREE_NO_WARNING (t
) = 1;
6627 else if (idx
> (wi::to_offset (up_bound
)
6628 - wi::to_offset (low_bound
) + 1))
6630 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6632 fprintf (dump_file
, "Array bound warning for ");
6633 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6634 fprintf (dump_file
, "\n");
6636 warning_at (location
, OPT_Warray_bounds
,
6637 "array subscript is above array bounds");
6638 TREE_NO_WARNING (t
) = 1;
6643 /* walk_tree() callback that checks if *TP is
6644 an ARRAY_REF inside an ADDR_EXPR (in which an array
6645 subscript one outside the valid range is allowed). Call
6646 check_array_ref for each ARRAY_REF found. The location is
6650 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6653 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6654 location_t location
;
6656 if (EXPR_HAS_LOCATION (t
))
6657 location
= EXPR_LOCATION (t
);
6660 location_t
*locp
= (location_t
*) wi
->info
;
6664 *walk_subtree
= TRUE
;
6666 if (TREE_CODE (t
) == ARRAY_REF
)
6667 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6669 else if (TREE_CODE (t
) == ADDR_EXPR
)
6671 search_for_addr_array (t
, location
);
6672 *walk_subtree
= FALSE
;
6678 /* Walk over all statements of all reachable BBs and call check_array_bounds
6682 check_all_array_refs (void)
6685 gimple_stmt_iterator si
;
6687 FOR_EACH_BB_FN (bb
, cfun
)
6691 bool executable
= false;
6693 /* Skip blocks that were found to be unreachable. */
6694 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6695 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6699 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6701 gimple
*stmt
= gsi_stmt (si
);
6702 struct walk_stmt_info wi
;
6703 if (!gimple_has_location (stmt
)
6704 || is_gimple_debug (stmt
))
6707 memset (&wi
, 0, sizeof (wi
));
6709 location_t loc
= gimple_location (stmt
);
6712 walk_gimple_op (gsi_stmt (si
),
6719 /* Return true if all imm uses of VAR are either in STMT, or
6720 feed (optionally through a chain of single imm uses) GIMPLE_COND
6721 in basic block COND_BB. */
6724 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
6726 use_operand_p use_p
, use2_p
;
6727 imm_use_iterator iter
;
6729 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6730 if (USE_STMT (use_p
) != stmt
)
6732 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
6733 if (is_gimple_debug (use_stmt
))
6735 while (is_gimple_assign (use_stmt
)
6736 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6737 && single_imm_use (gimple_assign_lhs (use_stmt
),
6738 &use2_p
, &use_stmt2
))
6739 use_stmt
= use_stmt2
;
6740 if (gimple_code (use_stmt
) != GIMPLE_COND
6741 || gimple_bb (use_stmt
) != cond_bb
)
6754 __builtin_unreachable ();
6756 x_5 = ASSERT_EXPR <x_3, ...>;
6757 If x_3 has no other immediate uses (checked by caller),
6758 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6759 from the non-zero bitmask. */
6762 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6764 edge e
= single_pred_edge (bb
);
6765 basic_block cond_bb
= e
->src
;
6766 gimple
*stmt
= last_stmt (cond_bb
);
6770 || gimple_code (stmt
) != GIMPLE_COND
6771 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6772 ? EQ_EXPR
: NE_EXPR
)
6773 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6774 || !integer_zerop (gimple_cond_rhs (stmt
)))
6777 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6778 if (!is_gimple_assign (stmt
)
6779 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6780 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6782 if (gimple_assign_rhs1 (stmt
) != var
)
6786 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6788 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6789 if (!gimple_assign_cast_p (stmt2
)
6790 || gimple_assign_rhs1 (stmt2
) != var
6791 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6792 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6793 != TYPE_PRECISION (TREE_TYPE (var
))))
6796 cst
= gimple_assign_rhs2 (stmt
);
6797 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6800 /* Convert range assertion expressions into the implied copies and
6801 copy propagate away the copies. Doing the trivial copy propagation
6802 here avoids the need to run the full copy propagation pass after
6805 FIXME, this will eventually lead to copy propagation removing the
6806 names that had useful range information attached to them. For
6807 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6808 then N_i will have the range [3, +INF].
6810 However, by converting the assertion into the implied copy
6811 operation N_i = N_j, we will then copy-propagate N_j into the uses
6812 of N_i and lose the range information. We may want to hold on to
6813 ASSERT_EXPRs a little while longer as the ranges could be used in
6814 things like jump threading.
6816 The problem with keeping ASSERT_EXPRs around is that passes after
6817 VRP need to handle them appropriately.
6819 Another approach would be to make the range information a first
6820 class property of the SSA_NAME so that it can be queried from
6821 any pass. This is made somewhat more complex by the need for
6822 multiple ranges to be associated with one SSA_NAME. */
6825 remove_range_assertions (void)
6828 gimple_stmt_iterator si
;
6829 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6830 a basic block preceeded by GIMPLE_COND branching to it and
6831 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6834 /* Note that the BSI iterator bump happens at the bottom of the
6835 loop and no bump is necessary if we're removing the statement
6836 referenced by the current BSI. */
6837 FOR_EACH_BB_FN (bb
, cfun
)
6838 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6840 gimple
*stmt
= gsi_stmt (si
);
6842 if (is_gimple_assign (stmt
)
6843 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6845 tree lhs
= gimple_assign_lhs (stmt
);
6846 tree rhs
= gimple_assign_rhs1 (stmt
);
6849 var
= ASSERT_EXPR_VAR (rhs
);
6851 if (TREE_CODE (var
) == SSA_NAME
6852 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
6853 && SSA_NAME_RANGE_INFO (lhs
))
6855 if (is_unreachable
== -1)
6858 if (single_pred_p (bb
)
6859 && assert_unreachable_fallthru_edge_p
6860 (single_pred_edge (bb
)))
6864 if (x_7 >= 10 && x_7 < 20)
6865 __builtin_unreachable ();
6866 x_8 = ASSERT_EXPR <x_7, ...>;
6867 if the only uses of x_7 are in the ASSERT_EXPR and
6868 in the condition. In that case, we can copy the
6869 range info from x_8 computed in this pass also
6872 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6875 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6876 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6877 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6878 maybe_set_nonzero_bits (bb
, var
);
6882 /* Propagate the RHS into every use of the LHS. For SSA names
6883 also propagate abnormals as it merely restores the original
6884 IL in this case (an replace_uses_by would assert). */
6885 if (TREE_CODE (var
) == SSA_NAME
)
6887 imm_use_iterator iter
;
6888 use_operand_p use_p
;
6890 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6891 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6892 SET_USE (use_p
, var
);
6895 replace_uses_by (lhs
, var
);
6897 /* And finally, remove the copy, it is not needed. */
6898 gsi_remove (&si
, true);
6899 release_defs (stmt
);
6903 if (!is_gimple_debug (gsi_stmt (si
)))
6911 /* Return true if STMT is interesting for VRP. */
6914 stmt_interesting_for_vrp (gimple
*stmt
)
6916 if (gimple_code (stmt
) == GIMPLE_PHI
)
6918 tree res
= gimple_phi_result (stmt
);
6919 return (!virtual_operand_p (res
)
6920 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6921 || POINTER_TYPE_P (TREE_TYPE (res
))));
6923 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6925 tree lhs
= gimple_get_lhs (stmt
);
6927 /* In general, assignments with virtual operands are not useful
6928 for deriving ranges, with the obvious exception of calls to
6929 builtin functions. */
6930 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6931 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6932 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6933 && (is_gimple_call (stmt
)
6934 || !gimple_vuse (stmt
)))
6936 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
6937 switch (gimple_call_internal_fn (stmt
))
6939 case IFN_ADD_OVERFLOW
:
6940 case IFN_SUB_OVERFLOW
:
6941 case IFN_MUL_OVERFLOW
:
6942 case IFN_ATOMIC_COMPARE_EXCHANGE
:
6943 /* These internal calls return _Complex integer type,
6944 but are interesting to VRP nevertheless. */
6945 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6952 else if (gimple_code (stmt
) == GIMPLE_COND
6953 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6959 /* Initialize VRP lattice. */
6962 vrp_initialize_lattice ()
6964 values_propagated
= false;
6965 num_vr_values
= num_ssa_names
;
6966 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
6967 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6968 bitmap_obstack_initialize (&vrp_equiv_obstack
);
6971 /* Initialization required by ssa_propagate engine. */
6978 FOR_EACH_BB_FN (bb
, cfun
)
6980 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6983 gphi
*phi
= si
.phi ();
6984 if (!stmt_interesting_for_vrp (phi
))
6986 tree lhs
= PHI_RESULT (phi
);
6987 set_value_range_to_varying (get_value_range (lhs
));
6988 prop_set_simulate_again (phi
, false);
6991 prop_set_simulate_again (phi
, true);
6994 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
6997 gimple
*stmt
= gsi_stmt (si
);
6999 /* If the statement is a control insn, then we do not
7000 want to avoid simulating the statement once. Failure
7001 to do so means that those edges will never get added. */
7002 if (stmt_ends_bb_p (stmt
))
7003 prop_set_simulate_again (stmt
, true);
7004 else if (!stmt_interesting_for_vrp (stmt
))
7006 set_defs_to_varying (stmt
);
7007 prop_set_simulate_again (stmt
, false);
7010 prop_set_simulate_again (stmt
, true);
7015 /* Return the singleton value-range for NAME or NAME. */
7018 vrp_valueize (tree name
)
7020 if (TREE_CODE (name
) == SSA_NAME
)
7022 value_range
*vr
= get_value_range (name
);
7023 if (vr
->type
== VR_RANGE
7024 && (TREE_CODE (vr
->min
) == SSA_NAME
7025 || is_gimple_min_invariant (vr
->min
))
7026 && vrp_operand_equal_p (vr
->min
, vr
->max
))
7032 /* Return the singleton value-range for NAME if that is a constant
7033 but signal to not follow SSA edges. */
7036 vrp_valueize_1 (tree name
)
7038 if (TREE_CODE (name
) == SSA_NAME
)
7040 /* If the definition may be simulated again we cannot follow
7041 this SSA edge as the SSA propagator does not necessarily
7042 re-visit the use. */
7043 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
7044 if (!gimple_nop_p (def_stmt
)
7045 && prop_simulate_again_p (def_stmt
))
7047 value_range
*vr
= get_value_range (name
);
7048 if (range_int_cst_singleton_p (vr
))
7054 /* Visit assignment STMT. If it produces an interesting range, record
7055 the range in VR and set LHS to OUTPUT_P. */
7058 vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
, value_range
*vr
)
7061 enum gimple_code code
= gimple_code (stmt
);
7062 lhs
= gimple_get_lhs (stmt
);
7063 *output_p
= NULL_TREE
;
7065 /* We only keep track of ranges in integral and pointer types. */
7066 if (TREE_CODE (lhs
) == SSA_NAME
7067 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7068 /* It is valid to have NULL MIN/MAX values on a type. See
7069 build_range_type. */
7070 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7071 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7072 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7076 /* Try folding the statement to a constant first. */
7077 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7081 if (TREE_CODE (tem
) == SSA_NAME
7082 && (SSA_NAME_IS_DEFAULT_DEF (tem
)
7083 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem
))))
7085 extract_range_from_ssa_name (vr
, tem
);
7088 else if (is_gimple_min_invariant (tem
))
7090 set_value_range_to_value (vr
, tem
, NULL
);
7094 /* Then dispatch to value-range extracting functions. */
7095 if (code
== GIMPLE_CALL
)
7096 extract_range_basic (vr
, stmt
);
7098 extract_range_from_assignment (vr
, as_a
<gassign
*> (stmt
));
7102 /* Helper that gets the value range of the SSA_NAME with version I
7103 or a symbolic range containing the SSA_NAME only if the value range
7104 is varying or undefined. */
7106 static inline value_range
7107 get_vr_for_comparison (int i
)
7109 value_range vr
= *get_value_range (ssa_name (i
));
7111 /* If name N_i does not have a valid range, use N_i as its own
7112 range. This allows us to compare against names that may
7113 have N_i in their ranges. */
7114 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7117 vr
.min
= ssa_name (i
);
7118 vr
.max
= ssa_name (i
);
7124 /* Compare all the value ranges for names equivalent to VAR with VAL
7125 using comparison code COMP. Return the same value returned by
7126 compare_range_with_value, including the setting of
7127 *STRICT_OVERFLOW_P. */
7130 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7131 bool *strict_overflow_p
, bool use_equiv_p
)
7137 int used_strict_overflow
;
7139 value_range equiv_vr
;
7141 /* Get the set of equivalences for VAR. */
7142 e
= get_value_range (var
)->equiv
;
7144 /* Start at -1. Set it to 0 if we do a comparison without relying
7145 on overflow, or 1 if all comparisons rely on overflow. */
7146 used_strict_overflow
= -1;
7148 /* Compare vars' value range with val. */
7149 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7151 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7153 used_strict_overflow
= sop
? 1 : 0;
7155 /* If the equiv set is empty we have done all work we need to do. */
7159 && used_strict_overflow
> 0)
7160 *strict_overflow_p
= true;
7164 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7166 tree name
= ssa_name (i
);
7171 && ! SSA_NAME_IS_DEFAULT_DEF (name
)
7172 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name
)))
7175 equiv_vr
= get_vr_for_comparison (i
);
7177 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7180 /* If we get different answers from different members
7181 of the equivalence set this check must be in a dead
7182 code region. Folding it to a trap representation
7183 would be correct here. For now just return don't-know. */
7193 used_strict_overflow
= 0;
7194 else if (used_strict_overflow
< 0)
7195 used_strict_overflow
= 1;
7200 && used_strict_overflow
> 0)
7201 *strict_overflow_p
= true;
7207 /* Given a comparison code COMP and names N1 and N2, compare all the
7208 ranges equivalent to N1 against all the ranges equivalent to N2
7209 to determine the value of N1 COMP N2. Return the same value
7210 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7211 whether we relied on an overflow infinity in the comparison. */
7215 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7216 bool *strict_overflow_p
)
7220 bitmap_iterator bi1
, bi2
;
7222 int used_strict_overflow
;
7223 static bitmap_obstack
*s_obstack
= NULL
;
7224 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7226 /* Compare the ranges of every name equivalent to N1 against the
7227 ranges of every name equivalent to N2. */
7228 e1
= get_value_range (n1
)->equiv
;
7229 e2
= get_value_range (n2
)->equiv
;
7231 /* Use the fake bitmaps if e1 or e2 are not available. */
7232 if (s_obstack
== NULL
)
7234 s_obstack
= XNEW (bitmap_obstack
);
7235 bitmap_obstack_initialize (s_obstack
);
7236 s_e1
= BITMAP_ALLOC (s_obstack
);
7237 s_e2
= BITMAP_ALLOC (s_obstack
);
7244 /* Add N1 and N2 to their own set of equivalences to avoid
7245 duplicating the body of the loop just to check N1 and N2
7247 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7248 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7250 /* If the equivalence sets have a common intersection, then the two
7251 names can be compared without checking their ranges. */
7252 if (bitmap_intersect_p (e1
, e2
))
7254 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7255 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7257 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7259 : boolean_false_node
;
7262 /* Start at -1. Set it to 0 if we do a comparison without relying
7263 on overflow, or 1 if all comparisons rely on overflow. */
7264 used_strict_overflow
= -1;
7266 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7267 N2 to their own set of equivalences to avoid duplicating the body
7268 of the loop just to check N1 and N2 ranges. */
7269 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7271 if (! ssa_name (i1
))
7274 value_range vr1
= get_vr_for_comparison (i1
);
7276 t
= retval
= NULL_TREE
;
7277 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7279 if (! ssa_name (i2
))
7284 value_range vr2
= get_vr_for_comparison (i2
);
7286 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7289 /* If we get different answers from different members
7290 of the equivalence set this check must be in a dead
7291 code region. Folding it to a trap representation
7292 would be correct here. For now just return don't-know. */
7296 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7297 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7303 used_strict_overflow
= 0;
7304 else if (used_strict_overflow
< 0)
7305 used_strict_overflow
= 1;
7311 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7312 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7313 if (used_strict_overflow
> 0)
7314 *strict_overflow_p
= true;
7319 /* None of the equivalent ranges are useful in computing this
7321 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7322 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7326 /* Helper function for vrp_evaluate_conditional_warnv & other
7330 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7332 bool * strict_overflow_p
)
7334 value_range
*vr0
, *vr1
;
7336 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7337 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7339 tree res
= NULL_TREE
;
7341 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7343 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7345 res
= (compare_range_with_value
7346 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7350 /* Helper function for vrp_evaluate_conditional_warnv. */
7353 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7354 tree op1
, bool use_equiv_p
,
7355 bool *strict_overflow_p
, bool *only_ranges
)
7359 *only_ranges
= true;
7361 /* We only deal with integral and pointer types. */
7362 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7363 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7366 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
7367 as a simple equality test, then prefer that over its current form
7370 An overflow test which collapses to an equality test can always be
7371 expressed as a comparison of one argument against zero. Overflow
7372 occurs when the chosen argument is zero and does not occur if the
7373 chosen argument is not zero. */
7375 if (overflow_comparison_p (code
, op0
, op1
, use_equiv_p
, &x
))
7377 wide_int max
= wi::max_value (TYPE_PRECISION (TREE_TYPE (op0
)), UNSIGNED
);
7378 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
7379 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
7380 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
7381 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
7382 if (integer_zerop (x
))
7385 code
= (code
== LT_EXPR
|| code
== LE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
7387 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
7388 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
7389 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
7390 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
7391 else if (wi::eq_p (x
, max
- 1))
7394 op1
= wide_int_to_tree (TREE_TYPE (op0
), 0);
7395 code
= (code
== GT_EXPR
|| code
== GE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
7399 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7400 (code
, op0
, op1
, strict_overflow_p
)))
7403 *only_ranges
= false;
7404 /* Do not use compare_names during propagation, it's quadratic. */
7405 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
7407 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7408 else if (TREE_CODE (op0
) == SSA_NAME
)
7409 return compare_name_with_value (code
, op0
, op1
,
7410 strict_overflow_p
, use_equiv_p
);
7411 else if (TREE_CODE (op1
) == SSA_NAME
)
7412 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
7413 strict_overflow_p
, use_equiv_p
);
7417 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7418 information. Return NULL if the conditional can not be evaluated.
7419 The ranges of all the names equivalent with the operands in COND
7420 will be used when trying to compute the value. If the result is
7421 based on undefined signed overflow, issue a warning if
7425 vrp_evaluate_conditional (tree_code code
, tree op0
, tree op1
, gimple
*stmt
)
7431 /* Some passes and foldings leak constants with overflow flag set
7432 into the IL. Avoid doing wrong things with these and bail out. */
7433 if ((TREE_CODE (op0
) == INTEGER_CST
7434 && TREE_OVERFLOW (op0
))
7435 || (TREE_CODE (op1
) == INTEGER_CST
7436 && TREE_OVERFLOW (op1
)))
7440 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7445 enum warn_strict_overflow_code wc
;
7446 const char* warnmsg
;
7448 if (is_gimple_min_invariant (ret
))
7450 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7451 warnmsg
= G_("assuming signed overflow does not occur when "
7452 "simplifying conditional to constant");
7456 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7457 warnmsg
= G_("assuming signed overflow does not occur when "
7458 "simplifying conditional");
7461 if (issue_strict_overflow_warning (wc
))
7463 location_t location
;
7465 if (!gimple_has_location (stmt
))
7466 location
= input_location
;
7468 location
= gimple_location (stmt
);
7469 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7473 if (warn_type_limits
7474 && ret
&& only_ranges
7475 && TREE_CODE_CLASS (code
) == tcc_comparison
7476 && TREE_CODE (op0
) == SSA_NAME
)
7478 /* If the comparison is being folded and the operand on the LHS
7479 is being compared against a constant value that is outside of
7480 the natural range of OP0's type, then the predicate will
7481 always fold regardless of the value of OP0. If -Wtype-limits
7482 was specified, emit a warning. */
7483 tree type
= TREE_TYPE (op0
);
7484 value_range
*vr0
= get_value_range (op0
);
7486 if (vr0
->type
== VR_RANGE
7487 && INTEGRAL_TYPE_P (type
)
7488 && vrp_val_is_min (vr0
->min
)
7489 && vrp_val_is_max (vr0
->max
)
7490 && is_gimple_min_invariant (op1
))
7492 location_t location
;
7494 if (!gimple_has_location (stmt
))
7495 location
= input_location
;
7497 location
= gimple_location (stmt
);
7499 warning_at (location
, OPT_Wtype_limits
,
7501 ? G_("comparison always false "
7502 "due to limited range of data type")
7503 : G_("comparison always true "
7504 "due to limited range of data type"));
7512 /* Visit conditional statement STMT. If we can determine which edge
7513 will be taken out of STMT's basic block, record it in
7514 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7517 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7522 *taken_edge_p
= NULL
;
7524 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7529 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7530 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7531 fprintf (dump_file
, "\nWith known ranges\n");
7533 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7535 fprintf (dump_file
, "\t");
7536 print_generic_expr (dump_file
, use
, 0);
7537 fprintf (dump_file
, ": ");
7538 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7541 fprintf (dump_file
, "\n");
7544 /* Compute the value of the predicate COND by checking the known
7545 ranges of each of its operands.
7547 Note that we cannot evaluate all the equivalent ranges here
7548 because those ranges may not yet be final and with the current
7549 propagation strategy, we cannot determine when the value ranges
7550 of the names in the equivalence set have changed.
7552 For instance, given the following code fragment
7556 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7560 Assume that on the first visit to i_14, i_5 has the temporary
7561 range [8, 8] because the second argument to the PHI function is
7562 not yet executable. We derive the range ~[0, 0] for i_14 and the
7563 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7564 the first time, since i_14 is equivalent to the range [8, 8], we
7565 determine that the predicate is always false.
7567 On the next round of propagation, i_13 is determined to be
7568 VARYING, which causes i_5 to drop down to VARYING. So, another
7569 visit to i_14 is scheduled. In this second visit, we compute the
7570 exact same range and equivalence set for i_14, namely ~[0, 0] and
7571 { i_5 }. But we did not have the previous range for i_5
7572 registered, so vrp_visit_assignment thinks that the range for
7573 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7574 is not visited again, which stops propagation from visiting
7575 statements in the THEN clause of that if().
7577 To properly fix this we would need to keep the previous range
7578 value for the names in the equivalence set. This way we would've
7579 discovered that from one visit to the other i_5 changed from
7580 range [8, 8] to VR_VARYING.
7582 However, fixing this apparent limitation may not be worth the
7583 additional checking. Testing on several code bases (GCC, DLV,
7584 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7585 4 more predicates folded in SPEC. */
7588 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7589 gimple_cond_lhs (stmt
),
7590 gimple_cond_rhs (stmt
),
7595 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7598 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7600 "\nIgnoring predicate evaluation because "
7601 "it assumes that signed overflow is undefined");
7606 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7608 fprintf (dump_file
, "\nPredicate evaluates to: ");
7609 if (val
== NULL_TREE
)
7610 fprintf (dump_file
, "DON'T KNOW\n");
7612 print_generic_stmt (dump_file
, val
, 0);
7616 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7617 that includes the value VAL. The search is restricted to the range
7618 [START_IDX, n - 1] where n is the size of VEC.
7620 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7623 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7624 it is placed in IDX and false is returned.
7626 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7630 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7632 size_t n
= gimple_switch_num_labels (stmt
);
7635 /* Find case label for minimum of the value range or the next one.
7636 At each iteration we are searching in [low, high - 1]. */
7638 for (low
= start_idx
, high
= n
; high
!= low
; )
7642 /* Note that i != high, so we never ask for n. */
7643 size_t i
= (high
+ low
) / 2;
7644 t
= gimple_switch_label (stmt
, i
);
7646 /* Cache the result of comparing CASE_LOW and val. */
7647 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7651 /* Ranges cannot be empty. */
7660 if (CASE_HIGH (t
) != NULL
7661 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7673 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7674 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7675 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7676 then MAX_IDX < MIN_IDX.
7677 Returns true if the default label is not needed. */
7680 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7684 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7685 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7689 && max_take_default
)
7691 /* Only the default case label reached.
7692 Return an empty range. */
7699 bool take_default
= min_take_default
|| max_take_default
;
7703 if (max_take_default
)
7706 /* If the case label range is continuous, we do not need
7707 the default case label. Verify that. */
7708 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7709 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7710 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7711 for (k
= i
+ 1; k
<= j
; ++k
)
7713 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7714 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7716 take_default
= true;
7720 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7721 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7726 return !take_default
;
7730 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7731 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7732 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7733 Returns true if the default label is not needed. */
7736 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
7737 size_t *max_idx1
, size_t *min_idx2
,
7741 unsigned int n
= gimple_switch_num_labels (stmt
);
7743 tree case_low
, case_high
;
7744 tree min
= vr
->min
, max
= vr
->max
;
7746 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7748 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7750 /* Set second range to emtpy. */
7754 if (vr
->type
== VR_RANGE
)
7758 return !take_default
;
7761 /* Set first range to all case labels. */
7768 /* Make sure all the values of case labels [i , j] are contained in
7769 range [MIN, MAX]. */
7770 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7771 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7772 if (tree_int_cst_compare (case_low
, min
) < 0)
7774 if (case_high
!= NULL_TREE
7775 && tree_int_cst_compare (max
, case_high
) < 0)
7781 /* If the range spans case labels [i, j], the corresponding anti-range spans
7782 the labels [1, i - 1] and [j + 1, n - 1]. */
7808 /* Visit switch statement STMT. If we can determine which edge
7809 will be taken out of STMT's basic block, record it in
7810 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7813 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7817 size_t i
= 0, j
= 0, k
, l
;
7820 *taken_edge_p
= NULL
;
7821 op
= gimple_switch_index (stmt
);
7822 if (TREE_CODE (op
) != SSA_NAME
)
7825 vr
= get_value_range (op
);
7826 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7828 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7829 print_generic_expr (dump_file
, op
, 0);
7830 fprintf (dump_file
, " with known range ");
7831 dump_value_range (dump_file
, vr
);
7832 fprintf (dump_file
, "\n");
7835 if ((vr
->type
!= VR_RANGE
7836 && vr
->type
!= VR_ANTI_RANGE
)
7837 || symbolic_range_p (vr
))
7840 /* Find the single edge that is taken from the switch expression. */
7841 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7843 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7847 gcc_assert (take_default
);
7848 val
= gimple_switch_default_label (stmt
);
7852 /* Check if labels with index i to j and maybe the default label
7853 are all reaching the same label. */
7855 val
= gimple_switch_label (stmt
, i
);
7857 && CASE_LABEL (gimple_switch_default_label (stmt
))
7858 != CASE_LABEL (val
))
7860 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7861 fprintf (dump_file
, " not a single destination for this "
7865 for (++i
; i
<= j
; ++i
)
7867 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7869 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7870 fprintf (dump_file
, " not a single destination for this "
7877 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7879 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7880 fprintf (dump_file
, " not a single destination for this "
7887 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7888 label_to_block (CASE_LABEL (val
)));
7890 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7892 fprintf (dump_file
, " will take edge to ");
7893 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7898 /* Evaluate statement STMT. If the statement produces a useful range,
7899 set VR and corepsponding OUTPUT_P.
7901 If STMT is a conditional branch and we can determine its truth
7902 value, the taken edge is recorded in *TAKEN_EDGE_P. */
7905 extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
7906 tree
*output_p
, value_range
*vr
)
7909 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7911 fprintf (dump_file
, "\nVisiting statement:\n");
7912 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7915 if (!stmt_interesting_for_vrp (stmt
))
7916 gcc_assert (stmt_ends_bb_p (stmt
));
7917 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7918 vrp_visit_assignment_or_call (stmt
, output_p
, vr
);
7919 else if (gimple_code (stmt
) == GIMPLE_COND
)
7920 vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
7921 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7922 vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
7925 /* Evaluate statement STMT. If the statement produces a useful range,
7926 return SSA_PROP_INTERESTING and record the SSA name with the
7927 interesting range into *OUTPUT_P.
7929 If STMT is a conditional branch and we can determine its truth
7930 value, the taken edge is recorded in *TAKEN_EDGE_P.
7932 If STMT produces a varying value, return SSA_PROP_VARYING. */
7934 static enum ssa_prop_result
7935 vrp_visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
7937 value_range vr
= VR_INITIALIZER
;
7938 tree lhs
= gimple_get_lhs (stmt
);
7939 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
7943 if (update_value_range (*output_p
, &vr
))
7945 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7947 fprintf (dump_file
, "Found new range for ");
7948 print_generic_expr (dump_file
, *output_p
, 0);
7949 fprintf (dump_file
, ": ");
7950 dump_value_range (dump_file
, &vr
);
7951 fprintf (dump_file
, "\n");
7954 if (vr
.type
== VR_VARYING
)
7955 return SSA_PROP_VARYING
;
7957 return SSA_PROP_INTERESTING
;
7959 return SSA_PROP_NOT_INTERESTING
;
7962 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7963 switch (gimple_call_internal_fn (stmt
))
7965 case IFN_ADD_OVERFLOW
:
7966 case IFN_SUB_OVERFLOW
:
7967 case IFN_MUL_OVERFLOW
:
7968 case IFN_ATOMIC_COMPARE_EXCHANGE
:
7969 /* These internal calls return _Complex integer type,
7970 which VRP does not track, but the immediate uses
7971 thereof might be interesting. */
7972 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7974 imm_use_iterator iter
;
7975 use_operand_p use_p
;
7976 enum ssa_prop_result res
= SSA_PROP_VARYING
;
7978 set_value_range_to_varying (get_value_range (lhs
));
7980 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
7982 gimple
*use_stmt
= USE_STMT (use_p
);
7983 if (!is_gimple_assign (use_stmt
))
7985 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
7986 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
7988 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
7989 tree use_lhs
= gimple_assign_lhs (use_stmt
);
7990 if (TREE_CODE (rhs1
) != rhs_code
7991 || TREE_OPERAND (rhs1
, 0) != lhs
7992 || TREE_CODE (use_lhs
) != SSA_NAME
7993 || !stmt_interesting_for_vrp (use_stmt
)
7994 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
7995 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
7996 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
7999 /* If there is a change in the value range for any of the
8000 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
8001 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
8002 or IMAGPART_EXPR immediate uses, but none of them have
8003 a change in their value ranges, return
8004 SSA_PROP_NOT_INTERESTING. If there are no
8005 {REAL,IMAG}PART_EXPR uses at all,
8006 return SSA_PROP_VARYING. */
8007 value_range new_vr
= VR_INITIALIZER
;
8008 extract_range_basic (&new_vr
, use_stmt
);
8009 value_range
*old_vr
= get_value_range (use_lhs
);
8010 if (old_vr
->type
!= new_vr
.type
8011 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
8012 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
8013 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
8014 res
= SSA_PROP_INTERESTING
;
8016 res
= SSA_PROP_NOT_INTERESTING
;
8017 BITMAP_FREE (new_vr
.equiv
);
8018 if (res
== SSA_PROP_INTERESTING
)
8032 /* All other statements produce nothing of interest for VRP, so mark
8033 their outputs varying and prevent further simulation. */
8034 set_defs_to_varying (stmt
);
8036 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
8039 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8040 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8041 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8042 possible such range. The resulting range is not canonicalized. */
8045 union_ranges (enum value_range_type
*vr0type
,
8046 tree
*vr0min
, tree
*vr0max
,
8047 enum value_range_type vr1type
,
8048 tree vr1min
, tree vr1max
)
8050 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8051 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8053 /* [] is vr0, () is vr1 in the following classification comments. */
8057 if (*vr0type
== vr1type
)
8058 /* Nothing to do for equal ranges. */
8060 else if ((*vr0type
== VR_RANGE
8061 && vr1type
== VR_ANTI_RANGE
)
8062 || (*vr0type
== VR_ANTI_RANGE
8063 && vr1type
== VR_RANGE
))
8065 /* For anti-range with range union the result is varying. */
8071 else if (operand_less_p (*vr0max
, vr1min
) == 1
8072 || operand_less_p (vr1max
, *vr0min
) == 1)
8074 /* [ ] ( ) or ( ) [ ]
8075 If the ranges have an empty intersection, result of the union
8076 operation is the anti-range or if both are anti-ranges
8078 if (*vr0type
== VR_ANTI_RANGE
8079 && vr1type
== VR_ANTI_RANGE
)
8081 else if (*vr0type
== VR_ANTI_RANGE
8082 && vr1type
== VR_RANGE
)
8084 else if (*vr0type
== VR_RANGE
8085 && vr1type
== VR_ANTI_RANGE
)
8091 else if (*vr0type
== VR_RANGE
8092 && vr1type
== VR_RANGE
)
8094 /* The result is the convex hull of both ranges. */
8095 if (operand_less_p (*vr0max
, vr1min
) == 1)
8097 /* If the result can be an anti-range, create one. */
8098 if (TREE_CODE (*vr0max
) == INTEGER_CST
8099 && TREE_CODE (vr1min
) == INTEGER_CST
8100 && vrp_val_is_min (*vr0min
)
8101 && vrp_val_is_max (vr1max
))
8103 tree min
= int_const_binop (PLUS_EXPR
,
8105 build_int_cst (TREE_TYPE (*vr0max
), 1));
8106 tree max
= int_const_binop (MINUS_EXPR
,
8108 build_int_cst (TREE_TYPE (vr1min
), 1));
8109 if (!operand_less_p (max
, min
))
8111 *vr0type
= VR_ANTI_RANGE
;
8123 /* If the result can be an anti-range, create one. */
8124 if (TREE_CODE (vr1max
) == INTEGER_CST
8125 && TREE_CODE (*vr0min
) == INTEGER_CST
8126 && vrp_val_is_min (vr1min
)
8127 && vrp_val_is_max (*vr0max
))
8129 tree min
= int_const_binop (PLUS_EXPR
,
8131 build_int_cst (TREE_TYPE (vr1max
), 1));
8132 tree max
= int_const_binop (MINUS_EXPR
,
8134 build_int_cst (TREE_TYPE (*vr0min
), 1));
8135 if (!operand_less_p (max
, min
))
8137 *vr0type
= VR_ANTI_RANGE
;
8151 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8152 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8154 /* [ ( ) ] or [( ) ] or [ ( )] */
8155 if (*vr0type
== VR_RANGE
8156 && vr1type
== VR_RANGE
)
8158 else if (*vr0type
== VR_ANTI_RANGE
8159 && vr1type
== VR_ANTI_RANGE
)
8165 else if (*vr0type
== VR_ANTI_RANGE
8166 && vr1type
== VR_RANGE
)
8168 /* Arbitrarily choose the right or left gap. */
8169 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8170 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8171 build_int_cst (TREE_TYPE (vr1min
), 1));
8172 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8173 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8174 build_int_cst (TREE_TYPE (vr1max
), 1));
8178 else if (*vr0type
== VR_RANGE
8179 && vr1type
== VR_ANTI_RANGE
)
8180 /* The result covers everything. */
8185 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8186 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8188 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8189 if (*vr0type
== VR_RANGE
8190 && vr1type
== VR_RANGE
)
8196 else if (*vr0type
== VR_ANTI_RANGE
8197 && vr1type
== VR_ANTI_RANGE
)
8199 else if (*vr0type
== VR_RANGE
8200 && vr1type
== VR_ANTI_RANGE
)
8202 *vr0type
= VR_ANTI_RANGE
;
8203 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8205 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8206 build_int_cst (TREE_TYPE (*vr0min
), 1));
8209 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8211 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8212 build_int_cst (TREE_TYPE (*vr0max
), 1));
8218 else if (*vr0type
== VR_ANTI_RANGE
8219 && vr1type
== VR_RANGE
)
8220 /* The result covers everything. */
8225 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8226 || operand_equal_p (vr1min
, *vr0max
, 0))
8227 && operand_less_p (*vr0min
, vr1min
) == 1
8228 && operand_less_p (*vr0max
, vr1max
) == 1)
8230 /* [ ( ] ) or [ ]( ) */
8231 if (*vr0type
== VR_RANGE
8232 && vr1type
== VR_RANGE
)
8234 else if (*vr0type
== VR_ANTI_RANGE
8235 && vr1type
== VR_ANTI_RANGE
)
8237 else if (*vr0type
== VR_ANTI_RANGE
8238 && vr1type
== VR_RANGE
)
8240 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8241 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8242 build_int_cst (TREE_TYPE (vr1min
), 1));
8246 else if (*vr0type
== VR_RANGE
8247 && vr1type
== VR_ANTI_RANGE
)
8249 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8252 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8253 build_int_cst (TREE_TYPE (*vr0max
), 1));
8262 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8263 || operand_equal_p (*vr0min
, vr1max
, 0))
8264 && operand_less_p (vr1min
, *vr0min
) == 1
8265 && operand_less_p (vr1max
, *vr0max
) == 1)
8267 /* ( [ ) ] or ( )[ ] */
8268 if (*vr0type
== VR_RANGE
8269 && vr1type
== VR_RANGE
)
8271 else if (*vr0type
== VR_ANTI_RANGE
8272 && vr1type
== VR_ANTI_RANGE
)
8274 else if (*vr0type
== VR_ANTI_RANGE
8275 && vr1type
== VR_RANGE
)
8277 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8278 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8279 build_int_cst (TREE_TYPE (vr1max
), 1));
8283 else if (*vr0type
== VR_RANGE
8284 && vr1type
== VR_ANTI_RANGE
)
8286 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8290 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8291 build_int_cst (TREE_TYPE (*vr0min
), 1));
8305 *vr0type
= VR_VARYING
;
8306 *vr0min
= NULL_TREE
;
8307 *vr0max
= NULL_TREE
;
8310 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8311 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8312 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8313 possible such range. The resulting range is not canonicalized. */
8316 intersect_ranges (enum value_range_type
*vr0type
,
8317 tree
*vr0min
, tree
*vr0max
,
8318 enum value_range_type vr1type
,
8319 tree vr1min
, tree vr1max
)
8321 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8322 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8324 /* [] is vr0, () is vr1 in the following classification comments. */
8328 if (*vr0type
== vr1type
)
8329 /* Nothing to do for equal ranges. */
8331 else if ((*vr0type
== VR_RANGE
8332 && vr1type
== VR_ANTI_RANGE
)
8333 || (*vr0type
== VR_ANTI_RANGE
8334 && vr1type
== VR_RANGE
))
8336 /* For anti-range with range intersection the result is empty. */
8337 *vr0type
= VR_UNDEFINED
;
8338 *vr0min
= NULL_TREE
;
8339 *vr0max
= NULL_TREE
;
8344 else if (operand_less_p (*vr0max
, vr1min
) == 1
8345 || operand_less_p (vr1max
, *vr0min
) == 1)
8347 /* [ ] ( ) or ( ) [ ]
8348 If the ranges have an empty intersection, the result of the
8349 intersect operation is the range for intersecting an
8350 anti-range with a range or empty when intersecting two ranges. */
8351 if (*vr0type
== VR_RANGE
8352 && vr1type
== VR_ANTI_RANGE
)
8354 else if (*vr0type
== VR_ANTI_RANGE
8355 && vr1type
== VR_RANGE
)
8361 else if (*vr0type
== VR_RANGE
8362 && vr1type
== VR_RANGE
)
8364 *vr0type
= VR_UNDEFINED
;
8365 *vr0min
= NULL_TREE
;
8366 *vr0max
= NULL_TREE
;
8368 else if (*vr0type
== VR_ANTI_RANGE
8369 && vr1type
== VR_ANTI_RANGE
)
8371 /* If the anti-ranges are adjacent to each other merge them. */
8372 if (TREE_CODE (*vr0max
) == INTEGER_CST
8373 && TREE_CODE (vr1min
) == INTEGER_CST
8374 && operand_less_p (*vr0max
, vr1min
) == 1
8375 && integer_onep (int_const_binop (MINUS_EXPR
,
8378 else if (TREE_CODE (vr1max
) == INTEGER_CST
8379 && TREE_CODE (*vr0min
) == INTEGER_CST
8380 && operand_less_p (vr1max
, *vr0min
) == 1
8381 && integer_onep (int_const_binop (MINUS_EXPR
,
8384 /* Else arbitrarily take VR0. */
8387 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8388 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8390 /* [ ( ) ] or [( ) ] or [ ( )] */
8391 if (*vr0type
== VR_RANGE
8392 && vr1type
== VR_RANGE
)
8394 /* If both are ranges the result is the inner one. */
8399 else if (*vr0type
== VR_RANGE
8400 && vr1type
== VR_ANTI_RANGE
)
8402 /* Choose the right gap if the left one is empty. */
8405 if (TREE_CODE (vr1max
) != INTEGER_CST
)
8407 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
8408 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
8410 = int_const_binop (MINUS_EXPR
, vr1max
,
8411 build_int_cst (TREE_TYPE (vr1max
), -1));
8414 = int_const_binop (PLUS_EXPR
, vr1max
,
8415 build_int_cst (TREE_TYPE (vr1max
), 1));
8417 /* Choose the left gap if the right one is empty. */
8420 if (TREE_CODE (vr1min
) != INTEGER_CST
)
8422 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
8423 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
8425 = int_const_binop (PLUS_EXPR
, vr1min
,
8426 build_int_cst (TREE_TYPE (vr1min
), -1));
8429 = int_const_binop (MINUS_EXPR
, vr1min
,
8430 build_int_cst (TREE_TYPE (vr1min
), 1));
8432 /* Choose the anti-range if the range is effectively varying. */
8433 else if (vrp_val_is_min (*vr0min
)
8434 && vrp_val_is_max (*vr0max
))
8440 /* Else choose the range. */
8442 else if (*vr0type
== VR_ANTI_RANGE
8443 && vr1type
== VR_ANTI_RANGE
)
8444 /* If both are anti-ranges the result is the outer one. */
8446 else if (*vr0type
== VR_ANTI_RANGE
8447 && vr1type
== VR_RANGE
)
8449 /* The intersection is empty. */
8450 *vr0type
= VR_UNDEFINED
;
8451 *vr0min
= NULL_TREE
;
8452 *vr0max
= NULL_TREE
;
8457 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8458 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8460 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8461 if (*vr0type
== VR_RANGE
8462 && vr1type
== VR_RANGE
)
8463 /* Choose the inner range. */
8465 else if (*vr0type
== VR_ANTI_RANGE
8466 && vr1type
== VR_RANGE
)
8468 /* Choose the right gap if the left is empty. */
8471 *vr0type
= VR_RANGE
;
8472 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
8474 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
8475 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
8477 = int_const_binop (MINUS_EXPR
, *vr0max
,
8478 build_int_cst (TREE_TYPE (*vr0max
), -1));
8481 = int_const_binop (PLUS_EXPR
, *vr0max
,
8482 build_int_cst (TREE_TYPE (*vr0max
), 1));
8485 /* Choose the left gap if the right is empty. */
8488 *vr0type
= VR_RANGE
;
8489 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
8491 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
8492 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
8494 = int_const_binop (PLUS_EXPR
, *vr0min
,
8495 build_int_cst (TREE_TYPE (*vr0min
), -1));
8498 = int_const_binop (MINUS_EXPR
, *vr0min
,
8499 build_int_cst (TREE_TYPE (*vr0min
), 1));
8502 /* Choose the anti-range if the range is effectively varying. */
8503 else if (vrp_val_is_min (vr1min
)
8504 && vrp_val_is_max (vr1max
))
8506 /* Choose the anti-range if it is ~[0,0], that range is special
8507 enough to special case when vr1's range is relatively wide. */
8508 else if (*vr0min
== *vr0max
8509 && integer_zerop (*vr0min
)
8510 && (TYPE_PRECISION (TREE_TYPE (*vr0min
))
8511 == TYPE_PRECISION (ptr_type_node
))
8512 && TREE_CODE (vr1max
) == INTEGER_CST
8513 && TREE_CODE (vr1min
) == INTEGER_CST
8514 && (wi::clz (wi::sub (vr1max
, vr1min
))
8515 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
8517 /* Else choose the range. */
8525 else if (*vr0type
== VR_ANTI_RANGE
8526 && vr1type
== VR_ANTI_RANGE
)
8528 /* If both are anti-ranges the result is the outer one. */
8533 else if (vr1type
== VR_ANTI_RANGE
8534 && *vr0type
== VR_RANGE
)
8536 /* The intersection is empty. */
8537 *vr0type
= VR_UNDEFINED
;
8538 *vr0min
= NULL_TREE
;
8539 *vr0max
= NULL_TREE
;
8544 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8545 || operand_equal_p (vr1min
, *vr0max
, 0))
8546 && operand_less_p (*vr0min
, vr1min
) == 1)
8548 /* [ ( ] ) or [ ]( ) */
8549 if (*vr0type
== VR_ANTI_RANGE
8550 && vr1type
== VR_ANTI_RANGE
)
8552 else if (*vr0type
== VR_RANGE
8553 && vr1type
== VR_RANGE
)
8555 else if (*vr0type
== VR_RANGE
8556 && vr1type
== VR_ANTI_RANGE
)
8558 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8559 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8560 build_int_cst (TREE_TYPE (vr1min
), 1));
8564 else if (*vr0type
== VR_ANTI_RANGE
8565 && vr1type
== VR_RANGE
)
8567 *vr0type
= VR_RANGE
;
8568 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8569 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8570 build_int_cst (TREE_TYPE (*vr0max
), 1));
8578 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8579 || operand_equal_p (*vr0min
, vr1max
, 0))
8580 && operand_less_p (vr1min
, *vr0min
) == 1)
8582 /* ( [ ) ] or ( )[ ] */
8583 if (*vr0type
== VR_ANTI_RANGE
8584 && vr1type
== VR_ANTI_RANGE
)
8586 else if (*vr0type
== VR_RANGE
8587 && vr1type
== VR_RANGE
)
8589 else if (*vr0type
== VR_RANGE
8590 && vr1type
== VR_ANTI_RANGE
)
8592 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8593 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8594 build_int_cst (TREE_TYPE (vr1max
), 1));
8598 else if (*vr0type
== VR_ANTI_RANGE
8599 && vr1type
== VR_RANGE
)
8601 *vr0type
= VR_RANGE
;
8602 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8603 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8604 build_int_cst (TREE_TYPE (*vr0min
), 1));
8613 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8614 result for the intersection. That's always a conservative
8615 correct estimate unless VR1 is a constant singleton range
8616 in which case we choose that. */
8617 if (vr1type
== VR_RANGE
8618 && is_gimple_min_invariant (vr1min
)
8619 && vrp_operand_equal_p (vr1min
, vr1max
))
8630 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8631 in *VR0. This may not be the smallest possible such range. */
8634 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
8638 /* If either range is VR_VARYING the other one wins. */
8639 if (vr1
->type
== VR_VARYING
)
8641 if (vr0
->type
== VR_VARYING
)
8643 copy_value_range (vr0
, vr1
);
8647 /* When either range is VR_UNDEFINED the resulting range is
8648 VR_UNDEFINED, too. */
8649 if (vr0
->type
== VR_UNDEFINED
)
8651 if (vr1
->type
== VR_UNDEFINED
)
8653 set_value_range_to_undefined (vr0
);
8657 /* Save the original vr0 so we can return it as conservative intersection
8658 result when our worker turns things to varying. */
8660 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8661 vr1
->type
, vr1
->min
, vr1
->max
);
8662 /* Make sure to canonicalize the result though as the inversion of a
8663 VR_RANGE can still be a VR_RANGE. */
8664 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8665 vr0
->min
, vr0
->max
, vr0
->equiv
);
8666 /* If that failed, use the saved original VR0. */
8667 if (vr0
->type
== VR_VARYING
)
8672 /* If the result is VR_UNDEFINED there is no need to mess with
8673 the equivalencies. */
8674 if (vr0
->type
== VR_UNDEFINED
)
8677 /* The resulting set of equivalences for range intersection is the union of
8679 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8680 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8681 else if (vr1
->equiv
&& !vr0
->equiv
)
8683 vr0
->equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
8684 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8689 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
8691 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8693 fprintf (dump_file
, "Intersecting\n ");
8694 dump_value_range (dump_file
, vr0
);
8695 fprintf (dump_file
, "\nand\n ");
8696 dump_value_range (dump_file
, vr1
);
8697 fprintf (dump_file
, "\n");
8699 vrp_intersect_ranges_1 (vr0
, vr1
);
8700 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8702 fprintf (dump_file
, "to\n ");
8703 dump_value_range (dump_file
, vr0
);
8704 fprintf (dump_file
, "\n");
8708 /* Meet operation for value ranges. Given two value ranges VR0 and
8709 VR1, store in VR0 a range that contains both VR0 and VR1. This
8710 may not be the smallest possible such range. */
8713 vrp_meet_1 (value_range
*vr0
, const value_range
*vr1
)
8717 if (vr0
->type
== VR_UNDEFINED
)
8719 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8723 if (vr1
->type
== VR_UNDEFINED
)
8725 /* VR0 already has the resulting range. */
8729 if (vr0
->type
== VR_VARYING
)
8731 /* Nothing to do. VR0 already has the resulting range. */
8735 if (vr1
->type
== VR_VARYING
)
8737 set_value_range_to_varying (vr0
);
8742 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8743 vr1
->type
, vr1
->min
, vr1
->max
);
8744 if (vr0
->type
== VR_VARYING
)
8746 /* Failed to find an efficient meet. Before giving up and setting
8747 the result to VARYING, see if we can at least derive a useful
8748 anti-range. FIXME, all this nonsense about distinguishing
8749 anti-ranges from ranges is necessary because of the odd
8750 semantics of range_includes_zero_p and friends. */
8751 if (((saved
.type
== VR_RANGE
8752 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8753 || (saved
.type
== VR_ANTI_RANGE
8754 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8755 && ((vr1
->type
== VR_RANGE
8756 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8757 || (vr1
->type
== VR_ANTI_RANGE
8758 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8760 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8762 /* Since this meet operation did not result from the meeting of
8763 two equivalent names, VR0 cannot have any equivalences. */
8765 bitmap_clear (vr0
->equiv
);
8769 set_value_range_to_varying (vr0
);
8772 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8774 if (vr0
->type
== VR_VARYING
)
8777 /* The resulting set of equivalences is always the intersection of
8779 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8780 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8781 else if (vr0
->equiv
&& !vr1
->equiv
)
8782 bitmap_clear (vr0
->equiv
);
8786 vrp_meet (value_range
*vr0
, const value_range
*vr1
)
8788 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8790 fprintf (dump_file
, "Meeting\n ");
8791 dump_value_range (dump_file
, vr0
);
8792 fprintf (dump_file
, "\nand\n ");
8793 dump_value_range (dump_file
, vr1
);
8794 fprintf (dump_file
, "\n");
8796 vrp_meet_1 (vr0
, vr1
);
8797 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8799 fprintf (dump_file
, "to\n ");
8800 dump_value_range (dump_file
, vr0
);
8801 fprintf (dump_file
, "\n");
8806 /* Visit all arguments for PHI node PHI that flow through executable
8807 edges. If a valid value range can be derived from all the incoming
8808 value ranges, set a new range in VR_RESULT. */
8811 extract_range_from_phi_node (gphi
*phi
, value_range
*vr_result
)
8814 tree lhs
= PHI_RESULT (phi
);
8815 value_range
*lhs_vr
= get_value_range (lhs
);
8817 int edges
, old_edges
;
8820 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8822 fprintf (dump_file
, "\nVisiting PHI node: ");
8823 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8826 bool may_simulate_backedge_again
= false;
8828 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8830 edge e
= gimple_phi_arg_edge (phi
, i
);
8832 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8835 " Argument #%d (%d -> %d %sexecutable)\n",
8836 (int) i
, e
->src
->index
, e
->dest
->index
,
8837 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8840 if (e
->flags
& EDGE_EXECUTABLE
)
8842 tree arg
= PHI_ARG_DEF (phi
, i
);
8847 if (TREE_CODE (arg
) == SSA_NAME
)
8849 /* See if we are eventually going to change one of the args. */
8850 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
8851 if (! gimple_nop_p (def_stmt
)
8852 && prop_simulate_again_p (def_stmt
)
8853 && e
->flags
& EDGE_DFS_BACK
)
8854 may_simulate_backedge_again
= true;
8856 vr_arg
= *(get_value_range (arg
));
8857 /* Do not allow equivalences or symbolic ranges to leak in from
8858 backedges. That creates invalid equivalencies.
8859 See PR53465 and PR54767. */
8860 if (e
->flags
& EDGE_DFS_BACK
)
8862 if (vr_arg
.type
== VR_RANGE
8863 || vr_arg
.type
== VR_ANTI_RANGE
)
8865 vr_arg
.equiv
= NULL
;
8866 if (symbolic_range_p (&vr_arg
))
8868 vr_arg
.type
= VR_VARYING
;
8869 vr_arg
.min
= NULL_TREE
;
8870 vr_arg
.max
= NULL_TREE
;
8876 /* If the non-backedge arguments range is VR_VARYING then
8877 we can still try recording a simple equivalence. */
8878 if (vr_arg
.type
== VR_VARYING
)
8880 vr_arg
.type
= VR_RANGE
;
8883 vr_arg
.equiv
= NULL
;
8889 if (TREE_OVERFLOW_P (arg
))
8890 arg
= drop_tree_overflow (arg
);
8892 vr_arg
.type
= VR_RANGE
;
8895 vr_arg
.equiv
= NULL
;
8898 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8900 fprintf (dump_file
, "\t");
8901 print_generic_expr (dump_file
, arg
, dump_flags
);
8902 fprintf (dump_file
, ": ");
8903 dump_value_range (dump_file
, &vr_arg
);
8904 fprintf (dump_file
, "\n");
8908 copy_value_range (vr_result
, &vr_arg
);
8910 vrp_meet (vr_result
, &vr_arg
);
8913 if (vr_result
->type
== VR_VARYING
)
8918 if (vr_result
->type
== VR_VARYING
)
8920 else if (vr_result
->type
== VR_UNDEFINED
)
8923 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8924 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8926 /* To prevent infinite iterations in the algorithm, derive ranges
8927 when the new value is slightly bigger or smaller than the
8928 previous one. We don't do this if we have seen a new executable
8929 edge; this helps us avoid an overflow infinity for conditionals
8930 which are not in a loop. If the old value-range was VR_UNDEFINED
8931 use the updated range and iterate one more time. If we will not
8932 simulate this PHI again via the backedge allow us to iterate. */
8934 && gimple_phi_num_args (phi
) > 1
8935 && edges
== old_edges
8936 && lhs_vr
->type
!= VR_UNDEFINED
8937 && may_simulate_backedge_again
)
8939 /* Compare old and new ranges, fall back to varying if the
8940 values are not comparable. */
8941 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
->min
);
8944 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
->max
);
8948 /* For non VR_RANGE or for pointers fall back to varying if
8949 the range changed. */
8950 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
->type
!= VR_RANGE
8951 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8952 && (cmp_min
!= 0 || cmp_max
!= 0))
8955 /* If the new minimum is larger than the previous one
8956 retain the old value. If the new minimum value is smaller
8957 than the previous one and not -INF go all the way to -INF + 1.
8958 In the first case, to avoid infinite bouncing between different
8959 minimums, and in the other case to avoid iterating millions of
8960 times to reach -INF. Going to -INF + 1 also lets the following
8961 iteration compute whether there will be any overflow, at the
8962 expense of one additional iteration. */
8964 vr_result
->min
= lhs_vr
->min
;
8965 else if (cmp_min
> 0
8966 && !vrp_val_is_min (vr_result
->min
))
8968 = int_const_binop (PLUS_EXPR
,
8969 vrp_val_min (TREE_TYPE (vr_result
->min
)),
8970 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
8972 /* Similarly for the maximum value. */
8974 vr_result
->max
= lhs_vr
->max
;
8975 else if (cmp_max
< 0
8976 && !vrp_val_is_max (vr_result
->max
))
8978 = int_const_binop (MINUS_EXPR
,
8979 vrp_val_max (TREE_TYPE (vr_result
->min
)),
8980 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
8982 /* If we dropped either bound to +-INF then if this is a loop
8983 PHI node SCEV may known more about its value-range. */
8984 if (cmp_min
> 0 || cmp_min
< 0
8985 || cmp_max
< 0 || cmp_max
> 0)
8988 goto infinite_check
;
8994 set_value_range_to_varying (vr_result
);
8997 /* If this is a loop PHI node SCEV may known more about its value-range.
8998 scev_check can be reached from two paths, one is a fall through from above
8999 "varying" label, the other is direct goto from code block which tries to
9000 avoid infinite simulation. */
9001 if ((l
= loop_containing_stmt (phi
))
9002 && l
->header
== gimple_bb (phi
))
9003 adjust_range_with_scev (vr_result
, l
, phi
, lhs
);
9006 /* If we will end up with a (-INF, +INF) range, set it to
9007 VARYING. Same if the previous max value was invalid for
9008 the type and we end up with vr_result.min > vr_result.max. */
9009 if ((vr_result
->type
== VR_RANGE
|| vr_result
->type
== VR_ANTI_RANGE
)
9010 && !((vrp_val_is_max (vr_result
->max
) && vrp_val_is_min (vr_result
->min
))
9011 || compare_values (vr_result
->min
, vr_result
->max
) > 0))
9014 set_value_range_to_varying (vr_result
);
9016 /* If the new range is different than the previous value, keep
9022 /* Visit all arguments for PHI node PHI that flow through executable
9023 edges. If a valid value range can be derived from all the incoming
9024 value ranges, set a new range for the LHS of PHI. */
9026 static enum ssa_prop_result
9027 vrp_visit_phi_node (gphi
*phi
)
9029 tree lhs
= PHI_RESULT (phi
);
9030 value_range vr_result
= VR_INITIALIZER
;
9031 extract_range_from_phi_node (phi
, &vr_result
);
9032 if (update_value_range (lhs
, &vr_result
))
9034 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9036 fprintf (dump_file
, "Found new range for ");
9037 print_generic_expr (dump_file
, lhs
, 0);
9038 fprintf (dump_file
, ": ");
9039 dump_value_range (dump_file
, &vr_result
);
9040 fprintf (dump_file
, "\n");
9043 if (vr_result
.type
== VR_VARYING
)
9044 return SSA_PROP_VARYING
;
9046 return SSA_PROP_INTERESTING
;
9049 /* Nothing changed, don't add outgoing edges. */
9050 return SSA_PROP_NOT_INTERESTING
;
9053 /* Simplify boolean operations if the source is known
9054 to be already a boolean. */
9056 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9058 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9060 bool need_conversion
;
9062 /* We handle only !=/== case here. */
9063 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
9065 op0
= gimple_assign_rhs1 (stmt
);
9066 if (!op_with_boolean_value_range_p (op0
))
9069 op1
= gimple_assign_rhs2 (stmt
);
9070 if (!op_with_boolean_value_range_p (op1
))
9073 /* Reduce number of cases to handle to NE_EXPR. As there is no
9074 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9075 if (rhs_code
== EQ_EXPR
)
9077 if (TREE_CODE (op1
) == INTEGER_CST
)
9078 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
9079 build_int_cst (TREE_TYPE (op1
), 1));
9084 lhs
= gimple_assign_lhs (stmt
);
9086 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
9088 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9090 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
9091 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
9092 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
9095 /* For A != 0 we can substitute A itself. */
9096 if (integer_zerop (op1
))
9097 gimple_assign_set_rhs_with_ops (gsi
,
9099 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
9100 /* For A != B we substitute A ^ B. Either with conversion. */
9101 else if (need_conversion
)
9103 tree tem
= make_ssa_name (TREE_TYPE (op0
));
9105 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
9106 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
9107 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9108 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
9109 set_range_info (tem
, VR_RANGE
,
9110 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
9111 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
9112 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
9116 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
9117 update_stmt (gsi_stmt (*gsi
));
9118 fold_stmt (gsi
, follow_single_use_edges
);
9123 /* Simplify a division or modulo operator to a right shift or bitwise and
9124 if the first operand is unsigned or is greater than zero and the second
9125 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
9126 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
9127 optimize it into just op0 if op0's range is known to be a subset of
9128 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
9132 simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9134 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9136 tree op0
= gimple_assign_rhs1 (stmt
);
9137 tree op1
= gimple_assign_rhs2 (stmt
);
9138 tree op0min
= NULL_TREE
, op0max
= NULL_TREE
;
9140 value_range
*vr
= NULL
;
9142 if (TREE_CODE (op0
) == INTEGER_CST
)
9149 vr
= get_value_range (op0
);
9150 if (range_int_cst_p (vr
))
9157 if (rhs_code
== TRUNC_MOD_EXPR
9158 && TREE_CODE (op1
) == SSA_NAME
)
9160 value_range
*vr1
= get_value_range (op1
);
9161 if (range_int_cst_p (vr1
))
9164 if (rhs_code
== TRUNC_MOD_EXPR
9165 && TREE_CODE (op1min
) == INTEGER_CST
9166 && tree_int_cst_sgn (op1min
) == 1
9168 && tree_int_cst_lt (op0max
, op1min
))
9170 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
9171 || tree_int_cst_sgn (op0min
) >= 0
9172 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1min
), op1min
),
9175 /* If op0 already has the range op0 % op1 has,
9176 then TRUNC_MOD_EXPR won't change anything. */
9177 gimple_assign_set_rhs_from_tree (gsi
, op0
);
9182 if (TREE_CODE (op0
) != SSA_NAME
)
9185 if (!integer_pow2p (op1
))
9187 /* X % -Y can be only optimized into X % Y either if
9188 X is not INT_MIN, or Y is not -1. Fold it now, as after
9189 remove_range_assertions the range info might be not available
9191 if (rhs_code
== TRUNC_MOD_EXPR
9192 && fold_stmt (gsi
, follow_single_use_edges
))
9197 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
9198 val
= integer_one_node
;
9203 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
9207 && integer_onep (val
)
9208 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9210 location_t location
;
9212 if (!gimple_has_location (stmt
))
9213 location
= input_location
;
9215 location
= gimple_location (stmt
);
9216 warning_at (location
, OPT_Wstrict_overflow
,
9217 "assuming signed overflow does not occur when "
9218 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9222 if (val
&& integer_onep (val
))
9226 if (rhs_code
== TRUNC_DIV_EXPR
)
9228 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
9229 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
9230 gimple_assign_set_rhs1 (stmt
, op0
);
9231 gimple_assign_set_rhs2 (stmt
, t
);
9235 t
= build_int_cst (TREE_TYPE (op1
), 1);
9236 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9237 t
= fold_convert (TREE_TYPE (op0
), t
);
9239 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9240 gimple_assign_set_rhs1 (stmt
, op0
);
9241 gimple_assign_set_rhs2 (stmt
, t
);
9245 fold_stmt (gsi
, follow_single_use_edges
);
9252 /* Simplify a min or max if the ranges of the two operands are
9253 disjoint. Return true if we do simplify. */
9256 simplify_min_or_max_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9258 tree op0
= gimple_assign_rhs1 (stmt
);
9259 tree op1
= gimple_assign_rhs2 (stmt
);
9263 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9264 (LE_EXPR
, op0
, op1
, &sop
));
9268 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9269 (LT_EXPR
, op0
, op1
, &sop
));
9274 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9276 location_t location
;
9278 if (!gimple_has_location (stmt
))
9279 location
= input_location
;
9281 location
= gimple_location (stmt
);
9282 warning_at (location
, OPT_Wstrict_overflow
,
9283 "assuming signed overflow does not occur when "
9284 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9287 /* VAL == TRUE -> OP0 < or <= op1
9288 VAL == FALSE -> OP0 > or >= op1. */
9289 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
9290 == integer_zerop (val
)) ? op0
: op1
;
9291 gimple_assign_set_rhs_from_tree (gsi
, res
);
9298 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9299 ABS_EXPR. If the operand is <= 0, then simplify the
9300 ABS_EXPR into a NEGATE_EXPR. */
9303 simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9305 tree op
= gimple_assign_rhs1 (stmt
);
9306 value_range
*vr
= get_value_range (op
);
9313 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9316 /* The range is neither <= 0 nor > 0. Now see if it is
9317 either < 0 or >= 0. */
9319 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
9325 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9327 location_t location
;
9329 if (!gimple_has_location (stmt
))
9330 location
= input_location
;
9332 location
= gimple_location (stmt
);
9333 warning_at (location
, OPT_Wstrict_overflow
,
9334 "assuming signed overflow does not occur when "
9335 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9338 gimple_assign_set_rhs1 (stmt
, op
);
9339 if (integer_zerop (val
))
9340 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9342 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9344 fold_stmt (gsi
, follow_single_use_edges
);
9352 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9353 If all the bits that are being cleared by & are already
9354 known to be zero from VR, or all the bits that are being
9355 set by | are already known to be one from VR, the bit
9356 operation is redundant. */
9359 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9361 tree op0
= gimple_assign_rhs1 (stmt
);
9362 tree op1
= gimple_assign_rhs2 (stmt
);
9363 tree op
= NULL_TREE
;
9364 value_range vr0
= VR_INITIALIZER
;
9365 value_range vr1
= VR_INITIALIZER
;
9366 wide_int may_be_nonzero0
, may_be_nonzero1
;
9367 wide_int must_be_nonzero0
, must_be_nonzero1
;
9370 if (TREE_CODE (op0
) == SSA_NAME
)
9371 vr0
= *(get_value_range (op0
));
9372 else if (is_gimple_min_invariant (op0
))
9373 set_value_range_to_value (&vr0
, op0
, NULL
);
9377 if (TREE_CODE (op1
) == SSA_NAME
)
9378 vr1
= *(get_value_range (op1
));
9379 else if (is_gimple_min_invariant (op1
))
9380 set_value_range_to_value (&vr1
, op1
, NULL
);
9384 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9387 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9391 switch (gimple_assign_rhs_code (stmt
))
9394 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9400 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9408 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9414 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9425 if (op
== NULL_TREE
)
9428 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9429 update_stmt (gsi_stmt (*gsi
));
9433 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9434 a known value range VR.
9436 If there is one and only one value which will satisfy the
9437 conditional, then return that value. Else return NULL.
9439 If signed overflow must be undefined for the value to satisfy
9440 the conditional, then set *STRICT_OVERFLOW_P to true. */
9443 test_for_singularity (enum tree_code cond_code
, tree op0
,
9444 tree op1
, value_range
*vr
)
9449 /* Extract minimum/maximum values which satisfy the conditional as it was
9451 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9453 /* This should not be negative infinity; there is no overflow
9455 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9458 if (cond_code
== LT_EXPR
)
9460 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9461 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9463 TREE_NO_WARNING (max
) = 1;
9466 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9468 /* This should not be positive infinity; there is no overflow
9470 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9473 if (cond_code
== GT_EXPR
)
9475 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9476 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9478 TREE_NO_WARNING (min
) = 1;
9482 /* Now refine the minimum and maximum values using any
9483 value range information we have for op0. */
9486 if (compare_values (vr
->min
, min
) == 1)
9488 if (compare_values (vr
->max
, max
) == -1)
9491 /* If the new min/max values have converged to a single value,
9492 then there is only one value which can satisfy the condition,
9493 return that value. */
9494 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9500 /* Return whether the value range *VR fits in an integer type specified
9501 by PRECISION and UNSIGNED_P. */
9504 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
9507 unsigned src_precision
;
9511 /* We can only handle integral and pointer types. */
9512 src_type
= TREE_TYPE (vr
->min
);
9513 if (!INTEGRAL_TYPE_P (src_type
)
9514 && !POINTER_TYPE_P (src_type
))
9517 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9518 and so is an identity transform. */
9519 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9520 src_sgn
= TYPE_SIGN (src_type
);
9521 if ((src_precision
< dest_precision
9522 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9523 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9526 /* Now we can only handle ranges with constant bounds. */
9527 if (vr
->type
!= VR_RANGE
9528 || TREE_CODE (vr
->min
) != INTEGER_CST
9529 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9532 /* For sign changes, the MSB of the wide_int has to be clear.
9533 An unsigned value with its MSB set cannot be represented by
9534 a signed wide_int, while a negative value cannot be represented
9535 by an unsigned wide_int. */
9536 if (src_sgn
!= dest_sgn
9537 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9540 /* Then we can perform the conversion on both ends and compare
9541 the result for equality. */
9542 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9543 if (tem
!= wi::to_widest (vr
->min
))
9545 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9546 if (tem
!= wi::to_widest (vr
->max
))
9552 /* Simplify a conditional using a relational operator to an equality
9553 test if the range information indicates only one value can satisfy
9554 the original conditional. */
9557 simplify_cond_using_ranges_1 (gcond
*stmt
)
9559 tree op0
= gimple_cond_lhs (stmt
);
9560 tree op1
= gimple_cond_rhs (stmt
);
9561 enum tree_code cond_code
= gimple_cond_code (stmt
);
9563 if (cond_code
!= NE_EXPR
9564 && cond_code
!= EQ_EXPR
9565 && TREE_CODE (op0
) == SSA_NAME
9566 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9567 && is_gimple_min_invariant (op1
))
9569 value_range
*vr
= get_value_range (op0
);
9571 /* If we have range information for OP0, then we might be
9572 able to simplify this conditional. */
9573 if (vr
->type
== VR_RANGE
)
9575 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9580 fprintf (dump_file
, "Simplified relational ");
9581 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9582 fprintf (dump_file
, " into ");
9585 gimple_cond_set_code (stmt
, EQ_EXPR
);
9586 gimple_cond_set_lhs (stmt
, op0
);
9587 gimple_cond_set_rhs (stmt
, new_tree
);
9593 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9594 fprintf (dump_file
, "\n");
9600 /* Try again after inverting the condition. We only deal
9601 with integral types here, so no need to worry about
9602 issues with inverting FP comparisons. */
9603 new_tree
= test_for_singularity
9604 (invert_tree_comparison (cond_code
, false),
9610 fprintf (dump_file
, "Simplified relational ");
9611 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9612 fprintf (dump_file
, " into ");
9615 gimple_cond_set_code (stmt
, NE_EXPR
);
9616 gimple_cond_set_lhs (stmt
, op0
);
9617 gimple_cond_set_rhs (stmt
, new_tree
);
9623 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9624 fprintf (dump_file
, "\n");
9634 /* STMT is a conditional at the end of a basic block.
9636 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
9637 was set via a type conversion, try to replace the SSA_NAME with the RHS
9638 of the type conversion. Doing so makes the conversion dead which helps
9639 subsequent passes. */
9642 simplify_cond_using_ranges_2 (gcond
*stmt
)
9644 tree op0
= gimple_cond_lhs (stmt
);
9645 tree op1
= gimple_cond_rhs (stmt
);
9647 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9648 see if OP0 was set by a type conversion where the source of
9649 the conversion is another SSA_NAME with a range that fits
9650 into the range of OP0's type.
9652 If so, the conversion is redundant as the earlier SSA_NAME can be
9653 used for the comparison directly if we just massage the constant in the
9655 if (TREE_CODE (op0
) == SSA_NAME
9656 && TREE_CODE (op1
) == INTEGER_CST
)
9658 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
9661 if (!is_gimple_assign (def_stmt
)
9662 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9665 innerop
= gimple_assign_rhs1 (def_stmt
);
9667 if (TREE_CODE (innerop
) == SSA_NAME
9668 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
9669 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
9670 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
9672 value_range
*vr
= get_value_range (innerop
);
9674 if (range_int_cst_p (vr
)
9675 && range_fits_type_p (vr
,
9676 TYPE_PRECISION (TREE_TYPE (op0
)),
9677 TYPE_SIGN (TREE_TYPE (op0
)))
9678 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
9680 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9681 gimple_cond_set_lhs (stmt
, innerop
);
9682 gimple_cond_set_rhs (stmt
, newconst
);
9684 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9686 fprintf (dump_file
, "Folded into: ");
9687 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
9688 fprintf (dump_file
, "\n");
9695 /* Simplify a switch statement using the value range of the switch
9699 simplify_switch_using_ranges (gswitch
*stmt
)
9701 tree op
= gimple_switch_index (stmt
);
9702 value_range
*vr
= NULL
;
9706 size_t i
= 0, j
= 0, n
, n2
;
9709 size_t k
= 1, l
= 0;
9711 if (TREE_CODE (op
) == SSA_NAME
)
9713 vr
= get_value_range (op
);
9715 /* We can only handle integer ranges. */
9716 if ((vr
->type
!= VR_RANGE
9717 && vr
->type
!= VR_ANTI_RANGE
)
9718 || symbolic_range_p (vr
))
9721 /* Find case label for min/max of the value range. */
9722 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9724 else if (TREE_CODE (op
) == INTEGER_CST
)
9726 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9740 n
= gimple_switch_num_labels (stmt
);
9742 /* We can truncate the case label ranges that partially overlap with OP's
9744 size_t min_idx
= 1, max_idx
= 0;
9746 find_case_label_range (stmt
, vr
->min
, vr
->max
, &min_idx
, &max_idx
);
9747 if (min_idx
<= max_idx
)
9749 tree min_label
= gimple_switch_label (stmt
, min_idx
);
9750 tree max_label
= gimple_switch_label (stmt
, max_idx
);
9752 /* Avoid changing the type of the case labels when truncating. */
9753 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
9754 tree vr_min
= fold_convert (case_label_type
, vr
->min
);
9755 tree vr_max
= fold_convert (case_label_type
, vr
->max
);
9757 if (vr
->type
== VR_RANGE
)
9759 /* If OP's value range is [2,8] and the low label range is
9760 0 ... 3, truncate the label's range to 2 .. 3. */
9761 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9762 && CASE_HIGH (min_label
) != NULL_TREE
9763 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9764 CASE_LOW (min_label
) = vr_min
;
9766 /* If OP's value range is [2,8] and the high label range is
9767 7 ... 10, truncate the label's range to 7 .. 8. */
9768 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9769 && CASE_HIGH (max_label
) != NULL_TREE
9770 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9771 CASE_HIGH (max_label
) = vr_max
;
9773 else if (vr
->type
== VR_ANTI_RANGE
)
9775 tree one_cst
= build_one_cst (case_label_type
);
9777 if (min_label
== max_label
)
9779 /* If OP's value range is ~[7,8] and the label's range is
9780 7 ... 10, truncate the label's range to 9 ... 10. */
9781 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
9782 && CASE_HIGH (min_label
) != NULL_TREE
9783 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
9784 CASE_LOW (min_label
)
9785 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9787 /* If OP's value range is ~[7,8] and the label's range is
9788 5 ... 8, truncate the label's range to 5 ... 6. */
9789 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9790 && CASE_HIGH (min_label
) != NULL_TREE
9791 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
9792 CASE_HIGH (min_label
)
9793 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9797 /* If OP's value range is ~[2,8] and the low label range is
9798 0 ... 3, truncate the label's range to 0 ... 1. */
9799 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9800 && CASE_HIGH (min_label
) != NULL_TREE
9801 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9802 CASE_HIGH (min_label
)
9803 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9805 /* If OP's value range is ~[2,8] and the high label range is
9806 7 ... 10, truncate the label's range to 9 ... 10. */
9807 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9808 && CASE_HIGH (max_label
) != NULL_TREE
9809 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9810 CASE_LOW (max_label
)
9811 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9815 /* Canonicalize singleton case ranges. */
9816 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
9817 CASE_HIGH (min_label
) = NULL_TREE
;
9818 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
9819 CASE_HIGH (max_label
) = NULL_TREE
;
9822 /* We can also eliminate case labels that lie completely outside OP's value
9825 /* Bail out if this is just all edges taken. */
9831 /* Build a new vector of taken case labels. */
9832 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9835 /* Add the default edge, if necessary. */
9837 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9839 for (; i
<= j
; ++i
, ++n2
)
9840 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9842 for (; k
<= l
; ++k
, ++n2
)
9843 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9845 /* Mark needed edges. */
9846 for (i
= 0; i
< n2
; ++i
)
9848 e
= find_edge (gimple_bb (stmt
),
9849 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9850 e
->aux
= (void *)-1;
9853 /* Queue not needed edges for later removal. */
9854 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9856 if (e
->aux
== (void *)-1)
9862 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9864 fprintf (dump_file
, "removing unreachable case label\n");
9866 to_remove_edges
.safe_push (e
);
9867 e
->flags
&= ~EDGE_EXECUTABLE
;
9870 /* And queue an update for the stmt. */
9873 to_update_switch_stmts
.safe_push (su
);
9877 /* Simplify an integral conversion from an SSA name in STMT. */
9880 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9882 tree innerop
, middleop
, finaltype
;
9884 signop inner_sgn
, middle_sgn
, final_sgn
;
9885 unsigned inner_prec
, middle_prec
, final_prec
;
9886 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9888 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9889 if (!INTEGRAL_TYPE_P (finaltype
))
9891 middleop
= gimple_assign_rhs1 (stmt
);
9892 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9893 if (!is_gimple_assign (def_stmt
)
9894 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9896 innerop
= gimple_assign_rhs1 (def_stmt
);
9897 if (TREE_CODE (innerop
) != SSA_NAME
9898 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9901 /* Get the value-range of the inner operand. Use get_range_info in
9902 case innerop was created during substitute-and-fold. */
9903 wide_int imin
, imax
;
9904 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
))
9905 || get_range_info (innerop
, &imin
, &imax
) != VR_RANGE
)
9907 innermin
= widest_int::from (imin
, TYPE_SIGN (TREE_TYPE (innerop
)));
9908 innermax
= widest_int::from (imax
, TYPE_SIGN (TREE_TYPE (innerop
)));
9910 /* Simulate the conversion chain to check if the result is equal if
9911 the middle conversion is removed. */
9912 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9913 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9914 final_prec
= TYPE_PRECISION (finaltype
);
9916 /* If the first conversion is not injective, the second must not
9918 if (wi::gtu_p (innermax
- innermin
,
9919 wi::mask
<widest_int
> (middle_prec
, false))
9920 && middle_prec
< final_prec
)
9922 /* We also want a medium value so that we can track the effect that
9923 narrowing conversions with sign change have. */
9924 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9925 if (inner_sgn
== UNSIGNED
)
9926 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9929 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9930 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9931 innermed
= innermin
;
9933 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9934 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9935 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9936 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9938 /* Require that the final conversion applied to both the original
9939 and the intermediate range produces the same result. */
9940 final_sgn
= TYPE_SIGN (finaltype
);
9941 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9942 != wi::ext (innermin
, final_prec
, final_sgn
)
9943 || wi::ext (middlemed
, final_prec
, final_sgn
)
9944 != wi::ext (innermed
, final_prec
, final_sgn
)
9945 || wi::ext (middlemax
, final_prec
, final_sgn
)
9946 != wi::ext (innermax
, final_prec
, final_sgn
))
9949 gimple_assign_set_rhs1 (stmt
, innerop
);
9950 fold_stmt (gsi
, follow_single_use_edges
);
9954 /* Simplify a conversion from integral SSA name to float in STMT. */
9957 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
9960 tree rhs1
= gimple_assign_rhs1 (stmt
);
9961 value_range
*vr
= get_value_range (rhs1
);
9962 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9967 /* We can only handle constant ranges. */
9968 if (vr
->type
!= VR_RANGE
9969 || TREE_CODE (vr
->min
) != INTEGER_CST
9970 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9973 /* First check if we can use a signed type in place of an unsigned. */
9974 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9975 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9976 != CODE_FOR_nothing
)
9977 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9978 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9979 /* If we can do the conversion in the current input mode do nothing. */
9980 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9981 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9983 /* Otherwise search for a mode we can use, starting from the narrowest
9984 integer mode available. */
9987 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9990 /* If we cannot do a signed conversion to float from mode
9991 or if the value-range does not fit in the signed type
9992 try with a wider mode. */
9993 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9994 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9997 mode
= GET_MODE_WIDER_MODE (mode
);
9998 /* But do not widen the input. Instead leave that to the
9999 optabs expansion code. */
10000 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
10003 while (mode
!= VOIDmode
);
10004 if (mode
== VOIDmode
)
10008 /* It works, insert a truncation or sign-change before the
10009 float conversion. */
10010 tem
= make_ssa_name (build_nonstandard_integer_type
10011 (GET_MODE_PRECISION (mode
), 0));
10012 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
10013 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
10014 gimple_assign_set_rhs1 (stmt
, tem
);
10015 fold_stmt (gsi
, follow_single_use_edges
);
10020 /* Simplify an internal fn call using ranges if possible. */
10023 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
10025 enum tree_code subcode
;
10026 bool is_ubsan
= false;
10028 switch (gimple_call_internal_fn (stmt
))
10030 case IFN_UBSAN_CHECK_ADD
:
10031 subcode
= PLUS_EXPR
;
10034 case IFN_UBSAN_CHECK_SUB
:
10035 subcode
= MINUS_EXPR
;
10038 case IFN_UBSAN_CHECK_MUL
:
10039 subcode
= MULT_EXPR
;
10042 case IFN_ADD_OVERFLOW
:
10043 subcode
= PLUS_EXPR
;
10045 case IFN_SUB_OVERFLOW
:
10046 subcode
= MINUS_EXPR
;
10048 case IFN_MUL_OVERFLOW
:
10049 subcode
= MULT_EXPR
;
10055 tree op0
= gimple_call_arg (stmt
, 0);
10056 tree op1
= gimple_call_arg (stmt
, 1);
10060 type
= TREE_TYPE (op0
);
10061 if (VECTOR_TYPE_P (type
))
10064 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
10067 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
10068 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
10069 || (is_ubsan
&& ovf
))
10073 location_t loc
= gimple_location (stmt
);
10075 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
10078 int prec
= TYPE_PRECISION (type
);
10081 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
10082 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
10083 utype
= build_nonstandard_integer_type (prec
, 1);
10084 if (TREE_CODE (op0
) == INTEGER_CST
)
10085 op0
= fold_convert (utype
, op0
);
10086 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
10088 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
10089 gimple_set_location (g
, loc
);
10090 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10091 op0
= gimple_assign_lhs (g
);
10093 if (TREE_CODE (op1
) == INTEGER_CST
)
10094 op1
= fold_convert (utype
, op1
);
10095 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
10097 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
10098 gimple_set_location (g
, loc
);
10099 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10100 op1
= gimple_assign_lhs (g
);
10102 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
10103 gimple_set_location (g
, loc
);
10104 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10107 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
10108 gimple_assign_lhs (g
));
10109 gimple_set_location (g
, loc
);
10110 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10112 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
10113 gimple_assign_lhs (g
),
10114 build_int_cst (type
, ovf
));
10116 gimple_set_location (g
, loc
);
10117 gsi_replace (gsi
, g
, false);
10121 /* Return true if VAR is a two-valued variable. Set a and b with the
10122 two-values when it is true. Return false otherwise. */
10125 two_valued_val_range_p (tree var
, tree
*a
, tree
*b
)
10127 value_range
*vr
= get_value_range (var
);
10128 if ((vr
->type
!= VR_RANGE
10129 && vr
->type
!= VR_ANTI_RANGE
)
10130 || TREE_CODE (vr
->min
) != INTEGER_CST
10131 || TREE_CODE (vr
->max
) != INTEGER_CST
)
10134 if (vr
->type
== VR_RANGE
10135 && wi::sub (vr
->max
, vr
->min
) == 1)
10142 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10143 if (vr
->type
== VR_ANTI_RANGE
10144 && wi::sub (vr
->min
, vrp_val_min (TREE_TYPE (var
))) == 1
10145 && wi::sub (vrp_val_max (TREE_TYPE (var
)), vr
->max
) == 1)
10147 *a
= vrp_val_min (TREE_TYPE (var
));
10148 *b
= vrp_val_max (TREE_TYPE (var
));
10155 /* Simplify STMT using ranges if possible. */
10158 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
10160 gimple
*stmt
= gsi_stmt (*gsi
);
10161 if (is_gimple_assign (stmt
))
10163 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
10164 tree rhs1
= gimple_assign_rhs1 (stmt
);
10165 tree rhs2
= gimple_assign_rhs2 (stmt
);
10166 tree lhs
= gimple_assign_lhs (stmt
);
10167 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
10168 use_operand_p use_p
;
10172 LHS = CST BINOP VAR
10173 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10175 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10178 LHS = VAR BINOP CST
10179 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10181 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10183 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
10184 && INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10185 && ((TREE_CODE (rhs1
) == INTEGER_CST
10186 && TREE_CODE (rhs2
) == SSA_NAME
)
10187 || (TREE_CODE (rhs2
) == INTEGER_CST
10188 && TREE_CODE (rhs1
) == SSA_NAME
))
10189 && single_imm_use (lhs
, &use_p
, &use_stmt
)
10190 && gimple_code (use_stmt
) == GIMPLE_COND
)
10193 tree new_rhs1
= NULL_TREE
;
10194 tree new_rhs2
= NULL_TREE
;
10195 tree cmp_var
= NULL_TREE
;
10197 if (TREE_CODE (rhs2
) == SSA_NAME
10198 && two_valued_val_range_p (rhs2
, &val1
, &val2
))
10200 /* Optimize RHS1 OP [VAL1, VAL2]. */
10201 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
10202 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
10205 else if (TREE_CODE (rhs1
) == SSA_NAME
10206 && two_valued_val_range_p (rhs1
, &val1
, &val2
))
10208 /* Optimize [VAL1, VAL2] OP RHS2. */
10209 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
10210 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
10214 /* If we could not find two-vals or the optimzation is invalid as
10215 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10216 if (new_rhs1
&& new_rhs2
)
10218 tree cond
= build2 (EQ_EXPR
, boolean_type_node
, cmp_var
, val1
);
10219 gimple_assign_set_rhs_with_ops (gsi
,
10223 update_stmt (gsi_stmt (*gsi
));
10224 fold_stmt (gsi
, follow_single_use_edges
);
10233 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10234 if the RHS is zero or one, and the LHS are known to be boolean
10236 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10237 return simplify_truth_ops_using_ranges (gsi
, stmt
);
10240 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10241 and BIT_AND_EXPR respectively if the first operand is greater
10242 than zero and the second operand is an exact power of two.
10243 Also optimize TRUNC_MOD_EXPR away if the second operand is
10244 constant and the first operand already has the right value
10246 case TRUNC_DIV_EXPR
:
10247 case TRUNC_MOD_EXPR
:
10248 if ((TREE_CODE (rhs1
) == SSA_NAME
10249 || TREE_CODE (rhs1
) == INTEGER_CST
)
10250 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10251 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
10254 /* Transform ABS (X) into X or -X as appropriate. */
10256 if (TREE_CODE (rhs1
) == SSA_NAME
10257 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10258 return simplify_abs_using_ranges (gsi
, stmt
);
10263 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10264 if all the bits being cleared are already cleared or
10265 all the bits being set are already set. */
10266 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10267 return simplify_bit_ops_using_ranges (gsi
, stmt
);
10271 if (TREE_CODE (rhs1
) == SSA_NAME
10272 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10273 return simplify_conversion_using_ranges (gsi
, stmt
);
10277 if (TREE_CODE (rhs1
) == SSA_NAME
10278 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10279 return simplify_float_conversion_using_ranges (gsi
, stmt
);
10284 return simplify_min_or_max_using_ranges (gsi
, stmt
);
10290 else if (gimple_code (stmt
) == GIMPLE_COND
)
10291 return simplify_cond_using_ranges_1 (as_a
<gcond
*> (stmt
));
10292 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
10293 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
10294 else if (is_gimple_call (stmt
)
10295 && gimple_call_internal_p (stmt
))
10296 return simplify_internal_call_using_ranges (gsi
, stmt
);
10301 /* If the statement pointed by SI has a predicate whose value can be
10302 computed using the value range information computed by VRP, compute
10303 its value and return true. Otherwise, return false. */
10306 fold_predicate_in (gimple_stmt_iterator
*si
)
10308 bool assignment_p
= false;
10310 gimple
*stmt
= gsi_stmt (*si
);
10312 if (is_gimple_assign (stmt
)
10313 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
10315 assignment_p
= true;
10316 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
10317 gimple_assign_rhs1 (stmt
),
10318 gimple_assign_rhs2 (stmt
),
10321 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10322 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10323 gimple_cond_lhs (cond_stmt
),
10324 gimple_cond_rhs (cond_stmt
),
10332 val
= fold_convert (gimple_expr_type (stmt
), val
);
10336 fprintf (dump_file
, "Folding predicate ");
10337 print_gimple_expr (dump_file
, stmt
, 0, 0);
10338 fprintf (dump_file
, " to ");
10339 print_generic_expr (dump_file
, val
, 0);
10340 fprintf (dump_file
, "\n");
10343 if (is_gimple_assign (stmt
))
10344 gimple_assign_set_rhs_from_tree (si
, val
);
10347 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
10348 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
10349 if (integer_zerop (val
))
10350 gimple_cond_make_false (cond_stmt
);
10351 else if (integer_onep (val
))
10352 gimple_cond_make_true (cond_stmt
);
10354 gcc_unreachable ();
10363 /* Callback for substitute_and_fold folding the stmt at *SI. */
10366 vrp_fold_stmt (gimple_stmt_iterator
*si
)
10368 if (fold_predicate_in (si
))
10371 return simplify_stmt_using_ranges (si
);
10374 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
10375 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
10376 BB. If no such ASSERT_EXPR is found, return OP. */
10379 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
10381 imm_use_iterator imm_iter
;
10383 use_operand_p use_p
;
10385 if (TREE_CODE (op
) == SSA_NAME
)
10387 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
10389 use_stmt
= USE_STMT (use_p
);
10390 if (use_stmt
!= stmt
10391 && gimple_assign_single_p (use_stmt
)
10392 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
10393 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
10394 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
10395 return gimple_assign_lhs (use_stmt
);
10401 /* A trivial wrapper so that we can present the generic jump threading
10402 code with a simple API for simplifying statements. STMT is the
10403 statement we want to simplify, WITHIN_STMT provides the location
10404 for any overflow warnings. */
10407 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
10408 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
,
10411 /* First see if the conditional is in the hash table. */
10412 tree cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
10413 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
10416 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10418 tree op0
= gimple_cond_lhs (cond_stmt
);
10419 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
10421 tree op1
= gimple_cond_rhs (cond_stmt
);
10422 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
10424 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10425 op0
, op1
, within_stmt
);
10428 /* We simplify a switch statement by trying to determine which case label
10429 will be taken. If we are successful then we return the corresponding
10430 CASE_LABEL_EXPR. */
10431 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
10433 tree op
= gimple_switch_index (switch_stmt
);
10434 if (TREE_CODE (op
) != SSA_NAME
)
10437 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
10439 value_range
*vr
= get_value_range (op
);
10440 if ((vr
->type
!= VR_RANGE
&& vr
->type
!= VR_ANTI_RANGE
)
10441 || symbolic_range_p (vr
))
10444 if (vr
->type
== VR_RANGE
)
10447 /* Get the range of labels that contain a part of the operand's
10449 find_case_label_range (switch_stmt
, vr
->min
, vr
->max
, &i
, &j
);
10451 /* Is there only one such label? */
10454 tree label
= gimple_switch_label (switch_stmt
, i
);
10456 /* The i'th label will be taken only if the value range of the
10457 operand is entirely within the bounds of this label. */
10458 if (CASE_HIGH (label
) != NULL_TREE
10459 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min
) <= 0
10460 && tree_int_cst_compare (CASE_HIGH (label
), vr
->max
) >= 0)
10461 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min
)
10462 && tree_int_cst_equal (vr
->min
, vr
->max
)))
10466 /* If there are no such labels then the default label will be
10469 return gimple_switch_label (switch_stmt
, 0);
10472 if (vr
->type
== VR_ANTI_RANGE
)
10474 unsigned n
= gimple_switch_num_labels (switch_stmt
);
10475 tree min_label
= gimple_switch_label (switch_stmt
, 1);
10476 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
10478 /* The default label will be taken only if the anti-range of the
10479 operand is entirely outside the bounds of all the (non-default)
10481 if (tree_int_cst_compare (vr
->min
, CASE_LOW (min_label
)) <= 0
10482 && (CASE_HIGH (max_label
) != NULL_TREE
10483 ? tree_int_cst_compare (vr
->max
, CASE_HIGH (max_label
)) >= 0
10484 : tree_int_cst_compare (vr
->max
, CASE_LOW (max_label
)) >= 0))
10485 return gimple_switch_label (switch_stmt
, 0);
10491 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10493 value_range new_vr
= VR_INITIALIZER
;
10494 tree lhs
= gimple_assign_lhs (assign_stmt
);
10496 if (TREE_CODE (lhs
) == SSA_NAME
10497 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10498 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10500 extract_range_from_assignment (&new_vr
, assign_stmt
);
10501 if (range_int_cst_singleton_p (&new_vr
))
10509 class vrp_dom_walker
: public dom_walker
10512 vrp_dom_walker (cdi_direction direction
,
10513 class const_and_copies
*const_and_copies
,
10514 class avail_exprs_stack
*avail_exprs_stack
)
10515 : dom_walker (direction
, true),
10516 m_const_and_copies (const_and_copies
),
10517 m_avail_exprs_stack (avail_exprs_stack
),
10518 m_dummy_cond (NULL
) {}
10520 virtual edge
before_dom_children (basic_block
);
10521 virtual void after_dom_children (basic_block
);
10524 class const_and_copies
*m_const_and_copies
;
10525 class avail_exprs_stack
*m_avail_exprs_stack
;
10527 gcond
*m_dummy_cond
;
10530 /* Called before processing dominator children of BB. We want to look
10531 at ASSERT_EXPRs and record information from them in the appropriate
10534 We could look at other statements here. It's not seen as likely
10535 to significantly increase the jump threads we discover. */
10538 vrp_dom_walker::before_dom_children (basic_block bb
)
10540 gimple_stmt_iterator gsi
;
10542 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
10544 gimple
*stmt
= gsi_stmt (gsi
);
10545 if (gimple_assign_single_p (stmt
)
10546 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
10548 tree rhs1
= gimple_assign_rhs1 (stmt
);
10549 tree cond
= TREE_OPERAND (rhs1
, 1);
10550 tree inverted
= invert_truthvalue (cond
);
10551 vec
<cond_equivalence
> p
;
10553 record_conditions (&p
, cond
, inverted
);
10554 for (unsigned int i
= 0; i
< p
.length (); i
++)
10555 m_avail_exprs_stack
->record_cond (&p
[i
]);
10557 tree lhs
= gimple_assign_lhs (stmt
);
10558 m_const_and_copies
->record_const_or_copy (lhs
,
10559 TREE_OPERAND (rhs1
, 0));
10568 /* Called after processing dominator children of BB. This is where we
10569 actually call into the threader. */
10571 vrp_dom_walker::after_dom_children (basic_block bb
)
10574 m_dummy_cond
= gimple_build_cond (NE_EXPR
,
10575 integer_zero_node
, integer_zero_node
,
10578 thread_outgoing_edges (bb
, m_dummy_cond
, m_const_and_copies
,
10579 m_avail_exprs_stack
,
10580 simplify_stmt_for_jump_threading
);
10582 m_avail_exprs_stack
->pop_to_marker ();
10583 m_const_and_copies
->pop_to_marker ();
10586 /* Blocks which have more than one predecessor and more than
10587 one successor present jump threading opportunities, i.e.,
10588 when the block is reached from a specific predecessor, we
10589 may be able to determine which of the outgoing edges will
10590 be traversed. When this optimization applies, we are able
10591 to avoid conditionals at runtime and we may expose secondary
10592 optimization opportunities.
10594 This routine is effectively a driver for the generic jump
10595 threading code. It basically just presents the generic code
10596 with edges that may be suitable for jump threading.
10598 Unlike DOM, we do not iterate VRP if jump threading was successful.
10599 While iterating may expose new opportunities for VRP, it is expected
10600 those opportunities would be very limited and the compile time cost
10601 to expose those opportunities would be significant.
10603 As jump threading opportunities are discovered, they are registered
10604 for later realization. */
10607 identify_jump_threads (void)
10612 /* Ugh. When substituting values earlier in this pass we can
10613 wipe the dominance information. So rebuild the dominator
10614 information as we need it within the jump threading code. */
10615 calculate_dominance_info (CDI_DOMINATORS
);
10617 /* We do not allow VRP information to be used for jump threading
10618 across a back edge in the CFG. Otherwise it becomes too
10619 difficult to avoid eliminating loop exit tests. Of course
10620 EDGE_DFS_BACK is not accurate at this time so we have to
10622 mark_dfs_back_edges ();
10624 /* Do not thread across edges we are about to remove. Just marking
10625 them as EDGE_IGNORE will do. */
10626 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10627 e
->flags
|= EDGE_IGNORE
;
10629 /* Allocate our unwinder stack to unwind any temporary equivalences
10630 that might be recorded. */
10631 const_and_copies
*equiv_stack
= new const_and_copies ();
10633 hash_table
<expr_elt_hasher
> *avail_exprs
10634 = new hash_table
<expr_elt_hasher
> (1024);
10635 avail_exprs_stack
*avail_exprs_stack
10636 = new class avail_exprs_stack (avail_exprs
);
10638 vrp_dom_walker
walker (CDI_DOMINATORS
, equiv_stack
, avail_exprs_stack
);
10639 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
10641 /* Clear EDGE_IGNORE. */
10642 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10643 e
->flags
&= ~EDGE_IGNORE
;
10645 /* We do not actually update the CFG or SSA graphs at this point as
10646 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10647 handle ASSERT_EXPRs gracefully. */
10648 delete equiv_stack
;
10649 delete avail_exprs
;
10650 delete avail_exprs_stack
;
10653 /* Free VRP lattice. */
10656 vrp_free_lattice ()
10658 /* Free allocated memory. */
10660 free (vr_phi_edge_counts
);
10661 bitmap_obstack_release (&vrp_equiv_obstack
);
10662 vrp_value_range_pool
.release ();
10664 /* So that we can distinguish between VRP data being available
10665 and not available. */
10667 vr_phi_edge_counts
= NULL
;
10670 /* Traverse all the blocks folding conditionals with known ranges. */
10673 vrp_finalize (bool warn_array_bounds_p
)
10677 values_propagated
= true;
10681 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10682 dump_all_value_ranges (dump_file
);
10683 fprintf (dump_file
, "\n");
10686 /* Set value range to non pointer SSA_NAMEs. */
10687 for (i
= 0; i
< num_vr_values
; i
++)
10690 tree name
= ssa_name (i
);
10693 || (vr_value
[i
]->type
== VR_VARYING
)
10694 || (vr_value
[i
]->type
== VR_UNDEFINED
)
10695 || (TREE_CODE (vr_value
[i
]->min
) != INTEGER_CST
)
10696 || (TREE_CODE (vr_value
[i
]->max
) != INTEGER_CST
))
10699 if (POINTER_TYPE_P (TREE_TYPE (name
))
10700 && ((vr_value
[i
]->type
== VR_RANGE
10701 && range_includes_zero_p (vr_value
[i
]->min
,
10702 vr_value
[i
]->max
) == 0)
10703 || (vr_value
[i
]->type
== VR_ANTI_RANGE
10704 && range_includes_zero_p (vr_value
[i
]->min
,
10705 vr_value
[i
]->max
) == 1)))
10706 set_ptr_nonnull (name
);
10707 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
10708 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10712 substitute_and_fold (op_with_constant_singleton_value_range
, vrp_fold_stmt
);
10714 if (warn_array_bounds
&& warn_array_bounds_p
)
10715 check_all_array_refs ();
10718 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10719 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10720 discover more VRs. */
10722 class evrp_dom_walker
: public dom_walker
10726 : dom_walker (CDI_DOMINATORS
), stack (10)
10728 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
10730 ~evrp_dom_walker ()
10732 BITMAP_FREE (need_eh_cleanup
);
10734 virtual edge
before_dom_children (basic_block
);
10735 virtual void after_dom_children (basic_block
);
10736 void push_value_range (tree var
, value_range
*vr
);
10737 value_range
*pop_value_range (tree var
);
10738 value_range
*try_find_new_range (tree
, tree op
, tree_code code
, tree limit
);
10740 /* Cond_stack holds the old VR. */
10741 auto_vec
<std::pair
<tree
, value_range
*> > stack
;
10742 bitmap need_eh_cleanup
;
10743 auto_vec
<gimple
*> stmts_to_fixup
;
10744 auto_vec
<gimple
*> stmts_to_remove
;
10747 /* Find new range for NAME such that (OP CODE LIMIT) is true. */
10750 evrp_dom_walker::try_find_new_range (tree name
,
10751 tree op
, tree_code code
, tree limit
)
10753 value_range vr
= VR_INITIALIZER
;
10754 value_range
*old_vr
= get_value_range (name
);
10756 /* Discover VR when condition is true. */
10757 extract_range_for_var_from_comparison_expr (name
, code
, op
,
10759 /* If we found any usable VR, set the VR to ssa_name and create a
10760 PUSH old value in the stack with the old VR. */
10761 if (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
)
10763 if (old_vr
->type
== vr
.type
10764 && vrp_operand_equal_p (old_vr
->min
, vr
.min
)
10765 && vrp_operand_equal_p (old_vr
->max
, vr
.max
))
10767 value_range
*new_vr
= vrp_value_range_pool
.allocate ();
10774 /* See if there is any new scope is entered with new VR and set that VR to
10775 ssa_name before visiting the statements in the scope. */
10778 evrp_dom_walker::before_dom_children (basic_block bb
)
10780 tree op0
= NULL_TREE
;
10784 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10785 fprintf (dump_file
, "Visiting BB%d\n", bb
->index
);
10787 stack
.safe_push (std::make_pair (NULL_TREE
, (value_range
*)NULL
));
10789 edge pred_e
= NULL
;
10790 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10792 /* Ignore simple backedges from this to allow recording conditions
10793 in loop headers. */
10794 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
10806 gimple
*stmt
= last_stmt (pred_e
->src
);
10808 && gimple_code (stmt
) == GIMPLE_COND
10809 && (op0
= gimple_cond_lhs (stmt
))
10810 && TREE_CODE (op0
) == SSA_NAME
10811 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))
10812 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))))
10814 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10816 fprintf (dump_file
, "Visiting controlling predicate ");
10817 print_gimple_stmt (dump_file
, stmt
, 0, 0);
10819 /* Entering a new scope. Try to see if we can find a VR
10821 tree op1
= gimple_cond_rhs (stmt
);
10822 if (TREE_OVERFLOW_P (op1
))
10823 op1
= drop_tree_overflow (op1
);
10824 tree_code code
= gimple_cond_code (stmt
);
10826 auto_vec
<assert_info
, 8> asserts
;
10827 register_edge_assert_for (op0
, pred_e
, code
, op0
, op1
, asserts
);
10828 if (TREE_CODE (op1
) == SSA_NAME
)
10829 register_edge_assert_for (op1
, pred_e
, code
, op0
, op1
, asserts
);
10831 auto_vec
<std::pair
<tree
, value_range
*>, 8> vrs
;
10832 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
10834 value_range
*vr
= try_find_new_range (asserts
[i
].name
,
10836 asserts
[i
].comp_code
,
10839 vrs
.safe_push (std::make_pair (asserts
[i
].name
, vr
));
10841 /* Push updated ranges only after finding all of them to avoid
10842 ordering issues that can lead to worse ranges. */
10843 for (unsigned i
= 0; i
< vrs
.length (); ++i
)
10844 push_value_range (vrs
[i
].first
, vrs
[i
].second
);
10848 /* Visit PHI stmts and discover any new VRs possible. */
10849 bool has_unvisited_preds
= false;
10850 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10851 if (e
->flags
& EDGE_EXECUTABLE
10852 && !(e
->src
->flags
& BB_VISITED
))
10854 has_unvisited_preds
= true;
10858 for (gphi_iterator gpi
= gsi_start_phis (bb
);
10859 !gsi_end_p (gpi
); gsi_next (&gpi
))
10861 gphi
*phi
= gpi
.phi ();
10862 tree lhs
= PHI_RESULT (phi
);
10863 if (virtual_operand_p (lhs
))
10865 value_range vr_result
= VR_INITIALIZER
;
10866 bool interesting
= stmt_interesting_for_vrp (phi
);
10867 if (interesting
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
10869 fprintf (dump_file
, "Visiting PHI node ");
10870 print_gimple_stmt (dump_file
, phi
, 0, 0);
10872 if (!has_unvisited_preds
10874 extract_range_from_phi_node (phi
, &vr_result
);
10877 set_value_range_to_varying (&vr_result
);
10878 /* When we have an unvisited executable predecessor we can't
10879 use PHI arg ranges which may be still UNDEFINED but have
10880 to use VARYING for them. But we can still resort to
10881 SCEV for loop header PHIs. */
10884 && (l
= loop_containing_stmt (phi
))
10885 && l
->header
== gimple_bb (phi
))
10886 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
10888 update_value_range (lhs
, &vr_result
);
10890 /* Mark PHIs whose lhs we fully propagate for removal. */
10891 tree val
= op_with_constant_singleton_value_range (lhs
);
10892 if (val
&& may_propagate_copy (lhs
, val
))
10894 stmts_to_remove
.safe_push (phi
);
10898 /* Set the SSA with the value range. */
10899 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
10901 if ((vr_result
.type
== VR_RANGE
10902 || vr_result
.type
== VR_ANTI_RANGE
)
10903 && (TREE_CODE (vr_result
.min
) == INTEGER_CST
)
10904 && (TREE_CODE (vr_result
.max
) == INTEGER_CST
))
10905 set_range_info (lhs
,
10906 vr_result
.type
, vr_result
.min
, vr_result
.max
);
10908 else if (POINTER_TYPE_P (TREE_TYPE (lhs
))
10909 && ((vr_result
.type
== VR_RANGE
10910 && range_includes_zero_p (vr_result
.min
,
10911 vr_result
.max
) == 0)
10912 || (vr_result
.type
== VR_ANTI_RANGE
10913 && range_includes_zero_p (vr_result
.min
,
10914 vr_result
.max
) == 1)))
10915 set_ptr_nonnull (lhs
);
10918 edge taken_edge
= NULL
;
10920 /* Visit all other stmts and discover any new VRs possible. */
10921 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
10922 !gsi_end_p (gsi
); gsi_next (&gsi
))
10924 gimple
*stmt
= gsi_stmt (gsi
);
10925 tree output
= NULL_TREE
;
10926 gimple
*old_stmt
= stmt
;
10927 bool was_noreturn
= (is_gimple_call (stmt
)
10928 && gimple_call_noreturn_p (stmt
));
10930 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10932 fprintf (dump_file
, "Visiting stmt ");
10933 print_gimple_stmt (dump_file
, stmt
, 0, 0);
10936 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
10938 vrp_visit_cond_stmt (cond
, &taken_edge
);
10941 if (taken_edge
->flags
& EDGE_TRUE_VALUE
)
10942 gimple_cond_make_true (cond
);
10943 else if (taken_edge
->flags
& EDGE_FALSE_VALUE
)
10944 gimple_cond_make_false (cond
);
10946 gcc_unreachable ();
10947 update_stmt (stmt
);
10950 else if (stmt_interesting_for_vrp (stmt
))
10953 value_range vr
= VR_INITIALIZER
;
10954 extract_range_from_stmt (stmt
, &taken_edge
, &output
, &vr
);
10956 && (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
))
10958 update_value_range (output
, &vr
);
10959 vr
= *get_value_range (output
);
10961 /* Mark stmts whose output we fully propagate for removal. */
10963 if ((val
= op_with_constant_singleton_value_range (output
))
10964 && may_propagate_copy (output
, val
)
10965 && !stmt_could_throw_p (stmt
)
10966 && !gimple_has_side_effects (stmt
))
10968 stmts_to_remove
.safe_push (stmt
);
10972 /* Set the SSA with the value range. */
10973 if (INTEGRAL_TYPE_P (TREE_TYPE (output
)))
10975 if ((vr
.type
== VR_RANGE
10976 || vr
.type
== VR_ANTI_RANGE
)
10977 && (TREE_CODE (vr
.min
) == INTEGER_CST
)
10978 && (TREE_CODE (vr
.max
) == INTEGER_CST
))
10979 set_range_info (output
, vr
.type
, vr
.min
, vr
.max
);
10981 else if (POINTER_TYPE_P (TREE_TYPE (output
))
10982 && ((vr
.type
== VR_RANGE
10983 && range_includes_zero_p (vr
.min
,
10985 || (vr
.type
== VR_ANTI_RANGE
10986 && range_includes_zero_p (vr
.min
,
10988 set_ptr_nonnull (output
);
10991 set_defs_to_varying (stmt
);
10994 set_defs_to_varying (stmt
);
10996 /* See if we can derive a range for any of STMT's operands. */
10999 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
11002 enum tree_code comp_code
;
11004 /* If OP is used in such a way that we can infer a value
11005 range for it, and we don't find a previous assertion for
11006 it, create a new assertion location node for OP. */
11007 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
11009 /* If we are able to infer a nonzero value range for OP,
11010 then walk backwards through the use-def chain to see if OP
11011 was set via a typecast.
11012 If so, then we can also infer a nonzero value range
11013 for the operand of the NOP_EXPR. */
11014 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
11017 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
11018 while (is_gimple_assign (def_stmt
)
11019 && CONVERT_EXPR_CODE_P
11020 (gimple_assign_rhs_code (def_stmt
))
11022 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
11024 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
11026 t
= gimple_assign_rhs1 (def_stmt
);
11027 def_stmt
= SSA_NAME_DEF_STMT (t
);
11029 /* Add VR when (T COMP_CODE value) condition is
11031 value_range
*op_range
11032 = try_find_new_range (t
, t
, comp_code
, value
);
11034 push_value_range (t
, op_range
);
11037 /* Add VR when (OP COMP_CODE value) condition is true. */
11038 value_range
*op_range
= try_find_new_range (op
, op
,
11041 push_value_range (op
, op_range
);
11045 /* Try folding stmts with the VR discovered. */
11047 = replace_uses_in (stmt
, op_with_constant_singleton_value_range
);
11048 if (fold_stmt (&gsi
, follow_single_use_edges
)
11051 stmt
= gsi_stmt (gsi
);
11052 update_stmt (stmt
);
11053 did_replace
= true;
11058 /* If we cleaned up EH information from the statement,
11059 remove EH edges. */
11060 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
11061 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
11063 /* If we turned a not noreturn call into a noreturn one
11064 schedule it for fixup. */
11066 && is_gimple_call (stmt
)
11067 && gimple_call_noreturn_p (stmt
))
11068 stmts_to_fixup
.safe_push (stmt
);
11070 if (gimple_assign_single_p (stmt
))
11072 tree rhs
= gimple_assign_rhs1 (stmt
);
11073 if (TREE_CODE (rhs
) == ADDR_EXPR
)
11074 recompute_tree_invariant_for_addr_expr (rhs
);
11079 /* Visit BB successor PHI nodes and replace PHI args. */
11080 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
11082 for (gphi_iterator gpi
= gsi_start_phis (e
->dest
);
11083 !gsi_end_p (gpi
); gsi_next (&gpi
))
11085 gphi
*phi
= gpi
.phi ();
11086 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
11087 tree arg
= USE_FROM_PTR (use_p
);
11088 if (TREE_CODE (arg
) != SSA_NAME
11089 || virtual_operand_p (arg
))
11091 tree val
= op_with_constant_singleton_value_range (arg
);
11092 if (val
&& may_propagate_copy (arg
, val
))
11093 propagate_value (use_p
, val
);
11097 bb
->flags
|= BB_VISITED
;
11102 /* Restore/pop VRs valid only for BB when we leave BB. */
11105 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED
)
11107 gcc_checking_assert (!stack
.is_empty ());
11108 while (stack
.last ().first
!= NULL_TREE
)
11109 pop_value_range (stack
.last ().first
);
11113 /* Push the Value Range of VAR to the stack and update it with new VR. */
11116 evrp_dom_walker::push_value_range (tree var
, value_range
*vr
)
11118 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
11120 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11122 fprintf (dump_file
, "pushing new range for ");
11123 print_generic_expr (dump_file
, var
, 0);
11124 fprintf (dump_file
, ": ");
11125 dump_value_range (dump_file
, vr
);
11126 fprintf (dump_file
, "\n");
11128 stack
.safe_push (std::make_pair (var
, get_value_range (var
)));
11129 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
11132 /* Pop the Value Range from the vrp_stack and update VAR with it. */
11135 evrp_dom_walker::pop_value_range (tree var
)
11137 value_range
*vr
= stack
.last ().second
;
11138 gcc_checking_assert (var
== stack
.last ().first
);
11139 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11141 fprintf (dump_file
, "popping range for ");
11142 print_generic_expr (dump_file
, var
, 0);
11143 fprintf (dump_file
, ", restoring ");
11144 dump_value_range (dump_file
, vr
);
11145 fprintf (dump_file
, "\n");
11147 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
11153 /* Main entry point for the early vrp pass which is a simplified non-iterative
11154 version of vrp where basic blocks are visited in dominance order. Value
11155 ranges discovered in early vrp will also be used by ipa-vrp. */
11157 static unsigned int
11158 execute_early_vrp ()
11164 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
11165 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
11166 scev_initialize ();
11167 calculate_dominance_info (CDI_DOMINATORS
);
11168 FOR_EACH_BB_FN (bb
, cfun
)
11170 bb
->flags
&= ~BB_VISITED
;
11171 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
11172 e
->flags
|= EDGE_EXECUTABLE
;
11174 vrp_initialize_lattice ();
11176 /* Walk stmts in dominance order and propagate VRP. */
11177 evrp_dom_walker walker
;
11178 walker
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
11182 fprintf (dump_file
, "\nValue ranges after Early VRP:\n\n");
11183 dump_all_value_ranges (dump_file
);
11184 fprintf (dump_file
, "\n");
11187 /* Remove stmts in reverse order to make debug stmt creation possible. */
11188 while (! walker
.stmts_to_remove
.is_empty ())
11190 gimple
*stmt
= walker
.stmts_to_remove
.pop ();
11191 if (dump_file
&& dump_flags
& TDF_DETAILS
)
11193 fprintf (dump_file
, "Removing dead stmt ");
11194 print_gimple_stmt (dump_file
, stmt
, 0, 0);
11195 fprintf (dump_file
, "\n");
11197 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
11198 if (gimple_code (stmt
) == GIMPLE_PHI
)
11199 remove_phi_node (&gsi
, true);
11202 unlink_stmt_vdef (stmt
);
11203 gsi_remove (&gsi
, true);
11204 release_defs (stmt
);
11208 if (!bitmap_empty_p (walker
.need_eh_cleanup
))
11209 gimple_purge_all_dead_eh_edges (walker
.need_eh_cleanup
);
11211 /* Fixup stmts that became noreturn calls. This may require splitting
11212 blocks and thus isn't possible during the dominator walk. Do this
11213 in reverse order so we don't inadvertedly remove a stmt we want to
11214 fixup by visiting a dominating now noreturn call first. */
11215 while (!walker
.stmts_to_fixup
.is_empty ())
11217 gimple
*stmt
= walker
.stmts_to_fixup
.pop ();
11218 fixup_noreturn_call (stmt
);
11221 vrp_free_lattice ();
11223 loop_optimizer_finalize ();
11228 /* Main entry point to VRP (Value Range Propagation). This pass is
11229 loosely based on J. R. C. Patterson, ``Accurate Static Branch
11230 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
11231 Programming Language Design and Implementation, pp. 67-78, 1995.
11232 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
11234 This is essentially an SSA-CCP pass modified to deal with ranges
11235 instead of constants.
11237 While propagating ranges, we may find that two or more SSA name
11238 have equivalent, though distinct ranges. For instance,
11241 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
11243 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
11247 In the code above, pointer p_5 has range [q_2, q_2], but from the
11248 code we can also determine that p_5 cannot be NULL and, if q_2 had
11249 a non-varying range, p_5's range should also be compatible with it.
11251 These equivalences are created by two expressions: ASSERT_EXPR and
11252 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
11253 result of another assertion, then we can use the fact that p_5 and
11254 p_4 are equivalent when evaluating p_5's range.
11256 Together with value ranges, we also propagate these equivalences
11257 between names so that we can take advantage of information from
11258 multiple ranges when doing final replacement. Note that this
11259 equivalency relation is transitive but not symmetric.
11261 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
11262 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
11263 in contexts where that assertion does not hold (e.g., in line 6).
11265 TODO, the main difference between this pass and Patterson's is that
11266 we do not propagate edge probabilities. We only compute whether
11267 edges can be taken or not. That is, instead of having a spectrum
11268 of jump probabilities between 0 and 1, we only deal with 0, 1 and
11269 DON'T KNOW. In the future, it may be worthwhile to propagate
11270 probabilities to aid branch prediction. */
11272 static unsigned int
11273 execute_vrp (bool warn_array_bounds_p
)
11279 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
11280 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
11281 scev_initialize ();
11283 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11284 Inserting assertions may split edges which will invalidate
11286 insert_range_assertions ();
11288 to_remove_edges
.create (10);
11289 to_update_switch_stmts
.create (5);
11290 threadedge_initialize_values ();
11292 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11293 mark_dfs_back_edges ();
11295 vrp_initialize_lattice ();
11297 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
11298 vrp_finalize (warn_array_bounds_p
);
11300 /* We must identify jump threading opportunities before we release
11301 the datastructures built by VRP. */
11302 identify_jump_threads ();
11304 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
11305 was set by a type conversion can often be rewritten to use the
11306 RHS of the type conversion.
11308 However, doing so inhibits jump threading through the comparison.
11309 So that transformation is not performed until after jump threading
11312 FOR_EACH_BB_FN (bb
, cfun
)
11314 gimple
*last
= last_stmt (bb
);
11315 if (last
&& gimple_code (last
) == GIMPLE_COND
)
11316 simplify_cond_using_ranges_2 (as_a
<gcond
*> (last
));
11319 vrp_free_lattice ();
11321 free_numbers_of_iterations_estimates (cfun
);
11323 /* ASSERT_EXPRs must be removed before finalizing jump threads
11324 as finalizing jump threads calls the CFG cleanup code which
11325 does not properly handle ASSERT_EXPRs. */
11326 remove_range_assertions ();
11328 /* If we exposed any new variables, go ahead and put them into
11329 SSA form now, before we handle jump threading. This simplifies
11330 interactions between rewriting of _DECL nodes into SSA form
11331 and rewriting SSA_NAME nodes into SSA form after block
11332 duplication and CFG manipulation. */
11333 update_ssa (TODO_update_ssa
);
11335 /* We identified all the jump threading opportunities earlier, but could
11336 not transform the CFG at that time. This routine transforms the
11337 CFG and arranges for the dominator tree to be rebuilt if necessary.
11339 Note the SSA graph update will occur during the normal TODO
11340 processing by the pass manager. */
11341 thread_through_all_blocks (false);
11343 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11344 CFG in a broken state and requires a cfg_cleanup run. */
11345 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
11347 /* Update SWITCH_EXPR case label vector. */
11348 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
11351 size_t n
= TREE_VEC_LENGTH (su
->vec
);
11353 gimple_switch_set_num_labels (su
->stmt
, n
);
11354 for (j
= 0; j
< n
; j
++)
11355 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
11356 /* As we may have replaced the default label with a regular one
11357 make sure to make it a real default label again. This ensures
11358 optimal expansion. */
11359 label
= gimple_switch_label (su
->stmt
, 0);
11360 CASE_LOW (label
) = NULL_TREE
;
11361 CASE_HIGH (label
) = NULL_TREE
;
11364 if (to_remove_edges
.length () > 0)
11366 free_dominance_info (CDI_DOMINATORS
);
11367 loops_state_set (LOOPS_NEED_FIXUP
);
11370 to_remove_edges
.release ();
11371 to_update_switch_stmts
.release ();
11372 threadedge_finalize_values ();
11375 loop_optimizer_finalize ();
11381 const pass_data pass_data_vrp
=
11383 GIMPLE_PASS
, /* type */
11385 OPTGROUP_NONE
, /* optinfo_flags */
11386 TV_TREE_VRP
, /* tv_id */
11387 PROP_ssa
, /* properties_required */
11388 0, /* properties_provided */
11389 0, /* properties_destroyed */
11390 0, /* todo_flags_start */
11391 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
11394 class pass_vrp
: public gimple_opt_pass
11397 pass_vrp (gcc::context
*ctxt
)
11398 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
11401 /* opt_pass methods: */
11402 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
11403 void set_pass_param (unsigned int n
, bool param
)
11405 gcc_assert (n
== 0);
11406 warn_array_bounds_p
= param
;
11408 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
11409 virtual unsigned int execute (function
*)
11410 { return execute_vrp (warn_array_bounds_p
); }
11413 bool warn_array_bounds_p
;
11414 }; // class pass_vrp
11416 } // anon namespace
11419 make_pass_vrp (gcc::context
*ctxt
)
11421 return new pass_vrp (ctxt
);
11426 const pass_data pass_data_early_vrp
=
11428 GIMPLE_PASS
, /* type */
11430 OPTGROUP_NONE
, /* optinfo_flags */
11431 TV_TREE_EARLY_VRP
, /* tv_id */
11432 PROP_ssa
, /* properties_required */
11433 0, /* properties_provided */
11434 0, /* properties_destroyed */
11435 0, /* todo_flags_start */
11436 ( TODO_cleanup_cfg
| TODO_update_ssa
| TODO_verify_all
),
11439 class pass_early_vrp
: public gimple_opt_pass
11442 pass_early_vrp (gcc::context
*ctxt
)
11443 : gimple_opt_pass (pass_data_early_vrp
, ctxt
)
11446 /* opt_pass methods: */
11447 opt_pass
* clone () { return new pass_early_vrp (m_ctxt
); }
11448 virtual bool gate (function
*)
11450 return flag_tree_vrp
!= 0;
11452 virtual unsigned int execute (function
*)
11453 { return execute_early_vrp (); }
11455 }; // class pass_vrp
11456 } // anon namespace
11459 make_pass_early_vrp (gcc::context
*ctxt
)
11461 return new pass_early_vrp (ctxt
);