summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
authorSanjoy Das <sanjoy@playingwithpointers.com>2016-04-21 19:26:45 +0000
committerSanjoy Das <sanjoy@playingwithpointers.com>2016-04-21 19:26:45 +0000
commita085cfc1503bcd37b207d1d4d6266e9ad64685a0 (patch)
treedf56f3a5987aa4d117b85c76e9db29daab34caf7 /llvm/lib/Transforms
parent5de5910d7db5faa2baafe98450b248d1cdb279c6 (diff)
downloadbcm5719-llvm-a085cfc1503bcd37b207d1d4d6266e9ad64685a0.tar.gz
bcm5719-llvm-a085cfc1503bcd37b207d1d4d6266e9ad64685a0.zip
Folding compares with unescaped allocations
Summary: If we know that the pointer allocated within a function does not escape, we can fold away comparisons that are done with global pointers Patch by Anna Thomas! Reviewers: reames, majnemer, sanjoy Subscribers: mgrang, mcrosier, majnemer, llvm-commits Differential Revision: http://reviews.llvm.org/D19276 llvm-svn: 267035
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstructionCombining.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 8a9d2821b44..586699d5889 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1863,6 +1863,14 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
return nullptr;
}
+static bool isNeverEqualToUnescapedAlloc(Value *V) {
+ if (isa<ConstantPointerNull>(V))
+ return true;
+ if (auto *LI = dyn_cast<LoadInst>(V))
+ return isa<GlobalVariable>(LI->getPointerOperand());
+ return false;
+}
+
static bool
isAllocSiteRemovable(Instruction *AI, SmallVectorImpl<WeakVH> &Users,
const TargetLibraryInfo *TLI) {
@@ -1887,7 +1895,12 @@ isAllocSiteRemovable(Instruction *AI, SmallVectorImpl<WeakVH> &Users,
case Instruction::ICmp: {
ICmpInst *ICI = cast<ICmpInst>(I);
// We can fold eq/ne comparisons with null to false/true, respectively.
- if (!ICI->isEquality() || !isa<ConstantPointerNull>(ICI->getOperand(1)))
+ // We fold comparisons in some conditions provided the alloc has not
+ // escaped.
+ if (!ICI->isEquality())
+ return false;
+ unsigned OtherIndex = (ICI->getOperand(0) == PI) ? 1 : 0;
+ if (!isNeverEqualToUnescapedAlloc(ICI->getOperand(OtherIndex)))
return false;
Users.emplace_back(I);
continue;
OpenPOWER on IntegriCloud