1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
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)
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.
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/>. */
22 #include "coretypes.h"
24 #include "insn-codes.h"
28 #include "optabs-tree.h"
29 #include "gimple-pretty-print.h"
30 #include "diagnostic-core.h"
32 #include "fold-const.h"
35 #include "gimple-iterator.h"
36 #include "gimple-fold.h"
38 #include "tree-ssa-loop-niter.h"
39 #include "tree-ssa-loop.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"
50 #include "vr-values.h"
53 #include "gimple-range.h"
55 /* Returns true if EXPR is a valid value (as expected by compare_values) --
56 a gimple invariant, or SSA_NAME +- CST. */
59 valid_value_p (tree expr
)
61 if (TREE_CODE (expr
) == SSA_NAME
)
64 if (TREE_CODE (expr
) == PLUS_EXPR
65 || TREE_CODE (expr
) == MINUS_EXPR
)
66 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
67 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
69 return is_gimple_min_invariant (expr
);
72 /* Return true if op is in a boolean [0, 1] value-range. */
75 simplify_using_ranges::op_with_boolean_value_range_p (tree op
, gimple
*s
)
77 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
80 if (integer_zerop (op
)
84 if (TREE_CODE (op
) != SSA_NAME
)
87 /* ?? Errr, this should probably check for [0,0] and [1,1] as well
89 const value_range
*vr
= query
->get_value_range (op
, s
);
90 return *vr
== value_range (build_zero_cst (TREE_TYPE (op
)),
91 build_one_cst (TREE_TYPE (op
)));
94 /* Helper function for simplify_internal_call_using_ranges and
95 extract_range_basic. Return true if OP0 SUBCODE OP1 for
96 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
97 always overflow. Set *OVF to true if it is known to always
101 check_for_binary_op_overflow (range_query
*query
,
102 enum tree_code subcode
, tree type
,
103 tree op0
, tree op1
, bool *ovf
, gimple
*s
= NULL
)
105 value_range vr0
, vr1
;
106 if (TREE_CODE (op0
) == SSA_NAME
)
107 vr0
= *query
->get_value_range (op0
, s
);
108 else if (TREE_CODE (op0
) == INTEGER_CST
)
111 vr0
.set_varying (TREE_TYPE (op0
));
113 if (TREE_CODE (op1
) == SSA_NAME
)
114 vr1
= *query
->get_value_range (op1
, s
);
115 else if (TREE_CODE (op1
) == INTEGER_CST
)
118 vr1
.set_varying (TREE_TYPE (op1
));
120 tree vr0min
= vr0
.min (), vr0max
= vr0
.max ();
121 tree vr1min
= vr1
.min (), vr1max
= vr1
.max ();
122 if (!range_int_cst_p (&vr0
)
123 || TREE_OVERFLOW (vr0min
)
124 || TREE_OVERFLOW (vr0max
))
126 vr0min
= vrp_val_min (TREE_TYPE (op0
));
127 vr0max
= vrp_val_max (TREE_TYPE (op0
));
129 if (!range_int_cst_p (&vr1
)
130 || TREE_OVERFLOW (vr1min
)
131 || TREE_OVERFLOW (vr1max
))
133 vr1min
= vrp_val_min (TREE_TYPE (op1
));
134 vr1max
= vrp_val_max (TREE_TYPE (op1
));
136 *ovf
= arith_overflowed_p (subcode
, type
, vr0min
,
137 subcode
== MINUS_EXPR
? vr1max
: vr1min
);
138 if (arith_overflowed_p (subcode
, type
, vr0max
,
139 subcode
== MINUS_EXPR
? vr1min
: vr1max
) != *ovf
)
141 if (subcode
== MULT_EXPR
)
143 if (arith_overflowed_p (subcode
, type
, vr0min
, vr1max
) != *ovf
144 || arith_overflowed_p (subcode
, type
, vr0max
, vr1min
) != *ovf
)
149 /* So far we found that there is an overflow on the boundaries.
150 That doesn't prove that there is an overflow even for all values
151 in between the boundaries. For that compute widest_int range
152 of the result and see if it doesn't overlap the range of
154 widest_int wmin
, wmax
;
157 w
[0] = wi::to_widest (vr0min
);
158 w
[1] = wi::to_widest (vr0max
);
159 w
[2] = wi::to_widest (vr1min
);
160 w
[3] = wi::to_widest (vr1max
);
161 for (i
= 0; i
< 4; i
++)
167 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
170 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
173 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
185 wmin
= wi::smin (wmin
, wt
);
186 wmax
= wi::smax (wmax
, wt
);
189 /* The result of op0 CODE op1 is known to be in range
191 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
192 widest_int wtmax
= wi::to_widest (vrp_val_max (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 if (wmax
< wtmin
|| wmin
> wtmax
)
203 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
205 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
206 all the values in the ranges.
208 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
210 - Return NULL_TREE if it is not always possible to determine the
211 value of the comparison.
213 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
214 assumed signed overflow is undefined. */
218 compare_ranges (enum tree_code comp
, const value_range
*vr0
,
219 const value_range
*vr1
, bool *strict_overflow_p
)
221 /* VARYING or UNDEFINED ranges cannot be compared. */
222 if (vr0
->varying_p ()
223 || vr0
->undefined_p ()
225 || vr1
->undefined_p ())
228 /* Anti-ranges need to be handled separately. */
229 if (vr0
->kind () == VR_ANTI_RANGE
|| vr1
->kind () == VR_ANTI_RANGE
)
231 /* If both are anti-ranges, then we cannot compute any
233 if (vr0
->kind () == VR_ANTI_RANGE
&& vr1
->kind () == VR_ANTI_RANGE
)
236 /* These comparisons are never statically computable. */
243 /* Equality can be computed only between a range and an
244 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
245 if (vr0
->kind () == VR_RANGE
)
246 /* To simplify processing, make VR0 the anti-range. */
247 std::swap (vr0
, vr1
);
249 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
251 if (compare_values_warnv (vr0
->min (), vr1
->min (), strict_overflow_p
) == 0
252 && compare_values_warnv (vr0
->max (), vr1
->max (), strict_overflow_p
) == 0)
253 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
258 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
259 operands around and change the comparison code. */
260 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
262 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
263 std::swap (vr0
, vr1
);
268 /* Equality may only be computed if both ranges represent
269 exactly one value. */
270 if (compare_values_warnv (vr0
->min (), vr0
->max (), strict_overflow_p
) == 0
271 && compare_values_warnv (vr1
->min (), vr1
->max (), strict_overflow_p
) == 0)
273 int cmp_min
= compare_values_warnv (vr0
->min (), vr1
->min (),
275 int cmp_max
= compare_values_warnv (vr0
->max (), vr1
->max (),
277 if (cmp_min
== 0 && cmp_max
== 0)
278 return boolean_true_node
;
279 else if (cmp_min
!= -2 && cmp_max
!= -2)
280 return boolean_false_node
;
282 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
283 else if (compare_values_warnv (vr0
->min (), vr1
->max (),
284 strict_overflow_p
) == 1
285 || compare_values_warnv (vr1
->min (), vr0
->max (),
286 strict_overflow_p
) == 1)
287 return boolean_false_node
;
291 else if (comp
== NE_EXPR
)
295 /* If VR0 is completely to the left or completely to the right
296 of VR1, they are always different. Notice that we need to
297 make sure that both comparisons yield similar results to
298 avoid comparing values that cannot be compared at
300 cmp1
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
301 cmp2
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
302 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
303 return boolean_true_node
;
305 /* If VR0 and VR1 represent a single value and are identical,
307 else if (compare_values_warnv (vr0
->min (), vr0
->max (),
308 strict_overflow_p
) == 0
309 && compare_values_warnv (vr1
->min (), vr1
->max (),
310 strict_overflow_p
) == 0
311 && compare_values_warnv (vr0
->min (), vr1
->min (),
312 strict_overflow_p
) == 0
313 && compare_values_warnv (vr0
->max (), vr1
->max (),
314 strict_overflow_p
) == 0)
315 return boolean_false_node
;
317 /* Otherwise, they may or may not be different. */
321 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
325 /* If VR0 is to the left of VR1, return true. */
326 tst
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
327 if ((comp
== LT_EXPR
&& tst
== -1)
328 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
329 return boolean_true_node
;
331 /* If VR0 is to the right of VR1, return false. */
332 tst
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
333 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
334 || (comp
== LE_EXPR
&& tst
== 1))
335 return boolean_false_node
;
337 /* Otherwise, we don't know. */
344 /* Given a value range VR, a value VAL and a comparison code COMP, return
345 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
346 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
347 always returns false. Return NULL_TREE if it is not always
348 possible to determine the value of the comparison. Also set
349 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
350 assumed signed overflow is undefined. */
353 compare_range_with_value (enum tree_code comp
, const value_range
*vr
,
354 tree val
, bool *strict_overflow_p
)
356 if (vr
->varying_p () || vr
->undefined_p ())
359 /* Anti-ranges need to be handled separately. */
360 if (vr
->kind () == VR_ANTI_RANGE
)
362 /* For anti-ranges, the only predicates that we can compute at
363 compile time are equality and inequality. */
370 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
371 if (!vr
->may_contain_p (val
))
372 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
379 /* EQ_EXPR may only be computed if VR represents exactly
381 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0)
383 int cmp
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
385 return boolean_true_node
;
386 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
387 return boolean_false_node
;
389 else if (compare_values_warnv (val
, vr
->min (), strict_overflow_p
) == -1
390 || compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1)
391 return boolean_false_node
;
395 else if (comp
== NE_EXPR
)
397 /* If VAL is not inside VR, then they are always different. */
398 if (compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1
399 || compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 1)
400 return boolean_true_node
;
402 /* If VR represents exactly one value equal to VAL, then return
404 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0
405 && compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 0)
406 return boolean_false_node
;
408 /* Otherwise, they may or may not be different. */
411 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
415 /* If VR is to the left of VAL, return true. */
416 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
417 if ((comp
== LT_EXPR
&& tst
== -1)
418 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
419 return boolean_true_node
;
421 /* If VR is to the right of VAL, return false. */
422 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
423 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
424 || (comp
== LE_EXPR
&& tst
== 1))
425 return boolean_false_node
;
427 /* Otherwise, we don't know. */
430 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
434 /* If VR is to the right of VAL, return true. */
435 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
436 if ((comp
== GT_EXPR
&& tst
== 1)
437 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
438 return boolean_true_node
;
440 /* If VR is to the left of VAL, return false. */
441 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
442 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
443 || (comp
== GE_EXPR
&& tst
== -1))
444 return boolean_false_node
;
446 /* Otherwise, we don't know. */
454 fix_overflow (tree
*min
, tree
*max
)
456 /* Even for valid range info, sometimes overflow flag will leak in.
457 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
459 if (TREE_OVERFLOW_P (*min
))
460 *min
= drop_tree_overflow (*min
);
461 if (TREE_OVERFLOW_P (*max
))
462 *max
= drop_tree_overflow (*max
);
464 gcc_checking_assert (compare_values (*min
, *max
) != 1);
467 /* Given a VAR in STMT within LOOP, determine the bounds of the
468 variable and store it in MIN/MAX and return TRUE. If no bounds
469 could be determined, return FALSE. */
472 bounds_of_var_in_loop (tree
*min
, tree
*max
, range_query
*query
,
473 class loop
*loop
, gimple
*stmt
, tree var
)
475 tree init
, step
, chrec
, tmin
, tmax
, type
= TREE_TYPE (var
);
476 enum ev_direction dir
;
479 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
481 /* Like in PR19590, scev can return a constant function. */
482 if (is_gimple_min_invariant (chrec
))
485 fix_overflow (min
, max
);
489 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
492 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
493 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
498 Value_Range
rinit (TREE_TYPE (init
));
499 Value_Range
rstep (TREE_TYPE (step
));
500 /* If INIT is an SSA with a singleton range, set INIT to said
501 singleton, otherwise leave INIT alone. */
502 if (TREE_CODE (init
) == SSA_NAME
503 && query
->range_of_expr (rinit
, init
, stmt
))
504 rinit
.singleton_p (&init
);
505 /* Likewise for step. */
506 if (TREE_CODE (step
) == SSA_NAME
507 && query
->range_of_expr (rstep
, step
, stmt
))
508 rstep
.singleton_p (&step
);
510 /* If STEP is symbolic, we can't know whether INIT will be the
511 minimum or maximum value in the range. Also, unless INIT is
512 a simple expression, compare_values and possibly other functions
513 in tree-vrp won't be able to handle it. */
514 if (step
== NULL_TREE
515 || !is_gimple_min_invariant (step
)
516 || !valid_value_p (init
))
519 dir
= scev_direction (chrec
);
520 if (/* Do not adjust ranges if we do not know whether the iv increases
522 dir
== EV_DIR_UNKNOWN
523 /* ... or if it may wrap. */
524 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
525 get_chrec_loop (chrec
), true))
528 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
529 tmin
= lower_bound_in_type (type
, type
);
531 tmin
= TYPE_MIN_VALUE (type
);
532 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
533 tmax
= upper_bound_in_type (type
, type
);
535 tmax
= TYPE_MAX_VALUE (type
);
537 /* Try to use estimated number of iterations for the loop to constrain the
538 final value in the evolution. */
539 if (TREE_CODE (step
) == INTEGER_CST
540 && is_gimple_val (init
)
541 && (TREE_CODE (init
) != SSA_NAME
542 || (query
->range_of_expr (r
, init
, stmt
)
543 && r
.kind () == VR_RANGE
)))
547 /* We are only entering here for loop header PHI nodes, so using
548 the number of latch executions is the correct thing to use. */
549 if (max_loop_iterations (loop
, &nit
))
551 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
552 wi::overflow_type overflow
;
554 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
556 /* If the multiplication overflowed we can't do a meaningful
557 adjustment. Likewise if the result doesn't fit in the type
558 of the induction variable. For a signed type we have to
559 check whether the result has the expected signedness which
560 is that of the step as number of iterations is unsigned. */
562 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
564 || wi::gts_p (wtmp
, 0) == wi::gts_p (wi::to_wide (step
), 0)))
566 value_range maxvr
, vr0
, vr1
;
567 if (TREE_CODE (init
) == SSA_NAME
)
568 query
->range_of_expr (vr0
, init
, stmt
);
569 else if (is_gimple_min_invariant (init
))
570 vr0
.set (init
, init
);
572 vr0
.set_varying (TREE_TYPE (init
));
573 tree tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
575 range_fold_binary_expr (&maxvr
, PLUS_EXPR
,
576 TREE_TYPE (init
), &vr0
, &vr1
);
578 /* Likewise if the addition did. */
579 if (maxvr
.kind () == VR_RANGE
)
583 if (TREE_CODE (init
) == SSA_NAME
)
584 query
->range_of_expr (initvr
, init
, stmt
);
585 else if (is_gimple_min_invariant (init
))
586 initvr
.set (init
, init
);
590 /* Check if init + nit * step overflows. Though we checked
591 scev {init, step}_loop doesn't wrap, it is not enough
592 because the loop may exit immediately. Overflow could
593 happen in the plus expression in this case. */
594 if ((dir
== EV_DIR_DECREASES
595 && compare_values (maxvr
.min (), initvr
.min ()) != -1)
596 || (dir
== EV_DIR_GROWS
597 && compare_values (maxvr
.max (), initvr
.max ()) != 1))
609 if (dir
== EV_DIR_DECREASES
)
614 fix_overflow (min
, max
);
618 /* Helper that gets the value range of the SSA_NAME with version I
619 or a symbolic range containing the SSA_NAME only if the value range
620 is varying or undefined. Uses TEM as storage for the alternate range. */
623 simplify_using_ranges::get_vr_for_comparison (int i
, value_range
*tem
,
626 /* Shallow-copy equiv bitmap. */
627 const value_range
*vr
= query
->get_value_range (ssa_name (i
), s
);
629 /* If name N_i does not have a valid range, use N_i as its own
630 range. This allows us to compare against names that may
631 have N_i in their ranges. */
632 if (vr
->varying_p () || vr
->undefined_p ())
634 tree ssa
= ssa_name (i
);
642 /* Compare all the value ranges for names equivalent to VAR with VAL
643 using comparison code COMP. Return the same value returned by
644 compare_range_with_value, including the setting of
645 *STRICT_OVERFLOW_P. */
648 simplify_using_ranges::compare_name_with_value
649 (enum tree_code comp
, tree var
, tree val
,
650 bool *strict_overflow_p
, gimple
*s
)
652 /* Start at -1. Set it to 0 if we do a comparison without relying
653 on overflow, or 1 if all comparisons rely on overflow. */
654 int used_strict_overflow
= -1;
656 /* Compare vars' value range with val. */
658 const value_range
*equiv_vr
659 = get_vr_for_comparison (SSA_NAME_VERSION (var
), &tem_vr
, s
);
661 tree retval
= compare_range_with_value (comp
, equiv_vr
, val
, &sop
);
663 used_strict_overflow
= sop
? 1 : 0;
665 if (retval
&& used_strict_overflow
> 0)
666 *strict_overflow_p
= true;
670 /* Helper function for vrp_evaluate_conditional_warnv & other
674 simplify_using_ranges::vrp_evaluate_conditional_warnv_with_ops_using_ranges
675 (enum tree_code code
, tree op0
, tree op1
, bool * strict_overflow_p
,
678 const value_range
*vr0
, *vr1
;
679 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? query
->get_value_range (op0
, s
) : NULL
;
680 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? query
->get_value_range (op1
, s
) : NULL
;
682 tree res
= NULL_TREE
;
684 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
686 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
688 res
= (compare_range_with_value
689 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
693 /* Helper function for vrp_evaluate_conditional_warnv. */
696 simplify_using_ranges::vrp_evaluate_conditional_warnv_with_ops
700 bool *strict_overflow_p
,
707 /* We only deal with integral and pointer types. */
708 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
709 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
712 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
713 as a simple equality test, then prefer that over its current form
716 An overflow test which collapses to an equality test can always be
717 expressed as a comparison of one argument against zero. Overflow
718 occurs when the chosen argument is zero and does not occur if the
719 chosen argument is not zero. */
721 if (overflow_comparison_p (code
, op0
, op1
, &x
))
723 wide_int max
= wi::max_value (TYPE_PRECISION (TREE_TYPE (op0
)), UNSIGNED
);
724 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
725 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
726 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
727 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
728 if (integer_zerop (x
))
731 code
= (code
== LT_EXPR
|| code
== LE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
733 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
734 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
735 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
736 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
737 else if (wi::to_wide (x
) == max
- 1)
740 op1
= wide_int_to_tree (TREE_TYPE (op0
), 0);
741 code
= (code
== GT_EXPR
|| code
== GE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
745 value_range vro
, vri
;
746 if (code
== GT_EXPR
|| code
== GE_EXPR
)
748 vro
.set (TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
, VR_ANTI_RANGE
);
749 vri
.set (TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
751 else if (code
== LT_EXPR
|| code
== LE_EXPR
)
753 vro
.set (TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
754 vri
.set (TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
, VR_ANTI_RANGE
);
758 const value_range
*vr0
= query
->get_value_range (op0
, stmt
);
759 /* If vro, the range for OP0 to pass the overflow test, has
760 no intersection with *vr0, OP0's known range, then the
761 overflow test can't pass, so return the node for false.
762 If it is the inverted range, vri, that has no
763 intersection, then the overflow test must pass, so return
764 the node for true. In other cases, we could proceed with
765 a simplified condition comparing OP0 and X, with LE_EXPR
766 for previously LE_ or LT_EXPR and GT_EXPR otherwise, but
767 the comments next to the enclosing if suggest it's not
768 generally profitable to do so. */
769 vro
.legacy_verbose_intersect (vr0
);
770 if (vro
.undefined_p ())
771 return boolean_false_node
;
772 vri
.legacy_verbose_intersect (vr0
);
773 if (vri
.undefined_p ())
774 return boolean_true_node
;
778 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
779 (code
, op0
, op1
, strict_overflow_p
, stmt
)))
782 *only_ranges
= false;
783 if (TREE_CODE (op0
) == SSA_NAME
)
784 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
, stmt
);
785 else if (TREE_CODE (op1
) == SSA_NAME
)
786 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
787 strict_overflow_p
, stmt
);
791 /* Visit conditional statement STMT. If we can determine which edge
792 will be taken out of STMT's basic block, record it in
793 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
796 simplify_using_ranges::vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
800 *taken_edge_p
= NULL
;
802 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
807 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
808 print_gimple_stmt (dump_file
, stmt
, 0);
809 fprintf (dump_file
, "\nWith known ranges\n");
811 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
813 fprintf (dump_file
, "\t");
814 print_generic_expr (dump_file
, use
);
815 fprintf (dump_file
, ": ");
816 Value_Range
r (TREE_TYPE (use
));
817 query
->range_of_expr (r
, use
, stmt
);
821 fprintf (dump_file
, "\n");
825 val
= vrp_evaluate_conditional_warnv_with_ops (stmt
,
826 gimple_cond_code (stmt
),
827 gimple_cond_lhs (stmt
),
828 gimple_cond_rhs (stmt
),
831 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
833 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
835 fprintf (dump_file
, "\nPredicate evaluates to: ");
836 if (val
== NULL_TREE
)
837 fprintf (dump_file
, "DON'T KNOW\n");
839 print_generic_stmt (dump_file
, val
);
843 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
844 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
845 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
846 Returns true if the default label is not needed. */
849 find_case_label_ranges (gswitch
*stmt
, const value_range
*vr
,
850 size_t *min_idx1
, size_t *max_idx1
,
851 size_t *min_idx2
, size_t *max_idx2
)
854 unsigned int n
= gimple_switch_num_labels (stmt
);
856 tree case_low
, case_high
;
857 tree min
= vr
->min (), max
= vr
->max ();
859 gcc_checking_assert (!vr
->varying_p () && !vr
->undefined_p ());
861 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
863 /* Set second range to empty. */
867 if (vr
->kind () == VR_RANGE
)
871 return !take_default
;
874 /* Set first range to all case labels. */
881 /* Make sure all the values of case labels [i , j] are contained in
883 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
884 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
885 if (tree_int_cst_compare (case_low
, min
) < 0)
887 if (case_high
!= NULL_TREE
888 && tree_int_cst_compare (max
, case_high
) < 0)
894 /* If the range spans case labels [i, j], the corresponding anti-range spans
895 the labels [1, i - 1] and [j + 1, n - 1]. */
921 /* Simplify boolean operations if the source is known
922 to be already a boolean. */
924 simplify_using_ranges::simplify_truth_ops_using_ranges
925 (gimple_stmt_iterator
*gsi
,
928 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
930 bool need_conversion
;
932 /* We handle only !=/== case here. */
933 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
935 op0
= gimple_assign_rhs1 (stmt
);
936 if (!op_with_boolean_value_range_p (op0
, stmt
))
939 op1
= gimple_assign_rhs2 (stmt
);
940 if (!op_with_boolean_value_range_p (op1
, stmt
))
943 /* Reduce number of cases to handle to NE_EXPR. As there is no
944 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
945 if (rhs_code
== EQ_EXPR
)
947 if (TREE_CODE (op1
) == INTEGER_CST
)
948 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
949 build_int_cst (TREE_TYPE (op1
), 1));
954 lhs
= gimple_assign_lhs (stmt
);
956 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
958 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
960 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
961 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
962 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
965 /* For A != 0 we can substitute A itself. */
966 if (integer_zerop (op1
))
967 gimple_assign_set_rhs_with_ops (gsi
,
969 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
970 /* For A != B we substitute A ^ B. Either with conversion. */
971 else if (need_conversion
)
973 tree tem
= make_ssa_name (TREE_TYPE (op0
));
975 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
976 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
977 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
978 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
980 value_range
vr (TREE_TYPE (tem
),
981 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
982 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
983 set_range_info (tem
, vr
);
985 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
989 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
990 update_stmt (gsi_stmt (*gsi
));
991 fold_stmt (gsi
, follow_single_use_edges
);
996 /* Simplify a division or modulo operator to a right shift or bitwise and
997 if the first operand is unsigned or is greater than zero and the second
998 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
999 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
1000 optimize it into just op0 if op0's range is known to be a subset of
1001 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
1005 simplify_using_ranges::simplify_div_or_mod_using_ranges
1006 (gimple_stmt_iterator
*gsi
,
1009 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
1011 tree op0
= gimple_assign_rhs1 (stmt
);
1012 tree op1
= gimple_assign_rhs2 (stmt
);
1013 tree op0min
= NULL_TREE
, op0max
= NULL_TREE
;
1015 const value_range
*vr
= NULL
;
1017 if (TREE_CODE (op0
) == INTEGER_CST
)
1024 vr
= query
->get_value_range (op0
, stmt
);
1025 if (range_int_cst_p (vr
))
1027 op0min
= vr
->min ();
1028 op0max
= vr
->max ();
1032 if (rhs_code
== TRUNC_MOD_EXPR
1033 && TREE_CODE (op1
) == SSA_NAME
)
1035 const value_range
*vr1
= query
->get_value_range (op1
, stmt
);
1036 if (range_int_cst_p (vr1
))
1037 op1min
= vr1
->min ();
1039 if (rhs_code
== TRUNC_MOD_EXPR
1040 && TREE_CODE (op1min
) == INTEGER_CST
1041 && tree_int_cst_sgn (op1min
) == 1
1043 && tree_int_cst_lt (op0max
, op1min
))
1045 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
1046 || tree_int_cst_sgn (op0min
) >= 0
1047 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1min
), op1min
),
1050 /* If op0 already has the range op0 % op1 has,
1051 then TRUNC_MOD_EXPR won't change anything. */
1052 gimple_assign_set_rhs_from_tree (gsi
, op0
);
1057 if (TREE_CODE (op0
) != SSA_NAME
)
1060 if (!integer_pow2p (op1
))
1062 /* X % -Y can be only optimized into X % Y either if
1063 X is not INT_MIN, or Y is not -1. Fold it now, as after
1064 remove_range_assertions the range info might be not available
1066 if (rhs_code
== TRUNC_MOD_EXPR
1067 && fold_stmt (gsi
, follow_single_use_edges
))
1072 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
1073 val
= integer_one_node
;
1078 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
1082 && integer_onep (val
)
1083 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
1085 location_t location
;
1087 if (!gimple_has_location (stmt
))
1088 location
= input_location
;
1090 location
= gimple_location (stmt
);
1091 warning_at (location
, OPT_Wstrict_overflow
,
1092 "assuming signed overflow does not occur when "
1093 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
1097 if (val
&& integer_onep (val
))
1101 if (rhs_code
== TRUNC_DIV_EXPR
)
1103 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
1104 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
1105 gimple_assign_set_rhs1 (stmt
, op0
);
1106 gimple_assign_set_rhs2 (stmt
, t
);
1110 t
= build_int_cst (TREE_TYPE (op1
), 1);
1111 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
1112 t
= fold_convert (TREE_TYPE (op0
), t
);
1114 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
1115 gimple_assign_set_rhs1 (stmt
, op0
);
1116 gimple_assign_set_rhs2 (stmt
, t
);
1120 fold_stmt (gsi
, follow_single_use_edges
);
1127 /* Simplify a min or max if the ranges of the two operands are
1128 disjoint. Return true if we do simplify. */
1131 simplify_using_ranges::simplify_min_or_max_using_ranges
1132 (gimple_stmt_iterator
*gsi
,
1135 tree op0
= gimple_assign_rhs1 (stmt
);
1136 tree op1
= gimple_assign_rhs2 (stmt
);
1140 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
1141 (LE_EXPR
, op0
, op1
, &sop
, stmt
));
1145 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
1146 (LT_EXPR
, op0
, op1
, &sop
, stmt
));
1151 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
1153 location_t location
;
1155 if (!gimple_has_location (stmt
))
1156 location
= input_location
;
1158 location
= gimple_location (stmt
);
1159 warning_at (location
, OPT_Wstrict_overflow
,
1160 "assuming signed overflow does not occur when "
1161 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
1164 /* VAL == TRUE -> OP0 < or <= op1
1165 VAL == FALSE -> OP0 > or >= op1. */
1166 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
1167 == integer_zerop (val
)) ? op0
: op1
;
1168 gimple_assign_set_rhs_from_tree (gsi
, res
);
1175 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
1176 ABS_EXPR. If the operand is <= 0, then simplify the
1177 ABS_EXPR into a NEGATE_EXPR. */
1180 simplify_using_ranges::simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
,
1183 tree op
= gimple_assign_rhs1 (stmt
);
1184 const value_range
*vr
= query
->get_value_range (op
, stmt
);
1191 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
1194 /* The range is neither <= 0 nor > 0. Now see if it is
1195 either < 0 or >= 0. */
1197 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
1203 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
1205 location_t location
;
1207 if (!gimple_has_location (stmt
))
1208 location
= input_location
;
1210 location
= gimple_location (stmt
);
1211 warning_at (location
, OPT_Wstrict_overflow
,
1212 "assuming signed overflow does not occur when "
1213 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
1216 gimple_assign_set_rhs1 (stmt
, op
);
1217 if (integer_zerop (val
))
1218 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
1220 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
1222 fold_stmt (gsi
, follow_single_use_edges
);
1230 /* value_range wrapper for wi_set_zero_nonzero_bits.
1232 Return TRUE if VR was a constant range and we were able to compute
1236 vr_set_zero_nonzero_bits (const tree expr_type
,
1237 const value_range
*vr
,
1238 wide_int
*may_be_nonzero
,
1239 wide_int
*must_be_nonzero
)
1241 if (range_int_cst_p (vr
))
1243 wi_set_zero_nonzero_bits (expr_type
,
1244 wi::to_wide (vr
->min ()),
1245 wi::to_wide (vr
->max ()),
1246 *may_be_nonzero
, *must_be_nonzero
);
1249 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1250 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1254 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
1255 If all the bits that are being cleared by & are already
1256 known to be zero from VR, or all the bits that are being
1257 set by | are already known to be one from VR, the bit
1258 operation is redundant. */
1261 simplify_using_ranges::simplify_bit_ops_using_ranges
1262 (gimple_stmt_iterator
*gsi
,
1265 tree op0
= gimple_assign_rhs1 (stmt
);
1266 tree op1
= gimple_assign_rhs2 (stmt
);
1267 tree op
= NULL_TREE
;
1268 value_range vr0
, vr1
;
1269 wide_int may_be_nonzero0
, may_be_nonzero1
;
1270 wide_int must_be_nonzero0
, must_be_nonzero1
;
1273 if (TREE_CODE (op0
) == SSA_NAME
)
1274 vr0
= *(query
->get_value_range (op0
, stmt
));
1275 else if (is_gimple_min_invariant (op0
))
1280 if (TREE_CODE (op1
) == SSA_NAME
)
1281 vr1
= *(query
->get_value_range (op1
, stmt
));
1282 else if (is_gimple_min_invariant (op1
))
1287 if (!vr_set_zero_nonzero_bits (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
1290 if (!vr_set_zero_nonzero_bits (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
1294 switch (gimple_assign_rhs_code (stmt
))
1297 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
1303 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
1311 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
1317 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
1328 if (op
== NULL_TREE
)
1331 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
1332 update_stmt (gsi_stmt (*gsi
));
1336 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
1337 a known value range VR.
1339 If there is one and only one value which will satisfy the
1340 conditional, then return that value. Else return NULL.
1342 If signed overflow must be undefined for the value to satisfy
1343 the conditional, then set *STRICT_OVERFLOW_P to true. */
1346 test_for_singularity (enum tree_code cond_code
, tree op0
,
1347 tree op1
, const value_range
*vr
)
1352 /* Extract minimum/maximum values which satisfy the conditional as it was
1354 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1356 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
1359 if (cond_code
== LT_EXPR
)
1361 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
1362 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
1363 /* Signal to compare_values_warnv this expr doesn't overflow. */
1365 suppress_warning (max
, OPT_Woverflow
);
1368 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1370 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
1373 if (cond_code
== GT_EXPR
)
1375 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
1376 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
1377 /* Signal to compare_values_warnv this expr doesn't overflow. */
1379 suppress_warning (min
, OPT_Woverflow
);
1383 /* Now refine the minimum and maximum values using any
1384 value range information we have for op0. */
1387 tree type
= TREE_TYPE (op0
);
1388 tree tmin
= wide_int_to_tree (type
, vr
->lower_bound ());
1389 tree tmax
= wide_int_to_tree (type
, vr
->upper_bound ());
1390 if (compare_values (tmin
, min
) == 1)
1392 if (compare_values (tmax
, max
) == -1)
1395 /* If the new min/max values have converged to a single value,
1396 then there is only one value which can satisfy the condition,
1397 return that value. */
1398 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
1404 /* Return whether the value range *VR fits in an integer type specified
1405 by PRECISION and UNSIGNED_P. */
1408 range_fits_type_p (const value_range
*vr
,
1409 unsigned dest_precision
, signop dest_sgn
)
1412 unsigned src_precision
;
1416 /* We can only handle integral and pointer types. */
1417 src_type
= vr
->type ();
1418 if (!INTEGRAL_TYPE_P (src_type
)
1419 && !POINTER_TYPE_P (src_type
))
1422 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
1423 and so is an identity transform. */
1424 src_precision
= TYPE_PRECISION (vr
->type ());
1425 src_sgn
= TYPE_SIGN (src_type
);
1426 if ((src_precision
< dest_precision
1427 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
1428 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
1431 /* Now we can only handle ranges with constant bounds. */
1432 if (!range_int_cst_p (vr
))
1435 /* For sign changes, the MSB of the wide_int has to be clear.
1436 An unsigned value with its MSB set cannot be represented by
1437 a signed wide_int, while a negative value cannot be represented
1438 by an unsigned wide_int. */
1439 if (src_sgn
!= dest_sgn
1440 && (wi::lts_p (wi::to_wide (vr
->min ()), 0)
1441 || wi::lts_p (wi::to_wide (vr
->max ()), 0)))
1444 /* Then we can perform the conversion on both ends and compare
1445 the result for equality. */
1446 tem
= wi::ext (wi::to_widest (vr
->min ()), dest_precision
, dest_sgn
);
1447 if (tem
!= wi::to_widest (vr
->min ()))
1449 tem
= wi::ext (wi::to_widest (vr
->max ()), dest_precision
, dest_sgn
);
1450 if (tem
!= wi::to_widest (vr
->max ()))
1456 // Clear edge E of EDGE_EXECUTABLE (it is unexecutable). If it wasn't
1457 // previously clear, propagate to successor blocks if appropriate.
1460 simplify_using_ranges::set_and_propagate_unexecutable (edge e
)
1462 // If not_executable is already set, we're done.
1463 // This works in the absence of a flag as well.
1464 if ((e
->flags
& m_not_executable_flag
) == m_not_executable_flag
)
1467 e
->flags
|= m_not_executable_flag
;
1468 m_flag_set_edges
.safe_push (e
);
1470 // Check if the destination block needs to propagate the property.
1471 basic_block bb
= e
->dest
;
1473 // If any incoming edge is executable, we are done.
1475 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1476 if ((e
->flags
& m_not_executable_flag
) == 0)
1479 // This block is also unexecutable, propagate to all exit edges as well.
1480 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1481 set_and_propagate_unexecutable (e
);
1484 /* If COND can be folded entirely as TRUE or FALSE, rewrite the
1485 conditional as such, and return TRUE. */
1488 simplify_using_ranges::fold_cond (gcond
*cond
)
1491 if (query
->range_of_stmt (r
, cond
) && r
.singleton_p ())
1493 // COND has already been folded if arguments are constant.
1494 if (TREE_CODE (gimple_cond_lhs (cond
)) != SSA_NAME
1495 && TREE_CODE (gimple_cond_rhs (cond
)) != SSA_NAME
)
1499 fprintf (dump_file
, "Folding predicate ");
1500 print_gimple_expr (dump_file
, cond
, 0);
1501 fprintf (dump_file
, " to ");
1503 edge e0
= EDGE_SUCC (gimple_bb (cond
), 0);
1504 edge e1
= EDGE_SUCC (gimple_bb (cond
), 1);
1508 fprintf (dump_file
, "0\n");
1509 gimple_cond_make_false (cond
);
1510 if (e0
->flags
& EDGE_TRUE_VALUE
)
1511 set_and_propagate_unexecutable (e0
);
1513 set_and_propagate_unexecutable (e1
);
1518 fprintf (dump_file
, "1\n");
1519 gimple_cond_make_true (cond
);
1520 if (e0
->flags
& EDGE_FALSE_VALUE
)
1521 set_and_propagate_unexecutable (e0
);
1523 set_and_propagate_unexecutable (e1
);
1529 // FIXME: Audit the code below and make sure it never finds anything.
1531 vrp_visit_cond_stmt (cond
, &taken_edge
);
1535 if (taken_edge
->flags
& EDGE_TRUE_VALUE
)
1537 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1538 fprintf (dump_file
, "\nVRP Predicate evaluates to: 1\n");
1539 gimple_cond_make_true (cond
);
1541 else if (taken_edge
->flags
& EDGE_FALSE_VALUE
)
1543 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1544 fprintf (dump_file
, "\nVRP Predicate evaluates to: 0\n");
1545 gimple_cond_make_false (cond
);
1555 /* Simplify a conditional using a relational operator to an equality
1556 test if the range information indicates only one value can satisfy
1557 the original conditional. */
1560 simplify_using_ranges::simplify_cond_using_ranges_1 (gcond
*stmt
)
1562 tree op0
= gimple_cond_lhs (stmt
);
1563 tree op1
= gimple_cond_rhs (stmt
);
1564 enum tree_code cond_code
= gimple_cond_code (stmt
);
1566 if (fold_cond (stmt
))
1569 if (cond_code
!= NE_EXPR
1570 && cond_code
!= EQ_EXPR
1571 && TREE_CODE (op0
) == SSA_NAME
1572 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
1573 && is_gimple_min_invariant (op1
))
1575 const value_range
*vr
= query
->get_value_range (op0
, stmt
);
1577 /* If we have range information for OP0, then we might be
1578 able to simplify this conditional. */
1579 if (!vr
->undefined_p () && !vr
->varying_p ())
1581 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
1586 fprintf (dump_file
, "Simplified relational ");
1587 print_gimple_stmt (dump_file
, stmt
, 0);
1588 fprintf (dump_file
, " into ");
1591 gimple_cond_set_code (stmt
, EQ_EXPR
);
1592 gimple_cond_set_lhs (stmt
, op0
);
1593 gimple_cond_set_rhs (stmt
, new_tree
);
1599 print_gimple_stmt (dump_file
, stmt
, 0);
1600 fprintf (dump_file
, "\n");
1606 /* Try again after inverting the condition. We only deal
1607 with integral types here, so no need to worry about
1608 issues with inverting FP comparisons. */
1609 new_tree
= test_for_singularity
1610 (invert_tree_comparison (cond_code
, false),
1616 fprintf (dump_file
, "Simplified relational ");
1617 print_gimple_stmt (dump_file
, stmt
, 0);
1618 fprintf (dump_file
, " into ");
1621 gimple_cond_set_code (stmt
, NE_EXPR
);
1622 gimple_cond_set_lhs (stmt
, op0
);
1623 gimple_cond_set_rhs (stmt
, new_tree
);
1629 print_gimple_stmt (dump_file
, stmt
, 0);
1630 fprintf (dump_file
, "\n");
1637 // Try to simplify casted conditions.
1638 return simplify_casted_cond (stmt
);
1641 /* STMT is a conditional at the end of a basic block.
1643 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
1644 was set via a type conversion, try to replace the SSA_NAME with the RHS
1645 of the type conversion. Doing so makes the conversion dead which helps
1646 subsequent passes. */
1649 simplify_using_ranges::simplify_casted_cond (gcond
*stmt
)
1651 tree op0
= gimple_cond_lhs (stmt
);
1652 tree op1
= gimple_cond_rhs (stmt
);
1654 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
1655 see if OP0 was set by a type conversion where the source of
1656 the conversion is another SSA_NAME with a range that fits
1657 into the range of OP0's type.
1659 If so, the conversion is redundant as the earlier SSA_NAME can be
1660 used for the comparison directly if we just massage the constant in the
1662 if (TREE_CODE (op0
) == SSA_NAME
1663 && TREE_CODE (op1
) == INTEGER_CST
)
1665 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
1668 if (!is_gimple_assign (def_stmt
))
1671 switch (gimple_assign_rhs_code (def_stmt
))
1674 innerop
= gimple_assign_rhs1 (def_stmt
);
1676 case VIEW_CONVERT_EXPR
:
1677 innerop
= TREE_OPERAND (gimple_assign_rhs1 (def_stmt
), 0);
1678 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
)))
1685 if (TREE_CODE (innerop
) == SSA_NAME
1686 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
1687 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
1688 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
1690 const value_range
*vr
= query
->get_value_range (innerop
);
1692 if (range_int_cst_p (vr
)
1693 && range_fits_type_p (vr
,
1694 TYPE_PRECISION (TREE_TYPE (op0
)),
1695 TYPE_SIGN (TREE_TYPE (op0
)))
1696 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
1698 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
1699 gimple_cond_set_lhs (stmt
, innerop
);
1700 gimple_cond_set_rhs (stmt
, newconst
);
1709 /* Simplify a switch statement using the value range of the switch
1713 simplify_using_ranges::simplify_switch_using_ranges (gswitch
*stmt
)
1715 tree op
= gimple_switch_index (stmt
);
1716 const value_range
*vr
= NULL
;
1720 size_t i
= 0, j
= 0, n
, n2
;
1723 size_t k
= 1, l
= 0;
1725 if (TREE_CODE (op
) == SSA_NAME
)
1727 vr
= query
->get_value_range (op
, stmt
);
1729 /* We can only handle integer ranges. */
1730 if (vr
->varying_p ()
1731 || vr
->undefined_p ()
1732 || vr
->symbolic_p ())
1735 /* Find case label for min/max of the value range. */
1736 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
1738 else if (TREE_CODE (op
) == INTEGER_CST
)
1740 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
1754 n
= gimple_switch_num_labels (stmt
);
1756 /* We can truncate the case label ranges that partially overlap with OP's
1758 size_t min_idx
= 1, max_idx
= 0;
1760 find_case_label_range (stmt
, vr
->min (), vr
->max (), &min_idx
, &max_idx
);
1761 if (min_idx
<= max_idx
)
1763 tree min_label
= gimple_switch_label (stmt
, min_idx
);
1764 tree max_label
= gimple_switch_label (stmt
, max_idx
);
1766 /* Avoid changing the type of the case labels when truncating. */
1767 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
1768 tree vr_min
= fold_convert (case_label_type
, vr
->min ());
1769 tree vr_max
= fold_convert (case_label_type
, vr
->max ());
1771 if (vr
->kind () == VR_RANGE
)
1773 /* If OP's value range is [2,8] and the low label range is
1774 0 ... 3, truncate the label's range to 2 .. 3. */
1775 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
1776 && CASE_HIGH (min_label
) != NULL_TREE
1777 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
1778 CASE_LOW (min_label
) = vr_min
;
1780 /* If OP's value range is [2,8] and the high label range is
1781 7 ... 10, truncate the label's range to 7 .. 8. */
1782 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
1783 && CASE_HIGH (max_label
) != NULL_TREE
1784 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
1785 CASE_HIGH (max_label
) = vr_max
;
1787 else if (vr
->kind () == VR_ANTI_RANGE
)
1789 tree one_cst
= build_one_cst (case_label_type
);
1791 if (min_label
== max_label
)
1793 /* If OP's value range is ~[7,8] and the label's range is
1794 7 ... 10, truncate the label's range to 9 ... 10. */
1795 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
1796 && CASE_HIGH (min_label
) != NULL_TREE
1797 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
1798 CASE_LOW (min_label
)
1799 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
1801 /* If OP's value range is ~[7,8] and the label's range is
1802 5 ... 8, truncate the label's range to 5 ... 6. */
1803 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
1804 && CASE_HIGH (min_label
) != NULL_TREE
1805 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
1806 CASE_HIGH (min_label
)
1807 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
1811 /* If OP's value range is ~[2,8] and the low label range is
1812 0 ... 3, truncate the label's range to 0 ... 1. */
1813 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
1814 && CASE_HIGH (min_label
) != NULL_TREE
1815 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
1816 CASE_HIGH (min_label
)
1817 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
1819 /* If OP's value range is ~[2,8] and the high label range is
1820 7 ... 10, truncate the label's range to 9 ... 10. */
1821 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
1822 && CASE_HIGH (max_label
) != NULL_TREE
1823 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
1824 CASE_LOW (max_label
)
1825 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
1829 /* Canonicalize singleton case ranges. */
1830 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
1831 CASE_HIGH (min_label
) = NULL_TREE
;
1832 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
1833 CASE_HIGH (max_label
) = NULL_TREE
;
1836 /* We can also eliminate case labels that lie completely outside OP's value
1839 /* Bail out if this is just all edges taken. */
1845 /* Build a new vector of taken case labels. */
1846 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
1849 /* Add the default edge, if necessary. */
1851 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
1853 for (; i
<= j
; ++i
, ++n2
)
1854 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
1856 for (; k
<= l
; ++k
, ++n2
)
1857 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
1859 /* Mark needed edges. */
1860 for (i
= 0; i
< n2
; ++i
)
1862 e
= find_edge (gimple_bb (stmt
),
1863 label_to_block (cfun
,
1864 CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
1865 e
->aux
= (void *)-1;
1868 /* Queue not needed edges for later removal. */
1869 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
1871 if (e
->aux
== (void *)-1)
1877 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1879 fprintf (dump_file
, "removing unreachable case label\n");
1881 to_remove_edges
.safe_push (e
);
1882 set_and_propagate_unexecutable (e
);
1883 e
->flags
&= ~EDGE_EXECUTABLE
;
1884 e
->flags
|= EDGE_IGNORE
;
1887 /* And queue an update for the stmt. */
1890 to_update_switch_stmts
.safe_push (su
);
1895 simplify_using_ranges::cleanup_edges_and_switches (void)
1901 /* Clear any edges marked as not executable. */
1902 if (m_not_executable_flag
)
1904 FOR_EACH_VEC_ELT (m_flag_set_edges
, i
, e
)
1905 e
->flags
&= ~m_not_executable_flag
;
1907 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
1908 CFG in a broken state and requires a cfg_cleanup run. */
1909 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
1912 /* Update SWITCH_EXPR case label vector. */
1913 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
1916 size_t n
= TREE_VEC_LENGTH (su
->vec
);
1918 gimple_switch_set_num_labels (su
->stmt
, n
);
1919 for (j
= 0; j
< n
; j
++)
1920 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
1921 /* As we may have replaced the default label with a regular one
1922 make sure to make it a real default label again. This ensures
1923 optimal expansion. */
1924 label
= gimple_switch_label (su
->stmt
, 0);
1925 CASE_LOW (label
) = NULL_TREE
;
1926 CASE_HIGH (label
) = NULL_TREE
;
1929 if (!to_remove_edges
.is_empty ())
1931 free_dominance_info (CDI_DOMINATORS
);
1932 loops_state_set (LOOPS_NEED_FIXUP
);
1935 to_remove_edges
.release ();
1936 to_update_switch_stmts
.release ();
1939 /* Simplify an integral conversion from an SSA name in STMT. */
1942 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
1944 tree innerop
, middleop
, finaltype
;
1946 signop inner_sgn
, middle_sgn
, final_sgn
;
1947 unsigned inner_prec
, middle_prec
, final_prec
;
1948 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
1950 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
1951 if (!INTEGRAL_TYPE_P (finaltype
))
1953 middleop
= gimple_assign_rhs1 (stmt
);
1954 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
1955 if (!is_gimple_assign (def_stmt
)
1956 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
1958 innerop
= gimple_assign_rhs1 (def_stmt
);
1959 if (TREE_CODE (innerop
) != SSA_NAME
1960 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
1963 /* Get the value-range of the inner operand. Use global ranges in
1964 case innerop was created during substitute-and-fold. */
1965 wide_int imin
, imax
;
1967 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
)))
1969 get_range_query (cfun
)->range_of_expr (vr
, innerop
, stmt
);
1970 if (vr
.undefined_p () || vr
.varying_p ())
1972 innermin
= widest_int::from (vr
.lower_bound (), TYPE_SIGN (TREE_TYPE (innerop
)));
1973 innermax
= widest_int::from (vr
.upper_bound (), TYPE_SIGN (TREE_TYPE (innerop
)));
1975 /* Simulate the conversion chain to check if the result is equal if
1976 the middle conversion is removed. */
1977 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
1978 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
1979 final_prec
= TYPE_PRECISION (finaltype
);
1981 /* If the first conversion is not injective, the second must not
1983 if (wi::gtu_p (innermax
- innermin
,
1984 wi::mask
<widest_int
> (middle_prec
, false))
1985 && middle_prec
< final_prec
)
1987 /* We also want a medium value so that we can track the effect that
1988 narrowing conversions with sign change have. */
1989 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
1990 if (inner_sgn
== UNSIGNED
)
1991 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
1994 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
1995 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
1996 innermed
= innermin
;
1998 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
1999 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
2000 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
2001 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
2003 /* Require that the final conversion applied to both the original
2004 and the intermediate range produces the same result. */
2005 final_sgn
= TYPE_SIGN (finaltype
);
2006 if (wi::ext (middlemin
, final_prec
, final_sgn
)
2007 != wi::ext (innermin
, final_prec
, final_sgn
)
2008 || wi::ext (middlemed
, final_prec
, final_sgn
)
2009 != wi::ext (innermed
, final_prec
, final_sgn
)
2010 || wi::ext (middlemax
, final_prec
, final_sgn
)
2011 != wi::ext (innermax
, final_prec
, final_sgn
))
2014 gimple_assign_set_rhs1 (stmt
, innerop
);
2015 fold_stmt (gsi
, follow_single_use_edges
);
2019 /* Simplify a conversion from integral SSA name to float in STMT. */
2022 simplify_using_ranges::simplify_float_conversion_using_ranges
2023 (gimple_stmt_iterator
*gsi
,
2026 tree rhs1
= gimple_assign_rhs1 (stmt
);
2027 const value_range
*vr
= query
->get_value_range (rhs1
, stmt
);
2028 scalar_float_mode fltmode
2029 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
2030 scalar_int_mode mode
;
2034 /* We can only handle constant ranges. */
2035 if (!range_int_cst_p (vr
))
2038 /* First check if we can use a signed type in place of an unsigned. */
2039 scalar_int_mode rhs_mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1
));
2040 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
2041 && can_float_p (fltmode
, rhs_mode
, 0) != CODE_FOR_nothing
2042 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
2044 /* If we can do the conversion in the current input mode do nothing. */
2045 else if (can_float_p (fltmode
, rhs_mode
,
2046 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
2048 /* Otherwise search for a mode we can use, starting from the narrowest
2049 integer mode available. */
2052 mode
= NARROWEST_INT_MODE
;
2055 /* If we cannot do a signed conversion to float from mode
2056 or if the value-range does not fit in the signed type
2057 try with a wider mode. */
2058 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
2059 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
2062 /* But do not widen the input. Instead leave that to the
2063 optabs expansion code. */
2064 if (!GET_MODE_WIDER_MODE (mode
).exists (&mode
)
2065 || GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
2070 /* It works, insert a truncation or sign-change before the
2071 float conversion. */
2072 tem
= make_ssa_name (build_nonstandard_integer_type
2073 (GET_MODE_PRECISION (mode
), 0));
2074 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
2075 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
2076 gimple_assign_set_rhs1 (stmt
, tem
);
2077 fold_stmt (gsi
, follow_single_use_edges
);
2082 /* Simplify an internal fn call using ranges if possible. */
2085 simplify_using_ranges::simplify_internal_call_using_ranges
2086 (gimple_stmt_iterator
*gsi
,
2089 enum tree_code subcode
;
2090 bool is_ubsan
= false;
2092 switch (gimple_call_internal_fn (stmt
))
2094 case IFN_UBSAN_CHECK_ADD
:
2095 subcode
= PLUS_EXPR
;
2098 case IFN_UBSAN_CHECK_SUB
:
2099 subcode
= MINUS_EXPR
;
2102 case IFN_UBSAN_CHECK_MUL
:
2103 subcode
= MULT_EXPR
;
2106 case IFN_ADD_OVERFLOW
:
2107 subcode
= PLUS_EXPR
;
2109 case IFN_SUB_OVERFLOW
:
2110 subcode
= MINUS_EXPR
;
2112 case IFN_MUL_OVERFLOW
:
2113 subcode
= MULT_EXPR
;
2119 tree op0
= gimple_call_arg (stmt
, 0);
2120 tree op1
= gimple_call_arg (stmt
, 1);
2124 type
= TREE_TYPE (op0
);
2125 if (VECTOR_TYPE_P (type
))
2128 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
2131 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
2132 if (!check_for_binary_op_overflow (query
, subcode
, type
, op0
, op1
, &ovf
, stmt
)
2133 || (is_ubsan
&& ovf
))
2137 location_t loc
= gimple_location (stmt
);
2139 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
2142 int prec
= TYPE_PRECISION (type
);
2145 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
2146 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
2147 utype
= build_nonstandard_integer_type (prec
, 1);
2148 if (TREE_CODE (op0
) == INTEGER_CST
)
2149 op0
= fold_convert (utype
, op0
);
2150 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
2152 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
2153 gimple_set_location (g
, loc
);
2154 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2155 op0
= gimple_assign_lhs (g
);
2157 if (TREE_CODE (op1
) == INTEGER_CST
)
2158 op1
= fold_convert (utype
, op1
);
2159 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
2161 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
2162 gimple_set_location (g
, loc
);
2163 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2164 op1
= gimple_assign_lhs (g
);
2166 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
2167 gimple_set_location (g
, loc
);
2168 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2171 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
2172 gimple_assign_lhs (g
));
2173 gimple_set_location (g
, loc
);
2174 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
2176 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
2177 gimple_assign_lhs (g
),
2178 build_int_cst (type
, ovf
));
2180 gimple_set_location (g
, loc
);
2181 gsi_replace (gsi
, g
, false);
2185 /* Return true if VAR is a two-valued variable. Set a and b with the
2186 two-values when it is true. Return false otherwise. */
2189 simplify_using_ranges::two_valued_val_range_p (tree var
, tree
*a
, tree
*b
,
2192 value_range vr
= *query
->get_value_range (var
, s
);
2193 vr
.normalize_symbolics ();
2194 if (vr
.varying_p () || vr
.undefined_p ())
2197 if ((vr
.num_pairs () == 1 && vr
.upper_bound () - vr
.lower_bound () == 1)
2198 || (vr
.num_pairs () == 2
2199 && vr
.lower_bound (0) == vr
.upper_bound (0)
2200 && vr
.lower_bound (1) == vr
.upper_bound (1)))
2202 *a
= wide_int_to_tree (TREE_TYPE (var
), vr
.lower_bound ());
2203 *b
= wide_int_to_tree (TREE_TYPE (var
), vr
.upper_bound ());
2209 simplify_using_ranges::simplify_using_ranges (range_query
*query
,
2210 int not_executable_flag
)
2213 to_remove_edges
= vNULL
;
2214 to_update_switch_stmts
= vNULL
;
2215 m_not_executable_flag
= not_executable_flag
;
2216 m_flag_set_edges
= vNULL
;
2219 simplify_using_ranges::~simplify_using_ranges ()
2221 cleanup_edges_and_switches ();
2222 m_flag_set_edges
.release ();
2225 /* Simplify STMT using ranges if possible. */
2228 simplify_using_ranges::simplify (gimple_stmt_iterator
*gsi
)
2230 gcc_checking_assert (query
);
2232 gimple
*stmt
= gsi_stmt (*gsi
);
2233 if (is_gimple_assign (stmt
))
2235 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
2236 tree rhs1
= gimple_assign_rhs1 (stmt
);
2237 tree rhs2
= gimple_assign_rhs2 (stmt
);
2238 tree lhs
= gimple_assign_lhs (stmt
);
2239 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
2240 use_operand_p use_p
;
2245 Where VAR is two-valued and LHS is used in GIMPLE_COND only
2247 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
2251 Where VAR is two-valued and LHS is used in GIMPLE_COND only
2253 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
2255 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
2256 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
2257 && ((TREE_CODE (rhs1
) == INTEGER_CST
2258 && TREE_CODE (rhs2
) == SSA_NAME
)
2259 || (TREE_CODE (rhs2
) == INTEGER_CST
2260 && TREE_CODE (rhs1
) == SSA_NAME
))
2261 && single_imm_use (lhs
, &use_p
, &use_stmt
)
2262 && gimple_code (use_stmt
) == GIMPLE_COND
)
2265 tree new_rhs1
= NULL_TREE
;
2266 tree new_rhs2
= NULL_TREE
;
2267 tree cmp_var
= NULL_TREE
;
2269 if (TREE_CODE (rhs2
) == SSA_NAME
2270 && two_valued_val_range_p (rhs2
, &val1
, &val2
, stmt
))
2272 /* Optimize RHS1 OP [VAL1, VAL2]. */
2273 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
2274 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
2277 else if (TREE_CODE (rhs1
) == SSA_NAME
2278 && two_valued_val_range_p (rhs1
, &val1
, &val2
, stmt
))
2280 /* Optimize [VAL1, VAL2] OP RHS2. */
2281 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
2282 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
2286 /* If we could not find two-vals or the optimzation is invalid as
2287 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
2288 if (new_rhs1
&& new_rhs2
)
2290 tree cond
= gimple_build (gsi
, true, GSI_SAME_STMT
,
2292 EQ_EXPR
, boolean_type_node
,
2294 gimple_assign_set_rhs_with_ops (gsi
,
2298 update_stmt (gsi_stmt (*gsi
));
2299 fold_stmt (gsi
, follow_single_use_edges
);
2308 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
2309 if the RHS is zero or one, and the LHS are known to be boolean
2311 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
2312 return simplify_truth_ops_using_ranges (gsi
, stmt
);
2315 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
2316 and BIT_AND_EXPR respectively if the first operand is greater
2317 than zero and the second operand is an exact power of two.
2318 Also optimize TRUNC_MOD_EXPR away if the second operand is
2319 constant and the first operand already has the right value
2321 case TRUNC_DIV_EXPR
:
2322 case TRUNC_MOD_EXPR
:
2323 if ((TREE_CODE (rhs1
) == SSA_NAME
2324 || TREE_CODE (rhs1
) == INTEGER_CST
)
2325 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
2326 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
2329 /* Transform ABS (X) into X or -X as appropriate. */
2331 if (TREE_CODE (rhs1
) == SSA_NAME
2332 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
2333 return simplify_abs_using_ranges (gsi
, stmt
);
2338 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
2339 if all the bits being cleared are already cleared or
2340 all the bits being set are already set. */
2341 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
2342 return simplify_bit_ops_using_ranges (gsi
, stmt
);
2346 if (TREE_CODE (rhs1
) == SSA_NAME
2347 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
2348 return simplify_conversion_using_ranges (gsi
, stmt
);
2352 if (TREE_CODE (rhs1
) == SSA_NAME
2353 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
2354 return simplify_float_conversion_using_ranges (gsi
, stmt
);
2359 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
2360 return simplify_min_or_max_using_ranges (gsi
, stmt
);
2365 tree op0
= gimple_assign_rhs1 (stmt
);
2366 tree type
= TREE_TYPE (op0
);
2367 int_range_max range
;
2368 if (TYPE_SIGN (type
) == SIGNED
2369 && query
->range_of_expr (range
, op0
, stmt
))
2371 unsigned prec
= TYPE_PRECISION (TREE_TYPE (op0
));
2372 int_range
<2> nzm1 (type
, wi::minus_one (prec
), wi::zero (prec
),
2374 range
.intersect (nzm1
);
2375 // If there are no ranges other than [-1, 0] remove the shift.
2376 if (range
.undefined_p ())
2378 gimple_assign_set_rhs_from_tree (gsi
, op0
);
2389 else if (gimple_code (stmt
) == GIMPLE_COND
)
2390 return simplify_cond_using_ranges_1 (as_a
<gcond
*> (stmt
));
2391 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
2392 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
2393 else if (is_gimple_call (stmt
)
2394 && gimple_call_internal_p (stmt
))
2395 return simplify_internal_call_using_ranges (gsi
, stmt
);