diff options
author | tyker <tyker1@outlook.com> | 2019-10-31 00:11:18 +0100 |
---|---|---|
committer | tyker <tyker1@outlook.com> | 2019-10-31 00:15:19 +0100 |
commit | c3b06d0c393e533eab712922911d14e5a079fa5d (patch) | |
tree | 7e6e292e9ce1a5e879bbcaa87774d606c869d2e9 /llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | |
parent | 5632d3756cd589a8139099317829a746dab650ee (diff) | |
download | bcm5719-llvm-c3b06d0c393e533eab712922911d14e5a079fa5d.tar.gz bcm5719-llvm-c3b06d0c393e533eab712922911d14e5a079fa5d.zip |
[InstCombine] keep assumption before sinking calls
Summary:
in the following C code the branch is not removed by clang in O3.
```
int f1(char* p) {
int i1 = __builtin_strlen(p);
if (!p)
return -1;
return i1;
}
```
The issue is that the call to strlen is sunk to the following block by instcombine. In its new place the call to strlen doesn't dominate the use in the icmp anymore so value tracking can't see that p cannot be null.
This patch resolves the issue by inserting an assumption at the place of the call before sinking a call when that call can be used to prove an argument to be nonnull.
This resolves this issue at O3.
Reviewers: majnemer, xbolva00, fhahn, jdoerfert, spatel, efriedma
Reviewed By: jdoerfert
Subscribers: hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D69477
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstructionCombining.cpp')
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index ecb486c544e..c4f9be0a15d 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -3115,7 +3115,8 @@ Instruction *InstCombiner::visitLandingPadInst(LandingPadInst &LI) { /// beginning of DestBlock, which can only happen if it's safe to move the /// instruction past all of the instructions between it and the end of its /// block. -static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { +static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock, + InstCombiner::BuilderTy &Builder) { assert(I->hasOneUse() && "Invariants didn't hold!"); BasicBlock *SrcBlock = I->getParent(); @@ -3149,6 +3150,24 @@ static bool TryToSinkInstruction(Instruction *I, BasicBlock *DestBlock) { if (Scan->mayWriteToMemory()) return false; } + + // If this instruction was providing nonnull guarantees insert assumptions for + // nonnull call site arguments. + if (auto CS = dyn_cast<CallBase>(I)) { + for (unsigned Idx = 0; Idx != CS->getNumArgOperands(); Idx++) + if (CS->paramHasAttr(Idx, Attribute::NonNull) || + (CS->paramHasAttr(Idx, Attribute::Dereferenceable) && + !llvm::NullPointerIsDefined(CS->getFunction(), + CS->getArgOperand(Idx) + ->getType() + ->getPointerAddressSpace()))) { + Value *V = CS->getArgOperand(Idx); + + Builder.SetInsertPoint(I->getParent(), I->getIterator()); + Builder.CreateAssumption(Builder.CreateIsNotNull(V)); + } + } + BasicBlock::iterator InsertPos = DestBlock->getFirstInsertionPt(); I->moveBefore(&*InsertPos); ++NumSunkInst; @@ -3283,7 +3302,7 @@ bool InstCombiner::run() { // otherwise), we can keep going. if (UserIsSuccessor && UserParent->getUniquePredecessor()) { // Okay, the CFG is simple enough, try to sink this instruction. - if (TryToSinkInstruction(I, UserParent)) { + if (TryToSinkInstruction(I, UserParent, Builder)) { LLVM_DEBUG(dbgs() << "IC: Sink: " << *I << '\n'); MadeIRChange = true; // We'll add uses of the sunk instruction below, but since sinking |