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