llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang fails to remove unnecessary loop

Open HomelikeBrick42 opened this issue 3 years ago • 0 comments

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

HomelikeBrick42 avatar Oct 05 '23 04:10 HomelikeBrick42