CodeGenPrepare: Move ret duplication out of the instruction iteration loop.

It can delete the block, and the loop continues on free'd memory.
No change in output. Found by valgrind.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168525 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer 2012-11-23 19:17:06 +00:00
parent 5d1e089eaa
commit 4ccb49a838

View File

@ -125,7 +125,7 @@ namespace {
bool MoveExtToFormExtLoad(Instruction *I); bool MoveExtToFormExtLoad(Instruction *I);
bool OptimizeExtUses(Instruction *I); bool OptimizeExtUses(Instruction *I);
bool OptimizeSelectInst(SelectInst *SI); bool OptimizeSelectInst(SelectInst *SI);
bool DupRetToEnableTailCallOpts(ReturnInst *RI); bool DupRetToEnableTailCallOpts(BasicBlock *BB);
bool PlaceDbgValues(Function &F); bool PlaceDbgValues(Function &F);
}; };
} }
@ -689,10 +689,14 @@ bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) {
/// %tmp2 = tail call i32 @f2() /// %tmp2 = tail call i32 @f2()
/// ret i32 %tmp2 /// ret i32 %tmp2
/// @endcode /// @endcode
bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) { bool CodeGenPrepare::DupRetToEnableTailCallOpts(BasicBlock *BB) {
if (!TLI) if (!TLI)
return false; return false;
ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator());
if (!RI)
return false;
PHINode *PN = 0; PHINode *PN = 0;
BitCastInst *BCI = 0; BitCastInst *BCI = 0;
Value *V = RI->getReturnValue(); Value *V = RI->getReturnValue();
@ -706,7 +710,6 @@ bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
return false; return false;
} }
BasicBlock *BB = RI->getParent();
if (PN && PN->getParent() != BB) if (PN && PN->getParent() != BB)
return false; return false;
@ -1319,9 +1322,6 @@ bool CodeGenPrepare::OptimizeInst(Instruction *I) {
if (CallInst *CI = dyn_cast<CallInst>(I)) if (CallInst *CI = dyn_cast<CallInst>(I))
return OptimizeCallInst(CI); return OptimizeCallInst(CI);
if (ReturnInst *RI = dyn_cast<ReturnInst>(I))
return DupRetToEnableTailCallOpts(RI);
if (SelectInst *SI = dyn_cast<SelectInst>(I)) if (SelectInst *SI = dyn_cast<SelectInst>(I))
return OptimizeSelectInst(SI); return OptimizeSelectInst(SI);
@ -1339,6 +1339,8 @@ bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) {
while (CurInstIterator != BB.end()) while (CurInstIterator != BB.end())
MadeChange |= OptimizeInst(CurInstIterator++); MadeChange |= OptimizeInst(CurInstIterator++);
MadeChange |= DupRetToEnableTailCallOpts(&BB);
return MadeChange; return MadeChange;
} }