Missed optimization: Clang fails to prove local object data pointer cannot alias reference parameter
Clang fails to eliminate an impossible pointer comparison between a subobject of a reference parameter and a subobject of a local automatic object. This keeps a dead branch and an unreachable call in the generated code.
Reproducer
void complicated_method(double const*, double*);
void simple_method(double const*, double*);
struct F {
double m_data[3];
double* data() { return m_data; }
const double* data() const { return m_data; }
};
double aliasTest(F const& v) {
F temp;
if (v.data() == temp.data()) {
complicated_method(v.data(), temp.data());
} else {
simple_method(v.data(), temp.data());
}
return temp.data()[0];
}
Command line:
clang++ -O3 -S -masm=intel test.cpp
Observed Behavior
Observed with Clang trunk: Clang emits a compare and keeps both calls:
cmp rax, rdi
je ...
call simple_method(...)
...
call complicated_method(...)
Expected Behavior
The v.data() == temp.data() branch should be folded to false, and only simple_method(v.data(), temp.data()) should remain. GCC 5.1 does this.
Godbolt reproducer: https://godbolt.org/z/q3ETMEccY
Thanks for the feedback.
I think the comment for function computePointerICmp in llvm/lib/Analysis/InstructionSimplify.cpp explained why we don't do the optmization.
// A significant optimization not implemented here is assuming that alloca
// addresses are not equal to incoming argument values. They don't *alias*,
// as we say, but that doesn't mean they aren't equal, so we take a
// conservative approach.
//
// This is inspired in part by C++11 5.10p1:
// "Two pointers of the same type compare equal if and only if they are both
// null, both point to the same function, or both represent the same
// address."
//
// This is pretty permissive.
//
// It's also partly due to C11 6.5.9p6:
// "Two pointers compare equal if and only if both are null pointers, both are
// pointers to the same object (including a pointer to an object and a
// subobject at its beginning) or function, both are pointers to one past the
// last element of the same array object, or one is a pointer to one past the
// end of one array object and the other is a pointer to the start of a
// different array object that happens to immediately follow the first array
// object in the address space.)
//
// C11's version is more restrictive, however there's no reason why an argument
// couldn't be a one-past-the-end value for a stack object in the caller and be
// equal to the beginning of a stack object in the callee.
//
// If the C and C++ standards are ever made sufficiently restrictive in this
// area, it may be possible to update LLVM's semantics accordingly and reinstate
// this optimization.
I think it may be possible to optimize this based on the dereferenceable attribute on the argument. This implies that the argument cannot point to the end of an object, which means that it cannot be the same as the pointer to the beginning of a different object.
@nikic Were you considering working on a fix?
No, feel free.
A nice TIL: a bit surprised to find out that we actually already fold the equality icmp of an alloca and an argument pointer (regardless of the dereferenceable attribute); and this is correct as long as we are able to demonstrate that the alloca is not captured, the only address capture comes from the comparison, and we are required to do it simultaneously for all the comparisons (https://llvm.godbolt.org/z/1x9Yrjh5v).