[GlobalISel] Finalize translated function on scope exit. NFC.

This is the compromise between having a per-function IRTranslator
and manually managing the per-function state.

llvm-svn: 296046
diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
index 2fb19e0..c94e295 100644
--- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -12,6 +12,7 @@
 
 #include "llvm/CodeGen/GlobalISel/IRTranslator.h"
 
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
@@ -1033,6 +1034,9 @@
 
   assert(PendingPHIs.empty() && "stale PHIs");
 
+  // Release the per-function state when we return, whether we succeeded or not.
+  auto FinalizeOnReturn = make_scope_exit([this]() { finalizeFunction(); });
+
   // Setup a separate basic-block for the arguments and constants, falling
   // through to the IR-level Function's entry block.
   MachineBasicBlock *EntryBB = MF->CreateMachineBasicBlock();
@@ -1050,7 +1054,6 @@
                                &MF->getFunction()->getEntryBlock());
     R << "unable to lower arguments: " << ore::NV("Prototype", F.getType());
     reportTranslationError(*MF, *TPC, *ORE, R);
-    finalizeFunction();
     return false;
   }
 
@@ -1113,7 +1116,5 @@
            "New entry wasn't next in the list of basic block!");
   }
 
-  finalizeFunction();
-
   return false;
 }