llvm-project
llvm-project copied to clipboard
Clang fails to remove unnecessary loop
The compiler seems to be pre-computing the result, but then it also doesnt remove the loop that was there to compute the value
#include <cstdint>
template<std::size_t Index>
static void testForLoop01(int32_t* valuesNew) {
if constexpr (Index < 8) {
*valuesNew += *valuesNew + Index;
testForLoop01<Index + 1>(valuesNew);
}
}
static void testForLoop02(int32_t* valuesNew) {
for (uint64_t x = 0; x < 8; ++x) {
*valuesNew += *valuesNew + x;
}
}
int a() {
int outputValue = 0;
for (uint64_t x = 0; x < 1024 * 1024; ++x) {
testForLoop01<0>(&outputValue);
}
return outputValue;
}
a(): # @a()
mov eax, 1048576
.LBB0_1: # =>This Inner Loop Header: Depth=1
add rax, -8
jne .LBB0_1
mov eax, -134744073
ret
godbolt link: https://godbolt.org/z/ba4xox9fv