* We now preserve the no-critical-edge pass (because we cannot insert critical edges)
authorChris Lattner <sabre@nondot.org>
Mon, 31 Mar 2003 17:30:25 +0000 (17:30 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 31 Mar 2003 17:30:25 +0000 (17:30 +0000)
* Small modification to be more efficient

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5757 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/UnifyFunctionExitNodes.cpp

index 3c522b53ab03e1300210a2ba8dcd12256eb0140b..b56cee9379e05e5637d690929e2eb814a375c167 100644 (file)
@@ -8,6 +8,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
+#include "llvm/Transforms/Scalar.h"
 #include "llvm/BasicBlock.h"
 #include "llvm/Function.h"
 #include "llvm/iTerminators.h"
@@ -18,6 +19,11 @@ using std::vector;
 static RegisterOpt<UnifyFunctionExitNodes>
 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
 // BasicBlock, and converting all returns to unconditional branches to this
 // new basic block.  The singular exit node is returned.
@@ -47,18 +53,11 @@ bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
   //
   BasicBlock *NewRetBlock = new BasicBlock("UnifiedExitNode", &F);
 
+  PHINode *PN = 0;
   if (F.getReturnType() != Type::VoidTy) {
     // 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);
-
-    // 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));
   } else {
     // 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(), 
                                      E = ReturningBlocks.end(); I != E; ++I) {
-    (*I)->getInstList().pop_back();  // Remove the return insn
-    (*I)->getInstList().push_back(new BranchInst(NewRetBlock));
+    BasicBlock *BB = *I;
+
+    // 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;
+
   return true;
 }