Line data Source code
1 : /* Support routines for Value Range Propagation (VRP).
2 : Copyright (C) 2005-2026 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-iterator.h"
36 : #include "gimple-fold.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 "range.h"
50 : #include "vr-values.h"
51 : #include "cfghooks.h"
52 : #include "range-op.h"
53 : #include "gimple-range.h"
54 :
55 : /* Return true if op is in a boolean [0, 1] value-range. */
56 :
57 : bool
58 510063 : simplify_using_ranges::op_with_boolean_value_range_p (tree op, gimple *s)
59 : {
60 510063 : if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
61 : return true;
62 :
63 496939 : if (integer_zerop (op)
64 496939 : || integer_onep (op))
65 : return true;
66 :
67 487014 : if (TREE_CODE (op) != SSA_NAME)
68 : return false;
69 :
70 : /* ?? Errr, this should probably check for [0,0] and [1,1] as well
71 : as [0,1]. */
72 487014 : int_range_max vr;
73 487014 : return (query->range_of_expr (vr, op, s)
74 487014 : && vr == range_true_and_false (TREE_TYPE (op)));
75 487014 : }
76 :
77 : /* Helper function for simplify_internal_call_using_ranges and
78 : extract_range_basic. Return true if OP0 SUBCODE OP1 for
79 : SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
80 : always overflow. Set *OVF to true if it is known to always
81 : overflow. */
82 :
83 : static bool
84 144197 : check_for_binary_op_overflow (range_query *query,
85 : enum tree_code subcode, tree type,
86 : tree op0, tree op1, bool *ovf, gimple *s = NULL)
87 : {
88 144197 : relation_kind rel = VREL_VARYING;
89 : /* For subtraction see if relations could simplify it. */
90 144197 : if (s
91 144197 : && subcode == MINUS_EXPR
92 144197 : && types_compatible_p (TREE_TYPE (op0), TREE_TYPE (op1)))
93 : {
94 29039 : rel = query->relation().query (s, op0, op1);
95 : /* The result of the infinite precision subtraction of
96 : the same values will be always 0. That will fit into any result
97 : type. */
98 29039 : if (rel == VREL_EQ)
99 : return true;
100 : }
101 :
102 144196 : int_range_max vr0, vr1;
103 144196 : if (!query->range_of_expr (vr0, op0, s) || vr0.undefined_p ())
104 12 : vr0.set_varying (TREE_TYPE (op0));
105 144196 : if (!query->range_of_expr (vr1, op1, s) || vr1.undefined_p ())
106 8 : vr1.set_varying (TREE_TYPE (op1));
107 :
108 144196 : tree vr0min = wide_int_to_tree (TREE_TYPE (op0), vr0.lower_bound ());
109 144196 : tree vr0max = wide_int_to_tree (TREE_TYPE (op0), vr0.upper_bound ());
110 144196 : tree vr1min = wide_int_to_tree (TREE_TYPE (op1), vr1.lower_bound ());
111 144196 : tree vr1max = wide_int_to_tree (TREE_TYPE (op1), vr1.upper_bound ());
112 :
113 : /* If op1 is not negative, op0 - op1 in infinite precision for op0 >= op1
114 : will be always in [0, op0] and so if vr0max - vr1min fits into type,
115 : there won't be any overflow. */
116 144196 : if ((rel == VREL_GT || rel == VREL_GE)
117 28 : && tree_int_cst_sgn (vr1min) >= 0
118 144218 : && !arith_overflowed_p (MINUS_EXPR, type, vr0max, vr1min))
119 : return true;
120 :
121 : /* If op1 is not negative, op0 - op1 in infinite precision for op0 < op1
122 : will be always in [-inf, -1] and so will always overflow if type is
123 : unsigned. */
124 144174 : if (rel == VREL_LT
125 1 : && tree_int_cst_sgn (vr1min) >= 0
126 144175 : && TYPE_UNSIGNED (type))
127 : {
128 1 : *ovf = true;
129 1 : return true;
130 : }
131 :
132 236516 : *ovf = arith_overflowed_p (subcode, type, vr0min,
133 : subcode == MINUS_EXPR ? vr1max : vr1min);
134 236516 : if (arith_overflowed_p (subcode, type, vr0max,
135 144173 : subcode == MINUS_EXPR ? vr1min : vr1max) != *ovf)
136 : return false;
137 39475 : if (subcode == MULT_EXPR)
138 : {
139 23934 : if (arith_overflowed_p (subcode, type, vr0min, vr1max) != *ovf
140 23934 : || arith_overflowed_p (subcode, type, vr0max, vr1min) != *ovf)
141 : return false;
142 : }
143 39099 : if (*ovf)
144 : {
145 : /* So far we found that there is an overflow on the boundaries.
146 : That doesn't prove that there is an overflow even for all values
147 : in between the boundaries. For that compute widest2_int range
148 : of the result and see if it doesn't overlap the range of
149 : type. */
150 37115 : widest2_int wmin, wmax;
151 334227 : widest2_int w[4];
152 37115 : int i;
153 37115 : signop sign0 = TYPE_SIGN (TREE_TYPE (op0));
154 37115 : signop sign1 = TYPE_SIGN (TREE_TYPE (op1));
155 37166 : w[0] = widest2_int::from (vr0.lower_bound (), sign0);
156 37174 : w[1] = widest2_int::from (vr0.upper_bound (), sign0);
157 37152 : w[2] = widest2_int::from (vr1.lower_bound (), sign1);
158 37160 : w[3] = widest2_int::from (vr1.upper_bound (), sign1);
159 185575 : for (i = 0; i < 4; i++)
160 : {
161 148460 : widest2_int wt;
162 148460 : switch (subcode)
163 : {
164 27068 : case PLUS_EXPR:
165 27068 : wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
166 27068 : break;
167 27904 : case MINUS_EXPR:
168 27904 : wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
169 27904 : break;
170 93488 : case MULT_EXPR:
171 93488 : wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
172 93488 : break;
173 0 : default:
174 0 : gcc_unreachable ();
175 : }
176 148460 : if (i == 0)
177 : {
178 37115 : wmin = wt;
179 37115 : wmax = wt;
180 : }
181 : else
182 : {
183 111345 : wmin = wi::smin (wmin, wt);
184 112195 : wmax = wi::smax (wmax, wt);
185 : }
186 148460 : }
187 : /* The result of op0 CODE op1 is known to be in range
188 : [wmin, wmax]. */
189 37115 : widest2_int wtmin
190 74230 : = widest2_int::from (irange_val_min (type), TYPE_SIGN (type));
191 37115 : widest2_int wtmax
192 74230 : = widest2_int::from (irange_val_max (type), TYPE_SIGN (type));
193 : /* If all values in [wmin, wmax] are smaller than
194 : [wtmin, wtmax] or all are larger than [wtmin, wtmax],
195 : the arithmetic operation will always overflow. */
196 73220 : if (wmax < wtmin || wmin > wtmax)
197 1661 : return true;
198 : return false;
199 222970 : }
200 : return true;
201 144196 : }
202 :
203 : /* Set INIT, STEP, and DIRECTION to the corresponding values of NAME
204 : within LOOP, and return TRUE. Otherwise return FALSE, and set R to
205 : the conservative range of NAME within the loop. */
206 :
207 : static bool
208 5773231 : get_scev_info (vrange &r, tree name, gimple *stmt, class loop *l,
209 : tree &init, tree &step, enum ev_direction &dir)
210 : {
211 5773231 : tree ev = analyze_scalar_evolution (l, name);
212 5773231 : tree chrec = instantiate_parameters (l, ev);
213 5773231 : tree type = TREE_TYPE (name);
214 5773231 : if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
215 : {
216 2742672 : r.set_varying (type);
217 2742672 : return false;
218 : }
219 3030559 : if (is_gimple_min_invariant (chrec))
220 : {
221 0 : if (is_gimple_constant (chrec))
222 0 : r.set (chrec, chrec);
223 : else
224 0 : r.set_varying (type);
225 : return false;
226 : }
227 :
228 3030559 : init = initial_condition_in_loop_num (chrec, l->num);
229 3030559 : step = evolution_part_in_loop_num (chrec, l->num);
230 3030559 : if (!init || !step)
231 : {
232 0 : r.set_varying (type);
233 0 : return false;
234 : }
235 3030559 : dir = scev_direction (chrec);
236 3030559 : if (dir == EV_DIR_UNKNOWN
237 3030559 : || scev_probably_wraps_p (NULL, init, step, stmt,
238 : get_chrec_loop (chrec), true))
239 : {
240 496738 : r.set_varying (type);
241 496738 : return false;
242 : }
243 : return true;
244 : }
245 :
246 : /* Return TRUE if STEP * NIT may overflow when calculated in TYPE. */
247 :
248 : static bool
249 2153781 : induction_variable_may_overflow_p (tree type,
250 : const wide_int &step, const widest_int &nit)
251 : {
252 2153781 : wi::overflow_type ovf;
253 2153781 : signop sign = TYPE_SIGN (type);
254 2153781 : widest_int max_step = wi::mul (widest_int::from (step, sign),
255 2153781 : nit, sign, &ovf);
256 :
257 2153781 : if (ovf || !wi::fits_to_tree_p (max_step, type))
258 : return true;
259 :
260 : /* For a signed type we have to check whether the result has the
261 : expected signedness which is that of the step as number of
262 : iterations is unsigned. */
263 2036902 : return (sign == SIGNED
264 4073168 : && wi::gts_p (max_step, 0) != wi::gts_p (step, 0));
265 2153781 : }
266 :
267 : /* Set R to the range from BEGIN to END, assuming the direction of the
268 : loop is DIR. */
269 :
270 : static void
271 2533528 : range_from_loop_direction (irange &r, tree type,
272 : const irange &begin, const irange &end,
273 : ev_direction dir)
274 : {
275 2533528 : signop sign = TYPE_SIGN (type);
276 :
277 2533528 : if (begin.undefined_p () || end.undefined_p ())
278 3388 : r.set_varying (type);
279 2530140 : else if (dir == EV_DIR_GROWS)
280 : {
281 2273174 : if (wi::ge_p (begin.lower_bound (), end.upper_bound (), sign))
282 229 : r.set_varying (type);
283 : else
284 2272945 : r = int_range<1> (type, begin.lower_bound (), end.upper_bound ());
285 : }
286 : else
287 : {
288 256988 : if (wi::ge_p (end.lower_bound (), begin.upper_bound (), sign))
289 223 : r.set_varying (type);
290 : else
291 256765 : r = int_range<1> (type, end.lower_bound (), begin.upper_bound ());
292 : }
293 2533528 : }
294 :
295 : /* Set V to the range of NAME in STMT within LOOP. Return TRUE if a
296 : range was found. */
297 :
298 : bool
299 5773231 : range_of_var_in_loop (vrange &v, tree name, class loop *l, gimple *stmt,
300 : range_query *query)
301 : {
302 5773231 : tree init, step;
303 5773231 : enum ev_direction dir;
304 5773231 : if (!get_scev_info (v, name, stmt, l, init, step, dir))
305 : return true;
306 :
307 : // Calculate ranges for the values from SCEV.
308 2533821 : irange &r = as_a <irange> (v);
309 2533821 : tree type = TREE_TYPE (init);
310 2533821 : int_range<2> rinit (type), rstep (type), max_init (type);
311 2533821 : if (!query->range_of_expr (rinit, init, stmt)
312 2533821 : || !query->range_of_expr (rstep, step, stmt))
313 : return false;
314 :
315 : // Calculate the final range of NAME if possible.
316 2533821 : if (rinit.singleton_p () && rstep.singleton_p ())
317 : {
318 2154074 : widest_int nit;
319 2154074 : if (!max_loop_iterations (l, &nit))
320 : return false;
321 :
322 2153782 : if (!induction_variable_may_overflow_p (type, rstep.lower_bound (), nit))
323 : {
324 : // Calculate the max bounds for init (init + niter * step).
325 2036266 : wide_int w = wide_int::from (nit, TYPE_PRECISION (type), TYPE_SIGN (type));
326 2036266 : int_range<1> niter (type, w, w);
327 2036266 : int_range_max max_step;
328 2036266 : range_op_handler mult_handler (MULT_EXPR);
329 2036266 : range_op_handler plus_handler (PLUS_EXPR);
330 2036266 : if (!mult_handler.fold_range (max_step, type, niter, rstep)
331 2036266 : || !plus_handler.fold_range (max_init, type, rinit, max_step))
332 0 : return false;
333 2036267 : }
334 2154074 : }
335 2533528 : range_from_loop_direction (r, type, rinit, max_init, dir);
336 2533528 : return true;
337 2533821 : }
338 :
339 : /* Helper function for vrp_evaluate_conditional_warnv & other
340 : optimizers. */
341 :
342 : tree
343 21060742 : simplify_using_ranges::fold_cond_with_ops (enum tree_code code,
344 : tree op0, tree op1, gimple *s)
345 : {
346 21060742 : value_range r0 (TREE_TYPE (op0));
347 21060742 : value_range r1 (TREE_TYPE (op1));
348 21060742 : if (!query->range_of_expr (r0, op0, s)
349 21060742 : || !query->range_of_expr (r1, op1, s))
350 : return NULL_TREE;
351 :
352 21053316 : int_range<1> res;
353 21053316 : range_op_handler handler (code);
354 :
355 : // Find any relation between op0 and op1 and pass it to fold_range.
356 21053316 : relation_kind rel = VREL_VARYING;
357 21053316 : if (gimple_range_ssa_p (op0) && gimple_range_ssa_p (op1))
358 4644047 : rel = query->relation ().query (s, op0, op1);
359 :
360 21053316 : if (handler && handler.fold_range (res, boolean_type_node, r0, r1,
361 : relation_trio::op1_op2 (rel)))
362 : {
363 21053316 : if (res == range_true ())
364 8703 : return boolean_true_node;
365 21044613 : if (res == range_false ())
366 5014 : return boolean_false_node;
367 : }
368 : return NULL;
369 21060742 : }
370 :
371 : /* Helper function for legacy_fold_cond. */
372 :
373 : tree
374 21369657 : simplify_using_ranges::legacy_fold_cond_overflow (gimple *stmt)
375 : {
376 21369657 : tree ret;
377 21369657 : tree_code code = gimple_cond_code (stmt);
378 21369657 : tree op0 = gimple_cond_lhs (stmt);
379 21369657 : tree op1 = gimple_cond_rhs (stmt);
380 :
381 : /* We only deal with integral and pointer types. */
382 42378028 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
383 26130861 : && !POINTER_TYPE_P (TREE_TYPE (op0)))
384 : return NULL_TREE;
385 :
386 : /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
387 : as a simple equality test, then prefer that over its current form
388 : for evaluation.
389 :
390 : An overflow test which collapses to an equality test can always be
391 : expressed as a comparison of one argument against zero. Overflow
392 : occurs when the chosen argument is zero and does not occur if the
393 : chosen argument is not zero. */
394 20509352 : tree x;
395 20509352 : if (overflow_comparison_p (code, op0, op1, &x))
396 : {
397 1163 : wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
398 : /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
399 : B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
400 : B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
401 : B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
402 1163 : if (integer_zerop (x))
403 : {
404 170 : op1 = x;
405 170 : code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
406 : }
407 : /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
408 : B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
409 : B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
410 : B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
411 993 : else if (wi::to_wide (x) == max - 1)
412 : {
413 651 : op0 = op1;
414 651 : op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
415 651 : code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
416 : }
417 : else
418 : {
419 342 : int_range_max vro, vri;
420 342 : tree type = TREE_TYPE (op0);
421 342 : if (code == GT_EXPR || code == GE_EXPR)
422 : {
423 92 : vro.set (type,
424 184 : wi::to_wide (TYPE_MIN_VALUE (type)),
425 92 : wi::to_wide (x), VR_ANTI_RANGE);
426 92 : vri.set (type,
427 184 : wi::to_wide (TYPE_MIN_VALUE (type)),
428 184 : wi::to_wide (x));
429 : }
430 250 : else if (code == LT_EXPR || code == LE_EXPR)
431 : {
432 250 : vro.set (type,
433 500 : wi::to_wide (TYPE_MIN_VALUE (type)),
434 250 : wi::to_wide (x));
435 250 : vri.set (type,
436 500 : wi::to_wide (TYPE_MIN_VALUE (type)),
437 500 : wi::to_wide (x),
438 : VR_ANTI_RANGE);
439 : }
440 : else
441 0 : gcc_unreachable ();
442 342 : int_range_max vr0;
443 342 : if (!query->range_of_expr (vr0, op0, stmt))
444 0 : vr0.set_varying (TREE_TYPE (op0));
445 : /* If vro, the range for OP0 to pass the overflow test, has
446 : no intersection with *vr0, OP0's known range, then the
447 : overflow test can't pass, so return the node for false.
448 : If it is the inverted range, vri, that has no
449 : intersection, then the overflow test must pass, so return
450 : the node for true. In other cases, we could proceed with
451 : a simplified condition comparing OP0 and X, with LE_EXPR
452 : for previously LE_ or LT_EXPR and GT_EXPR otherwise, but
453 : the comments next to the enclosing if suggest it's not
454 : generally profitable to do so. */
455 342 : vro.intersect (vr0);
456 342 : if (vro.undefined_p ())
457 8 : return boolean_false_node;
458 334 : vri.intersect (vr0);
459 334 : if (vri.undefined_p ())
460 0 : return boolean_true_node;
461 342 : }
462 1163 : }
463 :
464 20509344 : if ((ret = fold_cond_with_ops (code, op0, op1, stmt)))
465 : return ret;
466 : return NULL_TREE;
467 : }
468 :
469 : /* Visit conditional statement STMT. If we can determine which edge
470 : will be taken out of STMT's basic block, record it in
471 : *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
472 :
473 : void
474 21369657 : simplify_using_ranges::legacy_fold_cond (gcond *stmt, edge *taken_edge_p)
475 : {
476 21369657 : tree val;
477 :
478 21369657 : *taken_edge_p = NULL;
479 :
480 21369657 : if (dump_file && (dump_flags & TDF_DETAILS))
481 : {
482 384 : tree use;
483 384 : ssa_op_iter i;
484 :
485 384 : fprintf (dump_file, "\nVisiting conditional with predicate: ");
486 384 : print_gimple_stmt (dump_file, stmt, 0);
487 384 : fprintf (dump_file, "\nWith known ranges\n");
488 :
489 856 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
490 : {
491 472 : fprintf (dump_file, "\t");
492 472 : print_generic_expr (dump_file, use);
493 472 : fprintf (dump_file, ": ");
494 472 : value_range r (TREE_TYPE (use));
495 472 : query->range_of_expr (r, use, stmt);
496 472 : r.dump (dump_file);
497 472 : }
498 :
499 384 : fprintf (dump_file, "\n");
500 : }
501 :
502 21369657 : val = legacy_fold_cond_overflow (stmt);
503 21369657 : if (val)
504 9 : *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
505 :
506 21369657 : if (dump_file && (dump_flags & TDF_DETAILS))
507 : {
508 384 : fprintf (dump_file, "\nPredicate evaluates to: ");
509 384 : if (val == NULL_TREE)
510 384 : fprintf (dump_file, "DON'T KNOW\n");
511 : else
512 0 : print_generic_stmt (dump_file, val);
513 : }
514 21369657 : }
515 :
516 : /* Simplify boolean operations if the source is known
517 : to be already a boolean. */
518 : bool
519 492984 : simplify_using_ranges::simplify_truth_ops_using_ranges
520 : (gimple_stmt_iterator *gsi,
521 : gimple *stmt)
522 : {
523 492984 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
524 492984 : tree lhs, op0, op1;
525 492984 : bool need_conversion;
526 :
527 : /* We handle only !=/== case here. */
528 492984 : gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
529 :
530 492984 : op0 = gimple_assign_rhs1 (stmt);
531 492984 : if (!op_with_boolean_value_range_p (op0, stmt))
532 : return false;
533 :
534 17079 : op1 = gimple_assign_rhs2 (stmt);
535 17079 : if (!op_with_boolean_value_range_p (op1, stmt))
536 : return false;
537 :
538 : /* Reduce number of cases to handle to NE_EXPR. As there is no
539 : BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
540 16561 : if (rhs_code == EQ_EXPR)
541 : {
542 8330 : if (TREE_CODE (op1) == INTEGER_CST)
543 3590 : op1 = int_const_binop (BIT_XOR_EXPR, op1,
544 7180 : build_int_cst (TREE_TYPE (op1), 1));
545 : else
546 : return false;
547 : }
548 :
549 11821 : lhs = gimple_assign_lhs (stmt);
550 11821 : need_conversion
551 11821 : = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
552 :
553 : /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
554 11821 : if (need_conversion
555 10433 : && !TYPE_UNSIGNED (TREE_TYPE (op0))
556 3904 : && TYPE_PRECISION (TREE_TYPE (op0)) == 1
557 11850 : && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
558 : return false;
559 :
560 : /* For A != 0 we can substitute A itself. */
561 11821 : if (integer_zerop (op1))
562 7079 : gimple_assign_set_rhs_with_ops (gsi,
563 : need_conversion
564 0 : ? NOP_EXPR : TREE_CODE (op0), op0);
565 : /* For A != B we substitute A ^ B. Either with conversion. */
566 4742 : else if (need_conversion)
567 : {
568 3354 : tree tem = make_ssa_name (TREE_TYPE (op0));
569 3354 : gassign *newop
570 3354 : = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
571 3354 : gsi_insert_before (gsi, newop, GSI_SAME_STMT);
572 6682 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
573 6682 : && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
574 : {
575 6488 : int_range<1> vr (TREE_TYPE (tem),
576 6488 : wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
577 6488 : wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
578 3244 : set_range_info (tem, vr);
579 3244 : }
580 3354 : gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
581 : }
582 : /* Or without. */
583 : else
584 1388 : gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
585 11821 : update_stmt (gsi_stmt (*gsi));
586 11821 : fold_stmt (gsi, follow_single_use_edges);
587 :
588 11821 : return true;
589 : }
590 :
591 : /* Simplify a division or modulo operator to a right shift or bitwise and
592 : if the first operand is unsigned or is greater than zero and the second
593 : operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
594 : constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
595 : optimize it into just op0 if op0's range is known to be a subset of
596 : [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
597 : modulo. */
598 :
599 : bool
600 336439 : simplify_using_ranges::simplify_div_or_mod_using_ranges
601 : (gimple_stmt_iterator *gsi,
602 : gimple *stmt)
603 : {
604 336439 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
605 336439 : tree val = NULL;
606 336439 : tree op0 = gimple_assign_rhs1 (stmt);
607 336439 : tree op1 = gimple_assign_rhs2 (stmt);
608 336439 : tree op0min = NULL_TREE, op0max = NULL_TREE;
609 336439 : tree op1min = op1;
610 336439 : int_range_max vr;
611 :
612 336439 : if (TREE_CODE (op0) == INTEGER_CST)
613 : {
614 : op0min = op0;
615 : op0max = op0;
616 : }
617 : else
618 : {
619 310285 : if (!query->range_of_expr (vr, op0, stmt))
620 0 : vr.set_varying (TREE_TYPE (op0));
621 310285 : if (!vr.varying_p () && !vr.undefined_p ())
622 : {
623 154282 : tree type = vr.type ();
624 154282 : op0min = wide_int_to_tree (type, vr.lower_bound ());
625 154291 : op0max = wide_int_to_tree (type, vr.upper_bound ());
626 : }
627 : }
628 :
629 336439 : if (rhs_code == TRUNC_MOD_EXPR
630 158342 : && TREE_CODE (op1) == SSA_NAME)
631 : {
632 103164 : int_range_max vr1;
633 103164 : if (!query->range_of_expr (vr1, op1, stmt))
634 0 : vr1.set_varying (TREE_TYPE (op1));
635 103164 : if (!vr1.varying_p () && !vr1.undefined_p ())
636 27086 : op1min = wide_int_to_tree (vr1.type (), vr1.lower_bound ());
637 103164 : }
638 158342 : if (rhs_code == TRUNC_MOD_EXPR
639 158342 : && TREE_CODE (op1min) == INTEGER_CST
640 82260 : && tree_int_cst_sgn (op1min) == 1
641 60637 : && op0max
642 35051 : && tree_int_cst_lt (op0max, op1min))
643 : {
644 1207 : if (TYPE_UNSIGNED (TREE_TYPE (op0))
645 530 : || tree_int_cst_sgn (op0min) >= 0
646 1436 : || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
647 : op0min))
648 : {
649 : /* If op0 already has the range op0 % op1 has,
650 : then TRUNC_MOD_EXPR won't change anything. */
651 1170 : gimple_assign_set_rhs_from_tree (gsi, op0);
652 1170 : return true;
653 : }
654 : }
655 :
656 335269 : if (TREE_CODE (op0) != SSA_NAME)
657 : return false;
658 :
659 309115 : if (!integer_pow2p (op1))
660 : {
661 : /* X % -Y can be only optimized into X % Y either if
662 : X is not INT_MIN, or Y is not -1. Fold it now, as after
663 : remove_range_assertions the range info might be not available
664 : anymore. */
665 269054 : if (rhs_code == TRUNC_MOD_EXPR
666 269054 : && fold_stmt (gsi, follow_single_use_edges))
667 12 : return true;
668 : return false;
669 : }
670 :
671 40061 : if (TYPE_UNSIGNED (TREE_TYPE (op0)))
672 0 : val = integer_one_node;
673 : else
674 : {
675 40061 : tree zero = build_zero_cst (TREE_TYPE (op0));
676 40061 : val = fold_cond_with_ops (GE_EXPR, op0, zero, stmt);
677 : }
678 :
679 40061 : if (val && integer_onep (val))
680 : {
681 6482 : tree t;
682 :
683 6482 : if (rhs_code == TRUNC_DIV_EXPR)
684 : {
685 4220 : t = build_int_cst (integer_type_node, tree_log2 (op1));
686 4220 : gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
687 4220 : gimple_assign_set_rhs1 (stmt, op0);
688 4220 : gimple_assign_set_rhs2 (stmt, t);
689 : }
690 : else
691 : {
692 2262 : t = build_int_cst (TREE_TYPE (op1), 1);
693 2262 : t = int_const_binop (MINUS_EXPR, op1, t);
694 2262 : t = fold_convert (TREE_TYPE (op0), t);
695 :
696 2262 : gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
697 2262 : gimple_assign_set_rhs1 (stmt, op0);
698 2262 : gimple_assign_set_rhs2 (stmt, t);
699 : }
700 :
701 6482 : update_stmt (stmt);
702 6482 : fold_stmt (gsi, follow_single_use_edges);
703 6482 : return true;
704 : }
705 :
706 : return false;
707 336439 : }
708 :
709 : /* Simplify a min or max if the ranges of the two operands are
710 : disjoint. Return true if we do simplify. */
711 :
712 : bool
713 249259 : simplify_using_ranges::simplify_min_or_max_using_ranges
714 : (gimple_stmt_iterator *gsi,
715 : gimple *stmt)
716 : {
717 249259 : tree op0 = gimple_assign_rhs1 (stmt);
718 249259 : tree op1 = gimple_assign_rhs2 (stmt);
719 249259 : tree val;
720 :
721 249259 : val = fold_cond_with_ops (LE_EXPR, op0, op1, stmt);
722 249259 : if (!val)
723 243414 : val = fold_cond_with_ops (LT_EXPR, op0, op1, stmt);
724 :
725 243414 : if (val)
726 : {
727 : /* VAL == TRUE -> OP0 < or <= op1
728 : VAL == FALSE -> OP0 > or >= op1. */
729 6861 : tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
730 6861 : == integer_zerop (val)) ? op0 : op1;
731 6861 : gimple_assign_set_rhs_from_tree (gsi, res);
732 6861 : return true;
733 : }
734 :
735 : return false;
736 : }
737 :
738 : /* If the operand to an ABS_EXPR is >= 0, then eliminate the
739 : ABS_EXPR. If the operand is <= 0, then simplify the
740 : ABS_EXPR into a NEGATE_EXPR. */
741 :
742 : bool
743 9457 : simplify_using_ranges::simplify_abs_using_ranges (gimple_stmt_iterator *gsi,
744 : gimple *stmt)
745 : {
746 9457 : tree op = gimple_assign_rhs1 (stmt);
747 9457 : tree zero = build_zero_cst (TREE_TYPE (op));
748 9457 : tree val = fold_cond_with_ops (LE_EXPR, op, zero, stmt);
749 :
750 9457 : if (!val)
751 : {
752 : /* The range is neither <= 0 nor > 0. Now see if it is
753 : either < 0 or >= 0. */
754 9207 : val = fold_cond_with_ops (LT_EXPR, op, zero, stmt);
755 : }
756 9207 : if (val)
757 : {
758 372 : gimple_assign_set_rhs1 (stmt, op);
759 372 : if (integer_zerop (val))
760 172 : gimple_assign_set_rhs_code (stmt, SSA_NAME);
761 : else
762 200 : gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
763 372 : update_stmt (stmt);
764 372 : fold_stmt (gsi, follow_single_use_edges);
765 372 : return true;
766 : }
767 : return false;
768 : }
769 :
770 : /* irange wrapper for wi_set_zero_nonzero_bits.
771 :
772 : Return TRUE if VR was a constant range and we were able to compute
773 : the bit masks. */
774 :
775 : static bool
776 1644792 : vr_set_zero_nonzero_bits (const tree expr_type,
777 : const irange *vr,
778 : wide_int *may_be_nonzero,
779 : wide_int *must_be_nonzero)
780 : {
781 1644792 : if (vr->varying_p () || vr->undefined_p ())
782 : {
783 970441 : *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
784 970441 : *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
785 970441 : return false;
786 : }
787 674353 : wi_set_zero_nonzero_bits (expr_type, vr->lower_bound (), vr->upper_bound (),
788 : *may_be_nonzero, *must_be_nonzero);
789 674351 : return true;
790 : }
791 :
792 : /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
793 : If all the bits that are being cleared by & are already
794 : known to be zero from VR, or all the bits that are being
795 : set by | are already known to be one from VR, the bit
796 : operation is redundant. */
797 :
798 : bool
799 1286633 : simplify_using_ranges::simplify_bit_ops_using_ranges
800 : (gimple_stmt_iterator *gsi,
801 : gimple *stmt)
802 : {
803 1286633 : tree op0 = gimple_assign_rhs1 (stmt);
804 1286633 : tree op1 = gimple_assign_rhs2 (stmt);
805 1286633 : tree op = NULL_TREE;
806 1286633 : int_range_max vr0, vr1;
807 1286633 : wide_int may_be_nonzero0, may_be_nonzero1;
808 1286633 : wide_int must_be_nonzero0, must_be_nonzero1;
809 1286633 : wide_int mask;
810 :
811 1286633 : if (!query->range_of_expr (vr0, op0, stmt)
812 1286633 : || vr0.undefined_p ())
813 : return false;
814 1285954 : if (!query->range_of_expr (vr1, op1, stmt)
815 1285954 : || vr1.undefined_p ())
816 : return false;
817 :
818 1285603 : if (!vr_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
819 : &must_be_nonzero0))
820 : return false;
821 359189 : if (!vr_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
822 : &must_be_nonzero1))
823 : return false;
824 :
825 315162 : switch (gimple_assign_rhs_code (stmt))
826 : {
827 222691 : case BIT_AND_EXPR:
828 222691 : mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
829 222691 : if (mask == 0)
830 : {
831 : op = op0;
832 : break;
833 : }
834 218069 : mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
835 218069 : if (mask == 0)
836 : {
837 : op = op1;
838 : break;
839 : }
840 : break;
841 92471 : case BIT_IOR_EXPR:
842 92471 : mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
843 92471 : if (mask == 0)
844 : {
845 : op = op1;
846 : break;
847 : }
848 92471 : mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
849 92471 : if (mask == 0)
850 : {
851 : op = op0;
852 : break;
853 : }
854 : break;
855 0 : default:
856 0 : gcc_unreachable ();
857 : }
858 :
859 4818 : if (op == NULL_TREE)
860 : return false;
861 :
862 4818 : gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
863 4818 : update_stmt (gsi_stmt (*gsi));
864 4818 : return true;
865 1286645 : }
866 :
867 : /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
868 : a known value range VR.
869 :
870 : If there is one and only one value which will satisfy the
871 : conditional, then return that value. Else return NULL. */
872 :
873 : static tree
874 1641628 : test_for_singularity (enum tree_code cond_code, tree op0,
875 : tree op1, const irange *vr)
876 : {
877 1641628 : tree min = NULL;
878 1641628 : tree max = NULL;
879 :
880 : /* Extract minimum/maximum values which satisfy the conditional as it was
881 : written. */
882 1641628 : if (cond_code == LE_EXPR || cond_code == LT_EXPR)
883 : {
884 716768 : min = TYPE_MIN_VALUE (TREE_TYPE (op0));
885 :
886 716768 : max = op1;
887 716768 : if (cond_code == LT_EXPR)
888 : {
889 90631 : tree one = build_int_cst (TREE_TYPE (op0), 1);
890 90631 : max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
891 : }
892 : }
893 924860 : else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
894 : {
895 786836 : max = TYPE_MAX_VALUE (TREE_TYPE (op0));
896 :
897 786836 : min = op1;
898 786836 : if (cond_code == GT_EXPR)
899 : {
900 697824 : tree one = build_int_cst (TREE_TYPE (op0), 1);
901 697824 : min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
902 : }
903 : }
904 :
905 : /* Now refine the minimum and maximum values using any
906 : value range information we have for op0. */
907 1641628 : if (min && max)
908 : {
909 1503604 : tree type = TREE_TYPE (op0);
910 1503604 : tree tmin = wide_int_to_tree (type, vr->lower_bound ());
911 1503604 : tree tmax = wide_int_to_tree (type, vr->upper_bound ());
912 1503604 : if (compare_values (tmin, min) == 1)
913 453224 : min = tmin;
914 1503604 : if (compare_values (tmax, max) == -1)
915 564211 : max = tmax;
916 :
917 : /* If the new min/max values have converged to a single value,
918 : then there is only one value which can satisfy the condition,
919 : return that value. */
920 1503604 : if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
921 318296 : return min;
922 : }
923 : return NULL;
924 : }
925 :
926 : /* Return whether the value range *VR fits in an integer type specified
927 : by PRECISION and UNSIGNED_P. */
928 :
929 : bool
930 265101 : range_fits_type_p (const irange *vr,
931 : unsigned dest_precision, signop dest_sgn)
932 : {
933 265101 : tree src_type;
934 265101 : unsigned src_precision;
935 265101 : widest_int tem;
936 265101 : signop src_sgn;
937 :
938 : /* Now we can only handle ranges with constant bounds. */
939 265101 : if (vr->undefined_p () || vr->varying_p ())
940 : return false;
941 :
942 : /* We can only handle integral and pointer types. */
943 214023 : src_type = vr->type ();
944 214023 : if (!INTEGRAL_TYPE_P (src_type)
945 0 : && !POINTER_TYPE_P (src_type))
946 : return false;
947 :
948 : /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
949 : and so is an identity transform. */
950 214023 : src_precision = TYPE_PRECISION (src_type);
951 214023 : src_sgn = TYPE_SIGN (src_type);
952 214023 : if ((src_precision < dest_precision
953 15510 : && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
954 199932 : || (src_precision == dest_precision && src_sgn == dest_sgn))
955 : return true;
956 :
957 199776 : wide_int vrmin = vr->lower_bound ();
958 199776 : wide_int vrmax = vr->upper_bound ();
959 :
960 : /* For sign changes, the MSB of the wide_int has to be clear.
961 : An unsigned value with its MSB set cannot be represented by
962 : a signed wide_int, while a negative value cannot be represented
963 : by an unsigned wide_int. */
964 199776 : if (src_sgn != dest_sgn
965 199776 : && (wi::lts_p (vrmin, 0) || wi::lts_p (vrmax, 0)))
966 : return false;
967 :
968 : /* Then we can perform the conversion on both ends and compare
969 : the result for equality. */
970 126645 : signop sign = TYPE_SIGN (vr->type ());
971 126645 : tem = wi::ext (widest_int::from (vrmin, sign), dest_precision, dest_sgn);
972 126645 : if (tem != widest_int::from (vrmin, sign))
973 : return false;
974 121006 : tem = wi::ext (widest_int::from (vrmax, sign), dest_precision, dest_sgn);
975 121006 : if (tem != widest_int::from (vrmax, sign))
976 : return false;
977 :
978 : return true;
979 199776 : }
980 :
981 : // Clear edge E of EDGE_EXECUTABLE (it is unexecutable). If it wasn't
982 : // previously clear, propagate to successor blocks if appropriate.
983 :
984 : void
985 312205 : simplify_using_ranges::set_and_propagate_unexecutable (edge e)
986 : {
987 : // If not_executable is already set, we're done.
988 : // This works in the absence of a flag as well.
989 312205 : if ((e->flags & m_not_executable_flag) == m_not_executable_flag)
990 223933 : return;
991 :
992 203874 : e->flags |= m_not_executable_flag;
993 203874 : m_flag_set_edges.safe_push (e);
994 :
995 : // Check if the destination block needs to propagate the property.
996 203874 : basic_block bb = e->dest;
997 :
998 : // If any incoming edge is executable, we are done.
999 203874 : edge_iterator ei;
1000 333284 : FOR_EACH_EDGE (e, ei, bb->preds)
1001 245012 : if ((e->flags & m_not_executable_flag) == 0)
1002 : return;
1003 :
1004 : // This block is also unexecutable, propagate to all exit edges as well.
1005 153743 : FOR_EACH_EDGE (e, ei, bb->succs)
1006 65471 : set_and_propagate_unexecutable (e);
1007 : }
1008 :
1009 : /* If COND can be folded entirely as TRUE or FALSE, rewrite the
1010 : conditional as such, and return TRUE. */
1011 :
1012 : bool
1013 21699234 : simplify_using_ranges::fold_cond (gcond *cond)
1014 : {
1015 21699234 : int_range_max r;
1016 21699234 : if (query->range_of_stmt (r, cond) && r.singleton_p ())
1017 : {
1018 : // COND has already been folded if arguments are constant.
1019 329577 : if (TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
1020 329577 : && TREE_CODE (gimple_cond_rhs (cond)) != SSA_NAME)
1021 : return false;
1022 239636 : if (dump_file)
1023 : {
1024 190 : fprintf (dump_file, "Folding predicate ");
1025 190 : print_gimple_expr (dump_file, cond, 0);
1026 190 : fprintf (dump_file, " to ");
1027 : }
1028 239636 : edge e0 = EDGE_SUCC (gimple_bb (cond), 0);
1029 239636 : edge e1 = EDGE_SUCC (gimple_bb (cond), 1);
1030 239636 : if (r.zero_p ())
1031 : {
1032 144135 : if (dump_file)
1033 136 : fprintf (dump_file, "0\n");
1034 144135 : gimple_cond_make_false (cond);
1035 144135 : if (e0->flags & EDGE_TRUE_VALUE)
1036 142314 : set_and_propagate_unexecutable (e0);
1037 : else
1038 1821 : set_and_propagate_unexecutable (e1);
1039 : }
1040 : else
1041 : {
1042 95501 : if (dump_file)
1043 54 : fprintf (dump_file, "1\n");
1044 95501 : gimple_cond_make_true (cond);
1045 95501 : if (e0->flags & EDGE_FALSE_VALUE)
1046 715 : set_and_propagate_unexecutable (e0);
1047 : else
1048 94786 : set_and_propagate_unexecutable (e1);
1049 : }
1050 239636 : update_stmt (cond);
1051 239636 : return true;
1052 : }
1053 :
1054 : // FIXME: Audit the code below and make sure it never finds anything.
1055 21369657 : edge taken_edge;
1056 21369657 : legacy_fold_cond (cond, &taken_edge);
1057 :
1058 21369657 : if (taken_edge)
1059 : {
1060 9 : if (taken_edge->flags & EDGE_TRUE_VALUE)
1061 : {
1062 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1063 0 : fprintf (dump_file, "\nVRP Predicate evaluates to: 1\n");
1064 0 : gimple_cond_make_true (cond);
1065 : }
1066 9 : else if (taken_edge->flags & EDGE_FALSE_VALUE)
1067 : {
1068 9 : if (dump_file && (dump_flags & TDF_DETAILS))
1069 0 : fprintf (dump_file, "\nVRP Predicate evaluates to: 0\n");
1070 9 : gimple_cond_make_false (cond);
1071 : }
1072 : else
1073 0 : gcc_unreachable ();
1074 9 : update_stmt (cond);
1075 9 : return true;
1076 : }
1077 : return false;
1078 21699234 : }
1079 :
1080 : /* Simplify a conditional using a relational operator to an equality
1081 : test if the range information indicates only one value can satisfy
1082 : the original conditional. */
1083 :
1084 : bool
1085 12532739 : simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt)
1086 : {
1087 12532739 : tree op0 = gimple_cond_lhs (stmt);
1088 12532739 : tree op1 = gimple_cond_rhs (stmt);
1089 12532739 : enum tree_code cond_code = gimple_cond_code (stmt);
1090 :
1091 12532739 : if (fold_cond (stmt))
1092 : return true;
1093 :
1094 12401077 : if (simplify_compare_using_ranges_1 (cond_code, op0, op1, stmt))
1095 : {
1096 383059 : if (dump_file)
1097 : {
1098 26 : fprintf (dump_file, "Simplified relational ");
1099 26 : print_gimple_stmt (dump_file, stmt, 0);
1100 26 : fprintf (dump_file, " into ");
1101 : }
1102 :
1103 383059 : gimple_cond_set_code (stmt, cond_code);
1104 383059 : gimple_cond_set_lhs (stmt, op0);
1105 383059 : gimple_cond_set_rhs (stmt, op1);
1106 :
1107 383059 : update_stmt (stmt);
1108 :
1109 383059 : if (dump_file)
1110 : {
1111 26 : print_gimple_stmt (dump_file, stmt, 0);
1112 26 : fprintf (dump_file, "\n");
1113 : }
1114 : return true;
1115 : }
1116 : return false;
1117 : }
1118 :
1119 : /* Like simplify_cond_using_ranges_1 but for assignments rather
1120 : than GIMPLE_COND. */
1121 :
1122 : bool
1123 1271630 : simplify_using_ranges::simplify_compare_assign_using_ranges_1
1124 : (gimple_stmt_iterator *gsi,
1125 : gimple *stmt)
1126 : {
1127 1271630 : enum tree_code code = gimple_assign_rhs_code (stmt);
1128 1271630 : tree op0 = gimple_assign_rhs1 (stmt);
1129 1271630 : tree op1 = gimple_assign_rhs2 (stmt);
1130 1271630 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1131 1271630 : bool happened = false;
1132 :
1133 1271630 : if (simplify_compare_using_ranges_1 (code, op0, op1, stmt))
1134 : {
1135 6821 : if (dump_file)
1136 : {
1137 3 : fprintf (dump_file, "Simplified relational ");
1138 3 : print_gimple_stmt (dump_file, stmt, 0);
1139 3 : fprintf (dump_file, " into ");
1140 : }
1141 :
1142 6821 : gimple_assign_set_rhs_code (stmt, code);
1143 6821 : gimple_assign_set_rhs1 (stmt, op0);
1144 6821 : gimple_assign_set_rhs2 (stmt, op1);
1145 :
1146 6821 : update_stmt (stmt);
1147 :
1148 6821 : if (dump_file)
1149 : {
1150 3 : print_gimple_stmt (dump_file, stmt, 0);
1151 3 : fprintf (dump_file, "\n");
1152 : }
1153 : happened = true;
1154 : }
1155 :
1156 : /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
1157 : if the RHS is zero or one, and the LHS are known to be boolean
1158 : values. */
1159 1271630 : if ((code == EQ_EXPR || code == NE_EXPR)
1160 707329 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
1161 1764614 : && simplify_truth_ops_using_ranges (gsi, stmt))
1162 : happened = true;
1163 :
1164 1271630 : return happened;
1165 : }
1166 :
1167 : /* Try to simplify OP0 COND_CODE OP1 using a relational operator to an
1168 : equality test if the range information indicates only one value can
1169 : satisfy the original conditional. */
1170 :
1171 : bool
1172 13672707 : simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tree &op0, tree &op1, gimple *stmt)
1173 : {
1174 13672707 : bool happened = false;
1175 13672707 : if (cond_code != NE_EXPR
1176 13672707 : && cond_code != EQ_EXPR
1177 3558376 : && TREE_CODE (op0) == SSA_NAME
1178 3557900 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
1179 16872436 : && is_gimple_min_invariant (op1))
1180 : {
1181 1938530 : int_range_max vr;
1182 :
1183 1938530 : if (!query->range_of_expr (vr, op0, stmt))
1184 0 : vr.set_undefined ();
1185 :
1186 : /* If we have range information for OP0, then we might be
1187 : able to simplify this conditional. */
1188 1938530 : if (!vr.undefined_p () && !vr.varying_p ())
1189 : {
1190 820814 : tree new_tree = test_for_singularity (cond_code, op0, op1, &vr);
1191 820814 : if (new_tree)
1192 : {
1193 138024 : cond_code = EQ_EXPR;
1194 138024 : op1 = new_tree;
1195 138024 : happened = true;
1196 : }
1197 :
1198 : /* Try again after inverting the condition. We only deal
1199 : with integral types here, so no need to worry about
1200 : issues with inverting FP comparisons. */
1201 820814 : new_tree = test_for_singularity
1202 820814 : (invert_tree_comparison (cond_code, false),
1203 : op0, op1, &vr);
1204 820814 : if (new_tree)
1205 : {
1206 180272 : cond_code = NE_EXPR;
1207 180272 : op1 = new_tree;
1208 180272 : happened = true;
1209 : }
1210 : }
1211 1938530 : }
1212 : // Try to simplify casted conditions.
1213 13672707 : if (simplify_casted_compare (cond_code, op0, op1))
1214 84238 : happened = true;
1215 13672707 : return happened;
1216 : }
1217 :
1218 : /* Simplify OP0 code OP1 when OP1 is a constant and OP0 was a SSA_NAME
1219 : defined by a type conversion. Replacing OP0 with RHS of the type conversion.
1220 : Doing so makes the conversion dead which helps subsequent passes. */
1221 :
1222 : bool
1223 13672707 : simplify_using_ranges::simplify_casted_compare (tree_code &, tree &op0, tree &op1)
1224 : {
1225 :
1226 : /* If we have a comparison of an SSA_NAME (OP0) against a constant,
1227 : see if OP0 was set by a type conversion where the source of
1228 : the conversion is another SSA_NAME with a range that fits
1229 : into the range of OP0's type.
1230 :
1231 : If so, the conversion is redundant as the earlier SSA_NAME can be
1232 : used for the comparison directly if we just massage the constant in the
1233 : comparison. */
1234 13672707 : if (TREE_CODE (op0) == SSA_NAME
1235 13581204 : && TREE_CODE (op1) == INTEGER_CST)
1236 : {
1237 9558581 : gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
1238 9558581 : tree innerop;
1239 :
1240 9558581 : if (!is_gimple_assign (def_stmt))
1241 : return false;
1242 :
1243 5885004 : switch (gimple_assign_rhs_code (def_stmt))
1244 : {
1245 501247 : CASE_CONVERT:
1246 501247 : innerop = gimple_assign_rhs1 (def_stmt);
1247 501247 : break;
1248 52155 : case VIEW_CONVERT_EXPR:
1249 52155 : innerop = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
1250 52155 : if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
1251 : return false;
1252 : break;
1253 : default:
1254 : return false;
1255 : }
1256 :
1257 550570 : if (TREE_CODE (innerop) == SSA_NAME
1258 550542 : && !POINTER_TYPE_P (TREE_TYPE (innerop))
1259 545035 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
1260 1095592 : && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
1261 : {
1262 515339 : int_range_max vr;
1263 :
1264 515339 : if (query->range_of_expr (vr, innerop)
1265 515336 : && !vr.varying_p ()
1266 172323 : && !vr.undefined_p ()
1267 172202 : && range_fits_type_p (&vr,
1268 172202 : TYPE_PRECISION (TREE_TYPE (op0)),
1269 172202 : TYPE_SIGN (TREE_TYPE (op0)))
1270 599577 : && int_fits_type_p (op1, TREE_TYPE (innerop)))
1271 : {
1272 84238 : tree newconst = fold_convert (TREE_TYPE (innerop), op1);
1273 84238 : op0 = innerop;
1274 84238 : op1 = newconst;
1275 84238 : return true;
1276 : }
1277 515339 : }
1278 : }
1279 : return false;
1280 : }
1281 :
1282 : /* Simplify a switch statement using the value range of the switch
1283 : argument. */
1284 :
1285 : bool
1286 62185 : simplify_using_ranges::simplify_switch_using_ranges (gswitch *stmt)
1287 : {
1288 62185 : tree op = gimple_switch_index (stmt);
1289 62185 : tree type = TREE_TYPE (op);
1290 62185 : int_range_max op_range (type);
1291 62185 : int_range_max default_range (type);
1292 62185 : auto_vec<unsigned> cases;
1293 62185 : cases.truncate (0);
1294 62185 : edge e;
1295 62185 : switch_update su;
1296 :
1297 : // Abort if we don't have a useful range for the switch index.
1298 62185 : if (!query->range_of_expr (op_range, op, stmt)
1299 62185 : || op_range.varying_p () || op_range.undefined_p ())
1300 : return false;
1301 :
1302 : // Default range starts with full known range of op.
1303 16088 : default_range = op_range;
1304 16088 : edge default_edge = gimple_switch_default_edge (cfun, stmt);
1305 :
1306 16088 : unsigned x, lim = gimple_switch_num_labels (stmt);
1307 96286 : for (x = 1; x < lim; x++)
1308 : {
1309 81244 : e = gimple_switch_edge (cfun, stmt, x);
1310 81244 : tree label = gimple_switch_label (stmt, x);
1311 :
1312 : // If this edge is the same as the default edge, do nothing else.
1313 81244 : if (e == default_edge)
1314 6238 : continue;
1315 : // Ada sometimes has mismatched labels and index. Just bail.
1316 81238 : if (TREE_TYPE (CASE_LOW (label)) != type)
1317 1046 : return false;
1318 :
1319 80194 : wide_int low = wi::to_wide (CASE_LOW (label));
1320 80194 : wide_int high;
1321 : // Singleton cases have no CASE_HIGH.
1322 80194 : tree tree_high = CASE_HIGH (label);
1323 80194 : if (tree_high)
1324 3393 : high = wi::to_wide (tree_high);
1325 : else
1326 76801 : high = low;
1327 :
1328 : // If the case range is fully contained in op_range, leave the
1329 : // case as it is, otherwise adjust the labels.
1330 80194 : int_range_max case_range (type, low, high);
1331 80194 : if (case_range.intersect (op_range))
1332 : {
1333 : // If none of the label is in op_range, skip this label.
1334 51167 : if (case_range.undefined_p ())
1335 6232 : continue;
1336 :
1337 : // Part of the label is in op_range, but not all of it. CASE_RANGE
1338 : // contains the part that is. Adjust the case range to
1339 : // the new min/max.
1340 44935 : if (case_range.lower_bound () != low)
1341 13 : CASE_LOW (label) = wide_int_to_tree (type,
1342 26 : case_range.lower_bound ());
1343 44935 : if (case_range.singleton_p ())
1344 43445 : CASE_HIGH (label) = NULL_TREE;
1345 : else
1346 1490 : if (case_range.upper_bound () != high)
1347 8 : CASE_HIGH (label) = wide_int_to_tree (type,
1348 16 : case_range.upper_bound ());
1349 : }
1350 : // Add case label to the keep list.
1351 73962 : cases.safe_push (x);
1352 : // Remove case_range from needing to be handled by the default.
1353 73962 : if (!case_range.invert ())
1354 2 : return false;
1355 73960 : default_range.intersect (case_range);
1356 80194 : }
1357 :
1358 : // An undefined DEFAULT range means the current default case is not needed.
1359 15042 : unsigned idx = default_range.undefined_p () ? 0 : 1;
1360 15042 : unsigned vec_size = cases.length () + idx;
1361 15042 : if (vec_size == lim)
1362 : return false;
1363 :
1364 4105 : tree vec2 = make_tree_vec (vec_size);
1365 : // Add default label if there is one.
1366 4105 : if (idx)
1367 : {
1368 3112 : TREE_VEC_ELT (vec2, 0) = gimple_switch_default_label (stmt);
1369 3112 : e = gimple_switch_edge (cfun, stmt, 0);
1370 3112 : e->aux = (void *)-1;
1371 : }
1372 :
1373 17105 : for (x = 0; x < cases.length (); x++)
1374 : {
1375 13000 : unsigned swi = cases[x];
1376 13000 : TREE_VEC_ELT (vec2, idx++) = gimple_switch_label (stmt, swi);
1377 13000 : e = gimple_switch_edge (cfun, stmt, swi);
1378 13000 : e->aux = (void *)-1;
1379 : }
1380 :
1381 : /* Queue not needed edges for later removal. */
1382 4105 : edge_iterator ei;
1383 26341 : FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1384 : {
1385 22236 : if (e->aux == (void *)-1)
1386 : {
1387 15138 : e->aux = NULL;
1388 15138 : continue;
1389 : }
1390 :
1391 7098 : if (dump_file && (dump_flags & TDF_DETAILS))
1392 : {
1393 0 : fprintf (dump_file, "removing unreachable case label\n");
1394 : }
1395 7098 : to_remove_edges.safe_push (e);
1396 7098 : set_and_propagate_unexecutable (e);
1397 7098 : e->flags &= ~EDGE_EXECUTABLE;
1398 7098 : e->flags |= EDGE_IGNORE;
1399 : }
1400 :
1401 : /* And queue an update for the stmt. */
1402 4105 : su.stmt = stmt;
1403 4105 : su.vec = vec2;
1404 4105 : to_update_switch_stmts.safe_push (su);
1405 4105 : return true;
1406 62185 : }
1407 :
1408 : void
1409 13567939 : simplify_using_ranges::cleanup_edges_and_switches (void)
1410 : {
1411 13567939 : int i;
1412 13567939 : edge e;
1413 13567939 : switch_update *su;
1414 :
1415 : /* Clear any edges marked as not executable. */
1416 13567939 : if (m_not_executable_flag)
1417 : {
1418 4605309 : FOR_EACH_VEC_ELT (m_flag_set_edges, i, e)
1419 203874 : e->flags &= ~m_not_executable_flag;
1420 : }
1421 : /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
1422 : CFG in a broken state and requires a cfg_cleanup run. */
1423 13578870 : FOR_EACH_VEC_ELT (to_remove_edges, i, e)
1424 7098 : remove_edge (e);
1425 :
1426 : /* Update SWITCH_EXPR case label vector. */
1427 13572044 : FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
1428 : {
1429 4105 : size_t j;
1430 4105 : size_t n = TREE_VEC_LENGTH (su->vec);
1431 4105 : tree label;
1432 4105 : gimple_switch_set_num_labels (su->stmt, n);
1433 24322 : for (j = 0; j < n; j++)
1434 16112 : gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
1435 : /* As we may have replaced the default label with a regular one
1436 : make sure to make it a real default label again. This ensures
1437 : optimal expansion. */
1438 4105 : label = gimple_switch_label (su->stmt, 0);
1439 4105 : CASE_LOW (label) = NULL_TREE;
1440 4105 : CASE_HIGH (label) = NULL_TREE;
1441 : }
1442 :
1443 13567939 : if (!to_remove_edges.is_empty ())
1444 : {
1445 3833 : free_dominance_info (CDI_DOMINATORS);
1446 3833 : loops_state_set (LOOPS_NEED_FIXUP);
1447 : }
1448 :
1449 13567939 : to_remove_edges.release ();
1450 13567939 : to_update_switch_stmts.release ();
1451 13567939 : }
1452 :
1453 : /* Simplify an integral conversion from an SSA name in STMT. */
1454 :
1455 : static bool
1456 4930670 : simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
1457 : {
1458 4930670 : tree innerop, middleop, finaltype;
1459 4930670 : gimple *def_stmt;
1460 4930670 : signop inner_sgn, middle_sgn, final_sgn;
1461 4930670 : unsigned inner_prec, middle_prec, final_prec;
1462 4930670 : widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
1463 :
1464 4930670 : finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
1465 4930670 : if (!INTEGRAL_TYPE_P (finaltype))
1466 : return false;
1467 4525141 : middleop = gimple_assign_rhs1 (stmt);
1468 4525141 : def_stmt = SSA_NAME_DEF_STMT (middleop);
1469 4525141 : if (!is_gimple_assign (def_stmt)
1470 4525141 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
1471 : return false;
1472 150964 : innerop = gimple_assign_rhs1 (def_stmt);
1473 150964 : if (TREE_CODE (innerop) != SSA_NAME
1474 150964 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
1475 : return false;
1476 :
1477 : /* Get the value-range of the inner operand. Use global ranges in
1478 : case innerop was created during substitute-and-fold. */
1479 147498 : wide_int imin, imax;
1480 147498 : int_range_max vr;
1481 147498 : if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
1482 : return false;
1483 245042 : get_range_query (cfun)->range_of_expr (vr, innerop, stmt);
1484 122521 : if (vr.undefined_p () || vr.varying_p ())
1485 : return false;
1486 68369 : innermin = widest_int::from (vr.lower_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
1487 68369 : innermax = widest_int::from (vr.upper_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
1488 :
1489 : /* Simulate the conversion chain to check if the result is equal if
1490 : the middle conversion is removed. */
1491 68369 : inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
1492 68369 : middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
1493 68369 : final_prec = TYPE_PRECISION (finaltype);
1494 :
1495 : /* If the first conversion is not injective, the second must not
1496 : be widening. */
1497 68369 : if (wi::gtu_p (innermax - innermin,
1498 136738 : wi::mask <widest_int> (middle_prec, false))
1499 68369 : && middle_prec < final_prec)
1500 : return false;
1501 : /* We also want a medium value so that we can track the effect that
1502 : narrowing conversions with sign change have. */
1503 56781 : inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
1504 56781 : if (inner_sgn == UNSIGNED)
1505 41745 : innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
1506 : else
1507 15036 : innermed = 0;
1508 56781 : if (wi::cmp (innermin, innermed, inner_sgn) >= 0
1509 56781 : || wi::cmp (innermed, innermax, inner_sgn) >= 0)
1510 40784 : innermed = innermin;
1511 :
1512 56781 : middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
1513 56781 : middlemin = wi::ext (innermin, middle_prec, middle_sgn);
1514 56781 : middlemed = wi::ext (innermed, middle_prec, middle_sgn);
1515 56781 : middlemax = wi::ext (innermax, middle_prec, middle_sgn);
1516 :
1517 : /* Require that the final conversion applied to both the original
1518 : and the intermediate range produces the same result. */
1519 56781 : final_sgn = TYPE_SIGN (finaltype);
1520 113562 : if (wi::ext (middlemin, final_prec, final_sgn)
1521 113562 : != wi::ext (innermin, final_prec, final_sgn)
1522 54287 : || wi::ext (middlemed, final_prec, final_sgn)
1523 219642 : != wi::ext (innermed, final_prec, final_sgn)
1524 102488 : || wi::ext (middlemax, final_prec, final_sgn)
1525 193902 : != wi::ext (innermax, final_prec, final_sgn))
1526 : return false;
1527 :
1528 44358 : gimple_assign_set_rhs1 (stmt, innerop);
1529 44358 : fold_stmt (gsi, follow_single_use_edges);
1530 44358 : return true;
1531 5078168 : }
1532 :
1533 : /* Simplify a conversion from integral SSA name to float in STMT. */
1534 :
1535 : bool
1536 256820 : simplify_using_ranges::simplify_float_conversion_using_ranges
1537 : (gimple_stmt_iterator *gsi,
1538 : gimple *stmt)
1539 : {
1540 256820 : tree rhs1 = gimple_assign_rhs1 (stmt);
1541 256820 : int_range_max vr;
1542 256820 : scalar_float_mode fltmode
1543 256820 : = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
1544 256820 : scalar_int_mode mode;
1545 256820 : tree tem;
1546 256820 : gassign *conv;
1547 :
1548 : /* We can only handle constant ranges. */
1549 256820 : if (!query->range_of_expr (vr, rhs1, stmt)
1550 256820 : || vr.varying_p ()
1551 358690 : || vr.undefined_p ())
1552 : return false;
1553 :
1554 : /* The code below doesn't work for large/huge _BitInt, nor is really
1555 : needed for those, bitint lowering does use ranges already. */
1556 202365 : if (BITINT_TYPE_P (TREE_TYPE (rhs1))
1557 101189 : && TYPE_MODE (TREE_TYPE (rhs1)) == BLKmode)
1558 : return false;
1559 : /* First check if we can use a signed type in place of an unsigned. */
1560 101176 : scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
1561 101176 : if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
1562 5461 : && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
1563 104903 : && range_fits_type_p (&vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
1564 : mode = rhs_mode;
1565 : /* If we can do the conversion in the current input mode do nothing. */
1566 99551 : else if (can_float_p (fltmode, rhs_mode,
1567 99551 : TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
1568 : return false;
1569 : /* Otherwise search for a mode we can use, starting from the narrowest
1570 : integer mode available. */
1571 : else
1572 : {
1573 4425 : mode = NARROWEST_INT_MODE;
1574 15889 : for (;;)
1575 : {
1576 : /* If we cannot do a signed conversion to float from mode
1577 : or if the value-range does not fit in the signed type
1578 : try with a wider mode. */
1579 15889 : if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
1580 15889 : && range_fits_type_p (&vr, GET_MODE_PRECISION (mode), SIGNED))
1581 : break;
1582 :
1583 : /* But do not widen the input. Instead leave that to the
1584 : optabs expansion code. */
1585 31512 : if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
1586 15756 : || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
1587 : return false;
1588 : }
1589 : }
1590 :
1591 : /* It works, insert a truncation or sign-change before the
1592 : float conversion. */
1593 1758 : tem = make_ssa_name (build_nonstandard_integer_type
1594 1758 : (GET_MODE_PRECISION (mode), 0));
1595 1758 : conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
1596 1758 : gsi_insert_before (gsi, conv, GSI_SAME_STMT);
1597 1758 : gimple_assign_set_rhs1 (stmt, tem);
1598 1758 : fold_stmt (gsi, follow_single_use_edges);
1599 :
1600 1758 : return true;
1601 256820 : }
1602 :
1603 : /* Simplify an internal fn call using ranges if possible. */
1604 :
1605 : bool
1606 609485 : simplify_using_ranges::simplify_internal_call_using_ranges
1607 : (gimple_stmt_iterator *gsi,
1608 : gimple *stmt)
1609 : {
1610 609485 : enum tree_code subcode;
1611 609485 : bool is_ubsan = false;
1612 609485 : bool ovf = false;
1613 609485 : switch (gimple_call_internal_fn (stmt))
1614 : {
1615 : case IFN_UBSAN_CHECK_ADD:
1616 : subcode = PLUS_EXPR;
1617 : is_ubsan = true;
1618 : break;
1619 2572 : case IFN_UBSAN_CHECK_SUB:
1620 2572 : subcode = MINUS_EXPR;
1621 2572 : is_ubsan = true;
1622 2572 : break;
1623 2138 : case IFN_UBSAN_CHECK_MUL:
1624 2138 : subcode = MULT_EXPR;
1625 2138 : is_ubsan = true;
1626 2138 : break;
1627 40977 : case IFN_ADD_OVERFLOW:
1628 40977 : subcode = PLUS_EXPR;
1629 40977 : break;
1630 49746 : case IFN_SUB_OVERFLOW:
1631 49746 : subcode = MINUS_EXPR;
1632 49746 : break;
1633 47131 : case IFN_MUL_OVERFLOW:
1634 47131 : subcode = MULT_EXPR;
1635 47131 : break;
1636 : default:
1637 : return false;
1638 : }
1639 :
1640 145123 : tree op0 = gimple_call_arg (stmt, 0);
1641 145123 : tree op1 = gimple_call_arg (stmt, 1);
1642 145123 : tree type;
1643 145123 : if (is_ubsan)
1644 : {
1645 7269 : type = TREE_TYPE (op0);
1646 7269 : if (VECTOR_TYPE_P (type))
1647 : return false;
1648 : }
1649 137854 : else if (gimple_call_lhs (stmt) == NULL_TREE)
1650 : return false;
1651 : else
1652 137854 : type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
1653 144197 : if (!check_for_binary_op_overflow (query, subcode, type, op0, op1, &ovf, stmt)
1654 144197 : || (is_ubsan && ovf))
1655 : return false;
1656 :
1657 3496 : gimple *g;
1658 3496 : location_t loc = gimple_location (stmt);
1659 3496 : if (is_ubsan)
1660 529 : g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
1661 : else
1662 : {
1663 2967 : tree utype = type;
1664 2967 : if (ovf
1665 1478 : || !useless_type_conversion_p (type, TREE_TYPE (op0))
1666 3704 : || !useless_type_conversion_p (type, TREE_TYPE (op1)))
1667 2519 : utype = unsigned_type_for (type);
1668 2967 : if (TREE_CODE (op0) == INTEGER_CST)
1669 1175 : op0 = fold_convert (utype, op0);
1670 1792 : else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
1671 : {
1672 751 : g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
1673 751 : gimple_set_location (g, loc);
1674 751 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1675 751 : op0 = gimple_assign_lhs (g);
1676 : }
1677 2967 : if (TREE_CODE (op1) == INTEGER_CST)
1678 1720 : op1 = fold_convert (utype, op1);
1679 1247 : else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
1680 : {
1681 27 : g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
1682 27 : gimple_set_location (g, loc);
1683 27 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1684 27 : op1 = gimple_assign_lhs (g);
1685 : }
1686 2967 : g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
1687 2967 : gimple_set_location (g, loc);
1688 2967 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1689 2967 : if (utype != type)
1690 : {
1691 1361 : g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
1692 : gimple_assign_lhs (g));
1693 1361 : gimple_set_location (g, loc);
1694 1361 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1695 : }
1696 2967 : g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
1697 : gimple_assign_lhs (g),
1698 2967 : build_int_cst (type, ovf));
1699 : }
1700 3496 : gimple_set_location (g, loc);
1701 3496 : gsi_replace (gsi, g, false);
1702 3496 : return true;
1703 : }
1704 :
1705 : /* Return true if VAR is a two-valued variable. Set a and b with the
1706 : two-values when it is true. Return false otherwise. */
1707 :
1708 : bool
1709 489065 : simplify_using_ranges::two_valued_val_range_p (tree var, tree *a, tree *b,
1710 : gimple *s)
1711 : {
1712 489065 : int_range_max vr;
1713 489065 : if (!query->range_of_expr (vr, var, s))
1714 : return false;
1715 489065 : if (vr.varying_p () || vr.undefined_p ())
1716 : return false;
1717 :
1718 803463 : if ((vr.num_pairs () == 1 && vr.upper_bound () - vr.lower_bound () == 1)
1719 416868 : || (vr.num_pairs () == 2
1720 289469 : && vr.lower_bound (0) == vr.upper_bound (0)
1721 245064 : && vr.lower_bound (1) == vr.upper_bound (1)))
1722 : {
1723 7643 : *a = wide_int_to_tree (TREE_TYPE (var), vr.lower_bound ());
1724 7643 : *b = wide_int_to_tree (TREE_TYPE (var), vr.upper_bound ());
1725 7643 : return true;
1726 : }
1727 : return false;
1728 489065 : }
1729 :
1730 13567939 : simplify_using_ranges::simplify_using_ranges (range_query *query,
1731 : int not_executable_flag)
1732 13567939 : : query (query)
1733 : {
1734 13567939 : to_remove_edges = vNULL;
1735 13567939 : to_update_switch_stmts = vNULL;
1736 13567939 : m_not_executable_flag = not_executable_flag;
1737 13567939 : m_flag_set_edges = vNULL;
1738 13567939 : }
1739 :
1740 13567939 : simplify_using_ranges::~simplify_using_ranges ()
1741 : {
1742 13567939 : cleanup_edges_and_switches ();
1743 13567939 : m_flag_set_edges.release ();
1744 13567939 : }
1745 :
1746 : /* Simplify STMT using ranges if possible. */
1747 :
1748 : bool
1749 259327750 : simplify_using_ranges::simplify (gimple_stmt_iterator *gsi)
1750 : {
1751 259327750 : gcc_checking_assert (query);
1752 :
1753 259327750 : gimple *stmt = gsi_stmt (*gsi);
1754 259327750 : if (is_gimple_assign (stmt))
1755 : {
1756 69584729 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
1757 69584729 : tree rhs1 = gimple_assign_rhs1 (stmt);
1758 69584729 : tree rhs2 = gimple_assign_rhs2 (stmt);
1759 69584729 : tree lhs = gimple_assign_lhs (stmt);
1760 69584729 : tree val1 = NULL_TREE, val2 = NULL_TREE;
1761 69584729 : use_operand_p use_p;
1762 69584729 : gimple *use_stmt;
1763 :
1764 : /* Convert:
1765 : LHS = CST BINOP VAR
1766 : Where VAR is two-valued and LHS is used in GIMPLE_COND only
1767 : To:
1768 : LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
1769 :
1770 : Also handles:
1771 : LHS = VAR BINOP CST
1772 : Where VAR is two-valued and LHS is used in GIMPLE_COND only
1773 : To:
1774 : LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
1775 :
1776 69584729 : if (TREE_CODE_CLASS (rhs_code) == tcc_binary
1777 14797601 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1778 10547789 : && ((TREE_CODE (rhs1) == INTEGER_CST
1779 180423 : && TREE_CODE (rhs2) == SSA_NAME)
1780 10367964 : || (TREE_CODE (rhs2) == INTEGER_CST
1781 6918153 : && TREE_CODE (rhs1) == SSA_NAME))
1782 7097380 : && single_imm_use (lhs, &use_p, &use_stmt)
1783 75039784 : && gimple_code (use_stmt) == GIMPLE_COND)
1784 :
1785 : {
1786 489065 : tree new_rhs1 = NULL_TREE;
1787 489065 : tree new_rhs2 = NULL_TREE;
1788 489065 : tree cmp_var = NULL_TREE;
1789 :
1790 489065 : if (TREE_CODE (rhs2) == SSA_NAME
1791 489065 : && two_valued_val_range_p (rhs2, &val1, &val2, stmt))
1792 : {
1793 : /* Optimize RHS1 OP [VAL1, VAL2]. */
1794 241 : new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
1795 241 : new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
1796 241 : cmp_var = rhs2;
1797 : }
1798 488824 : else if (TREE_CODE (rhs1) == SSA_NAME
1799 488824 : && two_valued_val_range_p (rhs1, &val1, &val2, stmt))
1800 : {
1801 : /* Optimize [VAL1, VAL2] OP RHS2. */
1802 7402 : new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
1803 7402 : new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
1804 7402 : cmp_var = rhs1;
1805 : }
1806 :
1807 : /* If we could not find two-vals or the optimization is invalid as
1808 : in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
1809 489065 : if (new_rhs1 && new_rhs2)
1810 : {
1811 7643 : tree cond = gimple_build (gsi, true, GSI_SAME_STMT,
1812 : UNKNOWN_LOCATION,
1813 : EQ_EXPR, boolean_type_node,
1814 : cmp_var, val1);
1815 7643 : gimple_assign_set_rhs_with_ops (gsi,
1816 : COND_EXPR, cond,
1817 : new_rhs1,
1818 : new_rhs2);
1819 7643 : update_stmt (gsi_stmt (*gsi));
1820 7643 : fold_stmt (gsi, follow_single_use_edges);
1821 8461387 : return true;
1822 : }
1823 : }
1824 :
1825 69577086 : if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
1826 1271630 : return simplify_compare_assign_using_ranges_1 (gsi, stmt);
1827 :
1828 68305456 : switch (rhs_code)
1829 : {
1830 :
1831 : /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
1832 : and BIT_AND_EXPR respectively if the first operand is greater
1833 : than zero and the second operand is an exact power of two.
1834 : Also optimize TRUNC_MOD_EXPR away if the second operand is
1835 : constant and the first operand already has the right value
1836 : range. */
1837 337830 : case TRUNC_DIV_EXPR:
1838 337830 : case TRUNC_MOD_EXPR:
1839 337830 : if ((TREE_CODE (rhs1) == SSA_NAME
1840 337830 : || TREE_CODE (rhs1) == INTEGER_CST)
1841 337830 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1842 336439 : return simplify_div_or_mod_using_ranges (gsi, stmt);
1843 : break;
1844 :
1845 : /* Transform ABS (X) into X or -X as appropriate. */
1846 64820 : case ABS_EXPR:
1847 64820 : if (TREE_CODE (rhs1) == SSA_NAME
1848 64820 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1849 9457 : return simplify_abs_using_ranges (gsi, stmt);
1850 : break;
1851 :
1852 1324876 : case BIT_AND_EXPR:
1853 1324876 : case BIT_IOR_EXPR:
1854 : /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR if all the bits
1855 : being cleared are already cleared or all the bits being set
1856 : are already set. Beware that boolean types must be handled
1857 : logically (see range-op.cc) unless they have precision 1. */
1858 2562885 : if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1859 2524642 : && (TREE_CODE (TREE_TYPE (rhs1)) != BOOLEAN_TYPE
1860 320961 : || TYPE_PRECISION (TREE_TYPE (rhs1)) == 1))
1861 1286633 : return simplify_bit_ops_using_ranges (gsi, stmt);
1862 : break;
1863 :
1864 6052325 : CASE_CONVERT:
1865 6052325 : if (TREE_CODE (rhs1) == SSA_NAME
1866 6052325 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1867 4930670 : return simplify_conversion_using_ranges (gsi, stmt);
1868 : break;
1869 :
1870 259231 : case FLOAT_EXPR:
1871 259231 : if (TREE_CODE (rhs1) == SSA_NAME
1872 259231 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1873 256820 : return simplify_float_conversion_using_ranges (gsi, stmt);
1874 : break;
1875 :
1876 249259 : case MIN_EXPR:
1877 249259 : case MAX_EXPR:
1878 249259 : return simplify_min_or_max_using_ranges (gsi, stmt);
1879 :
1880 418962 : case RSHIFT_EXPR:
1881 418962 : {
1882 418962 : tree op0 = gimple_assign_rhs1 (stmt);
1883 418962 : tree type = TREE_TYPE (op0);
1884 418962 : int_range_max range;
1885 418962 : if (TYPE_SIGN (type) == SIGNED
1886 418962 : && query->range_of_expr (range, op0, stmt))
1887 : {
1888 112836 : unsigned prec = TYPE_PRECISION (TREE_TYPE (op0));
1889 225672 : int_range<2> nzm1 (type, wi::minus_one (prec), wi::zero (prec),
1890 112836 : VR_ANTI_RANGE);
1891 112836 : range.intersect (nzm1);
1892 : // If there are no ranges other than [-1, 0] remove the shift.
1893 112836 : if (range.undefined_p ())
1894 : {
1895 86 : gimple_assign_set_rhs_from_tree (gsi, op0);
1896 86 : return true;
1897 : }
1898 : return false;
1899 112836 : }
1900 306126 : break;
1901 418962 : }
1902 : default:
1903 : break;
1904 : }
1905 : }
1906 189743021 : else if (gimple_code (stmt) == GIMPLE_COND)
1907 12532739 : return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
1908 177210282 : else if (gimple_code (stmt) == GIMPLE_SWITCH)
1909 62185 : return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
1910 177148097 : else if (is_gimple_call (stmt)
1911 177148097 : && gimple_call_internal_p (stmt))
1912 609485 : return simplify_internal_call_using_ranges (gsi, stmt);
1913 :
1914 : return false;
1915 : }
|