[PATCH] Remove unnecessary variable indexing into single-element arrays

hfinkel at anl.gov hfinkel at anl.gov
Thu Feb 19 16:06:49 PST 2015

Hi chandlerc, majnemer, echristo, rafael,

This patch is intended to address PR22629. To copy from the bug report:

[from the bug report]

Consider this code:

  int f(int x) {
    int a[] = {12};
    return a[x];

GCC knows to optimize this to

  movl     $12, %eax

The code generated by recent Clang at -O3 is:

  movslq   %edi, %rax
  movl     .L_ZZ1fiE1a(,%rax,4), %eax

    .long    12                      # 0xc

[end from the bug report]

This definitely seems worth fixing. I've also seen this kind of code before (as the base case of generic vector wrapper templates with one element).

The general idea is to look at the GEP feeding a load or a store, which has some variable as its first non-zero index, and determine if that index must be zero (or else an out-of-bounds access would occur). We can do this for allocas and globals with constant initializers where we know the maximum size of the underlying object. When we find such a GEP, we create a new one for the memory access with that first variable index replaced with a constant zero.

Even if we can't eliminate the memory access (and sometimes we can't), it is still useful because it removes unnecessary indexing calculations.



-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7776.20351.patch
Type: text/x-patch
Size: 10175 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150220/dd68cfb0/attachment.bin>

More information about the llvm-commits mailing list