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