Issue 90119
Summary LLVM does not eliminate or hoist redundant bounds checks for std::vector and std::string
Labels missed-optimization, hardening
Assignees
Reporter ldionne
    Given the following code:

```c++
#include <vector>
#include <span>
#include <string>

template <class T>
void use(T const&);

template <class Container>
void f(Container container) {
  for (int i = 0; i != container.size(); ++i) {
 use(container[i]);
  }
}

template void f(std::string);
template void f(std::vector<int>);
template void f(std::span<int>);
```


Godbolt: https://godbolt.org/z/x8fdv8s63

When we compile with hardening enabled, there is a bounds check inside `operator[]`. That check is basically equivalent to `index < size()`. However, it doesn't get elided even though the loop condition should guarantee that the condition always holds. It seems to be elided when iterating over a `span`, but not a `string` or a `vector`, which makes me think the optimizer could be improved.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to