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