summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
authorSanjoy Das <sanjoy@playingwithpointers.com>2016-04-22 20:52:25 +0000
committerSanjoy Das <sanjoy@playingwithpointers.com>2016-04-22 20:52:25 +0000
commitf97229d6ba32dc9a74a53eb75dab806d2ca44bff (patch)
tree63924edaddff3cc0ff19aa1cdf79baeb21ade728 /llvm/lib/Transforms
parent0b3868ec6b908dd472bc07d9d003c9e6075bbfda (diff)
downloadbcm5719-llvm-f97229d6ba32dc9a74a53eb75dab806d2ca44bff.tar.gz
bcm5719-llvm-f97229d6ba32dc9a74a53eb75dab806d2ca44bff.zip
Fold compares for distinct allocations
Summary: We can fold compares to false when two distinct allocations within a function are compared for equality. Patch by Anna Thomas! Reviewers: majnemer, reames, sanjoy Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D19390 llvm-svn: 267214
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstructionCombining.cpp16
1 files changed, 11 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index d8ff7f7810c..3ef9a9ecd65 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1862,12 +1862,18 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
return nullptr;
}
-static bool isNeverEqualToUnescapedAlloc(Value *V) {
+static bool isNeverEqualToUnescapedAlloc(Value *V, const TargetLibraryInfo *TLI,
+ Instruction *AI) {
if (isa<ConstantPointerNull>(V))
return true;
if (auto *LI = dyn_cast<LoadInst>(V))
return isa<GlobalVariable>(LI->getPointerOperand());
- return false;
+ // Two distinct allocations will never be equal.
+ // We rely on LookThroughBitCast in isAllocLikeFn being false, since looking
+ // through bitcasts of V can cause
+ // the result statement below to be true, even when AI and V (ex:
+ // i8* ->i32* ->i8* of AI) are the same allocations.
+ return isAllocLikeFn(V, TLI) && V != AI;
}
static bool
@@ -1894,12 +1900,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.
- // We fold comparisons in some conditions provided the alloc has not
- // escaped.
+ // We also 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)))
+ if (!isNeverEqualToUnescapedAlloc(ICI->getOperand(OtherIndex), TLI, AI))
return false;
Users.emplace_back(I);
continue;
OpenPOWER on IntegriCloud