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