1380cd88cSQuentin Colombet //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==// 2374796d6SQuentin Colombet // 3374796d6SQuentin Colombet // The LLVM Compiler Infrastructure 4374796d6SQuentin Colombet // 5374796d6SQuentin Colombet // This file is distributed under the University of Illinois Open Source 6374796d6SQuentin Colombet // License. See LICENSE.TXT for details. 7374796d6SQuentin Colombet // 8374796d6SQuentin Colombet //===----------------------------------------------------------------------===// 9380cd88cSQuentin Colombet /// \file 10380cd88cSQuentin Colombet /// This file implements a pass that will conditionally reset a machine 11380cd88cSQuentin Colombet /// function as if it was just created. This is used to provide a fallback 12380cd88cSQuentin Colombet /// mechanism when GlobalISel fails, thus the condition for the reset to 13380cd88cSQuentin Colombet /// happen is that the MachineFunction has the FailedISel property. 14374796d6SQuentin Colombet //===----------------------------------------------------------------------===// 15374796d6SQuentin Colombet 163054eceaSRoman Tereshin #include "llvm/ADT/ScopeExit.h" 170f4c20a1SQuentin Colombet #include "llvm/ADT/Statistic.h" 18374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunction.h" 19374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunctionPass.h" 203054eceaSRoman Tereshin #include "llvm/CodeGen/MachineRegisterInfo.h" 216bda14b3SChandler Carruth #include "llvm/CodeGen/Passes.h" 22612cd1fdSQuentin Colombet #include "llvm/IR/DiagnosticInfo.h" 23374796d6SQuentin Colombet #include "llvm/Support/Debug.h" 24374796d6SQuentin Colombet using namespace llvm; 25374796d6SQuentin Colombet 26374796d6SQuentin Colombet #define DEBUG_TYPE "reset-machine-function" 27374796d6SQuentin Colombet 280f4c20a1SQuentin Colombet STATISTIC(NumFunctionsReset, "Number of functions reset"); 290f4c20a1SQuentin Colombet 30374796d6SQuentin Colombet namespace { 31374796d6SQuentin Colombet class ResetMachineFunction : public MachineFunctionPass { 32612cd1fdSQuentin Colombet /// Tells whether or not this pass should emit a fallback 33612cd1fdSQuentin Colombet /// diagnostic when it resets a function. 34612cd1fdSQuentin Colombet bool EmitFallbackDiag; 351a314dacSJustin Bogner /// Whether we should abort immediately instead of resetting the function. 361a314dacSJustin Bogner bool AbortOnFailedISel; 37612cd1fdSQuentin Colombet 38374796d6SQuentin Colombet public: 39374796d6SQuentin Colombet static char ID; // Pass identification, replacement for typeid 401a314dacSJustin Bogner ResetMachineFunction(bool EmitFallbackDiag = false, 411a314dacSJustin Bogner bool AbortOnFailedISel = false) 421a314dacSJustin Bogner : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag), 431a314dacSJustin Bogner AbortOnFailedISel(AbortOnFailedISel) {} 44374796d6SQuentin Colombet 45117296c0SMehdi Amini StringRef getPassName() const override { return "ResetMachineFunction"; } 46374796d6SQuentin Colombet 47374796d6SQuentin Colombet bool runOnMachineFunction(MachineFunction &MF) override { 483054eceaSRoman Tereshin // No matter what happened, whether we successfully selected the function 493054eceaSRoman Tereshin // or not, nothing is going to use the vreg types after us. Make sure they 503054eceaSRoman Tereshin // disappear. 513054eceaSRoman Tereshin auto ClearVRegTypesOnReturn = 523054eceaSRoman Tereshin make_scope_exit([&MF]() { MF.getRegInfo().getVRegToType().clear(); }); 533054eceaSRoman Tereshin 54374796d6SQuentin Colombet if (MF.getProperties().hasProperty( 55374796d6SQuentin Colombet MachineFunctionProperties::Property::FailedISel)) { 561a314dacSJustin Bogner if (AbortOnFailedISel) 571a314dacSJustin Bogner report_fatal_error("Instruction selection failed"); 58*d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n'); 590f4c20a1SQuentin Colombet ++NumFunctionsReset; 60374796d6SQuentin Colombet MF.reset(); 61612cd1fdSQuentin Colombet if (EmitFallbackDiag) { 62f1caa283SMatthias Braun const Function &F = MF.getFunction(); 63612cd1fdSQuentin Colombet DiagnosticInfoISelFallback DiagFallback(F); 64612cd1fdSQuentin Colombet F.getContext().diagnose(DiagFallback); 65612cd1fdSQuentin Colombet } 66374796d6SQuentin Colombet return true; 67374796d6SQuentin Colombet } 68374796d6SQuentin Colombet return false; 69374796d6SQuentin Colombet } 70374796d6SQuentin Colombet 71374796d6SQuentin Colombet }; 72374796d6SQuentin Colombet } // end anonymous namespace 73374796d6SQuentin Colombet 74374796d6SQuentin Colombet char ResetMachineFunction::ID = 0; 75374796d6SQuentin Colombet INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, 7698af4664SAmara Emerson "Reset machine function if ISel failed", false, false) 77374796d6SQuentin Colombet 78374796d6SQuentin Colombet MachineFunctionPass * 791a314dacSJustin Bogner llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false, 801a314dacSJustin Bogner bool AbortOnFailedISel = false) { 811a314dacSJustin Bogner return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel); 82374796d6SQuentin Colombet } 83