Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
[InstCombine] Infer inbounds on geps of allocas
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@277950 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
majnemer committed Aug 7, 2016
1 parent facbf76 commit 0c8a344
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 19 additions & 0 deletions lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,25 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
}
}

if (!GEP.isInBounds()) {
unsigned PtrWidth =
DL.getPointerSizeInBits(PtrOp->getType()->getPointerAddressSpace());
APInt BasePtrOffset(PtrWidth, 0);
Value *UnderlyingPtrOp =
PtrOp->stripAndAccumulateInBoundsConstantOffsets(DL,
BasePtrOffset);
if (auto *AI = dyn_cast<AllocaInst>(UnderlyingPtrOp)) {
if (GEP.accumulateConstantOffset(DL, BasePtrOffset) &&
BasePtrOffset.isNonNegative()) {
APInt AllocSize(PtrWidth, DL.getTypeAllocSize(AI->getAllocatedType()));
if (BasePtrOffset.ule(AllocSize)) {
return GetElementPtrInst::CreateInBounds(
PtrOp, makeArrayRef(Ops).slice(1), GEP.getName());
}
}
}
}

return nullptr;
}

Expand Down
6 changes: 3 additions & 3 deletions test/Transforms/InstCombine/getelementptr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ define i32 @test21() {
%rval = load i32, i32* %pbobel
ret i32 %rval
; CHECK-LABEL: @test21(
; CHECK: getelementptr %intstruct, %intstruct* %pbob1, i64 0, i32 0
; CHECK: getelementptr inbounds %intstruct, %intstruct* %pbob1, i64 0, i32 0
}


Expand Down Expand Up @@ -541,8 +541,8 @@ define i8* @test32(i8* %v) {
%G = load i8*, i8** %F
ret i8* %G
; CHECK-LABEL: @test32(
; CHECK: %D = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 1
; CHECK: %F = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 2
; CHECK: %D = getelementptr inbounds [4 x i8*], [4 x i8*]* %A, i64 0, i64 1
; CHECK: %F = getelementptr inbounds [4 x i8*], [4 x i8*]* %A, i64 0, i64 2
}

; PR3290
Expand Down

0 comments on commit 0c8a344

Please sign in to comment.