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 520521 : simplify_using_ranges::op_with_boolean_value_range_p (tree op, gimple *s)
59 : {
60 520521 : if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
61 : return true;
62 :
63 507641 : if (integer_zerop (op)
64 507641 : || integer_onep (op))
65 : return true;
66 :
67 497742 : 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 497742 : int_range_max vr;
73 497742 : return (query->range_of_expr (vr, op, s)
74 497742 : && vr == range_true_and_false (TREE_TYPE (op)));
75 497742 : }
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 144993 : 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 144993 : relation_kind rel = VREL_VARYING;
89 : /* For subtraction see if relations could simplify it. */
90 144993 : if (s
91 144993 : && subcode == MINUS_EXPR
92 144993 : && types_compatible_p (TREE_TYPE (op0), TREE_TYPE (op1)))
93 : {
94 29151 : 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 29151 : if (rel == VREL_EQ)
99 : return true;
100 : }
101 :
102 144992 : int_range_max vr0, vr1;
103 144992 : if (!query->range_of_expr (vr0, op0, s) || vr0.undefined_p ())
104 12 : vr0.set_varying (TREE_TYPE (op0));
105 144992 : if (!query->range_of_expr (vr1, op1, s) || vr1.undefined_p ())
106 8 : vr1.set_varying (TREE_TYPE (op1));
107 :
108 144992 : tree vr0min = wide_int_to_tree (TREE_TYPE (op0), vr0.lower_bound ());
109 144992 : tree vr0max = wide_int_to_tree (TREE_TYPE (op0), vr0.upper_bound ());
110 144992 : tree vr1min = wide_int_to_tree (TREE_TYPE (op1), vr1.lower_bound ());
111 144992 : 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 144992 : if ((rel == VREL_GT || rel == VREL_GE)
117 28 : && tree_int_cst_sgn (vr1min) >= 0
118 145014 : && !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 144970 : if (rel == VREL_LT
125 1 : && tree_int_cst_sgn (vr1min) >= 0
126 144971 : && TYPE_UNSIGNED (type))
127 : {
128 1 : *ovf = true;
129 1 : return true;
130 : }
131 :
132 237970 : *ovf = arith_overflowed_p (subcode, type, vr0min,
133 : subcode == MINUS_EXPR ? vr1max : vr1min);
134 237970 : if (arith_overflowed_p (subcode, type, vr0max,
135 144969 : subcode == MINUS_EXPR ? vr1min : vr1max) != *ovf)
136 : return false;
137 39762 : if (subcode == MULT_EXPR)
138 : {
139 24029 : if (arith_overflowed_p (subcode, type, vr0min, vr1max) != *ovf
140 24029 : || arith_overflowed_p (subcode, type, vr0max, vr1min) != *ovf)
141 : return false;
142 : }
143 39386 : 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 37400 : widest2_int wmin, wmax;
151 336792 : widest2_int w[4];
152 37400 : int i;
153 37400 : signop sign0 = TYPE_SIGN (TREE_TYPE (op0));
154 37400 : signop sign1 = TYPE_SIGN (TREE_TYPE (op1));
155 37451 : w[0] = widest2_int::from (vr0.lower_bound (), sign0);
156 37459 : w[1] = widest2_int::from (vr0.upper_bound (), sign0);
157 37437 : w[2] = widest2_int::from (vr1.lower_bound (), sign1);
158 37445 : w[3] = widest2_int::from (vr1.upper_bound (), sign1);
159 187000 : for (i = 0; i < 4; i++)
160 : {
161 149600 : widest2_int wt;
162 149600 : switch (subcode)
163 : {
164 27520 : case PLUS_EXPR:
165 27520 : wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
166 27520 : break;
167 28212 : case MINUS_EXPR:
168 28212 : wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
169 28212 : break;
170 93868 : case MULT_EXPR:
171 93868 : wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
172 93868 : break;
173 0 : default:
174 0 : gcc_unreachable ();
175 : }
176 149600 : if (i == 0)
177 : {
178 37400 : wmin = wt;
179 37400 : wmax = wt;
180 : }
181 : else
182 : {
183 112200 : wmin = wi::smin (wmin, wt);
184 113050 : wmax = wi::smax (wmax, wt);
185 : }
186 149600 : }
187 : /* The result of op0 CODE op1 is known to be in range
188 : [wmin, wmax]. */
189 37400 : widest2_int wtmin
190 74800 : = widest2_int::from (irange_val_min (type), TYPE_SIGN (type));
191 37400 : widest2_int wtmax
192 74800 : = 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 73790 : if (wmax < wtmin || wmin > wtmax)
197 1661 : return true;
198 : return false;
199 224680 : }
200 : return true;
201 144992 : }
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 6207464 : get_scev_info (vrange &r, tree name, gimple *stmt, class loop *l,
209 : tree &init, tree &step, enum ev_direction &dir)
210 : {
211 6207464 : tree ev = analyze_scalar_evolution (l, name);
212 6207464 : tree chrec = instantiate_parameters (l, ev);
213 6207464 : tree type = TREE_TYPE (name);
214 6207464 : if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
215 : {
216 2920626 : r.set_varying (type);
217 2920626 : return false;
218 : }
219 3286838 : 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 3286838 : init = initial_condition_in_loop_num (chrec, l->num);
229 3286838 : step = evolution_part_in_loop_num (chrec, l->num);
230 3286838 : if (!init || !step)
231 : {
232 0 : r.set_varying (type);
233 0 : return false;
234 : }
235 3286838 : dir = scev_direction (chrec);
236 3286838 : if (dir == EV_DIR_UNKNOWN
237 3286838 : || scev_probably_wraps_p (NULL, init, step, stmt,
238 : get_chrec_loop (chrec), true))
239 : {
240 562210 : r.set_varying (type);
241 562210 : 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 2285188 : induction_variable_may_overflow_p (tree type,
250 : const wide_int &step, const widest_int &nit)
251 : {
252 2285188 : wi::overflow_type ovf;
253 2285188 : signop sign = TYPE_SIGN (type);
254 2285188 : widest_int max_step = wi::mul (widest_int::from (step, sign),
255 2285188 : nit, sign, &ovf);
256 :
257 2285188 : 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 2160704 : return (sign == SIGNED
264 4320854 : && wi::gts_p (max_step, 0) != wi::gts_p (step, 0));
265 2285188 : }
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 2723748 : range_from_loop_direction (irange &r, tree type,
272 : const irange &begin, const irange &end,
273 : ev_direction dir)
274 : {
275 2723748 : signop sign = TYPE_SIGN (type);
276 :
277 2723748 : if (begin.undefined_p () || end.undefined_p ())
278 3527 : r.set_varying (type);
279 2720221 : else if (dir == EV_DIR_GROWS)
280 : {
281 2437885 : if (wi::ge_p (begin.lower_bound (), end.upper_bound (), sign))
282 230 : r.set_varying (type);
283 : else
284 2437655 : r = int_range<1> (type, begin.lower_bound (), end.upper_bound ());
285 : }
286 : else
287 : {
288 282354 : if (wi::ge_p (end.lower_bound (), begin.upper_bound (), sign))
289 210 : r.set_varying (type);
290 : else
291 282144 : r = int_range<1> (type, end.lower_bound (), begin.upper_bound ());
292 : }
293 2723748 : }
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 6207464 : range_of_var_in_loop (vrange &v, tree name, class loop *l, gimple *stmt,
300 : range_query *query)
301 : {
302 6207464 : tree init, step;
303 6207464 : enum ev_direction dir;
304 6207464 : if (!get_scev_info (v, name, stmt, l, init, step, dir))
305 : return true;
306 :
307 : // Calculate ranges for the values from SCEV.
308 2724628 : irange &r = as_a <irange> (v);
309 2724628 : tree type = TREE_TYPE (init);
310 2724628 : int_range<2> rinit (type), rstep (type), max_init (type);
311 2724628 : if (!query->range_of_expr (rinit, init, stmt)
312 2724628 : || !query->range_of_expr (rstep, step, stmt))
313 : return false;
314 :
315 : // Calculate the final range of NAME if possible.
316 2724628 : if (rinit.singleton_p () && rstep.singleton_p ())
317 : {
318 2286068 : widest_int nit;
319 2286068 : if (!max_loop_iterations (l, &nit))
320 : return false;
321 :
322 2285189 : if (!induction_variable_may_overflow_p (type, rstep.lower_bound (), nit))
323 : {
324 : // Calculate the max bounds for init (init + niter * step).
325 2160150 : wide_int w = wide_int::from (nit, TYPE_PRECISION (type), TYPE_SIGN (type));
326 2160150 : int_range<1> niter (type, w, w);
327 2160150 : int_range_max max_step;
328 2160150 : range_op_handler mult_handler (MULT_EXPR);
329 2160150 : range_op_handler plus_handler (PLUS_EXPR);
330 2160150 : if (!mult_handler.fold_range (max_step, type, niter, rstep)
331 2160150 : || !plus_handler.fold_range (max_init, type, rinit, max_step))
332 0 : return false;
333 2160151 : }
334 2286068 : }
335 2723748 : range_from_loop_direction (r, type, rinit, max_init, dir);
336 2723748 : return true;
337 2724628 : }
338 :
339 : /* Helper function for vrp_evaluate_conditional_warnv & other
340 : optimizers. */
341 :
342 : tree
343 21023930 : simplify_using_ranges::fold_cond_with_ops (enum tree_code code,
344 : tree op0, tree op1, gimple *s)
345 : {
346 21023930 : value_range r0 (TREE_TYPE (op0));
347 21023930 : value_range r1 (TREE_TYPE (op1));
348 21023930 : if (!query->range_of_expr (r0, op0, s)
349 21023930 : || !query->range_of_expr (r1, op1, s))
350 : return NULL_TREE;
351 :
352 21016286 : int_range<1> res;
353 21016286 : range_op_handler handler (code);
354 :
355 : // Find any relation between op0 and op1 and pass it to fold_range.
356 21016286 : relation_kind rel = VREL_VARYING;
357 21016286 : if (gimple_range_ssa_p (op0) && gimple_range_ssa_p (op1))
358 4667837 : rel = query->relation ().query (s, op0, op1);
359 :
360 21016286 : if (handler && handler.fold_range (res, boolean_type_node, r0, r1,
361 : relation_trio::op1_op2 (rel)))
362 : {
363 21016286 : if (res == range_true ())
364 8882 : return boolean_true_node;
365 21007404 : if (res == range_false ())
366 3528 : return boolean_false_node;
367 : }
368 : return NULL;
369 21023930 : }
370 :
371 : /* Helper function for legacy_fold_cond. */
372 :
373 : tree
374 21321359 : simplify_using_ranges::legacy_fold_cond_overflow (gimple *stmt)
375 : {
376 21321359 : tree ret;
377 21321359 : tree_code code = gimple_cond_code (stmt);
378 21321359 : tree op0 = gimple_cond_lhs (stmt);
379 21321359 : tree op1 = gimple_cond_rhs (stmt);
380 :
381 : /* We only deal with integral and pointer types. */
382 42289216 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
383 26031118 : && !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 20452512 : tree x;
395 20452512 : if (overflow_comparison_p (code, op0, op1, &x))
396 : {
397 1236 : 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 1236 : if (integer_zerop (x))
403 : {
404 187 : op1 = x;
405 187 : 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 1049 : else if (wi::to_wide (x) == max - 1)
412 : {
413 706 : op0 = op1;
414 706 : op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
415 706 : code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
416 : }
417 : else
418 : {
419 343 : int_range_max vro, vri;
420 343 : tree type = TREE_TYPE (op0);
421 343 : if (code == GT_EXPR || code == GE_EXPR)
422 : {
423 93 : vro.set (type,
424 186 : wi::to_wide (TYPE_MIN_VALUE (type)),
425 93 : wi::to_wide (x), VR_ANTI_RANGE);
426 93 : vri.set (type,
427 186 : wi::to_wide (TYPE_MIN_VALUE (type)),
428 186 : 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 343 : int_range_max vr0;
443 343 : 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 343 : vro.intersect (vr0);
456 343 : if (vro.undefined_p ())
457 8 : return boolean_false_node;
458 335 : vri.intersect (vr0);
459 335 : if (vri.undefined_p ())
460 1 : return boolean_true_node;
461 343 : }
462 1236 : }
463 :
464 20452503 : 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 21321359 : simplify_using_ranges::legacy_fold_cond (gcond *stmt, edge *taken_edge_p)
475 : {
476 21321359 : tree val;
477 :
478 21321359 : *taken_edge_p = NULL;
479 :
480 21321359 : if (dump_file && (dump_flags & TDF_DETAILS))
481 : {
482 390 : tree use;
483 390 : ssa_op_iter i;
484 :
485 390 : fprintf (dump_file, "\nVisiting conditional with predicate: ");
486 390 : print_gimple_stmt (dump_file, stmt, 0);
487 390 : fprintf (dump_file, "\nWith known ranges\n");
488 :
489 869 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
490 : {
491 479 : fprintf (dump_file, "\t");
492 479 : print_generic_expr (dump_file, use);
493 479 : fprintf (dump_file, ": ");
494 479 : value_range r (TREE_TYPE (use));
495 479 : query->range_of_expr (r, use, stmt);
496 479 : r.dump (dump_file);
497 479 : }
498 :
499 390 : fprintf (dump_file, "\n");
500 : }
501 :
502 21321359 : val = legacy_fold_cond_overflow (stmt);
503 21321359 : if (val)
504 35 : *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
505 :
506 21321359 : if (dump_file && (dump_flags & TDF_DETAILS))
507 : {
508 390 : fprintf (dump_file, "\nPredicate evaluates to: ");
509 390 : if (val == NULL_TREE)
510 390 : fprintf (dump_file, "DON'T KNOW\n");
511 : else
512 0 : print_generic_stmt (dump_file, val);
513 : }
514 21321359 : }
515 :
516 : /* Simplify boolean operations if the source is known
517 : to be already a boolean. */
518 : bool
519 503594 : simplify_using_ranges::simplify_truth_ops_using_ranges
520 : (gimple_stmt_iterator *gsi,
521 : gassign *stmt)
522 : {
523 503594 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
524 503594 : tree lhs, op0, op1;
525 503594 : bool need_conversion;
526 :
527 : /* We handle only !=/== case here. */
528 503594 : gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
529 :
530 503594 : op0 = gimple_assign_rhs1 (stmt);
531 503594 : if (!op_with_boolean_value_range_p (op0, stmt))
532 : return false;
533 :
534 16927 : op1 = gimple_assign_rhs2 (stmt);
535 16927 : 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 16413 : if (rhs_code == EQ_EXPR)
541 : {
542 8187 : if (TREE_CODE (op1) == INTEGER_CST)
543 3443 : op1 = int_const_binop (BIT_XOR_EXPR, op1,
544 6886 : build_int_cst (TREE_TYPE (op1), 1));
545 : else
546 : return false;
547 : }
548 :
549 11669 : lhs = gimple_assign_lhs (stmt);
550 11669 : need_conversion
551 11669 : = !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 11669 : if (need_conversion
555 10281 : && !TYPE_UNSIGNED (TREE_TYPE (op0))
556 3861 : && TYPE_PRECISION (TREE_TYPE (op0)) == 1
557 11691 : && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
558 : return false;
559 :
560 : /* For A != 0 we can substitute A itself. */
561 11669 : if (integer_zerop (op1))
562 7036 : 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 4633 : else if (need_conversion)
567 : {
568 3245 : tree tem = make_ssa_name (TREE_TYPE (op0));
569 3245 : gassign *newop
570 3245 : = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
571 3245 : gsi_insert_before (gsi, newop, GSI_SAME_STMT);
572 6464 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
573 6464 : && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
574 : {
575 6316 : int_range<1> vr (TREE_TYPE (tem),
576 6316 : wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
577 6316 : wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
578 3158 : set_range_info (tem, vr);
579 3158 : }
580 3245 : 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 11669 : update_stmt (gsi_stmt (*gsi));
586 11669 : fold_stmt (gsi, follow_single_use_edges);
587 :
588 11669 : 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 356563 : simplify_using_ranges::simplify_div_or_mod_using_ranges
601 : (gimple_stmt_iterator *gsi,
602 : gassign *stmt)
603 : {
604 356563 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
605 356563 : tree val = NULL;
606 356563 : tree op0 = gimple_assign_rhs1 (stmt);
607 356563 : tree op1 = gimple_assign_rhs2 (stmt);
608 356563 : tree op0min = NULL_TREE, op0max = NULL_TREE;
609 356563 : tree op1min = op1;
610 356563 : int_range_max vr;
611 :
612 356563 : if (TREE_CODE (op0) == INTEGER_CST)
613 : {
614 : op0min = op0;
615 : op0max = op0;
616 : }
617 : else
618 : {
619 327465 : if (!query->range_of_expr (vr, op0, stmt))
620 0 : vr.set_varying (TREE_TYPE (op0));
621 327465 : if (!vr.varying_p () && !vr.undefined_p ())
622 : {
623 162641 : tree type = vr.type ();
624 162641 : op0min = wide_int_to_tree (type, vr.lower_bound ());
625 162650 : op0max = wide_int_to_tree (type, vr.upper_bound ());
626 : }
627 : }
628 :
629 356563 : if (rhs_code == TRUNC_MOD_EXPR
630 167733 : && TREE_CODE (op1) == SSA_NAME)
631 : {
632 109953 : int_range_max vr1;
633 109953 : if (!query->range_of_expr (vr1, op1, stmt))
634 0 : vr1.set_varying (TREE_TYPE (op1));
635 109953 : if (!vr1.varying_p () && !vr1.undefined_p ())
636 27330 : op1min = wide_int_to_tree (vr1.type (), vr1.lower_bound ());
637 109953 : }
638 167733 : if (rhs_code == TRUNC_MOD_EXPR
639 167733 : && TREE_CODE (op1min) == INTEGER_CST
640 85106 : && tree_int_cst_sgn (op1min) == 1
641 63355 : && op0max
642 36172 : && tree_int_cst_lt (op0max, op1min))
643 : {
644 1283 : if (TYPE_UNSIGNED (TREE_TYPE (op0))
645 546 : || tree_int_cst_sgn (op0min) >= 0
646 1526 : || 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 1231 : gimple_assign_set_rhs_from_tree (gsi, op0);
652 1231 : return true;
653 : }
654 : }
655 :
656 355332 : if (TREE_CODE (op0) != SSA_NAME)
657 : return false;
658 :
659 326234 : 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 285542 : if (rhs_code == TRUNC_MOD_EXPR
666 285542 : && fold_stmt (gsi, follow_single_use_edges))
667 20 : return true;
668 : return false;
669 : }
670 :
671 40692 : if (TYPE_UNSIGNED (TREE_TYPE (op0)))
672 0 : val = integer_one_node;
673 : else
674 : {
675 40692 : tree zero = build_zero_cst (TREE_TYPE (op0));
676 40692 : val = fold_cond_with_ops (GE_EXPR, op0, zero, stmt);
677 : }
678 :
679 40692 : if (val && integer_onep (val))
680 : {
681 6680 : tree t;
682 :
683 6680 : if (rhs_code == TRUNC_DIV_EXPR)
684 : {
685 4378 : t = build_int_cst (integer_type_node, tree_log2 (op1));
686 4378 : gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
687 4378 : gimple_assign_set_rhs1 (stmt, op0);
688 4378 : gimple_assign_set_rhs2 (stmt, t);
689 : }
690 : else
691 : {
692 2302 : t = build_int_cst (TREE_TYPE (op1), 1);
693 2302 : t = int_const_binop (MINUS_EXPR, op1, t);
694 2302 : t = fold_convert (TREE_TYPE (op0), t);
695 :
696 2302 : gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
697 2302 : gimple_assign_set_rhs1 (stmt, op0);
698 2302 : gimple_assign_set_rhs2 (stmt, t);
699 : }
700 :
701 6680 : update_stmt (stmt);
702 6680 : fold_stmt (gsi, follow_single_use_edges);
703 6680 : return true;
704 : }
705 :
706 : return false;
707 356563 : }
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 258370 : simplify_using_ranges::simplify_min_or_max_using_ranges
714 : (gimple_stmt_iterator *gsi,
715 : gassign *stmt)
716 : {
717 258370 : tree op0 = gimple_assign_rhs1 (stmt);
718 258370 : tree op1 = gimple_assign_rhs2 (stmt);
719 258370 : tree val;
720 :
721 258370 : val = fold_cond_with_ops (LE_EXPR, op0, op1, stmt);
722 258370 : if (!val)
723 253977 : val = fold_cond_with_ops (LT_EXPR, op0, op1, stmt);
724 :
725 253977 : if (val)
726 : {
727 : /* VAL == TRUE -> OP0 < or <= op1
728 : VAL == FALSE -> OP0 > or >= op1. */
729 5328 : tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
730 5328 : == integer_zerop (val)) ? op0 : op1;
731 5328 : gimple_assign_set_rhs_from_tree (gsi, res);
732 5328 : 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 9319 : simplify_using_ranges::simplify_abs_using_ranges (gimple_stmt_iterator *gsi,
744 : gassign *stmt)
745 : {
746 9319 : tree op = gimple_assign_rhs1 (stmt);
747 9319 : tree zero = build_zero_cst (TREE_TYPE (op));
748 9319 : tree val = fold_cond_with_ops (LE_EXPR, op, zero, stmt);
749 :
750 9319 : if (!val)
751 : {
752 : /* The range is neither <= 0 nor > 0. Now see if it is
753 : either < 0 or >= 0. */
754 9069 : val = fold_cond_with_ops (LT_EXPR, op, zero, stmt);
755 : }
756 9069 : if (val)
757 : {
758 366 : gimple_assign_set_rhs1 (stmt, op);
759 366 : if (integer_zerop (val))
760 166 : gimple_assign_set_rhs_code (stmt, SSA_NAME);
761 : else
762 200 : gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
763 366 : update_stmt (stmt);
764 366 : fold_stmt (gsi, follow_single_use_edges);
765 366 : 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 1665422 : 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 1665422 : if (vr->varying_p () || vr->undefined_p ())
782 : {
783 979005 : *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
784 979005 : *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
785 979005 : return false;
786 : }
787 686419 : wi_set_zero_nonzero_bits (expr_type, vr->lower_bound (), vr->upper_bound (),
788 : *may_be_nonzero, *must_be_nonzero);
789 686417 : 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 1300839 : simplify_using_ranges::simplify_bit_ops_using_ranges
800 : (gimple_stmt_iterator *gsi,
801 : gassign *stmt)
802 : {
803 1300839 : tree op0 = gimple_assign_rhs1 (stmt);
804 1300839 : tree op1 = gimple_assign_rhs2 (stmt);
805 1300839 : tree op = NULL_TREE;
806 1300839 : int_range_max vr0, vr1;
807 1300839 : wide_int may_be_nonzero0, may_be_nonzero1;
808 1300839 : wide_int must_be_nonzero0, must_be_nonzero1;
809 1300839 : wide_int mask;
810 :
811 1300839 : if (!query->range_of_expr (vr0, op0, stmt)
812 1300839 : || vr0.undefined_p ())
813 : return false;
814 1300157 : if (!query->range_of_expr (vr1, op1, stmt)
815 1300157 : || vr1.undefined_p ())
816 : return false;
817 :
818 1299795 : if (!vr_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
819 : &must_be_nonzero0))
820 : return false;
821 365627 : if (!vr_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
822 : &must_be_nonzero1))
823 : return false;
824 :
825 320790 : switch (gimple_assign_rhs_code (stmt))
826 : {
827 226977 : case BIT_AND_EXPR:
828 226977 : mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
829 226977 : if (mask == 0)
830 : {
831 : op = op0;
832 : break;
833 : }
834 221968 : mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
835 221968 : if (mask == 0)
836 : {
837 : op = op1;
838 : break;
839 : }
840 : break;
841 93813 : case BIT_IOR_EXPR:
842 93813 : mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
843 93813 : if (mask == 0)
844 : {
845 : op = op1;
846 : break;
847 : }
848 93813 : mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
849 93813 : if (mask == 0)
850 : {
851 : op = op0;
852 : break;
853 : }
854 : break;
855 0 : default:
856 0 : gcc_unreachable ();
857 : }
858 :
859 5229 : if (op == NULL_TREE)
860 : return false;
861 :
862 5229 : gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
863 5229 : update_stmt (gsi_stmt (*gsi));
864 5229 : return true;
865 1300851 : }
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 1659758 : test_for_singularity (enum tree_code cond_code, tree op0,
875 : tree op1, const irange *vr)
876 : {
877 1659758 : tree min = NULL;
878 1659758 : tree max = NULL;
879 :
880 : /* Extract minimum/maximum values which satisfy the conditional as it was
881 : written. */
882 1659758 : if (cond_code == LE_EXPR || cond_code == LT_EXPR)
883 : {
884 723852 : min = TYPE_MIN_VALUE (TREE_TYPE (op0));
885 :
886 723852 : max = op1;
887 723852 : if (cond_code == LT_EXPR)
888 : {
889 93159 : tree one = build_int_cst (TREE_TYPE (op0), 1);
890 93159 : max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
891 : }
892 : }
893 935906 : else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
894 : {
895 792561 : max = TYPE_MAX_VALUE (TREE_TYPE (op0));
896 :
897 792561 : min = op1;
898 792561 : if (cond_code == GT_EXPR)
899 : {
900 700933 : tree one = build_int_cst (TREE_TYPE (op0), 1);
901 700933 : 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 1659758 : if (min && max)
908 : {
909 1516413 : tree type = TREE_TYPE (op0);
910 1516413 : tree tmin = wide_int_to_tree (type, vr->lower_bound ());
911 1516413 : tree tmax = wide_int_to_tree (type, vr->upper_bound ());
912 1516413 : if (compare_values (tmin, min) == 1)
913 462051 : min = tmin;
914 1516413 : if (compare_values (tmax, max) == -1)
915 569526 : 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 1516413 : if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
921 326112 : 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 347699 : range_fits_type_p (const irange *vr,
931 : unsigned dest_precision, signop dest_sgn)
932 : {
933 347699 : tree src_type;
934 347699 : unsigned src_precision;
935 347699 : widest_int tem;
936 347699 : signop src_sgn;
937 :
938 : /* Now we can only handle ranges with constant bounds. */
939 347699 : if (vr->undefined_p () || vr->varying_p ())
940 : return false;
941 :
942 : /* We can only handle integral and pointer types. */
943 261517 : src_type = vr->type ();
944 261517 : 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 261517 : src_precision = TYPE_PRECISION (src_type);
951 261517 : src_sgn = TYPE_SIGN (src_type);
952 261517 : if ((src_precision < dest_precision
953 16783 : && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
954 246221 : || (src_precision == dest_precision && src_sgn == dest_sgn))
955 : return true;
956 :
957 246064 : wide_int vrmin = vr->lower_bound ();
958 246064 : 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 246064 : if (src_sgn != dest_sgn
965 246064 : && (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 134539 : signop sign = TYPE_SIGN (vr->type ());
971 134539 : tem = wi::ext (widest_int::from (vrmin, sign), dest_precision, dest_sgn);
972 134539 : if (tem != widest_int::from (vrmin, sign))
973 : return false;
974 128748 : tem = wi::ext (widest_int::from (vrmax, sign), dest_precision, dest_sgn);
975 128748 : if (tem != widest_int::from (vrmax, sign))
976 : return false;
977 :
978 : return true;
979 246064 : }
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 302481 : 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 302481 : if ((e->flags & m_not_executable_flag) == m_not_executable_flag)
990 208981 : return;
991 :
992 218855 : e->flags |= m_not_executable_flag;
993 218855 : m_flag_set_edges.safe_push (e);
994 :
995 : // Check if the destination block needs to propagate the property.
996 218855 : basic_block bb = e->dest;
997 :
998 : // If any incoming edge is executable, we are done.
999 218855 : edge_iterator ei;
1000 351216 : FOR_EACH_EDGE (e, ei, bb->preds)
1001 257716 : 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 164416 : FOR_EACH_EDGE (e, ei, bb->succs)
1006 70916 : 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 21706056 : simplify_using_ranges::fold_cond (gcond *cond)
1014 : {
1015 21706056 : int_range_max r;
1016 21706056 : if (query->range_of_stmt (r, cond) && r.singleton_p ())
1017 : {
1018 : // COND has already been folded if arguments are constant.
1019 384697 : if (TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
1020 384697 : && TREE_CODE (gimple_cond_rhs (cond)) != SSA_NAME)
1021 : return false;
1022 224432 : if (dump_file)
1023 : {
1024 211 : fprintf (dump_file, "Folding predicate ");
1025 211 : print_gimple_expr (dump_file, cond, 0);
1026 211 : fprintf (dump_file, " to ");
1027 : }
1028 224432 : edge e0 = EDGE_SUCC (gimple_bb (cond), 0);
1029 224432 : edge e1 = EDGE_SUCC (gimple_bb (cond), 1);
1030 224432 : if (r.zero_p ())
1031 : {
1032 144152 : if (dump_file)
1033 146 : fprintf (dump_file, "0\n");
1034 144152 : gimple_cond_make_false (cond);
1035 144152 : if (e0->flags & EDGE_TRUE_VALUE)
1036 142562 : set_and_propagate_unexecutable (e0);
1037 : else
1038 1590 : set_and_propagate_unexecutable (e1);
1039 : }
1040 : else
1041 : {
1042 80280 : if (dump_file)
1043 65 : fprintf (dump_file, "1\n");
1044 80280 : gimple_cond_make_true (cond);
1045 80280 : if (e0->flags & EDGE_FALSE_VALUE)
1046 802 : set_and_propagate_unexecutable (e0);
1047 : else
1048 79478 : set_and_propagate_unexecutable (e1);
1049 : }
1050 224432 : update_stmt (cond);
1051 224432 : return true;
1052 : }
1053 :
1054 : // FIXME: Audit the code below and make sure it never finds anything.
1055 21321359 : edge taken_edge;
1056 21321359 : legacy_fold_cond (cond, &taken_edge);
1057 :
1058 21321359 : if (taken_edge)
1059 : {
1060 35 : if (taken_edge->flags & EDGE_TRUE_VALUE)
1061 : {
1062 27 : if (dump_file && (dump_flags & TDF_DETAILS))
1063 0 : fprintf (dump_file, "\nVRP Predicate evaluates to: 1\n");
1064 27 : gimple_cond_make_true (cond);
1065 : }
1066 8 : else if (taken_edge->flags & EDGE_FALSE_VALUE)
1067 : {
1068 8 : if (dump_file && (dump_flags & TDF_DETAILS))
1069 0 : fprintf (dump_file, "\nVRP Predicate evaluates to: 0\n");
1070 8 : gimple_cond_make_false (cond);
1071 : }
1072 : else
1073 0 : gcc_unreachable ();
1074 35 : update_stmt (cond);
1075 35 : return true;
1076 : }
1077 : return false;
1078 21706056 : }
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 12570854 : simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt)
1086 : {
1087 12570854 : tree op0 = gimple_cond_lhs (stmt);
1088 12570854 : tree op1 = gimple_cond_rhs (stmt);
1089 12570854 : enum tree_code cond_code = gimple_cond_code (stmt);
1090 :
1091 12570854 : if (fold_cond (stmt))
1092 : return true;
1093 :
1094 12429660 : if (simplify_compare_using_ranges_1 (cond_code, op0, op1, stmt))
1095 : {
1096 392578 : if (dump_file)
1097 : {
1098 28 : fprintf (dump_file, "Simplified relational ");
1099 28 : print_gimple_stmt (dump_file, stmt, 0);
1100 28 : fprintf (dump_file, " into ");
1101 : }
1102 :
1103 392578 : gimple_cond_set_code (stmt, cond_code);
1104 392578 : gimple_cond_set_lhs (stmt, op0);
1105 392578 : gimple_cond_set_rhs (stmt, op1);
1106 :
1107 392578 : update_stmt (stmt);
1108 :
1109 392578 : if (dump_file)
1110 : {
1111 28 : print_gimple_stmt (dump_file, stmt, 0);
1112 28 : 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 1275484 : simplify_using_ranges::simplify_compare_assign_using_ranges_1
1124 : (gimple_stmt_iterator *gsi,
1125 : gassign *stmt)
1126 : {
1127 1275484 : enum tree_code code = gimple_assign_rhs_code (stmt);
1128 1275484 : tree op0 = gimple_assign_rhs1 (stmt);
1129 1275484 : tree op1 = gimple_assign_rhs2 (stmt);
1130 1275484 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1131 1275484 : bool happened = false;
1132 :
1133 1275484 : if (simplify_compare_using_ranges_1 (code, op0, op1, stmt))
1134 : {
1135 7003 : 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 7003 : gimple_assign_set_rhs_code (stmt, code);
1143 7003 : gimple_assign_set_rhs1 (stmt, op0);
1144 7003 : gimple_assign_set_rhs2 (stmt, op1);
1145 :
1146 7003 : update_stmt (stmt);
1147 :
1148 7003 : 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 1275484 : if ((code == EQ_EXPR || code == NE_EXPR)
1160 718843 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
1161 1779078 : && simplify_truth_ops_using_ranges (gsi, stmt))
1162 : happened = true;
1163 :
1164 1275484 : 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 13705144 : simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tree &op0, tree &op1, gimple *stmt)
1173 : {
1174 13705144 : bool happened = false;
1175 13705144 : if (cond_code != NE_EXPR
1176 13705144 : && cond_code != EQ_EXPR
1177 3532306 : && TREE_CODE (op0) == SSA_NAME
1178 3531815 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
1179 16875497 : && is_gimple_min_invariant (op1))
1180 : {
1181 1926486 : int_range_max vr;
1182 :
1183 1926486 : 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 1926486 : if (!vr.undefined_p () && !vr.varying_p ())
1189 : {
1190 829879 : tree new_tree = test_for_singularity (cond_code, op0, op1, &vr);
1191 829879 : if (new_tree)
1192 : {
1193 143345 : cond_code = EQ_EXPR;
1194 143345 : op1 = new_tree;
1195 143345 : 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 829879 : new_tree = test_for_singularity
1202 829879 : (invert_tree_comparison (cond_code, false),
1203 : op0, op1, &vr);
1204 829879 : if (new_tree)
1205 : {
1206 182767 : cond_code = NE_EXPR;
1207 182767 : op1 = new_tree;
1208 182767 : happened = true;
1209 : }
1210 : }
1211 1926486 : }
1212 : // If OP0 >= OP1 and the relation between them is VREL_LE (or vice
1213 : // versa), we know OP0 == OP1.
1214 11778658 : else if ((cond_code == LE_EXPR || cond_code == GE_EXPR)
1215 395616 : && gimple_range_ssa_p (op0)
1216 12166928 : && gimple_range_ssa_p (op1))
1217 : {
1218 357442 : relation_kind rel = query->relation ().query (stmt, op0, op1);
1219 543215 : if (relation_intersect (cond_code == LE_EXPR ? VREL_LE : VREL_GE, rel)
1220 : == VREL_EQ)
1221 : {
1222 641 : cond_code = EQ_EXPR;
1223 641 : happened = true;
1224 : }
1225 : }
1226 : // Try to simplify casted conditions.
1227 13705144 : if (simplify_casted_compare (cond_code, op0, op1))
1228 85841 : happened = true;
1229 13705144 : return happened;
1230 : }
1231 :
1232 : /* Simplify OP0 code OP1 when OP1 is a constant and OP0 was a SSA_NAME
1233 : defined by a type conversion. Replacing OP0 with RHS of the type conversion.
1234 : Doing so makes the conversion dead which helps subsequent passes. */
1235 :
1236 : bool
1237 13705144 : simplify_using_ranges::simplify_casted_compare (tree_code &, tree &op0, tree &op1)
1238 : {
1239 :
1240 : /* If we have a comparison of an SSA_NAME (OP0) against a constant,
1241 : see if OP0 was set by a type conversion where the source of
1242 : the conversion is another SSA_NAME with a range that fits
1243 : into the range of OP0's type.
1244 :
1245 : If so, the conversion is redundant as the earlier SSA_NAME can be
1246 : used for the comparison directly if we just massage the constant in the
1247 : comparison. */
1248 13705144 : if (TREE_CODE (op0) == SSA_NAME
1249 13609273 : && TREE_CODE (op1) == INTEGER_CST)
1250 : {
1251 9598782 : tree innerop;
1252 :
1253 9598782 : auto *def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (op0));
1254 5905041 : if (!def_stmt)
1255 : return false;
1256 :
1257 9793003 : switch (gimple_assign_rhs_code (def_stmt))
1258 : {
1259 501898 : CASE_CONVERT:
1260 501898 : innerop = gimple_assign_rhs1 (def_stmt);
1261 501898 : break;
1262 52433 : case VIEW_CONVERT_EXPR:
1263 52433 : innerop = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
1264 52433 : if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
1265 : return false;
1266 : break;
1267 : default:
1268 : return false;
1269 : }
1270 :
1271 551482 : if (TREE_CODE (innerop) == SSA_NAME
1272 551454 : && !POINTER_TYPE_P (TREE_TYPE (innerop))
1273 545884 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
1274 1097353 : && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
1275 : {
1276 515945 : int_range_max vr;
1277 :
1278 515945 : if (query->range_of_expr (vr, innerop)
1279 515942 : && !vr.varying_p ()
1280 177351 : && !vr.undefined_p ()
1281 177236 : && range_fits_type_p (&vr,
1282 177236 : TYPE_PRECISION (TREE_TYPE (op0)),
1283 177236 : TYPE_SIGN (TREE_TYPE (op0)))
1284 601786 : && int_fits_type_p (op1, TREE_TYPE (innerop)))
1285 : {
1286 85841 : tree newconst = fold_convert (TREE_TYPE (innerop), op1);
1287 85841 : op0 = innerop;
1288 85841 : op1 = newconst;
1289 85841 : return true;
1290 : }
1291 515945 : }
1292 : }
1293 : return false;
1294 : }
1295 :
1296 : /* Simplify a switch statement using the value range of the switch
1297 : argument. */
1298 :
1299 : bool
1300 64107 : simplify_using_ranges::simplify_switch_using_ranges (gswitch *stmt)
1301 : {
1302 64107 : tree op = gimple_switch_index (stmt);
1303 64107 : tree type = TREE_TYPE (op);
1304 64107 : int_range_max op_range (type);
1305 64107 : int_range_max default_range (type);
1306 64107 : auto_vec<unsigned> cases;
1307 64107 : cases.truncate (0);
1308 64107 : edge e;
1309 64107 : switch_update su;
1310 :
1311 : // Abort if we don't have a useful range for the switch index.
1312 64107 : if (!query->range_of_expr (op_range, op, stmt)
1313 64107 : || op_range.varying_p () || op_range.undefined_p ())
1314 : return false;
1315 :
1316 : // Default range starts with full known range of op.
1317 16825 : default_range = op_range;
1318 16825 : edge default_edge = gimple_switch_default_edge (cfun, stmt);
1319 :
1320 16825 : unsigned x, lim = gimple_switch_num_labels (stmt);
1321 99040 : for (x = 1; x < lim; x++)
1322 : {
1323 83406 : e = gimple_switch_edge (cfun, stmt, x);
1324 83406 : tree label = gimple_switch_label (stmt, x);
1325 :
1326 : // If this edge is the same as the default edge, do nothing else.
1327 83406 : if (e == default_edge)
1328 6210 : continue;
1329 : // Ada sometimes has mismatched labels and index. Just bail.
1330 83400 : if (TREE_TYPE (CASE_LOW (label)) != type)
1331 1191 : return false;
1332 :
1333 82211 : wide_int low = wi::to_wide (CASE_LOW (label));
1334 82211 : wide_int high;
1335 : // Singleton cases have no CASE_HIGH.
1336 82211 : tree tree_high = CASE_HIGH (label);
1337 82211 : if (tree_high)
1338 3469 : high = wi::to_wide (tree_high);
1339 : else
1340 78742 : high = low;
1341 :
1342 : // If the case range is fully contained in op_range, leave the
1343 : // case as it is, otherwise adjust the labels.
1344 82211 : int_range_max case_range (type, low, high);
1345 82211 : if (case_range.intersect (op_range))
1346 : {
1347 : // If none of the label is in op_range, skip this label.
1348 50071 : if (case_range.undefined_p ())
1349 6204 : continue;
1350 :
1351 : // Part of the label is in op_range, but not all of it. CASE_RANGE
1352 : // contains the part that is. Adjust the case range to
1353 : // the new min/max.
1354 43867 : if (case_range.lower_bound () != low)
1355 13 : CASE_LOW (label) = wide_int_to_tree (type,
1356 26 : case_range.lower_bound ());
1357 43867 : if (case_range.singleton_p ())
1358 42514 : CASE_HIGH (label) = NULL_TREE;
1359 : else
1360 1353 : if (case_range.upper_bound () != high)
1361 8 : CASE_HIGH (label) = wide_int_to_tree (type,
1362 16 : case_range.upper_bound ());
1363 : }
1364 : // Add case label to the keep list.
1365 76007 : cases.safe_push (x);
1366 : // Remove case_range from needing to be handled by the default.
1367 76007 : if (!case_range.invert ())
1368 2 : return false;
1369 76005 : default_range.intersect (case_range);
1370 82211 : }
1371 :
1372 : // An undefined DEFAULT range means the current default case is not needed.
1373 15634 : unsigned idx = default_range.undefined_p () ? 0 : 1;
1374 15634 : unsigned vec_size = cases.length () + idx;
1375 15634 : if (vec_size == lim)
1376 : return false;
1377 :
1378 4162 : tree vec2 = make_tree_vec (vec_size);
1379 : // Add default label if there is one.
1380 4162 : if (idx)
1381 : {
1382 3106 : TREE_VEC_ELT (vec2, 0) = gimple_switch_default_label (stmt);
1383 3106 : e = gimple_switch_edge (cfun, stmt, 0);
1384 3106 : e->aux = (void *)-1;
1385 : }
1386 :
1387 16734 : for (x = 0; x < cases.length (); x++)
1388 : {
1389 12572 : unsigned swi = cases[x];
1390 12572 : TREE_VEC_ELT (vec2, idx++) = gimple_switch_label (stmt, swi);
1391 12572 : e = gimple_switch_edge (cfun, stmt, swi);
1392 12572 : e->aux = (void *)-1;
1393 : }
1394 :
1395 : /* Queue not needed edges for later removal. */
1396 4162 : edge_iterator ei;
1397 26233 : FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1398 : {
1399 22071 : if (e->aux == (void *)-1)
1400 : {
1401 14938 : e->aux = NULL;
1402 14938 : continue;
1403 : }
1404 :
1405 7133 : if (dump_file && (dump_flags & TDF_DETAILS))
1406 : {
1407 0 : fprintf (dump_file, "removing unreachable case label\n");
1408 : }
1409 7133 : to_remove_edges.safe_push (e);
1410 7133 : set_and_propagate_unexecutable (e);
1411 7133 : e->flags &= ~EDGE_EXECUTABLE;
1412 7133 : e->flags |= EDGE_IGNORE;
1413 : }
1414 :
1415 : /* And queue an update for the stmt. */
1416 4162 : su.stmt = stmt;
1417 4162 : su.vec = vec2;
1418 4162 : to_update_switch_stmts.safe_push (su);
1419 4162 : return true;
1420 64107 : }
1421 :
1422 : void
1423 13525841 : simplify_using_ranges::cleanup_edges_and_switches (void)
1424 : {
1425 13525841 : int i;
1426 13525841 : edge e;
1427 13525841 : switch_update *su;
1428 :
1429 : /* Clear any edges marked as not executable. */
1430 13525841 : if (m_not_executable_flag)
1431 : {
1432 4609485 : FOR_EACH_VEC_ELT (m_flag_set_edges, i, e)
1433 218855 : e->flags &= ~m_not_executable_flag;
1434 : }
1435 : /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
1436 : CFG in a broken state and requires a cfg_cleanup run. */
1437 13536858 : FOR_EACH_VEC_ELT (to_remove_edges, i, e)
1438 7133 : remove_edge (e);
1439 :
1440 : /* Update SWITCH_EXPR case label vector. */
1441 13530003 : FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
1442 : {
1443 4162 : size_t j;
1444 4162 : size_t n = TREE_VEC_LENGTH (su->vec);
1445 4162 : tree label;
1446 4162 : gimple_switch_set_num_labels (su->stmt, n);
1447 24002 : for (j = 0; j < n; j++)
1448 15678 : gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
1449 : /* As we may have replaced the default label with a regular one
1450 : make sure to make it a real default label again. This ensures
1451 : optimal expansion. */
1452 4162 : label = gimple_switch_label (su->stmt, 0);
1453 4162 : CASE_LOW (label) = NULL_TREE;
1454 4162 : CASE_HIGH (label) = NULL_TREE;
1455 : }
1456 :
1457 13525841 : if (!to_remove_edges.is_empty ())
1458 : {
1459 3884 : free_dominance_info (CDI_DOMINATORS);
1460 3884 : loops_state_set (LOOPS_NEED_FIXUP);
1461 : }
1462 :
1463 13525841 : to_remove_edges.release ();
1464 13525841 : to_update_switch_stmts.release ();
1465 13525841 : }
1466 :
1467 : /* Simplify an integral conversion from an SSA name in STMT. */
1468 :
1469 : static bool
1470 4999507 : simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gassign *stmt)
1471 : {
1472 4999507 : tree innerop, middleop, finaltype;
1473 4999507 : signop inner_sgn, middle_sgn, final_sgn;
1474 4999507 : unsigned inner_prec, middle_prec, final_prec;
1475 4999507 : widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
1476 :
1477 4999507 : finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
1478 4999507 : if (!INTEGRAL_TYPE_P (finaltype))
1479 : return false;
1480 4602672 : middleop = gimple_assign_rhs1 (stmt);
1481 8251828 : auto def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (middleop));
1482 4474220 : if (!def_stmt || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
1483 : return false;
1484 156072 : innerop = gimple_assign_rhs1 (def_stmt);
1485 156072 : if (TREE_CODE (innerop) != SSA_NAME
1486 156072 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
1487 : return false;
1488 :
1489 : /* Get the value-range of the inner operand. Use global ranges in
1490 : case innerop was created during substitute-and-fold. */
1491 152612 : wide_int imin, imax;
1492 152612 : int_range_max vr;
1493 152612 : if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
1494 : return false;
1495 251758 : get_range_query (cfun)->range_of_expr (vr, innerop, stmt);
1496 125879 : if (vr.undefined_p () || vr.varying_p ())
1497 : return false;
1498 69102 : innermin = widest_int::from (vr.lower_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
1499 69102 : innermax = widest_int::from (vr.upper_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
1500 :
1501 : /* Simulate the conversion chain to check if the result is equal if
1502 : the middle conversion is removed. */
1503 69102 : inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
1504 69102 : middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
1505 69102 : final_prec = TYPE_PRECISION (finaltype);
1506 :
1507 : /* If the first conversion is not injective, the second must not
1508 : be widening. */
1509 69102 : if (wi::gtu_p (innermax - innermin,
1510 138204 : wi::mask <widest_int> (middle_prec, false))
1511 69102 : && middle_prec < final_prec)
1512 : return false;
1513 : /* We also want a medium value so that we can track the effect that
1514 : narrowing conversions with sign change have. */
1515 57545 : inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
1516 57545 : if (inner_sgn == UNSIGNED)
1517 41758 : innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
1518 : else
1519 15787 : innermed = 0;
1520 57545 : if (wi::cmp (innermin, innermed, inner_sgn) >= 0
1521 57545 : || wi::cmp (innermed, innermax, inner_sgn) >= 0)
1522 40967 : innermed = innermin;
1523 :
1524 57545 : middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
1525 57545 : middlemin = wi::ext (innermin, middle_prec, middle_sgn);
1526 57545 : middlemed = wi::ext (innermed, middle_prec, middle_sgn);
1527 57545 : middlemax = wi::ext (innermax, middle_prec, middle_sgn);
1528 :
1529 : /* Require that the final conversion applied to both the original
1530 : and the intermediate range produces the same result. */
1531 57545 : final_sgn = TYPE_SIGN (finaltype);
1532 115090 : if (wi::ext (middlemin, final_prec, final_sgn)
1533 115090 : != wi::ext (innermin, final_prec, final_sgn)
1534 54919 : || wi::ext (middlemed, final_prec, final_sgn)
1535 222302 : != wi::ext (innermed, final_prec, final_sgn)
1536 103795 : || wi::ext (middlemax, final_prec, final_sgn)
1537 196295 : != wi::ext (innermax, final_prec, final_sgn))
1538 : return false;
1539 :
1540 44907 : gimple_assign_set_rhs1 (stmt, innerop);
1541 44907 : fold_stmt (gsi, follow_single_use_edges);
1542 44907 : return true;
1543 5152119 : }
1544 :
1545 : /* Simplify a conversion from integral SSA name to float in STMT. */
1546 :
1547 : bool
1548 260295 : simplify_using_ranges::simplify_float_conversion_using_ranges
1549 : (gimple_stmt_iterator *gsi,
1550 : gassign *stmt)
1551 : {
1552 260295 : tree rhs1 = gimple_assign_rhs1 (stmt);
1553 260295 : int_range_max vr;
1554 260295 : scalar_float_mode fltmode
1555 260295 : = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
1556 260295 : scalar_int_mode mode;
1557 260295 : tree tem;
1558 260295 : gassign *conv;
1559 :
1560 : /* We can only handle constant ranges. */
1561 260295 : if (!query->range_of_expr (vr, rhs1, stmt)
1562 260295 : || vr.varying_p ()
1563 363166 : || vr.undefined_p ())
1564 : return false;
1565 :
1566 : /* The code below doesn't work for large/huge _BitInt, nor is really
1567 : needed for those, bitint lowering does use ranges already. */
1568 204367 : if (BITINT_TYPE_P (TREE_TYPE (rhs1))
1569 102190 : && TYPE_MODE (TREE_TYPE (rhs1)) == BLKmode)
1570 : return false;
1571 : /* First check if we can use a signed type in place of an unsigned. */
1572 102177 : scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
1573 102177 : if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
1574 5515 : && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
1575 105958 : && range_fits_type_p (&vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
1576 : mode = rhs_mode;
1577 : /* If we can do the conversion in the current input mode do nothing. */
1578 100507 : else if (can_float_p (fltmode, rhs_mode,
1579 100507 : TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
1580 : return false;
1581 : /* Otherwise search for a mode we can use, starting from the narrowest
1582 : integer mode available. */
1583 : else
1584 : {
1585 4364 : mode = NARROWEST_INT_MODE;
1586 15726 : for (;;)
1587 : {
1588 : /* If we cannot do a signed conversion to float from mode
1589 : or if the value-range does not fit in the signed type
1590 : try with a wider mode. */
1591 15726 : if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
1592 15726 : && range_fits_type_p (&vr, GET_MODE_PRECISION (mode), SIGNED))
1593 : break;
1594 :
1595 : /* But do not widen the input. Instead leave that to the
1596 : optabs expansion code. */
1597 31186 : if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
1598 15593 : || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
1599 : return false;
1600 : }
1601 : }
1602 :
1603 : /* It works, insert a truncation or sign-change before the
1604 : float conversion. */
1605 1803 : tem = make_ssa_name (build_nonstandard_integer_type
1606 1803 : (GET_MODE_PRECISION (mode), 0));
1607 1803 : conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
1608 1803 : gsi_insert_before (gsi, conv, GSI_SAME_STMT);
1609 1803 : gimple_assign_set_rhs1 (stmt, tem);
1610 1803 : fold_stmt (gsi, follow_single_use_edges);
1611 :
1612 1803 : return true;
1613 260295 : }
1614 :
1615 : /* Simplify an internal fn call using ranges if possible. */
1616 :
1617 : bool
1618 621134 : simplify_using_ranges::simplify_internal_call_using_ranges
1619 : (gimple_stmt_iterator *gsi,
1620 : gimple *stmt)
1621 : {
1622 621134 : enum tree_code subcode;
1623 621134 : bool is_ubsan = false;
1624 621134 : bool ovf = false;
1625 621134 : switch (gimple_call_internal_fn (stmt))
1626 : {
1627 : case IFN_UBSAN_CHECK_ADD:
1628 : subcode = PLUS_EXPR;
1629 : is_ubsan = true;
1630 : break;
1631 2660 : case IFN_UBSAN_CHECK_SUB:
1632 2660 : subcode = MINUS_EXPR;
1633 2660 : is_ubsan = true;
1634 2660 : break;
1635 2138 : case IFN_UBSAN_CHECK_MUL:
1636 2138 : subcode = MULT_EXPR;
1637 2138 : is_ubsan = true;
1638 2138 : break;
1639 41374 : case IFN_ADD_OVERFLOW:
1640 41374 : subcode = PLUS_EXPR;
1641 41374 : break;
1642 49796 : case IFN_SUB_OVERFLOW:
1643 49796 : subcode = MINUS_EXPR;
1644 49796 : break;
1645 47358 : case IFN_MUL_OVERFLOW:
1646 47358 : subcode = MULT_EXPR;
1647 47358 : break;
1648 : default:
1649 : return false;
1650 : }
1651 :
1652 145919 : tree op0 = gimple_call_arg (stmt, 0);
1653 145919 : tree op1 = gimple_call_arg (stmt, 1);
1654 145919 : tree type;
1655 145919 : if (is_ubsan)
1656 : {
1657 7391 : type = TREE_TYPE (op0);
1658 7391 : if (VECTOR_TYPE_P (type))
1659 : return false;
1660 : }
1661 138528 : else if (gimple_call_lhs (stmt) == NULL_TREE)
1662 : return false;
1663 : else
1664 138528 : type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
1665 144993 : if (!check_for_binary_op_overflow (query, subcode, type, op0, op1, &ovf, stmt)
1666 144993 : || (is_ubsan && ovf))
1667 : return false;
1668 :
1669 3498 : gassign *g;
1670 3498 : location_t loc = gimple_location (stmt);
1671 3498 : if (is_ubsan)
1672 529 : g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
1673 : else
1674 : {
1675 2969 : tree utype = type;
1676 2969 : if (ovf
1677 1480 : || !useless_type_conversion_p (type, TREE_TYPE (op0))
1678 3706 : || !useless_type_conversion_p (type, TREE_TYPE (op1)))
1679 2521 : utype = unsigned_type_for (type);
1680 2969 : if (TREE_CODE (op0) == INTEGER_CST)
1681 1175 : op0 = fold_convert (utype, op0);
1682 1794 : else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
1683 : {
1684 753 : g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
1685 753 : gimple_set_location (g, loc);
1686 753 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1687 753 : op0 = gimple_assign_lhs (g);
1688 : }
1689 2969 : if (TREE_CODE (op1) == INTEGER_CST)
1690 1721 : op1 = fold_convert (utype, op1);
1691 1248 : else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
1692 : {
1693 28 : g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
1694 28 : gimple_set_location (g, loc);
1695 28 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1696 28 : op1 = gimple_assign_lhs (g);
1697 : }
1698 2969 : g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
1699 2969 : gimple_set_location (g, loc);
1700 2969 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1701 2969 : if (utype != type)
1702 : {
1703 1363 : g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
1704 : gimple_assign_lhs (g));
1705 1363 : gimple_set_location (g, loc);
1706 1363 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1707 : }
1708 2969 : g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
1709 : gimple_assign_lhs (g),
1710 2969 : build_int_cst (type, ovf));
1711 : }
1712 3498 : gimple_set_location (g, loc);
1713 3498 : gsi_replace (gsi, g, false);
1714 3498 : return true;
1715 : }
1716 :
1717 : /* Return true if VAR is a two-valued variable. Set a and b with the
1718 : two-values when it is true. Return false otherwise. */
1719 :
1720 : bool
1721 497772 : simplify_using_ranges::two_valued_val_range_p (tree var, tree *a, tree *b,
1722 : gimple *s)
1723 : {
1724 497772 : int_range_max vr;
1725 497772 : if (!query->range_of_expr (vr, var, s))
1726 : return false;
1727 497772 : if (vr.varying_p () || vr.undefined_p ())
1728 : return false;
1729 :
1730 818510 : if ((vr.num_pairs () == 1 && vr.upper_bound () - vr.lower_bound () == 1)
1731 425369 : || (vr.num_pairs () == 2
1732 298909 : && vr.lower_bound (0) == vr.upper_bound (0)
1733 252152 : && vr.lower_bound (1) == vr.upper_bound (1)))
1734 : {
1735 7876 : *a = wide_int_to_tree (TREE_TYPE (var), vr.lower_bound ());
1736 7876 : *b = wide_int_to_tree (TREE_TYPE (var), vr.upper_bound ());
1737 7876 : return true;
1738 : }
1739 : return false;
1740 497772 : }
1741 :
1742 13525841 : simplify_using_ranges::simplify_using_ranges (range_query *query,
1743 : int not_executable_flag)
1744 13525841 : : query (query)
1745 : {
1746 13525841 : to_remove_edges = vNULL;
1747 13525841 : to_update_switch_stmts = vNULL;
1748 13525841 : m_not_executable_flag = not_executable_flag;
1749 13525841 : m_flag_set_edges = vNULL;
1750 13525841 : }
1751 :
1752 13525841 : simplify_using_ranges::~simplify_using_ranges ()
1753 : {
1754 13525841 : cleanup_edges_and_switches ();
1755 13525841 : m_flag_set_edges.release ();
1756 13525841 : }
1757 :
1758 : /* Simplify STMT using ranges if possible. */
1759 :
1760 : bool
1761 257744017 : simplify_using_ranges::simplify (gimple_stmt_iterator *gsi)
1762 : {
1763 257744017 : gcc_checking_assert (query);
1764 :
1765 257744017 : gimple *stmt = gsi_stmt (*gsi);
1766 257744017 : if (auto assign = dyn_cast<gassign *> (stmt))
1767 : {
1768 70308381 : enum tree_code rhs_code = gimple_assign_rhs_code (assign);
1769 70308381 : tree rhs1 = gimple_assign_rhs1 (assign);
1770 70308381 : tree rhs2 = gimple_assign_rhs2 (assign);
1771 70308381 : tree lhs = gimple_assign_lhs (assign);
1772 70308381 : tree val1 = NULL_TREE, val2 = NULL_TREE;
1773 70308381 : use_operand_p use_p;
1774 70308381 : gimple *use_stmt;
1775 :
1776 : /* Convert:
1777 : LHS = CST BINOP VAR
1778 : Where VAR is two-valued and LHS is used in GIMPLE_COND only
1779 : To:
1780 : LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
1781 :
1782 : Also handles:
1783 : LHS = VAR BINOP CST
1784 : Where VAR is two-valued and LHS is used in GIMPLE_COND only
1785 : To:
1786 : LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
1787 :
1788 70308381 : if (TREE_CODE_CLASS (rhs_code) == tcc_binary
1789 15021132 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1790 10760682 : && ((TREE_CODE (rhs1) == INTEGER_CST
1791 186716 : && TREE_CODE (rhs2) == SSA_NAME)
1792 10574572 : || (TREE_CODE (rhs2) == INTEGER_CST
1793 7042981 : && TREE_CODE (rhs1) == SSA_NAME))
1794 7228485 : && single_imm_use (lhs, &use_p, &use_stmt)
1795 75863116 : && gimple_code (use_stmt) == GIMPLE_COND)
1796 :
1797 : {
1798 497772 : tree new_rhs1 = NULL_TREE;
1799 497772 : tree new_rhs2 = NULL_TREE;
1800 497772 : tree cmp_var = NULL_TREE;
1801 :
1802 497772 : if (TREE_CODE (rhs2) == SSA_NAME
1803 497772 : && two_valued_val_range_p (rhs2, &val1, &val2, stmt))
1804 : {
1805 : /* Optimize RHS1 OP [VAL1, VAL2]. */
1806 239 : new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
1807 239 : new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
1808 239 : cmp_var = rhs2;
1809 : }
1810 497533 : else if (TREE_CODE (rhs1) == SSA_NAME
1811 497533 : && two_valued_val_range_p (rhs1, &val1, &val2, stmt))
1812 : {
1813 : /* Optimize [VAL1, VAL2] OP RHS2. */
1814 7637 : new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
1815 7637 : new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
1816 7637 : cmp_var = rhs1;
1817 : }
1818 :
1819 : /* If we could not find two-vals or the optimization is invalid as
1820 : in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
1821 497772 : if (new_rhs1 && new_rhs2)
1822 : {
1823 7876 : tree cond = gimple_build (gsi, true, GSI_SAME_STMT,
1824 : UNKNOWN_LOCATION,
1825 : EQ_EXPR, boolean_type_node,
1826 : cmp_var, val1);
1827 7876 : gimple_assign_set_rhs_with_ops (gsi,
1828 : COND_EXPR, cond,
1829 : new_rhs1,
1830 : new_rhs2);
1831 7876 : update_stmt (gsi_stmt (*gsi));
1832 7876 : fold_stmt (gsi, follow_single_use_edges);
1833 8581899 : return true;
1834 : }
1835 : }
1836 :
1837 70300505 : if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
1838 1275484 : return simplify_compare_assign_using_ranges_1 (gsi, assign);
1839 :
1840 69025021 : switch (rhs_code)
1841 : {
1842 :
1843 : /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
1844 : and BIT_AND_EXPR respectively if the first operand is greater
1845 : than zero and the second operand is an exact power of two.
1846 : Also optimize TRUNC_MOD_EXPR away if the second operand is
1847 : constant and the first operand already has the right value
1848 : range. */
1849 358240 : case TRUNC_DIV_EXPR:
1850 358240 : case TRUNC_MOD_EXPR:
1851 358240 : if ((TREE_CODE (rhs1) == SSA_NAME
1852 358240 : || TREE_CODE (rhs1) == INTEGER_CST)
1853 358240 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1854 356563 : return simplify_div_or_mod_using_ranges (gsi, assign);
1855 : break;
1856 :
1857 : /* Transform ABS (X) into X or -X as appropriate. */
1858 65191 : case ABS_EXPR:
1859 65191 : if (TREE_CODE (rhs1) == SSA_NAME
1860 65191 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1861 9319 : return simplify_abs_using_ranges (gsi, assign);
1862 : break;
1863 :
1864 1341098 : case BIT_AND_EXPR:
1865 1341098 : case BIT_IOR_EXPR:
1866 : /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR if all the bits
1867 : being cleared are already cleared or all the bits being set
1868 : are already set. Beware that boolean types must be handled
1869 : logically (see range-op.cc) unless they have precision 1. */
1870 2593034 : if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1871 2552775 : && (TREE_CODE (TREE_TYPE (rhs1)) != BOOLEAN_TYPE
1872 315390 : || TYPE_PRECISION (TREE_TYPE (rhs1)) == 1))
1873 1300839 : return simplify_bit_ops_using_ranges (gsi, assign);
1874 : break;
1875 :
1876 6129392 : CASE_CONVERT:
1877 6129392 : if (TREE_CODE (rhs1) == SSA_NAME
1878 6129392 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1879 4999507 : return simplify_conversion_using_ranges (gsi, assign);
1880 : break;
1881 :
1882 262809 : case FLOAT_EXPR:
1883 262809 : if (TREE_CODE (rhs1) == SSA_NAME
1884 262809 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1885 260295 : return simplify_float_conversion_using_ranges (gsi, assign);
1886 : break;
1887 :
1888 258370 : case MIN_EXPR:
1889 258370 : case MAX_EXPR:
1890 258370 : return simplify_min_or_max_using_ranges (gsi, assign);
1891 :
1892 426259 : case RSHIFT_EXPR:
1893 426259 : {
1894 426259 : tree op0 = gimple_assign_rhs1 (assign);
1895 426259 : tree type = TREE_TYPE (op0);
1896 426259 : int_range_max range;
1897 426259 : if (TYPE_SIGN (type) == SIGNED
1898 426259 : && query->range_of_expr (range, op0, stmt))
1899 : {
1900 113646 : unsigned prec = TYPE_PRECISION (TREE_TYPE (op0));
1901 227292 : int_range<2> nzm1 (type, wi::minus_one (prec), wi::zero (prec),
1902 113646 : VR_ANTI_RANGE);
1903 113646 : range.intersect (nzm1);
1904 : // If there are no ranges other than [-1, 0] remove the shift.
1905 113646 : if (range.undefined_p ())
1906 : {
1907 86 : gimple_assign_set_rhs_from_tree (gsi, op0);
1908 86 : return true;
1909 : }
1910 : return false;
1911 113646 : }
1912 312613 : break;
1913 426259 : }
1914 : default:
1915 : break;
1916 : }
1917 : }
1918 187435636 : else if (gimple_code (stmt) == GIMPLE_COND)
1919 12570854 : return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
1920 174864782 : else if (gimple_code (stmt) == GIMPLE_SWITCH)
1921 64107 : return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
1922 174800675 : else if (is_gimple_call (stmt)
1923 174800675 : && gimple_call_internal_p (stmt))
1924 621134 : return simplify_internal_call_using_ranges (gsi, stmt);
1925 :
1926 : return false;
1927 : }
|