| Issue |
53367
|
| Summary |
Fails to optimize nested array access p[i/N].data[i%N]
|
| Labels |
enhancement,
clang:codegen
|
| Assignees |
|
| Reporter |
Quuxplusone
|
Clang seems to be unable to optimize a nested array access of the form `p[i/N].data[i%N]` into a simple `((T*)p)[i]`. The C test case is
```
struct CChunk { char data[2]; };
char f(struct CChunk *p, unsigned i) { return p[i/2].data[i%2]; }
```
`clang -O2` currently produces:
```
movl %esi, %eax
andl $-2, %eax
andl $1, %esi
addq %rdi, %rax
movb (%rsi,%rax), %al
retq
```
but I would prefer it to produce:
```
movzbl (%rsi,%rdi), %eax
retq
```
A more exhaustive C++ test follows ([Godbolt](https://godbolt.org/z/3E1e6c5e3)) — GCC can optimize a few of these, but right now Clang can't optimize _any_ of these.
```
template<class T, int N>
struct Chunk {
T data[N];
};
template<class T, int N, class IndexType>
int f(Chunk<T, N> *p, IndexType i) {
return p[i/N].data[i%N];
}
template int f(Chunk<char,2>*, unsigned long long); // GCC wins
template int f(Chunk<char,4>*, unsigned long long); // GCC wins
template int f(Chunk<char,2>*, unsigned);
template int f(Chunk<char,4>*, unsigned);
template int f(Chunk<int,2>*, unsigned long long); // GCC wins
template int f(Chunk<int,4>*, unsigned long long);
template int f(Chunk<int,2>*, unsigned);
template int f(Chunk<int,4>*, unsigned);
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs