summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp
diff options
context:
space:
mode:
authorCameron Zwarich <zwarich@apple.com>2011-03-24 04:52:10 +0000
committerCameron Zwarich <zwarich@apple.com>2011-03-24 04:52:10 +0000
commit4649f17db117913d4859236be98e0a02fc4c1fff (patch)
tree9f79ff5c72b03c206dbbd45d703deab37b6ee3ca /llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp
parent0e331c05ae4ff622637cb153028a4cfb6354004e (diff)
downloadbcm5719-llvm-4649f17db117913d4859236be98e0a02fc4c1fff.tar.gz
bcm5719-llvm-4649f17db117913d4859236be98e0a02fc4c1fff.zip
Do early taildup of ret in CodeGenPrepare for potential tail calls that have a
void return type. This fixes PR9487. llvm-svn: 128197
Diffstat (limited to 'llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp')
-rw-r--r--llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp54
1 files changed, 37 insertions, 17 deletions
diff --git a/llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp b/llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp
index e951d98c0c8..37b97830716 100644
--- a/llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp
+++ b/llvm/lib/Transforms/Scalar/CodeGenPrepare.cpp
@@ -594,15 +594,12 @@ bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
return false;
Value *V = RI->getReturnValue();
- if (!V)
- return false;
-
- PHINode *PN = dyn_cast<PHINode>(V);
- if (!PN)
+ PHINode *PN = V ? dyn_cast<PHINode>(V) : NULL;
+ if (V && !PN)
return false;
BasicBlock *BB = RI->getParent();
- if (PN->getParent() != BB)
+ if (PN && PN->getParent() != BB)
return false;
// It's not safe to eliminate the sign / zero extension of the return value.
@@ -612,21 +609,44 @@ bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
return false;
- // Make sure there are no instructions between PHI and return.
- BasicBlock::iterator BI = PN;
- do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
- if (&*BI != RI)
- return false;
+ // Make sure there are no instructions between the PHI and return, or that the
+ // return is the first instruction in the block.
+ if (PN) {
+ BasicBlock::iterator BI = BB->begin();
+ do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
+ if (&*BI != RI)
+ return false;
+ } else {
+ if (&*BB->begin() != RI)
+ return false;
+ }
/// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
/// call.
SmallVector<CallInst*, 4> TailCalls;
- for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
- CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
- // 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);
+ if (PN) {
+ for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
+ CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
+ // 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);
+ }
+ } else {
+ SmallPtrSet<BasicBlock*, 4> VisitedBBs;
+ for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
+ if (!VisitedBBs.insert(*PI))
+ continue;
+
+ BasicBlock::InstListType &InstList = (*PI)->getInstList();
+ BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin();
+ BasicBlock::InstListType::reverse_iterator RE = InstList.rend();
+ if (++RI == RE)
+ continue;
+ CallInst *CI = dyn_cast<CallInst>(&*RI);
+ if (CI && CI->getType()->isVoidTy() && TLI->mayBeEmittedAsTailCall(CI))
+ TailCalls.push_back(CI);
+ }
}
bool Changed = false;
OpenPOWER on IntegriCloud