1380cd88cSQuentin Colombet //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==// 2374796d6SQuentin Colombet // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6374796d6SQuentin Colombet // 7374796d6SQuentin Colombet //===----------------------------------------------------------------------===// 8380cd88cSQuentin Colombet /// \file 9380cd88cSQuentin Colombet /// This file implements a pass that will conditionally reset a machine 10380cd88cSQuentin Colombet /// function as if it was just created. This is used to provide a fallback 11380cd88cSQuentin Colombet /// mechanism when GlobalISel fails, thus the condition for the reset to 12380cd88cSQuentin Colombet /// happen is that the MachineFunction has the FailedISel property. 13374796d6SQuentin Colombet //===----------------------------------------------------------------------===// 14374796d6SQuentin Colombet 153054eceaSRoman Tereshin #include "llvm/ADT/ScopeExit.h" 160f4c20a1SQuentin Colombet #include "llvm/ADT/Statistic.h" 17374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunction.h" 18374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunctionPass.h" 193054eceaSRoman Tereshin #include "llvm/CodeGen/MachineRegisterInfo.h" 2090ad6835SMatthias Braun #include "llvm/CodeGen/StackProtector.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 4790ad6835SMatthias Braun void getAnalysisUsage(AnalysisUsage &AU) const override { 4890ad6835SMatthias Braun AU.addPreserved<StackProtector>(); 4990ad6835SMatthias Braun MachineFunctionPass::getAnalysisUsage(AU); 5090ad6835SMatthias Braun } 5190ad6835SMatthias Braun 52374796d6SQuentin Colombet bool runOnMachineFunction(MachineFunction &MF) override { 533054eceaSRoman Tereshin // No matter what happened, whether we successfully selected the function 543054eceaSRoman Tereshin // or not, nothing is going to use the vreg types after us. Make sure they 553054eceaSRoman Tereshin // disappear. 563054eceaSRoman Tereshin auto ClearVRegTypesOnReturn = 5713229affSRoman Tereshin make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); }); 583054eceaSRoman Tereshin 59374796d6SQuentin Colombet if (MF.getProperties().hasProperty( 60374796d6SQuentin Colombet MachineFunctionProperties::Property::FailedISel)) { 611a314dacSJustin Bogner if (AbortOnFailedISel) 621a314dacSJustin Bogner report_fatal_error("Instruction selection failed"); 63d34e60caSNicola Zaghen LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n'); 640f4c20a1SQuentin Colombet ++NumFunctionsReset; 65374796d6SQuentin Colombet MF.reset(); 66612cd1fdSQuentin Colombet if (EmitFallbackDiag) { 67f1caa283SMatthias Braun const Function &F = MF.getFunction(); 68612cd1fdSQuentin Colombet DiagnosticInfoISelFallback DiagFallback(F); 69612cd1fdSQuentin Colombet F.getContext().diagnose(DiagFallback); 70612cd1fdSQuentin Colombet } 71374796d6SQuentin Colombet return true; 72374796d6SQuentin Colombet } 73374796d6SQuentin Colombet return false; 74374796d6SQuentin Colombet } 75374796d6SQuentin Colombet 76374796d6SQuentin Colombet }; 77374796d6SQuentin Colombet } // end anonymous namespace 78374796d6SQuentin Colombet 79374796d6SQuentin Colombet char ResetMachineFunction::ID = 0; 80374796d6SQuentin Colombet INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, 8198af4664SAmara Emerson "Reset machine function if ISel failed", false, false) 82374796d6SQuentin Colombet 83374796d6SQuentin Colombet MachineFunctionPass * 841a314dacSJustin Bogner llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false, 851a314dacSJustin Bogner bool AbortOnFailedISel = false) { 861a314dacSJustin Bogner return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel); 87374796d6SQuentin Colombet } 88