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