Fix insert point handling for multiple return values.
authorDevang Patel <dpatel@apple.com>
Tue, 8 Apr 2008 02:24:08 +0000 (02:24 +0000)
committerDevang Patel <dpatel@apple.com>
Tue, 8 Apr 2008 02:24:08 +0000 (02:24 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49367 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/UnifyFunctionExitNodes.cpp

index 76b565c0e22a902ce1ba53625e6b1d4e16e3f962..6cdaba53a050f50d28934dc002644e21211bce40 100644 (file)
@@ -117,13 +117,21 @@ bool UnifyFunctionExitNodes::runOnFunction(Function &F) {
   if (NumRetVals == 0)
     ReturnInst::Create(NULL, NewRetBlock);
   else if (const StructType *STy = dyn_cast<StructType>(F.getReturnType())) {
-    Instruction *InsertPt = NewRetBlock->getFirstNonPHI();
+    Instruction *InsertPt = NULL;
+    if (NumRetVals == 0)
+      InsertPt = NewRetBlock->getFirstNonPHI();
+    PHINode *PN = NULL;
     for (unsigned i = 0; i < NumRetVals; ++i) {
-      PHINode *PN = PHINode::Create(STy->getElementType(i), "UnifiedRetVal." 
-                                    + utostr(i), InsertPt);
+      if (InsertPt)
+        PN = PHINode::Create(STy->getElementType(i), "UnifiedRetVal." 
+                         + utostr(i), InsertPt);
+      else
+        PN = PHINode::Create(STy->getElementType(i), "UnifiedRetVal." 
+                         + utostr(i), NewRetBlock);
       Phis.push_back(PN);
+      InsertPt = PN;
     }
-    ReturnInst::Create(&Phis[0], NumRetVals);
+    ReturnInst::Create(&Phis[0], NumRetVals, NewRetBlock);
   }
   else {
     // If the function doesn't return void... add a PHI node to the block...