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 533387 : simplify_using_ranges::op_with_boolean_value_range_p (tree op, gimple *s)
59 : {
60 533387 : if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
61 : return true;
62 :
63 519299 : if (integer_zerop (op)
64 519299 : || integer_onep (op))
65 : return true;
66 :
67 509382 : 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 509382 : int_range_max vr;
73 509382 : return (query->range_of_expr (vr, op, s)
74 509382 : && vr == range_true_and_false (TREE_TYPE (op)));
75 509382 : }
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 143974 : 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 143974 : relation_kind rel = VREL_VARYING;
89 : /* For subtraction see if relations could simplify it. */
90 143974 : if (s
91 143974 : && subcode == MINUS_EXPR
92 143974 : && types_compatible_p (TREE_TYPE (op0), TREE_TYPE (op1)))
93 : {
94 29026 : 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 29026 : if (rel == VREL_EQ)
99 : return true;
100 : }
101 :
102 143973 : int_range_max vr0, vr1;
103 143973 : if (!query->range_of_expr (vr0, op0, s) || vr0.undefined_p ())
104 12 : vr0.set_varying (TREE_TYPE (op0));
105 143973 : if (!query->range_of_expr (vr1, op1, s) || vr1.undefined_p ())
106 8 : vr1.set_varying (TREE_TYPE (op1));
107 :
108 143973 : tree vr0min = wide_int_to_tree (TREE_TYPE (op0), vr0.lower_bound ());
109 143973 : tree vr0max = wide_int_to_tree (TREE_TYPE (op0), vr0.upper_bound ());
110 143973 : tree vr1min = wide_int_to_tree (TREE_TYPE (op1), vr1.lower_bound ());
111 143973 : 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 143973 : if ((rel == VREL_GT || rel == VREL_GE)
117 23 : && tree_int_cst_sgn (vr1min) >= 0
118 143990 : && !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 143956 : if (rel == VREL_LT
125 1 : && tree_int_cst_sgn (vr1min) >= 0
126 143957 : && TYPE_UNSIGNED (type))
127 : {
128 1 : *ovf = true;
129 1 : return true;
130 : }
131 :
132 236088 : *ovf = arith_overflowed_p (subcode, type, vr0min,
133 : subcode == MINUS_EXPR ? vr1max : vr1min);
134 236088 : if (arith_overflowed_p (subcode, type, vr0max,
135 143955 : subcode == MINUS_EXPR ? vr1min : vr1max) != *ovf)
136 : return false;
137 39466 : if (subcode == MULT_EXPR)
138 : {
139 23941 : if (arith_overflowed_p (subcode, type, vr0min, vr1max) != *ovf
140 23941 : || arith_overflowed_p (subcode, type, vr0max, vr1min) != *ovf)
141 : return false;
142 : }
143 39090 : 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 37122 : widest2_int wmin, wmax;
151 334290 : widest2_int w[4];
152 37122 : int i;
153 37122 : signop sign0 = TYPE_SIGN (TREE_TYPE (op0));
154 37122 : signop sign1 = TYPE_SIGN (TREE_TYPE (op1));
155 37173 : w[0] = widest2_int::from (vr0.lower_bound (), sign0);
156 37181 : w[1] = widest2_int::from (vr0.upper_bound (), sign0);
157 37159 : w[2] = widest2_int::from (vr1.lower_bound (), sign1);
158 37167 : w[3] = widest2_int::from (vr1.upper_bound (), sign1);
159 185610 : for (i = 0; i < 4; i++)
160 : {
161 148488 : widest2_int wt;
162 148488 : switch (subcode)
163 : {
164 27024 : case PLUS_EXPR:
165 27024 : wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
166 27024 : break;
167 27904 : case MINUS_EXPR:
168 27904 : wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
169 27904 : break;
170 93560 : case MULT_EXPR:
171 93560 : wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
172 93560 : break;
173 0 : default:
174 0 : gcc_unreachable ();
175 : }
176 148488 : if (i == 0)
177 : {
178 37122 : wmin = wt;
179 37122 : wmax = wt;
180 : }
181 : else
182 : {
183 111366 : wmin = wi::smin (wmin, wt);
184 112216 : wmax = wi::smax (wmax, wt);
185 : }
186 148488 : }
187 : /* The result of op0 CODE op1 is known to be in range
188 : [wmin, wmax]. */
189 37122 : widest2_int wtmin
190 74244 : = widest2_int::from (irange_val_min (type), TYPE_SIGN (type));
191 37122 : widest2_int wtmax
192 74244 : = 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 73234 : if (wmax < wtmin || wmin > wtmax)
197 1661 : return true;
198 : return false;
199 223012 : }
200 : return true;
201 143973 : }
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 6492967 : get_scev_info (vrange &r, tree name, gimple *stmt, class loop *l,
209 : tree &init, tree &step, enum ev_direction &dir)
210 : {
211 6492967 : tree ev = analyze_scalar_evolution (l, name);
212 6492967 : tree chrec = instantiate_parameters (l, ev);
213 6492967 : tree type = TREE_TYPE (name);
214 6492967 : if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
215 : {
216 2980012 : r.set_varying (type);
217 2980012 : return false;
218 : }
219 3512955 : 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 3512955 : init = initial_condition_in_loop_num (chrec, l->num);
229 3512955 : step = evolution_part_in_loop_num (chrec, l->num);
230 3512955 : if (!init || !step)
231 : {
232 0 : r.set_varying (type);
233 0 : return false;
234 : }
235 3512955 : dir = scev_direction (chrec);
236 3512955 : if (dir == EV_DIR_UNKNOWN
237 3512955 : || scev_probably_wraps_p (NULL, init, step, stmt,
238 : get_chrec_loop (chrec), true))
239 : {
240 505593 : r.set_varying (type);
241 505593 : 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 2567304 : induction_variable_may_overflow_p (tree type,
250 : const wide_int &step, const widest_int &nit)
251 : {
252 2567304 : wi::overflow_type ovf;
253 2567304 : signop sign = TYPE_SIGN (type);
254 2567304 : widest_int max_step = wi::mul (widest_int::from (step, sign),
255 2567304 : nit, sign, &ovf);
256 :
257 2567304 : 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 2447382 : return (sign == SIGNED
264 4894128 : && wi::gts_p (max_step, 0) != wi::gts_p (step, 0));
265 2567304 : }
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 3007050 : range_from_loop_direction (irange &r, tree type,
272 : const irange &begin, const irange &end,
273 : ev_direction dir)
274 : {
275 3007050 : signop sign = TYPE_SIGN (type);
276 :
277 3007050 : if (begin.undefined_p () || end.undefined_p ())
278 3863 : r.set_varying (type);
279 3003187 : else if (dir == EV_DIR_GROWS)
280 : {
281 2716322 : if (wi::ge_p (begin.lower_bound (), end.upper_bound (), sign))
282 237 : r.set_varying (type);
283 : else
284 2716085 : r = int_range<1> (type, begin.lower_bound (), end.upper_bound ());
285 : }
286 : else
287 : {
288 286895 : if (wi::ge_p (end.lower_bound (), begin.upper_bound (), sign))
289 242 : r.set_varying (type);
290 : else
291 286653 : r = int_range<1> (type, end.lower_bound (), begin.upper_bound ());
292 : }
293 3007050 : }
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 6492967 : range_of_var_in_loop (vrange &v, tree name, class loop *l, gimple *stmt,
300 : range_query *query)
301 : {
302 6492967 : tree init, step;
303 6492967 : enum ev_direction dir;
304 6492967 : if (!get_scev_info (v, name, stmt, l, init, step, dir))
305 : return true;
306 :
307 : // Calculate ranges for the values from SCEV.
308 3007362 : irange &r = as_a <irange> (v);
309 3007362 : tree type = TREE_TYPE (init);
310 3007362 : int_range<2> rinit (type), rstep (type), max_init (type);
311 3007362 : if (!query->range_of_expr (rinit, init, stmt)
312 3007362 : || !query->range_of_expr (rstep, step, stmt))
313 : return false;
314 :
315 : // Calculate the final range of NAME if possible.
316 3007362 : if (rinit.singleton_p () && rstep.singleton_p ())
317 : {
318 2567616 : widest_int nit;
319 2567616 : if (!max_loop_iterations (l, &nit))
320 : return false;
321 :
322 2567306 : if (!induction_variable_may_overflow_p (type, rstep.lower_bound (), nit))
323 : {
324 : // Calculate the max bounds for init (init + niter * step).
325 2446746 : wide_int w = wide_int::from (nit, TYPE_PRECISION (type), TYPE_SIGN (type));
326 2446746 : int_range<1> niter (type, w, w);
327 2446746 : int_range_max max_step;
328 2446746 : range_op_handler mult_handler (MULT_EXPR);
329 2446746 : range_op_handler plus_handler (PLUS_EXPR);
330 2446746 : if (!mult_handler.fold_range (max_step, type, niter, rstep)
331 2446746 : || !plus_handler.fold_range (max_init, type, rinit, max_step))
332 0 : return false;
333 2446748 : }
334 2567616 : }
335 3007050 : range_from_loop_direction (r, type, rinit, max_init, dir);
336 3007050 : return true;
337 3007362 : }
338 :
339 : /* Helper function for vrp_evaluate_conditional_warnv & other
340 : optimizers. */
341 :
342 : tree
343 21057526 : simplify_using_ranges::fold_cond_with_ops (enum tree_code code,
344 : tree op0, tree op1, gimple *s)
345 : {
346 21057526 : value_range r0 (TREE_TYPE (op0));
347 21057526 : value_range r1 (TREE_TYPE (op1));
348 21057526 : if (!query->range_of_expr (r0, op0, s)
349 21057526 : || !query->range_of_expr (r1, op1, s))
350 : return NULL_TREE;
351 :
352 21050124 : int_range<1> res;
353 21050124 : range_op_handler handler (code);
354 :
355 : // Find any relation between op0 and op1 and pass it to fold_range.
356 21050124 : relation_kind rel = VREL_VARYING;
357 21050124 : if (gimple_range_ssa_p (op0) && gimple_range_ssa_p (op1))
358 4647831 : rel = query->relation ().query (s, op0, op1);
359 :
360 21050124 : if (handler && handler.fold_range (res, boolean_type_node, r0, r1,
361 : relation_trio::op1_op2 (rel)))
362 : {
363 21050124 : if (res == range_true ())
364 8693 : return boolean_true_node;
365 21041431 : if (res == range_false ())
366 5006 : return boolean_false_node;
367 : }
368 : return NULL;
369 21057526 : }
370 :
371 : /* Helper function for legacy_fold_cond. */
372 :
373 : tree
374 21383927 : simplify_using_ranges::legacy_fold_cond_overflow (gimple *stmt)
375 : {
376 21383927 : tree ret;
377 21383927 : tree_code code = gimple_cond_code (stmt);
378 21383927 : tree op0 = gimple_cond_lhs (stmt);
379 21383927 : tree op1 = gimple_cond_rhs (stmt);
380 :
381 : /* We only deal with integral and pointer types. */
382 42406888 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
383 26140726 : && !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 20526572 : tree x;
395 20526572 : if (overflow_comparison_p (code, op0, op1, &x))
396 : {
397 1201 : 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 1201 : if (integer_zerop (x))
403 : {
404 207 : op1 = x;
405 207 : 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 994 : else if (wi::to_wide (x) == max - 1)
412 : {
413 652 : op0 = op1;
414 652 : op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
415 652 : 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 124 : vro.set (type,
424 248 : wi::to_wide (TYPE_MIN_VALUE (type)),
425 124 : wi::to_wide (x), VR_ANTI_RANGE);
426 124 : vri.set (type,
427 248 : wi::to_wide (TYPE_MIN_VALUE (type)),
428 248 : wi::to_wide (x));
429 : }
430 218 : else if (code == LT_EXPR || code == LE_EXPR)
431 : {
432 218 : vro.set (type,
433 436 : wi::to_wide (TYPE_MIN_VALUE (type)),
434 218 : wi::to_wide (x));
435 218 : vri.set (type,
436 436 : wi::to_wide (TYPE_MIN_VALUE (type)),
437 436 : 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 1201 : }
463 :
464 20526564 : 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 21383927 : simplify_using_ranges::legacy_fold_cond (gcond *stmt, edge *taken_edge_p)
475 : {
476 21383927 : tree val;
477 :
478 21383927 : *taken_edge_p = NULL;
479 :
480 21383927 : 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 21383927 : val = legacy_fold_cond_overflow (stmt);
503 21383927 : if (val)
504 9 : *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
505 :
506 21383927 : 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 21383927 : }
515 :
516 : /* Simplify boolean operations if the source is known
517 : to be already a boolean. */
518 : bool
519 515828 : simplify_using_ranges::simplify_truth_ops_using_ranges
520 : (gimple_stmt_iterator *gsi,
521 : gimple *stmt)
522 : {
523 515828 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
524 515828 : tree lhs, op0, op1;
525 515828 : bool need_conversion;
526 :
527 : /* We handle only !=/== case here. */
528 515828 : gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
529 :
530 515828 : op0 = gimple_assign_rhs1 (stmt);
531 515828 : if (!op_with_boolean_value_range_p (op0, stmt))
532 : return false;
533 :
534 17559 : op1 = gimple_assign_rhs2 (stmt);
535 17559 : 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 17035 : if (rhs_code == EQ_EXPR)
541 : {
542 8809 : if (TREE_CODE (op1) == INTEGER_CST)
543 3617 : op1 = int_const_binop (BIT_XOR_EXPR, op1,
544 7234 : build_int_cst (TREE_TYPE (op1), 1));
545 : else
546 : return false;
547 : }
548 :
549 11843 : lhs = gimple_assign_lhs (stmt);
550 11843 : need_conversion
551 11843 : = !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 11843 : if (need_conversion
555 10413 : && !TYPE_UNSIGNED (TREE_TYPE (op0))
556 3893 : && TYPE_PRECISION (TREE_TYPE (op0)) == 1
557 11872 : && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
558 : return false;
559 :
560 : /* For A != 0 we can substitute A itself. */
561 11843 : if (integer_zerop (op1))
562 7030 : 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 4813 : else if (need_conversion)
567 : {
568 3383 : tree tem = make_ssa_name (TREE_TYPE (op0));
569 3383 : gassign *newop
570 3383 : = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
571 3383 : gsi_insert_before (gsi, newop, GSI_SAME_STMT);
572 6740 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
573 6740 : && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
574 : {
575 6546 : int_range<1> vr (TREE_TYPE (tem),
576 6546 : wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
577 6546 : wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
578 3273 : set_range_info (tem, vr);
579 3273 : }
580 3383 : gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
581 : }
582 : /* Or without. */
583 : else
584 1430 : gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
585 11843 : update_stmt (gsi_stmt (*gsi));
586 11843 : fold_stmt (gsi, follow_single_use_edges);
587 :
588 11843 : 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 335120 : simplify_using_ranges::simplify_div_or_mod_using_ranges
601 : (gimple_stmt_iterator *gsi,
602 : gimple *stmt)
603 : {
604 335120 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
605 335120 : tree val = NULL;
606 335120 : tree op0 = gimple_assign_rhs1 (stmt);
607 335120 : tree op1 = gimple_assign_rhs2 (stmt);
608 335120 : tree op0min = NULL_TREE, op0max = NULL_TREE;
609 335120 : tree op1min = op1;
610 335120 : int_range_max vr;
611 :
612 335120 : if (TREE_CODE (op0) == INTEGER_CST)
613 : {
614 : op0min = op0;
615 : op0max = op0;
616 : }
617 : else
618 : {
619 309034 : if (!query->range_of_expr (vr, op0, stmt))
620 0 : vr.set_varying (TREE_TYPE (op0));
621 309034 : if (!vr.varying_p () && !vr.undefined_p ())
622 : {
623 153717 : tree type = vr.type ();
624 153717 : op0min = wide_int_to_tree (type, vr.lower_bound ());
625 153726 : op0max = wide_int_to_tree (type, vr.upper_bound ());
626 : }
627 : }
628 :
629 335120 : if (rhs_code == TRUNC_MOD_EXPR
630 158157 : && TREE_CODE (op1) == SSA_NAME)
631 : {
632 103000 : int_range_max vr1;
633 103000 : if (!query->range_of_expr (vr1, op1, stmt))
634 0 : vr1.set_varying (TREE_TYPE (op1));
635 103000 : if (!vr1.varying_p () && !vr1.undefined_p ())
636 26991 : op1min = wide_int_to_tree (vr1.type (), vr1.lower_bound ());
637 103000 : }
638 158157 : if (rhs_code == TRUNC_MOD_EXPR
639 158157 : && TREE_CODE (op1min) == INTEGER_CST
640 82144 : && tree_int_cst_sgn (op1min) == 1
641 60600 : && op0max
642 35029 : && tree_int_cst_lt (op0max, op1min))
643 : {
644 1209 : if (TYPE_UNSIGNED (TREE_TYPE (op0))
645 529 : || tree_int_cst_sgn (op0min) >= 0
646 1437 : || 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 1174 : gimple_assign_set_rhs_from_tree (gsi, op0);
652 1174 : return true;
653 : }
654 : }
655 :
656 333946 : if (TREE_CODE (op0) != SSA_NAME)
657 : return false;
658 :
659 307861 : 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 268509 : if (rhs_code == TRUNC_MOD_EXPR
666 268509 : && fold_stmt (gsi, follow_single_use_edges))
667 12 : return true;
668 : return false;
669 : }
670 :
671 39352 : if (TYPE_UNSIGNED (TREE_TYPE (op0)))
672 0 : val = integer_one_node;
673 : else
674 : {
675 39352 : tree zero = build_zero_cst (TREE_TYPE (op0));
676 39352 : val = fold_cond_with_ops (GE_EXPR, op0, zero, stmt);
677 : }
678 :
679 39352 : if (val && integer_onep (val))
680 : {
681 6474 : tree t;
682 :
683 6474 : if (rhs_code == TRUNC_DIV_EXPR)
684 : {
685 4214 : t = build_int_cst (integer_type_node, tree_log2 (op1));
686 4214 : gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
687 4214 : gimple_assign_set_rhs1 (stmt, op0);
688 4214 : gimple_assign_set_rhs2 (stmt, t);
689 : }
690 : else
691 : {
692 2260 : t = build_int_cst (TREE_TYPE (op1), 1);
693 2260 : t = int_const_binop (MINUS_EXPR, op1, t);
694 2260 : t = fold_convert (TREE_TYPE (op0), t);
695 :
696 2260 : gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
697 2260 : gimple_assign_set_rhs1 (stmt, op0);
698 2260 : gimple_assign_set_rhs2 (stmt, t);
699 : }
700 :
701 6474 : update_stmt (stmt);
702 6474 : fold_stmt (gsi, follow_single_use_edges);
703 6474 : return true;
704 : }
705 :
706 : return false;
707 335120 : }
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 239259 : simplify_using_ranges::simplify_min_or_max_using_ranges
714 : (gimple_stmt_iterator *gsi,
715 : gimple *stmt)
716 : {
717 239259 : tree op0 = gimple_assign_rhs1 (stmt);
718 239259 : tree op1 = gimple_assign_rhs2 (stmt);
719 239259 : tree val;
720 :
721 239259 : val = fold_cond_with_ops (LE_EXPR, op0, op1, stmt);
722 239259 : if (!val)
723 233374 : val = fold_cond_with_ops (LT_EXPR, op0, op1, stmt);
724 :
725 233374 : if (val)
726 : {
727 : /* VAL == TRUE -> OP0 < or <= op1
728 : VAL == FALSE -> OP0 > or >= op1. */
729 6899 : tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
730 6899 : == integer_zerop (val)) ? op0 : op1;
731 6899 : gimple_assign_set_rhs_from_tree (gsi, res);
732 6899 : 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 9598 : simplify_using_ranges::simplify_abs_using_ranges (gimple_stmt_iterator *gsi,
744 : gimple *stmt)
745 : {
746 9598 : tree op = gimple_assign_rhs1 (stmt);
747 9598 : tree zero = build_zero_cst (TREE_TYPE (op));
748 9598 : tree val = fold_cond_with_ops (LE_EXPR, op, zero, stmt);
749 :
750 9598 : if (!val)
751 : {
752 : /* The range is neither <= 0 nor > 0. Now see if it is
753 : either < 0 or >= 0. */
754 9379 : val = fold_cond_with_ops (LT_EXPR, op, zero, stmt);
755 : }
756 9379 : if (val)
757 : {
758 324 : gimple_assign_set_rhs1 (stmt, op);
759 324 : if (integer_zerop (val))
760 146 : gimple_assign_set_rhs_code (stmt, SSA_NAME);
761 : else
762 178 : gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
763 324 : update_stmt (stmt);
764 324 : fold_stmt (gsi, follow_single_use_edges);
765 324 : 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 1641665 : 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 1641665 : if (vr->varying_p () || vr->undefined_p ())
782 : {
783 968643 : *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
784 968643 : *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
785 968643 : return false;
786 : }
787 673024 : wi_set_zero_nonzero_bits (expr_type, vr->lower_bound (), vr->upper_bound (),
788 : *may_be_nonzero, *must_be_nonzero);
789 673022 : 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 1284122 : simplify_using_ranges::simplify_bit_ops_using_ranges
800 : (gimple_stmt_iterator *gsi,
801 : gimple *stmt)
802 : {
803 1284122 : tree op0 = gimple_assign_rhs1 (stmt);
804 1284122 : tree op1 = gimple_assign_rhs2 (stmt);
805 1284122 : tree op = NULL_TREE;
806 1284122 : int_range_max vr0, vr1;
807 1284122 : wide_int may_be_nonzero0, may_be_nonzero1;
808 1284122 : wide_int must_be_nonzero0, must_be_nonzero1;
809 1284122 : wide_int mask;
810 :
811 1284122 : if (!query->range_of_expr (vr0, op0, stmt)
812 1284122 : || vr0.undefined_p ())
813 : return false;
814 1283443 : if (!query->range_of_expr (vr1, op1, stmt)
815 1283443 : || vr1.undefined_p ())
816 : return false;
817 :
818 1283098 : if (!vr_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
819 : &must_be_nonzero0))
820 : return false;
821 358567 : if (!vr_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
822 : &must_be_nonzero1))
823 : return false;
824 :
825 314455 : switch (gimple_assign_rhs_code (stmt))
826 : {
827 222000 : case BIT_AND_EXPR:
828 222000 : mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
829 222000 : if (mask == 0)
830 : {
831 : op = op0;
832 : break;
833 : }
834 217384 : mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
835 217384 : if (mask == 0)
836 : {
837 : op = op1;
838 : break;
839 : }
840 : break;
841 92455 : case BIT_IOR_EXPR:
842 92455 : mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
843 92455 : if (mask == 0)
844 : {
845 : op = op1;
846 : break;
847 : }
848 92455 : mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
849 92455 : if (mask == 0)
850 : {
851 : op = op0;
852 : break;
853 : }
854 : break;
855 0 : default:
856 0 : gcc_unreachable ();
857 : }
858 :
859 4812 : if (op == NULL_TREE)
860 : return false;
861 :
862 4812 : gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
863 4812 : update_stmt (gsi_stmt (*gsi));
864 4812 : return true;
865 1284134 : }
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 1661766 : test_for_singularity (enum tree_code cond_code, tree op0,
875 : tree op1, const irange *vr)
876 : {
877 1661766 : tree min = NULL;
878 1661766 : tree max = NULL;
879 :
880 : /* Extract minimum/maximum values which satisfy the conditional as it was
881 : written. */
882 1661766 : if (cond_code == LE_EXPR || cond_code == LT_EXPR)
883 : {
884 727483 : min = TYPE_MIN_VALUE (TREE_TYPE (op0));
885 :
886 727483 : max = op1;
887 727483 : if (cond_code == LT_EXPR)
888 : {
889 92403 : tree one = build_int_cst (TREE_TYPE (op0), 1);
890 92403 : max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
891 : }
892 : }
893 934283 : else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
894 : {
895 797042 : max = TYPE_MAX_VALUE (TREE_TYPE (op0));
896 :
897 797042 : min = op1;
898 797042 : if (cond_code == GT_EXPR)
899 : {
900 706261 : tree one = build_int_cst (TREE_TYPE (op0), 1);
901 706261 : 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 1661766 : if (min && max)
908 : {
909 1524525 : tree type = TREE_TYPE (op0);
910 1524525 : tree tmin = wide_int_to_tree (type, vr->lower_bound ());
911 1524525 : tree tmax = wide_int_to_tree (type, vr->upper_bound ());
912 1524525 : if (compare_values (tmin, min) == 1)
913 461669 : min = tmin;
914 1524525 : if (compare_values (tmax, max) == -1)
915 573494 : 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 1524525 : if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
921 316924 : 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 266514 : range_fits_type_p (const irange *vr,
931 : unsigned dest_precision, signop dest_sgn)
932 : {
933 266514 : tree src_type;
934 266514 : unsigned src_precision;
935 266514 : widest_int tem;
936 266514 : signop src_sgn;
937 :
938 : /* Now we can only handle ranges with constant bounds. */
939 266514 : if (vr->undefined_p () || vr->varying_p ())
940 : return false;
941 :
942 : /* We can only handle integral and pointer types. */
943 215502 : src_type = vr->type ();
944 215502 : 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 215502 : src_precision = TYPE_PRECISION (src_type);
951 215502 : src_sgn = TYPE_SIGN (src_type);
952 215502 : if ((src_precision < dest_precision
953 15506 : && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
954 201412 : || (src_precision == dest_precision && src_sgn == dest_sgn))
955 : return true;
956 :
957 201260 : wide_int vrmin = vr->lower_bound ();
958 201260 : 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 201260 : if (src_sgn != dest_sgn
965 201260 : && (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 126218 : signop sign = TYPE_SIGN (vr->type ());
971 126218 : tem = wi::ext (widest_int::from (vrmin, sign), dest_precision, dest_sgn);
972 126218 : if (tem != widest_int::from (vrmin, sign))
973 : return false;
974 120598 : tem = wi::ext (widest_int::from (vrmax, sign), dest_precision, dest_sgn);
975 120598 : if (tem != widest_int::from (vrmax, sign))
976 : return false;
977 :
978 : return true;
979 201260 : }
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 319791 : 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 319791 : if ((e->flags & m_not_executable_flag) == m_not_executable_flag)
990 225390 : return;
991 :
992 209900 : e->flags |= m_not_executable_flag;
993 209900 : m_flag_set_edges.safe_push (e);
994 :
995 : // Check if the destination block needs to propagate the property.
996 209900 : basic_block bb = e->dest;
997 :
998 : // If any incoming edge is executable, we are done.
999 209900 : edge_iterator ei;
1000 345853 : FOR_EACH_EDGE (e, ei, bb->preds)
1001 251452 : 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 159852 : FOR_EACH_EDGE (e, ei, bb->succs)
1006 65451 : 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 21725338 : simplify_using_ranges::fold_cond (gcond *cond)
1014 : {
1015 21725338 : int_range_max r;
1016 21725338 : if (query->range_of_stmt (r, cond) && r.singleton_p ())
1017 : {
1018 : // COND has already been folded if arguments are constant.
1019 341411 : if (TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
1020 341411 : && TREE_CODE (gimple_cond_rhs (cond)) != SSA_NAME)
1021 : return false;
1022 247255 : if (dump_file)
1023 : {
1024 186 : fprintf (dump_file, "Folding predicate ");
1025 186 : print_gimple_expr (dump_file, cond, 0);
1026 186 : fprintf (dump_file, " to ");
1027 : }
1028 247255 : edge e0 = EDGE_SUCC (gimple_bb (cond), 0);
1029 247255 : edge e1 = EDGE_SUCC (gimple_bb (cond), 1);
1030 247255 : if (r.zero_p ())
1031 : {
1032 150682 : if (dump_file)
1033 134 : fprintf (dump_file, "0\n");
1034 150682 : gimple_cond_make_false (cond);
1035 150682 : if (e0->flags & EDGE_TRUE_VALUE)
1036 148923 : set_and_propagate_unexecutable (e0);
1037 : else
1038 1759 : set_and_propagate_unexecutable (e1);
1039 : }
1040 : else
1041 : {
1042 96573 : if (dump_file)
1043 52 : fprintf (dump_file, "1\n");
1044 96573 : gimple_cond_make_true (cond);
1045 96573 : if (e0->flags & EDGE_FALSE_VALUE)
1046 703 : set_and_propagate_unexecutable (e0);
1047 : else
1048 95870 : set_and_propagate_unexecutable (e1);
1049 : }
1050 247255 : update_stmt (cond);
1051 247255 : return true;
1052 : }
1053 :
1054 : // FIXME: Audit the code below and make sure it never finds anything.
1055 21383927 : edge taken_edge;
1056 21383927 : legacy_fold_cond (cond, &taken_edge);
1057 :
1058 21383927 : 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 21725338 : }
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 12552552 : simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt)
1086 : {
1087 12552552 : tree op0 = gimple_cond_lhs (stmt);
1088 12552552 : tree op1 = gimple_cond_rhs (stmt);
1089 12552552 : enum tree_code cond_code = gimple_cond_code (stmt);
1090 :
1091 12552552 : if (fold_cond (stmt))
1092 : return true;
1093 :
1094 12414838 : if (simplify_compare_using_ranges_1 (cond_code, op0, op1, stmt))
1095 : {
1096 381463 : 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 381463 : gimple_cond_set_code (stmt, cond_code);
1104 381463 : gimple_cond_set_lhs (stmt, op0);
1105 381463 : gimple_cond_set_rhs (stmt, op1);
1106 :
1107 381463 : update_stmt (stmt);
1108 :
1109 381463 : 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 1317581 : simplify_using_ranges::simplify_compare_assign_using_ranges_1
1124 : (gimple_stmt_iterator *gsi,
1125 : gimple *stmt)
1126 : {
1127 1317581 : enum tree_code code = gimple_assign_rhs_code (stmt);
1128 1317581 : tree op0 = gimple_assign_rhs1 (stmt);
1129 1317581 : tree op1 = gimple_assign_rhs2 (stmt);
1130 1317581 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1131 1317581 : bool happened = false;
1132 :
1133 1317581 : if (simplify_compare_using_ranges_1 (code, op0, op1, stmt))
1134 : {
1135 6894 : 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 6894 : gimple_assign_set_rhs_code (stmt, code);
1143 6894 : gimple_assign_set_rhs1 (stmt, op0);
1144 6894 : gimple_assign_set_rhs2 (stmt, op1);
1145 :
1146 6894 : update_stmt (stmt);
1147 :
1148 6894 : 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 1317581 : if ((code == EQ_EXPR || code == NE_EXPR)
1160 735815 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
1161 1833409 : && simplify_truth_ops_using_ranges (gsi, stmt))
1162 : happened = true;
1163 :
1164 1317581 : 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 13732419 : simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tree &op0, tree &op1, gimple *stmt)
1173 : {
1174 13732419 : bool happened = false;
1175 13732419 : if (cond_code != NE_EXPR
1176 13732419 : && cond_code != EQ_EXPR
1177 3580085 : && TREE_CODE (op0) == SSA_NAME
1178 3579609 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
1179 16954528 : && is_gimple_min_invariant (op1))
1180 : {
1181 1958294 : int_range_max vr;
1182 :
1183 1958294 : 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 1958294 : if (!vr.undefined_p () && !vr.varying_p ())
1189 : {
1190 830883 : tree new_tree = test_for_singularity (cond_code, op0, op1, &vr);
1191 830883 : if (new_tree)
1192 : {
1193 137241 : cond_code = EQ_EXPR;
1194 137241 : op1 = new_tree;
1195 137241 : 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 830883 : new_tree = test_for_singularity
1202 830883 : (invert_tree_comparison (cond_code, false),
1203 : op0, op1, &vr);
1204 830883 : if (new_tree)
1205 : {
1206 179683 : cond_code = NE_EXPR;
1207 179683 : op1 = new_tree;
1208 179683 : happened = true;
1209 : }
1210 : }
1211 1958294 : }
1212 : // Try to simplify casted conditions.
1213 13732419 : if (simplify_casted_compare (cond_code, op0, op1))
1214 84065 : happened = true;
1215 13732419 : 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 13732419 : 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 13732419 : if (TREE_CODE (op0) == SSA_NAME
1235 13636701 : && TREE_CODE (op1) == INTEGER_CST)
1236 : {
1237 9607258 : gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
1238 9607258 : tree innerop;
1239 :
1240 9607258 : if (!is_gimple_assign (def_stmt))
1241 : return false;
1242 :
1243 5928061 : switch (gimple_assign_rhs_code (def_stmt))
1244 : {
1245 503743 : CASE_CONVERT:
1246 503743 : innerop = gimple_assign_rhs1 (def_stmt);
1247 503743 : break;
1248 52005 : case VIEW_CONVERT_EXPR:
1249 52005 : innerop = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
1250 52005 : if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
1251 : return false;
1252 : break;
1253 : default:
1254 : return false;
1255 : }
1256 :
1257 552916 : if (TREE_CODE (innerop) == SSA_NAME
1258 552888 : && !POINTER_TYPE_P (TREE_TYPE (innerop))
1259 547317 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
1260 1100220 : && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
1261 : {
1262 517670 : int_range_max vr;
1263 :
1264 517670 : if (query->range_of_expr (vr, innerop)
1265 517667 : && !vr.varying_p ()
1266 174075 : && !vr.undefined_p ()
1267 173953 : && range_fits_type_p (&vr,
1268 173953 : TYPE_PRECISION (TREE_TYPE (op0)),
1269 173953 : TYPE_SIGN (TREE_TYPE (op0)))
1270 601735 : && int_fits_type_p (op1, TREE_TYPE (innerop)))
1271 : {
1272 84065 : tree newconst = fold_convert (TREE_TYPE (innerop), op1);
1273 84065 : op0 = innerop;
1274 84065 : op1 = newconst;
1275 84065 : return true;
1276 : }
1277 517670 : }
1278 : }
1279 : return false;
1280 : }
1281 :
1282 : /* Simplify a switch statement using the value range of the switch
1283 : argument. */
1284 :
1285 : bool
1286 62148 : simplify_using_ranges::simplify_switch_using_ranges (gswitch *stmt)
1287 : {
1288 62148 : tree op = gimple_switch_index (stmt);
1289 62148 : tree type = TREE_TYPE (op);
1290 62148 : int_range_max op_range (type);
1291 62148 : int_range_max default_range (type);
1292 62148 : auto_vec<unsigned> cases;
1293 62148 : cases.truncate (0);
1294 62148 : edge e;
1295 62148 : switch_update su;
1296 :
1297 : // Abort if we don't have a useful range for the switch index.
1298 62148 : if (!query->range_of_expr (op_range, op, stmt)
1299 62148 : || op_range.varying_p () || op_range.undefined_p ())
1300 : return false;
1301 :
1302 : // Default range starts with full known range of op.
1303 16080 : default_range = op_range;
1304 16080 : edge default_edge = gimple_switch_default_edge (cfun, stmt);
1305 :
1306 16080 : unsigned x, lim = gimple_switch_num_labels (stmt);
1307 96000 : for (x = 1; x < lim; x++)
1308 : {
1309 80972 : e = gimple_switch_edge (cfun, stmt, x);
1310 80972 : tree label = gimple_switch_label (stmt, x);
1311 :
1312 : // If this edge is the same as the default edge, do nothing else.
1313 80972 : if (e == default_edge)
1314 6231 : continue;
1315 : // Ada sometimes has mismatched labels and index. Just bail.
1316 80966 : if (TREE_TYPE (CASE_LOW (label)) != type)
1317 1052 : return false;
1318 :
1319 79916 : wide_int low = wi::to_wide (CASE_LOW (label));
1320 79916 : wide_int high;
1321 : // Singleton cases have no CASE_HIGH.
1322 79916 : tree tree_high = CASE_HIGH (label);
1323 79916 : if (tree_high)
1324 3376 : high = wi::to_wide (tree_high);
1325 : else
1326 76540 : 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 79916 : int_range_max case_range (type, low, high);
1331 79916 : if (case_range.intersect (op_range))
1332 : {
1333 : // If none of the label is in op_range, skip this label.
1334 50960 : if (case_range.undefined_p ())
1335 6225 : 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 44735 : if (case_range.lower_bound () != low)
1341 13 : CASE_LOW (label) = wide_int_to_tree (type,
1342 26 : case_range.lower_bound ());
1343 44735 : if (case_range.singleton_p ())
1344 43257 : CASE_HIGH (label) = NULL_TREE;
1345 : else
1346 1478 : 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 73691 : cases.safe_push (x);
1352 : // Remove case_range from needing to be handled by the default.
1353 73691 : if (!case_range.invert ())
1354 2 : return false;
1355 73689 : default_range.intersect (case_range);
1356 79916 : }
1357 :
1358 : // An undefined DEFAULT range means the current default case is not needed.
1359 15028 : unsigned idx = default_range.undefined_p () ? 0 : 1;
1360 15028 : unsigned vec_size = cases.length () + idx;
1361 15028 : if (vec_size == lim)
1362 : return false;
1363 :
1364 4102 : tree vec2 = make_tree_vec (vec_size);
1365 : // Add default label if there is one.
1366 4102 : if (idx)
1367 : {
1368 3108 : TREE_VEC_ELT (vec2, 0) = gimple_switch_default_label (stmt);
1369 3108 : e = gimple_switch_edge (cfun, stmt, 0);
1370 3108 : e->aux = (void *)-1;
1371 : }
1372 :
1373 16877 : for (x = 0; x < cases.length (); x++)
1374 : {
1375 12775 : unsigned swi = cases[x];
1376 12775 : TREE_VEC_ELT (vec2, idx++) = gimple_switch_label (stmt, swi);
1377 12775 : e = gimple_switch_edge (cfun, stmt, swi);
1378 12775 : e->aux = (void *)-1;
1379 : }
1380 :
1381 : /* Queue not needed edges for later removal. */
1382 4102 : edge_iterator ei;
1383 26170 : FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1384 : {
1385 22068 : if (e->aux == (void *)-1)
1386 : {
1387 14983 : e->aux = NULL;
1388 14983 : continue;
1389 : }
1390 :
1391 7085 : if (dump_file && (dump_flags & TDF_DETAILS))
1392 : {
1393 0 : fprintf (dump_file, "removing unreachable case label\n");
1394 : }
1395 7085 : to_remove_edges.safe_push (e);
1396 7085 : set_and_propagate_unexecutable (e);
1397 7085 : e->flags &= ~EDGE_EXECUTABLE;
1398 7085 : e->flags |= EDGE_IGNORE;
1399 : }
1400 :
1401 : /* And queue an update for the stmt. */
1402 4102 : su.stmt = stmt;
1403 4102 : su.vec = vec2;
1404 4102 : to_update_switch_stmts.safe_push (su);
1405 4102 : return true;
1406 62148 : }
1407 :
1408 : void
1409 13568073 : simplify_using_ranges::cleanup_edges_and_switches (void)
1410 : {
1411 13568073 : int i;
1412 13568073 : edge e;
1413 13568073 : switch_update *su;
1414 :
1415 : /* Clear any edges marked as not executable. */
1416 13568073 : if (m_not_executable_flag)
1417 : {
1418 4605178 : FOR_EACH_VEC_ELT (m_flag_set_edges, i, e)
1419 209900 : 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 13578984 : FOR_EACH_VEC_ELT (to_remove_edges, i, e)
1424 7085 : remove_edge (e);
1425 :
1426 : /* Update SWITCH_EXPR case label vector. */
1427 13572175 : FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
1428 : {
1429 4102 : size_t j;
1430 4102 : size_t n = TREE_VEC_LENGTH (su->vec);
1431 4102 : tree label;
1432 4102 : gimple_switch_set_num_labels (su->stmt, n);
1433 24087 : for (j = 0; j < n; j++)
1434 15883 : 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 4102 : label = gimple_switch_label (su->stmt, 0);
1439 4102 : CASE_LOW (label) = NULL_TREE;
1440 4102 : CASE_HIGH (label) = NULL_TREE;
1441 : }
1442 :
1443 13568073 : if (!to_remove_edges.is_empty ())
1444 : {
1445 3826 : free_dominance_info (CDI_DOMINATORS);
1446 3826 : loops_state_set (LOOPS_NEED_FIXUP);
1447 : }
1448 :
1449 13568073 : to_remove_edges.release ();
1450 13568073 : to_update_switch_stmts.release ();
1451 13568073 : }
1452 :
1453 : /* Simplify an integral conversion from an SSA name in STMT. */
1454 :
1455 : static bool
1456 4923526 : simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
1457 : {
1458 4923526 : tree innerop, middleop, finaltype;
1459 4923526 : gimple *def_stmt;
1460 4923526 : signop inner_sgn, middle_sgn, final_sgn;
1461 4923526 : unsigned inner_prec, middle_prec, final_prec;
1462 4923526 : widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
1463 :
1464 4923526 : finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
1465 4923526 : if (!INTEGRAL_TYPE_P (finaltype))
1466 : return false;
1467 4523501 : middleop = gimple_assign_rhs1 (stmt);
1468 4523501 : def_stmt = SSA_NAME_DEF_STMT (middleop);
1469 4523501 : if (!is_gimple_assign (def_stmt)
1470 4523501 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
1471 : return false;
1472 152087 : innerop = gimple_assign_rhs1 (def_stmt);
1473 152087 : if (TREE_CODE (innerop) != SSA_NAME
1474 152087 : || 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 148608 : wide_int imin, imax;
1480 148608 : int_range_max vr;
1481 148608 : if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
1482 : return false;
1483 246910 : get_range_query (cfun)->range_of_expr (vr, innerop, stmt);
1484 123455 : if (vr.undefined_p () || vr.varying_p ())
1485 : return false;
1486 69104 : innermin = widest_int::from (vr.lower_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
1487 69104 : 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 69104 : inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
1492 69104 : middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
1493 69104 : final_prec = TYPE_PRECISION (finaltype);
1494 :
1495 : /* If the first conversion is not injective, the second must not
1496 : be widening. */
1497 69104 : if (wi::gtu_p (innermax - innermin,
1498 138208 : wi::mask <widest_int> (middle_prec, false))
1499 69104 : && 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 57638 : inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
1504 57638 : if (inner_sgn == UNSIGNED)
1505 42649 : innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
1506 : else
1507 14989 : innermed = 0;
1508 57638 : if (wi::cmp (innermin, innermed, inner_sgn) >= 0
1509 57638 : || wi::cmp (innermed, innermax, inner_sgn) >= 0)
1510 41431 : innermed = innermin;
1511 :
1512 57638 : middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
1513 57638 : middlemin = wi::ext (innermin, middle_prec, middle_sgn);
1514 57638 : middlemed = wi::ext (innermed, middle_prec, middle_sgn);
1515 57638 : 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 57638 : final_sgn = TYPE_SIGN (finaltype);
1520 115276 : if (wi::ext (middlemin, final_prec, final_sgn)
1521 115276 : != wi::ext (innermin, final_prec, final_sgn)
1522 55140 : || wi::ext (middlemed, final_prec, final_sgn)
1523 223058 : != wi::ext (innermed, final_prec, final_sgn)
1524 104156 : || wi::ext (middlemax, final_prec, final_sgn)
1525 197192 : != wi::ext (innermax, final_prec, final_sgn))
1526 : return false;
1527 :
1528 45015 : gimple_assign_set_rhs1 (stmt, innerop);
1529 45015 : fold_stmt (gsi, follow_single_use_edges);
1530 45015 : return true;
1531 5072134 : }
1532 :
1533 : /* Simplify a conversion from integral SSA name to float in STMT. */
1534 :
1535 : bool
1536 256573 : simplify_using_ranges::simplify_float_conversion_using_ranges
1537 : (gimple_stmt_iterator *gsi,
1538 : gimple *stmt)
1539 : {
1540 256573 : tree rhs1 = gimple_assign_rhs1 (stmt);
1541 256573 : int_range_max vr;
1542 256573 : scalar_float_mode fltmode
1543 256573 : = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
1544 256573 : scalar_int_mode mode;
1545 256573 : tree tem;
1546 256573 : gassign *conv;
1547 :
1548 : /* We can only handle constant ranges. */
1549 256573 : if (!query->range_of_expr (vr, rhs1, stmt)
1550 256573 : || vr.varying_p ()
1551 358298 : || 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 202075 : if (BITINT_TYPE_P (TREE_TYPE (rhs1))
1557 101044 : && 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 101031 : scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
1561 101031 : if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
1562 5447 : && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
1563 104744 : && 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 99390 : else if (can_float_p (fltmode, rhs_mode,
1567 99390 : 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 4399 : mode = NARROWEST_INT_MODE;
1574 15811 : 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 15811 : if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
1580 15811 : && 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 31356 : if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
1586 15678 : || 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 1774 : tem = make_ssa_name (build_nonstandard_integer_type
1594 1774 : (GET_MODE_PRECISION (mode), 0));
1595 1774 : conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
1596 1774 : gsi_insert_before (gsi, conv, GSI_SAME_STMT);
1597 1774 : gimple_assign_set_rhs1 (stmt, tem);
1598 1774 : fold_stmt (gsi, follow_single_use_edges);
1599 :
1600 1774 : return true;
1601 256573 : }
1602 :
1603 : /* Simplify an internal fn call using ranges if possible. */
1604 :
1605 : bool
1606 608819 : simplify_using_ranges::simplify_internal_call_using_ranges
1607 : (gimple_stmt_iterator *gsi,
1608 : gimple *stmt)
1609 : {
1610 608819 : enum tree_code subcode;
1611 608819 : bool is_ubsan = false;
1612 608819 : bool ovf = false;
1613 608819 : switch (gimple_call_internal_fn (stmt))
1614 : {
1615 : case IFN_UBSAN_CHECK_ADD:
1616 : subcode = PLUS_EXPR;
1617 : is_ubsan = true;
1618 : break;
1619 2569 : case IFN_UBSAN_CHECK_SUB:
1620 2569 : subcode = MINUS_EXPR;
1621 2569 : is_ubsan = true;
1622 2569 : break;
1623 2138 : case IFN_UBSAN_CHECK_MUL:
1624 2138 : subcode = MULT_EXPR;
1625 2138 : is_ubsan = true;
1626 2138 : break;
1627 40776 : case IFN_ADD_OVERFLOW:
1628 40776 : subcode = PLUS_EXPR;
1629 40776 : break;
1630 49736 : case IFN_SUB_OVERFLOW:
1631 49736 : subcode = MINUS_EXPR;
1632 49736 : break;
1633 47122 : case IFN_MUL_OVERFLOW:
1634 47122 : subcode = MULT_EXPR;
1635 47122 : break;
1636 : default:
1637 : return false;
1638 : }
1639 :
1640 144900 : tree op0 = gimple_call_arg (stmt, 0);
1641 144900 : tree op1 = gimple_call_arg (stmt, 1);
1642 144900 : tree type;
1643 144900 : if (is_ubsan)
1644 : {
1645 7266 : type = TREE_TYPE (op0);
1646 7266 : if (VECTOR_TYPE_P (type))
1647 : return false;
1648 : }
1649 137634 : else if (gimple_call_lhs (stmt) == NULL_TREE)
1650 : return false;
1651 : else
1652 137634 : type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
1653 143974 : if (!check_for_binary_op_overflow (query, subcode, type, op0, op1, &ovf, stmt)
1654 143974 : || (is_ubsan && ovf))
1655 : return false;
1656 :
1657 3475 : gimple *g;
1658 3475 : location_t loc = gimple_location (stmt);
1659 3475 : if (is_ubsan)
1660 529 : g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
1661 : else
1662 : {
1663 2946 : tree utype = type;
1664 2946 : if (ovf
1665 1457 : || !useless_type_conversion_p (type, TREE_TYPE (op0))
1666 3667 : || !useless_type_conversion_p (type, TREE_TYPE (op1)))
1667 2514 : utype = unsigned_type_for (type);
1668 2946 : if (TREE_CODE (op0) == INTEGER_CST)
1669 1175 : op0 = fold_convert (utype, op0);
1670 1771 : else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
1671 : {
1672 746 : g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
1673 746 : gimple_set_location (g, loc);
1674 746 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1675 746 : op0 = gimple_assign_lhs (g);
1676 : }
1677 2946 : if (TREE_CODE (op1) == INTEGER_CST)
1678 1715 : op1 = fold_convert (utype, op1);
1679 1231 : else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
1680 : {
1681 22 : g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
1682 22 : gimple_set_location (g, loc);
1683 22 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1684 22 : op1 = gimple_assign_lhs (g);
1685 : }
1686 2946 : g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
1687 2946 : gimple_set_location (g, loc);
1688 2946 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1689 2946 : if (utype != type)
1690 : {
1691 1356 : g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
1692 : gimple_assign_lhs (g));
1693 1356 : gimple_set_location (g, loc);
1694 1356 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1695 : }
1696 2946 : g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
1697 : gimple_assign_lhs (g),
1698 2946 : build_int_cst (type, ovf));
1699 : }
1700 3475 : gimple_set_location (g, loc);
1701 3475 : gsi_replace (gsi, g, false);
1702 3475 : 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 485782 : simplify_using_ranges::two_valued_val_range_p (tree var, tree *a, tree *b,
1710 : gimple *s)
1711 : {
1712 485782 : int_range_max vr;
1713 485782 : if (!query->range_of_expr (vr, var, s))
1714 : return false;
1715 485782 : if (vr.varying_p () || vr.undefined_p ())
1716 : return false;
1717 :
1718 800924 : if ((vr.num_pairs () == 1 && vr.upper_bound () - vr.lower_bound () == 1)
1719 415448 : || (vr.num_pairs () == 2
1720 288239 : && vr.lower_bound (0) == vr.upper_bound (0)
1721 244199 : && vr.lower_bound (1) == vr.upper_bound (1)))
1722 : {
1723 7602 : *a = wide_int_to_tree (TREE_TYPE (var), vr.lower_bound ());
1724 7602 : *b = wide_int_to_tree (TREE_TYPE (var), vr.upper_bound ());
1725 7602 : return true;
1726 : }
1727 : return false;
1728 485782 : }
1729 :
1730 13568073 : simplify_using_ranges::simplify_using_ranges (range_query *query,
1731 : int not_executable_flag)
1732 13568073 : : query (query)
1733 : {
1734 13568073 : to_remove_edges = vNULL;
1735 13568073 : to_update_switch_stmts = vNULL;
1736 13568073 : m_not_executable_flag = not_executable_flag;
1737 13568073 : m_flag_set_edges = vNULL;
1738 13568073 : }
1739 :
1740 13568073 : simplify_using_ranges::~simplify_using_ranges ()
1741 : {
1742 13568073 : cleanup_edges_and_switches ();
1743 13568073 : m_flag_set_edges.release ();
1744 13568073 : }
1745 :
1746 : /* Simplify STMT using ranges if possible. */
1747 :
1748 : bool
1749 259333216 : simplify_using_ranges::simplify (gimple_stmt_iterator *gsi)
1750 : {
1751 259333216 : gcc_checking_assert (query);
1752 :
1753 259333216 : gimple *stmt = gsi_stmt (*gsi);
1754 259333216 : if (is_gimple_assign (stmt))
1755 : {
1756 69543633 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
1757 69543633 : tree rhs1 = gimple_assign_rhs1 (stmt);
1758 69543633 : tree rhs2 = gimple_assign_rhs2 (stmt);
1759 69543633 : tree lhs = gimple_assign_lhs (stmt);
1760 69543633 : tree val1 = NULL_TREE, val2 = NULL_TREE;
1761 69543633 : use_operand_p use_p;
1762 69543633 : 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 69543633 : if (TREE_CODE_CLASS (rhs_code) == tcc_binary
1777 14768546 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1778 10529148 : && ((TREE_CODE (rhs1) == INTEGER_CST
1779 191242 : && TREE_CODE (rhs2) == SSA_NAME)
1780 10338504 : || (TREE_CODE (rhs2) == INTEGER_CST
1781 6873415 : && TREE_CODE (rhs1) == SSA_NAME))
1782 7063461 : && single_imm_use (lhs, &use_p, &use_stmt)
1783 74978843 : && gimple_code (use_stmt) == GIMPLE_COND)
1784 :
1785 : {
1786 485782 : tree new_rhs1 = NULL_TREE;
1787 485782 : tree new_rhs2 = NULL_TREE;
1788 485782 : tree cmp_var = NULL_TREE;
1789 :
1790 485782 : if (TREE_CODE (rhs2) == SSA_NAME
1791 485782 : && two_valued_val_range_p (rhs2, &val1, &val2, stmt))
1792 : {
1793 : /* Optimize RHS1 OP [VAL1, VAL2]. */
1794 242 : new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
1795 242 : new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
1796 242 : cmp_var = rhs2;
1797 : }
1798 485540 : else if (TREE_CODE (rhs1) == SSA_NAME
1799 485540 : && two_valued_val_range_p (rhs1, &val1, &val2, stmt))
1800 : {
1801 : /* Optimize [VAL1, VAL2] OP RHS2. */
1802 7360 : new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
1803 7360 : new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
1804 7360 : 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 485782 : if (new_rhs1 && new_rhs2)
1810 : {
1811 7602 : tree cond = gimple_build (gsi, true, GSI_SAME_STMT,
1812 : UNKNOWN_LOCATION,
1813 : EQ_EXPR, boolean_type_node,
1814 : cmp_var, val1);
1815 7602 : gimple_assign_set_rhs_with_ops (gsi,
1816 : COND_EXPR, cond,
1817 : new_rhs1,
1818 : new_rhs2);
1819 7602 : update_stmt (gsi_stmt (*gsi));
1820 7602 : fold_stmt (gsi, follow_single_use_edges);
1821 8485950 : return true;
1822 : }
1823 : }
1824 :
1825 69536031 : if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
1826 1317581 : return simplify_compare_assign_using_ranges_1 (gsi, stmt);
1827 :
1828 68218450 : 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 336502 : case TRUNC_DIV_EXPR:
1838 336502 : case TRUNC_MOD_EXPR:
1839 336502 : if ((TREE_CODE (rhs1) == SSA_NAME
1840 336502 : || TREE_CODE (rhs1) == INTEGER_CST)
1841 336502 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1842 335120 : return simplify_div_or_mod_using_ranges (gsi, stmt);
1843 : break;
1844 :
1845 : /* Transform ABS (X) into X or -X as appropriate. */
1846 64909 : case ABS_EXPR:
1847 64909 : if (TREE_CODE (rhs1) == SSA_NAME
1848 64909 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1849 9598 : return simplify_abs_using_ranges (gsi, stmt);
1850 : break;
1851 :
1852 1322119 : case BIT_AND_EXPR:
1853 1322119 : 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 2557309 : if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1859 2519312 : && (TREE_CODE (TREE_TYPE (rhs1)) != BOOLEAN_TYPE
1860 320781 : || TYPE_PRECISION (TREE_TYPE (rhs1)) == 1))
1861 1284122 : return simplify_bit_ops_using_ranges (gsi, stmt);
1862 : break;
1863 :
1864 6040356 : CASE_CONVERT:
1865 6040356 : if (TREE_CODE (rhs1) == SSA_NAME
1866 6040356 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1867 4923526 : return simplify_conversion_using_ranges (gsi, stmt);
1868 : break;
1869 :
1870 258970 : case FLOAT_EXPR:
1871 258970 : if (TREE_CODE (rhs1) == SSA_NAME
1872 258970 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1873 256573 : return simplify_float_conversion_using_ranges (gsi, stmt);
1874 : break;
1875 :
1876 239259 : case MIN_EXPR:
1877 239259 : case MAX_EXPR:
1878 239259 : return simplify_min_or_max_using_ranges (gsi, stmt);
1879 :
1880 419435 : case RSHIFT_EXPR:
1881 419435 : {
1882 419435 : tree op0 = gimple_assign_rhs1 (stmt);
1883 419435 : tree type = TREE_TYPE (op0);
1884 419435 : int_range_max range;
1885 419435 : if (TYPE_SIGN (type) == SIGNED
1886 419435 : && query->range_of_expr (range, op0, stmt))
1887 : {
1888 112569 : unsigned prec = TYPE_PRECISION (TREE_TYPE (op0));
1889 225138 : int_range<2> nzm1 (type, wi::minus_one (prec), wi::zero (prec),
1890 112569 : VR_ANTI_RANGE);
1891 112569 : range.intersect (nzm1);
1892 : // If there are no ranges other than [-1, 0] remove the shift.
1893 112569 : if (range.undefined_p ())
1894 : {
1895 86 : gimple_assign_set_rhs_from_tree (gsi, op0);
1896 86 : return true;
1897 : }
1898 : return false;
1899 112569 : }
1900 306866 : break;
1901 419435 : }
1902 : default:
1903 : break;
1904 : }
1905 : }
1906 189789583 : else if (gimple_code (stmt) == GIMPLE_COND)
1907 12552552 : return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
1908 177237031 : else if (gimple_code (stmt) == GIMPLE_SWITCH)
1909 62148 : return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
1910 177174883 : else if (is_gimple_call (stmt)
1911 177174883 : && gimple_call_internal_p (stmt))
1912 608819 : return simplify_internal_call_using_ranges (gsi, stmt);
1913 :
1914 : return false;
1915 : }
|