c++: ICE on unviable/ambiguous constrained dtors [PR96745]
[official-gcc.git] / gcc / tree-vrp.cc
blob3c431760a16aa4ad48ca7914d3813fafcfe04205
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2023 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 "basic-block.h"
25 #include "bitmap.h"
26 #include "sbitmap.h"
27 #include "options.h"
28 #include "dominance.h"
29 #include "function.h"
30 #include "cfg.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "tree-pass.h"
34 #include "ssa.h"
35 #include "gimple-pretty-print.h"
36 #include "fold-const.h"
37 #include "cfganal.h"
38 #include "gimple-iterator.h"
39 #include "tree-cfg.h"
40 #include "tree-ssa-loop-manip.h"
41 #include "tree-ssa-loop-niter.h"
42 #include "tree-into-ssa.h"
43 #include "cfgloop.h"
44 #include "tree-scalar-evolution.h"
45 #include "tree-ssa-propagate.h"
46 #include "domwalk.h"
47 #include "vr-values.h"
48 #include "gimple-array-bounds.h"
49 #include "gimple-range.h"
50 #include "gimple-range-path.h"
51 #include "value-pointer-equiv.h"
52 #include "gimple-fold.h"
53 #include "tree-dfa.h"
54 #include "tree-ssa-dce.h"
56 // This class is utilized by VRP and ranger to remove __builtin_unreachable
57 // calls, and reflect any resulting global ranges.
59 // maybe_register_block () is called on basic blocks, and if that block
60 // matches the pattern of one branch being a builtin_unreachable, register
61 // the resulting executable edge in a list.
63 // After all blocks have been processed, remove_and_update_globals() will
64 // - check all exports from registered blocks
65 // - ensure the cache entry of each export is set with the appropriate range
66 // - rewrite the conditions to take the executable edge
67 // - perform DCE on any feeding instructions to those rewritten conditions
69 // Then each of the immediate use chain of each export is walked, and a new
70 // global range created by unioning the ranges at all remaining use locations.
72 class remove_unreachable {
73 public:
74 remove_unreachable (gimple_ranger &r) : m_ranger (r) { m_list.create (30); }
75 ~remove_unreachable () { m_list.release (); }
76 void maybe_register_block (basic_block bb);
77 bool remove_and_update_globals (bool final_p);
78 vec<edge> m_list;
79 gimple_ranger &m_ranger;
82 // Check if block BB has a __builtin_unreachable () call on one arm, and
83 // register the executable edge if so.
85 void
86 remove_unreachable::maybe_register_block (basic_block bb)
88 gimple *s = gimple_outgoing_range_stmt_p (bb);
89 if (!s || gimple_code (s) != GIMPLE_COND)
90 return;
92 edge e0 = EDGE_SUCC (bb, 0);
93 basic_block bb0 = e0->dest;
94 bool un0 = EDGE_COUNT (bb0->succs) == 0
95 && gimple_seq_unreachable_p (bb_seq (bb0));
96 edge e1 = EDGE_SUCC (bb, 1);
97 basic_block bb1 = e1->dest;
98 bool un1 = EDGE_COUNT (bb1->succs) == 0
99 && gimple_seq_unreachable_p (bb_seq (bb1));
101 // If the 2 blocks are not different, ignore.
102 if (un0 == un1)
103 return;
105 if (un0)
106 m_list.safe_push (e1);
107 else
108 m_list.safe_push (e0);
111 // Process the edges in the list, change the conditions and removing any
112 // dead code feeding those conditions. Calculate the range of any
113 // names that may have been exported from those blocks, and determine if
114 // there is any updates to their global ranges..
115 // FINAL_P indicates all builtin_unreachable calls should be removed.
116 // Return true if any builtin_unreachables/globals eliminated/updated.
118 bool
119 remove_unreachable::remove_and_update_globals (bool final_p)
121 if (m_list.length () == 0)
122 return false;
124 bool change = false;
125 tree name;
126 unsigned i;
127 bitmap_iterator bi;
128 auto_bitmap all_exports;
129 for (i = 0; i < m_list.length (); i++)
131 edge e = m_list[i];
132 gimple *s = gimple_outgoing_range_stmt_p (e->src);
133 gcc_checking_assert (gimple_code (s) == GIMPLE_COND);
134 bool lhs_p = TREE_CODE (gimple_cond_lhs (s)) == SSA_NAME;
135 bool rhs_p = TREE_CODE (gimple_cond_rhs (s)) == SSA_NAME;
136 // Do not remove __builtin_unreachable if it confers a relation, or
137 // that relation will be lost in subsequent passes. Unless its the
138 // final pass.
139 if (!final_p && lhs_p && rhs_p)
140 continue;
141 // If this is already a constant condition, don't look either
142 if (!lhs_p && !rhs_p)
143 continue;
145 bool dominate_exit_p = true;
146 FOR_EACH_GORI_EXPORT_NAME (m_ranger.gori (), e->src, name)
148 // Ensure the cache is set for NAME in the succ block.
149 Value_Range r(TREE_TYPE (name));
150 Value_Range ex(TREE_TYPE (name));
151 m_ranger.range_on_entry (r, e->dest, name);
152 m_ranger.range_on_entry (ex, EXIT_BLOCK_PTR_FOR_FN (cfun), name);
153 // If the range produced by this __builtin_unreachacble expression
154 // is not fully reflected in the range at exit, then it does not
155 // dominate the exit of the funciton.
156 if (ex.intersect (r))
157 dominate_exit_p = false;
160 // If the exit is dominated, add to the export list. Otherwise if this
161 // isn't the final VRP pass, leave the call in the IL.
162 if (dominate_exit_p)
163 bitmap_ior_into (all_exports, m_ranger.gori ().exports (e->src));
164 else if (!final_p)
165 continue;
167 change = true;
168 // Rewrite the condition.
169 if (e->flags & EDGE_TRUE_VALUE)
170 gimple_cond_make_true (as_a<gcond *> (s));
171 else
172 gimple_cond_make_false (as_a<gcond *> (s));
173 update_stmt (s);
176 if (bitmap_empty_p (all_exports))
177 return false;
178 // Invoke DCE on all exported names to elimnate dead feeding defs
179 auto_bitmap dce;
180 bitmap_copy (dce, all_exports);
181 // Don't attempt to DCE parameters.
182 EXECUTE_IF_SET_IN_BITMAP (all_exports, 0, i, bi)
183 if (!ssa_name (i) || SSA_NAME_IS_DEFAULT_DEF (ssa_name (i)))
184 bitmap_clear_bit (dce, i);
185 simple_dce_from_worklist (dce);
187 // Loop over all uses of each name and find maximal range. This is the
188 // new global range.
189 use_operand_p use_p;
190 imm_use_iterator iter;
191 EXECUTE_IF_SET_IN_BITMAP (all_exports, 0, i, bi)
193 name = ssa_name (i);
194 if (!name || SSA_NAME_IN_FREE_LIST (name))
195 continue;
196 Value_Range r (TREE_TYPE (name));
197 Value_Range exp_range (TREE_TYPE (name));
198 r.set_undefined ();
199 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
201 gimple *use_stmt = USE_STMT (use_p);
202 if (is_gimple_debug (use_stmt))
203 continue;
204 if (!m_ranger.range_of_expr (exp_range, name, use_stmt))
205 exp_range.set_varying (TREE_TYPE (name));
206 r.union_ (exp_range);
207 if (r.varying_p ())
208 break;
210 // Include the on-exit range to ensure non-dominated unreachables
211 // don't incorrectly impact the global range.
212 m_ranger.range_on_entry (exp_range, EXIT_BLOCK_PTR_FOR_FN (cfun), name);
213 r.union_ (exp_range);
214 if (r.varying_p () || r.undefined_p ())
215 continue;
216 if (!set_range_info (name, r))
217 continue;
218 change = true;
219 if (dump_file)
221 fprintf (dump_file, "Global Exported (via unreachable): ");
222 print_generic_expr (dump_file, name, TDF_SLIM);
223 fprintf (dump_file, " = ");
224 gimple_range_global (r, name);
225 r.dump (dump_file);
226 fputc ('\n', dump_file);
229 return change;
232 /* VR_TYPE describes a range with mininum value *MIN and maximum
233 value *MAX. Restrict the range to the set of values that have
234 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
235 return the new range type.
237 SGN gives the sign of the values described by the range. */
239 enum value_range_kind
240 intersect_range_with_nonzero_bits (enum value_range_kind vr_type,
241 wide_int *min, wide_int *max,
242 const wide_int &nonzero_bits,
243 signop sgn)
245 if (vr_type == VR_ANTI_RANGE)
247 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
248 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
249 to create an inclusive upper bound for A and an inclusive lower
250 bound for B. */
251 wide_int a_max = wi::round_down_for_mask (*min - 1, nonzero_bits);
252 wide_int b_min = wi::round_up_for_mask (*max + 1, nonzero_bits);
254 /* If the calculation of A_MAX wrapped, A is effectively empty
255 and A_MAX is the highest value that satisfies NONZERO_BITS.
256 Likewise if the calculation of B_MIN wrapped, B is effectively
257 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
258 bool a_empty = wi::ge_p (a_max, *min, sgn);
259 bool b_empty = wi::le_p (b_min, *max, sgn);
261 /* If both A and B are empty, there are no valid values. */
262 if (a_empty && b_empty)
263 return VR_UNDEFINED;
265 /* If exactly one of A or B is empty, return a VR_RANGE for the
266 other one. */
267 if (a_empty || b_empty)
269 *min = b_min;
270 *max = a_max;
271 gcc_checking_assert (wi::le_p (*min, *max, sgn));
272 return VR_RANGE;
275 /* Update the VR_ANTI_RANGE bounds. */
276 *min = a_max + 1;
277 *max = b_min - 1;
278 gcc_checking_assert (wi::le_p (*min, *max, sgn));
280 /* Now check whether the excluded range includes any values that
281 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
282 if (wi::round_up_for_mask (*min, nonzero_bits) == b_min)
284 unsigned int precision = min->get_precision ();
285 *min = wi::min_value (precision, sgn);
286 *max = wi::max_value (precision, sgn);
287 vr_type = VR_RANGE;
290 if (vr_type == VR_RANGE || vr_type == VR_VARYING)
292 *max = wi::round_down_for_mask (*max, nonzero_bits);
294 /* Check that the range contains at least one valid value. */
295 if (wi::gt_p (*min, *max, sgn))
296 return VR_UNDEFINED;
298 *min = wi::round_up_for_mask (*min, nonzero_bits);
299 gcc_checking_assert (wi::le_p (*min, *max, sgn));
301 return vr_type;
304 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
305 a singleton. */
307 bool
308 range_int_cst_p (const value_range *vr)
310 return (vr->kind () == VR_RANGE && range_has_numeric_bounds_p (vr));
313 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
314 otherwise. We only handle additive operations and set NEG to true if the
315 symbol is negated and INV to the invariant part, if any. */
317 static tree
318 get_single_symbol (tree t, bool *neg, tree *inv)
320 bool neg_;
321 tree inv_;
323 *inv = NULL_TREE;
324 *neg = false;
326 if (TREE_CODE (t) == PLUS_EXPR
327 || TREE_CODE (t) == POINTER_PLUS_EXPR
328 || TREE_CODE (t) == MINUS_EXPR)
330 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
332 neg_ = (TREE_CODE (t) == MINUS_EXPR);
333 inv_ = TREE_OPERAND (t, 0);
334 t = TREE_OPERAND (t, 1);
336 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
338 neg_ = false;
339 inv_ = TREE_OPERAND (t, 1);
340 t = TREE_OPERAND (t, 0);
342 else
343 return NULL_TREE;
345 else
347 neg_ = false;
348 inv_ = NULL_TREE;
351 if (TREE_CODE (t) == NEGATE_EXPR)
353 t = TREE_OPERAND (t, 0);
354 neg_ = !neg_;
357 if (TREE_CODE (t) != SSA_NAME)
358 return NULL_TREE;
360 if (inv_ && TREE_OVERFLOW_P (inv_))
361 inv_ = drop_tree_overflow (inv_);
363 *neg = neg_;
364 *inv = inv_;
365 return t;
368 /* Return
369 1 if VAL < VAL2
370 0 if !(VAL < VAL2)
371 -2 if those are incomparable. */
373 operand_less_p (tree val, tree val2)
375 /* LT is folded faster than GE and others. Inline the common case. */
376 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
377 return tree_int_cst_lt (val, val2);
378 else if (TREE_CODE (val) == SSA_NAME && TREE_CODE (val2) == SSA_NAME)
379 return val == val2 ? 0 : -2;
380 else
382 int cmp = compare_values (val, val2);
383 if (cmp == -1)
384 return 1;
385 else if (cmp == 0 || cmp == 1)
386 return 0;
387 else
388 return -2;
392 /* Compare two values VAL1 and VAL2. Return
394 -2 if VAL1 and VAL2 cannot be compared at compile-time,
395 -1 if VAL1 < VAL2,
396 0 if VAL1 == VAL2,
397 +1 if VAL1 > VAL2, and
398 +2 if VAL1 != VAL2
400 This is similar to tree_int_cst_compare but supports pointer values
401 and values that cannot be compared at compile time.
403 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
404 true if the return value is only valid if we assume that signed
405 overflow is undefined. */
408 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
410 if (val1 == val2)
411 return 0;
413 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
414 both integers. */
415 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
416 == POINTER_TYPE_P (TREE_TYPE (val2)));
418 /* Convert the two values into the same type. This is needed because
419 sizetype causes sign extension even for unsigned types. */
420 if (!useless_type_conversion_p (TREE_TYPE (val1), TREE_TYPE (val2)))
421 val2 = fold_convert (TREE_TYPE (val1), val2);
423 const bool overflow_undefined
424 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
425 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
426 tree inv1, inv2;
427 bool neg1, neg2;
428 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
429 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
431 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
432 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
433 if (sym1 && sym2)
435 /* Both values must use the same name with the same sign. */
436 if (sym1 != sym2 || neg1 != neg2)
437 return -2;
439 /* [-]NAME + CST == [-]NAME + CST. */
440 if (inv1 == inv2)
441 return 0;
443 /* If overflow is defined we cannot simplify more. */
444 if (!overflow_undefined)
445 return -2;
447 if (strict_overflow_p != NULL
448 /* Symbolic range building sets the no-warning bit to declare
449 that overflow doesn't happen. */
450 && (!inv1 || !warning_suppressed_p (val1, OPT_Woverflow))
451 && (!inv2 || !warning_suppressed_p (val2, OPT_Woverflow)))
452 *strict_overflow_p = true;
454 if (!inv1)
455 inv1 = build_int_cst (TREE_TYPE (val1), 0);
456 if (!inv2)
457 inv2 = build_int_cst (TREE_TYPE (val2), 0);
459 return wi::cmp (wi::to_wide (inv1), wi::to_wide (inv2),
460 TYPE_SIGN (TREE_TYPE (val1)));
463 const bool cst1 = is_gimple_min_invariant (val1);
464 const bool cst2 = is_gimple_min_invariant (val2);
466 /* If one is of the form '[-]NAME + CST' and the other is constant, then
467 it might be possible to say something depending on the constants. */
468 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
470 if (!overflow_undefined)
471 return -2;
473 if (strict_overflow_p != NULL
474 /* Symbolic range building sets the no-warning bit to declare
475 that overflow doesn't happen. */
476 && (!sym1 || !warning_suppressed_p (val1, OPT_Woverflow))
477 && (!sym2 || !warning_suppressed_p (val2, OPT_Woverflow)))
478 *strict_overflow_p = true;
480 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
481 tree cst = cst1 ? val1 : val2;
482 tree inv = cst1 ? inv2 : inv1;
484 /* Compute the difference between the constants. If it overflows or
485 underflows, this means that we can trivially compare the NAME with
486 it and, consequently, the two values with each other. */
487 wide_int diff = wi::to_wide (cst) - wi::to_wide (inv);
488 if (wi::cmp (0, wi::to_wide (inv), sgn)
489 != wi::cmp (diff, wi::to_wide (cst), sgn))
491 const int res = wi::cmp (wi::to_wide (cst), wi::to_wide (inv), sgn);
492 return cst1 ? res : -res;
495 return -2;
498 /* We cannot say anything more for non-constants. */
499 if (!cst1 || !cst2)
500 return -2;
502 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
504 /* We cannot compare overflowed values. */
505 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
506 return -2;
508 if (TREE_CODE (val1) == INTEGER_CST
509 && TREE_CODE (val2) == INTEGER_CST)
510 return tree_int_cst_compare (val1, val2);
512 if (poly_int_tree_p (val1) && poly_int_tree_p (val2))
514 if (known_eq (wi::to_poly_widest (val1),
515 wi::to_poly_widest (val2)))
516 return 0;
517 if (known_lt (wi::to_poly_widest (val1),
518 wi::to_poly_widest (val2)))
519 return -1;
520 if (known_gt (wi::to_poly_widest (val1),
521 wi::to_poly_widest (val2)))
522 return 1;
525 return -2;
527 else
529 if (TREE_CODE (val1) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
531 /* We cannot compare overflowed values. */
532 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
533 return -2;
535 return tree_int_cst_compare (val1, val2);
538 /* First see if VAL1 and VAL2 are not the same. */
539 if (operand_equal_p (val1, val2, 0))
540 return 0;
542 fold_defer_overflow_warnings ();
544 /* If VAL1 is a lower address than VAL2, return -1. */
545 tree t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val1, val2);
546 if (t && integer_onep (t))
548 fold_undefer_and_ignore_overflow_warnings ();
549 return -1;
552 /* If VAL1 is a higher address than VAL2, return +1. */
553 t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val2, val1);
554 if (t && integer_onep (t))
556 fold_undefer_and_ignore_overflow_warnings ();
557 return 1;
560 /* If VAL1 is different than VAL2, return +2. */
561 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
562 fold_undefer_and_ignore_overflow_warnings ();
563 if (t && integer_onep (t))
564 return 2;
566 return -2;
570 /* Compare values like compare_values_warnv. */
573 compare_values (tree val1, tree val2)
575 bool sop;
576 return compare_values_warnv (val1, val2, &sop);
579 /* If the types passed are supported, return TRUE, otherwise set VR to
580 VARYING and return FALSE. */
582 static bool
583 supported_types_p (value_range *vr,
584 tree type0,
585 tree = NULL)
587 if (!value_range::supports_p (type0))
589 vr->set_varying (type0);
590 return false;
592 return true;
595 /* If any of the ranges passed are defined, return TRUE, otherwise set
596 VR to UNDEFINED and return FALSE. */
598 static bool
599 defined_ranges_p (value_range *vr,
600 const value_range *vr0, const value_range *vr1 = NULL)
602 if (vr0->undefined_p () && (!vr1 || vr1->undefined_p ()))
604 vr->set_undefined ();
605 return false;
607 return true;
610 /* Perform a binary operation on a pair of ranges. */
612 void
613 range_fold_binary_expr (value_range *vr,
614 enum tree_code code,
615 tree expr_type,
616 const value_range *vr0_,
617 const value_range *vr1_)
619 if (!supported_types_p (vr, expr_type)
620 || !defined_ranges_p (vr, vr0_, vr1_))
621 return;
622 range_op_handler op (code, expr_type);
623 if (!op)
625 vr->set_varying (expr_type);
626 return;
629 value_range vr0 (*vr0_);
630 value_range vr1 (*vr1_);
631 if (vr0.undefined_p ())
632 vr0.set_varying (expr_type);
633 if (vr1.undefined_p ())
634 vr1.set_varying (expr_type);
635 vr0.normalize_addresses ();
636 vr1.normalize_addresses ();
637 if (!op.fold_range (*vr, expr_type, vr0, vr1))
638 vr->set_varying (expr_type);
641 /* Perform a unary operation on a range. */
643 void
644 range_fold_unary_expr (value_range *vr,
645 enum tree_code code, tree expr_type,
646 const value_range *vr0,
647 tree vr0_type)
649 if (!supported_types_p (vr, expr_type, vr0_type)
650 || !defined_ranges_p (vr, vr0))
651 return;
652 range_op_handler op (code, expr_type);
653 if (!op)
655 vr->set_varying (expr_type);
656 return;
659 value_range vr0_cst (*vr0);
660 vr0_cst.normalize_addresses ();
661 if (!op.fold_range (*vr, expr_type, vr0_cst, value_range (expr_type)))
662 vr->set_varying (expr_type);
665 /* Helper for overflow_comparison_p
667 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
668 OP1's defining statement to see if it ultimately has the form
669 OP0 CODE (OP0 PLUS INTEGER_CST)
671 If so, return TRUE indicating this is an overflow test and store into
672 *NEW_CST an updated constant that can be used in a narrowed range test.
674 REVERSED indicates if the comparison was originally:
676 OP1 CODE' OP0.
678 This affects how we build the updated constant. */
680 static bool
681 overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
682 bool reversed, tree *new_cst)
684 /* See if this is a relational operation between two SSA_NAMES with
685 unsigned, overflow wrapping values. If so, check it more deeply. */
686 if ((code == LT_EXPR || code == LE_EXPR
687 || code == GE_EXPR || code == GT_EXPR)
688 && TREE_CODE (op0) == SSA_NAME
689 && TREE_CODE (op1) == SSA_NAME
690 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
691 && TYPE_UNSIGNED (TREE_TYPE (op0))
692 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
694 gimple *op1_def = SSA_NAME_DEF_STMT (op1);
696 /* Now look at the defining statement of OP1 to see if it adds
697 or subtracts a nonzero constant from another operand. */
698 if (op1_def
699 && is_gimple_assign (op1_def)
700 && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
701 && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
702 && !integer_zerop (gimple_assign_rhs2 (op1_def)))
704 tree target = gimple_assign_rhs1 (op1_def);
706 /* If we did not find our target SSA_NAME, then this is not
707 an overflow test. */
708 if (op0 != target)
709 return false;
711 tree type = TREE_TYPE (op0);
712 wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
713 tree inc = gimple_assign_rhs2 (op1_def);
714 if (reversed)
715 *new_cst = wide_int_to_tree (type, max + wi::to_wide (inc));
716 else
717 *new_cst = wide_int_to_tree (type, max - wi::to_wide (inc));
718 return true;
721 return false;
724 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
725 OP1's defining statement to see if it ultimately has the form
726 OP0 CODE (OP0 PLUS INTEGER_CST)
728 If so, return TRUE indicating this is an overflow test and store into
729 *NEW_CST an updated constant that can be used in a narrowed range test.
731 These statements are left as-is in the IL to facilitate discovery of
732 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
733 the alternate range representation is often useful within VRP. */
735 bool
736 overflow_comparison_p (tree_code code, tree name, tree val, tree *new_cst)
738 if (overflow_comparison_p_1 (code, name, val, false, new_cst))
739 return true;
740 return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
741 true, new_cst);
744 /* Handle
745 _4 = x_3 & 31;
746 if (_4 != 0)
747 goto <bb 6>;
748 else
749 goto <bb 7>;
750 <bb 6>:
751 __builtin_unreachable ();
752 <bb 7>:
754 If x_3 has no other immediate uses (checked by caller), var is the
755 x_3 var, we can clear low 5 bits from the non-zero bitmask. */
757 void
758 maybe_set_nonzero_bits (edge e, tree var)
760 basic_block cond_bb = e->src;
761 gimple *stmt = last_stmt (cond_bb);
762 tree cst;
764 if (stmt == NULL
765 || gimple_code (stmt) != GIMPLE_COND
766 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
767 ? EQ_EXPR : NE_EXPR)
768 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
769 || !integer_zerop (gimple_cond_rhs (stmt)))
770 return;
772 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
773 if (!is_gimple_assign (stmt)
774 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
775 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
776 return;
777 if (gimple_assign_rhs1 (stmt) != var)
779 gimple *stmt2;
781 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
782 return;
783 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
784 if (!gimple_assign_cast_p (stmt2)
785 || gimple_assign_rhs1 (stmt2) != var
786 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
787 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
788 != TYPE_PRECISION (TREE_TYPE (var))))
789 return;
791 cst = gimple_assign_rhs2 (stmt);
792 if (POINTER_TYPE_P (TREE_TYPE (var)))
794 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (var);
795 if (pi && pi->misalign)
796 return;
797 wide_int w = wi::bit_not (wi::to_wide (cst));
798 unsigned int bits = wi::ctz (w);
799 if (bits == 0 || bits >= HOST_BITS_PER_INT)
800 return;
801 unsigned int align = 1U << bits;
802 if (pi == NULL || pi->align < align)
803 set_ptr_info_alignment (get_ptr_info (var), align, 0);
805 else
806 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var),
807 wi::to_wide (cst)));
810 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
811 that includes the value VAL. The search is restricted to the range
812 [START_IDX, n - 1] where n is the size of VEC.
814 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
815 returned.
817 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
818 it is placed in IDX and false is returned.
820 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
821 returned. */
823 bool
824 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
826 size_t n = gimple_switch_num_labels (stmt);
827 size_t low, high;
829 /* Find case label for minimum of the value range or the next one.
830 At each iteration we are searching in [low, high - 1]. */
832 for (low = start_idx, high = n; high != low; )
834 tree t;
835 int cmp;
836 /* Note that i != high, so we never ask for n. */
837 size_t i = (high + low) / 2;
838 t = gimple_switch_label (stmt, i);
840 /* Cache the result of comparing CASE_LOW and val. */
841 cmp = tree_int_cst_compare (CASE_LOW (t), val);
843 if (cmp == 0)
845 /* Ranges cannot be empty. */
846 *idx = i;
847 return true;
849 else if (cmp > 0)
850 high = i;
851 else
853 low = i + 1;
854 if (CASE_HIGH (t) != NULL
855 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
857 *idx = i;
858 return true;
863 *idx = high;
864 return false;
867 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
868 for values between MIN and MAX. The first index is placed in MIN_IDX. The
869 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
870 then MAX_IDX < MIN_IDX.
871 Returns true if the default label is not needed. */
873 bool
874 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
875 size_t *max_idx)
877 size_t i, j;
878 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
879 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
881 if (i == j
882 && min_take_default
883 && max_take_default)
885 /* Only the default case label reached.
886 Return an empty range. */
887 *min_idx = 1;
888 *max_idx = 0;
889 return false;
891 else
893 bool take_default = min_take_default || max_take_default;
894 tree low, high;
895 size_t k;
897 if (max_take_default)
898 j--;
900 /* If the case label range is continuous, we do not need
901 the default case label. Verify that. */
902 high = CASE_LOW (gimple_switch_label (stmt, i));
903 if (CASE_HIGH (gimple_switch_label (stmt, i)))
904 high = CASE_HIGH (gimple_switch_label (stmt, i));
905 for (k = i + 1; k <= j; ++k)
907 low = CASE_LOW (gimple_switch_label (stmt, k));
908 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
910 take_default = true;
911 break;
913 high = low;
914 if (CASE_HIGH (gimple_switch_label (stmt, k)))
915 high = CASE_HIGH (gimple_switch_label (stmt, k));
918 *min_idx = i;
919 *max_idx = j;
920 return !take_default;
924 /* Given a SWITCH_STMT, return the case label that encompasses the
925 known possible values for the switch operand. RANGE_OF_OP is a
926 range for the known values of the switch operand. */
928 tree
929 find_case_label_range (gswitch *switch_stmt, const irange *range_of_op)
931 if (range_of_op->undefined_p ()
932 || range_of_op->varying_p ())
933 return NULL_TREE;
935 size_t i, j;
936 tree op = gimple_switch_index (switch_stmt);
937 tree type = TREE_TYPE (op);
938 tree tmin = wide_int_to_tree (type, range_of_op->lower_bound ());
939 tree tmax = wide_int_to_tree (type, range_of_op->upper_bound ());
940 find_case_label_range (switch_stmt, tmin, tmax, &i, &j);
941 if (i == j)
943 /* Look for exactly one label that encompasses the range of
944 the operand. */
945 tree label = gimple_switch_label (switch_stmt, i);
946 tree case_high
947 = CASE_HIGH (label) ? CASE_HIGH (label) : CASE_LOW (label);
948 int_range_max label_range (CASE_LOW (label), case_high);
949 if (!types_compatible_p (label_range.type (), range_of_op->type ()))
950 range_cast (label_range, range_of_op->type ());
951 label_range.intersect (*range_of_op);
952 if (label_range == *range_of_op)
953 return label;
955 else if (i > j)
957 /* If there are no labels at all, take the default. */
958 return gimple_switch_label (switch_stmt, 0);
960 else
962 /* Otherwise, there are various labels that can encompass
963 the range of operand. In which case, see if the range of
964 the operand is entirely *outside* the bounds of all the
965 (non-default) case labels. If so, take the default. */
966 unsigned n = gimple_switch_num_labels (switch_stmt);
967 tree min_label = gimple_switch_label (switch_stmt, 1);
968 tree max_label = gimple_switch_label (switch_stmt, n - 1);
969 tree case_high = CASE_HIGH (max_label);
970 if (!case_high)
971 case_high = CASE_LOW (max_label);
972 int_range_max label_range (CASE_LOW (min_label), case_high);
973 if (!types_compatible_p (label_range.type (), range_of_op->type ()))
974 range_cast (label_range, range_of_op->type ());
975 label_range.intersect (*range_of_op);
976 if (label_range.undefined_p ())
977 return gimple_switch_label (switch_stmt, 0);
979 return NULL_TREE;
982 struct case_info
984 tree expr;
985 basic_block bb;
988 // This is a ranger based folder which continues to use the dominator
989 // walk to access the substitute and fold machinery. Ranges are calculated
990 // on demand.
992 class rvrp_folder : public substitute_and_fold_engine
994 public:
996 rvrp_folder (gimple_ranger *r) : substitute_and_fold_engine (),
997 m_unreachable (*r),
998 m_simplifier (r, r->non_executable_edge_flag)
1000 m_ranger = r;
1001 m_pta = new pointer_equiv_analyzer (m_ranger);
1002 m_last_bb_stmt = NULL;
1005 ~rvrp_folder ()
1007 delete m_pta;
1010 tree value_of_expr (tree name, gimple *s = NULL) override
1012 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
1013 if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1014 return NULL;
1015 tree ret = m_ranger->value_of_expr (name, s);
1016 if (!ret && supported_pointer_equiv_p (name))
1017 ret = m_pta->get_equiv (name);
1018 return ret;
1021 tree value_on_edge (edge e, tree name) override
1023 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
1024 if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1025 return NULL;
1026 tree ret = m_ranger->value_on_edge (e, name);
1027 if (!ret && supported_pointer_equiv_p (name))
1028 ret = m_pta->get_equiv (name);
1029 return ret;
1032 tree value_of_stmt (gimple *s, tree name = NULL) override
1034 // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
1035 if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
1036 return NULL;
1037 return m_ranger->value_of_stmt (s, name);
1040 void pre_fold_bb (basic_block bb) override
1042 m_pta->enter (bb);
1043 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1044 gsi_next (&gsi))
1045 m_ranger->register_inferred_ranges (gsi.phi ());
1046 m_last_bb_stmt = last_stmt (bb);
1049 void post_fold_bb (basic_block bb) override
1051 m_pta->leave (bb);
1052 if (cfun->after_inlining)
1053 m_unreachable.maybe_register_block (bb);
1056 void pre_fold_stmt (gimple *stmt) override
1058 m_pta->visit_stmt (stmt);
1059 // If this is the last stmt and there are inferred ranges, reparse the
1060 // block for transitive inferred ranges that occur earlier in the block.
1061 if (stmt == m_last_bb_stmt)
1062 m_ranger->register_transitive_inferred_ranges (gimple_bb (stmt));
1065 bool fold_stmt (gimple_stmt_iterator *gsi) override
1067 bool ret = m_simplifier.simplify (gsi);
1068 if (!ret)
1069 ret = m_ranger->fold_stmt (gsi, follow_single_use_edges);
1070 m_ranger->register_inferred_ranges (gsi_stmt (*gsi));
1071 return ret;
1074 remove_unreachable m_unreachable;
1075 private:
1076 DISABLE_COPY_AND_ASSIGN (rvrp_folder);
1077 gimple_ranger *m_ranger;
1078 simplify_using_ranges m_simplifier;
1079 pointer_equiv_analyzer *m_pta;
1080 gimple *m_last_bb_stmt;
1083 /* Main entry point for a VRP pass using just ranger. This can be called
1084 from anywhere to perform a VRP pass, including from EVRP. */
1086 unsigned int
1087 execute_ranger_vrp (struct function *fun, bool warn_array_bounds_p,
1088 bool final_p)
1090 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
1091 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1092 scev_initialize ();
1093 calculate_dominance_info (CDI_DOMINATORS);
1095 set_all_edges_as_executable (fun);
1096 gimple_ranger *ranger = enable_ranger (fun, false);
1097 rvrp_folder folder (ranger);
1098 folder.substitute_and_fold ();
1099 // Remove tagged builtin-unreachable and maybe update globals.
1100 folder.m_unreachable.remove_and_update_globals (final_p);
1101 if (dump_file && (dump_flags & TDF_DETAILS))
1102 ranger->dump (dump_file);
1104 if ((warn_array_bounds || warn_strict_flex_arrays) && warn_array_bounds_p)
1106 // Set all edges as executable, except those ranger says aren't.
1107 int non_exec_flag = ranger->non_executable_edge_flag;
1108 basic_block bb;
1109 FOR_ALL_BB_FN (bb, fun)
1111 edge_iterator ei;
1112 edge e;
1113 FOR_EACH_EDGE (e, ei, bb->succs)
1114 if (e->flags & non_exec_flag)
1115 e->flags &= ~EDGE_EXECUTABLE;
1116 else
1117 e->flags |= EDGE_EXECUTABLE;
1119 scev_reset ();
1120 array_bounds_checker array_checker (fun, ranger);
1121 array_checker.check ();
1124 disable_ranger (fun);
1125 scev_finalize ();
1126 loop_optimizer_finalize ();
1127 return 0;
1130 namespace {
1132 const pass_data pass_data_vrp =
1134 GIMPLE_PASS, /* type */
1135 "vrp", /* name */
1136 OPTGROUP_NONE, /* optinfo_flags */
1137 TV_TREE_VRP, /* tv_id */
1138 PROP_ssa, /* properties_required */
1139 0, /* properties_provided */
1140 0, /* properties_destroyed */
1141 0, /* todo_flags_start */
1142 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
1145 const pass_data pass_data_early_vrp =
1147 GIMPLE_PASS, /* type */
1148 "evrp", /* name */
1149 OPTGROUP_NONE, /* optinfo_flags */
1150 TV_TREE_EARLY_VRP, /* tv_id */
1151 PROP_ssa, /* properties_required */
1152 0, /* properties_provided */
1153 0, /* properties_destroyed */
1154 0, /* todo_flags_start */
1155 ( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ),
1158 static int vrp_pass_num = 0;
1159 class pass_vrp : public gimple_opt_pass
1161 public:
1162 pass_vrp (gcc::context *ctxt, const pass_data &data_)
1163 : gimple_opt_pass (data_, ctxt), data (data_), warn_array_bounds_p (false),
1164 my_pass (vrp_pass_num++)
1167 /* opt_pass methods: */
1168 opt_pass * clone () final override { return new pass_vrp (m_ctxt, data); }
1169 void set_pass_param (unsigned int n, bool param) final override
1171 gcc_assert (n == 0);
1172 warn_array_bounds_p = param;
1174 bool gate (function *) final override { return flag_tree_vrp != 0; }
1175 unsigned int execute (function *fun) final override
1177 // Early VRP pass.
1178 if (my_pass == 0)
1179 return execute_ranger_vrp (fun, /*warn_array_bounds_p=*/false, false);
1181 return execute_ranger_vrp (fun, warn_array_bounds_p, my_pass == 2);
1184 private:
1185 const pass_data &data;
1186 bool warn_array_bounds_p;
1187 int my_pass;
1188 }; // class pass_vrp
1190 const pass_data pass_data_assumptions =
1192 GIMPLE_PASS, /* type */
1193 "assumptions", /* name */
1194 OPTGROUP_NONE, /* optinfo_flags */
1195 TV_TREE_ASSUMPTIONS, /* tv_id */
1196 PROP_ssa, /* properties_required */
1197 PROP_assumptions_done, /* properties_provided */
1198 0, /* properties_destroyed */
1199 0, /* todo_flags_start */
1200 0, /* todo_flags_end */
1203 class pass_assumptions : public gimple_opt_pass
1205 public:
1206 pass_assumptions (gcc::context *ctxt)
1207 : gimple_opt_pass (pass_data_assumptions, ctxt)
1210 /* opt_pass methods: */
1211 bool gate (function *fun) final override { return fun->assume_function; }
1212 unsigned int execute (function *) final override
1214 assume_query query;
1215 if (dump_file)
1216 fprintf (dump_file, "Assumptions :\n--------------\n");
1218 for (tree arg = DECL_ARGUMENTS (cfun->decl); arg; arg = DECL_CHAIN (arg))
1220 tree name = ssa_default_def (cfun, arg);
1221 if (!name || !gimple_range_ssa_p (name))
1222 continue;
1223 tree type = TREE_TYPE (name);
1224 if (!Value_Range::supports_type_p (type))
1225 continue;
1226 Value_Range assume_range (type);
1227 if (query.assume_range_p (assume_range, name))
1229 // Set the global range of NAME to anything calculated.
1230 set_range_info (name, assume_range);
1231 if (dump_file)
1233 print_generic_expr (dump_file, name, TDF_SLIM);
1234 fprintf (dump_file, " -> ");
1235 assume_range.dump (dump_file);
1236 fputc ('\n', dump_file);
1240 if (dump_file)
1242 fputc ('\n', dump_file);
1243 gimple_dump_cfg (dump_file, dump_flags & ~TDF_DETAILS);
1244 if (dump_flags & TDF_DETAILS)
1245 query.dump (dump_file);
1247 return TODO_discard_function;
1250 }; // class pass_assumptions
1252 } // anon namespace
1254 gimple_opt_pass *
1255 make_pass_vrp (gcc::context *ctxt)
1257 return new pass_vrp (ctxt, pass_data_vrp);
1260 gimple_opt_pass *
1261 make_pass_early_vrp (gcc::context *ctxt)
1263 return new pass_vrp (ctxt, pass_data_early_vrp);
1266 gimple_opt_pass *
1267 make_pass_assumptions (gcc::context *ctx)
1269 return new pass_assumptions (ctx);