1 //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file implements a pass that will conditionally reset a machine
10 /// function as if it was just created. This is used to provide a fallback
11 /// mechanism when GlobalISel fails, thus the condition for the reset to
12 /// happen is that the MachineFunction has the FailedISel property.
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/StackProtector.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     void getAnalysisUsage(AnalysisUsage &AU) const override {
48       AU.addPreserved<StackProtector>();
49       MachineFunctionPass::getAnalysisUsage(AU);
50     }
51 
52     bool runOnMachineFunction(MachineFunction &MF) override {
53       // No matter what happened, whether we successfully selected the function
54       // or not, nothing is going to use the vreg types after us. Make sure they
55       // disappear.
56       auto ClearVRegTypesOnReturn =
57           make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
58 
59       if (MF.getProperties().hasProperty(
60               MachineFunctionProperties::Property::FailedISel)) {
61         if (AbortOnFailedISel)
62           report_fatal_error("Instruction selection failed");
63         LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
64         ++NumFunctionsReset;
65         MF.reset();
66         if (EmitFallbackDiag) {
67           const Function &F = MF.getFunction();
68           DiagnosticInfoISelFallback DiagFallback(F);
69           F.getContext().diagnose(DiagFallback);
70         }
71         return true;
72       }
73       return false;
74     }
75 
76   };
77 } // end anonymous namespace
78 
79 char ResetMachineFunction::ID = 0;
80 INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
81                 "Reset machine function if ISel failed", false, false)
82 
83 MachineFunctionPass *
84 llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
85                                      bool AbortOnFailedISel = false) {
86   return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
87 }
88