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:
9 __attribute__((noinline))
17 __builtin_unreachable ();
20 during WPA we find all calls of the function
21 (yes the call with value 5 is UBSAN):
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]
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.
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
49 gcc/testsuite/ChangeLog:
52 * gcc.dg/ipa/pr96482.c: New test.
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
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;
69 + m_value = wi::bit_and (wi::bit_not (mask), value);
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);
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
83 index 00000000000..68ead798d28
85 +++ b/gcc/testsuite/gcc.dg/ipa/pr96482.c
89 +/* { dg-options "-O2 -flto" } */
90 +/* { dg-require-effective-target lto } */
93 +__attribute__((noinline))
101 + __builtin_unreachable ();
105 +__attribute__((noinline))
109 + return foo(3); /* called */
115 +__attribute__((noinline))
119 + return foo(5); /* not executed */
124 +int main(int argc, char **argv)
126 + if (bar(argc) != baz(argc))
127 + __builtin_abort ();
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;
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);
143 if (nonzero_bits != -1)
144 val.mask &= extend_mask (nonzero_bits,