forked from OSchip/llvm-project
Use an early return instead of a long if block.
llvm-svn: 128196
This commit is contained in:
parent
dd84bcce8f
commit
0e331c05ae
|
|
@ -597,67 +597,67 @@ bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
|
||||||
if (!V)
|
if (!V)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (PHINode *PN = dyn_cast<PHINode>(V)) {
|
PHINode *PN = dyn_cast<PHINode>(V);
|
||||||
BasicBlock *BB = RI->getParent();
|
if (!PN)
|
||||||
if (PN->getParent() != BB)
|
return false;
|
||||||
return false;
|
|
||||||
|
|
||||||
// It's not safe to eliminate the sign / zero extension of the return value.
|
BasicBlock *BB = RI->getParent();
|
||||||
// See llvm::isInTailCallPosition().
|
if (PN->getParent() != BB)
|
||||||
const Function *F = BB->getParent();
|
return false;
|
||||||
unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
|
|
||||||
if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Make sure there are no instructions between PHI and return.
|
// It's not safe to eliminate the sign / zero extension of the return value.
|
||||||
BasicBlock::iterator BI = PN;
|
// See llvm::isInTailCallPosition().
|
||||||
do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
|
const Function *F = BB->getParent();
|
||||||
if (&*BI != RI)
|
unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
|
||||||
return false;
|
if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
|
||||||
|
return false;
|
||||||
|
|
||||||
/// Only dup the ReturnInst if the CallInst is likely to be emitted as a
|
// Make sure there are no instructions between PHI and return.
|
||||||
/// tail call.
|
BasicBlock::iterator BI = PN;
|
||||||
SmallVector<CallInst*, 4> TailCalls;
|
do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
|
||||||
for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
|
if (&*BI != RI)
|
||||||
CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
|
return false;
|
||||||
// Make sure the phi value is indeed produced by the tail call.
|
|
||||||
if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
|
|
||||||
TLI->mayBeEmittedAsTailCall(CI))
|
|
||||||
TailCalls.push_back(CI);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Changed = false;
|
/// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
|
||||||
for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) {
|
/// call.
|
||||||
CallInst *CI = TailCalls[i];
|
SmallVector<CallInst*, 4> TailCalls;
|
||||||
CallSite CS(CI);
|
for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
|
||||||
|
CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
|
||||||
// Conservatively require the attributes of the call to match those of
|
// Make sure the phi value is indeed produced by the tail call.
|
||||||
// the return. Ignore noalias because it doesn't affect the call sequence.
|
if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
|
||||||
unsigned CalleeRetAttr = CS.getAttributes().getRetAttributes();
|
TLI->mayBeEmittedAsTailCall(CI))
|
||||||
if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
|
TailCalls.push_back(CI);
|
||||||
continue;
|
|
||||||
|
|
||||||
// Make sure the call instruction is followed by an unconditional branch
|
|
||||||
// to the return block.
|
|
||||||
BasicBlock *CallBB = CI->getParent();
|
|
||||||
BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator());
|
|
||||||
if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Duplicate the return into CallBB.
|
|
||||||
(void)FoldReturnIntoUncondBranch(RI, BB, CallBB);
|
|
||||||
UpdateDT = Changed = true;
|
|
||||||
++NumRetsDup;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we eliminated all predecessors of the block, delete the block now.
|
|
||||||
if (Changed && pred_begin(BB) == pred_end(BB))
|
|
||||||
BB->eraseFromParent();
|
|
||||||
|
|
||||||
return Changed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
bool Changed = false;
|
||||||
|
for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) {
|
||||||
|
CallInst *CI = TailCalls[i];
|
||||||
|
CallSite CS(CI);
|
||||||
|
|
||||||
|
// Conservatively require the attributes of the call to match those of the
|
||||||
|
// return. Ignore noalias because it doesn't affect the call sequence.
|
||||||
|
unsigned CalleeRetAttr = CS.getAttributes().getRetAttributes();
|
||||||
|
if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Make sure the call instruction is followed by an unconditional branch to
|
||||||
|
// the return block.
|
||||||
|
BasicBlock *CallBB = CI->getParent();
|
||||||
|
BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator());
|
||||||
|
if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Duplicate the return into CallBB.
|
||||||
|
(void)FoldReturnIntoUncondBranch(RI, BB, CallBB);
|
||||||
|
UpdateDT = Changed = true;
|
||||||
|
++NumRetsDup;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we eliminated all predecessors of the block, delete the block now.
|
||||||
|
if (Changed && pred_begin(BB) == pred_end(BB))
|
||||||
|
BB->eraseFromParent();
|
||||||
|
|
||||||
|
return Changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue