Files
llvm-6502/test/Transforms/InstCombine/mem-gep-zidx.ll
Hal Finkel 5ecf528fc2 [InstCombine] Remove unnecessary variable indexing into single-element arrays
This change addresses a deficiency pointed out in 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
ret

The code generated by recent Clang at -O3 is:

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

.L_ZZ1fiE1a:
  .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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229959 91177308-0d34-0410-b5e6-96231b3b80d8
2015-02-20 03:05:53 +00:00

49 lines
1.2 KiB
LLVM

; RUN: opt -S -instcombine < %s | FileCheck %s
target datalayout = "E-m:e-i64:64-n32:64"
target triple = "powerpc64-unknown-linux-gnu"
@f.a = private unnamed_addr constant [1 x i32] [i32 12], align 4
@f.b = private unnamed_addr constant [1 x i32] [i32 55], align 4
define signext i32 @test1(i32 signext %x) #0 {
entry:
%idxprom = sext i32 %x to i64
%arrayidx = getelementptr inbounds [1 x i32]* @f.a, i64 0, i64 %idxprom
%0 = load i32* %arrayidx, align 4
ret i32 %0
; CHECK-LABEL: @test1
; CHECK: ret i32 12
}
declare void @foo(i64* %p)
define void @test2(i32 signext %x, i64 %v) #0 {
entry:
%p = alloca i64
%idxprom = sext i32 %x to i64
%arrayidx = getelementptr inbounds i64* %p, i64 %idxprom
store i64 %v, i64* %arrayidx
call void @foo(i64* %p)
ret void
; CHECK-LABEL: @test2
; CHECK: %p = alloca i64
; CHECK: store i64 %v, i64* %p
; CHECK: ret void
}
define signext i32 @test3(i32 signext %x, i1 %y) #0 {
entry:
%idxprom = sext i32 %x to i64
%p = select i1 %y, [1 x i32]* @f.a, [1 x i32]* @f.b
%arrayidx = getelementptr inbounds [1 x i32]* %p, i64 0, i64 %idxprom
%0 = load i32* %arrayidx, align 4
ret i32 %0
; CHECK-LABEL: @test3
; CHECK: getelementptr inbounds [1 x i32]* %p, i64 0, i64 0
}
attributes #0 = { nounwind readnone }