aboutsummaryrefslogtreecommitdiff
path: root/gcc/match.pd
diff options
context:
space:
mode:
authorJiufu Guo <guojiufu@linux.ibm.com>2023-09-06 21:38:11 +0800
committerguojiufu <guojiufu@linux.ibm.com>2023-09-13 09:31:53 +0800
commit8d8bc560b6ab7f3153db23ffb37157528e5b2c9a (patch)
treea390e8eb40bcba37786d317a1d334f7ae39b04b4 /gcc/match.pd
parenta1c20158c68fe537c485006fe95ca198b6d15005 (diff)
downloadgcc-8d8bc560b6ab7f3153db23ffb37157528e5b2c9a.zip
gcc-8d8bc560b6ab7f3153db23ffb37157528e5b2c9a.tar.gz
gcc-8d8bc560b6ab7f3153db23ffb37157528e5b2c9a.tar.bz2
Checking undefined_p before using the vr
For pattern "(X + C) / N": "div (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)", Even if "X" has value-range and "X + C" does not overflow, "@3" may still be undefined. Like below example: _3 = _2 + -5; if (0 != 0) goto <bb 3>; [34.00%] else goto <bb 4>; [66.00%] ;; succ: 3 ;; 4 ;; basic block 3, loop depth 0 ;; pred: 2 _5 = _3 / 5; ;; succ: 4 The whole pattern "(_2 + -5 ) / 5" is in "bb 3", but "bb 3" would be unreachable (because "if (0 != 0)" is always false). And "get_range_query (cfun)->range_of_expr (vr3, @3)" is checked in "bb 3", "range_of_expr" gets an "undefined vr3". Where "@3" is "_5". So, before using "vr3", it would be safe to check "!vr3.undefined_p ()". PR tree-optimization/111303 gcc/ChangeLog: * match.pd ((X - N * M) / N): Add undefined_p checking. ((X + N * M) / N): Likewise. ((X + C) div_rshift N): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/pr111303.c: New test.
Diffstat (limited to 'gcc/match.pd')
-rw-r--r--gcc/match.pd3
1 files changed, 3 insertions, 0 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 39c7ea1..693638f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -975,6 +975,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* "X+(N*M)" doesn't overflow. */
&& range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3)
&& get_range_query (cfun)->range_of_expr (vr4, @4)
+ && !vr4.undefined_p ()
/* "X+N*M" is not with opposite sign as "X". */
&& (TYPE_UNSIGNED (type)
|| (vr0.nonnegative_p () && vr4.nonnegative_p ())
@@ -995,6 +996,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* "X - (N*M)" doesn't overflow. */
&& range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3)
&& get_range_query (cfun)->range_of_expr (vr4, @4)
+ && !vr4.undefined_p ()
/* "X-N*M" is not with opposite sign as "X". */
&& (TYPE_UNSIGNED (type)
|| (vr0.nonnegative_p () && vr4.nonnegative_p ())
@@ -1025,6 +1027,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* "X+C" doesn't overflow. */
&& range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1)
&& get_range_query (cfun)->range_of_expr (vr3, @3)
+ && !vr3.undefined_p ()
/* "X+C" and "X" are not of opposite sign. */
&& (TYPE_UNSIGNED (type)
|| (vr0.nonnegative_p () && vr3.nonnegative_p ())