Issue 86949
Summary Missed optimization (inline breaks knowledge about [[pure]])
Labels new issue
Assignees
Reporter kelbon
    
This code (intuitively) seams easy to optimize: function 'sum_foo' is pure, it does not change its arguments, so its result will be always same. Expected calculate one time + multiply by 100'000

But looks like compiler inlines 'sum_foo' and forgets, that it is [[pure]]. Then it tries to do all smart things about loops, but do not understands, that loop does not change its body
So, i added [[gnu::noinline]] and both gcc and clang create 100'000 * sum_foo, as expected.

```asm
foo(int*, int):                              # @foo(int*, int)
        push    rax
        call    sum_foo(int const*, int)
        imul    rax, rax, 100000
        pop     rcx
 ret
```

I think it is very confusing behavior and may be fixed

```cpp
[[gnu::pure]] [[gnu::noinline]] static long long sum_foo(const int* data, int arraySize) noexcept {
    long long sum = 0;
    for (int c = 0; c < arraySize; ++c)
        sum += data[c];
 return sum;
}
long long foo(int* data, int arraySize) noexcept {
 long long sum = 0;
   for (int i = 0; i < 100000; ++i)
       sum += sum_foo(data, arraySize);
   return sum;
}

```
https://godbolt.org/z/53Y8P34dx

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to