Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit ae32815

Browse files
majnemereddyb
authored andcommitted
[InstCombine] Infer inbounds on geps of allocas
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@277950 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 786aad1 commit ae32815

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,25 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
18981898
}
18991899
}
19001900

1901+
if (!GEP.isInBounds()) {
1902+
unsigned PtrWidth =
1903+
DL.getPointerSizeInBits(PtrOp->getType()->getPointerAddressSpace());
1904+
APInt BasePtrOffset(PtrWidth, 0);
1905+
Value *UnderlyingPtrOp =
1906+
PtrOp->stripAndAccumulateInBoundsConstantOffsets(DL,
1907+
BasePtrOffset);
1908+
if (auto *AI = dyn_cast<AllocaInst>(UnderlyingPtrOp)) {
1909+
if (GEP.accumulateConstantOffset(DL, BasePtrOffset) &&
1910+
BasePtrOffset.isNonNegative()) {
1911+
APInt AllocSize(PtrWidth, DL.getTypeAllocSize(AI->getAllocatedType()));
1912+
if (BasePtrOffset.ule(AllocSize)) {
1913+
return GetElementPtrInst::CreateInBounds(
1914+
PtrOp, makeArrayRef(Ops).slice(1), GEP.getName());
1915+
}
1916+
}
1917+
}
1918+
}
1919+
19011920
return nullptr;
19021921
}
19031922

test/Transforms/InstCombine/getelementptr.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ define i32 @test21() {
366366
%rval = load i32, i32* %pbobel
367367
ret i32 %rval
368368
; CHECK-LABEL: @test21(
369-
; CHECK: getelementptr %intstruct, %intstruct* %pbob1, i64 0, i32 0
369+
; CHECK: getelementptr inbounds %intstruct, %intstruct* %pbob1, i64 0, i32 0
370370
}
371371

372372

@@ -540,8 +540,8 @@ define i8* @test32(i8* %v) {
540540
%G = load i8*, i8** %F
541541
ret i8* %G
542542
; CHECK-LABEL: @test32(
543-
; CHECK: %D = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 1
544-
; CHECK: %F = getelementptr [4 x i8*], [4 x i8*]* %A, i64 0, i64 2
543+
; CHECK: %D = getelementptr inbounds [4 x i8*], [4 x i8*]* %A, i64 0, i64 1
544+
; CHECK: %F = getelementptr inbounds [4 x i8*], [4 x i8*]* %A, i64 0, i64 2
545545
}
546546

547547
; PR3290

0 commit comments

Comments
 (0)