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

Commit 0c8a344

Browse files
committed
[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 facbf76 commit 0c8a344

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
@@ -1899,6 +1899,25 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
18991899
}
19001900
}
19011901

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

test/Transforms/InstCombine/getelementptr.ll

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

373373

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

548548
; PR3290

0 commit comments

Comments
 (0)