diff options
author | David Majnemer <david.majnemer@gmail.com> | 2016-08-07 07:58:00 +0000 |
---|---|---|
committer | David Majnemer <david.majnemer@gmail.com> | 2016-08-07 07:58:00 +0000 |
commit | 4e4f4437c27744de40acb2f79e51a7617352d7b6 (patch) | |
tree | c4db1dc1bec6de0ab1eb02bda9434f502380142e /llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | |
parent | 49841c38121941982a96c0f635cdc3b71d4f1800 (diff) | |
download | llvm-4e4f4437c27744de40acb2f79e51a7617352d7b6.zip llvm-4e4f4437c27744de40acb2f79e51a7617352d7b6.tar.gz llvm-4e4f4437c27744de40acb2f79e51a7617352d7b6.tar.bz2 |
[InstCombine] Infer inbounds on geps of allocas
llvm-svn: 277950
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstructionCombining.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 7ffe34a..006deb6 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; } |