Fix a use after free error caught by the valgrind builders.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@110601 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/IPO/MergeFunctions.cpp b/lib/Transforms/IPO/MergeFunctions.cpp
index 0b36204..d7075b9 100644
--- a/lib/Transforms/IPO/MergeFunctions.cpp
+++ b/lib/Transforms/IPO/MergeFunctions.cpp
@@ -559,7 +559,7 @@
 }
 
 /// MergeTwoFunctions - Merge two equivalent functions. Upon completion,
-/// FnVec[j] should never be visited again.
+/// FnVec[j] is deleted but not removed from the vector.
 void MergeFunctions::MergeTwoFunctions(std::vector<Function *> &FnVec,
                                        unsigned i, unsigned j) const {
   Function *F = FnVec[i];
@@ -580,10 +580,12 @@
     H->takeName(F);
     F->replaceAllUsesWith(H);
 
+    unsigned MaxAlignment = std::max(G->getAlignment(), H->getAlignment());
+
     WriteThunk(F, G);
     WriteThunk(F, H);
 
-    F->setAlignment(std::max(G->getAlignment(), H->getAlignment()));
+    F->setAlignment(MaxAlignment);
     F->setLinkage(GlobalValue::InternalLinkage);
   } else {
     WriteThunk(F, G);