[llvm] r296006 - [GlobalISel] Simplify Select type cleanup using a ScopeExit. NFC.

Ahmed Bougacha via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 23 11:17:25 PST 2017


Author: ab
Date: Thu Feb 23 13:17:24 2017
New Revision: 296006

URL: http://llvm.org/viewvc/llvm-project?rev=296006&view=rev
Log:
[GlobalISel] Simplify Select type cleanup using a ScopeExit. NFC.

This lets us use more natural early-returns when selection fails.

Modified:
    llvm/trunk/lib/CodeGen/GlobalISel/InstructionSelect.cpp

Modified: llvm/trunk/lib/CodeGen/GlobalISel/InstructionSelect.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/GlobalISel/InstructionSelect.cpp?rev=296006&r1=296005&r2=296006&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/GlobalISel/InstructionSelect.cpp (original)
+++ llvm/trunk/lib/CodeGen/GlobalISel/InstructionSelect.cpp Thu Feb 23 13:17:24 2017
@@ -12,6 +12,7 @@
 
 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
 #include "llvm/ADT/PostOrderIterator.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
@@ -55,6 +56,13 @@ static void reportSelectionError(const M
 }
 
 bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
+  const MachineRegisterInfo &MRI = MF.getRegInfo();
+
+  // No matter what happens, whether we successfully select the function or not,
+  // nothing is going to use the vreg types after us.  Make sure they disappear.
+  auto ClearVRegTypesOnReturn =
+      make_scope_exit([&]() { MRI.getVRegToType().clear(); });
+
   // If the ISel pipeline failed, do not bother running that pass.
   if (MF.getProperties().hasProperty(
           MachineFunctionProperties::Property::FailedISel))
@@ -69,8 +77,6 @@ bool InstructionSelect::runOnMachineFunc
   // FIXME: freezeReservedRegs is now done in IRTranslator, but there are many
   // other MF/MFI fields we need to initialize.
 
-  const MachineRegisterInfo &MRI = MF.getRegInfo();
-
 #ifndef NDEBUG
   // Check that our input is fully legal: we require the function to have the
   // Legalized property, so it should be.
@@ -90,7 +96,6 @@ bool InstructionSelect::runOnMachineFunc
   // Until then, keep track of the number of blocks to assert that we don't.
   const size_t NumBlocks = MF.size();
 
-  bool Failed = false;
   for (MachineBasicBlock *MBB : post_order(&MF)) {
     if (MBB->empty())
       continue;
@@ -120,8 +125,8 @@ bool InstructionSelect::runOnMachineFunc
           // FIXME: It would be nice to dump all inserted instructions.  It's
           // not obvious how, esp. considering select() can insert after MI.
           reportSelectionError(MF, &MI, "Cannot select");
-        Failed = true;
-        break;
+        MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
+        return false;
       }
 
       // Dump the range of instructions that MI expanded into.
@@ -150,8 +155,8 @@ bool InstructionSelect::runOnMachineFunc
     if (MI && !RC) {
       if (TPC.isGlobalISelAbortEnabled())
         reportSelectionError(MF, MI, "VReg has no regclass after selection");
-      Failed = true;
-      break;
+      MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
+      return false;
     } else if (!RC)
       continue;
 
@@ -160,18 +165,18 @@ bool InstructionSelect::runOnMachineFunc
       if (TPC.isGlobalISelAbortEnabled())
         reportSelectionError(
             MF, MI, "VReg has explicit size different from class size");
-      Failed = true;
-      break;
+      MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
+      return false;
     }
   }
 
-  MRI.getVRegToType().clear();
-
-  if (!TPC.isGlobalISelAbortEnabled() && (Failed || MF.size() != NumBlocks)) {
+  if (MF.size() != NumBlocks) {
+    if (TPC.isGlobalISelAbortEnabled())
+      reportSelectionError(MF, /*MI=*/nullptr,
+                           "Inserting blocks is not supported yet");
     MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
     return false;
   }
-  assert(MF.size() == NumBlocks && "Inserting blocks is not supported yet");
 
   // FIXME: Should we accurately track changes?
   return true;




More information about the llvm-commits mailing list