[ConstantFold] Don't skip the first gep index when folding geps

We neglected to check if the first index made the GEP ineligible for
'inbounds'.

This fixes PR23753.

llvm-svn: 239015
This commit is contained in:
David Majnemer 2015-06-04 07:01:56 +00:00
parent 214335d703
commit 38eb9f46db
2 changed files with 32 additions and 3 deletions

View File

@ -2165,9 +2165,9 @@ static Constant *ConstantFoldGetElementPtrImpl(Type *PointeeTy, Constant *C,
// factored out into preceding dimensions.
bool Unknown = false;
SmallVector<Constant *, 8> NewIdxs;
Type *Ty = PointeeTy;
Type *Prev = C->getType();
for (unsigned i = 1, e = Idxs.size(); i != e;
Type *Ty = C->getType();
Type *Prev = nullptr;
for (unsigned i = 0, e = Idxs.size(); i != e;
Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
if (isa<ArrayType>(Ty) || isa<VectorType>(Ty))

View File

@ -0,0 +1,29 @@
; RUN: opt < %s -loop-reduce -S | FileCheck %s
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@a = external global i32, align 4
@d = external global i8, align 1
; CHECK-LABEL: void @f
define void @f() {
entry:
br label %for.body
for.body:
%indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
%arrayidx = getelementptr inbounds i32, i32* @a, i64 %indvars.iv
%cmp = icmp ne i32* %arrayidx, bitcast (i8* @d to i32*)
%indvars.iv.next = add i64 %indvars.iv, 1
br i1 %cmp, label %for.body, label %for.end
; CHECK: %[[phi:.*]] = phi i8* [ %[[gep:.*]], {{.*}} ], [ getelementptr (i8, i8* @d, i64 sub (i64 4, i64 ptrtoint (i32* @a to i64))), {{.*}} ]
; CHECK-NEXT: %[[gep]] = getelementptr i8, i8* %[[phi]], i64 -4
; CHECK-NEXT: %[[cst:.*]] = bitcast i8* %[[gep]] to i32*
; CHECK-NEXT: %[[cmp:.*]] = icmp ne i32* %[[cst]], null
; CHECK-NEXT: br i1 %[[cmp]]
for.end:
ret void
}