Hello.
First, I must confess I'm not familiar with bit constant propagation.
However, I learnt something during debugging session related to this PR.
As mentioned in the PR, let's consider the following example:
int
__attribute__((noinline))
foo(int arg)
{
if (arg == 3)
return 1;
if (arg == 4)
return 123;
__builtin_unreachable ();
}
during WPA we find all calls of the function
(yes the call with value 5 is UBSAN):
Node: foo/0:
param [0]: 5 [loc_time: 4, loc_size: 2, prop_time: 0, prop_size: 0]
3 [loc_time: 3, loc_size: 3, prop_time: 0, prop_size: 0]
ctxs: VARIABLE
Bits: value = 0x5, mask = 0x6
in LTRANS we have the following VRP info:
# RANGE [3, 3] NONZERO 3
when we AND masks in get_default_value we end up with 6 & 3 = 2 (0x010).
That means the only second (least significant bit) is unknown and
value (5 = 0x101) & ~mask gives us either 7 (0x111) or 5 (0x101).
That's why if (arg_2(D) == 3) gets optimized to false.
Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
Thoughts?
Martin
gcc/ChangeLog:
PR ipa/96482
* tree-ssa-ccp.c (get_default_value): Known bits from IPA bits
CP should be masked against nonzero bits get from VRP.
gcc/testsuite/ChangeLog:
PR ipa/96482
* gcc.dg/ipa/pr96482.c: New test.
---
gcc/testsuite/gcc.dg/ipa/pr96482.c | 44 ++++++++++++++++++++++++++++++
gcc/tree-ssa-ccp.c | 14 ++++++----
2 files changed, 53 insertions(+), 5 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/ipa/pr96482.c
diff --git a/gcc/testsuite/gcc.dg/ipa/pr96482.c
b/gcc/testsuite/gcc.dg/ipa/pr96482.c
new file mode 100644
index 00000000000..68ead798d28
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/pr96482.c
@@ -0,0 +1,44 @@
+/* PR ipa/96482 */
+/* { dg-do run } */
+/* { dg-options "-O2 -flto" } */
+/* { dg-require-effective-target lto } */
+
+int
+__attribute__((noinline))
+foo(int arg)
+{
+ if (arg == 3)
+ return 1;
+ if (arg == 4)
+ return 123;
+
+ __builtin_unreachable ();
+}
+
+int
+__attribute__((noinline))
+baz(int x)
+{
+ if (x != 0)
+ return foo(3); /* called */
+
+ return 1;
+}
+
+int
+__attribute__((noinline))
+bar(int x)
+{
+ if (x == 0)
+ return foo(5); /* not executed */
+
+ return 1;
+}
+
+int main(int argc, char **argv)
+{
+ if (bar(argc) != baz(argc))
+ __builtin_abort ();
+
+ return 0;
+}
diff --git a/gcc/tree-ssa-ccp.c b/gcc/tree-ssa-ccp.c
index 7e3921869b8..d8ba5379c4a 100644
--- a/gcc/tree-ssa-ccp.c
+++ b/gcc/tree-ssa-ccp.c
@@ -299,24 +299,28 @@ get_default_value (tree var)
wide_int nonzero_bits = get_nonzero_bits (var);
tree value;
widest_int mask;
+ tree type = TREE_TYPE (var);
if (SSA_NAME_VAR (var)
&& TREE_CODE (SSA_NAME_VAR (var)) == PARM_DECL
&& ipcp_get_parm_bits (SSA_NAME_VAR (var), &value, &mask))
{
val.lattice_val = CONSTANT;
- val.value = value;
+ /* Known bits from IPA CP should be masked
+ with nonzero_bits. */
+ wide_int ipa_value = wi::to_wide (value);
+ ipa_value &= nonzero_bits;
+ val.value = wide_int_to_tree (type, ipa_value);
+
val.mask = mask;
if (nonzero_bits != -1)
- val.mask &= extend_mask (nonzero_bits,
- TYPE_SIGN (TREE_TYPE (var)));
+ val.mask &= extend_mask (nonzero_bits, TYPE_SIGN (type));
}
else if (nonzero_bits != -1)
{
val.lattice_val = CONSTANT;
val.value = build_zero_cst (TREE_TYPE (var));
- val.mask = extend_mask (nonzero_bits,
- TYPE_SIGN (TREE_TYPE (var)));
+ val.mask = extend_mask (nonzero_bits, TYPE_SIGN (type));
}
}
}
--
2.28.0