summaryrefslogtreecommitdiff
path: root/0051-Backport-phiopt-Fix-up-conditional_replacement-PR993.patch
blob: 3d4767010979755fd540f57eaf24ad12b90f121d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
From 79a974bc7bb67cf425a7839f3c1f5689e41c7ee8 Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Tue, 9 Mar 2021 19:13:11 +0100
Subject: [PATCH 03/35] [Backport] phiopt: Fix up conditional_replacement
 [PR99305]

Reference: https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=b610c30453d8e4cc88693d85a5a100d089640be5

Before my PR97690 changes, conditional_replacement would not set neg
when the nonzero arg was boolean true.
I've simplified the testing, so that it first finds the zero argument
and then checks the other argument for all the handled cases
(1, -1 and 1 << X, where the last case is what the patch added support for).
But, unfortunately I've placed the integer_all_onesp test first.
For unsigned precision 1 types such as bool integer_all_onesp, integer_onep
and integer_pow2p can all be true and the code set neg to true in that case,
which is undesirable.

The following patch tests integer_pow2p first (which is trivially true
for integer_onep too and tree_log2 in that case gives shift == 0)
and only if that isn't the case, integer_all_onesp.

2021-03-09  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/99305
	* tree-ssa-phiopt.c (conditional_replacement): Test integer_pow2p
	before integer_all_onesp instead of vice versa.

	* g++.dg/opt/pr99305.C: New test.
---
 gcc/testsuite/g++.dg/opt/pr99305.C | 26 ++++++++++++++++++++++++++
 gcc/tree-ssa-phiopt.c              |  6 +++---
 2 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/opt/pr99305.C

diff --git a/gcc/testsuite/g++.dg/opt/pr99305.C b/gcc/testsuite/g++.dg/opt/pr99305.C
new file mode 100644
index 000000000..8a91277e7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/pr99305.C
@@ -0,0 +1,26 @@
+// PR tree-optimization/99305
+// { dg-do compile }
+// { dg-options "-O3 -fno-ipa-icf -fdump-tree-optimized" }
+// { dg-final { scan-tree-dump-times " = \\\(unsigned char\\\) c_\[0-9]*\\\(D\\\);" 3 "optimized" } }
+// { dg-final { scan-tree-dump-times " = \[^\n\r]* \\+ \[0-9]*;" 3 "optimized" } }
+// { dg-final { scan-tree-dump-times " = \[^\n\r]* <= 9;" 3 "optimized" } }
+// { dg-final { scan-tree-dump-not "if \\\(c_\[0-9]*\\\(D\\\) \[!=]= 0\\\)" "optimized" } }
+// { dg-final { scan-tree-dump-not " = PHI <" "optimized" } }
+
+bool
+foo (char c)
+{
+  return c >= 48 && c <= 57;
+}
+
+bool
+bar (char c)
+{
+  return c != 0 && foo (c);
+}
+
+bool
+baz (char c)
+{
+  return c != 0 && c >= 48 && c <= 57;
+}
diff --git a/gcc/tree-ssa-phiopt.c b/gcc/tree-ssa-phiopt.c
index 85587e8d1..b9be28474 100644
--- a/gcc/tree-ssa-phiopt.c
+++ b/gcc/tree-ssa-phiopt.c
@@ -774,14 +774,14 @@ conditional_replacement (basic_block cond_bb, basic_block middle_bb,
     nonzero_arg = arg0;
   else
     return false;
-  if (integer_all_onesp (nonzero_arg))
-    neg = true;
-  else if (integer_pow2p (nonzero_arg))
+  if (integer_pow2p (nonzero_arg))
     {
       shift = tree_log2 (nonzero_arg);
       if (shift && POINTER_TYPE_P (TREE_TYPE (nonzero_arg)))
 	return false;
     }
+  else if (integer_all_onesp (nonzero_arg))
+    neg = true;
   else
     return false;
 
-- 
2.27.0.windows.1