* We now preserve the no-critical-edge pass (because we cannot insert critical edges)

* Small modification to be more efficient


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5757 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2003-03-31 17:30:25 +00:00
parent d76370b306
commit 6c0e0496dc

View File

@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/BasicBlock.h" #include "llvm/BasicBlock.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "llvm/iTerminators.h" #include "llvm/iTerminators.h"
@ -18,6 +19,11 @@ using std::vector;
static RegisterOpt<UnifyFunctionExitNodes> static RegisterOpt<UnifyFunctionExitNodes>
X("mergereturn", "Unify function exit nodes"); X("mergereturn", "Unify function exit nodes");
void UnifyFunctionExitNodes::getAnalysisUsage(AnalysisUsage &AU) const{
// We preserve the non-critical-edgeness property
AU.addPreservedID(BreakCriticalEdgesID);
}
// UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new // UnifyAllExitNodes - Unify all exit nodes of the CFG by creating a new
// BasicBlock, and converting all returns to unconditional branches to this // BasicBlock, and converting all returns to unconditional branches to this
// new basic block. The singular exit node is returned. // new basic block. The singular exit node is returned.
@ -47,18 +53,11 @@ bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
// //
BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F); BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F);
PHINode *PN = 0;
if (F.getReturnType() != Type::VoidTy) { if (F.getReturnType() != Type::VoidTy) {
// If the function doesn't return void... add a PHI node to the block... // If the function doesn't return void... add a PHI node to the block...
PHINode *PN = new PHINode(F.getReturnType(), "UnifiedRetVal"); PN = new PHINode(F.getReturnType(), "UnifiedRetVal");
NewRetBlock->getInstList().push_back(PN); NewRetBlock->getInstList().push_back(PN);
// Add an incoming element to the PHI node for every return instruction that
// is merging into this new block...
for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
E = ReturningBlocks.end(); I != E; ++I)
PN->addIncoming((*I)->getTerminator()->getOperand(0), *I);
// Add a return instruction to return the result of the PHI node...
NewRetBlock->getInstList().push_back(new ReturnInst(PN)); NewRetBlock->getInstList().push_back(new ReturnInst(PN));
} else { } else {
// If it returns void, just add a return void instruction to the block // If it returns void, just add a return void instruction to the block
@ -70,9 +69,16 @@ bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
// //
for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(), for (vector<BasicBlock*>::iterator I = ReturningBlocks.begin(),
E = ReturningBlocks.end(); I != E; ++I) { E = ReturningBlocks.end(); I != E; ++I) {
(*I)->getInstList().pop_back(); // Remove the return insn BasicBlock *BB = *I;
(*I)->getInstList().push_back(new BranchInst(NewRetBlock));
// Add an incoming element to the PHI node for every return instruction that
// is merging into this new block...
if (PN) PN->addIncoming(BB->getTerminator()->getOperand(0), BB);
BB->getInstList().pop_back(); // Remove the return insn
BB->getInstList().push_back(new BranchInst(NewRetBlock));
} }
ExitNode = NewRetBlock; ExitNode = NewRetBlock;
return true; return true;
} }