libgo: update to Go 1.15.4 release
[official-gcc.git] / gcc / tree-vrp.c
blobe00c034fee3adc9fdff5e465cad5c91bbca88e7f
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2020 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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "insn-codes.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "flags.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "calls.h"
38 #include "cfganal.h"
39 #include "gimple-fold.h"
40 #include "tree-eh.h"
41 #include "gimple-iterator.h"
42 #include "gimple-walk.h"
43 #include "tree-cfg.h"
44 #include "tree-ssa-loop-manip.h"
45 #include "tree-ssa-loop-niter.h"
46 #include "tree-ssa-loop.h"
47 #include "tree-into-ssa.h"
48 #include "tree-ssa.h"
49 #include "cfgloop.h"
50 #include "tree-scalar-evolution.h"
51 #include "tree-ssa-propagate.h"
52 #include "tree-chrec.h"
53 #include "tree-ssa-threadupdate.h"
54 #include "tree-ssa-scopedtables.h"
55 #include "tree-ssa-threadedge.h"
56 #include "omp-general.h"
57 #include "target.h"
58 #include "case-cfn-macros.h"
59 #include "alloc-pool.h"
60 #include "domwalk.h"
61 #include "tree-cfgcleanup.h"
62 #include "stringpool.h"
63 #include "attribs.h"
64 #include "vr-values.h"
65 #include "builtins.h"
66 #include "range-op.h"
67 #include "value-range-equiv.h"
68 #include "gimple-array-bounds.h"
70 /* Set of SSA names found live during the RPO traversal of the function
71 for still active basic-blocks. */
72 class live_names
74 public:
75 live_names ();
76 ~live_names ();
77 void set (tree, basic_block);
78 void clear (tree, basic_block);
79 void merge (basic_block dest, basic_block src);
80 bool live_on_block_p (tree, basic_block);
81 bool live_on_edge_p (tree, edge);
82 bool block_has_live_names_p (basic_block);
83 void clear_block (basic_block);
85 private:
86 sbitmap *live;
87 unsigned num_blocks;
88 void init_bitmap_if_needed (basic_block);
91 void
92 live_names::init_bitmap_if_needed (basic_block bb)
94 unsigned i = bb->index;
95 if (!live[i])
97 live[i] = sbitmap_alloc (num_ssa_names);
98 bitmap_clear (live[i]);
102 bool
103 live_names::block_has_live_names_p (basic_block bb)
105 unsigned i = bb->index;
106 return live[i] && bitmap_empty_p (live[i]);
109 void
110 live_names::clear_block (basic_block bb)
112 unsigned i = bb->index;
113 if (live[i])
115 sbitmap_free (live[i]);
116 live[i] = NULL;
120 void
121 live_names::merge (basic_block dest, basic_block src)
123 init_bitmap_if_needed (dest);
124 init_bitmap_if_needed (src);
125 bitmap_ior (live[dest->index], live[dest->index], live[src->index]);
128 void
129 live_names::set (tree name, basic_block bb)
131 init_bitmap_if_needed (bb);
132 bitmap_set_bit (live[bb->index], SSA_NAME_VERSION (name));
135 void
136 live_names::clear (tree name, basic_block bb)
138 unsigned i = bb->index;
139 if (live[i])
140 bitmap_clear_bit (live[i], SSA_NAME_VERSION (name));
143 live_names::live_names ()
145 num_blocks = last_basic_block_for_fn (cfun);
146 live = XCNEWVEC (sbitmap, num_blocks);
149 live_names::~live_names ()
151 for (unsigned i = 0; i < num_blocks; ++i)
152 if (live[i])
153 sbitmap_free (live[i]);
154 XDELETEVEC (live);
157 bool
158 live_names::live_on_block_p (tree name, basic_block bb)
160 return (live[bb->index]
161 && bitmap_bit_p (live[bb->index], SSA_NAME_VERSION (name)));
165 /* Location information for ASSERT_EXPRs. Each instance of this
166 structure describes an ASSERT_EXPR for an SSA name. Since a single
167 SSA name may have more than one assertion associated with it, these
168 locations are kept in a linked list attached to the corresponding
169 SSA name. */
170 struct assert_locus
172 /* Basic block where the assertion would be inserted. */
173 basic_block bb;
175 /* Some assertions need to be inserted on an edge (e.g., assertions
176 generated by COND_EXPRs). In those cases, BB will be NULL. */
177 edge e;
179 /* Pointer to the statement that generated this assertion. */
180 gimple_stmt_iterator si;
182 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
183 enum tree_code comp_code;
185 /* Value being compared against. */
186 tree val;
188 /* Expression to compare. */
189 tree expr;
191 /* Next node in the linked list. */
192 assert_locus *next;
195 class vrp_insert
197 public:
198 vrp_insert (struct function *fn) : fun (fn) { }
200 /* Traverse the flowgraph looking for conditional jumps to insert range
201 expressions. These range expressions are meant to provide information
202 to optimizations that need to reason in terms of value ranges. They
203 will not be expanded into RTL. See method implementation comment
204 for example. */
205 void insert_range_assertions ();
207 /* Convert range assertion expressions into the implied copies and
208 copy propagate away the copies. */
209 void remove_range_assertions ();
211 /* Dump all the registered assertions for all the names to FILE. */
212 void dump (FILE *);
214 /* Dump all the registered assertions for NAME to FILE. */
215 void dump (FILE *file, tree name);
217 /* Dump all the registered assertions for NAME to stderr. */
218 void debug (tree name)
220 dump (stderr, name);
223 /* Dump all the registered assertions for all the names to stderr. */
224 void debug ()
226 dump (stderr);
229 private:
230 /* Set of SSA names found live during the RPO traversal of the function
231 for still active basic-blocks. */
232 live_names live;
234 /* Function to work on. */
235 struct function *fun;
237 /* If bit I is present, it means that SSA name N_i has a list of
238 assertions that should be inserted in the IL. */
239 bitmap need_assert_for;
241 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
242 holds a list of ASSERT_LOCUS_T nodes that describe where
243 ASSERT_EXPRs for SSA name N_I should be inserted. */
244 assert_locus **asserts_for;
246 /* Finish found ASSERTS for E and register them at GSI. */
247 void finish_register_edge_assert_for (edge e, gimple_stmt_iterator gsi,
248 vec<assert_info> &asserts);
250 /* Determine whether the outgoing edges of BB should receive an
251 ASSERT_EXPR for each of the operands of BB's LAST statement. The
252 last statement of BB must be a SWITCH_EXPR.
254 If any of the sub-graphs rooted at BB have an interesting use of
255 the predicate operands, an assert location node is added to the
256 list of assertions for the corresponding operands. */
257 void find_switch_asserts (basic_block bb, gswitch *last);
259 /* Do an RPO walk over the function computing SSA name liveness
260 on-the-fly and deciding on assert expressions to insert. */
261 void find_assert_locations ();
263 /* Traverse all the statements in block BB looking for statements that
264 may generate useful assertions for the SSA names in their operand.
265 See method implementation comentary for more information. */
266 void find_assert_locations_in_bb (basic_block bb);
268 /* Determine whether the outgoing edges of BB should receive an
269 ASSERT_EXPR for each of the operands of BB's LAST statement.
270 The last statement of BB must be a COND_EXPR.
272 If any of the sub-graphs rooted at BB have an interesting use of
273 the predicate operands, an assert location node is added to the
274 list of assertions for the corresponding operands. */
275 void find_conditional_asserts (basic_block bb, gcond *last);
277 /* Process all the insertions registered for every name N_i registered
278 in NEED_ASSERT_FOR. The list of assertions to be inserted are
279 found in ASSERTS_FOR[i]. */
280 void process_assert_insertions ();
282 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
283 'EXPR COMP_CODE VAL' at a location that dominates block BB or
284 E->DEST, then register this location as a possible insertion point
285 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
287 BB, E and SI provide the exact insertion point for the new
288 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
289 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
290 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
291 must not be NULL. */
292 void register_new_assert_for (tree name, tree expr,
293 enum tree_code comp_code,
294 tree val, basic_block bb,
295 edge e, gimple_stmt_iterator si);
297 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
298 create a new SSA name N and return the assertion assignment
299 'N = ASSERT_EXPR <V, V OP W>'. */
300 gimple *build_assert_expr_for (tree cond, tree v);
302 /* Create an ASSERT_EXPR for NAME and insert it in the location
303 indicated by LOC. Return true if we made any edge insertions. */
304 bool process_assert_insertions_for (tree name, assert_locus *loc);
306 /* Qsort callback for sorting assert locations. */
307 template <bool stable> static int compare_assert_loc (const void *,
308 const void *);
311 /* Return true if the SSA name NAME is live on the edge E. */
313 bool
314 live_names::live_on_edge_p (tree name, edge e)
316 return live_on_block_p (name, e->dest);
320 /* VR_TYPE describes a range with mininum value *MIN and maximum
321 value *MAX. Restrict the range to the set of values that have
322 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
323 return the new range type.
325 SGN gives the sign of the values described by the range. */
327 enum value_range_kind
328 intersect_range_with_nonzero_bits (enum value_range_kind vr_type,
329 wide_int *min, wide_int *max,
330 const wide_int &nonzero_bits,
331 signop sgn)
333 if (vr_type == VR_ANTI_RANGE)
335 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
336 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
337 to create an inclusive upper bound for A and an inclusive lower
338 bound for B. */
339 wide_int a_max = wi::round_down_for_mask (*min - 1, nonzero_bits);
340 wide_int b_min = wi::round_up_for_mask (*max + 1, nonzero_bits);
342 /* If the calculation of A_MAX wrapped, A is effectively empty
343 and A_MAX is the highest value that satisfies NONZERO_BITS.
344 Likewise if the calculation of B_MIN wrapped, B is effectively
345 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
346 bool a_empty = wi::ge_p (a_max, *min, sgn);
347 bool b_empty = wi::le_p (b_min, *max, sgn);
349 /* If both A and B are empty, there are no valid values. */
350 if (a_empty && b_empty)
351 return VR_UNDEFINED;
353 /* If exactly one of A or B is empty, return a VR_RANGE for the
354 other one. */
355 if (a_empty || b_empty)
357 *min = b_min;
358 *max = a_max;
359 gcc_checking_assert (wi::le_p (*min, *max, sgn));
360 return VR_RANGE;
363 /* Update the VR_ANTI_RANGE bounds. */
364 *min = a_max + 1;
365 *max = b_min - 1;
366 gcc_checking_assert (wi::le_p (*min, *max, sgn));
368 /* Now check whether the excluded range includes any values that
369 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
370 if (wi::round_up_for_mask (*min, nonzero_bits) == b_min)
372 unsigned int precision = min->get_precision ();
373 *min = wi::min_value (precision, sgn);
374 *max = wi::max_value (precision, sgn);
375 vr_type = VR_RANGE;
378 if (vr_type == VR_RANGE)
380 *max = wi::round_down_for_mask (*max, nonzero_bits);
382 /* Check that the range contains at least one valid value. */
383 if (wi::gt_p (*min, *max, sgn))
384 return VR_UNDEFINED;
386 *min = wi::round_up_for_mask (*min, nonzero_bits);
387 gcc_checking_assert (wi::le_p (*min, *max, sgn));
389 return vr_type;
392 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
393 a singleton. */
395 bool
396 range_int_cst_p (const value_range *vr)
398 return (vr->kind () == VR_RANGE && range_has_numeric_bounds_p (vr));
401 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
402 otherwise. We only handle additive operations and set NEG to true if the
403 symbol is negated and INV to the invariant part, if any. */
405 tree
406 get_single_symbol (tree t, bool *neg, tree *inv)
408 bool neg_;
409 tree inv_;
411 *inv = NULL_TREE;
412 *neg = false;
414 if (TREE_CODE (t) == PLUS_EXPR
415 || TREE_CODE (t) == POINTER_PLUS_EXPR
416 || TREE_CODE (t) == MINUS_EXPR)
418 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
420 neg_ = (TREE_CODE (t) == MINUS_EXPR);
421 inv_ = TREE_OPERAND (t, 0);
422 t = TREE_OPERAND (t, 1);
424 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
426 neg_ = false;
427 inv_ = TREE_OPERAND (t, 1);
428 t = TREE_OPERAND (t, 0);
430 else
431 return NULL_TREE;
433 else
435 neg_ = false;
436 inv_ = NULL_TREE;
439 if (TREE_CODE (t) == NEGATE_EXPR)
441 t = TREE_OPERAND (t, 0);
442 neg_ = !neg_;
445 if (TREE_CODE (t) != SSA_NAME)
446 return NULL_TREE;
448 if (inv_ && TREE_OVERFLOW_P (inv_))
449 inv_ = drop_tree_overflow (inv_);
451 *neg = neg_;
452 *inv = inv_;
453 return t;
456 /* The reverse operation: build a symbolic expression with TYPE
457 from symbol SYM, negated according to NEG, and invariant INV. */
459 static tree
460 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
462 const bool pointer_p = POINTER_TYPE_P (type);
463 tree t = sym;
465 if (neg)
466 t = build1 (NEGATE_EXPR, type, t);
468 if (integer_zerop (inv))
469 return t;
471 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
474 /* Return
475 1 if VAL < VAL2
476 0 if !(VAL < VAL2)
477 -2 if those are incomparable. */
479 operand_less_p (tree val, tree val2)
481 /* LT is folded faster than GE and others. Inline the common case. */
482 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
483 return tree_int_cst_lt (val, val2);
484 else if (TREE_CODE (val) == SSA_NAME && TREE_CODE (val2) == SSA_NAME)
485 return val == val2 ? 0 : -2;
486 else
488 int cmp = compare_values (val, val2);
489 if (cmp == -1)
490 return 1;
491 else if (cmp == 0 || cmp == 1)
492 return 0;
493 else
494 return -2;
497 return 0;
500 /* Compare two values VAL1 and VAL2. Return
502 -2 if VAL1 and VAL2 cannot be compared at compile-time,
503 -1 if VAL1 < VAL2,
504 0 if VAL1 == VAL2,
505 +1 if VAL1 > VAL2, and
506 +2 if VAL1 != VAL2
508 This is similar to tree_int_cst_compare but supports pointer values
509 and values that cannot be compared at compile time.
511 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
512 true if the return value is only valid if we assume that signed
513 overflow is undefined. */
516 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
518 if (val1 == val2)
519 return 0;
521 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
522 both integers. */
523 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
524 == POINTER_TYPE_P (TREE_TYPE (val2)));
526 /* Convert the two values into the same type. This is needed because
527 sizetype causes sign extension even for unsigned types. */
528 if (!useless_type_conversion_p (TREE_TYPE (val1), TREE_TYPE (val2)))
529 val2 = fold_convert (TREE_TYPE (val1), val2);
531 const bool overflow_undefined
532 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
533 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
534 tree inv1, inv2;
535 bool neg1, neg2;
536 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
537 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
539 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
540 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
541 if (sym1 && sym2)
543 /* Both values must use the same name with the same sign. */
544 if (sym1 != sym2 || neg1 != neg2)
545 return -2;
547 /* [-]NAME + CST == [-]NAME + CST. */
548 if (inv1 == inv2)
549 return 0;
551 /* If overflow is defined we cannot simplify more. */
552 if (!overflow_undefined)
553 return -2;
555 if (strict_overflow_p != NULL
556 /* Symbolic range building sets TREE_NO_WARNING to declare
557 that overflow doesn't happen. */
558 && (!inv1 || !TREE_NO_WARNING (val1))
559 && (!inv2 || !TREE_NO_WARNING (val2)))
560 *strict_overflow_p = true;
562 if (!inv1)
563 inv1 = build_int_cst (TREE_TYPE (val1), 0);
564 if (!inv2)
565 inv2 = build_int_cst (TREE_TYPE (val2), 0);
567 return wi::cmp (wi::to_wide (inv1), wi::to_wide (inv2),
568 TYPE_SIGN (TREE_TYPE (val1)));
571 const bool cst1 = is_gimple_min_invariant (val1);
572 const bool cst2 = is_gimple_min_invariant (val2);
574 /* If one is of the form '[-]NAME + CST' and the other is constant, then
575 it might be possible to say something depending on the constants. */
576 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
578 if (!overflow_undefined)
579 return -2;
581 if (strict_overflow_p != NULL
582 /* Symbolic range building sets TREE_NO_WARNING to declare
583 that overflow doesn't happen. */
584 && (!sym1 || !TREE_NO_WARNING (val1))
585 && (!sym2 || !TREE_NO_WARNING (val2)))
586 *strict_overflow_p = true;
588 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
589 tree cst = cst1 ? val1 : val2;
590 tree inv = cst1 ? inv2 : inv1;
592 /* Compute the difference between the constants. If it overflows or
593 underflows, this means that we can trivially compare the NAME with
594 it and, consequently, the two values with each other. */
595 wide_int diff = wi::to_wide (cst) - wi::to_wide (inv);
596 if (wi::cmp (0, wi::to_wide (inv), sgn)
597 != wi::cmp (diff, wi::to_wide (cst), sgn))
599 const int res = wi::cmp (wi::to_wide (cst), wi::to_wide (inv), sgn);
600 return cst1 ? res : -res;
603 return -2;
606 /* We cannot say anything more for non-constants. */
607 if (!cst1 || !cst2)
608 return -2;
610 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
612 /* We cannot compare overflowed values. */
613 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
614 return -2;
616 if (TREE_CODE (val1) == INTEGER_CST
617 && TREE_CODE (val2) == INTEGER_CST)
618 return tree_int_cst_compare (val1, val2);
620 if (poly_int_tree_p (val1) && poly_int_tree_p (val2))
622 if (known_eq (wi::to_poly_widest (val1),
623 wi::to_poly_widest (val2)))
624 return 0;
625 if (known_lt (wi::to_poly_widest (val1),
626 wi::to_poly_widest (val2)))
627 return -1;
628 if (known_gt (wi::to_poly_widest (val1),
629 wi::to_poly_widest (val2)))
630 return 1;
633 return -2;
635 else
637 if (TREE_CODE (val1) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
639 /* We cannot compare overflowed values. */
640 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
641 return -2;
643 return tree_int_cst_compare (val1, val2);
646 /* First see if VAL1 and VAL2 are not the same. */
647 if (operand_equal_p (val1, val2, 0))
648 return 0;
650 fold_defer_overflow_warnings ();
652 /* If VAL1 is a lower address than VAL2, return -1. */
653 tree t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val1, val2);
654 if (t && integer_onep (t))
656 fold_undefer_and_ignore_overflow_warnings ();
657 return -1;
660 /* If VAL1 is a higher address than VAL2, return +1. */
661 t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val2, val1);
662 if (t && integer_onep (t))
664 fold_undefer_and_ignore_overflow_warnings ();
665 return 1;
668 /* If VAL1 is different than VAL2, return +2. */
669 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
670 fold_undefer_and_ignore_overflow_warnings ();
671 if (t && integer_onep (t))
672 return 2;
674 return -2;
678 /* Compare values like compare_values_warnv. */
681 compare_values (tree val1, tree val2)
683 bool sop;
684 return compare_values_warnv (val1, val2, &sop);
687 /* If BOUND will include a symbolic bound, adjust it accordingly,
688 otherwise leave it as is.
690 CODE is the original operation that combined the bounds (PLUS_EXPR
691 or MINUS_EXPR).
693 TYPE is the type of the original operation.
695 SYM_OPn is the symbolic for OPn if it has a symbolic.
697 NEG_OPn is TRUE if the OPn was negated. */
699 static void
700 adjust_symbolic_bound (tree &bound, enum tree_code code, tree type,
701 tree sym_op0, tree sym_op1,
702 bool neg_op0, bool neg_op1)
704 bool minus_p = (code == MINUS_EXPR);
705 /* If the result bound is constant, we're done; otherwise, build the
706 symbolic lower bound. */
707 if (sym_op0 == sym_op1)
709 else if (sym_op0)
710 bound = build_symbolic_expr (type, sym_op0,
711 neg_op0, bound);
712 else if (sym_op1)
714 /* We may not negate if that might introduce
715 undefined overflow. */
716 if (!minus_p
717 || neg_op1
718 || TYPE_OVERFLOW_WRAPS (type))
719 bound = build_symbolic_expr (type, sym_op1,
720 neg_op1 ^ minus_p, bound);
721 else
722 bound = NULL_TREE;
726 /* Combine OP1 and OP1, which are two parts of a bound, into one wide
727 int bound according to CODE. CODE is the operation combining the
728 bound (either a PLUS_EXPR or a MINUS_EXPR).
730 TYPE is the type of the combine operation.
732 WI is the wide int to store the result.
734 OVF is -1 if an underflow occurred, +1 if an overflow occurred or 0
735 if over/underflow occurred. */
737 static void
738 combine_bound (enum tree_code code, wide_int &wi, wi::overflow_type &ovf,
739 tree type, tree op0, tree op1)
741 bool minus_p = (code == MINUS_EXPR);
742 const signop sgn = TYPE_SIGN (type);
743 const unsigned int prec = TYPE_PRECISION (type);
745 /* Combine the bounds, if any. */
746 if (op0 && op1)
748 if (minus_p)
749 wi = wi::sub (wi::to_wide (op0), wi::to_wide (op1), sgn, &ovf);
750 else
751 wi = wi::add (wi::to_wide (op0), wi::to_wide (op1), sgn, &ovf);
753 else if (op0)
754 wi = wi::to_wide (op0);
755 else if (op1)
757 if (minus_p)
758 wi = wi::neg (wi::to_wide (op1), &ovf);
759 else
760 wi = wi::to_wide (op1);
762 else
763 wi = wi::shwi (0, prec);
766 /* Given a range in [WMIN, WMAX], adjust it for possible overflow and
767 put the result in VR.
769 TYPE is the type of the range.
771 MIN_OVF and MAX_OVF indicate what type of overflow, if any,
772 occurred while originally calculating WMIN or WMAX. -1 indicates
773 underflow. +1 indicates overflow. 0 indicates neither. */
775 static void
776 set_value_range_with_overflow (value_range_kind &kind, tree &min, tree &max,
777 tree type,
778 const wide_int &wmin, const wide_int &wmax,
779 wi::overflow_type min_ovf,
780 wi::overflow_type max_ovf)
782 const signop sgn = TYPE_SIGN (type);
783 const unsigned int prec = TYPE_PRECISION (type);
785 /* For one bit precision if max < min, then the swapped
786 range covers all values. */
787 if (prec == 1 && wi::lt_p (wmax, wmin, sgn))
789 kind = VR_VARYING;
790 return;
793 if (TYPE_OVERFLOW_WRAPS (type))
795 /* If overflow wraps, truncate the values and adjust the
796 range kind and bounds appropriately. */
797 wide_int tmin = wide_int::from (wmin, prec, sgn);
798 wide_int tmax = wide_int::from (wmax, prec, sgn);
799 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
801 /* If the limits are swapped, we wrapped around and cover
802 the entire range. */
803 if (wi::gt_p (tmin, tmax, sgn))
804 kind = VR_VARYING;
805 else
807 kind = VR_RANGE;
808 /* No overflow or both overflow or underflow. The
809 range kind stays VR_RANGE. */
810 min = wide_int_to_tree (type, tmin);
811 max = wide_int_to_tree (type, tmax);
813 return;
815 else if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
816 || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
818 /* Min underflow or max overflow. The range kind
819 changes to VR_ANTI_RANGE. */
820 bool covers = false;
821 wide_int tem = tmin;
822 tmin = tmax + 1;
823 if (wi::cmp (tmin, tmax, sgn) < 0)
824 covers = true;
825 tmax = tem - 1;
826 if (wi::cmp (tmax, tem, sgn) > 0)
827 covers = true;
828 /* If the anti-range would cover nothing, drop to varying.
829 Likewise if the anti-range bounds are outside of the
830 types values. */
831 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
833 kind = VR_VARYING;
834 return;
836 kind = VR_ANTI_RANGE;
837 min = wide_int_to_tree (type, tmin);
838 max = wide_int_to_tree (type, tmax);
839 return;
841 else
843 /* Other underflow and/or overflow, drop to VR_VARYING. */
844 kind = VR_VARYING;
845 return;
848 else
850 /* If overflow does not wrap, saturate to the types min/max
851 value. */
852 wide_int type_min = wi::min_value (prec, sgn);
853 wide_int type_max = wi::max_value (prec, sgn);
854 kind = VR_RANGE;
855 if (min_ovf == wi::OVF_UNDERFLOW)
856 min = wide_int_to_tree (type, type_min);
857 else if (min_ovf == wi::OVF_OVERFLOW)
858 min = wide_int_to_tree (type, type_max);
859 else
860 min = wide_int_to_tree (type, wmin);
862 if (max_ovf == wi::OVF_UNDERFLOW)
863 max = wide_int_to_tree (type, type_min);
864 else if (max_ovf == wi::OVF_OVERFLOW)
865 max = wide_int_to_tree (type, type_max);
866 else
867 max = wide_int_to_tree (type, wmax);
871 /* Fold two value range's of a POINTER_PLUS_EXPR into VR. */
873 static void
874 extract_range_from_pointer_plus_expr (value_range *vr,
875 enum tree_code code,
876 tree expr_type,
877 const value_range *vr0,
878 const value_range *vr1)
880 gcc_checking_assert (POINTER_TYPE_P (expr_type)
881 && code == POINTER_PLUS_EXPR);
882 /* For pointer types, we are really only interested in asserting
883 whether the expression evaluates to non-NULL.
884 With -fno-delete-null-pointer-checks we need to be more
885 conservative. As some object might reside at address 0,
886 then some offset could be added to it and the same offset
887 subtracted again and the result would be NULL.
888 E.g.
889 static int a[12]; where &a[0] is NULL and
890 ptr = &a[6];
891 ptr -= 6;
892 ptr will be NULL here, even when there is POINTER_PLUS_EXPR
893 where the first range doesn't include zero and the second one
894 doesn't either. As the second operand is sizetype (unsigned),
895 consider all ranges where the MSB could be set as possible
896 subtractions where the result might be NULL. */
897 if ((!range_includes_zero_p (vr0)
898 || !range_includes_zero_p (vr1))
899 && !TYPE_OVERFLOW_WRAPS (expr_type)
900 && (flag_delete_null_pointer_checks
901 || (range_int_cst_p (vr1)
902 && !tree_int_cst_sign_bit (vr1->max ()))))
903 vr->set_nonzero (expr_type);
904 else if (vr0->zero_p () && vr1->zero_p ())
905 vr->set_zero (expr_type);
906 else
907 vr->set_varying (expr_type);
910 /* Extract range information from a PLUS/MINUS_EXPR and store the
911 result in *VR. */
913 static void
914 extract_range_from_plus_minus_expr (value_range *vr,
915 enum tree_code code,
916 tree expr_type,
917 const value_range *vr0_,
918 const value_range *vr1_)
920 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR);
922 value_range vr0 = *vr0_, vr1 = *vr1_;
923 value_range vrtem0, vrtem1;
925 /* Now canonicalize anti-ranges to ranges when they are not symbolic
926 and express ~[] op X as ([]' op X) U ([]'' op X). */
927 if (vr0.kind () == VR_ANTI_RANGE
928 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
930 extract_range_from_plus_minus_expr (vr, code, expr_type, &vrtem0, vr1_);
931 if (!vrtem1.undefined_p ())
933 value_range vrres;
934 extract_range_from_plus_minus_expr (&vrres, code, expr_type,
935 &vrtem1, vr1_);
936 vr->union_ (&vrres);
938 return;
940 /* Likewise for X op ~[]. */
941 if (vr1.kind () == VR_ANTI_RANGE
942 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
944 extract_range_from_plus_minus_expr (vr, code, expr_type, vr0_, &vrtem0);
945 if (!vrtem1.undefined_p ())
947 value_range vrres;
948 extract_range_from_plus_minus_expr (&vrres, code, expr_type,
949 vr0_, &vrtem1);
950 vr->union_ (&vrres);
952 return;
955 value_range_kind kind;
956 value_range_kind vr0_kind = vr0.kind (), vr1_kind = vr1.kind ();
957 tree vr0_min = vr0.min (), vr0_max = vr0.max ();
958 tree vr1_min = vr1.min (), vr1_max = vr1.max ();
959 tree min = NULL_TREE, max = NULL_TREE;
961 /* This will normalize things such that calculating
962 [0,0] - VR_VARYING is not dropped to varying, but is
963 calculated as [MIN+1, MAX]. */
964 if (vr0.varying_p ())
966 vr0_kind = VR_RANGE;
967 vr0_min = vrp_val_min (expr_type);
968 vr0_max = vrp_val_max (expr_type);
970 if (vr1.varying_p ())
972 vr1_kind = VR_RANGE;
973 vr1_min = vrp_val_min (expr_type);
974 vr1_max = vrp_val_max (expr_type);
977 const bool minus_p = (code == MINUS_EXPR);
978 tree min_op0 = vr0_min;
979 tree min_op1 = minus_p ? vr1_max : vr1_min;
980 tree max_op0 = vr0_max;
981 tree max_op1 = minus_p ? vr1_min : vr1_max;
982 tree sym_min_op0 = NULL_TREE;
983 tree sym_min_op1 = NULL_TREE;
984 tree sym_max_op0 = NULL_TREE;
985 tree sym_max_op1 = NULL_TREE;
986 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
988 neg_min_op0 = neg_min_op1 = neg_max_op0 = neg_max_op1 = false;
990 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
991 single-symbolic ranges, try to compute the precise resulting range,
992 but only if we know that this resulting range will also be constant
993 or single-symbolic. */
994 if (vr0_kind == VR_RANGE && vr1_kind == VR_RANGE
995 && (TREE_CODE (min_op0) == INTEGER_CST
996 || (sym_min_op0
997 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
998 && (TREE_CODE (min_op1) == INTEGER_CST
999 || (sym_min_op1
1000 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
1001 && (!(sym_min_op0 && sym_min_op1)
1002 || (sym_min_op0 == sym_min_op1
1003 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
1004 && (TREE_CODE (max_op0) == INTEGER_CST
1005 || (sym_max_op0
1006 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
1007 && (TREE_CODE (max_op1) == INTEGER_CST
1008 || (sym_max_op1
1009 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
1010 && (!(sym_max_op0 && sym_max_op1)
1011 || (sym_max_op0 == sym_max_op1
1012 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
1014 wide_int wmin, wmax;
1015 wi::overflow_type min_ovf = wi::OVF_NONE;
1016 wi::overflow_type max_ovf = wi::OVF_NONE;
1018 /* Build the bounds. */
1019 combine_bound (code, wmin, min_ovf, expr_type, min_op0, min_op1);
1020 combine_bound (code, wmax, max_ovf, expr_type, max_op0, max_op1);
1022 /* If the resulting range will be symbolic, we need to eliminate any
1023 explicit or implicit overflow introduced in the above computation
1024 because compare_values could make an incorrect use of it. That's
1025 why we require one of the ranges to be a singleton. */
1026 if ((sym_min_op0 != sym_min_op1 || sym_max_op0 != sym_max_op1)
1027 && ((bool)min_ovf || (bool)max_ovf
1028 || (min_op0 != max_op0 && min_op1 != max_op1)))
1030 vr->set_varying (expr_type);
1031 return;
1034 /* Adjust the range for possible overflow. */
1035 set_value_range_with_overflow (kind, min, max, expr_type,
1036 wmin, wmax, min_ovf, max_ovf);
1037 if (kind == VR_VARYING)
1039 vr->set_varying (expr_type);
1040 return;
1043 /* Build the symbolic bounds if needed. */
1044 adjust_symbolic_bound (min, code, expr_type,
1045 sym_min_op0, sym_min_op1,
1046 neg_min_op0, neg_min_op1);
1047 adjust_symbolic_bound (max, code, expr_type,
1048 sym_max_op0, sym_max_op1,
1049 neg_max_op0, neg_max_op1);
1051 else
1053 /* For other cases, for example if we have a PLUS_EXPR with two
1054 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1055 to compute a precise range for such a case.
1056 ??? General even mixed range kind operations can be expressed
1057 by for example transforming ~[3, 5] + [1, 2] to range-only
1058 operations and a union primitive:
1059 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1060 [-INF+1, 4] U [6, +INF(OVF)]
1061 though usually the union is not exactly representable with
1062 a single range or anti-range as the above is
1063 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1064 but one could use a scheme similar to equivalences for this. */
1065 vr->set_varying (expr_type);
1066 return;
1069 /* If either MIN or MAX overflowed, then set the resulting range to
1070 VARYING. */
1071 if (min == NULL_TREE
1072 || TREE_OVERFLOW_P (min)
1073 || max == NULL_TREE
1074 || TREE_OVERFLOW_P (max))
1076 vr->set_varying (expr_type);
1077 return;
1080 int cmp = compare_values (min, max);
1081 if (cmp == -2 || cmp == 1)
1083 /* If the new range has its limits swapped around (MIN > MAX),
1084 then the operation caused one of them to wrap around, mark
1085 the new range VARYING. */
1086 vr->set_varying (expr_type);
1088 else
1089 vr->set (min, max, kind);
1092 /* Return the range-ops handler for CODE and EXPR_TYPE. If no
1093 suitable operator is found, return NULL and set VR to VARYING. */
1095 static const range_operator *
1096 get_range_op_handler (value_range *vr,
1097 enum tree_code code,
1098 tree expr_type)
1100 const range_operator *op = range_op_handler (code, expr_type);
1101 if (!op)
1102 vr->set_varying (expr_type);
1103 return op;
1106 /* If the types passed are supported, return TRUE, otherwise set VR to
1107 VARYING and return FALSE. */
1109 static bool
1110 supported_types_p (value_range *vr,
1111 tree type0,
1112 tree type1 = NULL)
1114 if (!value_range::supports_type_p (type0)
1115 || (type1 && !value_range::supports_type_p (type1)))
1117 vr->set_varying (type0);
1118 return false;
1120 return true;
1123 /* If any of the ranges passed are defined, return TRUE, otherwise set
1124 VR to UNDEFINED and return FALSE. */
1126 static bool
1127 defined_ranges_p (value_range *vr,
1128 const value_range *vr0, const value_range *vr1 = NULL)
1130 if (vr0->undefined_p () && (!vr1 || vr1->undefined_p ()))
1132 vr->set_undefined ();
1133 return false;
1135 return true;
1138 static value_range
1139 drop_undefines_to_varying (const value_range *vr, tree expr_type)
1141 if (vr->undefined_p ())
1142 return value_range (expr_type);
1143 else
1144 return *vr;
1147 /* If any operand is symbolic, perform a binary operation on them and
1148 return TRUE, otherwise return FALSE. */
1150 static bool
1151 range_fold_binary_symbolics_p (value_range *vr,
1152 tree_code code,
1153 tree expr_type,
1154 const value_range *vr0_,
1155 const value_range *vr1_)
1157 if (vr0_->symbolic_p () || vr1_->symbolic_p ())
1159 value_range vr0 = drop_undefines_to_varying (vr0_, expr_type);
1160 value_range vr1 = drop_undefines_to_varying (vr1_, expr_type);
1161 if ((code == PLUS_EXPR || code == MINUS_EXPR))
1163 extract_range_from_plus_minus_expr (vr, code, expr_type,
1164 &vr0, &vr1);
1165 return true;
1167 if (POINTER_TYPE_P (expr_type) && code == POINTER_PLUS_EXPR)
1169 extract_range_from_pointer_plus_expr (vr, code, expr_type,
1170 &vr0, &vr1);
1171 return true;
1173 const range_operator *op = get_range_op_handler (vr, code, expr_type);
1174 vr0.normalize_symbolics ();
1175 vr1.normalize_symbolics ();
1176 return op->fold_range (*vr, expr_type, vr0, vr1);
1178 return false;
1181 /* If operand is symbolic, perform a unary operation on it and return
1182 TRUE, otherwise return FALSE. */
1184 static bool
1185 range_fold_unary_symbolics_p (value_range *vr,
1186 tree_code code,
1187 tree expr_type,
1188 const value_range *vr0)
1190 if (vr0->symbolic_p ())
1192 if (code == NEGATE_EXPR)
1194 /* -X is simply 0 - X. */
1195 value_range zero;
1196 zero.set_zero (vr0->type ());
1197 range_fold_binary_expr (vr, MINUS_EXPR, expr_type, &zero, vr0);
1198 return true;
1200 if (code == BIT_NOT_EXPR)
1202 /* ~X is simply -1 - X. */
1203 value_range minusone;
1204 minusone.set (build_int_cst (vr0->type (), -1));
1205 range_fold_binary_expr (vr, MINUS_EXPR, expr_type, &minusone, vr0);
1206 return true;
1208 const range_operator *op = get_range_op_handler (vr, code, expr_type);
1209 value_range vr0_cst (*vr0);
1210 vr0_cst.normalize_symbolics ();
1211 return op->fold_range (*vr, expr_type, vr0_cst, value_range (expr_type));
1213 return false;
1216 /* Perform a binary operation on a pair of ranges. */
1218 void
1219 range_fold_binary_expr (value_range *vr,
1220 enum tree_code code,
1221 tree expr_type,
1222 const value_range *vr0_,
1223 const value_range *vr1_)
1225 if (!supported_types_p (vr, expr_type)
1226 || !defined_ranges_p (vr, vr0_, vr1_))
1227 return;
1228 const range_operator *op = get_range_op_handler (vr, code, expr_type);
1229 if (!op)
1230 return;
1232 if (range_fold_binary_symbolics_p (vr, code, expr_type, vr0_, vr1_))
1233 return;
1235 value_range vr0 (*vr0_);
1236 value_range vr1 (*vr1_);
1237 if (vr0.undefined_p ())
1238 vr0.set_varying (expr_type);
1239 if (vr1.undefined_p ())
1240 vr1.set_varying (expr_type);
1241 vr0.normalize_addresses ();
1242 vr1.normalize_addresses ();
1243 op->fold_range (*vr, expr_type, vr0, vr1);
1246 /* Perform a unary operation on a range. */
1248 void
1249 range_fold_unary_expr (value_range *vr,
1250 enum tree_code code, tree expr_type,
1251 const value_range *vr0,
1252 tree vr0_type)
1254 if (!supported_types_p (vr, expr_type, vr0_type)
1255 || !defined_ranges_p (vr, vr0))
1256 return;
1257 const range_operator *op = get_range_op_handler (vr, code, expr_type);
1258 if (!op)
1259 return;
1261 if (range_fold_unary_symbolics_p (vr, code, expr_type, vr0))
1262 return;
1264 value_range vr0_cst (*vr0);
1265 vr0_cst.normalize_addresses ();
1266 op->fold_range (*vr, expr_type, vr0_cst, value_range (expr_type));
1269 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
1270 create a new SSA name N and return the assertion assignment
1271 'N = ASSERT_EXPR <V, V OP W>'. */
1273 gimple *
1274 vrp_insert::build_assert_expr_for (tree cond, tree v)
1276 tree a;
1277 gassign *assertion;
1279 gcc_assert (TREE_CODE (v) == SSA_NAME
1280 && COMPARISON_CLASS_P (cond));
1282 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
1283 assertion = gimple_build_assign (NULL_TREE, a);
1285 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
1286 operand of the ASSERT_EXPR. Create it so the new name and the old one
1287 are registered in the replacement table so that we can fix the SSA web
1288 after adding all the ASSERT_EXPRs. */
1289 tree new_def = create_new_def_for (v, assertion, NULL);
1290 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
1291 given we have to be able to fully propagate those out to re-create
1292 valid SSA when removing the asserts. */
1293 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v))
1294 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def) = 1;
1296 return assertion;
1300 /* Return false if EXPR is a predicate expression involving floating
1301 point values. */
1303 static inline bool
1304 fp_predicate (gimple *stmt)
1306 GIMPLE_CHECK (stmt, GIMPLE_COND);
1308 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
1311 /* If the range of values taken by OP can be inferred after STMT executes,
1312 return the comparison code (COMP_CODE_P) and value (VAL_P) that
1313 describes the inferred range. Return true if a range could be
1314 inferred. */
1316 bool
1317 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
1319 *val_p = NULL_TREE;
1320 *comp_code_p = ERROR_MARK;
1322 /* Do not attempt to infer anything in names that flow through
1323 abnormal edges. */
1324 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
1325 return false;
1327 /* If STMT is the last statement of a basic block with no normal
1328 successors, there is no point inferring anything about any of its
1329 operands. We would not be able to find a proper insertion point
1330 for the assertion, anyway. */
1331 if (stmt_ends_bb_p (stmt))
1333 edge_iterator ei;
1334 edge e;
1336 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1337 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
1338 break;
1339 if (e == NULL)
1340 return false;
1343 if (infer_nonnull_range (stmt, op))
1345 *val_p = build_int_cst (TREE_TYPE (op), 0);
1346 *comp_code_p = NE_EXPR;
1347 return true;
1350 return false;
1353 /* Dump all the registered assertions for NAME to FILE. */
1355 void
1356 vrp_insert::dump (FILE *file, tree name)
1358 assert_locus *loc;
1360 fprintf (file, "Assertions to be inserted for ");
1361 print_generic_expr (file, name);
1362 fprintf (file, "\n");
1364 loc = asserts_for[SSA_NAME_VERSION (name)];
1365 while (loc)
1367 fprintf (file, "\t");
1368 print_gimple_stmt (file, gsi_stmt (loc->si), 0);
1369 fprintf (file, "\n\tBB #%d", loc->bb->index);
1370 if (loc->e)
1372 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
1373 loc->e->dest->index);
1374 dump_edge_info (file, loc->e, dump_flags, 0);
1376 fprintf (file, "\n\tPREDICATE: ");
1377 print_generic_expr (file, loc->expr);
1378 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
1379 print_generic_expr (file, loc->val);
1380 fprintf (file, "\n\n");
1381 loc = loc->next;
1384 fprintf (file, "\n");
1387 /* Dump all the registered assertions for all the names to FILE. */
1389 void
1390 vrp_insert::dump (FILE *file)
1392 unsigned i;
1393 bitmap_iterator bi;
1395 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
1396 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
1397 dump (file, ssa_name (i));
1398 fprintf (file, "\n");
1401 /* Dump assert_info structure. */
1403 void
1404 dump_assert_info (FILE *file, const assert_info &assert)
1406 fprintf (file, "Assert for: ");
1407 print_generic_expr (file, assert.name);
1408 fprintf (file, "\n\tPREDICATE: expr=[");
1409 print_generic_expr (file, assert.expr);
1410 fprintf (file, "] %s ", get_tree_code_name (assert.comp_code));
1411 fprintf (file, "val=[");
1412 print_generic_expr (file, assert.val);
1413 fprintf (file, "]\n\n");
1416 DEBUG_FUNCTION void
1417 debug (const assert_info &assert)
1419 dump_assert_info (stderr, assert);
1422 /* Dump a vector of assert_info's. */
1424 void
1425 dump_asserts_info (FILE *file, const vec<assert_info> &asserts)
1427 for (unsigned i = 0; i < asserts.length (); ++i)
1429 dump_assert_info (file, asserts[i]);
1430 fprintf (file, "\n");
1434 DEBUG_FUNCTION void
1435 debug (const vec<assert_info> &asserts)
1437 dump_asserts_info (stderr, asserts);
1440 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
1442 static void
1443 add_assert_info (vec<assert_info> &asserts,
1444 tree name, tree expr, enum tree_code comp_code, tree val)
1446 assert_info info;
1447 info.comp_code = comp_code;
1448 info.name = name;
1449 if (TREE_OVERFLOW_P (val))
1450 val = drop_tree_overflow (val);
1451 info.val = val;
1452 info.expr = expr;
1453 asserts.safe_push (info);
1454 if (dump_enabled_p ())
1455 dump_printf (MSG_NOTE | MSG_PRIORITY_INTERNALS,
1456 "Adding assert for %T from %T %s %T\n",
1457 name, expr, op_symbol_code (comp_code), val);
1460 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
1461 'EXPR COMP_CODE VAL' at a location that dominates block BB or
1462 E->DEST, then register this location as a possible insertion point
1463 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
1465 BB, E and SI provide the exact insertion point for the new
1466 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
1467 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
1468 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
1469 must not be NULL. */
1471 void
1472 vrp_insert::register_new_assert_for (tree name, tree expr,
1473 enum tree_code comp_code,
1474 tree val,
1475 basic_block bb,
1476 edge e,
1477 gimple_stmt_iterator si)
1479 assert_locus *n, *loc, *last_loc;
1480 basic_block dest_bb;
1482 gcc_checking_assert (bb == NULL || e == NULL);
1484 if (e == NULL)
1485 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
1486 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
1488 /* Never build an assert comparing against an integer constant with
1489 TREE_OVERFLOW set. This confuses our undefined overflow warning
1490 machinery. */
1491 if (TREE_OVERFLOW_P (val))
1492 val = drop_tree_overflow (val);
1494 /* The new assertion A will be inserted at BB or E. We need to
1495 determine if the new location is dominated by a previously
1496 registered location for A. If we are doing an edge insertion,
1497 assume that A will be inserted at E->DEST. Note that this is not
1498 necessarily true.
1500 If E is a critical edge, it will be split. But even if E is
1501 split, the new block will dominate the same set of blocks that
1502 E->DEST dominates.
1504 The reverse, however, is not true, blocks dominated by E->DEST
1505 will not be dominated by the new block created to split E. So,
1506 if the insertion location is on a critical edge, we will not use
1507 the new location to move another assertion previously registered
1508 at a block dominated by E->DEST. */
1509 dest_bb = (bb) ? bb : e->dest;
1511 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
1512 VAL at a block dominating DEST_BB, then we don't need to insert a new
1513 one. Similarly, if the same assertion already exists at a block
1514 dominated by DEST_BB and the new location is not on a critical
1515 edge, then update the existing location for the assertion (i.e.,
1516 move the assertion up in the dominance tree).
1518 Note, this is implemented as a simple linked list because there
1519 should not be more than a handful of assertions registered per
1520 name. If this becomes a performance problem, a table hashed by
1521 COMP_CODE and VAL could be implemented. */
1522 loc = asserts_for[SSA_NAME_VERSION (name)];
1523 last_loc = loc;
1524 while (loc)
1526 if (loc->comp_code == comp_code
1527 && (loc->val == val
1528 || operand_equal_p (loc->val, val, 0))
1529 && (loc->expr == expr
1530 || operand_equal_p (loc->expr, expr, 0)))
1532 /* If E is not a critical edge and DEST_BB
1533 dominates the existing location for the assertion, move
1534 the assertion up in the dominance tree by updating its
1535 location information. */
1536 if ((e == NULL || !EDGE_CRITICAL_P (e))
1537 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
1539 loc->bb = dest_bb;
1540 loc->e = e;
1541 loc->si = si;
1542 return;
1546 /* Update the last node of the list and move to the next one. */
1547 last_loc = loc;
1548 loc = loc->next;
1551 /* If we didn't find an assertion already registered for
1552 NAME COMP_CODE VAL, add a new one at the end of the list of
1553 assertions associated with NAME. */
1554 n = XNEW (struct assert_locus);
1555 n->bb = dest_bb;
1556 n->e = e;
1557 n->si = si;
1558 n->comp_code = comp_code;
1559 n->val = val;
1560 n->expr = expr;
1561 n->next = NULL;
1563 if (last_loc)
1564 last_loc->next = n;
1565 else
1566 asserts_for[SSA_NAME_VERSION (name)] = n;
1568 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
1571 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
1572 Extract a suitable test code and value and store them into *CODE_P and
1573 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
1575 If no extraction was possible, return FALSE, otherwise return TRUE.
1577 If INVERT is true, then we invert the result stored into *CODE_P. */
1579 static bool
1580 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
1581 tree cond_op0, tree cond_op1,
1582 bool invert, enum tree_code *code_p,
1583 tree *val_p)
1585 enum tree_code comp_code;
1586 tree val;
1588 /* Otherwise, we have a comparison of the form NAME COMP VAL
1589 or VAL COMP NAME. */
1590 if (name == cond_op1)
1592 /* If the predicate is of the form VAL COMP NAME, flip
1593 COMP around because we need to register NAME as the
1594 first operand in the predicate. */
1595 comp_code = swap_tree_comparison (cond_code);
1596 val = cond_op0;
1598 else if (name == cond_op0)
1600 /* The comparison is of the form NAME COMP VAL, so the
1601 comparison code remains unchanged. */
1602 comp_code = cond_code;
1603 val = cond_op1;
1605 else
1606 gcc_unreachable ();
1608 /* Invert the comparison code as necessary. */
1609 if (invert)
1610 comp_code = invert_tree_comparison (comp_code, 0);
1612 /* VRP only handles integral and pointer types. */
1613 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
1614 && ! POINTER_TYPE_P (TREE_TYPE (val)))
1615 return false;
1617 /* Do not register always-false predicates.
1618 FIXME: this works around a limitation in fold() when dealing with
1619 enumerations. Given 'enum { N1, N2 } x;', fold will not
1620 fold 'if (x > N2)' to 'if (0)'. */
1621 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
1622 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1624 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
1625 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
1627 if (comp_code == GT_EXPR
1628 && (!max
1629 || compare_values (val, max) == 0))
1630 return false;
1632 if (comp_code == LT_EXPR
1633 && (!min
1634 || compare_values (val, min) == 0))
1635 return false;
1637 *code_p = comp_code;
1638 *val_p = val;
1639 return true;
1642 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
1643 (otherwise return VAL). VAL and MASK must be zero-extended for
1644 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
1645 (to transform signed values into unsigned) and at the end xor
1646 SGNBIT back. */
1648 wide_int
1649 masked_increment (const wide_int &val_in, const wide_int &mask,
1650 const wide_int &sgnbit, unsigned int prec)
1652 wide_int bit = wi::one (prec), res;
1653 unsigned int i;
1655 wide_int val = val_in ^ sgnbit;
1656 for (i = 0; i < prec; i++, bit += bit)
1658 res = mask;
1659 if ((res & bit) == 0)
1660 continue;
1661 res = bit - 1;
1662 res = wi::bit_and_not (val + bit, res);
1663 res &= mask;
1664 if (wi::gtu_p (res, val))
1665 return res ^ sgnbit;
1667 return val ^ sgnbit;
1670 /* Helper for overflow_comparison_p
1672 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
1673 OP1's defining statement to see if it ultimately has the form
1674 OP0 CODE (OP0 PLUS INTEGER_CST)
1676 If so, return TRUE indicating this is an overflow test and store into
1677 *NEW_CST an updated constant that can be used in a narrowed range test.
1679 REVERSED indicates if the comparison was originally:
1681 OP1 CODE' OP0.
1683 This affects how we build the updated constant. */
1685 static bool
1686 overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
1687 bool follow_assert_exprs, bool reversed, tree *new_cst)
1689 /* See if this is a relational operation between two SSA_NAMES with
1690 unsigned, overflow wrapping values. If so, check it more deeply. */
1691 if ((code == LT_EXPR || code == LE_EXPR
1692 || code == GE_EXPR || code == GT_EXPR)
1693 && TREE_CODE (op0) == SSA_NAME
1694 && TREE_CODE (op1) == SSA_NAME
1695 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
1696 && TYPE_UNSIGNED (TREE_TYPE (op0))
1697 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
1699 gimple *op1_def = SSA_NAME_DEF_STMT (op1);
1701 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
1702 if (follow_assert_exprs)
1704 while (gimple_assign_single_p (op1_def)
1705 && TREE_CODE (gimple_assign_rhs1 (op1_def)) == ASSERT_EXPR)
1707 op1 = TREE_OPERAND (gimple_assign_rhs1 (op1_def), 0);
1708 if (TREE_CODE (op1) != SSA_NAME)
1709 break;
1710 op1_def = SSA_NAME_DEF_STMT (op1);
1714 /* Now look at the defining statement of OP1 to see if it adds
1715 or subtracts a nonzero constant from another operand. */
1716 if (op1_def
1717 && is_gimple_assign (op1_def)
1718 && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
1719 && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
1720 && !integer_zerop (gimple_assign_rhs2 (op1_def)))
1722 tree target = gimple_assign_rhs1 (op1_def);
1724 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
1725 for one where TARGET appears on the RHS. */
1726 if (follow_assert_exprs)
1728 /* Now see if that "other operand" is op0, following the chain
1729 of ASSERT_EXPRs if necessary. */
1730 gimple *op0_def = SSA_NAME_DEF_STMT (op0);
1731 while (op0 != target
1732 && gimple_assign_single_p (op0_def)
1733 && TREE_CODE (gimple_assign_rhs1 (op0_def)) == ASSERT_EXPR)
1735 op0 = TREE_OPERAND (gimple_assign_rhs1 (op0_def), 0);
1736 if (TREE_CODE (op0) != SSA_NAME)
1737 break;
1738 op0_def = SSA_NAME_DEF_STMT (op0);
1742 /* If we did not find our target SSA_NAME, then this is not
1743 an overflow test. */
1744 if (op0 != target)
1745 return false;
1747 tree type = TREE_TYPE (op0);
1748 wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
1749 tree inc = gimple_assign_rhs2 (op1_def);
1750 if (reversed)
1751 *new_cst = wide_int_to_tree (type, max + wi::to_wide (inc));
1752 else
1753 *new_cst = wide_int_to_tree (type, max - wi::to_wide (inc));
1754 return true;
1757 return false;
1760 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
1761 OP1's defining statement to see if it ultimately has the form
1762 OP0 CODE (OP0 PLUS INTEGER_CST)
1764 If so, return TRUE indicating this is an overflow test and store into
1765 *NEW_CST an updated constant that can be used in a narrowed range test.
1767 These statements are left as-is in the IL to facilitate discovery of
1768 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
1769 the alternate range representation is often useful within VRP. */
1771 bool
1772 overflow_comparison_p (tree_code code, tree name, tree val,
1773 bool use_equiv_p, tree *new_cst)
1775 if (overflow_comparison_p_1 (code, name, val, use_equiv_p, false, new_cst))
1776 return true;
1777 return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
1778 use_equiv_p, true, new_cst);
1782 /* Try to register an edge assertion for SSA name NAME on edge E for
1783 the condition COND contributing to the conditional jump pointed to by BSI.
1784 Invert the condition COND if INVERT is true. */
1786 static void
1787 register_edge_assert_for_2 (tree name, edge e,
1788 enum tree_code cond_code,
1789 tree cond_op0, tree cond_op1, bool invert,
1790 vec<assert_info> &asserts)
1792 tree val;
1793 enum tree_code comp_code;
1795 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
1796 cond_op0,
1797 cond_op1,
1798 invert, &comp_code, &val))
1799 return;
1801 /* Queue the assert. */
1802 tree x;
1803 if (overflow_comparison_p (comp_code, name, val, false, &x))
1805 enum tree_code new_code = ((comp_code == GT_EXPR || comp_code == GE_EXPR)
1806 ? GT_EXPR : LE_EXPR);
1807 add_assert_info (asserts, name, name, new_code, x);
1809 add_assert_info (asserts, name, name, comp_code, val);
1811 /* In the case of NAME <= CST and NAME being defined as
1812 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
1813 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
1814 This catches range and anti-range tests. */
1815 if ((comp_code == LE_EXPR
1816 || comp_code == GT_EXPR)
1817 && TREE_CODE (val) == INTEGER_CST
1818 && TYPE_UNSIGNED (TREE_TYPE (val)))
1820 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1821 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
1823 /* Extract CST2 from the (optional) addition. */
1824 if (is_gimple_assign (def_stmt)
1825 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
1827 name2 = gimple_assign_rhs1 (def_stmt);
1828 cst2 = gimple_assign_rhs2 (def_stmt);
1829 if (TREE_CODE (name2) == SSA_NAME
1830 && TREE_CODE (cst2) == INTEGER_CST)
1831 def_stmt = SSA_NAME_DEF_STMT (name2);
1834 /* Extract NAME2 from the (optional) sign-changing cast. */
1835 if (gimple_assign_cast_p (def_stmt))
1837 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
1838 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
1839 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
1840 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
1841 name3 = gimple_assign_rhs1 (def_stmt);
1844 /* If name3 is used later, create an ASSERT_EXPR for it. */
1845 if (name3 != NULL_TREE
1846 && TREE_CODE (name3) == SSA_NAME
1847 && (cst2 == NULL_TREE
1848 || TREE_CODE (cst2) == INTEGER_CST)
1849 && INTEGRAL_TYPE_P (TREE_TYPE (name3)))
1851 tree tmp;
1853 /* Build an expression for the range test. */
1854 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
1855 if (cst2 != NULL_TREE)
1856 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
1857 add_assert_info (asserts, name3, tmp, comp_code, val);
1860 /* If name2 is used later, create an ASSERT_EXPR for it. */
1861 if (name2 != NULL_TREE
1862 && TREE_CODE (name2) == SSA_NAME
1863 && TREE_CODE (cst2) == INTEGER_CST
1864 && INTEGRAL_TYPE_P (TREE_TYPE (name2)))
1866 tree tmp;
1868 /* Build an expression for the range test. */
1869 tmp = name2;
1870 if (TREE_TYPE (name) != TREE_TYPE (name2))
1871 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
1872 if (cst2 != NULL_TREE)
1873 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
1874 add_assert_info (asserts, name2, tmp, comp_code, val);
1878 /* In the case of post-in/decrement tests like if (i++) ... and uses
1879 of the in/decremented value on the edge the extra name we want to
1880 assert for is not on the def chain of the name compared. Instead
1881 it is in the set of use stmts.
1882 Similar cases happen for conversions that were simplified through
1883 fold_{sign_changed,widened}_comparison. */
1884 if ((comp_code == NE_EXPR
1885 || comp_code == EQ_EXPR)
1886 && TREE_CODE (val) == INTEGER_CST)
1888 imm_use_iterator ui;
1889 gimple *use_stmt;
1890 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
1892 if (!is_gimple_assign (use_stmt))
1893 continue;
1895 /* Cut off to use-stmts that are dominating the predecessor. */
1896 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
1897 continue;
1899 tree name2 = gimple_assign_lhs (use_stmt);
1900 if (TREE_CODE (name2) != SSA_NAME)
1901 continue;
1903 enum tree_code code = gimple_assign_rhs_code (use_stmt);
1904 tree cst;
1905 if (code == PLUS_EXPR
1906 || code == MINUS_EXPR)
1908 cst = gimple_assign_rhs2 (use_stmt);
1909 if (TREE_CODE (cst) != INTEGER_CST)
1910 continue;
1911 cst = int_const_binop (code, val, cst);
1913 else if (CONVERT_EXPR_CODE_P (code))
1915 /* For truncating conversions we cannot record
1916 an inequality. */
1917 if (comp_code == NE_EXPR
1918 && (TYPE_PRECISION (TREE_TYPE (name2))
1919 < TYPE_PRECISION (TREE_TYPE (name))))
1920 continue;
1921 cst = fold_convert (TREE_TYPE (name2), val);
1923 else
1924 continue;
1926 if (TREE_OVERFLOW_P (cst))
1927 cst = drop_tree_overflow (cst);
1928 add_assert_info (asserts, name2, name2, comp_code, cst);
1932 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
1933 && TREE_CODE (val) == INTEGER_CST)
1935 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1936 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
1937 tree val2 = NULL_TREE;
1938 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
1939 wide_int mask = wi::zero (prec);
1940 unsigned int nprec = prec;
1941 enum tree_code rhs_code = ERROR_MARK;
1943 if (is_gimple_assign (def_stmt))
1944 rhs_code = gimple_assign_rhs_code (def_stmt);
1946 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
1947 assert that A != CST1 -+ CST2. */
1948 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
1949 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
1951 tree op0 = gimple_assign_rhs1 (def_stmt);
1952 tree op1 = gimple_assign_rhs2 (def_stmt);
1953 if (TREE_CODE (op0) == SSA_NAME
1954 && TREE_CODE (op1) == INTEGER_CST)
1956 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
1957 ? MINUS_EXPR : PLUS_EXPR);
1958 op1 = int_const_binop (reverse_op, val, op1);
1959 if (TREE_OVERFLOW (op1))
1960 op1 = drop_tree_overflow (op1);
1961 add_assert_info (asserts, op0, op0, comp_code, op1);
1965 /* Add asserts for NAME cmp CST and NAME being defined
1966 as NAME = (int) NAME2. */
1967 if (!TYPE_UNSIGNED (TREE_TYPE (val))
1968 && (comp_code == LE_EXPR || comp_code == LT_EXPR
1969 || comp_code == GT_EXPR || comp_code == GE_EXPR)
1970 && gimple_assign_cast_p (def_stmt))
1972 name2 = gimple_assign_rhs1 (def_stmt);
1973 if (CONVERT_EXPR_CODE_P (rhs_code)
1974 && TREE_CODE (name2) == SSA_NAME
1975 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
1976 && TYPE_UNSIGNED (TREE_TYPE (name2))
1977 && prec == TYPE_PRECISION (TREE_TYPE (name2))
1978 && (comp_code == LE_EXPR || comp_code == GT_EXPR
1979 || !tree_int_cst_equal (val,
1980 TYPE_MIN_VALUE (TREE_TYPE (val)))))
1982 tree tmp, cst;
1983 enum tree_code new_comp_code = comp_code;
1985 cst = fold_convert (TREE_TYPE (name2),
1986 TYPE_MIN_VALUE (TREE_TYPE (val)));
1987 /* Build an expression for the range test. */
1988 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
1989 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
1990 fold_convert (TREE_TYPE (name2), val));
1991 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
1993 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
1994 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
1995 build_int_cst (TREE_TYPE (name2), 1));
1997 add_assert_info (asserts, name2, tmp, new_comp_code, cst);
2001 /* Add asserts for NAME cmp CST and NAME being defined as
2002 NAME = NAME2 >> CST2.
2004 Extract CST2 from the right shift. */
2005 if (rhs_code == RSHIFT_EXPR)
2007 name2 = gimple_assign_rhs1 (def_stmt);
2008 cst2 = gimple_assign_rhs2 (def_stmt);
2009 if (TREE_CODE (name2) == SSA_NAME
2010 && tree_fits_uhwi_p (cst2)
2011 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
2012 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
2013 && type_has_mode_precision_p (TREE_TYPE (val)))
2015 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
2016 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
2019 if (val2 != NULL_TREE
2020 && TREE_CODE (val2) == INTEGER_CST
2021 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
2022 TREE_TYPE (val),
2023 val2, cst2), val))
2025 enum tree_code new_comp_code = comp_code;
2026 tree tmp, new_val;
2028 tmp = name2;
2029 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
2031 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
2033 tree type = build_nonstandard_integer_type (prec, 1);
2034 tmp = build1 (NOP_EXPR, type, name2);
2035 val2 = fold_convert (type, val2);
2037 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
2038 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
2039 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
2041 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
2043 wide_int minval
2044 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
2045 new_val = val2;
2046 if (minval == wi::to_wide (new_val))
2047 new_val = NULL_TREE;
2049 else
2051 wide_int maxval
2052 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
2053 mask |= wi::to_wide (val2);
2054 if (wi::eq_p (mask, maxval))
2055 new_val = NULL_TREE;
2056 else
2057 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
2060 if (new_val)
2061 add_assert_info (asserts, name2, tmp, new_comp_code, new_val);
2064 /* If we have a conversion that doesn't change the value of the source
2065 simply register the same assert for it. */
2066 if (CONVERT_EXPR_CODE_P (rhs_code))
2068 wide_int rmin, rmax;
2069 tree rhs1 = gimple_assign_rhs1 (def_stmt);
2070 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2071 && TREE_CODE (rhs1) == SSA_NAME
2072 /* Make sure the relation preserves the upper/lower boundary of
2073 the range conservatively. */
2074 && (comp_code == NE_EXPR
2075 || comp_code == EQ_EXPR
2076 || (TYPE_SIGN (TREE_TYPE (name))
2077 == TYPE_SIGN (TREE_TYPE (rhs1)))
2078 || ((comp_code == LE_EXPR
2079 || comp_code == LT_EXPR)
2080 && !TYPE_UNSIGNED (TREE_TYPE (rhs1)))
2081 || ((comp_code == GE_EXPR
2082 || comp_code == GT_EXPR)
2083 && TYPE_UNSIGNED (TREE_TYPE (rhs1))))
2084 /* And the conversion does not alter the value we compare
2085 against and all values in rhs1 can be represented in
2086 the converted to type. */
2087 && int_fits_type_p (val, TREE_TYPE (rhs1))
2088 && ((TYPE_PRECISION (TREE_TYPE (name))
2089 > TYPE_PRECISION (TREE_TYPE (rhs1)))
2090 || (get_range_info (rhs1, &rmin, &rmax) == VR_RANGE
2091 && wi::fits_to_tree_p (rmin, TREE_TYPE (name))
2092 && wi::fits_to_tree_p (rmax, TREE_TYPE (name)))))
2093 add_assert_info (asserts, rhs1, rhs1,
2094 comp_code, fold_convert (TREE_TYPE (rhs1), val));
2097 /* Add asserts for NAME cmp CST and NAME being defined as
2098 NAME = NAME2 & CST2.
2100 Extract CST2 from the and.
2102 Also handle
2103 NAME = (unsigned) NAME2;
2104 casts where NAME's type is unsigned and has smaller precision
2105 than NAME2's type as if it was NAME = NAME2 & MASK. */
2106 names[0] = NULL_TREE;
2107 names[1] = NULL_TREE;
2108 cst2 = NULL_TREE;
2109 if (rhs_code == BIT_AND_EXPR
2110 || (CONVERT_EXPR_CODE_P (rhs_code)
2111 && INTEGRAL_TYPE_P (TREE_TYPE (val))
2112 && TYPE_UNSIGNED (TREE_TYPE (val))
2113 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
2114 > prec))
2116 name2 = gimple_assign_rhs1 (def_stmt);
2117 if (rhs_code == BIT_AND_EXPR)
2118 cst2 = gimple_assign_rhs2 (def_stmt);
2119 else
2121 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
2122 nprec = TYPE_PRECISION (TREE_TYPE (name2));
2124 if (TREE_CODE (name2) == SSA_NAME
2125 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
2126 && TREE_CODE (cst2) == INTEGER_CST
2127 && !integer_zerop (cst2)
2128 && (nprec > 1
2129 || TYPE_UNSIGNED (TREE_TYPE (val))))
2131 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
2132 if (gimple_assign_cast_p (def_stmt2))
2134 names[1] = gimple_assign_rhs1 (def_stmt2);
2135 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
2136 || TREE_CODE (names[1]) != SSA_NAME
2137 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
2138 || (TYPE_PRECISION (TREE_TYPE (name2))
2139 != TYPE_PRECISION (TREE_TYPE (names[1]))))
2140 names[1] = NULL_TREE;
2142 names[0] = name2;
2145 if (names[0] || names[1])
2147 wide_int minv, maxv, valv, cst2v;
2148 wide_int tem, sgnbit;
2149 bool valid_p = false, valn, cst2n;
2150 enum tree_code ccode = comp_code;
2152 valv = wide_int::from (wi::to_wide (val), nprec, UNSIGNED);
2153 cst2v = wide_int::from (wi::to_wide (cst2), nprec, UNSIGNED);
2154 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
2155 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
2156 /* If CST2 doesn't have most significant bit set,
2157 but VAL is negative, we have comparison like
2158 if ((x & 0x123) > -4) (always true). Just give up. */
2159 if (!cst2n && valn)
2160 ccode = ERROR_MARK;
2161 if (cst2n)
2162 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2163 else
2164 sgnbit = wi::zero (nprec);
2165 minv = valv & cst2v;
2166 switch (ccode)
2168 case EQ_EXPR:
2169 /* Minimum unsigned value for equality is VAL & CST2
2170 (should be equal to VAL, otherwise we probably should
2171 have folded the comparison into false) and
2172 maximum unsigned value is VAL | ~CST2. */
2173 maxv = valv | ~cst2v;
2174 valid_p = true;
2175 break;
2177 case NE_EXPR:
2178 tem = valv | ~cst2v;
2179 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
2180 if (valv == 0)
2182 cst2n = false;
2183 sgnbit = wi::zero (nprec);
2184 goto gt_expr;
2186 /* If (VAL | ~CST2) is all ones, handle it as
2187 (X & CST2) < VAL. */
2188 if (tem == -1)
2190 cst2n = false;
2191 valn = false;
2192 sgnbit = wi::zero (nprec);
2193 goto lt_expr;
2195 if (!cst2n && wi::neg_p (cst2v))
2196 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
2197 if (sgnbit != 0)
2199 if (valv == sgnbit)
2201 cst2n = true;
2202 valn = true;
2203 goto gt_expr;
2205 if (tem == wi::mask (nprec - 1, false, nprec))
2207 cst2n = true;
2208 goto lt_expr;
2210 if (!cst2n)
2211 sgnbit = wi::zero (nprec);
2213 break;
2215 case GE_EXPR:
2216 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
2217 is VAL and maximum unsigned value is ~0. For signed
2218 comparison, if CST2 doesn't have most significant bit
2219 set, handle it similarly. If CST2 has MSB set,
2220 the minimum is the same, and maximum is ~0U/2. */
2221 if (minv != valv)
2223 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
2224 VAL. */
2225 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2226 if (minv == valv)
2227 break;
2229 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2230 valid_p = true;
2231 break;
2233 case GT_EXPR:
2234 gt_expr:
2235 /* Find out smallest MINV where MINV > VAL
2236 && (MINV & CST2) == MINV, if any. If VAL is signed and
2237 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
2238 minv = masked_increment (valv, cst2v, sgnbit, nprec);
2239 if (minv == valv)
2240 break;
2241 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
2242 valid_p = true;
2243 break;
2245 case LE_EXPR:
2246 /* Minimum unsigned value for <= is 0 and maximum
2247 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
2248 Otherwise, find smallest VAL2 where VAL2 > VAL
2249 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2250 as maximum.
2251 For signed comparison, if CST2 doesn't have most
2252 significant bit set, handle it similarly. If CST2 has
2253 MSB set, the maximum is the same and minimum is INT_MIN. */
2254 if (minv == valv)
2255 maxv = valv;
2256 else
2258 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2259 if (maxv == valv)
2260 break;
2261 maxv -= 1;
2263 maxv |= ~cst2v;
2264 minv = sgnbit;
2265 valid_p = true;
2266 break;
2268 case LT_EXPR:
2269 lt_expr:
2270 /* Minimum unsigned value for < is 0 and maximum
2271 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
2272 Otherwise, find smallest VAL2 where VAL2 > VAL
2273 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
2274 as maximum.
2275 For signed comparison, if CST2 doesn't have most
2276 significant bit set, handle it similarly. If CST2 has
2277 MSB set, the maximum is the same and minimum is INT_MIN. */
2278 if (minv == valv)
2280 if (valv == sgnbit)
2281 break;
2282 maxv = valv;
2284 else
2286 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
2287 if (maxv == valv)
2288 break;
2290 maxv -= 1;
2291 maxv |= ~cst2v;
2292 minv = sgnbit;
2293 valid_p = true;
2294 break;
2296 default:
2297 break;
2299 if (valid_p
2300 && (maxv - minv) != -1)
2302 tree tmp, new_val, type;
2303 int i;
2305 for (i = 0; i < 2; i++)
2306 if (names[i])
2308 wide_int maxv2 = maxv;
2309 tmp = names[i];
2310 type = TREE_TYPE (names[i]);
2311 if (!TYPE_UNSIGNED (type))
2313 type = build_nonstandard_integer_type (nprec, 1);
2314 tmp = build1 (NOP_EXPR, type, names[i]);
2316 if (minv != 0)
2318 tmp = build2 (PLUS_EXPR, type, tmp,
2319 wide_int_to_tree (type, -minv));
2320 maxv2 = maxv - minv;
2322 new_val = wide_int_to_tree (type, maxv2);
2323 add_assert_info (asserts, names[i], tmp, LE_EXPR, new_val);
2330 /* OP is an operand of a truth value expression which is known to have
2331 a particular value. Register any asserts for OP and for any
2332 operands in OP's defining statement.
2334 If CODE is EQ_EXPR, then we want to register OP is zero (false),
2335 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
2337 static void
2338 register_edge_assert_for_1 (tree op, enum tree_code code,
2339 edge e, vec<assert_info> &asserts)
2341 gimple *op_def;
2342 tree val;
2343 enum tree_code rhs_code;
2345 /* We only care about SSA_NAMEs. */
2346 if (TREE_CODE (op) != SSA_NAME)
2347 return;
2349 /* We know that OP will have a zero or nonzero value. */
2350 val = build_int_cst (TREE_TYPE (op), 0);
2351 add_assert_info (asserts, op, op, code, val);
2353 /* Now look at how OP is set. If it's set from a comparison,
2354 a truth operation or some bit operations, then we may be able
2355 to register information about the operands of that assignment. */
2356 op_def = SSA_NAME_DEF_STMT (op);
2357 if (gimple_code (op_def) != GIMPLE_ASSIGN)
2358 return;
2360 rhs_code = gimple_assign_rhs_code (op_def);
2362 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
2364 bool invert = (code == EQ_EXPR ? true : false);
2365 tree op0 = gimple_assign_rhs1 (op_def);
2366 tree op1 = gimple_assign_rhs2 (op_def);
2368 if (TREE_CODE (op0) == SSA_NAME)
2369 register_edge_assert_for_2 (op0, e, rhs_code, op0, op1, invert, asserts);
2370 if (TREE_CODE (op1) == SSA_NAME)
2371 register_edge_assert_for_2 (op1, e, rhs_code, op0, op1, invert, asserts);
2373 else if ((code == NE_EXPR
2374 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
2375 || (code == EQ_EXPR
2376 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
2378 /* Recurse on each operand. */
2379 tree op0 = gimple_assign_rhs1 (op_def);
2380 tree op1 = gimple_assign_rhs2 (op_def);
2381 if (TREE_CODE (op0) == SSA_NAME
2382 && has_single_use (op0))
2383 register_edge_assert_for_1 (op0, code, e, asserts);
2384 if (TREE_CODE (op1) == SSA_NAME
2385 && has_single_use (op1))
2386 register_edge_assert_for_1 (op1, code, e, asserts);
2388 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
2389 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
2391 /* Recurse, flipping CODE. */
2392 code = invert_tree_comparison (code, false);
2393 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
2395 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
2397 /* Recurse through the copy. */
2398 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
2400 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
2402 /* Recurse through the type conversion, unless it is a narrowing
2403 conversion or conversion from non-integral type. */
2404 tree rhs = gimple_assign_rhs1 (op_def);
2405 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
2406 && (TYPE_PRECISION (TREE_TYPE (rhs))
2407 <= TYPE_PRECISION (TREE_TYPE (op))))
2408 register_edge_assert_for_1 (rhs, code, e, asserts);
2412 /* Check if comparison
2413 NAME COND_OP INTEGER_CST
2414 has a form of
2415 (X & 11...100..0) COND_OP XX...X00...0
2416 Such comparison can yield assertions like
2417 X >= XX...X00...0
2418 X <= XX...X11...1
2419 in case of COND_OP being EQ_EXPR or
2420 X < XX...X00...0
2421 X > XX...X11...1
2422 in case of NE_EXPR. */
2424 static bool
2425 is_masked_range_test (tree name, tree valt, enum tree_code cond_code,
2426 tree *new_name, tree *low, enum tree_code *low_code,
2427 tree *high, enum tree_code *high_code)
2429 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
2431 if (!is_gimple_assign (def_stmt)
2432 || gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
2433 return false;
2435 tree t = gimple_assign_rhs1 (def_stmt);
2436 tree maskt = gimple_assign_rhs2 (def_stmt);
2437 if (TREE_CODE (t) != SSA_NAME || TREE_CODE (maskt) != INTEGER_CST)
2438 return false;
2440 wi::tree_to_wide_ref mask = wi::to_wide (maskt);
2441 wide_int inv_mask = ~mask;
2442 /* Must have been removed by now so don't bother optimizing. */
2443 if (mask == 0 || inv_mask == 0)
2444 return false;
2446 /* Assume VALT is INTEGER_CST. */
2447 wi::tree_to_wide_ref val = wi::to_wide (valt);
2449 if ((inv_mask & (inv_mask + 1)) != 0
2450 || (val & mask) != val)
2451 return false;
2453 bool is_range = cond_code == EQ_EXPR;
2455 tree type = TREE_TYPE (t);
2456 wide_int min = wi::min_value (type),
2457 max = wi::max_value (type);
2459 if (is_range)
2461 *low_code = val == min ? ERROR_MARK : GE_EXPR;
2462 *high_code = val == max ? ERROR_MARK : LE_EXPR;
2464 else
2466 /* We can still generate assertion if one of alternatives
2467 is known to always be false. */
2468 if (val == min)
2470 *low_code = (enum tree_code) 0;
2471 *high_code = GT_EXPR;
2473 else if ((val | inv_mask) == max)
2475 *low_code = LT_EXPR;
2476 *high_code = (enum tree_code) 0;
2478 else
2479 return false;
2482 *new_name = t;
2483 *low = wide_int_to_tree (type, val);
2484 *high = wide_int_to_tree (type, val | inv_mask);
2486 return true;
2489 /* Try to register an edge assertion for SSA name NAME on edge E for
2490 the condition COND contributing to the conditional jump pointed to by
2491 SI. */
2493 void
2494 register_edge_assert_for (tree name, edge e,
2495 enum tree_code cond_code, tree cond_op0,
2496 tree cond_op1, vec<assert_info> &asserts)
2498 tree val;
2499 enum tree_code comp_code;
2500 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
2502 /* Do not attempt to infer anything in names that flow through
2503 abnormal edges. */
2504 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
2505 return;
2507 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
2508 cond_op0, cond_op1,
2509 is_else_edge,
2510 &comp_code, &val))
2511 return;
2513 /* Register ASSERT_EXPRs for name. */
2514 register_edge_assert_for_2 (name, e, cond_code, cond_op0,
2515 cond_op1, is_else_edge, asserts);
2518 /* If COND is effectively an equality test of an SSA_NAME against
2519 the value zero or one, then we may be able to assert values
2520 for SSA_NAMEs which flow into COND. */
2522 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
2523 statement of NAME we can assert both operands of the BIT_AND_EXPR
2524 have nonzero value. */
2525 if (((comp_code == EQ_EXPR && integer_onep (val))
2526 || (comp_code == NE_EXPR && integer_zerop (val))))
2528 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
2530 if (is_gimple_assign (def_stmt)
2531 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
2533 tree op0 = gimple_assign_rhs1 (def_stmt);
2534 tree op1 = gimple_assign_rhs2 (def_stmt);
2535 register_edge_assert_for_1 (op0, NE_EXPR, e, asserts);
2536 register_edge_assert_for_1 (op1, NE_EXPR, e, asserts);
2540 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
2541 statement of NAME we can assert both operands of the BIT_IOR_EXPR
2542 have zero value. */
2543 if (((comp_code == EQ_EXPR && integer_zerop (val))
2544 || (comp_code == NE_EXPR && integer_onep (val))))
2546 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
2548 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
2549 necessarily zero value, or if type-precision is one. */
2550 if (is_gimple_assign (def_stmt)
2551 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
2552 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
2553 || comp_code == EQ_EXPR)))
2555 tree op0 = gimple_assign_rhs1 (def_stmt);
2556 tree op1 = gimple_assign_rhs2 (def_stmt);
2557 register_edge_assert_for_1 (op0, EQ_EXPR, e, asserts);
2558 register_edge_assert_for_1 (op1, EQ_EXPR, e, asserts);
2562 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
2563 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
2564 && TREE_CODE (val) == INTEGER_CST)
2566 enum tree_code low_code, high_code;
2567 tree low, high;
2568 if (is_masked_range_test (name, val, comp_code, &name, &low,
2569 &low_code, &high, &high_code))
2571 if (low_code != ERROR_MARK)
2572 register_edge_assert_for_2 (name, e, low_code, name,
2573 low, /*invert*/false, asserts);
2574 if (high_code != ERROR_MARK)
2575 register_edge_assert_for_2 (name, e, high_code, name,
2576 high, /*invert*/false, asserts);
2581 /* Finish found ASSERTS for E and register them at GSI. */
2583 void
2584 vrp_insert::finish_register_edge_assert_for (edge e, gimple_stmt_iterator gsi,
2585 vec<assert_info> &asserts)
2587 for (unsigned i = 0; i < asserts.length (); ++i)
2588 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
2589 reachable from E. */
2590 if (live.live_on_edge_p (asserts[i].name, e))
2591 register_new_assert_for (asserts[i].name, asserts[i].expr,
2592 asserts[i].comp_code, asserts[i].val,
2593 NULL, e, gsi);
2598 /* Determine whether the outgoing edges of BB should receive an
2599 ASSERT_EXPR for each of the operands of BB's LAST statement.
2600 The last statement of BB must be a COND_EXPR.
2602 If any of the sub-graphs rooted at BB have an interesting use of
2603 the predicate operands, an assert location node is added to the
2604 list of assertions for the corresponding operands. */
2606 void
2607 vrp_insert::find_conditional_asserts (basic_block bb, gcond *last)
2609 gimple_stmt_iterator bsi;
2610 tree op;
2611 edge_iterator ei;
2612 edge e;
2613 ssa_op_iter iter;
2615 bsi = gsi_for_stmt (last);
2617 /* Look for uses of the operands in each of the sub-graphs
2618 rooted at BB. We need to check each of the outgoing edges
2619 separately, so that we know what kind of ASSERT_EXPR to
2620 insert. */
2621 FOR_EACH_EDGE (e, ei, bb->succs)
2623 if (e->dest == bb)
2624 continue;
2626 /* Register the necessary assertions for each operand in the
2627 conditional predicate. */
2628 auto_vec<assert_info, 8> asserts;
2629 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
2630 register_edge_assert_for (op, e,
2631 gimple_cond_code (last),
2632 gimple_cond_lhs (last),
2633 gimple_cond_rhs (last), asserts);
2634 finish_register_edge_assert_for (e, bsi, asserts);
2638 struct case_info
2640 tree expr;
2641 basic_block bb;
2644 /* Compare two case labels sorting first by the destination bb index
2645 and then by the case value. */
2647 static int
2648 compare_case_labels (const void *p1, const void *p2)
2650 const struct case_info *ci1 = (const struct case_info *) p1;
2651 const struct case_info *ci2 = (const struct case_info *) p2;
2652 int idx1 = ci1->bb->index;
2653 int idx2 = ci2->bb->index;
2655 if (idx1 < idx2)
2656 return -1;
2657 else if (idx1 == idx2)
2659 /* Make sure the default label is first in a group. */
2660 if (!CASE_LOW (ci1->expr))
2661 return -1;
2662 else if (!CASE_LOW (ci2->expr))
2663 return 1;
2664 else
2665 return tree_int_cst_compare (CASE_LOW (ci1->expr),
2666 CASE_LOW (ci2->expr));
2668 else
2669 return 1;
2672 /* Determine whether the outgoing edges of BB should receive an
2673 ASSERT_EXPR for each of the operands of BB's LAST statement.
2674 The last statement of BB must be a SWITCH_EXPR.
2676 If any of the sub-graphs rooted at BB have an interesting use of
2677 the predicate operands, an assert location node is added to the
2678 list of assertions for the corresponding operands. */
2680 void
2681 vrp_insert::find_switch_asserts (basic_block bb, gswitch *last)
2683 gimple_stmt_iterator bsi;
2684 tree op;
2685 edge e;
2686 struct case_info *ci;
2687 size_t n = gimple_switch_num_labels (last);
2688 #if GCC_VERSION >= 4000
2689 unsigned int idx;
2690 #else
2691 /* Work around GCC 3.4 bug (PR 37086). */
2692 volatile unsigned int idx;
2693 #endif
2695 bsi = gsi_for_stmt (last);
2696 op = gimple_switch_index (last);
2697 if (TREE_CODE (op) != SSA_NAME)
2698 return;
2700 /* Build a vector of case labels sorted by destination label. */
2701 ci = XNEWVEC (struct case_info, n);
2702 for (idx = 0; idx < n; ++idx)
2704 ci[idx].expr = gimple_switch_label (last, idx);
2705 ci[idx].bb = label_to_block (fun, CASE_LABEL (ci[idx].expr));
2707 edge default_edge = find_edge (bb, ci[0].bb);
2708 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
2710 for (idx = 0; idx < n; ++idx)
2712 tree min, max;
2713 tree cl = ci[idx].expr;
2714 basic_block cbb = ci[idx].bb;
2716 min = CASE_LOW (cl);
2717 max = CASE_HIGH (cl);
2719 /* If there are multiple case labels with the same destination
2720 we need to combine them to a single value range for the edge. */
2721 if (idx + 1 < n && cbb == ci[idx + 1].bb)
2723 /* Skip labels until the last of the group. */
2724 do {
2725 ++idx;
2726 } while (idx < n && cbb == ci[idx].bb);
2727 --idx;
2729 /* Pick up the maximum of the case label range. */
2730 if (CASE_HIGH (ci[idx].expr))
2731 max = CASE_HIGH (ci[idx].expr);
2732 else
2733 max = CASE_LOW (ci[idx].expr);
2736 /* Can't extract a useful assertion out of a range that includes the
2737 default label. */
2738 if (min == NULL_TREE)
2739 continue;
2741 /* Find the edge to register the assert expr on. */
2742 e = find_edge (bb, cbb);
2744 /* Register the necessary assertions for the operand in the
2745 SWITCH_EXPR. */
2746 auto_vec<assert_info, 8> asserts;
2747 register_edge_assert_for (op, e,
2748 max ? GE_EXPR : EQ_EXPR,
2749 op, fold_convert (TREE_TYPE (op), min),
2750 asserts);
2751 if (max)
2752 register_edge_assert_for (op, e, LE_EXPR, op,
2753 fold_convert (TREE_TYPE (op), max),
2754 asserts);
2755 finish_register_edge_assert_for (e, bsi, asserts);
2758 XDELETEVEC (ci);
2760 if (!live.live_on_edge_p (op, default_edge))
2761 return;
2763 /* Now register along the default label assertions that correspond to the
2764 anti-range of each label. */
2765 int insertion_limit = param_max_vrp_switch_assertions;
2766 if (insertion_limit == 0)
2767 return;
2769 /* We can't do this if the default case shares a label with another case. */
2770 tree default_cl = gimple_switch_default_label (last);
2771 for (idx = 1; idx < n; idx++)
2773 tree min, max;
2774 tree cl = gimple_switch_label (last, idx);
2775 if (CASE_LABEL (cl) == CASE_LABEL (default_cl))
2776 continue;
2778 min = CASE_LOW (cl);
2779 max = CASE_HIGH (cl);
2781 /* Combine contiguous case ranges to reduce the number of assertions
2782 to insert. */
2783 for (idx = idx + 1; idx < n; idx++)
2785 tree next_min, next_max;
2786 tree next_cl = gimple_switch_label (last, idx);
2787 if (CASE_LABEL (next_cl) == CASE_LABEL (default_cl))
2788 break;
2790 next_min = CASE_LOW (next_cl);
2791 next_max = CASE_HIGH (next_cl);
2793 wide_int difference = (wi::to_wide (next_min)
2794 - wi::to_wide (max ? max : min));
2795 if (wi::eq_p (difference, 1))
2796 max = next_max ? next_max : next_min;
2797 else
2798 break;
2800 idx--;
2802 if (max == NULL_TREE)
2804 /* Register the assertion OP != MIN. */
2805 auto_vec<assert_info, 8> asserts;
2806 min = fold_convert (TREE_TYPE (op), min);
2807 register_edge_assert_for (op, default_edge, NE_EXPR, op, min,
2808 asserts);
2809 finish_register_edge_assert_for (default_edge, bsi, asserts);
2811 else
2813 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
2814 which will give OP the anti-range ~[MIN,MAX]. */
2815 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
2816 min = fold_convert (TREE_TYPE (uop), min);
2817 max = fold_convert (TREE_TYPE (uop), max);
2819 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
2820 tree rhs = int_const_binop (MINUS_EXPR, max, min);
2821 register_new_assert_for (op, lhs, GT_EXPR, rhs,
2822 NULL, default_edge, bsi);
2825 if (--insertion_limit == 0)
2826 break;
2831 /* Traverse all the statements in block BB looking for statements that
2832 may generate useful assertions for the SSA names in their operand.
2833 If a statement produces a useful assertion A for name N_i, then the
2834 list of assertions already generated for N_i is scanned to
2835 determine if A is actually needed.
2837 If N_i already had the assertion A at a location dominating the
2838 current location, then nothing needs to be done. Otherwise, the
2839 new location for A is recorded instead.
2841 1- For every statement S in BB, all the variables used by S are
2842 added to bitmap FOUND_IN_SUBGRAPH.
2844 2- If statement S uses an operand N in a way that exposes a known
2845 value range for N, then if N was not already generated by an
2846 ASSERT_EXPR, create a new assert location for N. For instance,
2847 if N is a pointer and the statement dereferences it, we can
2848 assume that N is not NULL.
2850 3- COND_EXPRs are a special case of #2. We can derive range
2851 information from the predicate but need to insert different
2852 ASSERT_EXPRs for each of the sub-graphs rooted at the
2853 conditional block. If the last statement of BB is a conditional
2854 expression of the form 'X op Y', then
2856 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
2858 b) If the conditional is the only entry point to the sub-graph
2859 corresponding to the THEN_CLAUSE, recurse into it. On
2860 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
2861 an ASSERT_EXPR is added for the corresponding variable.
2863 c) Repeat step (b) on the ELSE_CLAUSE.
2865 d) Mark X and Y in FOUND_IN_SUBGRAPH.
2867 For instance,
2869 if (a == 9)
2870 b = a;
2871 else
2872 b = c + 1;
2874 In this case, an assertion on the THEN clause is useful to
2875 determine that 'a' is always 9 on that edge. However, an assertion
2876 on the ELSE clause would be unnecessary.
2878 4- If BB does not end in a conditional expression, then we recurse
2879 into BB's dominator children.
2881 At the end of the recursive traversal, every SSA name will have a
2882 list of locations where ASSERT_EXPRs should be added. When a new
2883 location for name N is found, it is registered by calling
2884 register_new_assert_for. That function keeps track of all the
2885 registered assertions to prevent adding unnecessary assertions.
2886 For instance, if a pointer P_4 is dereferenced more than once in a
2887 dominator tree, only the location dominating all the dereference of
2888 P_4 will receive an ASSERT_EXPR. */
2890 void
2891 vrp_insert::find_assert_locations_in_bb (basic_block bb)
2893 gimple *last;
2895 last = last_stmt (bb);
2897 /* If BB's last statement is a conditional statement involving integer
2898 operands, determine if we need to add ASSERT_EXPRs. */
2899 if (last
2900 && gimple_code (last) == GIMPLE_COND
2901 && !fp_predicate (last)
2902 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
2903 find_conditional_asserts (bb, as_a <gcond *> (last));
2905 /* If BB's last statement is a switch statement involving integer
2906 operands, determine if we need to add ASSERT_EXPRs. */
2907 if (last
2908 && gimple_code (last) == GIMPLE_SWITCH
2909 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
2910 find_switch_asserts (bb, as_a <gswitch *> (last));
2912 /* Traverse all the statements in BB marking used names and looking
2913 for statements that may infer assertions for their used operands. */
2914 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
2915 gsi_prev (&si))
2917 gimple *stmt;
2918 tree op;
2919 ssa_op_iter i;
2921 stmt = gsi_stmt (si);
2923 if (is_gimple_debug (stmt))
2924 continue;
2926 /* See if we can derive an assertion for any of STMT's operands. */
2927 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
2929 tree value;
2930 enum tree_code comp_code;
2932 /* If op is not live beyond this stmt, do not bother to insert
2933 asserts for it. */
2934 if (!live.live_on_block_p (op, bb))
2935 continue;
2937 /* If OP is used in such a way that we can infer a value
2938 range for it, and we don't find a previous assertion for
2939 it, create a new assertion location node for OP. */
2940 if (infer_value_range (stmt, op, &comp_code, &value))
2942 /* If we are able to infer a nonzero value range for OP,
2943 then walk backwards through the use-def chain to see if OP
2944 was set via a typecast.
2946 If so, then we can also infer a nonzero value range
2947 for the operand of the NOP_EXPR. */
2948 if (comp_code == NE_EXPR && integer_zerop (value))
2950 tree t = op;
2951 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
2953 while (is_gimple_assign (def_stmt)
2954 && CONVERT_EXPR_CODE_P
2955 (gimple_assign_rhs_code (def_stmt))
2956 && TREE_CODE
2957 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
2958 && POINTER_TYPE_P
2959 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
2961 t = gimple_assign_rhs1 (def_stmt);
2962 def_stmt = SSA_NAME_DEF_STMT (t);
2964 /* Note we want to register the assert for the
2965 operand of the NOP_EXPR after SI, not after the
2966 conversion. */
2967 if (live.live_on_block_p (t, bb))
2968 register_new_assert_for (t, t, comp_code, value,
2969 bb, NULL, si);
2973 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
2977 /* Update live. */
2978 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
2979 live.set (op, bb);
2980 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
2981 live.clear (op, bb);
2984 /* Traverse all PHI nodes in BB, updating live. */
2985 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
2986 gsi_next (&si))
2988 use_operand_p arg_p;
2989 ssa_op_iter i;
2990 gphi *phi = si.phi ();
2991 tree res = gimple_phi_result (phi);
2993 if (virtual_operand_p (res))
2994 continue;
2996 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
2998 tree arg = USE_FROM_PTR (arg_p);
2999 if (TREE_CODE (arg) == SSA_NAME)
3000 live.set (arg, bb);
3003 live.clear (res, bb);
3007 /* Do an RPO walk over the function computing SSA name liveness
3008 on-the-fly and deciding on assert expressions to insert. */
3010 void
3011 vrp_insert::find_assert_locations (void)
3013 int *rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
3014 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
3015 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (fun));
3016 int rpo_cnt, i;
3018 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3019 for (i = 0; i < rpo_cnt; ++i)
3020 bb_rpo[rpo[i]] = i;
3022 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
3023 the order we compute liveness and insert asserts we otherwise
3024 fail to insert asserts into the loop latch. */
3025 loop_p loop;
3026 FOR_EACH_LOOP (loop, 0)
3028 i = loop->latch->index;
3029 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
3030 for (gphi_iterator gsi = gsi_start_phis (loop->header);
3031 !gsi_end_p (gsi); gsi_next (&gsi))
3033 gphi *phi = gsi.phi ();
3034 if (virtual_operand_p (gimple_phi_result (phi)))
3035 continue;
3036 tree arg = gimple_phi_arg_def (phi, j);
3037 if (TREE_CODE (arg) == SSA_NAME)
3038 live.set (arg, loop->latch);
3042 for (i = rpo_cnt - 1; i >= 0; --i)
3044 basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i]);
3045 edge e;
3046 edge_iterator ei;
3048 /* Process BB and update the live information with uses in
3049 this block. */
3050 find_assert_locations_in_bb (bb);
3052 /* Merge liveness into the predecessor blocks and free it. */
3053 if (!live.block_has_live_names_p (bb))
3055 int pred_rpo = i;
3056 FOR_EACH_EDGE (e, ei, bb->preds)
3058 int pred = e->src->index;
3059 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
3060 continue;
3062 live.merge (e->src, bb);
3064 if (bb_rpo[pred] < pred_rpo)
3065 pred_rpo = bb_rpo[pred];
3068 /* Record the RPO number of the last visited block that needs
3069 live information from this block. */
3070 last_rpo[rpo[i]] = pred_rpo;
3072 else
3073 live.clear_block (bb);
3075 /* We can free all successors live bitmaps if all their
3076 predecessors have been visited already. */
3077 FOR_EACH_EDGE (e, ei, bb->succs)
3078 if (last_rpo[e->dest->index] == i)
3079 live.clear_block (e->dest);
3082 XDELETEVEC (rpo);
3083 XDELETEVEC (bb_rpo);
3084 XDELETEVEC (last_rpo);
3087 /* Create an ASSERT_EXPR for NAME and insert it in the location
3088 indicated by LOC. Return true if we made any edge insertions. */
3090 bool
3091 vrp_insert::process_assert_insertions_for (tree name, assert_locus *loc)
3093 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3094 gimple *stmt;
3095 tree cond;
3096 gimple *assert_stmt;
3097 edge_iterator ei;
3098 edge e;
3100 /* If we have X <=> X do not insert an assert expr for that. */
3101 if (loc->expr == loc->val)
3102 return false;
3104 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
3105 assert_stmt = build_assert_expr_for (cond, name);
3106 if (loc->e)
3108 /* We have been asked to insert the assertion on an edge. This
3109 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3110 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
3111 || (gimple_code (gsi_stmt (loc->si))
3112 == GIMPLE_SWITCH));
3114 gsi_insert_on_edge (loc->e, assert_stmt);
3115 return true;
3118 /* If the stmt iterator points at the end then this is an insertion
3119 at the beginning of a block. */
3120 if (gsi_end_p (loc->si))
3122 gimple_stmt_iterator si = gsi_after_labels (loc->bb);
3123 gsi_insert_before (&si, assert_stmt, GSI_SAME_STMT);
3124 return false;
3127 /* Otherwise, we can insert right after LOC->SI iff the
3128 statement must not be the last statement in the block. */
3129 stmt = gsi_stmt (loc->si);
3130 if (!stmt_ends_bb_p (stmt))
3132 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
3133 return false;
3136 /* If STMT must be the last statement in BB, we can only insert new
3137 assertions on the non-abnormal edge out of BB. Note that since
3138 STMT is not control flow, there may only be one non-abnormal/eh edge
3139 out of BB. */
3140 FOR_EACH_EDGE (e, ei, loc->bb->succs)
3141 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
3143 gsi_insert_on_edge (e, assert_stmt);
3144 return true;
3147 gcc_unreachable ();
3150 /* Qsort helper for sorting assert locations. If stable is true, don't
3151 use iterative_hash_expr because it can be unstable for -fcompare-debug,
3152 on the other side some pointers might be NULL. */
3154 template <bool stable>
3156 vrp_insert::compare_assert_loc (const void *pa, const void *pb)
3158 assert_locus * const a = *(assert_locus * const *)pa;
3159 assert_locus * const b = *(assert_locus * const *)pb;
3161 /* If stable, some asserts might be optimized away already, sort
3162 them last. */
3163 if (stable)
3165 if (a == NULL)
3166 return b != NULL;
3167 else if (b == NULL)
3168 return -1;
3171 if (a->e == NULL && b->e != NULL)
3172 return 1;
3173 else if (a->e != NULL && b->e == NULL)
3174 return -1;
3176 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
3177 no need to test both a->e and b->e. */
3179 /* Sort after destination index. */
3180 if (a->e == NULL)
3182 else if (a->e->dest->index > b->e->dest->index)
3183 return 1;
3184 else if (a->e->dest->index < b->e->dest->index)
3185 return -1;
3187 /* Sort after comp_code. */
3188 if (a->comp_code > b->comp_code)
3189 return 1;
3190 else if (a->comp_code < b->comp_code)
3191 return -1;
3193 hashval_t ha, hb;
3195 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
3196 uses DECL_UID of the VAR_DECL, so sorting might differ between
3197 -g and -g0. When doing the removal of redundant assert exprs
3198 and commonization to successors, this does not matter, but for
3199 the final sort needs to be stable. */
3200 if (stable)
3202 ha = 0;
3203 hb = 0;
3205 else
3207 ha = iterative_hash_expr (a->expr, iterative_hash_expr (a->val, 0));
3208 hb = iterative_hash_expr (b->expr, iterative_hash_expr (b->val, 0));
3211 /* Break the tie using hashing and source/bb index. */
3212 if (ha == hb)
3213 return (a->e != NULL
3214 ? a->e->src->index - b->e->src->index
3215 : a->bb->index - b->bb->index);
3216 return ha > hb ? 1 : -1;
3219 /* Process all the insertions registered for every name N_i registered
3220 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3221 found in ASSERTS_FOR[i]. */
3223 void
3224 vrp_insert::process_assert_insertions ()
3226 unsigned i;
3227 bitmap_iterator bi;
3228 bool update_edges_p = false;
3229 int num_asserts = 0;
3231 if (dump_file && (dump_flags & TDF_DETAILS))
3232 dump (dump_file);
3234 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
3236 assert_locus *loc = asserts_for[i];
3237 gcc_assert (loc);
3239 auto_vec<assert_locus *, 16> asserts;
3240 for (; loc; loc = loc->next)
3241 asserts.safe_push (loc);
3242 asserts.qsort (compare_assert_loc<false>);
3244 /* Push down common asserts to successors and remove redundant ones. */
3245 unsigned ecnt = 0;
3246 assert_locus *common = NULL;
3247 unsigned commonj = 0;
3248 for (unsigned j = 0; j < asserts.length (); ++j)
3250 loc = asserts[j];
3251 if (! loc->e)
3252 common = NULL;
3253 else if (! common
3254 || loc->e->dest != common->e->dest
3255 || loc->comp_code != common->comp_code
3256 || ! operand_equal_p (loc->val, common->val, 0)
3257 || ! operand_equal_p (loc->expr, common->expr, 0))
3259 commonj = j;
3260 common = loc;
3261 ecnt = 1;
3263 else if (loc->e == asserts[j-1]->e)
3265 /* Remove duplicate asserts. */
3266 if (commonj == j - 1)
3268 commonj = j;
3269 common = loc;
3271 free (asserts[j-1]);
3272 asserts[j-1] = NULL;
3274 else
3276 ecnt++;
3277 if (EDGE_COUNT (common->e->dest->preds) == ecnt)
3279 /* We have the same assertion on all incoming edges of a BB.
3280 Insert it at the beginning of that block. */
3281 loc->bb = loc->e->dest;
3282 loc->e = NULL;
3283 loc->si = gsi_none ();
3284 common = NULL;
3285 /* Clear asserts commoned. */
3286 for (; commonj != j; ++commonj)
3287 if (asserts[commonj])
3289 free (asserts[commonj]);
3290 asserts[commonj] = NULL;
3296 /* The asserts vector sorting above might be unstable for
3297 -fcompare-debug, sort again to ensure a stable sort. */
3298 asserts.qsort (compare_assert_loc<true>);
3299 for (unsigned j = 0; j < asserts.length (); ++j)
3301 loc = asserts[j];
3302 if (! loc)
3303 break;
3304 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
3305 num_asserts++;
3306 free (loc);
3310 if (update_edges_p)
3311 gsi_commit_edge_inserts ();
3313 statistics_counter_event (fun, "Number of ASSERT_EXPR expressions inserted",
3314 num_asserts);
3318 /* Traverse the flowgraph looking for conditional jumps to insert range
3319 expressions. These range expressions are meant to provide information
3320 to optimizations that need to reason in terms of value ranges. They
3321 will not be expanded into RTL. For instance, given:
3323 x = ...
3324 y = ...
3325 if (x < y)
3326 y = x - 2;
3327 else
3328 x = y + 3;
3330 this pass will transform the code into:
3332 x = ...
3333 y = ...
3334 if (x < y)
3336 x = ASSERT_EXPR <x, x < y>
3337 y = x - 2
3339 else
3341 y = ASSERT_EXPR <y, x >= y>
3342 x = y + 3
3345 The idea is that once copy and constant propagation have run, other
3346 optimizations will be able to determine what ranges of values can 'x'
3347 take in different paths of the code, simply by checking the reaching
3348 definition of 'x'. */
3350 void
3351 vrp_insert::insert_range_assertions (void)
3353 need_assert_for = BITMAP_ALLOC (NULL);
3354 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
3356 calculate_dominance_info (CDI_DOMINATORS);
3358 find_assert_locations ();
3359 if (!bitmap_empty_p (need_assert_for))
3361 process_assert_insertions ();
3362 update_ssa (TODO_update_ssa_no_phi);
3365 if (dump_file && (dump_flags & TDF_DETAILS))
3367 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
3368 dump_function_to_file (current_function_decl, dump_file, dump_flags);
3371 free (asserts_for);
3372 BITMAP_FREE (need_assert_for);
3375 class vrp_prop : public ssa_propagation_engine
3377 public:
3378 enum ssa_prop_result visit_stmt (gimple *, edge *, tree *) FINAL OVERRIDE;
3379 enum ssa_prop_result visit_phi (gphi *) FINAL OVERRIDE;
3381 struct function *fun;
3383 void vrp_initialize (struct function *);
3384 void vrp_finalize (class vrp_folder *, bool);
3386 class vr_values vr_values;
3388 private:
3389 /* Temporary delegator to minimize code churn. */
3390 const value_range_equiv *get_value_range (const_tree op)
3391 { return vr_values.get_value_range (op); }
3392 void set_def_to_varying (const_tree def)
3393 { vr_values.set_def_to_varying (def); }
3394 void set_defs_to_varying (gimple *stmt)
3395 { vr_values.set_defs_to_varying (stmt); }
3396 void extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
3397 tree *output_p, value_range_equiv *vr)
3398 { vr_values.extract_range_from_stmt (stmt, taken_edge_p, output_p, vr); }
3399 bool update_value_range (const_tree op, value_range_equiv *vr)
3400 { return vr_values.update_value_range (op, vr); }
3401 void extract_range_basic (value_range_equiv *vr, gimple *stmt)
3402 { vr_values.extract_range_basic (vr, stmt); }
3403 void extract_range_from_phi_node (gphi *phi, value_range_equiv *vr)
3404 { vr_values.extract_range_from_phi_node (phi, vr); }
3407 /* Return true if all imm uses of VAR are either in STMT, or
3408 feed (optionally through a chain of single imm uses) GIMPLE_COND
3409 in basic block COND_BB. */
3411 static bool
3412 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
3414 use_operand_p use_p, use2_p;
3415 imm_use_iterator iter;
3417 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
3418 if (USE_STMT (use_p) != stmt)
3420 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
3421 if (is_gimple_debug (use_stmt))
3422 continue;
3423 while (is_gimple_assign (use_stmt)
3424 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
3425 && single_imm_use (gimple_assign_lhs (use_stmt),
3426 &use2_p, &use_stmt2))
3427 use_stmt = use_stmt2;
3428 if (gimple_code (use_stmt) != GIMPLE_COND
3429 || gimple_bb (use_stmt) != cond_bb)
3430 return false;
3432 return true;
3435 /* Handle
3436 _4 = x_3 & 31;
3437 if (_4 != 0)
3438 goto <bb 6>;
3439 else
3440 goto <bb 7>;
3441 <bb 6>:
3442 __builtin_unreachable ();
3443 <bb 7>:
3444 x_5 = ASSERT_EXPR <x_3, ...>;
3445 If x_3 has no other immediate uses (checked by caller),
3446 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
3447 from the non-zero bitmask. */
3449 void
3450 maybe_set_nonzero_bits (edge e, tree var)
3452 basic_block cond_bb = e->src;
3453 gimple *stmt = last_stmt (cond_bb);
3454 tree cst;
3456 if (stmt == NULL
3457 || gimple_code (stmt) != GIMPLE_COND
3458 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
3459 ? EQ_EXPR : NE_EXPR)
3460 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
3461 || !integer_zerop (gimple_cond_rhs (stmt)))
3462 return;
3464 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
3465 if (!is_gimple_assign (stmt)
3466 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
3467 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
3468 return;
3469 if (gimple_assign_rhs1 (stmt) != var)
3471 gimple *stmt2;
3473 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
3474 return;
3475 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
3476 if (!gimple_assign_cast_p (stmt2)
3477 || gimple_assign_rhs1 (stmt2) != var
3478 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
3479 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
3480 != TYPE_PRECISION (TREE_TYPE (var))))
3481 return;
3483 cst = gimple_assign_rhs2 (stmt);
3484 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
3485 wi::to_wide (cst)));
3488 /* Convert range assertion expressions into the implied copies and
3489 copy propagate away the copies. Doing the trivial copy propagation
3490 here avoids the need to run the full copy propagation pass after
3491 VRP.
3493 FIXME, this will eventually lead to copy propagation removing the
3494 names that had useful range information attached to them. For
3495 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
3496 then N_i will have the range [3, +INF].
3498 However, by converting the assertion into the implied copy
3499 operation N_i = N_j, we will then copy-propagate N_j into the uses
3500 of N_i and lose the range information. We may want to hold on to
3501 ASSERT_EXPRs a little while longer as the ranges could be used in
3502 things like jump threading.
3504 The problem with keeping ASSERT_EXPRs around is that passes after
3505 VRP need to handle them appropriately.
3507 Another approach would be to make the range information a first
3508 class property of the SSA_NAME so that it can be queried from
3509 any pass. This is made somewhat more complex by the need for
3510 multiple ranges to be associated with one SSA_NAME. */
3512 void
3513 vrp_insert::remove_range_assertions ()
3515 basic_block bb;
3516 gimple_stmt_iterator si;
3517 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
3518 a basic block preceeded by GIMPLE_COND branching to it and
3519 __builtin_trap, -1 if not yet checked, 0 otherwise. */
3520 int is_unreachable;
3522 /* Note that the BSI iterator bump happens at the bottom of the
3523 loop and no bump is necessary if we're removing the statement
3524 referenced by the current BSI. */
3525 FOR_EACH_BB_FN (bb, fun)
3526 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
3528 gimple *stmt = gsi_stmt (si);
3530 if (is_gimple_assign (stmt)
3531 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
3533 tree lhs = gimple_assign_lhs (stmt);
3534 tree rhs = gimple_assign_rhs1 (stmt);
3535 tree var;
3537 var = ASSERT_EXPR_VAR (rhs);
3539 if (TREE_CODE (var) == SSA_NAME
3540 && !POINTER_TYPE_P (TREE_TYPE (lhs))
3541 && SSA_NAME_RANGE_INFO (lhs))
3543 if (is_unreachable == -1)
3545 is_unreachable = 0;
3546 if (single_pred_p (bb)
3547 && assert_unreachable_fallthru_edge_p
3548 (single_pred_edge (bb)))
3549 is_unreachable = 1;
3551 /* Handle
3552 if (x_7 >= 10 && x_7 < 20)
3553 __builtin_unreachable ();
3554 x_8 = ASSERT_EXPR <x_7, ...>;
3555 if the only uses of x_7 are in the ASSERT_EXPR and
3556 in the condition. In that case, we can copy the
3557 range info from x_8 computed in this pass also
3558 for x_7. */
3559 if (is_unreachable
3560 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
3561 single_pred (bb)))
3563 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
3564 SSA_NAME_RANGE_INFO (lhs)->get_min (),
3565 SSA_NAME_RANGE_INFO (lhs)->get_max ());
3566 maybe_set_nonzero_bits (single_pred_edge (bb), var);
3570 /* Propagate the RHS into every use of the LHS. For SSA names
3571 also propagate abnormals as it merely restores the original
3572 IL in this case (an replace_uses_by would assert). */
3573 if (TREE_CODE (var) == SSA_NAME)
3575 imm_use_iterator iter;
3576 use_operand_p use_p;
3577 gimple *use_stmt;
3578 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
3579 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3580 SET_USE (use_p, var);
3582 else
3583 replace_uses_by (lhs, var);
3585 /* And finally, remove the copy, it is not needed. */
3586 gsi_remove (&si, true);
3587 release_defs (stmt);
3589 else
3591 if (!is_gimple_debug (gsi_stmt (si)))
3592 is_unreachable = 0;
3593 gsi_next (&si);
3598 /* Return true if STMT is interesting for VRP. */
3600 bool
3601 stmt_interesting_for_vrp (gimple *stmt)
3603 if (gimple_code (stmt) == GIMPLE_PHI)
3605 tree res = gimple_phi_result (stmt);
3606 return (!virtual_operand_p (res)
3607 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
3608 || POINTER_TYPE_P (TREE_TYPE (res))));
3610 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
3612 tree lhs = gimple_get_lhs (stmt);
3614 /* In general, assignments with virtual operands are not useful
3615 for deriving ranges, with the obvious exception of calls to
3616 builtin functions. */
3617 if (lhs && TREE_CODE (lhs) == SSA_NAME
3618 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
3619 || POINTER_TYPE_P (TREE_TYPE (lhs)))
3620 && (is_gimple_call (stmt)
3621 || !gimple_vuse (stmt)))
3622 return true;
3623 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
3624 switch (gimple_call_internal_fn (stmt))
3626 case IFN_ADD_OVERFLOW:
3627 case IFN_SUB_OVERFLOW:
3628 case IFN_MUL_OVERFLOW:
3629 case IFN_ATOMIC_COMPARE_EXCHANGE:
3630 /* These internal calls return _Complex integer type,
3631 but are interesting to VRP nevertheless. */
3632 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3633 return true;
3634 break;
3635 default:
3636 break;
3639 else if (gimple_code (stmt) == GIMPLE_COND
3640 || gimple_code (stmt) == GIMPLE_SWITCH)
3641 return true;
3643 return false;
3646 /* Initialization required by ssa_propagate engine. */
3648 void
3649 vrp_prop::vrp_initialize (struct function *fn)
3651 basic_block bb;
3652 fun = fn;
3654 FOR_EACH_BB_FN (bb, fun)
3656 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
3657 gsi_next (&si))
3659 gphi *phi = si.phi ();
3660 if (!stmt_interesting_for_vrp (phi))
3662 tree lhs = PHI_RESULT (phi);
3663 set_def_to_varying (lhs);
3664 prop_set_simulate_again (phi, false);
3666 else
3667 prop_set_simulate_again (phi, true);
3670 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
3671 gsi_next (&si))
3673 gimple *stmt = gsi_stmt (si);
3675 /* If the statement is a control insn, then we do not
3676 want to avoid simulating the statement once. Failure
3677 to do so means that those edges will never get added. */
3678 if (stmt_ends_bb_p (stmt))
3679 prop_set_simulate_again (stmt, true);
3680 else if (!stmt_interesting_for_vrp (stmt))
3682 set_defs_to_varying (stmt);
3683 prop_set_simulate_again (stmt, false);
3685 else
3686 prop_set_simulate_again (stmt, true);
3691 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
3692 that includes the value VAL. The search is restricted to the range
3693 [START_IDX, n - 1] where n is the size of VEC.
3695 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
3696 returned.
3698 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
3699 it is placed in IDX and false is returned.
3701 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
3702 returned. */
3704 bool
3705 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
3707 size_t n = gimple_switch_num_labels (stmt);
3708 size_t low, high;
3710 /* Find case label for minimum of the value range or the next one.
3711 At each iteration we are searching in [low, high - 1]. */
3713 for (low = start_idx, high = n; high != low; )
3715 tree t;
3716 int cmp;
3717 /* Note that i != high, so we never ask for n. */
3718 size_t i = (high + low) / 2;
3719 t = gimple_switch_label (stmt, i);
3721 /* Cache the result of comparing CASE_LOW and val. */
3722 cmp = tree_int_cst_compare (CASE_LOW (t), val);
3724 if (cmp == 0)
3726 /* Ranges cannot be empty. */
3727 *idx = i;
3728 return true;
3730 else if (cmp > 0)
3731 high = i;
3732 else
3734 low = i + 1;
3735 if (CASE_HIGH (t) != NULL
3736 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
3738 *idx = i;
3739 return true;
3744 *idx = high;
3745 return false;
3748 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
3749 for values between MIN and MAX. The first index is placed in MIN_IDX. The
3750 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
3751 then MAX_IDX < MIN_IDX.
3752 Returns true if the default label is not needed. */
3754 bool
3755 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
3756 size_t *max_idx)
3758 size_t i, j;
3759 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
3760 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
3762 if (i == j
3763 && min_take_default
3764 && max_take_default)
3766 /* Only the default case label reached.
3767 Return an empty range. */
3768 *min_idx = 1;
3769 *max_idx = 0;
3770 return false;
3772 else
3774 bool take_default = min_take_default || max_take_default;
3775 tree low, high;
3776 size_t k;
3778 if (max_take_default)
3779 j--;
3781 /* If the case label range is continuous, we do not need
3782 the default case label. Verify that. */
3783 high = CASE_LOW (gimple_switch_label (stmt, i));
3784 if (CASE_HIGH (gimple_switch_label (stmt, i)))
3785 high = CASE_HIGH (gimple_switch_label (stmt, i));
3786 for (k = i + 1; k <= j; ++k)
3788 low = CASE_LOW (gimple_switch_label (stmt, k));
3789 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
3791 take_default = true;
3792 break;
3794 high = low;
3795 if (CASE_HIGH (gimple_switch_label (stmt, k)))
3796 high = CASE_HIGH (gimple_switch_label (stmt, k));
3799 *min_idx = i;
3800 *max_idx = j;
3801 return !take_default;
3805 /* Given a SWITCH_STMT, return the case label that encompasses the
3806 known possible values for the switch operand. RANGE_OF_OP is a
3807 range for the known values of the switch operand. */
3809 tree
3810 find_case_label_range (gswitch *switch_stmt, const irange *range_of_op)
3812 if (range_of_op->undefined_p ()
3813 || range_of_op->varying_p ()
3814 || range_of_op->symbolic_p ())
3815 return NULL_TREE;
3817 size_t i, j;
3818 tree op = gimple_switch_index (switch_stmt);
3819 tree type = TREE_TYPE (op);
3820 tree tmin = wide_int_to_tree (type, range_of_op->lower_bound ());
3821 tree tmax = wide_int_to_tree (type, range_of_op->upper_bound ());
3822 find_case_label_range (switch_stmt, tmin, tmax, &i, &j);
3823 if (i == j)
3825 /* Look for exactly one label that encompasses the range of
3826 the operand. */
3827 tree label = gimple_switch_label (switch_stmt, i);
3828 tree case_high
3829 = CASE_HIGH (label) ? CASE_HIGH (label) : CASE_LOW (label);
3830 int_range_max label_range (CASE_LOW (label), case_high);
3831 if (!types_compatible_p (label_range.type (), range_of_op->type ()))
3832 range_cast (label_range, range_of_op->type ());
3833 label_range.intersect (range_of_op);
3834 if (label_range == *range_of_op)
3835 return label;
3837 else if (i > j)
3839 /* If there are no labels at all, take the default. */
3840 return gimple_switch_label (switch_stmt, 0);
3842 else
3844 /* Otherwise, there are various labels that can encompass
3845 the range of operand. In which case, see if the range of
3846 the operand is entirely *outside* the bounds of all the
3847 (non-default) case labels. If so, take the default. */
3848 unsigned n = gimple_switch_num_labels (switch_stmt);
3849 tree min_label = gimple_switch_label (switch_stmt, 1);
3850 tree max_label = gimple_switch_label (switch_stmt, n - 1);
3851 tree case_high = CASE_HIGH (max_label);
3852 if (!case_high)
3853 case_high = CASE_LOW (max_label);
3854 int_range_max label_range (CASE_LOW (min_label), case_high);
3855 if (!types_compatible_p (label_range.type (), range_of_op->type ()))
3856 range_cast (label_range, range_of_op->type ());
3857 label_range.intersect (range_of_op);
3858 if (label_range.undefined_p ())
3859 return gimple_switch_label (switch_stmt, 0);
3861 return NULL_TREE;
3864 /* Evaluate statement STMT. If the statement produces a useful range,
3865 return SSA_PROP_INTERESTING and record the SSA name with the
3866 interesting range into *OUTPUT_P.
3868 If STMT is a conditional branch and we can determine its truth
3869 value, the taken edge is recorded in *TAKEN_EDGE_P.
3871 If STMT produces a varying value, return SSA_PROP_VARYING. */
3873 enum ssa_prop_result
3874 vrp_prop::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
3876 tree lhs = gimple_get_lhs (stmt);
3877 value_range_equiv vr;
3878 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
3880 if (*output_p)
3882 if (update_value_range (*output_p, &vr))
3884 if (dump_file && (dump_flags & TDF_DETAILS))
3886 fprintf (dump_file, "Found new range for ");
3887 print_generic_expr (dump_file, *output_p);
3888 fprintf (dump_file, ": ");
3889 dump_value_range (dump_file, &vr);
3890 fprintf (dump_file, "\n");
3893 if (vr.varying_p ())
3894 return SSA_PROP_VARYING;
3896 return SSA_PROP_INTERESTING;
3898 return SSA_PROP_NOT_INTERESTING;
3901 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
3902 switch (gimple_call_internal_fn (stmt))
3904 case IFN_ADD_OVERFLOW:
3905 case IFN_SUB_OVERFLOW:
3906 case IFN_MUL_OVERFLOW:
3907 case IFN_ATOMIC_COMPARE_EXCHANGE:
3908 /* These internal calls return _Complex integer type,
3909 which VRP does not track, but the immediate uses
3910 thereof might be interesting. */
3911 if (lhs && TREE_CODE (lhs) == SSA_NAME)
3913 imm_use_iterator iter;
3914 use_operand_p use_p;
3915 enum ssa_prop_result res = SSA_PROP_VARYING;
3917 set_def_to_varying (lhs);
3919 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
3921 gimple *use_stmt = USE_STMT (use_p);
3922 if (!is_gimple_assign (use_stmt))
3923 continue;
3924 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
3925 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
3926 continue;
3927 tree rhs1 = gimple_assign_rhs1 (use_stmt);
3928 tree use_lhs = gimple_assign_lhs (use_stmt);
3929 if (TREE_CODE (rhs1) != rhs_code
3930 || TREE_OPERAND (rhs1, 0) != lhs
3931 || TREE_CODE (use_lhs) != SSA_NAME
3932 || !stmt_interesting_for_vrp (use_stmt)
3933 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
3934 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
3935 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
3936 continue;
3938 /* If there is a change in the value range for any of the
3939 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
3940 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
3941 or IMAGPART_EXPR immediate uses, but none of them have
3942 a change in their value ranges, return
3943 SSA_PROP_NOT_INTERESTING. If there are no
3944 {REAL,IMAG}PART_EXPR uses at all,
3945 return SSA_PROP_VARYING. */
3946 value_range_equiv new_vr;
3947 extract_range_basic (&new_vr, use_stmt);
3948 const value_range_equiv *old_vr = get_value_range (use_lhs);
3949 if (!old_vr->equal_p (new_vr, /*ignore_equivs=*/false))
3950 res = SSA_PROP_INTERESTING;
3951 else
3952 res = SSA_PROP_NOT_INTERESTING;
3953 new_vr.equiv_clear ();
3954 if (res == SSA_PROP_INTERESTING)
3956 *output_p = lhs;
3957 return res;
3961 return res;
3963 break;
3964 default:
3965 break;
3968 /* All other statements produce nothing of interest for VRP, so mark
3969 their outputs varying and prevent further simulation. */
3970 set_defs_to_varying (stmt);
3972 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
3975 /* Visit all arguments for PHI node PHI that flow through executable
3976 edges. If a valid value range can be derived from all the incoming
3977 value ranges, set a new range for the LHS of PHI. */
3979 enum ssa_prop_result
3980 vrp_prop::visit_phi (gphi *phi)
3982 tree lhs = PHI_RESULT (phi);
3983 value_range_equiv vr_result;
3984 extract_range_from_phi_node (phi, &vr_result);
3985 if (update_value_range (lhs, &vr_result))
3987 if (dump_file && (dump_flags & TDF_DETAILS))
3989 fprintf (dump_file, "Found new range for ");
3990 print_generic_expr (dump_file, lhs);
3991 fprintf (dump_file, ": ");
3992 dump_value_range (dump_file, &vr_result);
3993 fprintf (dump_file, "\n");
3996 if (vr_result.varying_p ())
3997 return SSA_PROP_VARYING;
3999 return SSA_PROP_INTERESTING;
4002 /* Nothing changed, don't add outgoing edges. */
4003 return SSA_PROP_NOT_INTERESTING;
4006 class vrp_folder : public substitute_and_fold_engine
4008 public:
4009 vrp_folder (vr_values *v)
4010 : substitute_and_fold_engine (/* Fold all stmts. */ true),
4011 m_vr_values (v), simplifier (v)
4013 bool fold_stmt (gimple_stmt_iterator *) FINAL OVERRIDE;
4015 tree value_of_expr (tree name, gimple *stmt) OVERRIDE
4017 return m_vr_values->value_of_expr (name, stmt);
4019 class vr_values *m_vr_values;
4021 private:
4022 bool fold_predicate_in (gimple_stmt_iterator *);
4023 /* Delegators. */
4024 tree vrp_evaluate_conditional (tree_code code, tree op0,
4025 tree op1, gimple *stmt)
4026 { return simplifier.vrp_evaluate_conditional (code, op0, op1, stmt); }
4027 bool simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
4028 { return simplifier.simplify (gsi); }
4030 simplify_using_ranges simplifier;
4033 /* If the statement pointed by SI has a predicate whose value can be
4034 computed using the value range information computed by VRP, compute
4035 its value and return true. Otherwise, return false. */
4037 bool
4038 vrp_folder::fold_predicate_in (gimple_stmt_iterator *si)
4040 bool assignment_p = false;
4041 tree val;
4042 gimple *stmt = gsi_stmt (*si);
4044 if (is_gimple_assign (stmt)
4045 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
4047 assignment_p = true;
4048 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
4049 gimple_assign_rhs1 (stmt),
4050 gimple_assign_rhs2 (stmt),
4051 stmt);
4053 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
4054 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
4055 gimple_cond_lhs (cond_stmt),
4056 gimple_cond_rhs (cond_stmt),
4057 stmt);
4058 else
4059 return false;
4061 if (val)
4063 if (assignment_p)
4064 val = fold_convert (gimple_expr_type (stmt), val);
4066 if (dump_file)
4068 fprintf (dump_file, "Folding predicate ");
4069 print_gimple_expr (dump_file, stmt, 0);
4070 fprintf (dump_file, " to ");
4071 print_generic_expr (dump_file, val);
4072 fprintf (dump_file, "\n");
4075 if (is_gimple_assign (stmt))
4076 gimple_assign_set_rhs_from_tree (si, val);
4077 else
4079 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
4080 gcond *cond_stmt = as_a <gcond *> (stmt);
4081 if (integer_zerop (val))
4082 gimple_cond_make_false (cond_stmt);
4083 else if (integer_onep (val))
4084 gimple_cond_make_true (cond_stmt);
4085 else
4086 gcc_unreachable ();
4089 return true;
4092 return false;
4095 /* Callback for substitute_and_fold folding the stmt at *SI. */
4097 bool
4098 vrp_folder::fold_stmt (gimple_stmt_iterator *si)
4100 if (fold_predicate_in (si))
4101 return true;
4103 return simplify_stmt_using_ranges (si);
4106 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
4107 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
4108 BB. If no such ASSERT_EXPR is found, return OP. */
4110 static tree
4111 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
4113 imm_use_iterator imm_iter;
4114 gimple *use_stmt;
4115 use_operand_p use_p;
4117 if (TREE_CODE (op) == SSA_NAME)
4119 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
4121 use_stmt = USE_STMT (use_p);
4122 if (use_stmt != stmt
4123 && gimple_assign_single_p (use_stmt)
4124 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
4125 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
4126 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
4127 return gimple_assign_lhs (use_stmt);
4130 return op;
4133 /* A hack. */
4134 static class vr_values *x_vr_values;
4136 /* A trivial wrapper so that we can present the generic jump threading
4137 code with a simple API for simplifying statements. STMT is the
4138 statement we want to simplify, WITHIN_STMT provides the location
4139 for any overflow warnings.
4141 ?? This should be cleaned up. There's a virtually identical copy
4142 of this function in tree-ssa-dom.c. */
4144 static tree
4145 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
4146 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED,
4147 basic_block bb)
4149 /* First see if the conditional is in the hash table. */
4150 tree cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, false, true);
4151 if (cached_lhs && is_gimple_min_invariant (cached_lhs))
4152 return cached_lhs;
4154 vr_values *vr_values = x_vr_values;
4155 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
4157 tree op0 = gimple_cond_lhs (cond_stmt);
4158 op0 = lhs_of_dominating_assert (op0, bb, stmt);
4160 tree op1 = gimple_cond_rhs (cond_stmt);
4161 op1 = lhs_of_dominating_assert (op1, bb, stmt);
4163 simplify_using_ranges simplifier (vr_values);
4164 return simplifier.vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
4165 op0, op1, within_stmt);
4168 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
4170 tree op = gimple_switch_index (switch_stmt);
4171 if (TREE_CODE (op) != SSA_NAME)
4172 return NULL_TREE;
4174 op = lhs_of_dominating_assert (op, bb, stmt);
4176 const value_range_equiv *vr = vr_values->get_value_range (op);
4177 return find_case_label_range (switch_stmt, vr);
4180 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
4182 tree lhs = gimple_assign_lhs (assign_stmt);
4183 if (TREE_CODE (lhs) == SSA_NAME
4184 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4185 || POINTER_TYPE_P (TREE_TYPE (lhs)))
4186 && stmt_interesting_for_vrp (stmt))
4188 edge dummy_e;
4189 tree dummy_tree;
4190 value_range_equiv new_vr;
4191 vr_values->extract_range_from_stmt (stmt, &dummy_e,
4192 &dummy_tree, &new_vr);
4193 tree singleton;
4194 if (new_vr.singleton_p (&singleton))
4195 return singleton;
4199 return NULL_TREE;
4202 class vrp_dom_walker : public dom_walker
4204 public:
4205 vrp_dom_walker (cdi_direction direction,
4206 class const_and_copies *const_and_copies,
4207 class avail_exprs_stack *avail_exprs_stack)
4208 : dom_walker (direction, REACHABLE_BLOCKS),
4209 m_const_and_copies (const_and_copies),
4210 m_avail_exprs_stack (avail_exprs_stack),
4211 m_dummy_cond (NULL) {}
4213 virtual edge before_dom_children (basic_block);
4214 virtual void after_dom_children (basic_block);
4216 class vr_values *vr_values;
4218 private:
4219 class const_and_copies *m_const_and_copies;
4220 class avail_exprs_stack *m_avail_exprs_stack;
4222 gcond *m_dummy_cond;
4226 /* Called before processing dominator children of BB. We want to look
4227 at ASSERT_EXPRs and record information from them in the appropriate
4228 tables.
4230 We could look at other statements here. It's not seen as likely
4231 to significantly increase the jump threads we discover. */
4233 edge
4234 vrp_dom_walker::before_dom_children (basic_block bb)
4236 gimple_stmt_iterator gsi;
4238 m_avail_exprs_stack->push_marker ();
4239 m_const_and_copies->push_marker ();
4240 for (gsi = gsi_start_nondebug_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
4242 gimple *stmt = gsi_stmt (gsi);
4243 if (gimple_assign_single_p (stmt)
4244 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
4246 tree rhs1 = gimple_assign_rhs1 (stmt);
4247 tree cond = TREE_OPERAND (rhs1, 1);
4248 tree inverted = invert_truthvalue (cond);
4249 vec<cond_equivalence> p;
4250 p.create (3);
4251 record_conditions (&p, cond, inverted);
4252 for (unsigned int i = 0; i < p.length (); i++)
4253 m_avail_exprs_stack->record_cond (&p[i]);
4255 tree lhs = gimple_assign_lhs (stmt);
4256 m_const_and_copies->record_const_or_copy (lhs,
4257 TREE_OPERAND (rhs1, 0));
4258 p.release ();
4259 continue;
4261 break;
4263 return NULL;
4266 /* Called after processing dominator children of BB. This is where we
4267 actually call into the threader. */
4268 void
4269 vrp_dom_walker::after_dom_children (basic_block bb)
4271 if (!m_dummy_cond)
4272 m_dummy_cond = gimple_build_cond (NE_EXPR,
4273 integer_zero_node, integer_zero_node,
4274 NULL, NULL);
4276 x_vr_values = vr_values;
4277 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
4278 m_avail_exprs_stack, NULL,
4279 simplify_stmt_for_jump_threading);
4280 x_vr_values = NULL;
4282 m_avail_exprs_stack->pop_to_marker ();
4283 m_const_and_copies->pop_to_marker ();
4286 /* Blocks which have more than one predecessor and more than
4287 one successor present jump threading opportunities, i.e.,
4288 when the block is reached from a specific predecessor, we
4289 may be able to determine which of the outgoing edges will
4290 be traversed. When this optimization applies, we are able
4291 to avoid conditionals at runtime and we may expose secondary
4292 optimization opportunities.
4294 This routine is effectively a driver for the generic jump
4295 threading code. It basically just presents the generic code
4296 with edges that may be suitable for jump threading.
4298 Unlike DOM, we do not iterate VRP if jump threading was successful.
4299 While iterating may expose new opportunities for VRP, it is expected
4300 those opportunities would be very limited and the compile time cost
4301 to expose those opportunities would be significant.
4303 As jump threading opportunities are discovered, they are registered
4304 for later realization. */
4306 static void
4307 identify_jump_threads (struct function *fun, class vr_values *vr_values)
4309 /* Ugh. When substituting values earlier in this pass we can
4310 wipe the dominance information. So rebuild the dominator
4311 information as we need it within the jump threading code. */
4312 calculate_dominance_info (CDI_DOMINATORS);
4314 /* We do not allow VRP information to be used for jump threading
4315 across a back edge in the CFG. Otherwise it becomes too
4316 difficult to avoid eliminating loop exit tests. Of course
4317 EDGE_DFS_BACK is not accurate at this time so we have to
4318 recompute it. */
4319 mark_dfs_back_edges ();
4321 /* Allocate our unwinder stack to unwind any temporary equivalences
4322 that might be recorded. */
4323 const_and_copies *equiv_stack = new const_and_copies ();
4325 hash_table<expr_elt_hasher> *avail_exprs
4326 = new hash_table<expr_elt_hasher> (1024);
4327 avail_exprs_stack *avail_exprs_stack
4328 = new class avail_exprs_stack (avail_exprs);
4330 vrp_dom_walker walker (CDI_DOMINATORS, equiv_stack, avail_exprs_stack);
4331 walker.vr_values = vr_values;
4332 walker.walk (fun->cfg->x_entry_block_ptr);
4334 /* We do not actually update the CFG or SSA graphs at this point as
4335 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
4336 handle ASSERT_EXPRs gracefully. */
4337 delete equiv_stack;
4338 delete avail_exprs;
4339 delete avail_exprs_stack;
4342 /* Traverse all the blocks folding conditionals with known ranges. */
4344 void
4345 vrp_prop::vrp_finalize (vrp_folder *folder, bool warn_array_bounds_p)
4347 size_t i;
4349 /* We have completed propagating through the lattice. */
4350 vr_values.set_lattice_propagation_complete ();
4352 if (dump_file)
4354 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
4355 vr_values.dump_all_value_ranges (dump_file);
4356 fprintf (dump_file, "\n");
4359 /* Set value range to non pointer SSA_NAMEs. */
4360 for (i = 0; i < num_ssa_names; i++)
4362 tree name = ssa_name (i);
4363 if (!name)
4364 continue;
4366 const value_range_equiv *vr = get_value_range (name);
4367 if (!name || !vr->constant_p ())
4368 continue;
4370 if (POINTER_TYPE_P (TREE_TYPE (name))
4371 && range_includes_zero_p (vr) == 0)
4372 set_ptr_nonnull (name);
4373 else if (!POINTER_TYPE_P (TREE_TYPE (name)))
4374 set_range_info (name, *vr);
4377 /* If we're checking array refs, we want to merge information on
4378 the executability of each edge between vrp_folder and the
4379 check_array_bounds_dom_walker: each can clear the
4380 EDGE_EXECUTABLE flag on edges, in different ways.
4382 Hence, if we're going to call check_all_array_refs, set
4383 the flag on every edge now, rather than in
4384 check_array_bounds_dom_walker's ctor; vrp_folder may clear
4385 it from some edges. */
4386 if (warn_array_bounds && warn_array_bounds_p)
4387 set_all_edges_as_executable (fun);
4389 folder->substitute_and_fold ();
4391 if (warn_array_bounds && warn_array_bounds_p)
4393 array_bounds_checker array_checker (fun, &vr_values);
4394 array_checker.check ();
4398 /* STMT is a conditional at the end of a basic block.
4400 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
4401 was set via a type conversion, try to replace the SSA_NAME with the RHS
4402 of the type conversion. Doing so makes the conversion dead which helps
4403 subsequent passes. */
4405 static void
4406 vrp_simplify_cond_using_ranges (vr_values *query, gcond *stmt)
4408 tree op0 = gimple_cond_lhs (stmt);
4409 tree op1 = gimple_cond_rhs (stmt);
4411 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
4412 see if OP0 was set by a type conversion where the source of
4413 the conversion is another SSA_NAME with a range that fits
4414 into the range of OP0's type.
4416 If so, the conversion is redundant as the earlier SSA_NAME can be
4417 used for the comparison directly if we just massage the constant in the
4418 comparison. */
4419 if (TREE_CODE (op0) == SSA_NAME
4420 && TREE_CODE (op1) == INTEGER_CST)
4422 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
4423 tree innerop;
4425 if (!is_gimple_assign (def_stmt)
4426 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
4427 return;
4429 innerop = gimple_assign_rhs1 (def_stmt);
4431 if (TREE_CODE (innerop) == SSA_NAME
4432 && !POINTER_TYPE_P (TREE_TYPE (innerop))
4433 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
4434 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
4436 const value_range *vr = query->get_value_range (innerop);
4438 if (range_int_cst_p (vr)
4439 && range_fits_type_p (vr,
4440 TYPE_PRECISION (TREE_TYPE (op0)),
4441 TYPE_SIGN (TREE_TYPE (op0)))
4442 && int_fits_type_p (op1, TREE_TYPE (innerop)))
4444 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
4445 gimple_cond_set_lhs (stmt, innerop);
4446 gimple_cond_set_rhs (stmt, newconst);
4447 update_stmt (stmt);
4448 if (dump_file && (dump_flags & TDF_DETAILS))
4450 fprintf (dump_file, "Folded into: ");
4451 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
4452 fprintf (dump_file, "\n");
4459 /* Main entry point to VRP (Value Range Propagation). This pass is
4460 loosely based on J. R. C. Patterson, ``Accurate Static Branch
4461 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
4462 Programming Language Design and Implementation, pp. 67-78, 1995.
4463 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
4465 This is essentially an SSA-CCP pass modified to deal with ranges
4466 instead of constants.
4468 While propagating ranges, we may find that two or more SSA name
4469 have equivalent, though distinct ranges. For instance,
4471 1 x_9 = p_3->a;
4472 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
4473 3 if (p_4 == q_2)
4474 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
4475 5 endif
4476 6 if (q_2)
4478 In the code above, pointer p_5 has range [q_2, q_2], but from the
4479 code we can also determine that p_5 cannot be NULL and, if q_2 had
4480 a non-varying range, p_5's range should also be compatible with it.
4482 These equivalences are created by two expressions: ASSERT_EXPR and
4483 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
4484 result of another assertion, then we can use the fact that p_5 and
4485 p_4 are equivalent when evaluating p_5's range.
4487 Together with value ranges, we also propagate these equivalences
4488 between names so that we can take advantage of information from
4489 multiple ranges when doing final replacement. Note that this
4490 equivalency relation is transitive but not symmetric.
4492 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
4493 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
4494 in contexts where that assertion does not hold (e.g., in line 6).
4496 TODO, the main difference between this pass and Patterson's is that
4497 we do not propagate edge probabilities. We only compute whether
4498 edges can be taken or not. That is, instead of having a spectrum
4499 of jump probabilities between 0 and 1, we only deal with 0, 1 and
4500 DON'T KNOW. In the future, it may be worthwhile to propagate
4501 probabilities to aid branch prediction. */
4503 static unsigned int
4504 execute_vrp (struct function *fun, bool warn_array_bounds_p)
4507 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
4508 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
4509 scev_initialize ();
4511 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
4512 Inserting assertions may split edges which will invalidate
4513 EDGE_DFS_BACK. */
4514 vrp_insert assert_engine (fun);
4515 assert_engine.insert_range_assertions ();
4517 threadedge_initialize_values ();
4519 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
4520 mark_dfs_back_edges ();
4522 class vrp_prop vrp_prop;
4523 vrp_prop.vrp_initialize (fun);
4524 vrp_prop.ssa_propagate ();
4525 /* Instantiate the folder here, so that edge cleanups happen at the
4526 end of this function. */
4527 vrp_folder folder (&vrp_prop.vr_values);
4528 vrp_prop.vrp_finalize (&folder, warn_array_bounds_p);
4530 /* We must identify jump threading opportunities before we release
4531 the datastructures built by VRP. */
4532 identify_jump_threads (fun, &vrp_prop.vr_values);
4534 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
4535 was set by a type conversion can often be rewritten to use the
4536 RHS of the type conversion.
4538 However, doing so inhibits jump threading through the comparison.
4539 So that transformation is not performed until after jump threading
4540 is complete. */
4541 basic_block bb;
4542 FOR_EACH_BB_FN (bb, fun)
4544 gimple *last = last_stmt (bb);
4545 if (last && gimple_code (last) == GIMPLE_COND)
4546 vrp_simplify_cond_using_ranges (&vrp_prop.vr_values,
4547 as_a <gcond *> (last));
4550 free_numbers_of_iterations_estimates (fun);
4552 /* ASSERT_EXPRs must be removed before finalizing jump threads
4553 as finalizing jump threads calls the CFG cleanup code which
4554 does not properly handle ASSERT_EXPRs. */
4555 assert_engine.remove_range_assertions ();
4557 /* If we exposed any new variables, go ahead and put them into
4558 SSA form now, before we handle jump threading. This simplifies
4559 interactions between rewriting of _DECL nodes into SSA form
4560 and rewriting SSA_NAME nodes into SSA form after block
4561 duplication and CFG manipulation. */
4562 update_ssa (TODO_update_ssa);
4564 /* We identified all the jump threading opportunities earlier, but could
4565 not transform the CFG at that time. This routine transforms the
4566 CFG and arranges for the dominator tree to be rebuilt if necessary.
4568 Note the SSA graph update will occur during the normal TODO
4569 processing by the pass manager. */
4570 thread_through_all_blocks (false);
4572 threadedge_finalize_values ();
4574 scev_finalize ();
4575 loop_optimizer_finalize ();
4576 return 0;
4579 namespace {
4581 const pass_data pass_data_vrp =
4583 GIMPLE_PASS, /* type */
4584 "vrp", /* name */
4585 OPTGROUP_NONE, /* optinfo_flags */
4586 TV_TREE_VRP, /* tv_id */
4587 PROP_ssa, /* properties_required */
4588 0, /* properties_provided */
4589 0, /* properties_destroyed */
4590 0, /* todo_flags_start */
4591 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
4594 class pass_vrp : public gimple_opt_pass
4596 public:
4597 pass_vrp (gcc::context *ctxt)
4598 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
4601 /* opt_pass methods: */
4602 opt_pass * clone () { return new pass_vrp (m_ctxt); }
4603 void set_pass_param (unsigned int n, bool param)
4605 gcc_assert (n == 0);
4606 warn_array_bounds_p = param;
4608 virtual bool gate (function *) { return flag_tree_vrp != 0; }
4609 virtual unsigned int execute (function *fun)
4610 { return execute_vrp (fun, warn_array_bounds_p); }
4612 private:
4613 bool warn_array_bounds_p;
4614 }; // class pass_vrp
4616 } // anon namespace
4618 gimple_opt_pass *
4619 make_pass_vrp (gcc::context *ctxt)
4621 return new pass_vrp (ctxt);
4625 /* Worker for determine_value_range. */
4627 static void
4628 determine_value_range_1 (value_range *vr, tree expr)
4630 if (BINARY_CLASS_P (expr))
4632 value_range vr0, vr1;
4633 determine_value_range_1 (&vr0, TREE_OPERAND (expr, 0));
4634 determine_value_range_1 (&vr1, TREE_OPERAND (expr, 1));
4635 range_fold_binary_expr (vr, TREE_CODE (expr), TREE_TYPE (expr),
4636 &vr0, &vr1);
4638 else if (UNARY_CLASS_P (expr))
4640 value_range vr0;
4641 determine_value_range_1 (&vr0, TREE_OPERAND (expr, 0));
4642 range_fold_unary_expr (vr, TREE_CODE (expr), TREE_TYPE (expr),
4643 &vr0, TREE_TYPE (TREE_OPERAND (expr, 0)));
4645 else if (TREE_CODE (expr) == INTEGER_CST)
4646 vr->set (expr);
4647 else
4649 value_range_kind kind;
4650 wide_int min, max;
4651 /* For SSA names try to extract range info computed by VRP. Otherwise
4652 fall back to varying. */
4653 if (TREE_CODE (expr) == SSA_NAME
4654 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
4655 && (kind = get_range_info (expr, &min, &max)) != VR_VARYING)
4656 vr->set (wide_int_to_tree (TREE_TYPE (expr), min),
4657 wide_int_to_tree (TREE_TYPE (expr), max),
4658 kind);
4659 else
4660 vr->set_varying (TREE_TYPE (expr));
4664 /* Compute a value-range for EXPR and set it in *MIN and *MAX. Return
4665 the determined range type. */
4667 value_range_kind
4668 determine_value_range (tree expr, wide_int *min, wide_int *max)
4670 value_range vr;
4671 determine_value_range_1 (&vr, expr);
4672 if (vr.constant_p ())
4674 *min = wi::to_wide (vr.min ());
4675 *max = wi::to_wide (vr.max ());
4676 return vr.kind ();
4679 return VR_VARYING;