1 //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// This file implements a pass that will conditionally reset a machine 11 /// function as if it was just created. This is used to provide a fallback 12 /// mechanism when GlobalISel fails, thus the condition for the reset to 13 /// happen is that the MachineFunction has the FailedISel property. 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/ADT/ScopeExit.h" 17 #include "llvm/ADT/Statistic.h" 18 #include "llvm/CodeGen/MachineFunction.h" 19 #include "llvm/CodeGen/MachineFunctionPass.h" 20 #include "llvm/CodeGen/MachineRegisterInfo.h" 21 #include "llvm/CodeGen/Passes.h" 22 #include "llvm/IR/DiagnosticInfo.h" 23 #include "llvm/Support/Debug.h" 24 using namespace llvm; 25 26 #define DEBUG_TYPE "reset-machine-function" 27 28 STATISTIC(NumFunctionsReset, "Number of functions reset"); 29 30 namespace { 31 class ResetMachineFunction : public MachineFunctionPass { 32 /// Tells whether or not this pass should emit a fallback 33 /// diagnostic when it resets a function. 34 bool EmitFallbackDiag; 35 /// Whether we should abort immediately instead of resetting the function. 36 bool AbortOnFailedISel; 37 38 public: 39 static char ID; // Pass identification, replacement for typeid 40 ResetMachineFunction(bool EmitFallbackDiag = false, 41 bool AbortOnFailedISel = false) 42 : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag), 43 AbortOnFailedISel(AbortOnFailedISel) {} 44 45 StringRef getPassName() const override { return "ResetMachineFunction"; } 46 47 bool runOnMachineFunction(MachineFunction &MF) override { 48 // No matter what happened, whether we successfully selected the function 49 // or not, nothing is going to use the vreg types after us. Make sure they 50 // disappear. 51 auto ClearVRegTypesOnReturn = 52 make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); }); 53 54 if (MF.getProperties().hasProperty( 55 MachineFunctionProperties::Property::FailedISel)) { 56 if (AbortOnFailedISel) 57 report_fatal_error("Instruction selection failed"); 58 LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n'); 59 ++NumFunctionsReset; 60 MF.reset(); 61 if (EmitFallbackDiag) { 62 const Function &F = MF.getFunction(); 63 DiagnosticInfoISelFallback DiagFallback(F); 64 F.getContext().diagnose(DiagFallback); 65 } 66 return true; 67 } 68 return false; 69 } 70 71 }; 72 } // end anonymous namespace 73 74 char ResetMachineFunction::ID = 0; 75 INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, 76 "Reset machine function if ISel failed", false, false) 77 78 MachineFunctionPass * 79 llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false, 80 bool AbortOnFailedISel = false) { 81 return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel); 82 } 83