]>
Commit | Line | Data |
---|---|---|
c2ad9885 | 1 | /* Support routines for Value Range Propagation (VRP). |
99dee823 | 2 | Copyright (C) 2005-2021 Free Software Foundation, Inc. |
c2ad9885 JL |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING3. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
23 | #include "backend.h" | |
24 | #include "insn-codes.h" | |
25 | #include "tree.h" | |
26 | #include "gimple.h" | |
27 | #include "ssa.h" | |
28 | #include "optabs-tree.h" | |
29 | #include "gimple-pretty-print.h" | |
30 | #include "diagnostic-core.h" | |
31 | #include "flags.h" | |
32 | #include "fold-const.h" | |
33 | #include "calls.h" | |
34 | #include "cfganal.h" | |
35 | #include "gimple-fold.h" | |
36 | #include "gimple-iterator.h" | |
37 | #include "tree-cfg.h" | |
38 | #include "tree-ssa-loop-niter.h" | |
39 | #include "tree-ssa-loop.h" | |
40 | #include "intl.h" | |
41 | #include "cfgloop.h" | |
42 | #include "tree-scalar-evolution.h" | |
43 | #include "tree-ssa-propagate.h" | |
44 | #include "tree-chrec.h" | |
45 | #include "omp-general.h" | |
46 | #include "case-cfn-macros.h" | |
47 | #include "alloc-pool.h" | |
48 | #include "attribs.h" | |
38a73435 | 49 | #include "range.h" |
c2ad9885 | 50 | #include "vr-values.h" |
35b66f30 | 51 | #include "cfghooks.h" |
38a73435 | 52 | #include "range-op.h" |
16e4f1ad | 53 | #include "gimple-range.h" |
c2ad9885 JL |
54 | |
55 | /* Set value range VR to a non-negative range of type TYPE. */ | |
56 | ||
57 | static inline void | |
028d81b1 | 58 | set_value_range_to_nonnegative (value_range_equiv *vr, tree type) |
c2ad9885 JL |
59 | { |
60 | tree zero = build_int_cst (type, 0); | |
5d462877 | 61 | vr->update (zero, vrp_val_max (type)); |
c2ad9885 JL |
62 | } |
63 | ||
64 | /* Set value range VR to a range of a truthvalue of type TYPE. */ | |
65 | ||
66 | static inline void | |
028d81b1 | 67 | set_value_range_to_truthvalue (value_range_equiv *vr, tree type) |
c2ad9885 JL |
68 | { |
69 | if (TYPE_PRECISION (type) == 1) | |
97ecc8d5 | 70 | vr->set_varying (type); |
c2ad9885 | 71 | else |
5d462877 | 72 | vr->update (build_int_cst (type, 0), build_int_cst (type, 1)); |
c2ad9885 JL |
73 | } |
74 | ||
0982acbe RB |
75 | /* Return the lattice entry for VAR or NULL if it doesn't exist or cannot |
76 | be initialized. */ | |
c2ad9885 | 77 | |
028d81b1 | 78 | value_range_equiv * |
0982acbe | 79 | vr_values::get_lattice_entry (const_tree var) |
c2ad9885 | 80 | { |
028d81b1 | 81 | value_range_equiv *vr; |
c2ad9885 JL |
82 | tree sym; |
83 | unsigned ver = SSA_NAME_VERSION (var); | |
84 | ||
0982acbe RB |
85 | /* If we query the entry for a new SSA name avoid reallocating the lattice |
86 | since we should get here at most from the substitute-and-fold stage which | |
c2ad9885 JL |
87 | will never try to change values. */ |
88 | if (ver >= num_vr_values) | |
0982acbe | 89 | return NULL; |
c2ad9885 JL |
90 | |
91 | vr = vr_value[ver]; | |
92 | if (vr) | |
93 | return vr; | |
94 | ||
c2ad9885 | 95 | /* Create a default value range. */ |
a889e06a | 96 | vr = allocate_value_range_equiv (); |
4ba9fb0a | 97 | vr_value[ver] = vr; |
97ecc8d5 AH |
98 | |
99 | /* After propagation finished return varying. */ | |
100 | if (values_propagated) | |
101 | { | |
102 | vr->set_varying (TREE_TYPE (var)); | |
103 | return vr; | |
104 | } | |
105 | ||
54994253 | 106 | vr->set_undefined (); |
c2ad9885 JL |
107 | |
108 | /* If VAR is a default definition of a parameter, the variable can | |
109 | take any value in VAR's type. */ | |
110 | if (SSA_NAME_IS_DEFAULT_DEF (var)) | |
111 | { | |
112 | sym = SSA_NAME_VAR (var); | |
113 | if (TREE_CODE (sym) == PARM_DECL) | |
114 | { | |
115 | /* Try to use the "nonnull" attribute to create ~[0, 0] | |
116 | anti-ranges for pointers. Note that this is only valid with | |
117 | default definitions of PARM_DECLs. */ | |
118 | if (POINTER_TYPE_P (TREE_TYPE (sym)) | |
119 | && (nonnull_arg_p (sym) | |
45f4e2b0 AH |
120 | || (get_global_range_query ()->range_of_expr (*vr, |
121 | const_cast <tree> (var)) | |
122 | && vr->nonzero_p ()))) | |
f2b00d2b AH |
123 | { |
124 | vr->set_nonzero (TREE_TYPE (sym)); | |
125 | vr->equiv_clear (); | |
126 | } | |
c2ad9885 JL |
127 | else if (INTEGRAL_TYPE_P (TREE_TYPE (sym))) |
128 | { | |
45f4e2b0 | 129 | get_global_range_query ()->range_of_expr (*vr, const_cast <tree> (var)); |
5756b6a8 | 130 | if (vr->undefined_p ()) |
97ecc8d5 | 131 | vr->set_varying (TREE_TYPE (sym)); |
c2ad9885 JL |
132 | } |
133 | else | |
97ecc8d5 | 134 | vr->set_varying (TREE_TYPE (sym)); |
c2ad9885 JL |
135 | } |
136 | else if (TREE_CODE (sym) == RESULT_DECL | |
137 | && DECL_BY_REFERENCE (sym)) | |
f2b00d2b AH |
138 | { |
139 | vr->set_nonzero (TREE_TYPE (sym)); | |
140 | vr->equiv_clear (); | |
141 | } | |
c2ad9885 JL |
142 | } |
143 | ||
144 | return vr; | |
145 | } | |
146 | ||
0982acbe RB |
147 | /* Return value range information for VAR. |
148 | ||
149 | If we have no values ranges recorded (ie, VRP is not running), then | |
150 | return NULL. Otherwise create an empty range if none existed for VAR. */ | |
151 | ||
028d81b1 | 152 | const value_range_equiv * |
d8b8023c AH |
153 | vr_values::get_value_range (const_tree var, |
154 | gimple *stmt ATTRIBUTE_UNUSED) | |
0982acbe | 155 | { |
0982acbe RB |
156 | /* If we have no recorded ranges, then return NULL. */ |
157 | if (!vr_value) | |
158 | return NULL; | |
159 | ||
028d81b1 | 160 | value_range_equiv *vr = get_lattice_entry (var); |
97ecc8d5 AH |
161 | |
162 | /* Reallocate the lattice if needed. */ | |
0982acbe | 163 | if (!vr) |
97ecc8d5 AH |
164 | { |
165 | unsigned int old_sz = num_vr_values; | |
166 | num_vr_values = num_ssa_names + num_ssa_names / 10; | |
028d81b1 | 167 | vr_value = XRESIZEVEC (value_range_equiv *, vr_value, num_vr_values); |
97ecc8d5 AH |
168 | for ( ; old_sz < num_vr_values; old_sz++) |
169 | vr_value [old_sz] = NULL; | |
170 | ||
171 | /* Now that the lattice has been resized, we should never fail. */ | |
172 | vr = get_lattice_entry (var); | |
173 | gcc_assert (vr); | |
174 | } | |
0982acbe RB |
175 | |
176 | return vr; | |
177 | } | |
178 | ||
a889e06a | 179 | bool |
16e4f1ad | 180 | vr_values::range_of_expr (irange &r, tree expr, gimple *stmt) |
a889e06a | 181 | { |
16e4f1ad | 182 | if (!gimple_range_ssa_p (expr)) |
caa60c12 | 183 | return get_tree_range (r, expr, stmt); |
16e4f1ad AH |
184 | |
185 | if (const value_range *vr = get_value_range (expr, stmt)) | |
a889e06a | 186 | { |
694c956b | 187 | if (vr->undefined_p () || vr->constant_p ()) |
a889e06a AH |
188 | r = *vr; |
189 | else | |
190 | { | |
191 | value_range tmp = *vr; | |
192 | tmp.normalize_symbolics (); | |
193 | r = tmp; | |
194 | } | |
195 | return true; | |
196 | } | |
197 | return false; | |
198 | } | |
199 | ||
200 | tree | |
201 | vr_values::value_of_expr (tree op, gimple *) | |
202 | { | |
203 | return op_with_constant_singleton_value_range (op); | |
204 | } | |
205 | ||
206 | tree | |
207 | vr_values::value_on_edge (edge, tree op) | |
208 | { | |
209 | return op_with_constant_singleton_value_range (op); | |
210 | } | |
211 | ||
212 | tree | |
213 | vr_values::value_of_stmt (gimple *stmt, tree op) | |
214 | { | |
215 | if (!op) | |
216 | op = gimple_get_lhs (stmt); | |
217 | ||
218 | gcc_checking_assert (!op|| op == gimple_get_lhs (stmt)); | |
219 | ||
220 | if (op) | |
221 | return op_with_constant_singleton_value_range (op); | |
222 | return NULL_TREE; | |
223 | } | |
224 | ||
0982acbe RB |
225 | /* Set the lattice entry for DEF to VARYING. */ |
226 | ||
227 | void | |
228 | vr_values::set_def_to_varying (const_tree def) | |
229 | { | |
028d81b1 | 230 | value_range_equiv *vr = get_lattice_entry (def); |
0982acbe | 231 | if (vr) |
97ecc8d5 | 232 | vr->set_varying (TREE_TYPE (def)); |
0982acbe RB |
233 | } |
234 | ||
c2ad9885 JL |
235 | /* Set value-ranges of all SSA names defined by STMT to varying. */ |
236 | ||
237 | void | |
238 | vr_values::set_defs_to_varying (gimple *stmt) | |
239 | { | |
240 | ssa_op_iter i; | |
241 | tree def; | |
242 | FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF) | |
0982acbe | 243 | set_def_to_varying (def); |
c2ad9885 JL |
244 | } |
245 | ||
246 | /* Update the value range and equivalence set for variable VAR to | |
247 | NEW_VR. Return true if NEW_VR is different from VAR's previous | |
248 | value. | |
249 | ||
250 | NOTE: This function assumes that NEW_VR is a temporary value range | |
251 | object created for the sole purpose of updating VAR's range. The | |
252 | storage used by the equivalence set from NEW_VR will be freed by | |
253 | this function. Do not call update_value_range when NEW_VR | |
254 | is the range object associated with another SSA name. */ | |
255 | ||
256 | bool | |
028d81b1 | 257 | vr_values::update_value_range (const_tree var, value_range_equiv *new_vr) |
c2ad9885 | 258 | { |
028d81b1 | 259 | value_range_equiv *old_vr; |
c2ad9885 JL |
260 | bool is_new; |
261 | ||
262 | /* If there is a value-range on the SSA name from earlier analysis | |
263 | factor that in. */ | |
264 | if (INTEGRAL_TYPE_P (TREE_TYPE (var))) | |
265 | { | |
028d81b1 | 266 | value_range_equiv nr; |
45f4e2b0 | 267 | get_global_range_query ()->range_of_expr (nr, const_cast <tree> (var)); |
028d81b1 | 268 | if (!nr.undefined_p ()) |
5756b6a8 | 269 | new_vr->intersect (&nr); |
c2ad9885 JL |
270 | } |
271 | ||
3c2f6fae RB |
272 | /* Update the value range, if necessary. If we cannot allocate a lattice |
273 | entry for VAR keep it at VARYING. This happens when DOM feeds us stmts | |
274 | with SSA names allocated after setting up the lattice. */ | |
0982acbe | 275 | old_vr = get_lattice_entry (var); |
3c2f6fae RB |
276 | if (!old_vr) |
277 | return false; | |
ff361cc6 | 278 | is_new = !old_vr->equal_p (*new_vr, /*ignore_equivs=*/false); |
c2ad9885 JL |
279 | |
280 | if (is_new) | |
281 | { | |
282 | /* Do not allow transitions up the lattice. The following | |
283 | is slightly more awkward than just new_vr->type < old_vr->type | |
284 | because VR_RANGE and VR_ANTI_RANGE need to be considered | |
285 | the same. We may not have is_new when transitioning to | |
286 | UNDEFINED. If old_vr->type is VARYING, we shouldn't be | |
4f150726 JJ |
287 | called, if we are anyway, keep it VARYING. */ |
288 | if (old_vr->varying_p ()) | |
289 | { | |
22fca489 | 290 | new_vr->set_varying (TREE_TYPE (var)); |
4f150726 JJ |
291 | is_new = false; |
292 | } | |
293 | else if (new_vr->undefined_p ()) | |
c2ad9885 | 294 | { |
97ecc8d5 AH |
295 | old_vr->set_varying (TREE_TYPE (var)); |
296 | new_vr->set_varying (TREE_TYPE (var)); | |
c2ad9885 JL |
297 | return true; |
298 | } | |
299 | else | |
5d462877 AH |
300 | old_vr->set (new_vr->min (), new_vr->max (), new_vr->equiv (), |
301 | new_vr->kind ()); | |
c2ad9885 JL |
302 | } |
303 | ||
54994253 | 304 | new_vr->equiv_clear (); |
c2ad9885 JL |
305 | |
306 | return is_new; | |
307 | } | |
308 | ||
c2ad9885 JL |
309 | /* Return true if value range VR involves exactly one symbol SYM. */ |
310 | ||
311 | static bool | |
028d81b1 | 312 | symbolic_range_based_on_p (value_range *vr, const_tree sym) |
c2ad9885 JL |
313 | { |
314 | bool neg, min_has_symbol, max_has_symbol; | |
315 | tree inv; | |
316 | ||
54994253 | 317 | if (is_gimple_min_invariant (vr->min ())) |
c2ad9885 | 318 | min_has_symbol = false; |
54994253 | 319 | else if (get_single_symbol (vr->min (), &neg, &inv) == sym) |
c2ad9885 JL |
320 | min_has_symbol = true; |
321 | else | |
322 | return false; | |
323 | ||
54994253 | 324 | if (is_gimple_min_invariant (vr->max ())) |
c2ad9885 | 325 | max_has_symbol = false; |
54994253 | 326 | else if (get_single_symbol (vr->max (), &neg, &inv) == sym) |
c2ad9885 JL |
327 | max_has_symbol = true; |
328 | else | |
329 | return false; | |
330 | ||
331 | return (min_has_symbol || max_has_symbol); | |
332 | } | |
333 | ||
334 | /* Return true if the result of assignment STMT is know to be non-zero. */ | |
335 | ||
336 | static bool | |
337 | gimple_assign_nonzero_p (gimple *stmt) | |
338 | { | |
339 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
340 | bool strict_overflow_p; | |
341 | switch (get_gimple_rhs_class (code)) | |
342 | { | |
343 | case GIMPLE_UNARY_RHS: | |
344 | return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt), | |
345 | gimple_expr_type (stmt), | |
346 | gimple_assign_rhs1 (stmt), | |
347 | &strict_overflow_p); | |
348 | case GIMPLE_BINARY_RHS: | |
349 | return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt), | |
350 | gimple_expr_type (stmt), | |
351 | gimple_assign_rhs1 (stmt), | |
352 | gimple_assign_rhs2 (stmt), | |
353 | &strict_overflow_p); | |
354 | case GIMPLE_TERNARY_RHS: | |
355 | return false; | |
356 | case GIMPLE_SINGLE_RHS: | |
357 | return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt), | |
358 | &strict_overflow_p); | |
359 | case GIMPLE_INVALID_RHS: | |
360 | gcc_unreachable (); | |
361 | default: | |
362 | gcc_unreachable (); | |
363 | } | |
364 | } | |
365 | ||
366 | /* Return true if STMT is known to compute a non-zero value. */ | |
367 | ||
368 | static bool | |
369 | gimple_stmt_nonzero_p (gimple *stmt) | |
370 | { | |
371 | switch (gimple_code (stmt)) | |
372 | { | |
373 | case GIMPLE_ASSIGN: | |
374 | return gimple_assign_nonzero_p (stmt); | |
375 | case GIMPLE_CALL: | |
376 | { | |
288aaa5f AH |
377 | gcall *call_stmt = as_a<gcall *> (stmt); |
378 | return (gimple_call_nonnull_result_p (call_stmt) | |
379 | || gimple_call_nonnull_arg (call_stmt)); | |
c2ad9885 JL |
380 | } |
381 | default: | |
382 | gcc_unreachable (); | |
383 | } | |
384 | } | |
385 | /* Like tree_expr_nonzero_p, but this function uses value ranges | |
386 | obtained so far. */ | |
387 | ||
388 | bool | |
389 | vr_values::vrp_stmt_computes_nonzero (gimple *stmt) | |
390 | { | |
391 | if (gimple_stmt_nonzero_p (stmt)) | |
392 | return true; | |
393 | ||
394 | /* If we have an expression of the form &X->a, then the expression | |
395 | is nonnull if X is nonnull. */ | |
396 | if (is_gimple_assign (stmt) | |
397 | && gimple_assign_rhs_code (stmt) == ADDR_EXPR) | |
398 | { | |
399 | tree expr = gimple_assign_rhs1 (stmt); | |
b8a003c1 JJ |
400 | poly_int64 bitsize, bitpos; |
401 | tree offset; | |
402 | machine_mode mode; | |
403 | int unsignedp, reversep, volatilep; | |
404 | tree base = get_inner_reference (TREE_OPERAND (expr, 0), &bitsize, | |
405 | &bitpos, &offset, &mode, &unsignedp, | |
406 | &reversep, &volatilep); | |
c2ad9885 JL |
407 | |
408 | if (base != NULL_TREE | |
409 | && TREE_CODE (base) == MEM_REF | |
410 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME) | |
411 | { | |
b8a003c1 JJ |
412 | poly_offset_int off = 0; |
413 | bool off_cst = false; | |
414 | if (offset == NULL_TREE || TREE_CODE (offset) == INTEGER_CST) | |
415 | { | |
416 | off = mem_ref_offset (base); | |
417 | if (offset) | |
418 | off += poly_offset_int::from (wi::to_poly_wide (offset), | |
419 | SIGNED); | |
420 | off <<= LOG2_BITS_PER_UNIT; | |
421 | off += bitpos; | |
422 | off_cst = true; | |
423 | } | |
424 | /* If &X->a is equal to X and X is ~[0, 0], the result is too. | |
425 | For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't | |
426 | allow going from non-NULL pointer to NULL. */ | |
427 | if ((off_cst && known_eq (off, 0)) | |
428 | || (flag_delete_null_pointer_checks | |
429 | && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)))) | |
430 | { | |
028d81b1 AH |
431 | const value_range_equiv *vr |
432 | = get_value_range (TREE_OPERAND (base, 0)); | |
b8a003c1 JJ |
433 | if (!range_includes_zero_p (vr)) |
434 | return true; | |
435 | } | |
436 | /* If MEM_REF has a "positive" offset, consider it non-NULL | |
437 | always, for -fdelete-null-pointer-checks also "negative" | |
438 | ones. Punt for unknown offsets (e.g. variable ones). */ | |
439 | if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr)) | |
440 | && off_cst | |
441 | && known_ne (off, 0) | |
442 | && (flag_delete_null_pointer_checks || known_gt (off, 0))) | |
c2ad9885 JL |
443 | return true; |
444 | } | |
445 | } | |
446 | ||
447 | return false; | |
448 | } | |
449 | ||
450 | /* Returns true if EXPR is a valid value (as expected by compare_values) -- | |
451 | a gimple invariant, or SSA_NAME +- CST. */ | |
452 | ||
453 | static bool | |
454 | valid_value_p (tree expr) | |
455 | { | |
456 | if (TREE_CODE (expr) == SSA_NAME) | |
457 | return true; | |
458 | ||
459 | if (TREE_CODE (expr) == PLUS_EXPR | |
460 | || TREE_CODE (expr) == MINUS_EXPR) | |
461 | return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME | |
462 | && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST); | |
463 | ||
464 | return is_gimple_min_invariant (expr); | |
465 | } | |
466 | ||
467 | /* If OP has a value range with a single constant value return that, | |
468 | otherwise return NULL_TREE. This returns OP itself if OP is a | |
469 | constant. */ | |
470 | ||
471 | tree | |
472 | vr_values::op_with_constant_singleton_value_range (tree op) | |
473 | { | |
474 | if (is_gimple_min_invariant (op)) | |
475 | return op; | |
476 | ||
477 | if (TREE_CODE (op) != SSA_NAME) | |
478 | return NULL_TREE; | |
479 | ||
57bbc3e2 AH |
480 | tree t; |
481 | if (get_value_range (op)->singleton_p (&t)) | |
482 | return t; | |
483 | return NULL; | |
c2ad9885 JL |
484 | } |
485 | ||
486 | /* Return true if op is in a boolean [0, 1] value-range. */ | |
487 | ||
488 | bool | |
fc36b97a | 489 | simplify_using_ranges::op_with_boolean_value_range_p (tree op) |
c2ad9885 | 490 | { |
c2ad9885 JL |
491 | if (TYPE_PRECISION (TREE_TYPE (op)) == 1) |
492 | return true; | |
493 | ||
494 | if (integer_zerop (op) | |
495 | || integer_onep (op)) | |
496 | return true; | |
497 | ||
498 | if (TREE_CODE (op) != SSA_NAME) | |
499 | return false; | |
500 | ||
52202199 AH |
501 | /* ?? Errr, this should probably check for [0,0] and [1,1] as well |
502 | as [0,1]. */ | |
a889e06a | 503 | const value_range *vr = query->get_value_range (op); |
52202199 AH |
504 | return *vr == value_range (build_zero_cst (TREE_TYPE (op)), |
505 | build_one_cst (TREE_TYPE (op))); | |
c2ad9885 JL |
506 | } |
507 | ||
508 | /* Extract value range information for VAR when (OP COND_CODE LIMIT) is | |
509 | true and store it in *VR_P. */ | |
510 | ||
511 | void | |
512 | vr_values::extract_range_for_var_from_comparison_expr (tree var, | |
513 | enum tree_code cond_code, | |
514 | tree op, tree limit, | |
028d81b1 | 515 | value_range_equiv *vr_p) |
c2ad9885 JL |
516 | { |
517 | tree min, max, type; | |
028d81b1 | 518 | const value_range_equiv *limit_vr; |
c2ad9885 | 519 | type = TREE_TYPE (var); |
c2ad9885 JL |
520 | |
521 | /* For pointer arithmetic, we only keep track of pointer equality | |
e729c8e0 RB |
522 | and inequality. If we arrive here with unfolded conditions like |
523 | _1 > _1 do not derive anything. */ | |
524 | if ((POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR) | |
525 | || limit == var) | |
c2ad9885 | 526 | { |
97ecc8d5 | 527 | vr_p->set_varying (type); |
c2ad9885 JL |
528 | return; |
529 | } | |
530 | ||
531 | /* If LIMIT is another SSA name and LIMIT has a range of its own, | |
532 | try to use LIMIT's range to avoid creating symbolic ranges | |
533 | unnecessarily. */ | |
534 | limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL; | |
535 | ||
536 | /* LIMIT's range is only interesting if it has any useful information. */ | |
537 | if (! limit_vr | |
54994253 AH |
538 | || limit_vr->undefined_p () |
539 | || limit_vr->varying_p () | |
540 | || (limit_vr->symbolic_p () | |
541 | && ! (limit_vr->kind () == VR_RANGE | |
542 | && (limit_vr->min () == limit_vr->max () | |
543 | || operand_equal_p (limit_vr->min (), | |
544 | limit_vr->max (), 0))))) | |
c2ad9885 JL |
545 | limit_vr = NULL; |
546 | ||
547 | /* Initially, the new range has the same set of equivalences of | |
548 | VAR's range. This will be revised before returning the final | |
549 | value. Since assertions may be chained via mutually exclusive | |
550 | predicates, we will need to trim the set of equivalences before | |
551 | we are done. */ | |
54994253 AH |
552 | gcc_assert (vr_p->equiv () == NULL); |
553 | vr_p->equiv_add (var, get_value_range (var), &vrp_equiv_obstack); | |
c2ad9885 JL |
554 | |
555 | /* Extract a new range based on the asserted comparison for VAR and | |
556 | LIMIT's value range. Notice that if LIMIT has an anti-range, we | |
557 | will only use it for equality comparisons (EQ_EXPR). For any | |
558 | other kind of assertion, we cannot derive a range from LIMIT's | |
559 | anti-range that can be used to describe the new range. For | |
560 | instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10], | |
561 | then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is | |
562 | no single range for x_2 that could describe LE_EXPR, so we might | |
563 | as well build the range [b_4, +INF] for it. | |
564 | One special case we handle is extracting a range from a | |
565 | range test encoded as (unsigned)var + CST <= limit. */ | |
566 | if (TREE_CODE (op) == NOP_EXPR | |
567 | || TREE_CODE (op) == PLUS_EXPR) | |
568 | { | |
569 | if (TREE_CODE (op) == PLUS_EXPR) | |
570 | { | |
571 | min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)), | |
572 | TREE_OPERAND (op, 1)); | |
573 | max = int_const_binop (PLUS_EXPR, limit, min); | |
574 | op = TREE_OPERAND (op, 0); | |
575 | } | |
576 | else | |
577 | { | |
578 | min = build_int_cst (TREE_TYPE (var), 0); | |
579 | max = limit; | |
580 | } | |
581 | ||
582 | /* Make sure to not set TREE_OVERFLOW on the final type | |
583 | conversion. We are willingly interpreting large positive | |
584 | unsigned values as negative signed values here. */ | |
585 | min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false); | |
586 | max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false); | |
587 | ||
588 | /* We can transform a max, min range to an anti-range or | |
54994253 AH |
589 | vice-versa. Use set_and_canonicalize which does this for |
590 | us. */ | |
c2ad9885 | 591 | if (cond_code == LE_EXPR) |
5d462877 | 592 | vr_p->set (min, max, vr_p->equiv ()); |
c2ad9885 | 593 | else if (cond_code == GT_EXPR) |
5d462877 | 594 | vr_p->set (min, max, vr_p->equiv (), VR_ANTI_RANGE); |
c2ad9885 JL |
595 | else |
596 | gcc_unreachable (); | |
597 | } | |
598 | else if (cond_code == EQ_EXPR) | |
599 | { | |
5d462877 | 600 | enum value_range_kind range_kind; |
c2ad9885 JL |
601 | |
602 | if (limit_vr) | |
603 | { | |
5d462877 | 604 | range_kind = limit_vr->kind (); |
54994253 AH |
605 | min = limit_vr->min (); |
606 | max = limit_vr->max (); | |
c2ad9885 JL |
607 | } |
608 | else | |
609 | { | |
5d462877 | 610 | range_kind = VR_RANGE; |
c2ad9885 JL |
611 | min = limit; |
612 | max = limit; | |
613 | } | |
614 | ||
5d462877 | 615 | vr_p->update (min, max, range_kind); |
c2ad9885 JL |
616 | |
617 | /* When asserting the equality VAR == LIMIT and LIMIT is another | |
618 | SSA name, the new range will also inherit the equivalence set | |
619 | from LIMIT. */ | |
620 | if (TREE_CODE (limit) == SSA_NAME) | |
54994253 | 621 | vr_p->equiv_add (limit, get_value_range (limit), &vrp_equiv_obstack); |
c2ad9885 JL |
622 | } |
623 | else if (cond_code == NE_EXPR) | |
624 | { | |
625 | /* As described above, when LIMIT's range is an anti-range and | |
626 | this assertion is an inequality (NE_EXPR), then we cannot | |
627 | derive anything from the anti-range. For instance, if | |
628 | LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does | |
629 | not imply that VAR's range is [0, 0]. So, in the case of | |
630 | anti-ranges, we just assert the inequality using LIMIT and | |
631 | not its anti-range. | |
632 | ||
633 | If LIMIT_VR is a range, we can only use it to build a new | |
634 | anti-range if LIMIT_VR is a single-valued range. For | |
635 | instance, if LIMIT_VR is [0, 1], the predicate | |
636 | VAR != [0, 1] does not mean that VAR's range is ~[0, 1]. | |
637 | Rather, it means that for value 0 VAR should be ~[0, 0] | |
638 | and for value 1, VAR should be ~[1, 1]. We cannot | |
639 | represent these ranges. | |
640 | ||
641 | The only situation in which we can build a valid | |
642 | anti-range is when LIMIT_VR is a single-valued range | |
643 | (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case, | |
644 | build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */ | |
645 | if (limit_vr | |
54994253 AH |
646 | && limit_vr->kind () == VR_RANGE |
647 | && compare_values (limit_vr->min (), limit_vr->max ()) == 0) | |
c2ad9885 | 648 | { |
54994253 AH |
649 | min = limit_vr->min (); |
650 | max = limit_vr->max (); | |
c2ad9885 JL |
651 | } |
652 | else | |
653 | { | |
654 | /* In any other case, we cannot use LIMIT's range to build a | |
655 | valid anti-range. */ | |
656 | min = max = limit; | |
657 | } | |
658 | ||
659 | /* If MIN and MAX cover the whole range for their type, then | |
660 | just use the original LIMIT. */ | |
661 | if (INTEGRAL_TYPE_P (type) | |
662 | && vrp_val_is_min (min) | |
663 | && vrp_val_is_max (max)) | |
664 | min = max = limit; | |
665 | ||
5d462877 | 666 | vr_p->set (min, max, vr_p->equiv (), VR_ANTI_RANGE); |
c2ad9885 JL |
667 | } |
668 | else if (cond_code == LE_EXPR || cond_code == LT_EXPR) | |
669 | { | |
670 | min = TYPE_MIN_VALUE (type); | |
671 | ||
54994253 | 672 | if (limit_vr == NULL || limit_vr->kind () == VR_ANTI_RANGE) |
c2ad9885 JL |
673 | max = limit; |
674 | else | |
675 | { | |
676 | /* If LIMIT_VR is of the form [N1, N2], we need to build the | |
677 | range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for | |
678 | LT_EXPR. */ | |
54994253 | 679 | max = limit_vr->max (); |
c2ad9885 JL |
680 | } |
681 | ||
682 | /* If the maximum value forces us to be out of bounds, simply punt. | |
683 | It would be pointless to try and do anything more since this | |
684 | all should be optimized away above us. */ | |
685 | if (cond_code == LT_EXPR | |
686 | && compare_values (max, min) == 0) | |
97ecc8d5 | 687 | vr_p->set_varying (TREE_TYPE (min)); |
c2ad9885 JL |
688 | else |
689 | { | |
690 | /* For LT_EXPR, we create the range [MIN, MAX - 1]. */ | |
691 | if (cond_code == LT_EXPR) | |
692 | { | |
693 | if (TYPE_PRECISION (TREE_TYPE (max)) == 1 | |
694 | && !TYPE_UNSIGNED (TREE_TYPE (max))) | |
695 | max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max, | |
696 | build_int_cst (TREE_TYPE (max), -1)); | |
697 | else | |
698 | max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, | |
699 | build_int_cst (TREE_TYPE (max), 1)); | |
700 | /* Signal to compare_values_warnv this expr doesn't overflow. */ | |
701 | if (EXPR_P (max)) | |
e9e2bad7 | 702 | suppress_warning (max, OPT_Woverflow); |
c2ad9885 JL |
703 | } |
704 | ||
5d462877 | 705 | vr_p->update (min, max); |
c2ad9885 JL |
706 | } |
707 | } | |
708 | else if (cond_code == GE_EXPR || cond_code == GT_EXPR) | |
709 | { | |
710 | max = TYPE_MAX_VALUE (type); | |
711 | ||
54994253 | 712 | if (limit_vr == NULL || limit_vr->kind () == VR_ANTI_RANGE) |
c2ad9885 JL |
713 | min = limit; |
714 | else | |
715 | { | |
716 | /* If LIMIT_VR is of the form [N1, N2], we need to build the | |
717 | range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for | |
718 | GT_EXPR. */ | |
54994253 | 719 | min = limit_vr->min (); |
c2ad9885 JL |
720 | } |
721 | ||
722 | /* If the minimum value forces us to be out of bounds, simply punt. | |
723 | It would be pointless to try and do anything more since this | |
724 | all should be optimized away above us. */ | |
725 | if (cond_code == GT_EXPR | |
726 | && compare_values (min, max) == 0) | |
97ecc8d5 | 727 | vr_p->set_varying (TREE_TYPE (min)); |
c2ad9885 JL |
728 | else |
729 | { | |
730 | /* For GT_EXPR, we create the range [MIN + 1, MAX]. */ | |
731 | if (cond_code == GT_EXPR) | |
732 | { | |
733 | if (TYPE_PRECISION (TREE_TYPE (min)) == 1 | |
734 | && !TYPE_UNSIGNED (TREE_TYPE (min))) | |
735 | min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min, | |
736 | build_int_cst (TREE_TYPE (min), -1)); | |
737 | else | |
738 | min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min, | |
739 | build_int_cst (TREE_TYPE (min), 1)); | |
740 | /* Signal to compare_values_warnv this expr doesn't overflow. */ | |
741 | if (EXPR_P (min)) | |
e9e2bad7 | 742 | suppress_warning (min, OPT_Woverflow); |
c2ad9885 JL |
743 | } |
744 | ||
5d462877 | 745 | vr_p->update (min, max); |
c2ad9885 JL |
746 | } |
747 | } | |
748 | else | |
749 | gcc_unreachable (); | |
750 | ||
751 | /* Finally intersect the new range with what we already know about var. */ | |
54994253 | 752 | vr_p->intersect (get_value_range (var)); |
c2ad9885 JL |
753 | } |
754 | ||
755 | /* Extract value range information from an ASSERT_EXPR EXPR and store | |
756 | it in *VR_P. */ | |
757 | ||
758 | void | |
028d81b1 | 759 | vr_values::extract_range_from_assert (value_range_equiv *vr_p, tree expr) |
c2ad9885 JL |
760 | { |
761 | tree var = ASSERT_EXPR_VAR (expr); | |
762 | tree cond = ASSERT_EXPR_COND (expr); | |
763 | tree limit, op; | |
764 | enum tree_code cond_code; | |
765 | gcc_assert (COMPARISON_CLASS_P (cond)); | |
766 | ||
767 | /* Find VAR in the ASSERT_EXPR conditional. */ | |
768 | if (var == TREE_OPERAND (cond, 0) | |
769 | || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR | |
770 | || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR) | |
771 | { | |
772 | /* If the predicate is of the form VAR COMP LIMIT, then we just | |
773 | take LIMIT from the RHS and use the same comparison code. */ | |
774 | cond_code = TREE_CODE (cond); | |
775 | limit = TREE_OPERAND (cond, 1); | |
776 | op = TREE_OPERAND (cond, 0); | |
777 | } | |
778 | else | |
779 | { | |
780 | /* If the predicate is of the form LIMIT COMP VAR, then we need | |
781 | to flip around the comparison code to create the proper range | |
782 | for VAR. */ | |
783 | cond_code = swap_tree_comparison (TREE_CODE (cond)); | |
784 | limit = TREE_OPERAND (cond, 0); | |
785 | op = TREE_OPERAND (cond, 1); | |
786 | } | |
787 | extract_range_for_var_from_comparison_expr (var, cond_code, op, | |
788 | limit, vr_p); | |
789 | } | |
790 | ||
791 | /* Extract range information from SSA name VAR and store it in VR. If | |
792 | VAR has an interesting range, use it. Otherwise, create the | |
793 | range [VAR, VAR] and return it. This is useful in situations where | |
794 | we may have conditionals testing values of VARYING names. For | |
795 | instance, | |
796 | ||
797 | x_3 = y_5; | |
798 | if (x_3 > y_5) | |
799 | ... | |
800 | ||
801 | Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is | |
802 | always false. */ | |
803 | ||
804 | void | |
028d81b1 | 805 | vr_values::extract_range_from_ssa_name (value_range_equiv *vr, tree var) |
c2ad9885 | 806 | { |
028d81b1 | 807 | const value_range_equiv *var_vr = get_value_range (var); |
c2ad9885 | 808 | |
54994253 AH |
809 | if (!var_vr->varying_p ()) |
810 | vr->deep_copy (var_vr); | |
c2ad9885 | 811 | else |
27922d51 | 812 | vr->set (var); |
c2ad9885 | 813 | |
8ce6fb5f RB |
814 | if (!vr->undefined_p ()) |
815 | vr->equiv_add (var, get_value_range (var), &vrp_equiv_obstack); | |
c2ad9885 JL |
816 | } |
817 | ||
818 | /* Extract range information from a binary expression OP0 CODE OP1 based on | |
819 | the ranges of each of its operands with resulting type EXPR_TYPE. | |
820 | The resulting range is stored in *VR. */ | |
821 | ||
822 | void | |
028d81b1 | 823 | vr_values::extract_range_from_binary_expr (value_range_equiv *vr, |
c2ad9885 JL |
824 | enum tree_code code, |
825 | tree expr_type, tree op0, tree op1) | |
826 | { | |
c2ad9885 JL |
827 | /* Get value ranges for each operand. For constant operands, create |
828 | a new value range with the operand to simplify processing. */ | |
028d81b1 | 829 | value_range vr0, vr1; |
c2ad9885 JL |
830 | if (TREE_CODE (op0) == SSA_NAME) |
831 | vr0 = *(get_value_range (op0)); | |
832 | else if (is_gimple_min_invariant (op0)) | |
27922d51 | 833 | vr0.set (op0); |
c2ad9885 | 834 | else |
97ecc8d5 | 835 | vr0.set_varying (TREE_TYPE (op0)); |
c2ad9885 JL |
836 | |
837 | if (TREE_CODE (op1) == SSA_NAME) | |
838 | vr1 = *(get_value_range (op1)); | |
839 | else if (is_gimple_min_invariant (op1)) | |
27922d51 | 840 | vr1.set (op1); |
c2ad9885 | 841 | else |
97ecc8d5 | 842 | vr1.set_varying (TREE_TYPE (op1)); |
c2ad9885 | 843 | |
50dec459 MG |
844 | /* If one argument is varying, we can sometimes still deduce a |
845 | range for the output: any + [3, +INF] is in [MIN+3, +INF]. */ | |
846 | if (INTEGRAL_TYPE_P (TREE_TYPE (op0)) | |
847 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))) | |
848 | { | |
54994253 | 849 | if (vr0.varying_p () && !vr1.varying_p ()) |
028d81b1 | 850 | vr0 = value_range (vrp_val_min (expr_type), vrp_val_max (expr_type)); |
54994253 | 851 | else if (vr1.varying_p () && !vr0.varying_p ()) |
028d81b1 | 852 | vr1 = value_range (vrp_val_min (expr_type), vrp_val_max (expr_type)); |
50dec459 MG |
853 | } |
854 | ||
38a73435 | 855 | range_fold_binary_expr (vr, code, expr_type, &vr0, &vr1); |
c2ad9885 | 856 | |
41e2c1b0 PK |
857 | /* Set value_range for n in following sequence: |
858 | def = __builtin_memchr (arg, 0, sz) | |
859 | n = def - arg | |
860 | Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */ | |
861 | ||
54994253 | 862 | if (vr->varying_p () |
41e2c1b0 PK |
863 | && code == POINTER_DIFF_EXPR |
864 | && TREE_CODE (op0) == SSA_NAME | |
865 | && TREE_CODE (op1) == SSA_NAME) | |
866 | { | |
867 | tree op0_ptype = TREE_TYPE (TREE_TYPE (op0)); | |
868 | tree op1_ptype = TREE_TYPE (TREE_TYPE (op1)); | |
869 | gcall *call_stmt = NULL; | |
870 | ||
871 | if (TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node) | |
872 | && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node) | |
873 | && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node) | |
874 | && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node) | |
875 | && (call_stmt = dyn_cast<gcall *>(SSA_NAME_DEF_STMT (op0))) | |
876 | && gimple_call_builtin_p (call_stmt, BUILT_IN_MEMCHR) | |
877 | && operand_equal_p (op0, gimple_call_lhs (call_stmt), 0) | |
878 | && operand_equal_p (op1, gimple_call_arg (call_stmt, 0), 0) | |
879 | && integer_zerop (gimple_call_arg (call_stmt, 1))) | |
880 | { | |
881 | tree max = vrp_val_max (ptrdiff_type_node); | |
882 | wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max))); | |
883 | tree range_min = build_zero_cst (expr_type); | |
884 | tree range_max = wide_int_to_tree (expr_type, wmax - 1); | |
5d462877 | 885 | vr->set (range_min, range_max); |
41e2c1b0 PK |
886 | return; |
887 | } | |
888 | } | |
889 | ||
c2ad9885 JL |
890 | /* Try harder for PLUS and MINUS if the range of one operand is symbolic |
891 | and based on the other operand, for example if it was deduced from a | |
892 | symbolic comparison. When a bound of the range of the first operand | |
893 | is invariant, we set the corresponding bound of the new range to INF | |
894 | in order to avoid recursing on the range of the second operand. */ | |
54994253 | 895 | if (vr->varying_p () |
c2ad9885 JL |
896 | && (code == PLUS_EXPR || code == MINUS_EXPR) |
897 | && TREE_CODE (op1) == SSA_NAME | |
54994253 | 898 | && vr0.kind () == VR_RANGE |
c2ad9885 JL |
899 | && symbolic_range_based_on_p (&vr0, op1)) |
900 | { | |
901 | const bool minus_p = (code == MINUS_EXPR); | |
54994253 | 902 | value_range n_vr1; |
c2ad9885 JL |
903 | |
904 | /* Try with VR0 and [-INF, OP1]. */ | |
54994253 | 905 | if (is_gimple_min_invariant (minus_p ? vr0.max () : vr0.min ())) |
5d462877 | 906 | n_vr1.set (vrp_val_min (expr_type), op1); |
c2ad9885 JL |
907 | |
908 | /* Try with VR0 and [OP1, +INF]. */ | |
54994253 | 909 | else if (is_gimple_min_invariant (minus_p ? vr0.min () : vr0.max ())) |
5d462877 | 910 | n_vr1.set (op1, vrp_val_max (expr_type)); |
c2ad9885 JL |
911 | |
912 | /* Try with VR0 and [OP1, OP1]. */ | |
913 | else | |
5d462877 | 914 | n_vr1.set (op1, op1); |
c2ad9885 | 915 | |
38a73435 | 916 | range_fold_binary_expr (vr, code, expr_type, &vr0, &n_vr1); |
c2ad9885 JL |
917 | } |
918 | ||
54994253 | 919 | if (vr->varying_p () |
c2ad9885 JL |
920 | && (code == PLUS_EXPR || code == MINUS_EXPR) |
921 | && TREE_CODE (op0) == SSA_NAME | |
54994253 | 922 | && vr1.kind () == VR_RANGE |
c2ad9885 JL |
923 | && symbolic_range_based_on_p (&vr1, op0)) |
924 | { | |
925 | const bool minus_p = (code == MINUS_EXPR); | |
54994253 | 926 | value_range n_vr0; |
c2ad9885 JL |
927 | |
928 | /* Try with [-INF, OP0] and VR1. */ | |
54994253 | 929 | if (is_gimple_min_invariant (minus_p ? vr1.max () : vr1.min ())) |
5d462877 | 930 | n_vr0.set (vrp_val_min (expr_type), op0); |
c2ad9885 JL |
931 | |
932 | /* Try with [OP0, +INF] and VR1. */ | |
54994253 | 933 | else if (is_gimple_min_invariant (minus_p ? vr1.min (): vr1.max ())) |
5d462877 | 934 | n_vr0.set (op0, vrp_val_max (expr_type)); |
c2ad9885 JL |
935 | |
936 | /* Try with [OP0, OP0] and VR1. */ | |
937 | else | |
27922d51 | 938 | n_vr0.set (op0); |
c2ad9885 | 939 | |
38a73435 | 940 | range_fold_binary_expr (vr, code, expr_type, &n_vr0, &vr1); |
c2ad9885 JL |
941 | } |
942 | ||
943 | /* If we didn't derive a range for MINUS_EXPR, and | |
944 | op1's range is ~[op0,op0] or vice-versa, then we | |
945 | can derive a non-null range. This happens often for | |
946 | pointer subtraction. */ | |
54994253 | 947 | if (vr->varying_p () |
1af4ebf5 | 948 | && (code == MINUS_EXPR || code == POINTER_DIFF_EXPR) |
c2ad9885 | 949 | && TREE_CODE (op0) == SSA_NAME |
54994253 AH |
950 | && ((vr0.kind () == VR_ANTI_RANGE |
951 | && vr0.min () == op1 | |
952 | && vr0.min () == vr0.max ()) | |
953 | || (vr1.kind () == VR_ANTI_RANGE | |
954 | && vr1.min () == op0 | |
955 | && vr1.min () == vr1.max ()))) | |
f2b00d2b AH |
956 | { |
957 | vr->set_nonzero (expr_type); | |
958 | vr->equiv_clear (); | |
959 | } | |
c2ad9885 JL |
960 | } |
961 | ||
962 | /* Extract range information from a unary expression CODE OP0 based on | |
963 | the range of its operand with resulting type TYPE. | |
964 | The resulting range is stored in *VR. */ | |
965 | ||
966 | void | |
028d81b1 AH |
967 | vr_values::extract_range_from_unary_expr (value_range_equiv *vr, |
968 | enum tree_code code, | |
c2ad9885 JL |
969 | tree type, tree op0) |
970 | { | |
028d81b1 | 971 | value_range vr0; |
c2ad9885 JL |
972 | |
973 | /* Get value ranges for the operand. For constant operands, create | |
974 | a new value range with the operand to simplify processing. */ | |
975 | if (TREE_CODE (op0) == SSA_NAME) | |
976 | vr0 = *(get_value_range (op0)); | |
977 | else if (is_gimple_min_invariant (op0)) | |
27922d51 | 978 | vr0.set (op0); |
c2ad9885 | 979 | else |
97ecc8d5 | 980 | vr0.set_varying (type); |
c2ad9885 | 981 | |
38a73435 | 982 | range_fold_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0)); |
c2ad9885 JL |
983 | } |
984 | ||
985 | ||
986 | /* Extract range information from a conditional expression STMT based on | |
987 | the ranges of each of its operands and the expression code. */ | |
988 | ||
989 | void | |
028d81b1 | 990 | vr_values::extract_range_from_cond_expr (value_range_equiv *vr, gassign *stmt) |
c2ad9885 | 991 | { |
c2ad9885 JL |
992 | /* Get value ranges for each operand. For constant operands, create |
993 | a new value range with the operand to simplify processing. */ | |
54994253 | 994 | tree op0 = gimple_assign_rhs2 (stmt); |
028d81b1 AH |
995 | value_range_equiv tem0; |
996 | const value_range_equiv *vr0 = &tem0; | |
c2ad9885 | 997 | if (TREE_CODE (op0) == SSA_NAME) |
27922d51 | 998 | vr0 = get_value_range (op0); |
c2ad9885 | 999 | else if (is_gimple_min_invariant (op0)) |
27922d51 | 1000 | tem0.set (op0); |
c2ad9885 | 1001 | else |
97ecc8d5 | 1002 | tem0.set_varying (TREE_TYPE (op0)); |
c2ad9885 | 1003 | |
54994253 | 1004 | tree op1 = gimple_assign_rhs3 (stmt); |
028d81b1 AH |
1005 | value_range_equiv tem1; |
1006 | const value_range_equiv *vr1 = &tem1; | |
c2ad9885 | 1007 | if (TREE_CODE (op1) == SSA_NAME) |
27922d51 | 1008 | vr1 = get_value_range (op1); |
c2ad9885 | 1009 | else if (is_gimple_min_invariant (op1)) |
27922d51 | 1010 | tem1.set (op1); |
c2ad9885 | 1011 | else |
97ecc8d5 | 1012 | tem1.set_varying (TREE_TYPE (op1)); |
c2ad9885 JL |
1013 | |
1014 | /* The resulting value range is the union of the operand ranges */ | |
27922d51 RB |
1015 | vr->deep_copy (vr0); |
1016 | vr->union_ (vr1); | |
c2ad9885 JL |
1017 | } |
1018 | ||
1019 | ||
1020 | /* Extract range information from a comparison expression EXPR based | |
1021 | on the range of its operand and the expression code. */ | |
1022 | ||
1023 | void | |
028d81b1 | 1024 | vr_values::extract_range_from_comparison (value_range_equiv *vr, |
d8b8023c | 1025 | gimple *stmt) |
c2ad9885 | 1026 | { |
d8b8023c AH |
1027 | enum tree_code code = gimple_assign_rhs_code (stmt); |
1028 | tree type = gimple_expr_type (stmt); | |
1029 | tree op0 = gimple_assign_rhs1 (stmt); | |
1030 | tree op1 = gimple_assign_rhs2 (stmt); | |
c2ad9885 | 1031 | bool sop; |
fc36b97a | 1032 | tree val |
d8b8023c | 1033 | = simplifier.vrp_evaluate_conditional_warnv_with_ops (stmt, code, op0, op1, |
fc36b97a | 1034 | false, &sop, NULL); |
c2ad9885 JL |
1035 | if (val) |
1036 | { | |
1037 | /* Since this expression was found on the RHS of an assignment, | |
1038 | its type may be different from _Bool. Convert VAL to EXPR's | |
1039 | type. */ | |
1040 | val = fold_convert (type, val); | |
1041 | if (is_gimple_min_invariant (val)) | |
27922d51 | 1042 | vr->set (val); |
c2ad9885 | 1043 | else |
5d462877 | 1044 | vr->update (val, val); |
c2ad9885 JL |
1045 | } |
1046 | else | |
1047 | /* The result of a comparison is always true or false. */ | |
1048 | set_value_range_to_truthvalue (vr, type); | |
1049 | } | |
1050 | ||
1051 | /* Helper function for simplify_internal_call_using_ranges and | |
1052 | extract_range_basic. Return true if OP0 SUBCODE OP1 for | |
1053 | SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or | |
1054 | always overflow. Set *OVF to true if it is known to always | |
1055 | overflow. */ | |
1056 | ||
fc36b97a | 1057 | static bool |
a889e06a | 1058 | check_for_binary_op_overflow (range_query *query, |
fc36b97a AH |
1059 | enum tree_code subcode, tree type, |
1060 | tree op0, tree op1, bool *ovf) | |
c2ad9885 | 1061 | { |
028d81b1 | 1062 | value_range vr0, vr1; |
c2ad9885 | 1063 | if (TREE_CODE (op0) == SSA_NAME) |
a889e06a | 1064 | vr0 = *query->get_value_range (op0); |
c2ad9885 | 1065 | else if (TREE_CODE (op0) == INTEGER_CST) |
27922d51 | 1066 | vr0.set (op0); |
c2ad9885 | 1067 | else |
97ecc8d5 | 1068 | vr0.set_varying (TREE_TYPE (op0)); |
c2ad9885 JL |
1069 | |
1070 | if (TREE_CODE (op1) == SSA_NAME) | |
a889e06a | 1071 | vr1 = *query->get_value_range (op1); |
c2ad9885 | 1072 | else if (TREE_CODE (op1) == INTEGER_CST) |
27922d51 | 1073 | vr1.set (op1); |
c2ad9885 | 1074 | else |
97ecc8d5 | 1075 | vr1.set_varying (TREE_TYPE (op1)); |
c2ad9885 | 1076 | |
54994253 AH |
1077 | tree vr0min = vr0.min (), vr0max = vr0.max (); |
1078 | tree vr1min = vr1.min (), vr1max = vr1.max (); | |
c2ad9885 | 1079 | if (!range_int_cst_p (&vr0) |
54994253 AH |
1080 | || TREE_OVERFLOW (vr0min) |
1081 | || TREE_OVERFLOW (vr0max)) | |
c2ad9885 | 1082 | { |
54994253 AH |
1083 | vr0min = vrp_val_min (TREE_TYPE (op0)); |
1084 | vr0max = vrp_val_max (TREE_TYPE (op0)); | |
c2ad9885 JL |
1085 | } |
1086 | if (!range_int_cst_p (&vr1) | |
54994253 AH |
1087 | || TREE_OVERFLOW (vr1min) |
1088 | || TREE_OVERFLOW (vr1max)) | |
c2ad9885 | 1089 | { |
54994253 AH |
1090 | vr1min = vrp_val_min (TREE_TYPE (op1)); |
1091 | vr1max = vrp_val_max (TREE_TYPE (op1)); | |
c2ad9885 | 1092 | } |
54994253 AH |
1093 | *ovf = arith_overflowed_p (subcode, type, vr0min, |
1094 | subcode == MINUS_EXPR ? vr1max : vr1min); | |
1095 | if (arith_overflowed_p (subcode, type, vr0max, | |
1096 | subcode == MINUS_EXPR ? vr1min : vr1max) != *ovf) | |
c2ad9885 JL |
1097 | return false; |
1098 | if (subcode == MULT_EXPR) | |
1099 | { | |
54994253 AH |
1100 | if (arith_overflowed_p (subcode, type, vr0min, vr1max) != *ovf |
1101 | || arith_overflowed_p (subcode, type, vr0max, vr1min) != *ovf) | |
c2ad9885 JL |
1102 | return false; |
1103 | } | |
1104 | if (*ovf) | |
1105 | { | |
1106 | /* So far we found that there is an overflow on the boundaries. | |
1107 | That doesn't prove that there is an overflow even for all values | |
1108 | in between the boundaries. For that compute widest_int range | |
1109 | of the result and see if it doesn't overlap the range of | |
1110 | type. */ | |
1111 | widest_int wmin, wmax; | |
1112 | widest_int w[4]; | |
1113 | int i; | |
54994253 AH |
1114 | w[0] = wi::to_widest (vr0min); |
1115 | w[1] = wi::to_widest (vr0max); | |
1116 | w[2] = wi::to_widest (vr1min); | |
1117 | w[3] = wi::to_widest (vr1max); | |
c2ad9885 JL |
1118 | for (i = 0; i < 4; i++) |
1119 | { | |
1120 | widest_int wt; | |
1121 | switch (subcode) | |
1122 | { | |
1123 | case PLUS_EXPR: | |
1124 | wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]); | |
1125 | break; | |
1126 | case MINUS_EXPR: | |
1127 | wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]); | |
1128 | break; | |
1129 | case MULT_EXPR: | |
1130 | wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]); | |
1131 | break; | |
1132 | default: | |
1133 | gcc_unreachable (); | |
1134 | } | |
1135 | if (i == 0) | |
1136 | { | |
1137 | wmin = wt; | |
1138 | wmax = wt; | |
1139 | } | |
1140 | else | |
1141 | { | |
1142 | wmin = wi::smin (wmin, wt); | |
1143 | wmax = wi::smax (wmax, wt); | |
1144 | } | |
1145 | } | |
1146 | /* The result of op0 CODE op1 is known to be in range | |
1147 | [wmin, wmax]. */ | |
1148 | widest_int wtmin = wi::to_widest (vrp_val_min (type)); | |
1149 | widest_int wtmax = wi::to_widest (vrp_val_max (type)); | |
1150 | /* If all values in [wmin, wmax] are smaller than | |
1151 | [wtmin, wtmax] or all are larger than [wtmin, wtmax], | |
1152 | the arithmetic operation will always overflow. */ | |
1153 | if (wmax < wtmin || wmin > wtmax) | |
1154 | return true; | |
1155 | return false; | |
1156 | } | |
1157 | return true; | |
1158 | } | |
1159 | ||
16e4f1ad AH |
1160 | /* Derive a range from a builtin. Set range in VR and return TRUE if |
1161 | successful. */ | |
c2ad9885 | 1162 | |
16e4f1ad | 1163 | bool |
82b6d25d | 1164 | vr_values::extract_range_from_ubsan_builtin (value_range_equiv *vr, gimple *stmt) |
c2ad9885 | 1165 | { |
16e4f1ad | 1166 | gcc_assert (is_gimple_call (stmt)); |
c2ad9885 | 1167 | tree type = gimple_expr_type (stmt); |
16e4f1ad AH |
1168 | enum tree_code subcode = ERROR_MARK; |
1169 | combined_fn cfn = gimple_call_combined_fn (stmt); | |
1170 | scalar_int_mode mode; | |
c2ad9885 | 1171 | |
16e4f1ad | 1172 | switch (cfn) |
c2ad9885 | 1173 | { |
16e4f1ad AH |
1174 | case CFN_UBSAN_CHECK_ADD: |
1175 | subcode = PLUS_EXPR; | |
1176 | break; | |
1177 | case CFN_UBSAN_CHECK_SUB: | |
1178 | subcode = MINUS_EXPR; | |
1179 | break; | |
1180 | case CFN_UBSAN_CHECK_MUL: | |
1181 | subcode = MULT_EXPR; | |
1182 | break; | |
16e4f1ad AH |
1183 | default: |
1184 | break; | |
1185 | } | |
1186 | if (subcode != ERROR_MARK) | |
1187 | { | |
1188 | bool saved_flag_wrapv = flag_wrapv; | |
1189 | /* Pretend the arithmetics is wrapping. If there is | |
1190 | any overflow, we'll complain, but will actually do | |
1191 | wrapping operation. */ | |
1192 | flag_wrapv = 1; | |
1193 | extract_range_from_binary_expr (vr, subcode, type, | |
1194 | gimple_call_arg (stmt, 0), | |
1195 | gimple_call_arg (stmt, 1)); | |
1196 | flag_wrapv = saved_flag_wrapv; | |
1197 | ||
1198 | /* If for both arguments vrp_valueize returned non-NULL, | |
1199 | this should have been already folded and if not, it | |
1200 | wasn't folded because of overflow. Avoid removing the | |
1201 | UBSAN_CHECK_* calls in that case. */ | |
1202 | if (vr->kind () == VR_RANGE | |
1203 | && (vr->min () == vr->max () | |
1204 | || operand_equal_p (vr->min (), vr->max (), 0))) | |
1205 | vr->set_varying (vr->type ()); | |
1206 | ||
1207 | return !vr->varying_p (); | |
1208 | } | |
1209 | return false; | |
1210 | } | |
1211 | ||
1212 | /* Try to derive a nonnegative or nonzero range out of STMT relying | |
1213 | primarily on generic routines in fold in conjunction with range data. | |
1214 | Store the result in *VR */ | |
1215 | ||
1216 | void | |
1217 | vr_values::extract_range_basic (value_range_equiv *vr, gimple *stmt) | |
1218 | { | |
1219 | bool sop; | |
1220 | tree type = gimple_expr_type (stmt); | |
1221 | ||
82b6d25d | 1222 | if (is_gimple_call (stmt)) |
16e4f1ad | 1223 | { |
054d7b9f | 1224 | combined_fn cfn = gimple_call_combined_fn (stmt); |
82b6d25d AH |
1225 | switch (cfn) |
1226 | { | |
1227 | case CFN_UBSAN_CHECK_ADD: | |
1228 | case CFN_UBSAN_CHECK_SUB: | |
1229 | case CFN_UBSAN_CHECK_MUL: | |
1230 | if (extract_range_from_ubsan_builtin (vr, stmt)) | |
1231 | return; | |
1232 | break; | |
1233 | default: | |
dc6758f0 | 1234 | if (fold_range (*vr, stmt, this)) |
82b6d25d AH |
1235 | { |
1236 | /* The original code nuked equivalences every time a | |
1237 | range was found, so do the same here. */ | |
1238 | vr->equiv_clear (); | |
1239 | return; | |
1240 | } | |
1241 | break; | |
1242 | } | |
c2ad9885 JL |
1243 | } |
1244 | /* Handle extraction of the two results (result of arithmetics and | |
1245 | a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW | |
1246 | internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */ | |
1247 | else if (is_gimple_assign (stmt) | |
1248 | && (gimple_assign_rhs_code (stmt) == REALPART_EXPR | |
1249 | || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR) | |
1250 | && INTEGRAL_TYPE_P (type)) | |
1251 | { | |
1252 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
1253 | tree op = gimple_assign_rhs1 (stmt); | |
1254 | if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME) | |
1255 | { | |
1256 | gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0)); | |
1257 | if (is_gimple_call (g) && gimple_call_internal_p (g)) | |
1258 | { | |
1259 | enum tree_code subcode = ERROR_MARK; | |
1260 | switch (gimple_call_internal_fn (g)) | |
1261 | { | |
1262 | case IFN_ADD_OVERFLOW: | |
1263 | subcode = PLUS_EXPR; | |
1264 | break; | |
1265 | case IFN_SUB_OVERFLOW: | |
1266 | subcode = MINUS_EXPR; | |
1267 | break; | |
1268 | case IFN_MUL_OVERFLOW: | |
1269 | subcode = MULT_EXPR; | |
1270 | break; | |
1271 | case IFN_ATOMIC_COMPARE_EXCHANGE: | |
1272 | if (code == IMAGPART_EXPR) | |
1273 | { | |
1274 | /* This is the boolean return value whether compare and | |
1275 | exchange changed anything or not. */ | |
5d462877 | 1276 | vr->set (build_int_cst (type, 0), |
27922d51 | 1277 | build_int_cst (type, 1)); |
c2ad9885 JL |
1278 | return; |
1279 | } | |
1280 | break; | |
1281 | default: | |
1282 | break; | |
1283 | } | |
1284 | if (subcode != ERROR_MARK) | |
1285 | { | |
1286 | tree op0 = gimple_call_arg (g, 0); | |
1287 | tree op1 = gimple_call_arg (g, 1); | |
1288 | if (code == IMAGPART_EXPR) | |
1289 | { | |
1290 | bool ovf = false; | |
fc36b97a | 1291 | if (check_for_binary_op_overflow (this, subcode, type, |
c2ad9885 | 1292 | op0, op1, &ovf)) |
27922d51 | 1293 | vr->set (build_int_cst (type, ovf)); |
c2ad9885 JL |
1294 | else if (TYPE_PRECISION (type) == 1 |
1295 | && !TYPE_UNSIGNED (type)) | |
97ecc8d5 | 1296 | vr->set_varying (type); |
c2ad9885 | 1297 | else |
5d462877 | 1298 | vr->set (build_int_cst (type, 0), |
27922d51 | 1299 | build_int_cst (type, 1)); |
c2ad9885 JL |
1300 | } |
1301 | else if (types_compatible_p (type, TREE_TYPE (op0)) | |
1302 | && types_compatible_p (type, TREE_TYPE (op1))) | |
1303 | { | |
1304 | bool saved_flag_wrapv = flag_wrapv; | |
1305 | /* Pretend the arithmetics is wrapping. If there is | |
1306 | any overflow, IMAGPART_EXPR will be set. */ | |
1307 | flag_wrapv = 1; | |
1308 | extract_range_from_binary_expr (vr, subcode, type, | |
1309 | op0, op1); | |
1310 | flag_wrapv = saved_flag_wrapv; | |
1311 | } | |
1312 | else | |
1313 | { | |
028d81b1 | 1314 | value_range_equiv vr0, vr1; |
c2ad9885 JL |
1315 | bool saved_flag_wrapv = flag_wrapv; |
1316 | /* Pretend the arithmetics is wrapping. If there is | |
1317 | any overflow, IMAGPART_EXPR will be set. */ | |
1318 | flag_wrapv = 1; | |
1319 | extract_range_from_unary_expr (&vr0, NOP_EXPR, | |
1320 | type, op0); | |
1321 | extract_range_from_unary_expr (&vr1, NOP_EXPR, | |
1322 | type, op1); | |
38a73435 | 1323 | range_fold_binary_expr (vr, subcode, type, &vr0, &vr1); |
c2ad9885 JL |
1324 | flag_wrapv = saved_flag_wrapv; |
1325 | } | |
1326 | return; | |
1327 | } | |
1328 | } | |
1329 | } | |
1330 | } | |
1331 | if (INTEGRAL_TYPE_P (type) | |
1332 | && gimple_stmt_nonnegative_warnv_p (stmt, &sop)) | |
1333 | set_value_range_to_nonnegative (vr, type); | |
1334 | else if (vrp_stmt_computes_nonzero (stmt)) | |
f2b00d2b AH |
1335 | { |
1336 | vr->set_nonzero (type); | |
1337 | vr->equiv_clear (); | |
1338 | } | |
c2ad9885 | 1339 | else |
97ecc8d5 | 1340 | vr->set_varying (type); |
c2ad9885 JL |
1341 | } |
1342 | ||
1343 | ||
1344 | /* Try to compute a useful range out of assignment STMT and store it | |
1345 | in *VR. */ | |
1346 | ||
1347 | void | |
028d81b1 | 1348 | vr_values::extract_range_from_assignment (value_range_equiv *vr, gassign *stmt) |
c2ad9885 JL |
1349 | { |
1350 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
1351 | ||
1352 | if (code == ASSERT_EXPR) | |
1353 | extract_range_from_assert (vr, gimple_assign_rhs1 (stmt)); | |
1354 | else if (code == SSA_NAME) | |
1355 | extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt)); | |
1356 | else if (TREE_CODE_CLASS (code) == tcc_binary) | |
1357 | extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt), | |
1358 | gimple_expr_type (stmt), | |
1359 | gimple_assign_rhs1 (stmt), | |
1360 | gimple_assign_rhs2 (stmt)); | |
1361 | else if (TREE_CODE_CLASS (code) == tcc_unary) | |
1362 | extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt), | |
1363 | gimple_expr_type (stmt), | |
1364 | gimple_assign_rhs1 (stmt)); | |
1365 | else if (code == COND_EXPR) | |
1366 | extract_range_from_cond_expr (vr, stmt); | |
1367 | else if (TREE_CODE_CLASS (code) == tcc_comparison) | |
d8b8023c | 1368 | extract_range_from_comparison (vr, stmt); |
c2ad9885 JL |
1369 | else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS |
1370 | && is_gimple_min_invariant (gimple_assign_rhs1 (stmt))) | |
27922d51 | 1371 | vr->set (gimple_assign_rhs1 (stmt)); |
c2ad9885 | 1372 | else |
97ecc8d5 | 1373 | vr->set_varying (TREE_TYPE (gimple_assign_lhs (stmt))); |
c2ad9885 | 1374 | |
54994253 | 1375 | if (vr->varying_p ()) |
c2ad9885 JL |
1376 | extract_range_basic (vr, stmt); |
1377 | } | |
1378 | ||
1379 | /* Given two numeric value ranges VR0, VR1 and a comparison code COMP: | |
1380 | ||
1381 | - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for | |
1382 | all the values in the ranges. | |
1383 | ||
1384 | - Return BOOLEAN_FALSE_NODE if the comparison always returns false. | |
1385 | ||
1386 | - Return NULL_TREE if it is not always possible to determine the | |
1387 | value of the comparison. | |
1388 | ||
1389 | Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation | |
1390 | assumed signed overflow is undefined. */ | |
1391 | ||
1392 | ||
1393 | static tree | |
028d81b1 AH |
1394 | compare_ranges (enum tree_code comp, const value_range_equiv *vr0, |
1395 | const value_range_equiv *vr1, bool *strict_overflow_p) | |
c2ad9885 JL |
1396 | { |
1397 | /* VARYING or UNDEFINED ranges cannot be compared. */ | |
54994253 AH |
1398 | if (vr0->varying_p () |
1399 | || vr0->undefined_p () | |
1400 | || vr1->varying_p () | |
1401 | || vr1->undefined_p ()) | |
c2ad9885 JL |
1402 | return NULL_TREE; |
1403 | ||
1404 | /* Anti-ranges need to be handled separately. */ | |
54994253 | 1405 | if (vr0->kind () == VR_ANTI_RANGE || vr1->kind () == VR_ANTI_RANGE) |
c2ad9885 JL |
1406 | { |
1407 | /* If both are anti-ranges, then we cannot compute any | |
1408 | comparison. */ | |
54994253 | 1409 | if (vr0->kind () == VR_ANTI_RANGE && vr1->kind () == VR_ANTI_RANGE) |
c2ad9885 JL |
1410 | return NULL_TREE; |
1411 | ||
1412 | /* These comparisons are never statically computable. */ | |
1413 | if (comp == GT_EXPR | |
1414 | || comp == GE_EXPR | |
1415 | || comp == LT_EXPR | |
1416 | || comp == LE_EXPR) | |
1417 | return NULL_TREE; | |
1418 | ||
1419 | /* Equality can be computed only between a range and an | |
1420 | anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */ | |
54994253 | 1421 | if (vr0->kind () == VR_RANGE) |
0982acbe RB |
1422 | /* To simplify processing, make VR0 the anti-range. */ |
1423 | std::swap (vr0, vr1); | |
c2ad9885 JL |
1424 | |
1425 | gcc_assert (comp == NE_EXPR || comp == EQ_EXPR); | |
1426 | ||
54994253 AH |
1427 | if (compare_values_warnv (vr0->min (), vr1->min (), strict_overflow_p) == 0 |
1428 | && compare_values_warnv (vr0->max (), vr1->max (), strict_overflow_p) == 0) | |
c2ad9885 JL |
1429 | return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node; |
1430 | ||
1431 | return NULL_TREE; | |
1432 | } | |
1433 | ||
1434 | /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the | |
1435 | operands around and change the comparison code. */ | |
1436 | if (comp == GT_EXPR || comp == GE_EXPR) | |
1437 | { | |
1438 | comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR; | |
1439 | std::swap (vr0, vr1); | |
1440 | } | |
1441 | ||
1442 | if (comp == EQ_EXPR) | |
1443 | { | |
1444 | /* Equality may only be computed if both ranges represent | |
1445 | exactly one value. */ | |
54994253 AH |
1446 | if (compare_values_warnv (vr0->min (), vr0->max (), strict_overflow_p) == 0 |
1447 | && compare_values_warnv (vr1->min (), vr1->max (), strict_overflow_p) == 0) | |
c2ad9885 | 1448 | { |
54994253 | 1449 | int cmp_min = compare_values_warnv (vr0->min (), vr1->min (), |
c2ad9885 | 1450 | strict_overflow_p); |
54994253 | 1451 | int cmp_max = compare_values_warnv (vr0->max (), vr1->max (), |
c2ad9885 JL |
1452 | strict_overflow_p); |
1453 | if (cmp_min == 0 && cmp_max == 0) | |
1454 | return boolean_true_node; | |
1455 | else if (cmp_min != -2 && cmp_max != -2) | |
1456 | return boolean_false_node; | |
1457 | } | |
1458 | /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */ | |
54994253 | 1459 | else if (compare_values_warnv (vr0->min (), vr1->max (), |
c2ad9885 | 1460 | strict_overflow_p) == 1 |
54994253 | 1461 | || compare_values_warnv (vr1->min (), vr0->max (), |
c2ad9885 JL |
1462 | strict_overflow_p) == 1) |
1463 | return boolean_false_node; | |
1464 | ||
1465 | return NULL_TREE; | |
1466 | } | |
1467 | else if (comp == NE_EXPR) | |
1468 | { | |
1469 | int cmp1, cmp2; | |
1470 | ||
1471 | /* If VR0 is completely to the left or completely to the right | |
1472 | of VR1, they are always different. Notice that we need to | |
1473 | make sure that both comparisons yield similar results to | |
1474 | avoid comparing values that cannot be compared at | |
1475 | compile-time. */ | |
54994253 AH |
1476 | cmp1 = compare_values_warnv (vr0->max (), vr1->min (), strict_overflow_p); |
1477 | cmp2 = compare_values_warnv (vr0->min (), vr1->max (), strict_overflow_p); | |
c2ad9885 JL |
1478 | if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1)) |
1479 | return boolean_true_node; | |
1480 | ||
1481 | /* If VR0 and VR1 represent a single value and are identical, | |
1482 | return false. */ | |
54994253 | 1483 | else if (compare_values_warnv (vr0->min (), vr0->max (), |
c2ad9885 | 1484 | strict_overflow_p) == 0 |
54994253 | 1485 | && compare_values_warnv (vr1->min (), vr1->max (), |
c2ad9885 | 1486 | strict_overflow_p) == 0 |
54994253 | 1487 | && compare_values_warnv (vr0->min (), vr1->min (), |
c2ad9885 | 1488 | strict_overflow_p) == 0 |
54994253 | 1489 | && compare_values_warnv (vr0->max (), vr1->max (), |
c2ad9885 JL |
1490 | strict_overflow_p) == 0) |
1491 | return boolean_false_node; | |
1492 | ||
1493 | /* Otherwise, they may or may not be different. */ | |
1494 | else | |
1495 | return NULL_TREE; | |
1496 | } | |
1497 | else if (comp == LT_EXPR || comp == LE_EXPR) | |
1498 | { | |
1499 | int tst; | |
1500 | ||
1501 | /* If VR0 is to the left of VR1, return true. */ | |
54994253 | 1502 | tst = compare_values_warnv (vr0->max (), vr1->min (), strict_overflow_p); |
c2ad9885 JL |
1503 | if ((comp == LT_EXPR && tst == -1) |
1504 | || (comp == LE_EXPR && (tst == -1 || tst == 0))) | |
1505 | return boolean_true_node; | |
1506 | ||
1507 | /* If VR0 is to the right of VR1, return false. */ | |
54994253 | 1508 | tst = compare_values_warnv (vr0->min (), vr1->max (), strict_overflow_p); |
c2ad9885 JL |
1509 | if ((comp == LT_EXPR && (tst == 0 || tst == 1)) |
1510 | || (comp == LE_EXPR && tst == 1)) | |
1511 | return boolean_false_node; | |
1512 | ||
1513 | /* Otherwise, we don't know. */ | |
1514 | return NULL_TREE; | |
1515 | } | |
1516 | ||
1517 | gcc_unreachable (); | |
1518 | } | |
1519 | ||
1520 | /* Given a value range VR, a value VAL and a comparison code COMP, return | |
1521 | BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the | |
1522 | values in VR. Return BOOLEAN_FALSE_NODE if the comparison | |
1523 | always returns false. Return NULL_TREE if it is not always | |
1524 | possible to determine the value of the comparison. Also set | |
1525 | *STRICT_OVERFLOW_P to indicate whether comparision evaluation | |
1526 | assumed signed overflow is undefined. */ | |
1527 | ||
1528 | static tree | |
fc36b97a | 1529 | compare_range_with_value (enum tree_code comp, const value_range *vr, |
028d81b1 | 1530 | tree val, bool *strict_overflow_p) |
c2ad9885 | 1531 | { |
54994253 | 1532 | if (vr->varying_p () || vr->undefined_p ()) |
c2ad9885 JL |
1533 | return NULL_TREE; |
1534 | ||
1535 | /* Anti-ranges need to be handled separately. */ | |
54994253 | 1536 | if (vr->kind () == VR_ANTI_RANGE) |
c2ad9885 JL |
1537 | { |
1538 | /* For anti-ranges, the only predicates that we can compute at | |
1539 | compile time are equality and inequality. */ | |
1540 | if (comp == GT_EXPR | |
1541 | || comp == GE_EXPR | |
1542 | || comp == LT_EXPR | |
1543 | || comp == LE_EXPR) | |
1544 | return NULL_TREE; | |
1545 | ||
1546 | /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */ | |
fa8ba8b8 | 1547 | if (!vr->may_contain_p (val)) |
c2ad9885 JL |
1548 | return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node; |
1549 | ||
1550 | return NULL_TREE; | |
1551 | } | |
1552 | ||
1553 | if (comp == EQ_EXPR) | |
1554 | { | |
1555 | /* EQ_EXPR may only be computed if VR represents exactly | |
1556 | one value. */ | |
54994253 | 1557 | if (compare_values_warnv (vr->min (), vr->max (), strict_overflow_p) == 0) |
c2ad9885 | 1558 | { |
54994253 | 1559 | int cmp = compare_values_warnv (vr->min (), val, strict_overflow_p); |
c2ad9885 JL |
1560 | if (cmp == 0) |
1561 | return boolean_true_node; | |
1562 | else if (cmp == -1 || cmp == 1 || cmp == 2) | |
1563 | return boolean_false_node; | |
1564 | } | |
54994253 AH |
1565 | else if (compare_values_warnv (val, vr->min (), strict_overflow_p) == -1 |
1566 | || compare_values_warnv (vr->max (), val, strict_overflow_p) == -1) | |
c2ad9885 JL |
1567 | return boolean_false_node; |
1568 | ||
1569 | return NULL_TREE; | |
1570 | } | |
1571 | else if (comp == NE_EXPR) | |
1572 | { | |
1573 | /* If VAL is not inside VR, then they are always different. */ | |
54994253 AH |
1574 | if (compare_values_warnv (vr->max (), val, strict_overflow_p) == -1 |
1575 | || compare_values_warnv (vr->min (), val, strict_overflow_p) == 1) | |
c2ad9885 JL |
1576 | return boolean_true_node; |
1577 | ||
1578 | /* If VR represents exactly one value equal to VAL, then return | |
1579 | false. */ | |
54994253 AH |
1580 | if (compare_values_warnv (vr->min (), vr->max (), strict_overflow_p) == 0 |
1581 | && compare_values_warnv (vr->min (), val, strict_overflow_p) == 0) | |
c2ad9885 JL |
1582 | return boolean_false_node; |
1583 | ||
1584 | /* Otherwise, they may or may not be different. */ | |
1585 | return NULL_TREE; | |
1586 | } | |
1587 | else if (comp == LT_EXPR || comp == LE_EXPR) | |
1588 | { | |
1589 | int tst; | |
1590 | ||
1591 | /* If VR is to the left of VAL, return true. */ | |
54994253 | 1592 | tst = compare_values_warnv (vr->max (), val, strict_overflow_p); |
c2ad9885 JL |
1593 | if ((comp == LT_EXPR && tst == -1) |
1594 | || (comp == LE_EXPR && (tst == -1 || tst == 0))) | |
1595 | return boolean_true_node; | |
1596 | ||
1597 | /* If VR is to the right of VAL, return false. */ | |
54994253 | 1598 | tst = compare_values_warnv (vr->min (), val, strict_overflow_p); |
c2ad9885 JL |
1599 | if ((comp == LT_EXPR && (tst == 0 || tst == 1)) |
1600 | || (comp == LE_EXPR && tst == 1)) | |
1601 | return boolean_false_node; | |
1602 | ||
1603 | /* Otherwise, we don't know. */ | |
1604 | return NULL_TREE; | |
1605 | } | |
1606 | else if (comp == GT_EXPR || comp == GE_EXPR) | |
1607 | { | |
1608 | int tst; | |
1609 | ||
1610 | /* If VR is to the right of VAL, return true. */ | |
54994253 | 1611 | tst = compare_values_warnv (vr->min (), val, strict_overflow_p); |
c2ad9885 JL |
1612 | if ((comp == GT_EXPR && tst == 1) |
1613 | || (comp == GE_EXPR && (tst == 0 || tst == 1))) | |
1614 | return boolean_true_node; | |
1615 | ||
1616 | /* If VR is to the left of VAL, return false. */ | |
54994253 | 1617 | tst = compare_values_warnv (vr->max (), val, strict_overflow_p); |
c2ad9885 JL |
1618 | if ((comp == GT_EXPR && (tst == -1 || tst == 0)) |
1619 | || (comp == GE_EXPR && tst == -1)) | |
1620 | return boolean_false_node; | |
1621 | ||
1622 | /* Otherwise, we don't know. */ | |
1623 | return NULL_TREE; | |
1624 | } | |
1625 | ||
1626 | gcc_unreachable (); | |
1627 | } | |
c2ad9885 | 1628 | |
ea95ba8d AH |
1629 | /* Given a VAR in STMT within LOOP, determine the bounds of the |
1630 | variable and store it in MIN/MAX and return TRUE. If no bounds | |
1631 | could be determined, return FALSE. */ | |
1632 | ||
1633 | bool | |
1634 | bounds_of_var_in_loop (tree *min, tree *max, range_query *query, | |
1635 | class loop *loop, gimple *stmt, tree var) | |
c2ad9885 | 1636 | { |
ea95ba8d | 1637 | tree init, step, chrec, tmin, tmax, type = TREE_TYPE (var); |
c2ad9885 JL |
1638 | enum ev_direction dir; |
1639 | ||
c2ad9885 JL |
1640 | chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var)); |
1641 | ||
1642 | /* Like in PR19590, scev can return a constant function. */ | |
1643 | if (is_gimple_min_invariant (chrec)) | |
1644 | { | |
ea95ba8d | 1645 | *min = *max = chrec; |
878315ae | 1646 | goto fix_overflow; |
c2ad9885 JL |
1647 | } |
1648 | ||
1649 | if (TREE_CODE (chrec) != POLYNOMIAL_CHREC) | |
ea95ba8d | 1650 | return false; |
c2ad9885 JL |
1651 | |
1652 | init = initial_condition_in_loop_num (chrec, loop->num); | |
c2ad9885 | 1653 | step = evolution_part_in_loop_num (chrec, loop->num); |
ea95ba8d | 1654 | |
ce812822 AH |
1655 | if (!init || !step) |
1656 | return false; | |
1657 | ||
ea95ba8d AH |
1658 | /* If INIT is an SSA with a singleton range, set INIT to said |
1659 | singleton, otherwise leave INIT alone. */ | |
1660 | if (TREE_CODE (init) == SSA_NAME) | |
1661 | query->get_value_range (init, stmt)->singleton_p (&init); | |
1662 | /* Likewise for step. */ | |
1663 | if (TREE_CODE (step) == SSA_NAME) | |
1664 | query->get_value_range (step, stmt)->singleton_p (&step); | |
c2ad9885 JL |
1665 | |
1666 | /* If STEP is symbolic, we can't know whether INIT will be the | |
1667 | minimum or maximum value in the range. Also, unless INIT is | |
1668 | a simple expression, compare_values and possibly other functions | |
1669 | in tree-vrp won't be able to handle it. */ | |
1670 | if (step == NULL_TREE | |
1671 | || !is_gimple_min_invariant (step) | |
1672 | || !valid_value_p (init)) | |
ea95ba8d | 1673 | return false; |
c2ad9885 JL |
1674 | |
1675 | dir = scev_direction (chrec); | |
1676 | if (/* Do not adjust ranges if we do not know whether the iv increases | |
1677 | or decreases, ... */ | |
1678 | dir == EV_DIR_UNKNOWN | |
1679 | /* ... or if it may wrap. */ | |
1680 | || scev_probably_wraps_p (NULL_TREE, init, step, stmt, | |
1681 | get_chrec_loop (chrec), true)) | |
ea95ba8d | 1682 | return false; |
c2ad9885 | 1683 | |
c2ad9885 JL |
1684 | if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type)) |
1685 | tmin = lower_bound_in_type (type, type); | |
1686 | else | |
1687 | tmin = TYPE_MIN_VALUE (type); | |
1688 | if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type)) | |
1689 | tmax = upper_bound_in_type (type, type); | |
1690 | else | |
1691 | tmax = TYPE_MAX_VALUE (type); | |
1692 | ||
1693 | /* Try to use estimated number of iterations for the loop to constrain the | |
1694 | final value in the evolution. */ | |
1695 | if (TREE_CODE (step) == INTEGER_CST | |
1696 | && is_gimple_val (init) | |
1697 | && (TREE_CODE (init) != SSA_NAME | |
ea95ba8d | 1698 | || query->get_value_range (init, stmt)->kind () == VR_RANGE)) |
c2ad9885 JL |
1699 | { |
1700 | widest_int nit; | |
1701 | ||
1702 | /* We are only entering here for loop header PHI nodes, so using | |
1703 | the number of latch executions is the correct thing to use. */ | |
1704 | if (max_loop_iterations (loop, &nit)) | |
1705 | { | |
c2ad9885 | 1706 | signop sgn = TYPE_SIGN (TREE_TYPE (step)); |
4a669ac3 | 1707 | wi::overflow_type overflow; |
c2ad9885 JL |
1708 | |
1709 | widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn, | |
1710 | &overflow); | |
1711 | /* If the multiplication overflowed we can't do a meaningful | |
1712 | adjustment. Likewise if the result doesn't fit in the type | |
1713 | of the induction variable. For a signed type we have to | |
1714 | check whether the result has the expected signedness which | |
1715 | is that of the step as number of iterations is unsigned. */ | |
1716 | if (!overflow | |
1717 | && wi::fits_to_tree_p (wtmp, TREE_TYPE (init)) | |
1718 | && (sgn == UNSIGNED | |
1719 | || wi::gts_p (wtmp, 0) == wi::gts_p (wi::to_wide (step), 0))) | |
1720 | { | |
ea95ba8d AH |
1721 | value_range maxvr, vr0, vr1; |
1722 | if (TREE_CODE (init) == SSA_NAME) | |
1723 | vr0 = *(query->get_value_range (init, stmt)); | |
1724 | else if (is_gimple_min_invariant (init)) | |
1725 | vr0.set (init); | |
1726 | else | |
1727 | vr0.set_varying (TREE_TYPE (init)); | |
1728 | tree tem = wide_int_to_tree (TREE_TYPE (init), wtmp); | |
1729 | vr1.set (tem, tem); | |
1730 | range_fold_binary_expr (&maxvr, PLUS_EXPR, | |
1731 | TREE_TYPE (init), &vr0, &vr1); | |
1732 | ||
c2ad9885 | 1733 | /* Likewise if the addition did. */ |
54994253 | 1734 | if (maxvr.kind () == VR_RANGE) |
c2ad9885 | 1735 | { |
028d81b1 | 1736 | value_range initvr; |
c2ad9885 JL |
1737 | |
1738 | if (TREE_CODE (init) == SSA_NAME) | |
ea95ba8d | 1739 | initvr = *(query->get_value_range (init, stmt)); |
c2ad9885 | 1740 | else if (is_gimple_min_invariant (init)) |
27922d51 | 1741 | initvr.set (init); |
c2ad9885 | 1742 | else |
ea95ba8d | 1743 | return false; |
c2ad9885 JL |
1744 | |
1745 | /* Check if init + nit * step overflows. Though we checked | |
1746 | scev {init, step}_loop doesn't wrap, it is not enough | |
1747 | because the loop may exit immediately. Overflow could | |
1748 | happen in the plus expression in this case. */ | |
1749 | if ((dir == EV_DIR_DECREASES | |
54994253 | 1750 | && compare_values (maxvr.min (), initvr.min ()) != -1) |
c2ad9885 | 1751 | || (dir == EV_DIR_GROWS |
54994253 | 1752 | && compare_values (maxvr.max (), initvr.max ()) != 1)) |
ea95ba8d | 1753 | return false; |
c2ad9885 | 1754 | |
54994253 AH |
1755 | tmin = maxvr.min (); |
1756 | tmax = maxvr.max (); | |
c2ad9885 JL |
1757 | } |
1758 | } | |
1759 | } | |
1760 | } | |
1761 | ||
ea95ba8d AH |
1762 | *min = tmin; |
1763 | *max = tmax; | |
1764 | if (dir == EV_DIR_DECREASES) | |
1765 | *max = init; | |
1766 | else | |
1767 | *min = init; | |
c2ad9885 | 1768 | |
878315ae | 1769 | fix_overflow: |
ea95ba8d AH |
1770 | /* Even for valid range info, sometimes overflow flag will leak in. |
1771 | As GIMPLE IL should have no constants with TREE_OVERFLOW set, we | |
1772 | drop them. */ | |
1773 | if (TREE_OVERFLOW_P (*min)) | |
1774 | *min = drop_tree_overflow (*min); | |
1775 | if (TREE_OVERFLOW_P (*max)) | |
1776 | *max = drop_tree_overflow (*max); | |
c2ad9885 | 1777 | |
ea95ba8d AH |
1778 | gcc_checking_assert (compare_values (*min, *max) != 1); |
1779 | return true; | |
1780 | } | |
1781 | ||
1782 | /* Given a range VR, a LOOP and a variable VAR, determine whether it | |
1783 | would be profitable to adjust VR using scalar evolution information | |
1784 | for VAR. If so, update VR with the new limits. */ | |
c2ad9885 | 1785 | |
ea95ba8d AH |
1786 | void |
1787 | vr_values::adjust_range_with_scev (value_range_equiv *vr, class loop *loop, | |
1788 | gimple *stmt, tree var) | |
1789 | { | |
1790 | tree min, max; | |
1791 | if (bounds_of_var_in_loop (&min, &max, this, loop, stmt, var)) | |
1792 | { | |
1793 | if (vr->undefined_p () || vr->varying_p ()) | |
c2ad9885 | 1794 | { |
ea95ba8d AH |
1795 | /* For VARYING or UNDEFINED ranges, just about anything we get |
1796 | from scalar evolutions should be better. */ | |
1797 | vr->update (min, max); | |
1798 | } | |
1799 | else if (vr->kind () == VR_RANGE) | |
1800 | { | |
1801 | /* Start with the input range... */ | |
1802 | tree vrmin = vr->min (); | |
1803 | tree vrmax = vr->max (); | |
c2ad9885 | 1804 | |
ea95ba8d AH |
1805 | /* ...and narrow it down with what we got from SCEV. */ |
1806 | if (compare_values (min, vrmin) == 1) | |
1807 | vrmin = min; | |
1808 | if (compare_values (max, vrmax) == -1) | |
1809 | vrmax = max; | |
c2ad9885 | 1810 | |
ea95ba8d | 1811 | vr->update (vrmin, vrmax); |
c2ad9885 | 1812 | } |
ea95ba8d | 1813 | else if (vr->kind () == VR_ANTI_RANGE) |
c2ad9885 | 1814 | { |
ea95ba8d AH |
1815 | /* ?? As an enhancement, if VR, MIN, and MAX are constants, one |
1816 | could just intersect VR with a range of [MIN,MAX]. */ | |
c2ad9885 JL |
1817 | } |
1818 | } | |
c2ad9885 JL |
1819 | } |
1820 | ||
1821 | /* Dump value ranges of all SSA_NAMEs to FILE. */ | |
1822 | ||
1823 | void | |
586d6f7a | 1824 | vr_values::dump (FILE *file) |
c2ad9885 JL |
1825 | { |
1826 | size_t i; | |
1827 | ||
1828 | for (i = 0; i < num_vr_values; i++) | |
1829 | { | |
3e8d8f3b | 1830 | if (vr_value[i] && ssa_name (i)) |
c2ad9885 JL |
1831 | { |
1832 | print_generic_expr (file, ssa_name (i)); | |
1833 | fprintf (file, ": "); | |
1834 | dump_value_range (file, vr_value[i]); | |
1835 | fprintf (file, "\n"); | |
1836 | } | |
1837 | } | |
1838 | ||
1839 | fprintf (file, "\n"); | |
1840 | } | |
1841 | ||
1842 | /* Initialize VRP lattice. */ | |
1843 | ||
a889e06a | 1844 | vr_values::vr_values () : simplifier (this) |
c2ad9885 JL |
1845 | { |
1846 | values_propagated = false; | |
97ecc8d5 | 1847 | num_vr_values = num_ssa_names * 2; |
028d81b1 | 1848 | vr_value = XCNEWVEC (value_range_equiv *, num_vr_values); |
c2ad9885 JL |
1849 | vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names); |
1850 | bitmap_obstack_initialize (&vrp_equiv_obstack); | |
1851 | } | |
1852 | ||
1853 | /* Free VRP lattice. */ | |
1854 | ||
1855 | vr_values::~vr_values () | |
1856 | { | |
1857 | /* Free allocated memory. */ | |
1858 | free (vr_value); | |
1859 | free (vr_phi_edge_counts); | |
1860 | bitmap_obstack_release (&vrp_equiv_obstack); | |
c2ad9885 JL |
1861 | |
1862 | /* So that we can distinguish between VRP data being available | |
1863 | and not available. */ | |
1864 | vr_value = NULL; | |
1865 | vr_phi_edge_counts = NULL; | |
1866 | } | |
1867 | ||
1868 | ||
1869 | /* A hack. */ | |
1870 | static class vr_values *x_vr_values; | |
1871 | ||
1872 | /* Return the singleton value-range for NAME or NAME. */ | |
1873 | ||
1874 | static inline tree | |
1875 | vrp_valueize (tree name) | |
1876 | { | |
1877 | if (TREE_CODE (name) == SSA_NAME) | |
1878 | { | |
028d81b1 | 1879 | const value_range_equiv *vr = x_vr_values->get_value_range (name); |
54994253 AH |
1880 | if (vr->kind () == VR_RANGE |
1881 | && (TREE_CODE (vr->min ()) == SSA_NAME | |
1882 | || is_gimple_min_invariant (vr->min ())) | |
1883 | && vrp_operand_equal_p (vr->min (), vr->max ())) | |
1884 | return vr->min (); | |
c2ad9885 JL |
1885 | } |
1886 | return name; | |
1887 | } | |
1888 | ||
1889 | /* Return the singleton value-range for NAME if that is a constant | |
1890 | but signal to not follow SSA edges. */ | |
1891 | ||
1892 | static inline tree | |
1893 | vrp_valueize_1 (tree name) | |
1894 | { | |
1895 | if (TREE_CODE (name) == SSA_NAME) | |
1896 | { | |
1897 | /* If the definition may be simulated again we cannot follow | |
1898 | this SSA edge as the SSA propagator does not necessarily | |
1899 | re-visit the use. */ | |
1900 | gimple *def_stmt = SSA_NAME_DEF_STMT (name); | |
1901 | if (!gimple_nop_p (def_stmt) | |
1902 | && prop_simulate_again_p (def_stmt)) | |
1903 | return NULL_TREE; | |
028d81b1 | 1904 | const value_range_equiv *vr = x_vr_values->get_value_range (name); |
54994253 AH |
1905 | tree singleton; |
1906 | if (vr->singleton_p (&singleton)) | |
1907 | return singleton; | |
c2ad9885 JL |
1908 | } |
1909 | return name; | |
1910 | } | |
c2ad9885 | 1911 | |
f432e4fc JL |
1912 | /* Given STMT, an assignment or call, return its LHS if the type |
1913 | of the LHS is suitable for VRP analysis, else return NULL_TREE. */ | |
1914 | ||
1915 | tree | |
1916 | get_output_for_vrp (gimple *stmt) | |
c2ad9885 | 1917 | { |
f432e4fc JL |
1918 | if (!is_gimple_assign (stmt) && !is_gimple_call (stmt)) |
1919 | return NULL_TREE; | |
c2ad9885 JL |
1920 | |
1921 | /* We only keep track of ranges in integral and pointer types. */ | |
f432e4fc | 1922 | tree lhs = gimple_get_lhs (stmt); |
c2ad9885 JL |
1923 | if (TREE_CODE (lhs) == SSA_NAME |
1924 | && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs)) | |
1925 | /* It is valid to have NULL MIN/MAX values on a type. See | |
1926 | build_range_type. */ | |
1927 | && TYPE_MIN_VALUE (TREE_TYPE (lhs)) | |
1928 | && TYPE_MAX_VALUE (TREE_TYPE (lhs))) | |
1929 | || POINTER_TYPE_P (TREE_TYPE (lhs)))) | |
f432e4fc JL |
1930 | return lhs; |
1931 | ||
1932 | return NULL_TREE; | |
1933 | } | |
1934 | ||
1935 | /* Visit assignment STMT. If it produces an interesting range, record | |
1936 | the range in VR and set LHS to OUTPUT_P. */ | |
1937 | ||
1938 | void | |
1939 | vr_values::vrp_visit_assignment_or_call (gimple *stmt, tree *output_p, | |
028d81b1 | 1940 | value_range_equiv *vr) |
f432e4fc JL |
1941 | { |
1942 | tree lhs = get_output_for_vrp (stmt); | |
1943 | *output_p = lhs; | |
1944 | ||
1945 | /* We only keep track of ranges in integral and pointer types. */ | |
1946 | if (lhs) | |
c2ad9885 | 1947 | { |
f432e4fc | 1948 | enum gimple_code code = gimple_code (stmt); |
c2ad9885 JL |
1949 | |
1950 | /* Try folding the statement to a constant first. */ | |
1951 | x_vr_values = this; | |
1952 | tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize, | |
1953 | vrp_valueize_1); | |
1954 | x_vr_values = NULL; | |
1955 | if (tem) | |
1956 | { | |
1957 | if (TREE_CODE (tem) == SSA_NAME | |
1958 | && (SSA_NAME_IS_DEFAULT_DEF (tem) | |
1959 | || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem)))) | |
1960 | { | |
1961 | extract_range_from_ssa_name (vr, tem); | |
1962 | return; | |
1963 | } | |
1964 | else if (is_gimple_min_invariant (tem)) | |
1965 | { | |
27922d51 | 1966 | vr->set (tem); |
c2ad9885 JL |
1967 | return; |
1968 | } | |
1969 | } | |
1970 | /* Then dispatch to value-range extracting functions. */ | |
1971 | if (code == GIMPLE_CALL) | |
1972 | extract_range_basic (vr, stmt); | |
1973 | else | |
1974 | extract_range_from_assignment (vr, as_a <gassign *> (stmt)); | |
1975 | } | |
1976 | } | |
1977 | ||
1978 | /* Helper that gets the value range of the SSA_NAME with version I | |
1979 | or a symbolic range containing the SSA_NAME only if the value range | |
27922d51 | 1980 | is varying or undefined. Uses TEM as storage for the alternate range. */ |
c2ad9885 | 1981 | |
028d81b1 | 1982 | const value_range_equiv * |
fc36b97a | 1983 | simplify_using_ranges::get_vr_for_comparison (int i, value_range_equiv *tem) |
c2ad9885 | 1984 | { |
27922d51 | 1985 | /* Shallow-copy equiv bitmap. */ |
a889e06a | 1986 | const value_range_equiv *vr = query->get_value_range (ssa_name (i)); |
c2ad9885 JL |
1987 | |
1988 | /* If name N_i does not have a valid range, use N_i as its own | |
1989 | range. This allows us to compare against names that may | |
1990 | have N_i in their ranges. */ | |
27922d51 RB |
1991 | if (vr->varying_p () || vr->undefined_p ()) |
1992 | { | |
1993 | tem->set (ssa_name (i)); | |
1994 | return tem; | |
1995 | } | |
c2ad9885 JL |
1996 | |
1997 | return vr; | |
1998 | } | |
1999 | ||
2000 | /* Compare all the value ranges for names equivalent to VAR with VAL | |
2001 | using comparison code COMP. Return the same value returned by | |
2002 | compare_range_with_value, including the setting of | |
2003 | *STRICT_OVERFLOW_P. */ | |
2004 | ||
2005 | tree | |
fc36b97a AH |
2006 | simplify_using_ranges::compare_name_with_value |
2007 | (enum tree_code comp, tree var, tree val, | |
2008 | bool *strict_overflow_p, bool use_equiv_p) | |
c2ad9885 | 2009 | { |
c2ad9885 | 2010 | /* Get the set of equivalences for VAR. */ |
a889e06a | 2011 | bitmap e = query->get_value_range (var)->equiv (); |
c2ad9885 JL |
2012 | |
2013 | /* Start at -1. Set it to 0 if we do a comparison without relying | |
2014 | on overflow, or 1 if all comparisons rely on overflow. */ | |
028d81b1 | 2015 | int used_strict_overflow = -1; |
c2ad9885 JL |
2016 | |
2017 | /* Compare vars' value range with val. */ | |
028d81b1 AH |
2018 | value_range_equiv tem_vr; |
2019 | const value_range_equiv *equiv_vr | |
2020 | = get_vr_for_comparison (SSA_NAME_VERSION (var), &tem_vr); | |
2021 | bool sop = false; | |
2022 | tree retval = compare_range_with_value (comp, equiv_vr, val, &sop); | |
c2ad9885 JL |
2023 | if (retval) |
2024 | used_strict_overflow = sop ? 1 : 0; | |
2025 | ||
2026 | /* If the equiv set is empty we have done all work we need to do. */ | |
2027 | if (e == NULL) | |
2028 | { | |
028d81b1 | 2029 | if (retval && used_strict_overflow > 0) |
c2ad9885 JL |
2030 | *strict_overflow_p = true; |
2031 | return retval; | |
2032 | } | |
2033 | ||
028d81b1 AH |
2034 | unsigned i; |
2035 | bitmap_iterator bi; | |
c2ad9885 JL |
2036 | EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi) |
2037 | { | |
2038 | tree name = ssa_name (i); | |
028d81b1 | 2039 | if (!name) |
c2ad9885 JL |
2040 | continue; |
2041 | ||
028d81b1 AH |
2042 | if (!use_equiv_p |
2043 | && !SSA_NAME_IS_DEFAULT_DEF (name) | |
c2ad9885 JL |
2044 | && prop_simulate_again_p (SSA_NAME_DEF_STMT (name))) |
2045 | continue; | |
2046 | ||
27922d51 | 2047 | equiv_vr = get_vr_for_comparison (i, &tem_vr); |
c2ad9885 | 2048 | sop = false; |
028d81b1 | 2049 | tree t = compare_range_with_value (comp, equiv_vr, val, &sop); |
c2ad9885 JL |
2050 | if (t) |
2051 | { | |
2052 | /* If we get different answers from different members | |
2053 | of the equivalence set this check must be in a dead | |
2054 | code region. Folding it to a trap representation | |
2055 | would be correct here. For now just return don't-know. */ | |
2056 | if (retval != NULL | |
2057 | && t != retval) | |
2058 | { | |
2059 | retval = NULL_TREE; | |
2060 | break; | |
2061 | } | |
2062 | retval = t; | |
2063 | ||
2064 | if (!sop) | |
2065 | used_strict_overflow = 0; | |
2066 | else if (used_strict_overflow < 0) | |
2067 | used_strict_overflow = 1; | |
2068 | } | |
2069 | } | |
2070 | ||
028d81b1 | 2071 | if (retval && used_strict_overflow > 0) |
c2ad9885 JL |
2072 | *strict_overflow_p = true; |
2073 | ||
2074 | return retval; | |
2075 | } | |
2076 | ||
2077 | ||
2078 | /* Given a comparison code COMP and names N1 and N2, compare all the | |
2079 | ranges equivalent to N1 against all the ranges equivalent to N2 | |
2080 | to determine the value of N1 COMP N2. Return the same value | |
2081 | returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate | |
2082 | whether we relied on undefined signed overflow in the comparison. */ | |
2083 | ||
2084 | ||
2085 | tree | |
fc36b97a AH |
2086 | simplify_using_ranges::compare_names (enum tree_code comp, tree n1, tree n2, |
2087 | bool *strict_overflow_p) | |
c2ad9885 | 2088 | { |
c2ad9885 JL |
2089 | /* Compare the ranges of every name equivalent to N1 against the |
2090 | ranges of every name equivalent to N2. */ | |
a889e06a AH |
2091 | bitmap e1 = query->get_value_range (n1)->equiv (); |
2092 | bitmap e2 = query->get_value_range (n2)->equiv (); | |
c2ad9885 JL |
2093 | |
2094 | /* Use the fake bitmaps if e1 or e2 are not available. */ | |
028d81b1 AH |
2095 | static bitmap s_e1 = NULL, s_e2 = NULL; |
2096 | static bitmap_obstack *s_obstack = NULL; | |
c2ad9885 JL |
2097 | if (s_obstack == NULL) |
2098 | { | |
2099 | s_obstack = XNEW (bitmap_obstack); | |
2100 | bitmap_obstack_initialize (s_obstack); | |
2101 | s_e1 = BITMAP_ALLOC (s_obstack); | |
2102 | s_e2 = BITMAP_ALLOC (s_obstack); | |
2103 | } | |
2104 | if (e1 == NULL) | |
2105 | e1 = s_e1; | |
2106 | if (e2 == NULL) | |
2107 | e2 = s_e2; | |
2108 | ||
2109 | /* Add N1 and N2 to their own set of equivalences to avoid | |
2110 | duplicating the body of the loop just to check N1 and N2 | |
2111 | ranges. */ | |
2112 | bitmap_set_bit (e1, SSA_NAME_VERSION (n1)); | |
2113 | bitmap_set_bit (e2, SSA_NAME_VERSION (n2)); | |
2114 | ||
2115 | /* If the equivalence sets have a common intersection, then the two | |
2116 | names can be compared without checking their ranges. */ | |
2117 | if (bitmap_intersect_p (e1, e2)) | |
2118 | { | |
2119 | bitmap_clear_bit (e1, SSA_NAME_VERSION (n1)); | |
2120 | bitmap_clear_bit (e2, SSA_NAME_VERSION (n2)); | |
2121 | ||
2122 | return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR) | |
2123 | ? boolean_true_node | |
2124 | : boolean_false_node; | |
2125 | } | |
2126 | ||
2127 | /* Start at -1. Set it to 0 if we do a comparison without relying | |
2128 | on overflow, or 1 if all comparisons rely on overflow. */ | |
028d81b1 | 2129 | int used_strict_overflow = -1; |
c2ad9885 JL |
2130 | |
2131 | /* Otherwise, compare all the equivalent ranges. First, add N1 and | |
2132 | N2 to their own set of equivalences to avoid duplicating the body | |
2133 | of the loop just to check N1 and N2 ranges. */ | |
028d81b1 AH |
2134 | bitmap_iterator bi1; |
2135 | unsigned i1; | |
c2ad9885 JL |
2136 | EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1) |
2137 | { | |
028d81b1 | 2138 | if (!ssa_name (i1)) |
c2ad9885 JL |
2139 | continue; |
2140 | ||
028d81b1 AH |
2141 | value_range_equiv tem_vr1; |
2142 | const value_range_equiv *vr1 = get_vr_for_comparison (i1, &tem_vr1); | |
c2ad9885 | 2143 | |
028d81b1 AH |
2144 | tree t = NULL_TREE, retval = NULL_TREE; |
2145 | bitmap_iterator bi2; | |
2146 | unsigned i2; | |
c2ad9885 JL |
2147 | EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2) |
2148 | { | |
028d81b1 | 2149 | if (!ssa_name (i2)) |
c2ad9885 JL |
2150 | continue; |
2151 | ||
2152 | bool sop = false; | |
2153 | ||
028d81b1 AH |
2154 | value_range_equiv tem_vr2; |
2155 | const value_range_equiv *vr2 = get_vr_for_comparison (i2, &tem_vr2); | |
c2ad9885 | 2156 | |
27922d51 | 2157 | t = compare_ranges (comp, vr1, vr2, &sop); |
c2ad9885 JL |
2158 | if (t) |
2159 | { | |
2160 | /* If we get different answers from different members | |
2161 | of the equivalence set this check must be in a dead | |
2162 | code region. Folding it to a trap representation | |
2163 | would be correct here. For now just return don't-know. */ | |
028d81b1 | 2164 | if (retval != NULL && t != retval) |
c2ad9885 JL |
2165 | { |
2166 | bitmap_clear_bit (e1, SSA_NAME_VERSION (n1)); | |
2167 | bitmap_clear_bit (e2, SSA_NAME_VERSION (n2)); | |
2168 | return NULL_TREE; | |
2169 | } | |
2170 | retval = t; | |
2171 | ||
2172 | if (!sop) | |
2173 | used_strict_overflow = 0; | |
2174 | else if (used_strict_overflow < 0) | |
2175 | used_strict_overflow = 1; | |
2176 | } | |
2177 | } | |
2178 | ||
2179 | if (retval) | |
2180 | { | |
2181 | bitmap_clear_bit (e1, SSA_NAME_VERSION (n1)); | |
2182 | bitmap_clear_bit (e2, SSA_NAME_VERSION (n2)); | |
2183 | if (used_strict_overflow > 0) | |
2184 | *strict_overflow_p = true; | |
2185 | return retval; | |
2186 | } | |
2187 | } | |
2188 | ||
2189 | /* None of the equivalent ranges are useful in computing this | |
2190 | comparison. */ | |
2191 | bitmap_clear_bit (e1, SSA_NAME_VERSION (n1)); | |
2192 | bitmap_clear_bit (e2, SSA_NAME_VERSION (n2)); | |
2193 | return NULL_TREE; | |
2194 | } | |
2195 | ||
2196 | /* Helper function for vrp_evaluate_conditional_warnv & other | |
2197 | optimizers. */ | |
2198 | ||
2199 | tree | |
fc36b97a | 2200 | simplify_using_ranges::vrp_evaluate_conditional_warnv_with_ops_using_ranges |
c2ad9885 JL |
2201 | (enum tree_code code, tree op0, tree op1, bool * strict_overflow_p) |
2202 | { | |
028d81b1 | 2203 | const value_range_equiv *vr0, *vr1; |
a889e06a AH |
2204 | vr0 = (TREE_CODE (op0) == SSA_NAME) ? query->get_value_range (op0) : NULL; |
2205 | vr1 = (TREE_CODE (op1) == SSA_NAME) ? query->get_value_range (op1) : NULL; | |
c2ad9885 JL |
2206 | |
2207 | tree res = NULL_TREE; | |
2208 | if (vr0 && vr1) | |
2209 | res = compare_ranges (code, vr0, vr1, strict_overflow_p); | |
2210 | if (!res && vr0) | |
2211 | res = compare_range_with_value (code, vr0, op1, strict_overflow_p); | |
2212 | if (!res && vr1) | |
2213 | res = (compare_range_with_value | |
2214 | (swap_tree_comparison (code), vr1, op0, strict_overflow_p)); | |
2215 | return res; | |
2216 | } | |
2217 | ||
2218 | /* Helper function for vrp_evaluate_conditional_warnv. */ | |
2219 | ||
2220 | tree | |
fc36b97a | 2221 | simplify_using_ranges::vrp_evaluate_conditional_warnv_with_ops |
d8b8023c AH |
2222 | (gimple *stmt, |
2223 | enum tree_code code, | |
fc36b97a AH |
2224 | tree op0, tree op1, |
2225 | bool use_equiv_p, | |
2226 | bool *strict_overflow_p, | |
2227 | bool *only_ranges) | |
c2ad9885 JL |
2228 | { |
2229 | tree ret; | |
2230 | if (only_ranges) | |
2231 | *only_ranges = true; | |
2232 | ||
2233 | /* We only deal with integral and pointer types. */ | |
2234 | if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)) | |
2235 | && !POINTER_TYPE_P (TREE_TYPE (op0))) | |
2236 | return NULL_TREE; | |
2237 | ||
2238 | /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed | |
2239 | as a simple equality test, then prefer that over its current form | |
2240 | for evaluation. | |
2241 | ||
2242 | An overflow test which collapses to an equality test can always be | |
2243 | expressed as a comparison of one argument against zero. Overflow | |
2244 | occurs when the chosen argument is zero and does not occur if the | |
2245 | chosen argument is not zero. */ | |
2246 | tree x; | |
2247 | if (overflow_comparison_p (code, op0, op1, use_equiv_p, &x)) | |
2248 | { | |
2249 | wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED); | |
2250 | /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0) | |
2251 | B = A - 1; if (A > B) -> B = A - 1; if (A != 0) | |
2252 | B = A + 1; if (B < A) -> B = A + 1; if (B == 0) | |
2253 | B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */ | |
2254 | if (integer_zerop (x)) | |
2255 | { | |
2256 | op1 = x; | |
2257 | code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR; | |
2258 | } | |
2259 | /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0) | |
2260 | B = A + 1; if (A < B) -> B = A + 1; if (B != 0) | |
2261 | B = A - 1; if (B > A) -> B = A - 1; if (A == 0) | |
2262 | B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */ | |
2263 | else if (wi::to_wide (x) == max - 1) | |
2264 | { | |
2265 | op0 = op1; | |
2266 | op1 = wide_int_to_tree (TREE_TYPE (op0), 0); | |
2267 | code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR; | |
2268 | } | |
0d3d674b AO |
2269 | else |
2270 | { | |
028d81b1 | 2271 | value_range vro, vri; |
0d3d674b AO |
2272 | if (code == GT_EXPR || code == GE_EXPR) |
2273 | { | |
5d462877 AH |
2274 | vro.set (TYPE_MIN_VALUE (TREE_TYPE (op0)), x, VR_ANTI_RANGE); |
2275 | vri.set (TYPE_MIN_VALUE (TREE_TYPE (op0)), x); | |
0d3d674b AO |
2276 | } |
2277 | else if (code == LT_EXPR || code == LE_EXPR) | |
2278 | { | |
5d462877 AH |
2279 | vro.set (TYPE_MIN_VALUE (TREE_TYPE (op0)), x); |
2280 | vri.set (TYPE_MIN_VALUE (TREE_TYPE (op0)), x, VR_ANTI_RANGE); | |
0d3d674b AO |
2281 | } |
2282 | else | |
2283 | gcc_unreachable (); | |
a889e06a | 2284 | const value_range_equiv *vr0 = query->get_value_range (op0, stmt); |
0d3d674b AO |
2285 | /* If vro, the range for OP0 to pass the overflow test, has |
2286 | no intersection with *vr0, OP0's known range, then the | |
2287 | overflow test can't pass, so return the node for false. | |
2288 | If it is the inverted range, vri, that has no | |
2289 | intersection, then the overflow test must pass, so return | |
2290 | the node for true. In other cases, we could proceed with | |
2291 | a simplified condition comparing OP0 and X, with LE_EXPR | |
2292 | for previously LE_ or LT_EXPR and GT_EXPR otherwise, but | |
2293 | the comments next to the enclosing if suggest it's not | |
2294 | generally profitable to do so. */ | |
2295 | vro.intersect (vr0); | |
2296 | if (vro.undefined_p ()) | |
2297 | return boolean_false_node; | |
2298 | vri.intersect (vr0); | |
2299 | if (vri.undefined_p ()) | |
2300 | return boolean_true_node; | |
2301 | } | |
c2ad9885 JL |
2302 | } |
2303 | ||
2304 | if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges | |
2305 | (code, op0, op1, strict_overflow_p))) | |
2306 | return ret; | |
2307 | if (only_ranges) | |
2308 | *only_ranges = false; | |
2309 | /* Do not use compare_names during propagation, it's quadratic. */ | |
2310 | if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME | |
2311 | && use_equiv_p) | |
2312 | return compare_names (code, op0, op1, strict_overflow_p); | |
2313 | else if (TREE_CODE (op0) == SSA_NAME) | |
2314 | return compare_name_with_value (code, op0, op1, | |
2315 | strict_overflow_p, use_equiv_p); | |
2316 | else if (TREE_CODE (op1) == SSA_NAME) | |
2317 | return compare_name_with_value (swap_tree_comparison (code), op1, op0, | |
2318 | strict_overflow_p, use_equiv_p); | |
2319 | return NULL_TREE; | |
2320 | } | |
2321 | ||
2322 | /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range | |
67914693 | 2323 | information. Return NULL if the conditional cannot be evaluated. |
c2ad9885 JL |
2324 | The ranges of all the names equivalent with the operands in COND |
2325 | will be used when trying to compute the value. If the result is | |
2326 | based on undefined signed overflow, issue a warning if | |
2327 | appropriate. */ | |
2328 | ||
2329 | tree | |
fc36b97a AH |
2330 | simplify_using_ranges::vrp_evaluate_conditional (tree_code code, tree op0, |
2331 | tree op1, gimple *stmt) | |
c2ad9885 JL |
2332 | { |
2333 | bool sop; | |
2334 | tree ret; | |
2335 | bool only_ranges; | |
2336 | ||
2337 | /* Some passes and foldings leak constants with overflow flag set | |
2338 | into the IL. Avoid doing wrong things with these and bail out. */ | |
2339 | if ((TREE_CODE (op0) == INTEGER_CST | |
2340 | && TREE_OVERFLOW (op0)) | |
2341 | || (TREE_CODE (op1) == INTEGER_CST | |
2342 | && TREE_OVERFLOW (op1))) | |
2343 | return NULL_TREE; | |
2344 | ||
2345 | sop = false; | |
d8b8023c AH |
2346 | ret = vrp_evaluate_conditional_warnv_with_ops (stmt, code, op0, op1, true, |
2347 | &sop, &only_ranges); | |
c2ad9885 JL |
2348 | |
2349 | if (ret && sop) | |
2350 | { | |
2351 | enum warn_strict_overflow_code wc; | |
2352 | const char* warnmsg; | |
2353 | ||
2354 | if (is_gimple_min_invariant (ret)) | |
2355 | { | |
2356 | wc = WARN_STRICT_OVERFLOW_CONDITIONAL; | |
2357 | warnmsg = G_("assuming signed overflow does not occur when " | |
2358 | "simplifying conditional to constant"); | |
2359 | } | |
2360 | else | |
2361 | { | |
2362 | wc = WARN_STRICT_OVERFLOW_COMPARISON; | |
2363 | warnmsg = G_("assuming signed overflow does not occur when " | |
2364 | "simplifying conditional"); | |
2365 | } | |
2366 | ||
2367 | if (issue_strict_overflow_warning (wc)) | |
2368 | { | |
2369 | location_t location; | |
2370 | ||
2371 | if (!gimple_has_location (stmt)) | |
2372 | location = input_location; | |
2373 | else | |
2374 | location = gimple_location (stmt); | |
2375 | warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg); | |
2376 | } | |
2377 | } | |
2378 | ||
2379 | if (warn_type_limits | |
2380 | && ret && only_ranges | |
2381 | && TREE_CODE_CLASS (code) == tcc_comparison | |
2382 | && TREE_CODE (op0) == SSA_NAME) | |
2383 | { | |
2384 | /* If the comparison is being folded and the operand on the LHS | |
2385 | is being compared against a constant value that is outside of | |
2386 | the natural range of OP0's type, then the predicate will | |
2387 | always fold regardless of the value of OP0. If -Wtype-limits | |
2388 | was specified, emit a warning. */ | |
2389 | tree type = TREE_TYPE (op0); | |
a889e06a | 2390 | const value_range_equiv *vr0 = query->get_value_range (op0, stmt); |
c2ad9885 | 2391 | |
d79d9445 AH |
2392 | if (vr0->varying_p () |
2393 | && INTEGRAL_TYPE_P (type) | |
2394 | && is_gimple_min_invariant (op1)) | |
c2ad9885 JL |
2395 | { |
2396 | location_t location; | |
2397 | ||
2398 | if (!gimple_has_location (stmt)) | |
2399 | location = input_location; | |
2400 | else | |
2401 | location = gimple_location (stmt); | |
2402 | ||
2403 | warning_at (location, OPT_Wtype_limits, | |
2404 | integer_zerop (ret) | |
2405 | ? G_("comparison always false " | |
2406 | "due to limited range of data type") | |
2407 | : G_("comparison always true " | |
2408 | "due to limited range of data type")); | |
2409 | } | |
2410 | } | |
2411 | ||
2412 | return ret; | |
2413 | } | |
2414 | ||
2415 | ||
2416 | /* Visit conditional statement STMT. If we can determine which edge | |
2417 | will be taken out of STMT's basic block, record it in | |
2418 | *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */ | |
2419 | ||
2420 | void | |
fc36b97a | 2421 | simplify_using_ranges::vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p) |
c2ad9885 JL |
2422 | { |
2423 | tree val; | |
2424 | ||
2425 | *taken_edge_p = NULL; | |
2426 | ||
2427 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2428 | { | |
2429 | tree use; | |
2430 | ssa_op_iter i; | |
2431 | ||
2432 | fprintf (dump_file, "\nVisiting conditional with predicate: "); | |
2433 | print_gimple_stmt (dump_file, stmt, 0); | |
2434 | fprintf (dump_file, "\nWith known ranges\n"); | |
2435 | ||
2436 | FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE) | |
2437 | { | |
2438 | fprintf (dump_file, "\t"); | |
2439 | print_generic_expr (dump_file, use); | |
2440 | fprintf (dump_file, ": "); | |
a889e06a | 2441 | dump_value_range (dump_file, query->get_value_range (use, stmt)); |
c2ad9885 JL |
2442 | } |
2443 | ||
2444 | fprintf (dump_file, "\n"); | |
2445 | } | |
2446 | ||
2447 | /* Compute the value of the predicate COND by checking the known | |
2448 | ranges of each of its operands. | |
2449 | ||
2450 | Note that we cannot evaluate all the equivalent ranges here | |
2451 | because those ranges may not yet be final and with the current | |
2452 | propagation strategy, we cannot determine when the value ranges | |
2453 | of the names in the equivalence set have changed. | |
2454 | ||
2455 | For instance, given the following code fragment | |
2456 | ||
2457 | i_5 = PHI <8, i_13> | |
2458 | ... | |
2459 | i_14 = ASSERT_EXPR <i_5, i_5 != 0> | |
2460 | if (i_14 == 1) | |
2461 | ... | |
2462 | ||
2463 | Assume that on the first visit to i_14, i_5 has the temporary | |
2464 | range [8, 8] because the second argument to the PHI function is | |
2465 | not yet executable. We derive the range ~[0, 0] for i_14 and the | |
2466 | equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for | |
2467 | the first time, since i_14 is equivalent to the range [8, 8], we | |
2468 | determine that the predicate is always false. | |
2469 | ||
2470 | On the next round of propagation, i_13 is determined to be | |
2471 | VARYING, which causes i_5 to drop down to VARYING. So, another | |
2472 | visit to i_14 is scheduled. In this second visit, we compute the | |
2473 | exact same range and equivalence set for i_14, namely ~[0, 0] and | |
2474 | { i_5 }. But we did not have the previous range for i_5 | |
2475 | registered, so vrp_visit_assignment thinks that the range for | |
2476 | i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)' | |
2477 | is not visited again, which stops propagation from visiting | |
2478 | statements in the THEN clause of that if(). | |
2479 | ||
2480 | To properly fix this we would need to keep the previous range | |
2481 | value for the names in the equivalence set. This way we would've | |
2482 | discovered that from one visit to the other i_5 changed from | |
2483 | range [8, 8] to VR_VARYING. | |
2484 | ||
2485 | However, fixing this apparent limitation may not be worth the | |
2486 | additional checking. Testing on several code bases (GCC, DLV, | |
2487 | MICO, TRAMP3D and SPEC2000) showed that doing this results in | |
2488 | 4 more predicates folded in SPEC. */ | |
2489 | ||
2490 | bool sop; | |
d8b8023c AH |
2491 | val = vrp_evaluate_conditional_warnv_with_ops (stmt, |
2492 | gimple_cond_code (stmt), | |
c2ad9885 JL |
2493 | gimple_cond_lhs (stmt), |
2494 | gimple_cond_rhs (stmt), | |
2495 | false, &sop, NULL); | |
2496 | if (val) | |
2497 | *taken_edge_p = find_taken_edge (gimple_bb (stmt), val); | |
2498 | ||
2499 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2500 | { | |
2501 | fprintf (dump_file, "\nPredicate evaluates to: "); | |
2502 | if (val == NULL_TREE) | |
2503 | fprintf (dump_file, "DON'T KNOW\n"); | |
2504 | else | |
2505 | print_generic_stmt (dump_file, val); | |
2506 | } | |
2507 | } | |
2508 | ||
2509 | /* Searches the case label vector VEC for the ranges of CASE_LABELs that are | |
2510 | used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and | |
2511 | MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1. | |
2512 | Returns true if the default label is not needed. */ | |
2513 | ||
2514 | static bool | |
fc36b97a | 2515 | find_case_label_ranges (gswitch *stmt, const value_range *vr, |
028d81b1 AH |
2516 | size_t *min_idx1, size_t *max_idx1, |
2517 | size_t *min_idx2, size_t *max_idx2) | |
c2ad9885 JL |
2518 | { |
2519 | size_t i, j, k, l; | |
2520 | unsigned int n = gimple_switch_num_labels (stmt); | |
2521 | bool take_default; | |
2522 | tree case_low, case_high; | |
54994253 | 2523 | tree min = vr->min (), max = vr->max (); |
c2ad9885 | 2524 | |
54994253 | 2525 | gcc_checking_assert (!vr->varying_p () && !vr->undefined_p ()); |
c2ad9885 JL |
2526 | |
2527 | take_default = !find_case_label_range (stmt, min, max, &i, &j); | |
2528 | ||
eea18a4e | 2529 | /* Set second range to empty. */ |
c2ad9885 JL |
2530 | *min_idx2 = 1; |
2531 | *max_idx2 = 0; | |
2532 | ||
54994253 | 2533 | if (vr->kind () == VR_RANGE) |
c2ad9885 JL |
2534 | { |
2535 | *min_idx1 = i; | |
2536 | *max_idx1 = j; | |
2537 | return !take_default; | |
2538 | } | |
2539 | ||
2540 | /* Set first range to all case labels. */ | |
2541 | *min_idx1 = 1; | |
2542 | *max_idx1 = n - 1; | |
2543 | ||
2544 | if (i > j) | |
2545 | return false; | |
2546 | ||
2547 | /* Make sure all the values of case labels [i , j] are contained in | |
2548 | range [MIN, MAX]. */ | |
2549 | case_low = CASE_LOW (gimple_switch_label (stmt, i)); | |
2550 | case_high = CASE_HIGH (gimple_switch_label (stmt, j)); | |
2551 | if (tree_int_cst_compare (case_low, min) < 0) | |
2552 | i += 1; | |
2553 | if (case_high != NULL_TREE | |
2554 | && tree_int_cst_compare (max, case_high) < 0) | |
2555 | j -= 1; | |
2556 | ||
2557 | if (i > j) | |
2558 | return false; | |
2559 | ||
2560 | /* If the range spans case labels [i, j], the corresponding anti-range spans | |
2561 | the labels [1, i - 1] and [j + 1, n - 1]. */ | |
2562 | k = j + 1; | |
2563 | l = n - 1; | |
2564 | if (k > l) | |
2565 | { | |
2566 | k = 1; | |
2567 | l = 0; | |
2568 | } | |
2569 | ||
2570 | j = i - 1; | |
2571 | i = 1; | |
2572 | if (i > j) | |
2573 | { | |
2574 | i = k; | |
2575 | j = l; | |
2576 | k = 1; | |
2577 | l = 0; | |
2578 | } | |
2579 | ||
2580 | *min_idx1 = i; | |
2581 | *max_idx1 = j; | |
2582 | *min_idx2 = k; | |
2583 | *max_idx2 = l; | |
2584 | return false; | |
2585 | } | |
2586 | ||
2587 | /* Visit switch statement STMT. If we can determine which edge | |
2588 | will be taken out of STMT's basic block, record it in | |
2589 | *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */ | |
2590 | ||
2591 | void | |
2592 | vr_values::vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p) | |
2593 | { | |
2594 | tree op, val; | |
028d81b1 | 2595 | const value_range_equiv *vr; |
c2ad9885 JL |
2596 | size_t i = 0, j = 0, k, l; |
2597 | bool take_default; | |
2598 | ||
2599 | *taken_edge_p = NULL; | |
2600 | op = gimple_switch_index (stmt); | |
2601 | if (TREE_CODE (op) != SSA_NAME) | |
2602 | return; | |
2603 | ||
2604 | vr = get_value_range (op); | |
2605 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2606 | { | |
2607 | fprintf (dump_file, "\nVisiting switch expression with operand "); | |
2608 | print_generic_expr (dump_file, op); | |
2609 | fprintf (dump_file, " with known range "); | |
2610 | dump_value_range (dump_file, vr); | |
2611 | fprintf (dump_file, "\n"); | |
2612 | } | |
2613 | ||
54994253 AH |
2614 | if (vr->undefined_p () |
2615 | || vr->varying_p () | |
2616 | || vr->symbolic_p ()) | |
c2ad9885 JL |
2617 | return; |
2618 | ||
2619 | /* Find the single edge that is taken from the switch expression. */ | |
2620 | take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l); | |
2621 | ||
2622 | /* Check if the range spans no CASE_LABEL. If so, we only reach the default | |
2623 | label */ | |
2624 | if (j < i) | |
2625 | { | |
2626 | gcc_assert (take_default); | |
2627 | val = gimple_switch_default_label (stmt); | |
2628 | } | |
2629 | else | |
2630 | { | |
2631 | /* Check if labels with index i to j and maybe the default label | |
2632 | are all reaching the same label. */ | |
2633 | ||
2634 | val = gimple_switch_label (stmt, i); | |
2635 | if (take_default | |
2636 | && CASE_LABEL (gimple_switch_default_label (stmt)) | |
2637 | != CASE_LABEL (val)) | |
2638 | { | |
2639 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2640 | fprintf (dump_file, " not a single destination for this " | |
2641 | "range\n"); | |
2642 | return; | |
2643 | } | |
2644 | for (++i; i <= j; ++i) | |
2645 | { | |
2646 | if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val)) | |
2647 | { | |
2648 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2649 | fprintf (dump_file, " not a single destination for this " | |
2650 | "range\n"); | |
2651 | return; | |
2652 | } | |
2653 | } | |
2654 | for (; k <= l; ++k) | |
2655 | { | |
2656 | if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val)) | |
2657 | { | |
2658 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2659 | fprintf (dump_file, " not a single destination for this " | |
2660 | "range\n"); | |
2661 | return; | |
2662 | } | |
2663 | } | |
2664 | } | |
2665 | ||
2666 | *taken_edge_p = find_edge (gimple_bb (stmt), | |
61ff5d6f | 2667 | label_to_block (cfun, CASE_LABEL (val))); |
c2ad9885 JL |
2668 | |
2669 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2670 | { | |
2671 | fprintf (dump_file, " will take edge to "); | |
2672 | print_generic_stmt (dump_file, CASE_LABEL (val)); | |
2673 | } | |
2674 | } | |
2675 | ||
2676 | ||
2677 | /* Evaluate statement STMT. If the statement produces a useful range, | |
2678 | set VR and corepsponding OUTPUT_P. | |
2679 | ||
2680 | If STMT is a conditional branch and we can determine its truth | |
2681 | value, the taken edge is recorded in *TAKEN_EDGE_P. */ | |
2682 | ||
2683 | void | |
2684 | vr_values::extract_range_from_stmt (gimple *stmt, edge *taken_edge_p, | |
028d81b1 | 2685 | tree *output_p, value_range_equiv *vr) |
c2ad9885 JL |
2686 | { |
2687 | ||
2688 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2689 | { | |
1396fa5b | 2690 | fprintf (dump_file, "\nextract_range_from_stmt visiting:\n"); |
c2ad9885 JL |
2691 | print_gimple_stmt (dump_file, stmt, 0, dump_flags); |
2692 | } | |
2693 | ||
2694 | if (!stmt_interesting_for_vrp (stmt)) | |
2695 | gcc_assert (stmt_ends_bb_p (stmt)); | |
2696 | else if (is_gimple_assign (stmt) || is_gimple_call (stmt)) | |
2697 | vrp_visit_assignment_or_call (stmt, output_p, vr); | |
2698 | else if (gimple_code (stmt) == GIMPLE_COND) | |
fc36b97a | 2699 | simplifier.vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p); |
c2ad9885 JL |
2700 | else if (gimple_code (stmt) == GIMPLE_SWITCH) |
2701 | vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p); | |
2702 | } | |
2703 | ||
2704 | /* Visit all arguments for PHI node PHI that flow through executable | |
2705 | edges. If a valid value range can be derived from all the incoming | |
2706 | value ranges, set a new range in VR_RESULT. */ | |
2707 | ||
2708 | void | |
028d81b1 AH |
2709 | vr_values::extract_range_from_phi_node (gphi *phi, |
2710 | value_range_equiv *vr_result) | |
c2ad9885 | 2711 | { |
c2ad9885 | 2712 | tree lhs = PHI_RESULT (phi); |
028d81b1 | 2713 | const value_range_equiv *lhs_vr = get_value_range (lhs); |
c2ad9885 | 2714 | bool first = true; |
028d81b1 | 2715 | int old_edges; |
99b1c316 | 2716 | class loop *l; |
c2ad9885 JL |
2717 | |
2718 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2719 | { | |
2720 | fprintf (dump_file, "\nVisiting PHI node: "); | |
2721 | print_gimple_stmt (dump_file, phi, 0, dump_flags); | |
2722 | } | |
2723 | ||
2724 | bool may_simulate_backedge_again = false; | |
028d81b1 AH |
2725 | int edges = 0; |
2726 | for (size_t i = 0; i < gimple_phi_num_args (phi); i++) | |
c2ad9885 JL |
2727 | { |
2728 | edge e = gimple_phi_arg_edge (phi, i); | |
2729 | ||
2730 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2731 | { | |
2732 | fprintf (dump_file, | |
2733 | " Argument #%d (%d -> %d %sexecutable)\n", | |
2734 | (int) i, e->src->index, e->dest->index, | |
2735 | (e->flags & EDGE_EXECUTABLE) ? "" : "not "); | |
2736 | } | |
2737 | ||
2738 | if (e->flags & EDGE_EXECUTABLE) | |
2739 | { | |
028d81b1 AH |
2740 | value_range_equiv vr_arg_tem; |
2741 | const value_range_equiv *vr_arg = &vr_arg_tem; | |
c2ad9885 JL |
2742 | |
2743 | ++edges; | |
2744 | ||
028d81b1 | 2745 | tree arg = PHI_ARG_DEF (phi, i); |
c2ad9885 JL |
2746 | if (TREE_CODE (arg) == SSA_NAME) |
2747 | { | |
2748 | /* See if we are eventually going to change one of the args. */ | |
2749 | gimple *def_stmt = SSA_NAME_DEF_STMT (arg); | |
2750 | if (! gimple_nop_p (def_stmt) | |
2751 | && prop_simulate_again_p (def_stmt) | |
2752 | && e->flags & EDGE_DFS_BACK) | |
2753 | may_simulate_backedge_again = true; | |
2754 | ||
028d81b1 | 2755 | const value_range_equiv *vr_arg_ = get_value_range (arg); |
c2ad9885 JL |
2756 | /* Do not allow equivalences or symbolic ranges to leak in from |
2757 | backedges. That creates invalid equivalencies. | |
2758 | See PR53465 and PR54767. */ | |
2759 | if (e->flags & EDGE_DFS_BACK) | |
2760 | { | |
27922d51 | 2761 | if (!vr_arg_->varying_p () && !vr_arg_->undefined_p ()) |
c2ad9885 | 2762 | { |
5d462877 AH |
2763 | vr_arg_tem.set (vr_arg_->min (), vr_arg_->max (), NULL, |
2764 | vr_arg_->kind ()); | |
27922d51 | 2765 | if (vr_arg_tem.symbolic_p ()) |
97ecc8d5 | 2766 | vr_arg_tem.set_varying (TREE_TYPE (arg)); |
c2ad9885 | 2767 | } |
27922d51 RB |
2768 | else |
2769 | vr_arg = vr_arg_; | |
c2ad9885 | 2770 | } |
54994253 AH |
2771 | /* If the non-backedge arguments range is VR_VARYING then |
2772 | we can still try recording a simple equivalence. */ | |
27922d51 RB |
2773 | else if (vr_arg_->varying_p ()) |
2774 | vr_arg_tem.set (arg); | |
2775 | else | |
2776 | vr_arg = vr_arg_; | |
c2ad9885 JL |
2777 | } |
2778 | else | |
2779 | { | |
2780 | if (TREE_OVERFLOW_P (arg)) | |
2781 | arg = drop_tree_overflow (arg); | |
2782 | ||
27922d51 | 2783 | vr_arg_tem.set (arg); |
c2ad9885 JL |
2784 | } |
2785 | ||
2786 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2787 | { | |
2788 | fprintf (dump_file, "\t"); | |
2789 | print_generic_expr (dump_file, arg, dump_flags); | |
2790 | fprintf (dump_file, ": "); | |
27922d51 | 2791 | dump_value_range (dump_file, vr_arg); |
c2ad9885 JL |
2792 | fprintf (dump_file, "\n"); |
2793 | } | |
2794 | ||
2795 | if (first) | |
27922d51 | 2796 | vr_result->deep_copy (vr_arg); |
c2ad9885 | 2797 | else |
27922d51 | 2798 | vr_result->union_ (vr_arg); |
c2ad9885 JL |
2799 | first = false; |
2800 | ||
54994253 | 2801 | if (vr_result->varying_p ()) |
c2ad9885 JL |
2802 | break; |
2803 | } | |
2804 | } | |
2805 | ||
54994253 | 2806 | if (vr_result->varying_p ()) |
c2ad9885 | 2807 | goto varying; |
54994253 | 2808 | else if (vr_result->undefined_p ()) |
c2ad9885 JL |
2809 | goto update_range; |
2810 | ||
2811 | old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)]; | |
2812 | vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges; | |
2813 | ||
2814 | /* To prevent infinite iterations in the algorithm, derive ranges | |
2815 | when the new value is slightly bigger or smaller than the | |
2816 | previous one. We don't do this if we have seen a new executable | |
2817 | edge; this helps us avoid an infinity for conditionals | |
2818 | which are not in a loop. If the old value-range was VR_UNDEFINED | |
2819 | use the updated range and iterate one more time. If we will not | |
2820 | simulate this PHI again via the backedge allow us to iterate. */ | |
2821 | if (edges > 0 | |
2822 | && gimple_phi_num_args (phi) > 1 | |
2823 | && edges == old_edges | |
54994253 | 2824 | && !lhs_vr->undefined_p () |
c2ad9885 JL |
2825 | && may_simulate_backedge_again) |
2826 | { | |
2827 | /* Compare old and new ranges, fall back to varying if the | |
2828 | values are not comparable. */ | |
54994253 | 2829 | int cmp_min = compare_values (lhs_vr->min (), vr_result->min ()); |
c2ad9885 JL |
2830 | if (cmp_min == -2) |
2831 | goto varying; | |
54994253 | 2832 | int cmp_max = compare_values (lhs_vr->max (), vr_result->max ()); |
c2ad9885 JL |
2833 | if (cmp_max == -2) |
2834 | goto varying; | |
2835 | ||
2836 | /* For non VR_RANGE or for pointers fall back to varying if | |
2837 | the range changed. */ | |
54994253 | 2838 | if ((lhs_vr->kind () != VR_RANGE || vr_result->kind () != VR_RANGE |
c2ad9885 JL |
2839 | || POINTER_TYPE_P (TREE_TYPE (lhs))) |
2840 | && (cmp_min != 0 || cmp_max != 0)) | |
2841 | goto varying; | |
2842 | ||
2843 | /* If the new minimum is larger than the previous one | |
2844 | retain the old value. If the new minimum value is smaller | |
2845 | than the previous one and not -INF go all the way to -INF + 1. | |
2846 | In the first case, to avoid infinite bouncing between different | |
2847 | minimums, and in the other case to avoid iterating millions of | |
2848 | times to reach -INF. Going to -INF + 1 also lets the following | |
2849 | iteration compute whether there will be any overflow, at the | |
2850 | expense of one additional iteration. */ | |
54994253 AH |
2851 | tree new_min = vr_result->min (); |
2852 | tree new_max = vr_result->max (); | |
c2ad9885 | 2853 | if (cmp_min < 0) |
54994253 | 2854 | new_min = lhs_vr->min (); |
c2ad9885 | 2855 | else if (cmp_min > 0 |
dee75828 RB |
2856 | && (TREE_CODE (vr_result->min ()) != INTEGER_CST |
2857 | || tree_int_cst_lt (vrp_val_min (vr_result->type ()), | |
2858 | vr_result->min ()))) | |
54994253 AH |
2859 | new_min = int_const_binop (PLUS_EXPR, |
2860 | vrp_val_min (vr_result->type ()), | |
2861 | build_int_cst (vr_result->type (), 1)); | |
c2ad9885 JL |
2862 | |
2863 | /* Similarly for the maximum value. */ | |
2864 | if (cmp_max > 0) | |
54994253 | 2865 | new_max = lhs_vr->max (); |
c2ad9885 | 2866 | else if (cmp_max < 0 |
dee75828 RB |
2867 | && (TREE_CODE (vr_result->max ()) != INTEGER_CST |
2868 | || tree_int_cst_lt (vr_result->max (), | |
2869 | vrp_val_max (vr_result->type ())))) | |
54994253 AH |
2870 | new_max = int_const_binop (MINUS_EXPR, |
2871 | vrp_val_max (vr_result->type ()), | |
2872 | build_int_cst (vr_result->type (), 1)); | |
2873 | ||
5d462877 | 2874 | vr_result->update (new_min, new_max, vr_result->kind ()); |
c2ad9885 JL |
2875 | |
2876 | /* If we dropped either bound to +-INF then if this is a loop | |
2877 | PHI node SCEV may known more about its value-range. */ | |
2878 | if (cmp_min > 0 || cmp_min < 0 | |
2879 | || cmp_max < 0 || cmp_max > 0) | |
2880 | goto scev_check; | |
2881 | ||
2882 | goto infinite_check; | |
2883 | } | |
2884 | ||
2885 | goto update_range; | |
2886 | ||
2887 | varying: | |
97ecc8d5 | 2888 | vr_result->set_varying (TREE_TYPE (lhs)); |
c2ad9885 JL |
2889 | |
2890 | scev_check: | |
2891 | /* If this is a loop PHI node SCEV may known more about its value-range. | |
2892 | scev_check can be reached from two paths, one is a fall through from above | |
2893 | "varying" label, the other is direct goto from code block which tries to | |
2894 | avoid infinite simulation. */ | |
5e4a80e8 JL |
2895 | if (scev_initialized_p () |
2896 | && (l = loop_containing_stmt (phi)) | |
c2ad9885 JL |
2897 | && l->header == gimple_bb (phi)) |
2898 | adjust_range_with_scev (vr_result, l, phi, lhs); | |
2899 | ||
2900 | infinite_check: | |
2901 | /* If we will end up with a (-INF, +INF) range, set it to | |
2902 | VARYING. Same if the previous max value was invalid for | |
2903 | the type and we end up with vr_result.min > vr_result.max. */ | |
54994253 AH |
2904 | if ((!vr_result->varying_p () && !vr_result->undefined_p ()) |
2905 | && !((vrp_val_is_max (vr_result->max ()) && vrp_val_is_min (vr_result->min ())) | |
2906 | || compare_values (vr_result->min (), vr_result->max ()) > 0)) | |
c2ad9885 JL |
2907 | ; |
2908 | else | |
97ecc8d5 | 2909 | vr_result->set_varying (TREE_TYPE (lhs)); |
c2ad9885 JL |
2910 | |
2911 | /* If the new range is different than the previous value, keep | |
2912 | iterating. */ | |
2913 | update_range: | |
2914 | return; | |
2915 | } | |
2916 | ||
2917 | /* Simplify boolean operations if the source is known | |
2918 | to be already a boolean. */ | |
2919 | bool | |
fc36b97a AH |
2920 | simplify_using_ranges::simplify_truth_ops_using_ranges |
2921 | (gimple_stmt_iterator *gsi, | |
2922 | gimple *stmt) | |
c2ad9885 JL |
2923 | { |
2924 | enum tree_code rhs_code = gimple_assign_rhs_code (stmt); | |
2925 | tree lhs, op0, op1; | |
2926 | bool need_conversion; | |
2927 | ||
2928 | /* We handle only !=/== case here. */ | |
2929 | gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR); | |
2930 | ||
2931 | op0 = gimple_assign_rhs1 (stmt); | |
2932 | if (!op_with_boolean_value_range_p (op0)) | |
2933 | return false; | |
2934 | ||
2935 | op1 = gimple_assign_rhs2 (stmt); | |
2936 | if (!op_with_boolean_value_range_p (op1)) | |
2937 | return false; | |
2938 | ||
2939 | /* Reduce number of cases to handle to NE_EXPR. As there is no | |
2940 | BIT_XNOR_EXPR we cannot replace A == B with a single statement. */ | |
2941 | if (rhs_code == EQ_EXPR) | |
2942 | { | |
2943 | if (TREE_CODE (op1) == INTEGER_CST) | |
2944 | op1 = int_const_binop (BIT_XOR_EXPR, op1, | |
2945 | build_int_cst (TREE_TYPE (op1), 1)); | |
2946 | else | |
2947 | return false; | |
2948 | } | |
2949 | ||
2950 | lhs = gimple_assign_lhs (stmt); | |
2951 | need_conversion | |
2952 | = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0)); | |
2953 | ||
2954 | /* Make sure to not sign-extend a 1-bit 1 when converting the result. */ | |
2955 | if (need_conversion | |
2956 | && !TYPE_UNSIGNED (TREE_TYPE (op0)) | |
2957 | && TYPE_PRECISION (TREE_TYPE (op0)) == 1 | |
2958 | && TYPE_PRECISION (TREE_TYPE (lhs)) > 1) | |
2959 | return false; | |
2960 | ||
2961 | /* For A != 0 we can substitute A itself. */ | |
2962 | if (integer_zerop (op1)) | |
2963 | gimple_assign_set_rhs_with_ops (gsi, | |
2964 | need_conversion | |
2965 | ? NOP_EXPR : TREE_CODE (op0), op0); | |
2966 | /* For A != B we substitute A ^ B. Either with conversion. */ | |
2967 | else if (need_conversion) | |
2968 | { | |
2969 | tree tem = make_ssa_name (TREE_TYPE (op0)); | |
2970 | gassign *newop | |
2971 | = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1); | |
2972 | gsi_insert_before (gsi, newop, GSI_SAME_STMT); | |
2973 | if (INTEGRAL_TYPE_P (TREE_TYPE (tem)) | |
2974 | && TYPE_PRECISION (TREE_TYPE (tem)) > 1) | |
2975 | set_range_info (tem, VR_RANGE, | |
2976 | wi::zero (TYPE_PRECISION (TREE_TYPE (tem))), | |
2977 | wi::one (TYPE_PRECISION (TREE_TYPE (tem)))); | |
2978 | gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem); | |
2979 | } | |
2980 | /* Or without. */ | |
2981 | else | |
2982 | gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1); | |
2983 | update_stmt (gsi_stmt (*gsi)); | |
2984 | fold_stmt (gsi, follow_single_use_edges); | |
2985 | ||
2986 | return true; | |
2987 | } | |
2988 | ||
2989 | /* Simplify a division or modulo operator to a right shift or bitwise and | |
2990 | if the first operand is unsigned or is greater than zero and the second | |
2991 | operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with | |
2992 | constant op1 (op1min = op1) or with op1 in [op1min, op1max] range, | |
2993 | optimize it into just op0 if op0's range is known to be a subset of | |
2994 | [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned | |
2995 | modulo. */ | |
2996 | ||
2997 | bool | |
fc36b97a AH |
2998 | simplify_using_ranges::simplify_div_or_mod_using_ranges |
2999 | (gimple_stmt_iterator *gsi, | |
3000 | gimple *stmt) | |
c2ad9885 JL |
3001 | { |
3002 | enum tree_code rhs_code = gimple_assign_rhs_code (stmt); | |
3003 | tree val = NULL; | |
3004 | tree op0 = gimple_assign_rhs1 (stmt); | |
3005 | tree op1 = gimple_assign_rhs2 (stmt); | |
3006 | tree op0min = NULL_TREE, op0max = NULL_TREE; | |
3007 | tree op1min = op1; | |
fc36b97a | 3008 | const value_range *vr = NULL; |
c2ad9885 JL |
3009 | |
3010 | if (TREE_CODE (op0) == INTEGER_CST) | |
3011 | { | |
3012 | op0min = op0; | |
3013 | op0max = op0; | |
3014 | } | |
3015 | else | |
3016 | { | |
a889e06a | 3017 | vr = query->get_value_range (op0, stmt); |
c2ad9885 JL |
3018 | if (range_int_cst_p (vr)) |
3019 | { | |
54994253 AH |
3020 | op0min = vr->min (); |
3021 | op0max = vr->max (); | |
c2ad9885 JL |
3022 | } |
3023 | } | |
3024 | ||
3025 | if (rhs_code == TRUNC_MOD_EXPR | |
3026 | && TREE_CODE (op1) == SSA_NAME) | |
3027 | { | |
a889e06a | 3028 | const value_range_equiv *vr1 = query->get_value_range (op1, stmt); |
c2ad9885 | 3029 | if (range_int_cst_p (vr1)) |
54994253 | 3030 | op1min = vr1->min (); |
c2ad9885 JL |
3031 | } |
3032 | if (rhs_code == TRUNC_MOD_EXPR | |
3033 | && TREE_CODE (op1min) == INTEGER_CST | |
3034 | && tree_int_cst_sgn (op1min) == 1 | |
3035 | && op0max | |
3036 | && tree_int_cst_lt (op0max, op1min)) | |
3037 | { | |
3038 | if (TYPE_UNSIGNED (TREE_TYPE (op0)) | |
3039 | || tree_int_cst_sgn (op0min) >= 0 | |
3040 | || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min), | |
3041 | op0min)) | |
3042 | { | |
3043 | /* If op0 already has the range op0 % op1 has, | |
3044 | then TRUNC_MOD_EXPR won't change anything. */ | |
3045 | gimple_assign_set_rhs_from_tree (gsi, op0); | |
3046 | return true; | |
3047 | } | |
3048 | } | |
3049 | ||
3050 | if (TREE_CODE (op0) != SSA_NAME) | |
3051 | return false; | |
3052 | ||
3053 | if (!integer_pow2p (op1)) | |
3054 | { | |
3055 | /* X % -Y can be only optimized into X % Y either if | |
3056 | X is not INT_MIN, or Y is not -1. Fold it now, as after | |
3057 | remove_range_assertions the range info might be not available | |
3058 | anymore. */ | |
3059 | if (rhs_code == TRUNC_MOD_EXPR | |
3060 | && fold_stmt (gsi, follow_single_use_edges)) | |
3061 | return true; | |
3062 | return false; | |
3063 | } | |
3064 | ||
3065 | if (TYPE_UNSIGNED (TREE_TYPE (op0))) | |
3066 | val = integer_one_node; | |
3067 | else | |
3068 | { | |
3069 | bool sop = false; | |
3070 | ||
3071 | val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop); | |
3072 | ||
3073 | if (val | |
3074 | && sop | |
3075 | && integer_onep (val) | |
3076 | && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC)) | |
3077 | { | |
3078 | location_t location; | |
3079 | ||
3080 | if (!gimple_has_location (stmt)) | |
3081 | location = input_location; | |
3082 | else | |
3083 | location = gimple_location (stmt); | |
3084 | warning_at (location, OPT_Wstrict_overflow, | |
3085 | "assuming signed overflow does not occur when " | |
3086 | "simplifying %</%> or %<%%%> to %<>>%> or %<&%>"); | |
3087 | } | |
3088 | } | |
3089 | ||
3090 | if (val && integer_onep (val)) | |
3091 | { | |
3092 | tree t; | |
3093 | ||
3094 | if (rhs_code == TRUNC_DIV_EXPR) | |
3095 | { | |
3096 | t = build_int_cst (integer_type_node, tree_log2 (op1)); | |
3097 | gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR); | |
3098 | gimple_assign_set_rhs1 (stmt, op0); | |
3099 | gimple_assign_set_rhs2 (stmt, t); | |
3100 | } | |
3101 | else | |
3102 | { | |
3103 | t = build_int_cst (TREE_TYPE (op1), 1); | |
3104 | t = int_const_binop (MINUS_EXPR, op1, t); | |
3105 | t = fold_convert (TREE_TYPE (op0), t); | |
3106 | ||
3107 | gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR); | |
3108 | gimple_assign_set_rhs1 (stmt, op0); | |
3109 | gimple_assign_set_rhs2 (stmt, t); | |
3110 | } | |
3111 | ||
3112 | update_stmt (stmt); | |
3113 | fold_stmt (gsi, follow_single_use_edges); | |
3114 | return true; | |
3115 | } | |
3116 | ||
3117 | return false; | |
3118 | } | |
3119 | ||
3120 | /* Simplify a min or max if the ranges of the two operands are | |
3121 | disjoint. Return true if we do simplify. */ | |
3122 | ||
3123 | bool | |
fc36b97a AH |
3124 | simplify_using_ranges::simplify_min_or_max_using_ranges |
3125 | (gimple_stmt_iterator *gsi, | |
3126 | gimple *stmt) | |
c2ad9885 JL |
3127 | { |
3128 | tree op0 = gimple_assign_rhs1 (stmt); | |
3129 | tree op1 = gimple_assign_rhs2 (stmt); | |
3130 | bool sop = false; | |
3131 | tree val; | |
3132 | ||
3133 | val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges | |
3134 | (LE_EXPR, op0, op1, &sop)); | |
3135 | if (!val) | |
3136 | { | |
3137 | sop = false; | |
3138 | val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges | |
3139 | (LT_EXPR, op0, op1, &sop)); | |
3140 | } | |
3141 | ||
3142 | if (val) | |
3143 | { | |
3144 | if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC)) | |
3145 | { | |
3146 | location_t location; | |
3147 | ||
3148 | if (!gimple_has_location (stmt)) | |
3149 | location = input_location; | |
3150 | else | |
3151 | location = gimple_location (stmt); | |
3152 | warning_at (location, OPT_Wstrict_overflow, | |
3153 | "assuming signed overflow does not occur when " | |
3154 | "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>"); | |
3155 | } | |
3156 | ||
3157 | /* VAL == TRUE -> OP0 < or <= op1 | |
3158 | VAL == FALSE -> OP0 > or >= op1. */ | |
3159 | tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR) | |
3160 | == integer_zerop (val)) ? op0 : op1; | |
3161 | gimple_assign_set_rhs_from_tree (gsi, res); | |
3162 | return true; | |
3163 | } | |
3164 | ||
3165 | return false; | |
3166 | } | |
3167 | ||
3168 | /* If the operand to an ABS_EXPR is >= 0, then eliminate the | |
3169 | ABS_EXPR. If the operand is <= 0, then simplify the | |
3170 | ABS_EXPR into a NEGATE_EXPR. */ | |
3171 | ||
3172 | bool | |
fc36b97a AH |
3173 | simplify_using_ranges::simplify_abs_using_ranges (gimple_stmt_iterator *gsi, |
3174 | gimple *stmt) | |
c2ad9885 JL |
3175 | { |
3176 | tree op = gimple_assign_rhs1 (stmt); | |
a889e06a | 3177 | const value_range *vr = query->get_value_range (op, stmt); |
c2ad9885 JL |
3178 | |
3179 | if (vr) | |
3180 | { | |
3181 | tree val = NULL; | |
3182 | bool sop = false; | |
3183 | ||
3184 | val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop); | |
3185 | if (!val) | |
3186 | { | |
3187 | /* The range is neither <= 0 nor > 0. Now see if it is | |
3188 | either < 0 or >= 0. */ | |
3189 | sop = false; | |
3190 | val = compare_range_with_value (LT_EXPR, vr, integer_zero_node, | |
3191 | &sop); | |
3192 | } | |
3193 | ||
3194 | if (val) | |
3195 | { | |
3196 | if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC)) | |
3197 | { | |
3198 | location_t location; | |
3199 | ||
3200 | if (!gimple_has_location (stmt)) | |
3201 | location = input_location; | |
3202 | else | |
3203 | location = gimple_location (stmt); | |
3204 | warning_at (location, OPT_Wstrict_overflow, | |
3205 | "assuming signed overflow does not occur when " | |
3206 | "simplifying %<abs (X)%> to %<X%> or %<-X%>"); | |
3207 | } | |
3208 | ||
3209 | gimple_assign_set_rhs1 (stmt, op); | |
3210 | if (integer_zerop (val)) | |
3211 | gimple_assign_set_rhs_code (stmt, SSA_NAME); | |
3212 | else | |
3213 | gimple_assign_set_rhs_code (stmt, NEGATE_EXPR); | |
3214 | update_stmt (stmt); | |
3215 | fold_stmt (gsi, follow_single_use_edges); | |
3216 | return true; | |
3217 | } | |
3218 | } | |
3219 | ||
3220 | return false; | |
3221 | } | |
3222 | ||
8f119c55 AH |
3223 | /* value_range wrapper for wi_set_zero_nonzero_bits. |
3224 | ||
3225 | Return TRUE if VR was a constant range and we were able to compute | |
3226 | the bit masks. */ | |
3227 | ||
3228 | static bool | |
3229 | vr_set_zero_nonzero_bits (const tree expr_type, | |
028d81b1 | 3230 | const value_range *vr, |
8f119c55 AH |
3231 | wide_int *may_be_nonzero, |
3232 | wide_int *must_be_nonzero) | |
3233 | { | |
3234 | if (range_int_cst_p (vr)) | |
3235 | { | |
3236 | wi_set_zero_nonzero_bits (expr_type, | |
3237 | wi::to_wide (vr->min ()), | |
3238 | wi::to_wide (vr->max ()), | |
3239 | *may_be_nonzero, *must_be_nonzero); | |
3240 | return true; | |
3241 | } | |
3242 | *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type)); | |
3243 | *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type)); | |
3244 | return false; | |
3245 | } | |
3246 | ||
c2ad9885 JL |
3247 | /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR. |
3248 | If all the bits that are being cleared by & are already | |
3249 | known to be zero from VR, or all the bits that are being | |
3250 | set by | are already known to be one from VR, the bit | |
3251 | operation is redundant. */ | |
3252 | ||
3253 | bool | |
fc36b97a AH |
3254 | simplify_using_ranges::simplify_bit_ops_using_ranges |
3255 | (gimple_stmt_iterator *gsi, | |
3256 | gimple *stmt) | |
c2ad9885 JL |
3257 | { |
3258 | tree op0 = gimple_assign_rhs1 (stmt); | |
3259 | tree op1 = gimple_assign_rhs2 (stmt); | |
3260 | tree op = NULL_TREE; | |
028d81b1 | 3261 | value_range vr0, vr1; |
c2ad9885 JL |
3262 | wide_int may_be_nonzero0, may_be_nonzero1; |
3263 | wide_int must_be_nonzero0, must_be_nonzero1; | |
3264 | wide_int mask; | |
3265 | ||
3266 | if (TREE_CODE (op0) == SSA_NAME) | |
a889e06a | 3267 | vr0 = *(query->get_value_range (op0, stmt)); |
c2ad9885 | 3268 | else if (is_gimple_min_invariant (op0)) |
27922d51 | 3269 | vr0.set (op0); |
c2ad9885 JL |
3270 | else |
3271 | return false; | |
3272 | ||
3273 | if (TREE_CODE (op1) == SSA_NAME) | |
a889e06a | 3274 | vr1 = *(query->get_value_range (op1, stmt)); |
c2ad9885 | 3275 | else if (is_gimple_min_invariant (op1)) |
27922d51 | 3276 | vr1.set (op1); |
c2ad9885 JL |
3277 | else |
3278 | return false; | |
3279 | ||
8f119c55 AH |
3280 | if (!vr_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0, |
3281 | &must_be_nonzero0)) | |
c2ad9885 | 3282 | return false; |
8f119c55 AH |
3283 | if (!vr_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1, |
3284 | &must_be_nonzero1)) | |
c2ad9885 JL |
3285 | return false; |
3286 | ||
3287 | switch (gimple_assign_rhs_code (stmt)) | |
3288 | { | |
3289 | case BIT_AND_EXPR: | |
3290 | mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1); | |
3291 | if (mask == 0) | |
3292 | { | |
3293 | op = op0; | |
3294 | break; | |
3295 | } | |
3296 | mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0); | |
3297 | if (mask == 0) | |
3298 | { | |
3299 | op = op1; | |
3300 | break; | |
3301 | } | |
3302 | break; | |
3303 | case BIT_IOR_EXPR: | |
3304 | mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1); | |
3305 | if (mask == 0) | |
3306 | { | |
3307 | op = op1; | |
3308 | break; | |
3309 | } | |
3310 | mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0); | |
3311 | if (mask == 0) | |
3312 | { | |
3313 | op = op0; | |
3314 | break; | |
3315 | } | |
3316 | break; | |
3317 | default: | |
3318 | gcc_unreachable (); | |
3319 | } | |
3320 | ||
3321 | if (op == NULL_TREE) | |
3322 | return false; | |
3323 | ||
3324 | gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op); | |
3325 | update_stmt (gsi_stmt (*gsi)); | |
3326 | return true; | |
3327 | } | |
3328 | ||
3329 | /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has | |
3330 | a known value range VR. | |
3331 | ||
3332 | If there is one and only one value which will satisfy the | |
3333 | conditional, then return that value. Else return NULL. | |
3334 | ||
3335 | If signed overflow must be undefined for the value to satisfy | |
3336 | the conditional, then set *STRICT_OVERFLOW_P to true. */ | |
3337 | ||
3338 | static tree | |
3339 | test_for_singularity (enum tree_code cond_code, tree op0, | |
fc36b97a | 3340 | tree op1, const value_range *vr) |
c2ad9885 JL |
3341 | { |
3342 | tree min = NULL; | |
3343 | tree max = NULL; | |
3344 | ||
3345 | /* Extract minimum/maximum values which satisfy the conditional as it was | |
3346 | written. */ | |
3347 | if (cond_code == LE_EXPR || cond_code == LT_EXPR) | |
3348 | { | |
3349 | min = TYPE_MIN_VALUE (TREE_TYPE (op0)); | |
3350 | ||
3351 | max = op1; | |
3352 | if (cond_code == LT_EXPR) | |
3353 | { | |
3354 | tree one = build_int_cst (TREE_TYPE (op0), 1); | |
3355 | max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one); | |
3356 | /* Signal to compare_values_warnv this expr doesn't overflow. */ | |
3357 | if (EXPR_P (max)) | |
e9e2bad7 | 3358 | suppress_warning (max, OPT_Woverflow); |
c2ad9885 JL |
3359 | } |
3360 | } | |
3361 | else if (cond_code == GE_EXPR || cond_code == GT_EXPR) | |
3362 | { | |
3363 | max = TYPE_MAX_VALUE (TREE_TYPE (op0)); | |
3364 | ||
3365 | min = op1; | |
3366 | if (cond_code == GT_EXPR) | |
3367 | { | |
3368 | tree one = build_int_cst (TREE_TYPE (op0), 1); | |
3369 | min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one); | |
3370 | /* Signal to compare_values_warnv this expr doesn't overflow. */ | |
3371 | if (EXPR_P (min)) | |
e9e2bad7 | 3372 | suppress_warning (min, OPT_Woverflow); |
c2ad9885 JL |
3373 | } |
3374 | } | |
3375 | ||
3376 | /* Now refine the minimum and maximum values using any | |
3377 | value range information we have for op0. */ | |
3378 | if (min && max) | |
3379 | { | |
92877ab8 AH |
3380 | tree type = TREE_TYPE (op0); |
3381 | tree tmin = wide_int_to_tree (type, vr->lower_bound ()); | |
3382 | tree tmax = wide_int_to_tree (type, vr->upper_bound ()); | |
3383 | if (compare_values (tmin, min) == 1) | |
3384 | min = tmin; | |
3385 | if (compare_values (tmax, max) == -1) | |
3386 | max = tmax; | |
c2ad9885 JL |
3387 | |
3388 | /* If the new min/max values have converged to a single value, | |
3389 | then there is only one value which can satisfy the condition, | |
3390 | return that value. */ | |
3391 | if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min)) | |
3392 | return min; | |
3393 | } | |
3394 | return NULL; | |
3395 | } | |
3396 | ||
3397 | /* Return whether the value range *VR fits in an integer type specified | |
3398 | by PRECISION and UNSIGNED_P. */ | |
3399 | ||
bae73ca5 | 3400 | bool |
fc36b97a | 3401 | range_fits_type_p (const value_range *vr, |
0982acbe | 3402 | unsigned dest_precision, signop dest_sgn) |
c2ad9885 JL |
3403 | { |
3404 | tree src_type; | |
3405 | unsigned src_precision; | |
3406 | widest_int tem; | |
3407 | signop src_sgn; | |
3408 | ||
3409 | /* We can only handle integral and pointer types. */ | |
54994253 | 3410 | src_type = vr->type (); |
c2ad9885 JL |
3411 | if (!INTEGRAL_TYPE_P (src_type) |
3412 | && !POINTER_TYPE_P (src_type)) | |
3413 | return false; | |
3414 | ||
3415 | /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED, | |
3416 | and so is an identity transform. */ | |
54994253 | 3417 | src_precision = TYPE_PRECISION (vr->type ()); |
c2ad9885 JL |
3418 | src_sgn = TYPE_SIGN (src_type); |
3419 | if ((src_precision < dest_precision | |
3420 | && !(dest_sgn == UNSIGNED && src_sgn == SIGNED)) | |
3421 | || (src_precision == dest_precision && src_sgn == dest_sgn)) | |
3422 | return true; | |
3423 | ||
3424 | /* Now we can only handle ranges with constant bounds. */ | |
54994253 | 3425 | if (!range_int_cst_p (vr)) |
c2ad9885 JL |
3426 | return false; |
3427 | ||
3428 | /* For sign changes, the MSB of the wide_int has to be clear. | |
3429 | An unsigned value with its MSB set cannot be represented by | |
3430 | a signed wide_int, while a negative value cannot be represented | |
3431 | by an unsigned wide_int. */ | |
3432 | if (src_sgn != dest_sgn | |
54994253 AH |
3433 | && (wi::lts_p (wi::to_wide (vr->min ()), 0) |
3434 | || wi::lts_p (wi::to_wide (vr->max ()), 0))) | |
c2ad9885 JL |
3435 | return false; |
3436 | ||
3437 | /* Then we can perform the conversion on both ends and compare | |
3438 | the result for equality. */ | |
54994253 AH |
3439 | tem = wi::ext (wi::to_widest (vr->min ()), dest_precision, dest_sgn); |
3440 | if (tem != wi::to_widest (vr->min ())) | |
c2ad9885 | 3441 | return false; |
54994253 AH |
3442 | tem = wi::ext (wi::to_widest (vr->max ()), dest_precision, dest_sgn); |
3443 | if (tem != wi::to_widest (vr->max ())) | |
c2ad9885 JL |
3444 | return false; |
3445 | ||
3446 | return true; | |
3447 | } | |
3448 | ||
1396fa5b AH |
3449 | /* If COND can be folded entirely as TRUE or FALSE, rewrite the |
3450 | conditional as such, and return TRUE. */ | |
3451 | ||
3452 | bool | |
fc36b97a | 3453 | simplify_using_ranges::fold_cond (gcond *cond) |
1396fa5b AH |
3454 | { |
3455 | /* ?? vrp_folder::fold_predicate_in() is a superset of this. At | |
3456 | some point we should merge all variants of this code. */ | |
3457 | edge taken_edge; | |
3458 | vrp_visit_cond_stmt (cond, &taken_edge); | |
fcae5121 AM |
3459 | |
3460 | int_range_max r; | |
3461 | if (query->range_of_stmt (r, cond) && r.singleton_p ()) | |
3462 | { | |
3463 | // COND has already been folded if arguments are constant. | |
3464 | if (TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME | |
3465 | && TREE_CODE (gimple_cond_rhs (cond)) != SSA_NAME) | |
3466 | return false; | |
3467 | ||
3468 | if (r.zero_p ()) | |
3469 | { | |
3470 | gcc_checking_assert (!taken_edge | |
3471 | || taken_edge->flags & EDGE_FALSE_VALUE); | |
3472 | if (dump_file && (dump_flags & TDF_DETAILS) && !taken_edge) | |
3473 | fprintf (dump_file, "\nPredicate evaluates to: 0\n"); | |
3474 | gimple_cond_make_false (cond); | |
3475 | } | |
3476 | else | |
3477 | { | |
3478 | gcc_checking_assert (!taken_edge | |
3479 | || taken_edge->flags & EDGE_TRUE_VALUE); | |
3480 | if (dump_file && (dump_flags & TDF_DETAILS) && !taken_edge) | |
3481 | fprintf (dump_file, "\nPredicate evaluates to: 1\n"); | |
3482 | gimple_cond_make_true (cond); | |
3483 | } | |
3484 | update_stmt (cond); | |
3485 | return true; | |
3486 | } | |
3487 | ||
1396fa5b AH |
3488 | if (taken_edge) |
3489 | { | |
3490 | if (taken_edge->flags & EDGE_TRUE_VALUE) | |
3491 | gimple_cond_make_true (cond); | |
3492 | else if (taken_edge->flags & EDGE_FALSE_VALUE) | |
3493 | gimple_cond_make_false (cond); | |
3494 | else | |
3495 | gcc_unreachable (); | |
3496 | update_stmt (cond); | |
3497 | return true; | |
3498 | } | |
3499 | return false; | |
3500 | } | |
3501 | ||
c2ad9885 JL |
3502 | /* Simplify a conditional using a relational operator to an equality |
3503 | test if the range information indicates only one value can satisfy | |
3504 | the original conditional. */ | |
3505 | ||
3506 | bool | |
fc36b97a | 3507 | simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt) |
c2ad9885 JL |
3508 | { |
3509 | tree op0 = gimple_cond_lhs (stmt); | |
3510 | tree op1 = gimple_cond_rhs (stmt); | |
3511 | enum tree_code cond_code = gimple_cond_code (stmt); | |
3512 | ||
1396fa5b AH |
3513 | if (fold_cond (stmt)) |
3514 | return true; | |
3515 | ||
c2ad9885 JL |
3516 | if (cond_code != NE_EXPR |
3517 | && cond_code != EQ_EXPR | |
3518 | && TREE_CODE (op0) == SSA_NAME | |
3519 | && INTEGRAL_TYPE_P (TREE_TYPE (op0)) | |
3520 | && is_gimple_min_invariant (op1)) | |
3521 | { | |
a889e06a | 3522 | const value_range *vr = query->get_value_range (op0, stmt); |
c2ad9885 JL |
3523 | |
3524 | /* If we have range information for OP0, then we might be | |
3525 | able to simplify this conditional. */ | |
92877ab8 | 3526 | if (!vr->undefined_p () && !vr->varying_p ()) |
c2ad9885 JL |
3527 | { |
3528 | tree new_tree = test_for_singularity (cond_code, op0, op1, vr); | |
3529 | if (new_tree) | |
3530 | { | |
3531 | if (dump_file) | |
3532 | { | |
3533 | fprintf (dump_file, "Simplified relational "); | |
3534 | print_gimple_stmt (dump_file, stmt, 0); | |
3535 | fprintf (dump_file, " into "); | |
3536 | } | |
3537 | ||
3538 | gimple_cond_set_code (stmt, EQ_EXPR); | |
3539 | gimple_cond_set_lhs (stmt, op0); | |
3540 | gimple_cond_set_rhs (stmt, new_tree); | |
3541 | ||
3542 | update_stmt (stmt); | |
3543 | ||
3544 | if (dump_file) | |
3545 | { | |
3546 | print_gimple_stmt (dump_file, stmt, 0); | |
3547 | fprintf (dump_file, "\n"); | |
3548 | } | |
3549 | ||
3550 | return true; | |
3551 | } | |
3552 | ||
3553 | /* Try again after inverting the condition. We only deal | |
3554 | with integral types here, so no need to worry about | |
3555 | issues with inverting FP comparisons. */ | |
3556 | new_tree = test_for_singularity | |
3557 | (invert_tree_comparison (cond_code, false), | |
3558 | op0, op1, vr); | |
3559 | if (new_tree) | |
3560 | { | |
3561 | if (dump_file) | |
3562 | { | |
3563 | fprintf (dump_file, "Simplified relational "); | |
3564 | print_gimple_stmt (dump_file, stmt, 0); | |
3565 | fprintf (dump_file, " into "); | |
3566 | } | |
3567 | ||
3568 | gimple_cond_set_code (stmt, NE_EXPR); | |
3569 | gimple_cond_set_lhs (stmt, op0); | |
3570 | gimple_cond_set_rhs (stmt, new_tree); | |
3571 | ||
3572 | update_stmt (stmt); | |
3573 | ||
3574 | if (dump_file) | |
3575 | { | |
3576 | print_gimple_stmt (dump_file, stmt, 0); | |
3577 | fprintf (dump_file, "\n"); | |
3578 | } | |
3579 | ||
3580 | return true; | |
3581 | } | |
3582 | } | |
3583 | } | |
3584 | return false; | |
3585 | } | |
3586 | ||
c2ad9885 JL |
3587 | /* Simplify a switch statement using the value range of the switch |
3588 | argument. */ | |
3589 | ||
3590 | bool | |
fc36b97a | 3591 | simplify_using_ranges::simplify_switch_using_ranges (gswitch *stmt) |
c2ad9885 JL |
3592 | { |
3593 | tree op = gimple_switch_index (stmt); | |
fc36b97a | 3594 | const value_range *vr = NULL; |
c2ad9885 JL |
3595 | bool take_default; |
3596 | edge e; | |
3597 | edge_iterator ei; | |
3598 | size_t i = 0, j = 0, n, n2; | |
3599 | tree vec2; | |
3600 | switch_update su; | |
3601 | size_t k = 1, l = 0; | |
3602 | ||
3603 | if (TREE_CODE (op) == SSA_NAME) | |
3604 | { | |
a889e06a | 3605 | vr = query->get_value_range (op, stmt); |
c2ad9885 JL |
3606 | |
3607 | /* We can only handle integer ranges. */ | |
54994253 AH |
3608 | if (vr->varying_p () |
3609 | || vr->undefined_p () | |
3610 | || vr->symbolic_p ()) | |
c2ad9885 JL |
3611 | return false; |
3612 | ||
3613 | /* Find case label for min/max of the value range. */ | |
3614 | take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l); | |
3615 | } | |
3616 | else if (TREE_CODE (op) == INTEGER_CST) | |
3617 | { | |
3618 | take_default = !find_case_label_index (stmt, 1, op, &i); | |
3619 | if (take_default) | |
3620 | { | |
3621 | i = 1; | |
3622 | j = 0; | |
3623 | } | |
3624 | else | |
3625 | { | |
3626 | j = i; | |
3627 | } | |
3628 | } | |
3629 | else | |
3630 | return false; | |
3631 | ||
3632 | n = gimple_switch_num_labels (stmt); | |
3633 | ||
3634 | /* We can truncate the case label ranges that partially overlap with OP's | |
3635 | value range. */ | |
3636 | size_t min_idx = 1, max_idx = 0; | |
3637 | if (vr != NULL) | |
54994253 | 3638 | find_case_label_range (stmt, vr->min (), vr->max (), &min_idx, &max_idx); |
c2ad9885 JL |
3639 | if (min_idx <= max_idx) |
3640 | { | |
3641 | tree min_label = gimple_switch_label (stmt, min_idx); | |
3642 | tree max_label = gimple_switch_label (stmt, max_idx); | |
3643 | ||
3644 | /* Avoid changing the type of the case labels when truncating. */ | |
3645 | tree case_label_type = TREE_TYPE (CASE_LOW (min_label)); | |
54994253 AH |
3646 | tree vr_min = fold_convert (case_label_type, vr->min ()); |
3647 | tree vr_max = fold_convert (case_label_type, vr->max ()); | |
c2ad9885 | 3648 | |
54994253 | 3649 | if (vr->kind () == VR_RANGE) |
c2ad9885 JL |
3650 | { |
3651 | /* If OP's value range is [2,8] and the low label range is | |
3652 | 0 ... 3, truncate the label's range to 2 .. 3. */ | |
3653 | if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0 | |
3654 | && CASE_HIGH (min_label) != NULL_TREE | |
3655 | && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0) | |
3656 | CASE_LOW (min_label) = vr_min; | |
3657 | ||
3658 | /* If OP's value range is [2,8] and the high label range is | |
3659 | 7 ... 10, truncate the label's range to 7 .. 8. */ | |
3660 | if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0 | |
3661 | && CASE_HIGH (max_label) != NULL_TREE | |
3662 | && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0) | |
3663 | CASE_HIGH (max_label) = vr_max; | |
3664 | } | |
54994253 | 3665 | else if (vr->kind () == VR_ANTI_RANGE) |
c2ad9885 JL |
3666 | { |
3667 | tree one_cst = build_one_cst (case_label_type); | |
3668 | ||
3669 | if (min_label == max_label) | |
3670 | { | |
3671 | /* If OP's value range is ~[7,8] and the label's range is | |
3672 | 7 ... 10, truncate the label's range to 9 ... 10. */ | |
3673 | if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0 | |
3674 | && CASE_HIGH (min_label) != NULL_TREE | |
3675 | && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0) | |
3676 | CASE_LOW (min_label) | |
3677 | = int_const_binop (PLUS_EXPR, vr_max, one_cst); | |
3678 | ||
3679 | /* If OP's value range is ~[7,8] and the label's range is | |
3680 | 5 ... 8, truncate the label's range to 5 ... 6. */ | |
3681 | if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0 | |
3682 | && CASE_HIGH (min_label) != NULL_TREE | |
3683 | && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0) | |
3684 | CASE_HIGH (min_label) | |
3685 | = int_const_binop (MINUS_EXPR, vr_min, one_cst); | |
3686 | } | |
3687 | else | |
3688 | { | |
3689 | /* If OP's value range is ~[2,8] and the low label range is | |
3690 | 0 ... 3, truncate the label's range to 0 ... 1. */ | |
3691 | if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0 | |
3692 | && CASE_HIGH (min_label) != NULL_TREE | |
3693 | && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0) | |
3694 | CASE_HIGH (min_label) | |
3695 | = int_const_binop (MINUS_EXPR, vr_min, one_cst); | |
3696 | ||
3697 | /* If OP's value range is ~[2,8] and the high label range is | |
3698 | 7 ... 10, truncate the label's range to 9 ... 10. */ | |
3699 | if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0 | |
3700 | && CASE_HIGH (max_label) != NULL_TREE | |
3701 | && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0) | |
3702 | CASE_LOW (max_label) | |
3703 | = int_const_binop (PLUS_EXPR, vr_max, one_cst); | |
3704 | } | |
3705 | } | |
3706 | ||
3707 | /* Canonicalize singleton case ranges. */ | |
3708 | if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label))) | |
3709 | CASE_HIGH (min_label) = NULL_TREE; | |
3710 | if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label))) | |
3711 | CASE_HIGH (max_label) = NULL_TREE; | |
3712 | } | |
3713 | ||
3714 | /* We can also eliminate case labels that lie completely outside OP's value | |
3715 | range. */ | |
3716 | ||
3717 | /* Bail out if this is just all edges taken. */ | |
3718 | if (i == 1 | |
3719 | && j == n - 1 | |
3720 | && take_default) | |
3721 | return false; | |
3722 | ||
3723 | /* Build a new vector of taken case labels. */ | |
3724 | vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default); | |
3725 | n2 = 0; | |
3726 | ||
3727 | /* Add the default edge, if necessary. */ | |
3728 | if (take_default) | |
3729 | TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt); | |
3730 | ||
3731 | for (; i <= j; ++i, ++n2) | |
3732 | TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i); | |
3733 | ||
3734 | for (; k <= l; ++k, ++n2) | |
3735 | TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k); | |
3736 | ||
3737 | /* Mark needed edges. */ | |
3738 | for (i = 0; i < n2; ++i) | |
3739 | { | |
3740 | e = find_edge (gimple_bb (stmt), | |
61ff5d6f ML |
3741 | label_to_block (cfun, |
3742 | CASE_LABEL (TREE_VEC_ELT (vec2, i)))); | |
c2ad9885 JL |
3743 | e->aux = (void *)-1; |
3744 | } | |
3745 | ||
3746 | /* Queue not needed edges for later removal. */ | |
3747 | FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs) | |
3748 | { | |
3749 | if (e->aux == (void *)-1) | |
3750 | { | |
3751 | e->aux = NULL; | |
3752 | continue; | |
3753 | } | |
3754 | ||
3755 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3756 | { | |
3757 | fprintf (dump_file, "removing unreachable case label\n"); | |
3758 | } | |
3759 | to_remove_edges.safe_push (e); | |
3760 | e->flags &= ~EDGE_EXECUTABLE; | |
35b66f30 | 3761 | e->flags |= EDGE_IGNORE; |
c2ad9885 JL |
3762 | } |
3763 | ||
3764 | /* And queue an update for the stmt. */ | |
3765 | su.stmt = stmt; | |
3766 | su.vec = vec2; | |
3767 | to_update_switch_stmts.safe_push (su); | |
fcae5121 | 3768 | return true; |
c2ad9885 JL |
3769 | } |
3770 | ||
35b66f30 | 3771 | void |
fc36b97a | 3772 | simplify_using_ranges::cleanup_edges_and_switches (void) |
35b66f30 JL |
3773 | { |
3774 | int i; | |
3775 | edge e; | |
3776 | switch_update *su; | |
3777 | ||
3778 | /* Remove dead edges from SWITCH_EXPR optimization. This leaves the | |
3779 | CFG in a broken state and requires a cfg_cleanup run. */ | |
3780 | FOR_EACH_VEC_ELT (to_remove_edges, i, e) | |
3781 | remove_edge (e); | |
3782 | ||
3783 | /* Update SWITCH_EXPR case label vector. */ | |
3784 | FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su) | |
3785 | { | |
3786 | size_t j; | |
3787 | size_t n = TREE_VEC_LENGTH (su->vec); | |
3788 | tree label; | |
3789 | gimple_switch_set_num_labels (su->stmt, n); | |
3790 | for (j = 0; j < n; j++) | |
3791 | gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j)); | |
3792 | /* As we may have replaced the default label with a regular one | |
3793 | make sure to make it a real default label again. This ensures | |
3794 | optimal expansion. */ | |
3795 | label = gimple_switch_label (su->stmt, 0); | |
3796 | CASE_LOW (label) = NULL_TREE; | |
3797 | CASE_HIGH (label) = NULL_TREE; | |
3798 | } | |
3799 | ||
3800 | if (!to_remove_edges.is_empty ()) | |
3801 | { | |
3802 | free_dominance_info (CDI_DOMINATORS); | |
3803 | loops_state_set (LOOPS_NEED_FIXUP); | |
3804 | } | |
3805 | ||
3806 | to_remove_edges.release (); | |
3807 | to_update_switch_stmts.release (); | |
3808 | } | |
3809 | ||
c2ad9885 JL |
3810 | /* Simplify an integral conversion from an SSA name in STMT. */ |
3811 | ||
3812 | static bool | |
3813 | simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt) | |
3814 | { | |
3815 | tree innerop, middleop, finaltype; | |
3816 | gimple *def_stmt; | |
3817 | signop inner_sgn, middle_sgn, final_sgn; | |
3818 | unsigned inner_prec, middle_prec, final_prec; | |
3819 | widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax; | |
3820 | ||
3821 | finaltype = TREE_TYPE (gimple_assign_lhs (stmt)); | |
3822 | if (!INTEGRAL_TYPE_P (finaltype)) | |
3823 | return false; | |
3824 | middleop = gimple_assign_rhs1 (stmt); | |
3825 | def_stmt = SSA_NAME_DEF_STMT (middleop); | |
3826 | if (!is_gimple_assign (def_stmt) | |
3827 | || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))) | |
3828 | return false; | |
3829 | innerop = gimple_assign_rhs1 (def_stmt); | |
3830 | if (TREE_CODE (innerop) != SSA_NAME | |
3831 | || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)) | |
3832 | return false; | |
3833 | ||
45f4e2b0 | 3834 | /* Get the value-range of the inner operand. Use global ranges in |
c2ad9885 JL |
3835 | case innerop was created during substitute-and-fold. */ |
3836 | wide_int imin, imax; | |
70be5895 AH |
3837 | value_range vr; |
3838 | if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))) | |
c2ad9885 | 3839 | return false; |
22137a3d | 3840 | get_range_query (cfun)->range_of_expr (vr, innerop, stmt); |
70be5895 AH |
3841 | if (vr.undefined_p () || vr.varying_p ()) |
3842 | return false; | |
3843 | innermin = widest_int::from (vr.lower_bound (), TYPE_SIGN (TREE_TYPE (innerop))); | |
3844 | innermax = widest_int::from (vr.upper_bound (), TYPE_SIGN (TREE_TYPE (innerop))); | |
c2ad9885 JL |
3845 | |
3846 | /* Simulate the conversion chain to check if the result is equal if | |
3847 | the middle conversion is removed. */ | |
3848 | inner_prec = TYPE_PRECISION (TREE_TYPE (innerop)); | |
3849 | middle_prec = TYPE_PRECISION (TREE_TYPE (middleop)); | |
3850 | final_prec = TYPE_PRECISION (finaltype); | |
3851 | ||
3852 | /* If the first conversion is not injective, the second must not | |
3853 | be widening. */ | |
3854 | if (wi::gtu_p (innermax - innermin, | |
3855 | wi::mask <widest_int> (middle_prec, false)) | |
3856 | && middle_prec < final_prec) | |
3857 | return false; | |
3858 | /* We also want a medium value so that we can track the effect that | |
3859 | narrowing conversions with sign change have. */ | |
3860 | inner_sgn = TYPE_SIGN (TREE_TYPE (innerop)); | |
3861 | if (inner_sgn == UNSIGNED) | |
3862 | innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false); | |
3863 | else | |
3864 | innermed = 0; | |
3865 | if (wi::cmp (innermin, innermed, inner_sgn) >= 0 | |
3866 | || wi::cmp (innermed, innermax, inner_sgn) >= 0) | |
3867 | innermed = innermin; | |
3868 | ||
3869 | middle_sgn = TYPE_SIGN (TREE_TYPE (middleop)); | |
3870 | middlemin = wi::ext (innermin, middle_prec, middle_sgn); | |
3871 | middlemed = wi::ext (innermed, middle_prec, middle_sgn); | |
3872 | middlemax = wi::ext (innermax, middle_prec, middle_sgn); | |
3873 | ||
3874 | /* Require that the final conversion applied to both the original | |
3875 | and the intermediate range produces the same result. */ | |
3876 | final_sgn = TYPE_SIGN (finaltype); | |
3877 | if (wi::ext (middlemin, final_prec, final_sgn) | |
3878 | != wi::ext (innermin, final_prec, final_sgn) | |
3879 | || wi::ext (middlemed, final_prec, final_sgn) | |
3880 | != wi::ext (innermed, final_prec, final_sgn) | |
3881 | || wi::ext (middlemax, final_prec, final_sgn) | |
3882 | != wi::ext (innermax, final_prec, final_sgn)) | |
3883 | return false; | |
3884 | ||
3885 | gimple_assign_set_rhs1 (stmt, innerop); | |
3886 | fold_stmt (gsi, follow_single_use_edges); | |
3887 | return true; | |
3888 | } | |
3889 | ||
3890 | /* Simplify a conversion from integral SSA name to float in STMT. */ | |
3891 | ||
3892 | bool | |
fc36b97a AH |
3893 | simplify_using_ranges::simplify_float_conversion_using_ranges |
3894 | (gimple_stmt_iterator *gsi, | |
3895 | gimple *stmt) | |
c2ad9885 JL |
3896 | { |
3897 | tree rhs1 = gimple_assign_rhs1 (stmt); | |
a889e06a | 3898 | const value_range *vr = query->get_value_range (rhs1, stmt); |
c2ad9885 JL |
3899 | scalar_float_mode fltmode |
3900 | = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt))); | |
3901 | scalar_int_mode mode; | |
3902 | tree tem; | |
3903 | gassign *conv; | |
3904 | ||
3905 | /* We can only handle constant ranges. */ | |
54994253 | 3906 | if (!range_int_cst_p (vr)) |
c2ad9885 JL |
3907 | return false; |
3908 | ||
3909 | /* First check if we can use a signed type in place of an unsigned. */ | |
3910 | scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1)); | |
3911 | if (TYPE_UNSIGNED (TREE_TYPE (rhs1)) | |
3912 | && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing | |
3913 | && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED)) | |
3914 | mode = rhs_mode; | |
3915 | /* If we can do the conversion in the current input mode do nothing. */ | |
3916 | else if (can_float_p (fltmode, rhs_mode, | |
3917 | TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing) | |
3918 | return false; | |
3919 | /* Otherwise search for a mode we can use, starting from the narrowest | |
3920 | integer mode available. */ | |
3921 | else | |
3922 | { | |
3923 | mode = NARROWEST_INT_MODE; | |
3924 | for (;;) | |
3925 | { | |
3926 | /* If we cannot do a signed conversion to float from mode | |
3927 | or if the value-range does not fit in the signed type | |
3928 | try with a wider mode. */ | |
3929 | if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing | |
3930 | && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED)) | |
3931 | break; | |
3932 | ||
3933 | /* But do not widen the input. Instead leave that to the | |
3934 | optabs expansion code. */ | |
3935 | if (!GET_MODE_WIDER_MODE (mode).exists (&mode) | |
3936 | || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1))) | |
3937 | return false; | |
3938 | } | |
3939 | } | |
3940 | ||
3941 | /* It works, insert a truncation or sign-change before the | |
3942 | float conversion. */ | |
3943 | tem = make_ssa_name (build_nonstandard_integer_type | |
3944 | (GET_MODE_PRECISION (mode), 0)); | |
3945 | conv = gimple_build_assign (tem, NOP_EXPR, rhs1); | |
3946 | gsi_insert_before (gsi, conv, GSI_SAME_STMT); | |
3947 | gimple_assign_set_rhs1 (stmt, tem); | |
3948 | fold_stmt (gsi, follow_single_use_edges); | |
3949 | ||
3950 | return true; | |
3951 | } | |
3952 | ||
3953 | /* Simplify an internal fn call using ranges if possible. */ | |
3954 | ||
3955 | bool | |
fc36b97a AH |
3956 | simplify_using_ranges::simplify_internal_call_using_ranges |
3957 | (gimple_stmt_iterator *gsi, | |
3958 | gimple *stmt) | |
c2ad9885 JL |
3959 | { |
3960 | enum tree_code subcode; | |
3961 | bool is_ubsan = false; | |
3962 | bool ovf = false; | |
3963 | switch (gimple_call_internal_fn (stmt)) | |
3964 | { | |
3965 | case IFN_UBSAN_CHECK_ADD: | |
3966 | subcode = PLUS_EXPR; | |
3967 | is_ubsan = true; | |
3968 | break; | |
3969 | case IFN_UBSAN_CHECK_SUB: | |
3970 | subcode = MINUS_EXPR; | |
3971 | is_ubsan = true; | |
3972 | break; | |
3973 | case IFN_UBSAN_CHECK_MUL: | |
3974 | subcode = MULT_EXPR; | |
3975 | is_ubsan = true; | |
3976 | break; | |
3977 | case IFN_ADD_OVERFLOW: | |
3978 | subcode = PLUS_EXPR; | |
3979 | break; | |
3980 | case IFN_SUB_OVERFLOW: | |
3981 | subcode = MINUS_EXPR; | |
3982 | break; | |
3983 | case IFN_MUL_OVERFLOW: | |
3984 | subcode = MULT_EXPR; | |
3985 | break; | |
3986 | default: | |
3987 | return false; | |
3988 | } | |
3989 | ||
3990 | tree op0 = gimple_call_arg (stmt, 0); | |
3991 | tree op1 = gimple_call_arg (stmt, 1); | |
3992 | tree type; | |
3993 | if (is_ubsan) | |
3994 | { | |
3995 | type = TREE_TYPE (op0); | |
3996 | if (VECTOR_TYPE_P (type)) | |
3997 | return false; | |
3998 | } | |
3999 | else if (gimple_call_lhs (stmt) == NULL_TREE) | |
4000 | return false; | |
4001 | else | |
4002 | type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt))); | |
a889e06a | 4003 | if (!check_for_binary_op_overflow (query, subcode, type, op0, op1, &ovf) |
c2ad9885 JL |
4004 | || (is_ubsan && ovf)) |
4005 | return false; | |
4006 | ||
4007 | gimple *g; | |
4008 | location_t loc = gimple_location (stmt); | |
4009 | if (is_ubsan) | |
4010 | g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1); | |
4011 | else | |
4012 | { | |
4013 | int prec = TYPE_PRECISION (type); | |
4014 | tree utype = type; | |
4015 | if (ovf | |
4016 | || !useless_type_conversion_p (type, TREE_TYPE (op0)) | |
4017 | || !useless_type_conversion_p (type, TREE_TYPE (op1))) | |
4018 | utype = build_nonstandard_integer_type (prec, 1); | |
4019 | if (TREE_CODE (op0) == INTEGER_CST) | |
4020 | op0 = fold_convert (utype, op0); | |
4021 | else if (!useless_type_conversion_p (utype, TREE_TYPE (op0))) | |
4022 | { | |
4023 | g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0); | |
4024 | gimple_set_location (g, loc); | |
4025 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
4026 | op0 = gimple_assign_lhs (g); | |
4027 | } | |
4028 | if (TREE_CODE (op1) == INTEGER_CST) | |
4029 | op1 = fold_convert (utype, op1); | |
4030 | else if (!useless_type_conversion_p (utype, TREE_TYPE (op1))) | |
4031 | { | |
4032 | g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1); | |
4033 | gimple_set_location (g, loc); | |
4034 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
4035 | op1 = gimple_assign_lhs (g); | |
4036 | } | |
4037 | g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1); | |
4038 | gimple_set_location (g, loc); | |
4039 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
4040 | if (utype != type) | |
4041 | { | |
4042 | g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, | |
4043 | gimple_assign_lhs (g)); | |
4044 | gimple_set_location (g, loc); | |
4045 | gsi_insert_before (gsi, g, GSI_SAME_STMT); | |
4046 | } | |
4047 | g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR, | |
4048 | gimple_assign_lhs (g), | |
4049 | build_int_cst (type, ovf)); | |
4050 | } | |
4051 | gimple_set_location (g, loc); | |
4052 | gsi_replace (gsi, g, false); | |
4053 | return true; | |
4054 | } | |
4055 | ||
4056 | /* Return true if VAR is a two-valued variable. Set a and b with the | |
4057 | two-values when it is true. Return false otherwise. */ | |
4058 | ||
4059 | bool | |
fc36b97a | 4060 | simplify_using_ranges::two_valued_val_range_p (tree var, tree *a, tree *b) |
c2ad9885 | 4061 | { |
a889e06a | 4062 | value_range vr = *query->get_value_range (var); |
506bd24a AH |
4063 | vr.normalize_symbolics (); |
4064 | if (vr.varying_p () || vr.undefined_p ()) | |
c2ad9885 JL |
4065 | return false; |
4066 | ||
506bd24a AH |
4067 | if ((vr.num_pairs () == 1 && vr.upper_bound () - vr.lower_bound () == 1) |
4068 | || (vr.num_pairs () == 2 | |
4069 | && vr.lower_bound (0) == vr.upper_bound (0) | |
4070 | && vr.lower_bound (1) == vr.upper_bound (1))) | |
c2ad9885 | 4071 | { |
506bd24a AH |
4072 | *a = wide_int_to_tree (TREE_TYPE (var), vr.lower_bound ()); |
4073 | *b = wide_int_to_tree (TREE_TYPE (var), vr.upper_bound ()); | |
c2ad9885 JL |
4074 | return true; |
4075 | } | |
c2ad9885 JL |
4076 | return false; |
4077 | } | |
4078 | ||
a889e06a AH |
4079 | simplify_using_ranges::simplify_using_ranges (range_query *query) |
4080 | : query (query) | |
fc36b97a AH |
4081 | { |
4082 | to_remove_edges = vNULL; | |
4083 | to_update_switch_stmts = vNULL; | |
4084 | } | |
4085 | ||
4086 | simplify_using_ranges::~simplify_using_ranges () | |
4087 | { | |
4088 | cleanup_edges_and_switches (); | |
4089 | } | |
4090 | ||
c2ad9885 JL |
4091 | /* Simplify STMT using ranges if possible. */ |
4092 | ||
4093 | bool | |
fc36b97a | 4094 | simplify_using_ranges::simplify (gimple_stmt_iterator *gsi) |
c2ad9885 | 4095 | { |
a889e06a AH |
4096 | gcc_checking_assert (query); |
4097 | ||
c2ad9885 JL |
4098 | gimple *stmt = gsi_stmt (*gsi); |
4099 | if (is_gimple_assign (stmt)) | |
4100 | { | |
4101 | enum tree_code rhs_code = gimple_assign_rhs_code (stmt); | |
4102 | tree rhs1 = gimple_assign_rhs1 (stmt); | |
4103 | tree rhs2 = gimple_assign_rhs2 (stmt); | |
4104 | tree lhs = gimple_assign_lhs (stmt); | |
4105 | tree val1 = NULL_TREE, val2 = NULL_TREE; | |
4106 | use_operand_p use_p; | |
4107 | gimple *use_stmt; | |
4108 | ||
4109 | /* Convert: | |
4110 | LHS = CST BINOP VAR | |
4111 | Where VAR is two-valued and LHS is used in GIMPLE_COND only | |
4112 | To: | |
4113 | LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2) | |
4114 | ||
4115 | Also handles: | |
4116 | LHS = VAR BINOP CST | |
4117 | Where VAR is two-valued and LHS is used in GIMPLE_COND only | |
4118 | To: | |
4119 | LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */ | |
4120 | ||
4121 | if (TREE_CODE_CLASS (rhs_code) == tcc_binary | |
e54675bb | 4122 | && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)) |
c2ad9885 JL |
4123 | && ((TREE_CODE (rhs1) == INTEGER_CST |
4124 | && TREE_CODE (rhs2) == SSA_NAME) | |
4125 | || (TREE_CODE (rhs2) == INTEGER_CST | |
4126 | && TREE_CODE (rhs1) == SSA_NAME)) | |
4127 | && single_imm_use (lhs, &use_p, &use_stmt) | |
4128 | && gimple_code (use_stmt) == GIMPLE_COND) | |
4129 | ||
4130 | { | |
4131 | tree new_rhs1 = NULL_TREE; | |
4132 | tree new_rhs2 = NULL_TREE; | |
4133 | tree cmp_var = NULL_TREE; | |
4134 | ||
4135 | if (TREE_CODE (rhs2) == SSA_NAME | |
4136 | && two_valued_val_range_p (rhs2, &val1, &val2)) | |
4137 | { | |
4138 | /* Optimize RHS1 OP [VAL1, VAL2]. */ | |
4139 | new_rhs1 = int_const_binop (rhs_code, rhs1, val1); | |
4140 | new_rhs2 = int_const_binop (rhs_code, rhs1, val2); | |
4141 | cmp_var = rhs2; | |
4142 | } | |
4143 | else if (TREE_CODE (rhs1) == SSA_NAME | |
4144 | && two_valued_val_range_p (rhs1, &val1, &val2)) | |
4145 | { | |
4146 | /* Optimize [VAL1, VAL2] OP RHS2. */ | |
4147 | new_rhs1 = int_const_binop (rhs_code, val1, rhs2); | |
4148 | new_rhs2 = int_const_binop (rhs_code, val2, rhs2); | |
4149 | cmp_var = rhs1; | |
4150 | } | |
4151 | ||
4152 | /* If we could not find two-vals or the optimzation is invalid as | |
4153 | in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */ | |
4154 | if (new_rhs1 && new_rhs2) | |
4155 | { | |
4156 | tree cond = build2 (EQ_EXPR, boolean_type_node, cmp_var, val1); | |
4157 | gimple_assign_set_rhs_with_ops (gsi, | |
4158 | COND_EXPR, cond, | |
4159 | new_rhs1, | |
4160 | new_rhs2); | |
4161 | update_stmt (gsi_stmt (*gsi)); | |
4162 | fold_stmt (gsi, follow_single_use_edges); | |
4163 | return true; | |
4164 | } | |
4165 | } | |
4166 | ||
4167 | switch (rhs_code) | |
4168 | { | |
4169 | case EQ_EXPR: | |
4170 | case NE_EXPR: | |
4171 | /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity | |
4172 | if the RHS is zero or one, and the LHS are known to be boolean | |
4173 | values. */ | |
4174 | if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) | |
4175 | return simplify_truth_ops_using_ranges (gsi, stmt); | |
4176 | break; | |
4177 | ||
4178 | /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR | |
4179 | and BIT_AND_EXPR respectively if the first operand is greater | |
4180 | than zero and the second operand is an exact power of two. | |
4181 | Also optimize TRUNC_MOD_EXPR away if the second operand is | |
4182 | constant and the first operand already has the right value | |
4183 | range. */ | |
4184 | case TRUNC_DIV_EXPR: | |
4185 | case TRUNC_MOD_EXPR: | |
4186 | if ((TREE_CODE (rhs1) == SSA_NAME | |
4187 | || TREE_CODE (rhs1) == INTEGER_CST) | |
4188 | && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) | |
4189 | return simplify_div_or_mod_using_ranges (gsi, stmt); | |
4190 | break; | |
4191 | ||
4192 | /* Transform ABS (X) into X or -X as appropriate. */ | |
4193 | case ABS_EXPR: | |
4194 | if (TREE_CODE (rhs1) == SSA_NAME | |
4195 | && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) | |
4196 | return simplify_abs_using_ranges (gsi, stmt); | |
4197 | break; | |
4198 | ||
4199 | case BIT_AND_EXPR: | |
4200 | case BIT_IOR_EXPR: | |
4201 | /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR | |
4202 | if all the bits being cleared are already cleared or | |
4203 | all the bits being set are already set. */ | |
4204 | if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) | |
4205 | return simplify_bit_ops_using_ranges (gsi, stmt); | |
4206 | break; | |
4207 | ||
4208 | CASE_CONVERT: | |
4209 | if (TREE_CODE (rhs1) == SSA_NAME | |
4210 | && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) | |
4211 | return simplify_conversion_using_ranges (gsi, stmt); | |
4212 | break; | |
4213 | ||
4214 | case FLOAT_EXPR: | |
4215 | if (TREE_CODE (rhs1) == SSA_NAME | |
4216 | && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) | |
4217 | return simplify_float_conversion_using_ranges (gsi, stmt); | |
4218 | break; | |
4219 | ||
4220 | case MIN_EXPR: | |
4221 | case MAX_EXPR: | |
4222 | return simplify_min_or_max_using_ranges (gsi, stmt); | |
4223 | ||
4224 | default: | |
4225 | break; | |
4226 | } | |
4227 | } | |
4228 | else if (gimple_code (stmt) == GIMPLE_COND) | |
4229 | return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt)); | |
4230 | else if (gimple_code (stmt) == GIMPLE_SWITCH) | |
4231 | return simplify_switch_using_ranges (as_a <gswitch *> (stmt)); | |
4232 | else if (is_gimple_call (stmt) | |
4233 | && gimple_call_internal_p (stmt)) | |
4234 | return simplify_internal_call_using_ranges (gsi, stmt); | |
4235 | ||
4236 | return false; | |
4237 | } | |
4238 | ||
f86c2e71 RB |
4239 | /* Set the lattice entry for VAR to VR. */ |
4240 | ||
c2ad9885 | 4241 | void |
028d81b1 | 4242 | vr_values::set_vr_value (tree var, value_range_equiv *vr) |
c2ad9885 JL |
4243 | { |
4244 | if (SSA_NAME_VERSION (var) >= num_vr_values) | |
4245 | return; | |
4246 | vr_value[SSA_NAME_VERSION (var)] = vr; | |
4247 | } | |
4248 | ||
f86c2e71 RB |
4249 | /* Swap the lattice entry for VAR with VR and return the old entry. */ |
4250 | ||
028d81b1 AH |
4251 | value_range_equiv * |
4252 | vr_values::swap_vr_value (tree var, value_range_equiv *vr) | |
f86c2e71 RB |
4253 | { |
4254 | if (SSA_NAME_VERSION (var) >= num_vr_values) | |
4255 | return NULL; | |
4256 | std::swap (vr_value[SSA_NAME_VERSION (var)], vr); | |
4257 | return vr; | |
4258 | } |