archrelease: copy trunk to extra-x86_64
[arch-packages.git] / gcc / trunk / ipa-fix-bit-CPP-when-combined-with-IPA-bit-CP.patch
blobcd812623cec3469bd6abb989a4fc70bd211ba913
1 From d58f078ce2d53e5dab6b3d0d5f960504268e1894 Mon Sep 17 00:00:00 2001
2 From: Martin Liska <mliska@suse.cz>
3 Date: Wed, 12 Aug 2020 09:21:51 +0200
4 Subject: [PATCH] ipa: fix bit CPP when combined with IPA bit CP
6 As mentioned in the PR, let's consider the following example:
8 int
9 __attribute__((noinline))
10 foo(int arg)
12 if (arg == 3)
13 return 1;
14 if (arg == 4)
15 return 123;
17 __builtin_unreachable ();
20 during WPA we find all calls of the function
21 (yes the call with value 5 is UBSAN):
23 Node: foo/0:
24 param [0]: 5 [loc_time: 4, loc_size: 2, prop_time: 0, prop_size: 0]
25 3 [loc_time: 3, loc_size: 3, prop_time: 0, prop_size: 0]
26 ctxs: VARIABLE
27 Bits: value = 0x5, mask = 0x6
29 in LTRANS we have the following VRP info:
31 # RANGE [3, 3] NONZERO 3
33 when we AND masks in get_default_value we end up with 6 & 3 = 2 (0x010).
34 That means the only second (least significant bit) is unknown and
35 value (5 = 0x101) & ~mask gives us either 7 (0x111) or 5 (0x101).
37 That's why if (arg_2(D) == 3) gets optimized to false.
39 gcc/ChangeLog:
41 PR ipa/96482
42 * ipa-cp.c (ipcp_bits_lattice::meet_with_1): Drop value bits
43 for bits that are unknown.
44 (ipcp_bits_lattice::set_to_constant): Likewise.
45 * tree-ssa-ccp.c (get_default_value): Add sanity check that
46 IPA CP bit info has all bits set to zero in bits that
47 are unknown.
49 gcc/testsuite/ChangeLog:
51 PR ipa/96482
52 * gcc.dg/ipa/pr96482.c: New test.
53 ---
54 gcc/ipa-cp.c | 3 +-
55 gcc/testsuite/gcc.dg/ipa/pr96482.c | 44 ++++++++++++++++++++++++++++++
56 gcc/tree-ssa-ccp.c | 3 ++
57 3 files changed, 49 insertions(+), 1 deletion(-)
58 create mode 100644 gcc/testsuite/gcc.dg/ipa/pr96482.c
60 diff --git a/gcc/ipa-cp.c b/gcc/ipa-cp.c
61 index 945a69977f3..2b21280d919 100644
62 --- a/gcc/ipa-cp.c
63 +++ b/gcc/ipa-cp.c
64 @@ -1011,7 +1011,7 @@ ipcp_bits_lattice::set_to_constant (widest_int value, widest_int mask)
66 gcc_assert (top_p ());
67 m_lattice_val = IPA_BITS_CONSTANT;
68 - m_value = value;
69 + m_value = wi::bit_and (wi::bit_not (mask), value);
70 m_mask = mask;
71 return true;
73 @@ -1048,6 +1048,7 @@ ipcp_bits_lattice::meet_with_1 (widest_int value, widest_int mask,
75 widest_int old_mask = m_mask;
76 m_mask = (m_mask | mask) | (m_value ^ value);
77 + m_value &= value;
79 if (wi::sext (m_mask, precision) == -1)
80 return set_to_bottom ();
81 diff --git a/gcc/testsuite/gcc.dg/ipa/pr96482.c b/gcc/testsuite/gcc.dg/ipa/pr96482.c
82 new file mode 100644
83 index 00000000000..68ead798d28
84 --- /dev/null
85 +++ b/gcc/testsuite/gcc.dg/ipa/pr96482.c
86 @@ -0,0 +1,44 @@
87 +/* PR ipa/96482 */
88 +/* { dg-do run } */
89 +/* { dg-options "-O2 -flto" } */
90 +/* { dg-require-effective-target lto } */
92 +int
93 +__attribute__((noinline))
94 +foo(int arg)
96 + if (arg == 3)
97 + return 1;
98 + if (arg == 4)
99 + return 123;
101 + __builtin_unreachable ();
104 +int
105 +__attribute__((noinline))
106 +baz(int x)
108 + if (x != 0)
109 + return foo(3); /* called */
111 + return 1;
114 +int
115 +__attribute__((noinline))
116 +bar(int x)
118 + if (x == 0)
119 + return foo(5); /* not executed */
121 + return 1;
124 +int main(int argc, char **argv)
126 + if (bar(argc) != baz(argc))
127 + __builtin_abort ();
129 + return 0;
131 diff --git a/gcc/tree-ssa-ccp.c b/gcc/tree-ssa-ccp.c
132 index 7e3921869b8..65dffe06530 100644
133 --- a/gcc/tree-ssa-ccp.c
134 +++ b/gcc/tree-ssa-ccp.c
135 @@ -306,6 +306,9 @@ get_default_value (tree var)
137 val.lattice_val = CONSTANT;
138 val.value = value;
139 + widest_int ipa_value = wi::to_widest (value);
140 + /* Unknown bits from IPA CP must be equal to zero. */
141 + gcc_assert (wi::bit_and (ipa_value, mask) == 0);
142 val.mask = mask;
143 if (nonzero_bits != -1)
144 val.mask &= extend_mask (nonzero_bits,
146 2.28.0