1380cd88cSQuentin Colombet //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
2374796d6SQuentin Colombet //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler 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"
206bda14b3SChandler Carruth #include "llvm/CodeGen/Passes.h"
21*05da2fe5SReid Kleckner #include "llvm/CodeGen/StackProtector.h"
22612cd1fdSQuentin Colombet #include "llvm/IR/DiagnosticInfo.h"
23*05da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
24374796d6SQuentin Colombet #include "llvm/Support/Debug.h"
25374796d6SQuentin Colombet using namespace llvm;
26374796d6SQuentin Colombet 
27374796d6SQuentin Colombet #define DEBUG_TYPE "reset-machine-function"
28374796d6SQuentin Colombet 
290f4c20a1SQuentin Colombet STATISTIC(NumFunctionsReset, "Number of functions reset");
3066df7361SCraig Topper STATISTIC(NumFunctionsVisited, "Number of functions visited");
310f4c20a1SQuentin Colombet 
32374796d6SQuentin Colombet namespace {
33374796d6SQuentin Colombet   class ResetMachineFunction : public MachineFunctionPass {
34612cd1fdSQuentin Colombet     /// Tells whether or not this pass should emit a fallback
35612cd1fdSQuentin Colombet     /// diagnostic when it resets a function.
36612cd1fdSQuentin Colombet     bool EmitFallbackDiag;
371a314dacSJustin Bogner     /// Whether we should abort immediately instead of resetting the function.
381a314dacSJustin Bogner     bool AbortOnFailedISel;
39612cd1fdSQuentin Colombet 
40374796d6SQuentin Colombet   public:
41374796d6SQuentin Colombet     static char ID; // Pass identification, replacement for typeid
ResetMachineFunction(bool EmitFallbackDiag=false,bool AbortOnFailedISel=false)421a314dacSJustin Bogner     ResetMachineFunction(bool EmitFallbackDiag = false,
431a314dacSJustin Bogner                          bool AbortOnFailedISel = false)
441a314dacSJustin Bogner         : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
451a314dacSJustin Bogner           AbortOnFailedISel(AbortOnFailedISel) {}
46374796d6SQuentin Colombet 
getPassName() const47117296c0SMehdi Amini     StringRef getPassName() const override { return "ResetMachineFunction"; }
48374796d6SQuentin Colombet 
getAnalysisUsage(AnalysisUsage & AU) const4990ad6835SMatthias Braun     void getAnalysisUsage(AnalysisUsage &AU) const override {
5090ad6835SMatthias Braun       AU.addPreserved<StackProtector>();
5190ad6835SMatthias Braun       MachineFunctionPass::getAnalysisUsage(AU);
5290ad6835SMatthias Braun     }
5390ad6835SMatthias Braun 
runOnMachineFunction(MachineFunction & MF)54374796d6SQuentin Colombet     bool runOnMachineFunction(MachineFunction &MF) override {
5566df7361SCraig Topper       ++NumFunctionsVisited;
563054eceaSRoman Tereshin       // No matter what happened, whether we successfully selected the function
573054eceaSRoman Tereshin       // or not, nothing is going to use the vreg types after us. Make sure they
583054eceaSRoman Tereshin       // disappear.
593054eceaSRoman Tereshin       auto ClearVRegTypesOnReturn =
6013229affSRoman Tereshin           make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
613054eceaSRoman Tereshin 
62374796d6SQuentin Colombet       if (MF.getProperties().hasProperty(
63374796d6SQuentin Colombet               MachineFunctionProperties::Property::FailedISel)) {
641a314dacSJustin Bogner         if (AbortOnFailedISel)
651a314dacSJustin Bogner           report_fatal_error("Instruction selection failed");
66d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
670f4c20a1SQuentin Colombet         ++NumFunctionsReset;
68374796d6SQuentin Colombet         MF.reset();
69612cd1fdSQuentin Colombet         if (EmitFallbackDiag) {
70f1caa283SMatthias Braun           const Function &F = MF.getFunction();
71612cd1fdSQuentin Colombet           DiagnosticInfoISelFallback DiagFallback(F);
72612cd1fdSQuentin Colombet           F.getContext().diagnose(DiagFallback);
73612cd1fdSQuentin Colombet         }
74374796d6SQuentin Colombet         return true;
75374796d6SQuentin Colombet       }
76374796d6SQuentin Colombet       return false;
77374796d6SQuentin Colombet     }
78374796d6SQuentin Colombet 
79374796d6SQuentin Colombet   };
80374796d6SQuentin Colombet } // end anonymous namespace
81374796d6SQuentin Colombet 
82374796d6SQuentin Colombet char ResetMachineFunction::ID = 0;
83374796d6SQuentin Colombet INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
8498af4664SAmara Emerson                 "Reset machine function if ISel failed", false, false)
85374796d6SQuentin Colombet 
86374796d6SQuentin Colombet MachineFunctionPass *
createResetMachineFunctionPass(bool EmitFallbackDiag=false,bool AbortOnFailedISel=false)871a314dacSJustin Bogner llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
881a314dacSJustin Bogner                                      bool AbortOnFailedISel = false) {
891a314dacSJustin Bogner   return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
90374796d6SQuentin Colombet }
91