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 528511 : simplify_using_ranges::op_with_boolean_value_range_p (tree op, gimple *s)
59 : {
60 528511 : if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
61 : return true;
62 :
63 514761 : if (integer_zerop (op)
64 514761 : || integer_onep (op))
65 9861 : return true;
66 :
67 504900 : 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 504900 : int_range_max vr;
73 504900 : return (query->range_of_expr (vr, op, s)
74 504900 : && vr == range_true_and_false (TREE_TYPE (op)));
75 504900 : }
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 143667 : 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 143667 : relation_kind rel = VREL_VARYING;
89 : /* For subtraction see if relations could simplify it. */
90 143667 : if (s
91 143667 : && subcode == MINUS_EXPR
92 143667 : && types_compatible_p (TREE_TYPE (op0), TREE_TYPE (op1)))
93 : {
94 28987 : 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 28987 : if (rel == VREL_EQ)
99 : return true;
100 : }
101 :
102 143666 : int_range_max vr0, vr1;
103 143666 : if (!query->range_of_expr (vr0, op0, s) || vr0.undefined_p ())
104 12 : vr0.set_varying (TREE_TYPE (op0));
105 143666 : if (!query->range_of_expr (vr1, op1, s) || vr1.undefined_p ())
106 0 : vr1.set_varying (TREE_TYPE (op1));
107 :
108 143666 : tree vr0min = wide_int_to_tree (TREE_TYPE (op0), vr0.lower_bound ());
109 143666 : tree vr0max = wide_int_to_tree (TREE_TYPE (op0), vr0.upper_bound ());
110 143666 : tree vr1min = wide_int_to_tree (TREE_TYPE (op1), vr1.lower_bound ());
111 143666 : 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 143666 : if ((rel == VREL_GT || rel == VREL_GE)
117 23 : && tree_int_cst_sgn (vr1min) >= 0
118 143683 : && !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 143649 : if (rel == VREL_LT
125 1 : && tree_int_cst_sgn (vr1min) >= 0
126 143650 : && TYPE_UNSIGNED (type))
127 : {
128 1 : *ovf = true;
129 1 : return true;
130 : }
131 :
132 235513 : *ovf = arith_overflowed_p (subcode, type, vr0min,
133 : subcode == MINUS_EXPR ? vr1max : vr1min);
134 235513 : if (arith_overflowed_p (subcode, type, vr0max,
135 143648 : subcode == MINUS_EXPR ? vr1min : vr1max) != *ovf)
136 : return false;
137 39409 : if (subcode == MULT_EXPR)
138 : {
139 23896 : if (arith_overflowed_p (subcode, type, vr0min, vr1max) != *ovf
140 23896 : || arith_overflowed_p (subcode, type, vr0max, vr1min) != *ovf)
141 376 : return false;
142 : }
143 39033 : 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 37081 : widest2_int wmin, wmax;
151 333911 : widest2_int w[4];
152 37081 : int i;
153 37081 : signop sign0 = TYPE_SIGN (TREE_TYPE (op0));
154 37081 : signop sign1 = TYPE_SIGN (TREE_TYPE (op1));
155 37127 : w[0] = widest2_int::from (vr0.lower_bound (), sign0);
156 37135 : w[1] = widest2_int::from (vr0.upper_bound (), sign0);
157 37118 : w[2] = widest2_int::from (vr1.lower_bound (), sign1);
158 37126 : w[3] = widest2_int::from (vr1.upper_bound (), sign1);
159 185405 : for (i = 0; i < 4; i++)
160 : {
161 148324 : widest2_int wt;
162 148324 : switch (subcode)
163 : {
164 27012 : case PLUS_EXPR:
165 27012 : wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
166 27012 : break;
167 27868 : case MINUS_EXPR:
168 27868 : wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
169 27868 : break;
170 93444 : case MULT_EXPR:
171 93444 : wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
172 93444 : break;
173 0 : default:
174 0 : gcc_unreachable ();
175 : }
176 148324 : if (i == 0)
177 : {
178 37081 : wmin = wt;
179 37081 : wmax = wt;
180 : }
181 : else
182 : {
183 111243 : wmin = wi::smin (wmin, wt);
184 112051 : wmax = wi::smax (wmax, wt);
185 : }
186 148324 : }
187 : /* The result of op0 CODE op1 is known to be in range
188 : [wmin, wmax]. */
189 37081 : widest2_int wtmin
190 74162 : = widest2_int::from (irange_val_min (type), TYPE_SIGN (type));
191 37081 : widest2_int wtmax
192 74162 : = 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 73152 : if (wmax < wtmin || wmin > wtmax)
197 1661 : return true;
198 : return false;
199 222752 : }
200 : return true;
201 143666 : }
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 4686710 : get_scev_info (vrange &r, tree name, gimple *stmt, class loop *l,
209 : tree &init, tree &step, enum ev_direction &dir)
210 : {
211 4686710 : tree ev = analyze_scalar_evolution (l, name);
212 4686710 : tree chrec = instantiate_parameters (l, ev);
213 4686710 : tree type = TREE_TYPE (name);
214 4686710 : if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
215 : {
216 2232199 : r.set_varying (type);
217 2232199 : return false;
218 : }
219 2454511 : 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 0 : return false;
226 : }
227 :
228 2454511 : init = initial_condition_in_loop_num (chrec, l->num);
229 2454511 : step = evolution_part_in_loop_num (chrec, l->num);
230 2454511 : if (!init || !step)
231 : {
232 0 : r.set_varying (type);
233 0 : return false;
234 : }
235 2454511 : dir = scev_direction (chrec);
236 2454511 : if (dir == EV_DIR_UNKNOWN
237 2454511 : || scev_probably_wraps_p (NULL, init, step, stmt,
238 : get_chrec_loop (chrec), true))
239 : {
240 434174 : r.set_varying (type);
241 434174 : 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 1715397 : induction_variable_may_overflow_p (tree type,
250 : const wide_int &step, const widest_int &nit)
251 : {
252 1715397 : wi::overflow_type ovf;
253 1715397 : signop sign = TYPE_SIGN (type);
254 1715397 : widest_int max_step = wi::mul (widest_int::from (step, sign),
255 1715397 : nit, sign, &ovf);
256 :
257 1715397 : if (ovf || !wi::fits_to_tree_p (max_step, type))
258 114188 : 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 1601209 : return (sign == SIGNED
264 3201739 : && wi::gts_p (max_step, 0) != wi::gts_p (step, 0));
265 1715397 : }
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 2020087 : range_from_loop_direction (irange &r, tree type,
272 : const irange &begin, const irange &end,
273 : ev_direction dir)
274 : {
275 2020087 : signop sign = TYPE_SIGN (type);
276 :
277 2020087 : if (begin.undefined_p () || end.undefined_p ())
278 2698 : r.set_varying (type);
279 2017389 : else if (dir == EV_DIR_GROWS)
280 : {
281 1789664 : if (wi::ge_p (begin.lower_bound (), end.upper_bound (), sign))
282 198 : r.set_varying (type);
283 : else
284 1789466 : r = int_range<1> (type, begin.lower_bound (), end.upper_bound ());
285 : }
286 : else
287 : {
288 227745 : if (wi::ge_p (end.lower_bound (), begin.upper_bound (), sign))
289 214 : r.set_varying (type);
290 : else
291 227531 : r = int_range<1> (type, end.lower_bound (), begin.upper_bound ());
292 : }
293 2020087 : }
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 4686710 : range_of_var_in_loop (vrange &v, tree name, class loop *l, gimple *stmt,
300 : range_query *query)
301 : {
302 4686710 : tree init, step;
303 4686710 : enum ev_direction dir;
304 4686710 : if (!get_scev_info (v, name, stmt, l, init, step, dir))
305 : return true;
306 :
307 : // Calculate ranges for the values from SCEV.
308 2020337 : irange &r = as_a <irange> (v);
309 2020337 : tree type = TREE_TYPE (init);
310 2020337 : int_range<2> rinit (type), rstep (type), max_init (type);
311 2020337 : if (!query->range_of_expr (rinit, init, stmt)
312 2020337 : || !query->range_of_expr (rstep, step, stmt))
313 0 : return false;
314 :
315 : // Calculate the final range of NAME if possible.
316 2020337 : if (rinit.singleton_p () && rstep.singleton_p ())
317 : {
318 1715647 : widest_int nit;
319 1715647 : if (!max_loop_iterations (l, &nit))
320 : return false;
321 :
322 1715397 : if (!induction_variable_may_overflow_p (type, rstep.lower_bound (), nit))
323 : {
324 : // Calculate the max bounds for init (init + niter * step).
325 1600530 : wide_int w = wide_int::from (nit, TYPE_PRECISION (type), TYPE_SIGN (type));
326 1600530 : int_range<1> niter (type, w, w);
327 1600530 : int_range_max max_step;
328 1600530 : range_op_handler mult_handler (MULT_EXPR);
329 1600530 : range_op_handler plus_handler (PLUS_EXPR);
330 1600530 : if (!mult_handler.fold_range (max_step, type, niter, rstep)
331 1600530 : || !plus_handler.fold_range (max_init, type, rinit, max_step))
332 0 : return false;
333 1600530 : }
334 1715647 : }
335 2020087 : range_from_loop_direction (r, type, rinit, max_init, dir);
336 2020087 : return true;
337 2020337 : }
338 :
339 : /* Helper function for vrp_evaluate_conditional_warnv & other
340 : optimizers. */
341 :
342 : tree
343 20942802 : simplify_using_ranges::fold_cond_with_ops (enum tree_code code,
344 : tree op0, tree op1, gimple *s)
345 : {
346 20942802 : value_range r0 (TREE_TYPE (op0));
347 20942802 : value_range r1 (TREE_TYPE (op1));
348 20942802 : if (!query->range_of_expr (r0, op0, s)
349 20942802 : || !query->range_of_expr (r1, op1, s))
350 7266 : return NULL_TREE;
351 :
352 20935536 : int_range<1> res;
353 20935536 : range_op_handler handler (code);
354 :
355 : // Find any relation between op0 and op1 and pass it to fold_range.
356 20935536 : relation_kind rel = VREL_VARYING;
357 20935536 : if (gimple_range_ssa_p (op0) && gimple_range_ssa_p (op1))
358 4632641 : rel = query->relation ().query (s, op0, op1);
359 :
360 20935536 : if (handler && handler.fold_range (res, boolean_type_node, r0, r1,
361 : relation_trio::op1_op2 (rel)))
362 : {
363 20935536 : if (res == range_true ())
364 8876 : return boolean_true_node;
365 20926660 : if (res == range_false ())
366 5066 : return boolean_false_node;
367 : }
368 : return NULL;
369 20942802 : }
370 :
371 : /* Helper function for legacy_fold_cond. */
372 :
373 : tree
374 21270894 : simplify_using_ranges::legacy_fold_cond_overflow (gimple *stmt)
375 : {
376 21270894 : tree ret;
377 21270894 : tree_code code = gimple_cond_code (stmt);
378 21270894 : tree op0 = gimple_cond_lhs (stmt);
379 21270894 : tree op1 = gimple_cond_rhs (stmt);
380 :
381 : /* We only deal with integral and pointer types. */
382 42184563 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
383 26005401 : && !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 20419024 : tree x;
395 20419024 : if (overflow_comparison_p (code, op0, op1, &x))
396 : {
397 1216 : 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 1216 : if (integer_zerop (x))
403 : {
404 219 : op1 = x;
405 219 : 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 997 : else if (wi::to_wide (x) == max - 1)
412 : {
413 657 : op0 = op1;
414 657 : op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
415 657 : code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
416 : }
417 : else
418 : {
419 340 : int_range_max vro, vri;
420 340 : tree type = TREE_TYPE (op0);
421 340 : if (code == GT_EXPR || code == GE_EXPR)
422 : {
423 126 : vro.set (type,
424 252 : wi::to_wide (TYPE_MIN_VALUE (type)),
425 126 : wi::to_wide (x), VR_ANTI_RANGE);
426 126 : vri.set (type,
427 252 : wi::to_wide (TYPE_MIN_VALUE (type)),
428 252 : wi::to_wide (x));
429 : }
430 214 : else if (code == LT_EXPR || code == LE_EXPR)
431 : {
432 214 : vro.set (type,
433 428 : wi::to_wide (TYPE_MIN_VALUE (type)),
434 214 : wi::to_wide (x));
435 214 : vri.set (type,
436 428 : wi::to_wide (TYPE_MIN_VALUE (type)),
437 428 : wi::to_wide (x),
438 : VR_ANTI_RANGE);
439 : }
440 : else
441 0 : gcc_unreachable ();
442 340 : int_range_max vr0;
443 340 : 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 340 : vro.intersect (vr0);
456 340 : if (vro.undefined_p ())
457 8 : return boolean_false_node;
458 332 : vri.intersect (vr0);
459 332 : if (vri.undefined_p ())
460 0 : return boolean_true_node;
461 340 : }
462 1216 : }
463 :
464 20419016 : 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 21270894 : simplify_using_ranges::legacy_fold_cond (gcond *stmt, edge *taken_edge_p)
475 : {
476 21270894 : tree val;
477 :
478 21270894 : *taken_edge_p = NULL;
479 :
480 21270894 : if (dump_file && (dump_flags & TDF_DETAILS))
481 : {
482 377 : tree use;
483 377 : ssa_op_iter i;
484 :
485 377 : fprintf (dump_file, "\nVisiting conditional with predicate: ");
486 377 : print_gimple_stmt (dump_file, stmt, 0);
487 377 : fprintf (dump_file, "\nWith known ranges\n");
488 :
489 842 : FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
490 : {
491 465 : fprintf (dump_file, "\t");
492 465 : print_generic_expr (dump_file, use);
493 465 : fprintf (dump_file, ": ");
494 465 : value_range r (TREE_TYPE (use));
495 465 : query->range_of_expr (r, use, stmt);
496 465 : r.dump (dump_file);
497 465 : }
498 :
499 377 : fprintf (dump_file, "\n");
500 : }
501 :
502 21270894 : val = legacy_fold_cond_overflow (stmt);
503 21270894 : if (val)
504 8 : *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
505 :
506 21270894 : if (dump_file && (dump_flags & TDF_DETAILS))
507 : {
508 377 : fprintf (dump_file, "\nPredicate evaluates to: ");
509 377 : if (val == NULL_TREE)
510 377 : fprintf (dump_file, "DON'T KNOW\n");
511 : else
512 0 : print_generic_stmt (dump_file, val);
513 : }
514 21270894 : }
515 :
516 : /* Simplify boolean operations if the source is known
517 : to be already a boolean. */
518 : bool
519 511177 : simplify_using_ranges::simplify_truth_ops_using_ranges
520 : (gimple_stmt_iterator *gsi,
521 : gimple *stmt)
522 : {
523 511177 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
524 511177 : tree lhs, op0, op1;
525 511177 : bool need_conversion;
526 :
527 : /* We handle only !=/== case here. */
528 511177 : gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
529 :
530 511177 : op0 = gimple_assign_rhs1 (stmt);
531 511177 : if (!op_with_boolean_value_range_p (op0, stmt))
532 : return false;
533 :
534 17334 : op1 = gimple_assign_rhs2 (stmt);
535 17334 : 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 16810 : if (rhs_code == EQ_EXPR)
541 : {
542 8699 : if (TREE_CODE (op1) == INTEGER_CST)
543 3606 : op1 = int_const_binop (BIT_XOR_EXPR, op1,
544 7212 : build_int_cst (TREE_TYPE (op1), 1));
545 : else
546 : return false;
547 : }
548 :
549 11717 : lhs = gimple_assign_lhs (stmt);
550 11717 : need_conversion
551 11717 : = !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 11717 : if (need_conversion
555 10287 : && !TYPE_UNSIGNED (TREE_TYPE (op0))
556 3870 : && TYPE_PRECISION (TREE_TYPE (op0)) == 1
557 11740 : && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
558 : return false;
559 :
560 : /* For A != 0 we can substitute A itself. */
561 11717 : if (integer_zerop (op1))
562 6935 : 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 4782 : else if (need_conversion)
567 : {
568 3352 : tree tem = make_ssa_name (TREE_TYPE (op0));
569 3352 : gassign *newop
570 3352 : = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
571 3352 : gsi_insert_before (gsi, newop, GSI_SAME_STMT);
572 6678 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
573 6678 : && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
574 : {
575 6520 : int_range<1> vr (TREE_TYPE (tem),
576 6520 : wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
577 6520 : wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
578 3260 : set_range_info (tem, vr);
579 3260 : }
580 3352 : 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 11717 : update_stmt (gsi_stmt (*gsi));
586 11717 : fold_stmt (gsi, follow_single_use_edges);
587 :
588 11717 : 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 333555 : simplify_using_ranges::simplify_div_or_mod_using_ranges
601 : (gimple_stmt_iterator *gsi,
602 : gimple *stmt)
603 : {
604 333555 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
605 333555 : tree val = NULL;
606 333555 : tree op0 = gimple_assign_rhs1 (stmt);
607 333555 : tree op1 = gimple_assign_rhs2 (stmt);
608 333555 : tree op0min = NULL_TREE, op0max = NULL_TREE;
609 333555 : tree op1min = op1;
610 333555 : int_range_max vr;
611 :
612 333555 : if (TREE_CODE (op0) == INTEGER_CST)
613 : {
614 : op0min = op0;
615 : op0max = op0;
616 : }
617 : else
618 : {
619 308579 : if (!query->range_of_expr (vr, op0, stmt))
620 0 : vr.set_varying (TREE_TYPE (op0));
621 308579 : if (!vr.varying_p () && !vr.undefined_p ())
622 : {
623 153336 : tree type = vr.type ();
624 153336 : op0min = wide_int_to_tree (type, vr.lower_bound ());
625 153345 : op0max = wide_int_to_tree (type, vr.upper_bound ());
626 : }
627 : }
628 :
629 333555 : if (rhs_code == TRUNC_MOD_EXPR
630 157582 : && TREE_CODE (op1) == SSA_NAME)
631 : {
632 101660 : int_range_max vr1;
633 101660 : if (!query->range_of_expr (vr1, op1, stmt))
634 0 : vr1.set_varying (TREE_TYPE (op1));
635 101660 : if (!vr1.varying_p () && !vr1.undefined_p ())
636 26737 : op1min = wide_int_to_tree (vr1.type (), vr1.lower_bound ());
637 101660 : }
638 157582 : if (rhs_code == TRUNC_MOD_EXPR
639 157582 : && TREE_CODE (op1min) == INTEGER_CST
640 82655 : && tree_int_cst_sgn (op1min) == 1
641 61333 : && op0max
642 35377 : && tree_int_cst_lt (op0max, op1min))
643 : {
644 1209 : if (TYPE_UNSIGNED (TREE_TYPE (op0))
645 538 : || tree_int_cst_sgn (op0min) >= 0
646 1438 : || 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 332381 : if (TREE_CODE (op0) != SSA_NAME)
657 : return false;
658 :
659 307406 : 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 267517 : if (rhs_code == TRUNC_MOD_EXPR
666 267517 : && fold_stmt (gsi, follow_single_use_edges))
667 : return true;
668 267505 : return false;
669 : }
670 :
671 39889 : if (TYPE_UNSIGNED (TREE_TYPE (op0)))
672 0 : val = integer_one_node;
673 : else
674 : {
675 39889 : tree zero = build_zero_cst (TREE_TYPE (op0));
676 39889 : val = fold_cond_with_ops (GE_EXPR, op0, zero, stmt);
677 : }
678 :
679 39889 : if (val && integer_onep (val))
680 : {
681 6847 : tree t;
682 :
683 6847 : if (rhs_code == TRUNC_DIV_EXPR)
684 : {
685 4475 : t = build_int_cst (integer_type_node, tree_log2 (op1));
686 4475 : gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
687 4475 : gimple_assign_set_rhs1 (stmt, op0);
688 4475 : gimple_assign_set_rhs2 (stmt, t);
689 : }
690 : else
691 : {
692 2372 : t = build_int_cst (TREE_TYPE (op1), 1);
693 2372 : t = int_const_binop (MINUS_EXPR, op1, t);
694 2372 : t = fold_convert (TREE_TYPE (op0), t);
695 :
696 2372 : gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
697 2372 : gimple_assign_set_rhs1 (stmt, op0);
698 2372 : gimple_assign_set_rhs2 (stmt, t);
699 : }
700 :
701 6847 : update_stmt (stmt);
702 6847 : fold_stmt (gsi, follow_single_use_edges);
703 6847 : return true;
704 : }
705 :
706 : return false;
707 333555 : }
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 235295 : simplify_using_ranges::simplify_min_or_max_using_ranges
714 : (gimple_stmt_iterator *gsi,
715 : gimple *stmt)
716 : {
717 235295 : tree op0 = gimple_assign_rhs1 (stmt);
718 235295 : tree op1 = gimple_assign_rhs2 (stmt);
719 235295 : tree val;
720 :
721 235295 : val = fold_cond_with_ops (LE_EXPR, op0, op1, stmt);
722 235295 : if (!val)
723 229706 : val = fold_cond_with_ops (LT_EXPR, op0, op1, stmt);
724 :
725 229706 : if (val)
726 : {
727 : /* VAL == TRUE -> OP0 < or <= op1
728 : VAL == FALSE -> OP0 > or >= op1. */
729 6612 : tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
730 6612 : == integer_zerop (val)) ? op0 : op1;
731 6612 : gimple_assign_set_rhs_from_tree (gsi, res);
732 6612 : 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 9589 : simplify_using_ranges::simplify_abs_using_ranges (gimple_stmt_iterator *gsi,
744 : gimple *stmt)
745 : {
746 9589 : tree op = gimple_assign_rhs1 (stmt);
747 9589 : tree zero = build_zero_cst (TREE_TYPE (op));
748 9589 : tree val = fold_cond_with_ops (LE_EXPR, op, zero, stmt);
749 :
750 9589 : if (!val)
751 : {
752 : /* The range is neither <= 0 nor > 0. Now see if it is
753 : either < 0 or >= 0. */
754 9307 : val = fold_cond_with_ops (LT_EXPR, op, zero, stmt);
755 : }
756 9307 : if (val)
757 : {
758 482 : gimple_assign_set_rhs1 (stmt, op);
759 482 : if (integer_zerop (val))
760 304 : gimple_assign_set_rhs_code (stmt, SSA_NAME);
761 : else
762 178 : gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
763 482 : update_stmt (stmt);
764 482 : fold_stmt (gsi, follow_single_use_edges);
765 482 : 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 1637295 : 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 1637295 : if (vr->varying_p () || vr->undefined_p ())
782 : {
783 963747 : *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
784 963747 : *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
785 963747 : return false;
786 : }
787 673550 : wi_set_zero_nonzero_bits (expr_type, vr->lower_bound (), vr->upper_bound (),
788 : *may_be_nonzero, *must_be_nonzero);
789 673548 : 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 1279570 : simplify_using_ranges::simplify_bit_ops_using_ranges
800 : (gimple_stmt_iterator *gsi,
801 : gimple *stmt)
802 : {
803 1279570 : tree op0 = gimple_assign_rhs1 (stmt);
804 1279570 : tree op1 = gimple_assign_rhs2 (stmt);
805 1279570 : tree op = NULL_TREE;
806 1279570 : int_range_max vr0, vr1;
807 1279570 : wide_int may_be_nonzero0, may_be_nonzero1;
808 1279570 : wide_int must_be_nonzero0, must_be_nonzero1;
809 1279570 : wide_int mask;
810 :
811 1279570 : if (!query->range_of_expr (vr0, op0, stmt)
812 1279570 : || vr0.undefined_p ())
813 : return false;
814 1278906 : if (!query->range_of_expr (vr1, op1, stmt)
815 1278906 : || vr1.undefined_p ())
816 : return false;
817 :
818 1278559 : if (!vr_set_zero_nonzero_bits (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
819 : &must_be_nonzero0))
820 : return false;
821 358736 : if (!vr_set_zero_nonzero_bits (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
822 : &must_be_nonzero1))
823 : return false;
824 :
825 314812 : switch (gimple_assign_rhs_code (stmt))
826 : {
827 223194 : case BIT_AND_EXPR:
828 223194 : mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
829 223194 : if (mask == 0)
830 : {
831 : op = op0;
832 : break;
833 : }
834 218628 : mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
835 218628 : if (mask == 0)
836 : {
837 : op = op1;
838 : break;
839 : }
840 : break;
841 91618 : case BIT_IOR_EXPR:
842 91618 : mask = wi::bit_and_not (may_be_nonzero0, must_be_nonzero1);
843 91618 : if (mask == 0)
844 : {
845 : op = op1;
846 : break;
847 : }
848 91618 : mask = wi::bit_and_not (may_be_nonzero1, must_be_nonzero0);
849 91618 : if (mask == 0)
850 : {
851 : op = op0;
852 : break;
853 : }
854 : break;
855 0 : default:
856 0 : gcc_unreachable ();
857 : }
858 :
859 4768 : if (op == NULL_TREE)
860 310044 : return false;
861 :
862 4768 : gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
863 4768 : update_stmt (gsi_stmt (*gsi));
864 4768 : return true;
865 1279582 : }
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 1652262 : test_for_singularity (enum tree_code cond_code, tree op0,
875 : tree op1, const irange *vr)
876 : {
877 1652262 : tree min = NULL;
878 1652262 : tree max = NULL;
879 :
880 : /* Extract minimum/maximum values which satisfy the conditional as it was
881 : written. */
882 1652262 : if (cond_code == LE_EXPR || cond_code == LT_EXPR)
883 : {
884 723276 : min = TYPE_MIN_VALUE (TREE_TYPE (op0));
885 :
886 723276 : max = op1;
887 723276 : if (cond_code == LT_EXPR)
888 : {
889 93931 : tree one = build_int_cst (TREE_TYPE (op0), 1);
890 93931 : max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
891 : }
892 : }
893 928986 : else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
894 : {
895 793036 : max = TYPE_MAX_VALUE (TREE_TYPE (op0));
896 :
897 793036 : min = op1;
898 793036 : if (cond_code == GT_EXPR)
899 : {
900 700699 : tree one = build_int_cst (TREE_TYPE (op0), 1);
901 700699 : 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 1652262 : if (min && max)
908 : {
909 1516312 : tree type = TREE_TYPE (op0);
910 1516312 : tree tmin = wide_int_to_tree (type, vr->lower_bound ());
911 1516312 : tree tmax = wide_int_to_tree (type, vr->upper_bound ());
912 1516312 : if (compare_values (tmin, min) == 1)
913 460385 : min = tmin;
914 1516312 : if (compare_values (tmax, max) == -1)
915 572120 : 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 1516312 : if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
921 : 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 264473 : range_fits_type_p (const irange *vr,
931 : unsigned dest_precision, signop dest_sgn)
932 : {
933 264473 : tree src_type;
934 264473 : unsigned src_precision;
935 264473 : widest_int tem;
936 264473 : signop src_sgn;
937 :
938 : /* Now we can only handle ranges with constant bounds. */
939 264473 : if (vr->undefined_p () || vr->varying_p ())
940 : return false;
941 :
942 : /* We can only handle integral and pointer types. */
943 213586 : src_type = vr->type ();
944 213586 : 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 213586 : src_precision = TYPE_PRECISION (src_type);
951 213586 : src_sgn = TYPE_SIGN (src_type);
952 213586 : if ((src_precision < dest_precision
953 15487 : && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
954 199512 : || (src_precision == dest_precision && src_sgn == dest_sgn))
955 : return true;
956 :
957 199369 : wide_int vrmin = vr->lower_bound ();
958 199369 : 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 199369 : if (src_sgn != dest_sgn
965 199369 : && (wi::lts_p (vrmin, 0) || wi::lts_p (vrmax, 0)))
966 72719 : return false;
967 :
968 : /* Then we can perform the conversion on both ends and compare
969 : the result for equality. */
970 126650 : signop sign = TYPE_SIGN (vr->type ());
971 126650 : tem = wi::ext (widest_int::from (vrmin, sign), dest_precision, dest_sgn);
972 126650 : if (tem != widest_int::from (vrmin, sign))
973 : return false;
974 120988 : tem = wi::ext (widest_int::from (vrmax, sign), dest_precision, dest_sgn);
975 120988 : if (tem != widest_int::from (vrmax, sign))
976 : return false;
977 :
978 : return true;
979 199369 : }
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 314302 : 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 314302 : if ((e->flags & m_not_executable_flag) == m_not_executable_flag)
990 220468 : return;
991 :
992 207732 : e->flags |= m_not_executable_flag;
993 207732 : m_flag_set_edges.safe_push (e);
994 :
995 : // Check if the destination block needs to propagate the property.
996 207732 : basic_block bb = e->dest;
997 :
998 : // If any incoming edge is executable, we are done.
999 207732 : edge_iterator ei;
1000 342412 : FOR_EACH_EDGE (e, ei, bb->preds)
1001 248578 : 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 159379 : FOR_EACH_EDGE (e, ei, bb->succs)
1006 65545 : 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 21605331 : simplify_using_ranges::fold_cond (gcond *cond)
1014 : {
1015 21605331 : int_range_max r;
1016 21605331 : if (query->range_of_stmt (r, cond) && r.singleton_p ())
1017 : {
1018 : // COND has already been folded if arguments are constant.
1019 334437 : if (TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
1020 334437 : && TREE_CODE (gimple_cond_rhs (cond)) != SSA_NAME)
1021 : return false;
1022 241774 : if (dump_file)
1023 : {
1024 183 : fprintf (dump_file, "Folding predicate ");
1025 183 : print_gimple_expr (dump_file, cond, 0);
1026 183 : fprintf (dump_file, " to ");
1027 : }
1028 241774 : edge e0 = EDGE_SUCC (gimple_bb (cond), 0);
1029 241774 : edge e1 = EDGE_SUCC (gimple_bb (cond), 1);
1030 241774 : if (r.zero_p ())
1031 : {
1032 148850 : if (dump_file)
1033 129 : fprintf (dump_file, "0\n");
1034 148850 : gimple_cond_make_false (cond);
1035 148850 : if (e0->flags & EDGE_TRUE_VALUE)
1036 147110 : set_and_propagate_unexecutable (e0);
1037 : else
1038 1740 : set_and_propagate_unexecutable (e1);
1039 : }
1040 : else
1041 : {
1042 92924 : if (dump_file)
1043 54 : fprintf (dump_file, "1\n");
1044 92924 : gimple_cond_make_true (cond);
1045 92924 : if (e0->flags & EDGE_FALSE_VALUE)
1046 688 : set_and_propagate_unexecutable (e0);
1047 : else
1048 92236 : set_and_propagate_unexecutable (e1);
1049 : }
1050 241774 : update_stmt (cond);
1051 241774 : return true;
1052 : }
1053 :
1054 : // FIXME: Audit the code below and make sure it never finds anything.
1055 21270894 : edge taken_edge;
1056 21270894 : legacy_fold_cond (cond, &taken_edge);
1057 :
1058 21270894 : if (taken_edge)
1059 : {
1060 8 : 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 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 8 : update_stmt (cond);
1075 8 : return true;
1076 : }
1077 : return false;
1078 21605331 : }
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 12473621 : simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt)
1086 : {
1087 12473621 : tree op0 = gimple_cond_lhs (stmt);
1088 12473621 : tree op1 = gimple_cond_rhs (stmt);
1089 12473621 : enum tree_code cond_code = gimple_cond_code (stmt);
1090 :
1091 12473621 : if (fold_cond (stmt))
1092 : return true;
1093 :
1094 12338110 : if (simplify_compare_using_ranges_1 (cond_code, op0, op1, stmt))
1095 : {
1096 380593 : 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 380593 : gimple_cond_set_code (stmt, cond_code);
1104 380593 : gimple_cond_set_lhs (stmt, op0);
1105 380593 : gimple_cond_set_rhs (stmt, op1);
1106 :
1107 380593 : update_stmt (stmt);
1108 :
1109 380593 : if (dump_file)
1110 : {
1111 26 : print_gimple_stmt (dump_file, stmt, 0);
1112 26 : fprintf (dump_file, "\n");
1113 : }
1114 380593 : 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 1310650 : simplify_using_ranges::simplify_compare_assign_using_ranges_1
1124 : (gimple_stmt_iterator *gsi,
1125 : gimple *stmt)
1126 : {
1127 1310650 : enum tree_code code = gimple_assign_rhs_code (stmt);
1128 1310650 : tree op0 = gimple_assign_rhs1 (stmt);
1129 1310650 : tree op1 = gimple_assign_rhs2 (stmt);
1130 1310650 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
1131 1310650 : bool happened = false;
1132 :
1133 1310650 : if (simplify_compare_using_ranges_1 (code, op0, op1, stmt))
1134 : {
1135 6812 : 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 6812 : gimple_assign_set_rhs_code (stmt, code);
1143 6812 : gimple_assign_set_rhs1 (stmt, op0);
1144 6812 : gimple_assign_set_rhs2 (stmt, op1);
1145 :
1146 6812 : update_stmt (stmt);
1147 :
1148 6812 : 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 1310650 : if ((code == EQ_EXPR || code == NE_EXPR)
1160 731250 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
1161 1821827 : && simplify_truth_ops_using_ranges (gsi, stmt))
1162 : happened = true;
1163 :
1164 1310650 : 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 13648760 : simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tree &op0, tree &op1, gimple *stmt)
1173 : {
1174 13648760 : bool happened = false;
1175 13648760 : if (cond_code != NE_EXPR
1176 13648760 : && cond_code != EQ_EXPR
1177 3555346 : && TREE_CODE (op0) == SSA_NAME
1178 3554871 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
1179 16848885 : && is_gimple_min_invariant (op1))
1180 : {
1181 1939949 : int_range_max vr;
1182 :
1183 1939949 : 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 1939949 : if (!vr.undefined_p () && !vr.varying_p ())
1189 : {
1190 826131 : tree new_tree = test_for_singularity (cond_code, op0, op1, &vr);
1191 826131 : if (new_tree)
1192 : {
1193 135950 : cond_code = EQ_EXPR;
1194 135950 : op1 = new_tree;
1195 135950 : 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 826131 : new_tree = test_for_singularity
1202 826131 : (invert_tree_comparison (cond_code, false),
1203 : op0, op1, &vr);
1204 826131 : if (new_tree)
1205 : {
1206 179001 : cond_code = NE_EXPR;
1207 179001 : op1 = new_tree;
1208 179001 : happened = true;
1209 : }
1210 : }
1211 1939949 : }
1212 : // Try to simplify casted conditions.
1213 13648760 : if (simplify_casted_compare (cond_code, op0, op1))
1214 85330 : happened = true;
1215 13648760 : 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 13648760 : 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 13648760 : if (TREE_CODE (op0) == SSA_NAME
1235 13554536 : && TREE_CODE (op1) == INTEGER_CST)
1236 : {
1237 9544709 : gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
1238 9544709 : tree innerop;
1239 :
1240 9544709 : if (!is_gimple_assign (def_stmt))
1241 : return false;
1242 :
1243 5884816 : switch (gimple_assign_rhs_code (def_stmt))
1244 : {
1245 496608 : CASE_CONVERT:
1246 496608 : innerop = gimple_assign_rhs1 (def_stmt);
1247 496608 : break;
1248 51880 : case VIEW_CONVERT_EXPR:
1249 51880 : innerop = TREE_OPERAND (gimple_assign_rhs1 (def_stmt), 0);
1250 51880 : if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
1251 : return false;
1252 : break;
1253 : default:
1254 : return false;
1255 : }
1256 :
1257 545656 : if (TREE_CODE (innerop) == SSA_NAME
1258 545628 : && !POINTER_TYPE_P (TREE_TYPE (innerop))
1259 540140 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
1260 1085783 : && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
1261 : {
1262 511087 : int_range_max vr;
1263 :
1264 511087 : if (query->range_of_expr (vr, innerop)
1265 511084 : && !vr.varying_p ()
1266 172960 : && !vr.undefined_p ()
1267 172840 : && range_fits_type_p (&vr,
1268 172840 : TYPE_PRECISION (TREE_TYPE (op0)),
1269 172840 : TYPE_SIGN (TREE_TYPE (op0)))
1270 596417 : && int_fits_type_p (op1, TREE_TYPE (innerop)))
1271 : {
1272 85330 : tree newconst = fold_convert (TREE_TYPE (innerop), op1);
1273 85330 : op0 = innerop;
1274 85330 : op1 = newconst;
1275 85330 : return true;
1276 : }
1277 511087 : }
1278 : }
1279 : return false;
1280 : }
1281 :
1282 : /* Simplify a switch statement using the value range of the switch
1283 : argument. */
1284 :
1285 : bool
1286 60955 : simplify_using_ranges::simplify_switch_using_ranges (gswitch *stmt)
1287 : {
1288 60955 : tree op = gimple_switch_index (stmt);
1289 60955 : tree type = TREE_TYPE (op);
1290 60955 : int_range_max op_range (type);
1291 60955 : int_range_max default_range (type);
1292 60955 : auto_vec<unsigned> cases;
1293 60955 : cases.truncate (0);
1294 60955 : edge e;
1295 60955 : switch_update su;
1296 :
1297 : // Abort if we don't have a useful range for the switch index.
1298 60955 : if (!query->range_of_expr (op_range, op, stmt)
1299 60955 : || op_range.varying_p () || op_range.undefined_p ())
1300 : return false;
1301 :
1302 : // Default range starts with full known range of op.
1303 16009 : default_range = op_range;
1304 16009 : edge default_edge = gimple_switch_default_edge (cfun, stmt);
1305 :
1306 16009 : unsigned x, lim = gimple_switch_num_labels (stmt);
1307 95457 : for (x = 1; x < lim; x++)
1308 : {
1309 80464 : e = gimple_switch_edge (cfun, stmt, x);
1310 80464 : tree label = gimple_switch_label (stmt, x);
1311 :
1312 : // If this edge is the same as the default edge, do nothing else.
1313 80464 : if (e == default_edge)
1314 6145 : continue;
1315 : // Ada sometimes has mismatched labels and index. Just bail.
1316 80458 : if (TREE_TYPE (CASE_LOW (label)) != type)
1317 1016 : return false;
1318 :
1319 79442 : wide_int low = wi::to_wide (CASE_LOW (label));
1320 79442 : wide_int high;
1321 : // Singleton cases have no CASE_HIGH.
1322 79442 : tree tree_high = CASE_HIGH (label);
1323 79442 : if (tree_high)
1324 3341 : high = wi::to_wide (tree_high);
1325 : else
1326 76101 : 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 79442 : int_range_max case_range (type, low, high);
1331 79442 : if (case_range.intersect (op_range))
1332 : {
1333 : // If none of the label is in op_range, skip this label.
1334 50748 : if (case_range.undefined_p ())
1335 6139 : 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 44609 : if (case_range.lower_bound () != low)
1341 10 : CASE_LOW (label) = wide_int_to_tree (type,
1342 20 : case_range.lower_bound ());
1343 44609 : if (case_range.singleton_p ())
1344 43144 : CASE_HIGH (label) = NULL_TREE;
1345 : else
1346 1465 : if (case_range.upper_bound () != high)
1347 7 : CASE_HIGH (label) = wide_int_to_tree (type,
1348 14 : case_range.upper_bound ());
1349 : }
1350 : // Add case label to the keep list.
1351 73303 : cases.safe_push (x);
1352 : // Remove case_range from needing to be handled by the default.
1353 73303 : case_range.invert ();
1354 73303 : default_range.intersect (case_range);
1355 79442 : }
1356 :
1357 : // An undefined DEFAULT range means the current default case is not needed.
1358 14993 : unsigned idx = default_range.undefined_p () ? 0 : 1;
1359 14993 : unsigned vec_size = cases.length () + idx;
1360 14993 : if (vec_size == lim)
1361 : return false;
1362 :
1363 4055 : tree vec2 = make_tree_vec (vec_size);
1364 : // Add default label if there is one.
1365 4055 : if (idx)
1366 : {
1367 3078 : TREE_VEC_ELT (vec2, 0) = gimple_switch_default_label (stmt);
1368 3078 : e = gimple_switch_edge (cfun, stmt, 0);
1369 3078 : e->aux = (void *)-1;
1370 : }
1371 :
1372 16455 : for (x = 0; x < cases.length (); x++)
1373 : {
1374 12400 : unsigned swi = cases[x];
1375 12400 : TREE_VEC_ELT (vec2, idx++) = gimple_switch_label (stmt, swi);
1376 12400 : e = gimple_switch_edge (cfun, stmt, swi);
1377 12400 : e->aux = (void *)-1;
1378 : }
1379 :
1380 : /* Queue not needed edges for later removal. */
1381 4055 : edge_iterator ei;
1382 25700 : FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
1383 : {
1384 21645 : if (e->aux == (void *)-1)
1385 : {
1386 14662 : e->aux = NULL;
1387 14662 : continue;
1388 : }
1389 :
1390 6983 : if (dump_file && (dump_flags & TDF_DETAILS))
1391 : {
1392 0 : fprintf (dump_file, "removing unreachable case label\n");
1393 : }
1394 6983 : to_remove_edges.safe_push (e);
1395 6983 : set_and_propagate_unexecutable (e);
1396 6983 : e->flags &= ~EDGE_EXECUTABLE;
1397 6983 : e->flags |= EDGE_IGNORE;
1398 : }
1399 :
1400 : /* And queue an update for the stmt. */
1401 4055 : su.stmt = stmt;
1402 4055 : su.vec = vec2;
1403 4055 : to_update_switch_stmts.safe_push (su);
1404 4055 : return true;
1405 60955 : }
1406 :
1407 : void
1408 13504247 : simplify_using_ranges::cleanup_edges_and_switches (void)
1409 : {
1410 13504247 : int i;
1411 13504247 : edge e;
1412 13504247 : switch_update *su;
1413 :
1414 : /* Clear any edges marked as not executable. */
1415 13504247 : if (m_not_executable_flag)
1416 : {
1417 4580263 : FOR_EACH_VEC_ELT (m_flag_set_edges, i, e)
1418 207732 : e->flags &= ~m_not_executable_flag;
1419 : }
1420 : /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
1421 : CFG in a broken state and requires a cfg_cleanup run. */
1422 13515005 : FOR_EACH_VEC_ELT (to_remove_edges, i, e)
1423 6983 : remove_edge (e);
1424 :
1425 : /* Update SWITCH_EXPR case label vector. */
1426 13508302 : FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
1427 : {
1428 4055 : size_t j;
1429 4055 : size_t n = TREE_VEC_LENGTH (su->vec);
1430 4055 : tree label;
1431 4055 : gimple_switch_set_num_labels (su->stmt, n);
1432 23588 : for (j = 0; j < n; j++)
1433 15478 : gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
1434 : /* As we may have replaced the default label with a regular one
1435 : make sure to make it a real default label again. This ensures
1436 : optimal expansion. */
1437 4055 : label = gimple_switch_label (su->stmt, 0);
1438 4055 : CASE_LOW (label) = NULL_TREE;
1439 4055 : CASE_HIGH (label) = NULL_TREE;
1440 : }
1441 :
1442 13504247 : if (!to_remove_edges.is_empty ())
1443 : {
1444 3775 : free_dominance_info (CDI_DOMINATORS);
1445 3775 : loops_state_set (LOOPS_NEED_FIXUP);
1446 : }
1447 :
1448 13504247 : to_remove_edges.release ();
1449 13504247 : to_update_switch_stmts.release ();
1450 13504247 : }
1451 :
1452 : /* Simplify an integral conversion from an SSA name in STMT. */
1453 :
1454 : static bool
1455 4859391 : simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
1456 : {
1457 4859391 : tree innerop, middleop, finaltype;
1458 4859391 : gimple *def_stmt;
1459 4859391 : signop inner_sgn, middle_sgn, final_sgn;
1460 4859391 : unsigned inner_prec, middle_prec, final_prec;
1461 4859391 : widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
1462 :
1463 4859391 : finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
1464 4859391 : if (!INTEGRAL_TYPE_P (finaltype))
1465 : return false;
1466 4464073 : middleop = gimple_assign_rhs1 (stmt);
1467 4464073 : def_stmt = SSA_NAME_DEF_STMT (middleop);
1468 4464073 : if (!is_gimple_assign (def_stmt)
1469 4464073 : || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
1470 : return false;
1471 152260 : innerop = gimple_assign_rhs1 (def_stmt);
1472 152260 : if (TREE_CODE (innerop) != SSA_NAME
1473 152260 : || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
1474 : return false;
1475 :
1476 : /* Get the value-range of the inner operand. Use global ranges in
1477 : case innerop was created during substitute-and-fold. */
1478 148788 : wide_int imin, imax;
1479 148788 : int_range_max vr;
1480 148788 : if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop)))
1481 : return false;
1482 249164 : get_range_query (cfun)->range_of_expr (vr, innerop, stmt);
1483 124582 : if (vr.undefined_p () || vr.varying_p ())
1484 : return false;
1485 70795 : innermin = widest_int::from (vr.lower_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
1486 70795 : innermax = widest_int::from (vr.upper_bound (), TYPE_SIGN (TREE_TYPE (innerop)));
1487 :
1488 : /* Simulate the conversion chain to check if the result is equal if
1489 : the middle conversion is removed. */
1490 70795 : inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
1491 70795 : middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
1492 70795 : final_prec = TYPE_PRECISION (finaltype);
1493 :
1494 : /* If the first conversion is not injective, the second must not
1495 : be widening. */
1496 70795 : if (wi::gtu_p (innermax - innermin,
1497 141590 : wi::mask <widest_int> (middle_prec, false))
1498 70795 : && middle_prec < final_prec)
1499 : return false;
1500 : /* We also want a medium value so that we can track the effect that
1501 : narrowing conversions with sign change have. */
1502 59323 : inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
1503 59323 : if (inner_sgn == UNSIGNED)
1504 44561 : innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
1505 : else
1506 14762 : innermed = 0;
1507 59323 : if (wi::cmp (innermin, innermed, inner_sgn) >= 0
1508 59323 : || wi::cmp (innermed, innermax, inner_sgn) >= 0)
1509 43745 : innermed = innermin;
1510 :
1511 59323 : middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
1512 59323 : middlemin = wi::ext (innermin, middle_prec, middle_sgn);
1513 59323 : middlemed = wi::ext (innermed, middle_prec, middle_sgn);
1514 59323 : middlemax = wi::ext (innermax, middle_prec, middle_sgn);
1515 :
1516 : /* Require that the final conversion applied to both the original
1517 : and the intermediate range produces the same result. */
1518 59323 : final_sgn = TYPE_SIGN (finaltype);
1519 118646 : if (wi::ext (middlemin, final_prec, final_sgn)
1520 118646 : != wi::ext (innermin, final_prec, final_sgn)
1521 113668 : || wi::ext (middlemed, final_prec, final_sgn)
1522 229825 : != wi::ext (innermed, final_prec, final_sgn)
1523 155837 : || wi::ext (middlemax, final_prec, final_sgn)
1524 204094 : != wi::ext (innermax, final_prec, final_sgn))
1525 : return false;
1526 :
1527 46729 : gimple_assign_set_rhs1 (stmt, innerop);
1528 46729 : fold_stmt (gsi, follow_single_use_edges);
1529 46729 : return true;
1530 5008179 : }
1531 :
1532 : /* Simplify a conversion from integral SSA name to float in STMT. */
1533 :
1534 : bool
1535 256324 : simplify_using_ranges::simplify_float_conversion_using_ranges
1536 : (gimple_stmt_iterator *gsi,
1537 : gimple *stmt)
1538 : {
1539 256324 : tree rhs1 = gimple_assign_rhs1 (stmt);
1540 256324 : int_range_max vr;
1541 256324 : scalar_float_mode fltmode
1542 256324 : = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
1543 256324 : scalar_int_mode mode;
1544 256324 : tree tem;
1545 256324 : gassign *conv;
1546 :
1547 : /* We can only handle constant ranges. */
1548 256324 : if (!query->range_of_expr (vr, rhs1, stmt)
1549 256324 : || vr.varying_p ()
1550 358096 : || vr.undefined_p ())
1551 : return false;
1552 :
1553 : /* The code below doesn't work for large/huge _BitInt, nor is really
1554 : needed for those, bitint lowering does use ranges already. */
1555 202204 : if (BITINT_TYPE_P (TREE_TYPE (rhs1))
1556 101103 : && TYPE_MODE (TREE_TYPE (rhs1)) == BLKmode)
1557 : return false;
1558 : /* First check if we can use a signed type in place of an unsigned. */
1559 101101 : scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
1560 101101 : if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
1561 5373 : && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
1562 104704 : && range_fits_type_p (&vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
1563 : mode = rhs_mode;
1564 : /* If we can do the conversion in the current input mode do nothing. */
1565 99528 : else if (can_float_p (fltmode, rhs_mode,
1566 99528 : TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
1567 : return false;
1568 : /* Otherwise search for a mode we can use, starting from the narrowest
1569 : integer mode available. */
1570 : else
1571 : {
1572 4448 : mode = NARROWEST_INT_MODE;
1573 16033 : for (;;)
1574 : {
1575 : /* If we cannot do a signed conversion to float from mode
1576 : or if the value-range does not fit in the signed type
1577 : try with a wider mode. */
1578 16033 : if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
1579 16033 : && range_fits_type_p (&vr, GET_MODE_PRECISION (mode), SIGNED))
1580 : break;
1581 :
1582 : /* But do not widen the input. Instead leave that to the
1583 : optabs expansion code. */
1584 31800 : if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
1585 15900 : || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
1586 : return false;
1587 : }
1588 : }
1589 :
1590 : /* It works, insert a truncation or sign-change before the
1591 : float conversion. */
1592 1706 : tem = make_ssa_name (build_nonstandard_integer_type
1593 1706 : (GET_MODE_PRECISION (mode), 0));
1594 1706 : conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
1595 1706 : gsi_insert_before (gsi, conv, GSI_SAME_STMT);
1596 1706 : gimple_assign_set_rhs1 (stmt, tem);
1597 1706 : fold_stmt (gsi, follow_single_use_edges);
1598 :
1599 1706 : return true;
1600 256324 : }
1601 :
1602 : /* Simplify an internal fn call using ranges if possible. */
1603 :
1604 : bool
1605 601993 : simplify_using_ranges::simplify_internal_call_using_ranges
1606 : (gimple_stmt_iterator *gsi,
1607 : gimple *stmt)
1608 : {
1609 601993 : enum tree_code subcode;
1610 601993 : bool is_ubsan = false;
1611 601993 : bool ovf = false;
1612 601993 : switch (gimple_call_internal_fn (stmt))
1613 : {
1614 : case IFN_UBSAN_CHECK_ADD:
1615 : subcode = PLUS_EXPR;
1616 : is_ubsan = true;
1617 : break;
1618 2480 : case IFN_UBSAN_CHECK_SUB:
1619 2480 : subcode = MINUS_EXPR;
1620 2480 : is_ubsan = true;
1621 2480 : break;
1622 2123 : case IFN_UBSAN_CHECK_MUL:
1623 2123 : subcode = MULT_EXPR;
1624 2123 : is_ubsan = true;
1625 2123 : break;
1626 40694 : case IFN_ADD_OVERFLOW:
1627 40694 : subcode = PLUS_EXPR;
1628 40694 : break;
1629 49730 : case IFN_SUB_OVERFLOW:
1630 49730 : subcode = MINUS_EXPR;
1631 49730 : break;
1632 46975 : case IFN_MUL_OVERFLOW:
1633 46975 : subcode = MULT_EXPR;
1634 46975 : break;
1635 : default:
1636 : return false;
1637 : }
1638 :
1639 144537 : tree op0 = gimple_call_arg (stmt, 0);
1640 144537 : tree op1 = gimple_call_arg (stmt, 1);
1641 144537 : tree type;
1642 144537 : if (is_ubsan)
1643 : {
1644 7138 : type = TREE_TYPE (op0);
1645 7138 : if (VECTOR_TYPE_P (type))
1646 : return false;
1647 : }
1648 137399 : else if (gimple_call_lhs (stmt) == NULL_TREE)
1649 : return false;
1650 : else
1651 137399 : type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
1652 143667 : if (!check_for_binary_op_overflow (query, subcode, type, op0, op1, &ovf, stmt)
1653 143667 : || (is_ubsan && ovf))
1654 : return false;
1655 :
1656 3459 : gimple *g;
1657 3459 : location_t loc = gimple_location (stmt);
1658 3459 : if (is_ubsan)
1659 529 : g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
1660 : else
1661 : {
1662 2930 : tree utype = type;
1663 2930 : if (ovf
1664 1441 : || !useless_type_conversion_p (type, TREE_TYPE (op0))
1665 3646 : || !useless_type_conversion_p (type, TREE_TYPE (op1)))
1666 2503 : utype = unsigned_type_for (type);
1667 2930 : if (TREE_CODE (op0) == INTEGER_CST)
1668 1175 : op0 = fold_convert (utype, op0);
1669 1755 : else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
1670 : {
1671 735 : g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
1672 735 : gimple_set_location (g, loc);
1673 735 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1674 735 : op0 = gimple_assign_lhs (g);
1675 : }
1676 2930 : if (TREE_CODE (op1) == INTEGER_CST)
1677 1699 : op1 = fold_convert (utype, op1);
1678 1231 : else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
1679 : {
1680 22 : g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
1681 22 : gimple_set_location (g, loc);
1682 22 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1683 22 : op1 = gimple_assign_lhs (g);
1684 : }
1685 2930 : g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
1686 2930 : gimple_set_location (g, loc);
1687 2930 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1688 2930 : if (utype != type)
1689 : {
1690 1345 : g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
1691 : gimple_assign_lhs (g));
1692 1345 : gimple_set_location (g, loc);
1693 1345 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
1694 : }
1695 2930 : g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
1696 : gimple_assign_lhs (g),
1697 2930 : build_int_cst (type, ovf));
1698 : }
1699 3459 : gimple_set_location (g, loc);
1700 3459 : gsi_replace (gsi, g, false);
1701 3459 : return true;
1702 : }
1703 :
1704 : /* Return true if VAR is a two-valued variable. Set a and b with the
1705 : two-values when it is true. Return false otherwise. */
1706 :
1707 : bool
1708 484157 : simplify_using_ranges::two_valued_val_range_p (tree var, tree *a, tree *b,
1709 : gimple *s)
1710 : {
1711 484157 : int_range_max vr;
1712 484157 : if (!query->range_of_expr (vr, var, s))
1713 : return false;
1714 484157 : if (vr.varying_p () || vr.undefined_p ())
1715 : return false;
1716 :
1717 802924 : if ((vr.num_pairs () == 1 && vr.upper_bound () - vr.lower_bound () == 1)
1718 416435 : || (vr.num_pairs () == 2
1719 288473 : && vr.lower_bound (0) == vr.upper_bound (0)
1720 244604 : && vr.lower_bound (1) == vr.upper_bound (1)))
1721 : {
1722 7658 : *a = wide_int_to_tree (TREE_TYPE (var), vr.lower_bound ());
1723 7658 : *b = wide_int_to_tree (TREE_TYPE (var), vr.upper_bound ());
1724 7658 : return true;
1725 : }
1726 : return false;
1727 484157 : }
1728 :
1729 13504247 : simplify_using_ranges::simplify_using_ranges (range_query *query,
1730 13504247 : int not_executable_flag)
1731 13504247 : : query (query)
1732 : {
1733 13504247 : to_remove_edges = vNULL;
1734 13504247 : to_update_switch_stmts = vNULL;
1735 13504247 : m_not_executable_flag = not_executable_flag;
1736 13504247 : m_flag_set_edges = vNULL;
1737 13504247 : }
1738 :
1739 13504247 : simplify_using_ranges::~simplify_using_ranges ()
1740 : {
1741 13504247 : cleanup_edges_and_switches ();
1742 13504247 : m_flag_set_edges.release ();
1743 13504247 : }
1744 :
1745 : /* Simplify STMT using ranges if possible. */
1746 :
1747 : bool
1748 253969843 : simplify_using_ranges::simplify (gimple_stmt_iterator *gsi)
1749 : {
1750 253969843 : gcc_checking_assert (query);
1751 :
1752 253969843 : gimple *stmt = gsi_stmt (*gsi);
1753 253969843 : if (is_gimple_assign (stmt))
1754 : {
1755 69049432 : enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
1756 69049432 : tree rhs1 = gimple_assign_rhs1 (stmt);
1757 69049432 : tree rhs2 = gimple_assign_rhs2 (stmt);
1758 69049432 : tree lhs = gimple_assign_lhs (stmt);
1759 69049432 : tree val1 = NULL_TREE, val2 = NULL_TREE;
1760 69049432 : use_operand_p use_p;
1761 69049432 : gimple *use_stmt;
1762 :
1763 : /* Convert:
1764 : LHS = CST BINOP VAR
1765 : Where VAR is two-valued and LHS is used in GIMPLE_COND only
1766 : To:
1767 : LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
1768 :
1769 : Also handles:
1770 : LHS = VAR BINOP CST
1771 : Where VAR is two-valued and LHS is used in GIMPLE_COND only
1772 : To:
1773 : LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
1774 :
1775 69049432 : if (TREE_CODE_CLASS (rhs_code) == tcc_binary
1776 14679442 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1777 10483082 : && ((TREE_CODE (rhs1) == INTEGER_CST
1778 188841 : && TREE_CODE (rhs2) == SSA_NAME)
1779 10294790 : || (TREE_CODE (rhs2) == INTEGER_CST
1780 6843596 : && TREE_CODE (rhs1) == SSA_NAME))
1781 7031339 : && single_imm_use (lhs, &use_p, &use_stmt)
1782 74464247 : && gimple_code (use_stmt) == GIMPLE_COND)
1783 :
1784 : {
1785 484157 : tree new_rhs1 = NULL_TREE;
1786 484157 : tree new_rhs2 = NULL_TREE;
1787 484157 : tree cmp_var = NULL_TREE;
1788 :
1789 484157 : if (TREE_CODE (rhs2) == SSA_NAME
1790 484157 : && two_valued_val_range_p (rhs2, &val1, &val2, stmt))
1791 : {
1792 : /* Optimize RHS1 OP [VAL1, VAL2]. */
1793 243 : new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
1794 243 : new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
1795 243 : cmp_var = rhs2;
1796 : }
1797 483914 : else if (TREE_CODE (rhs1) == SSA_NAME
1798 483914 : && two_valued_val_range_p (rhs1, &val1, &val2, stmt))
1799 : {
1800 : /* Optimize [VAL1, VAL2] OP RHS2. */
1801 7415 : new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
1802 7415 : new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
1803 7415 : cmp_var = rhs1;
1804 : }
1805 :
1806 : /* If we could not find two-vals or the optimization is invalid as
1807 : in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
1808 484157 : if (new_rhs1 && new_rhs2)
1809 : {
1810 7658 : tree cond = gimple_build (gsi, true, GSI_SAME_STMT,
1811 : UNKNOWN_LOCATION,
1812 : EQ_EXPR, boolean_type_node,
1813 : cmp_var, val1);
1814 7658 : gimple_assign_set_rhs_with_ops (gsi,
1815 : COND_EXPR, cond,
1816 : new_rhs1,
1817 : new_rhs2);
1818 7658 : update_stmt (gsi_stmt (*gsi));
1819 7658 : fold_stmt (gsi, follow_single_use_edges);
1820 8400611 : return true;
1821 : }
1822 : }
1823 :
1824 69041774 : if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
1825 1310650 : return simplify_compare_assign_using_ranges_1 (gsi, stmt);
1826 :
1827 67731124 : switch (rhs_code)
1828 : {
1829 :
1830 : /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
1831 : and BIT_AND_EXPR respectively if the first operand is greater
1832 : than zero and the second operand is an exact power of two.
1833 : Also optimize TRUNC_MOD_EXPR away if the second operand is
1834 : constant and the first operand already has the right value
1835 : range. */
1836 334933 : case TRUNC_DIV_EXPR:
1837 334933 : case TRUNC_MOD_EXPR:
1838 334933 : if ((TREE_CODE (rhs1) == SSA_NAME
1839 334933 : || TREE_CODE (rhs1) == INTEGER_CST)
1840 334933 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1841 333555 : return simplify_div_or_mod_using_ranges (gsi, stmt);
1842 : break;
1843 :
1844 : /* Transform ABS (X) into X or -X as appropriate. */
1845 61825 : case ABS_EXPR:
1846 61825 : if (TREE_CODE (rhs1) == SSA_NAME
1847 61825 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1848 9589 : return simplify_abs_using_ranges (gsi, stmt);
1849 : break;
1850 :
1851 1317798 : case BIT_AND_EXPR:
1852 1317798 : case BIT_IOR_EXPR:
1853 : /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR if all the bits
1854 : being cleared are already cleared or all the bits being set
1855 : are already set. Beware that boolean types must be handled
1856 : logically (see range-op.cc) unless they have precision 1. */
1857 2549246 : if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
1858 2511018 : && (TREE_CODE (TREE_TYPE (rhs1)) != BOOLEAN_TYPE
1859 320103 : || TYPE_PRECISION (TREE_TYPE (rhs1)) == 1))
1860 1279570 : return simplify_bit_ops_using_ranges (gsi, stmt);
1861 : break;
1862 :
1863 5969338 : CASE_CONVERT:
1864 5969338 : if (TREE_CODE (rhs1) == SSA_NAME
1865 5969338 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1866 4859391 : return simplify_conversion_using_ranges (gsi, stmt);
1867 : break;
1868 :
1869 258705 : case FLOAT_EXPR:
1870 258705 : if (TREE_CODE (rhs1) == SSA_NAME
1871 258705 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
1872 256324 : return simplify_float_conversion_using_ranges (gsi, stmt);
1873 : break;
1874 :
1875 235295 : case MIN_EXPR:
1876 235295 : case MAX_EXPR:
1877 235295 : return simplify_min_or_max_using_ranges (gsi, stmt);
1878 :
1879 415315 : case RSHIFT_EXPR:
1880 415315 : {
1881 415315 : tree op0 = gimple_assign_rhs1 (stmt);
1882 415315 : tree type = TREE_TYPE (op0);
1883 415315 : int_range_max range;
1884 415315 : if (TYPE_SIGN (type) == SIGNED
1885 415315 : && query->range_of_expr (range, op0, stmt))
1886 : {
1887 108579 : unsigned prec = TYPE_PRECISION (TREE_TYPE (op0));
1888 217158 : int_range<2> nzm1 (type, wi::minus_one (prec), wi::zero (prec),
1889 108579 : VR_ANTI_RANGE);
1890 108579 : range.intersect (nzm1);
1891 : // If there are no ranges other than [-1, 0] remove the shift.
1892 108579 : if (range.undefined_p ())
1893 : {
1894 80 : gimple_assign_set_rhs_from_tree (gsi, op0);
1895 80 : return true;
1896 : }
1897 : return false;
1898 108579 : }
1899 306736 : break;
1900 415315 : }
1901 : default:
1902 : break;
1903 : }
1904 : }
1905 184920411 : else if (gimple_code (stmt) == GIMPLE_COND)
1906 12473621 : return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
1907 172446790 : else if (gimple_code (stmt) == GIMPLE_SWITCH)
1908 60955 : return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
1909 172385835 : else if (is_gimple_call (stmt)
1910 172385835 : && gimple_call_internal_p (stmt))
1911 601993 : return simplify_internal_call_using_ranges (gsi, stmt);
1912 :
1913 : return false;
1914 : }
|