summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Majnemer <david.majnemer@gmail.com>2016-08-07 07:58:00 +0000
committerDavid Majnemer <david.majnemer@gmail.com>2016-08-07 07:58:00 +0000
commit4e4f4437c27744de40acb2f79e51a7617352d7b6 (patch)
treec4db1dc1bec6de0ab1eb02bda9434f502380142e
parent49841c38121941982a96c0f635cdc3b71d4f1800 (diff)
downloadbcm5719-llvm-4e4f4437c27744de40acb2f79e51a7617352d7b6.tar.gz
bcm5719-llvm-4e4f4437c27744de40acb2f79e51a7617352d7b6.zip
[InstCombine] Infer inbounds on geps of allocas
llvm-svn: 277950
-rw-r--r--llvm/lib/Transforms/InstCombine/InstructionCombining.cpp19
-rw-r--r--llvm/test/Transforms/InstCombine/getelementptr.ll6
2 files changed, 22 insertions, 3 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 7ffe34a3c26..006deb6b5ff 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -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;
}
diff --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index a3c2beb7eb2..491fc355cba 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -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
}
@@ -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
OpenPOWER on IntegriCloud