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/CodeGen/ResetMachineFunctionPass.h"
16#include "llvm/ADT/ScopeExit.h"
17#include "llvm/ADT/Statistic.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineRegisterInfo.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/StackProtector.h"
23#include "llvm/IR/DiagnosticInfo.h"
24#include "llvm/InitializePasses.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Target/TargetMachine.h"
27using namespace llvm;
28
29#define DEBUG_TYPE "reset-machine-function"
30
31STATISTIC(NumFunctionsReset, "Number of functions reset");
32STATISTIC(NumFunctionsVisited, "Number of functions visited");
33
34static bool runImpl(MachineFunction &MF, bool EmitFallbackDiag,
35 bool AbortOnFailedISel) {
36 ++NumFunctionsVisited;
37 // No matter what happened, whether we successfully selected the function
38 // or not, nothing is going to use the vreg types after us. Make sure they
39 // disappear.
40 llvm::scope_exit ClearVRegTypesOnReturn(
41 [&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
42
43 if (!MF.getProperties().hasFailedISel())
44 return false;
45
46 if (AbortOnFailedISel)
47 report_fatal_error(reason: "Instruction selection failed");
48
49 LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
50 ++NumFunctionsReset;
51
52 if (MF.empty()) {
53 // Nothing was materialized in the MachineFunction, so avoid the cost of
54 // tearing down and rebuilding all of the per-function state. Just clear
55 // the FailedISel bit so the SelectionDAG pipeline can proceed.
56 auto &Props = MF.getProperties();
57 Props.resetToInitial();
58 } else {
59 MF.reset();
60 MF.initTargetMachineFunctionInfo(STI: MF.getSubtarget());
61
62 const TargetMachine &TM = MF.getTarget();
63 // MRI callback for target specific initializations.
64 TM.registerMachineRegisterInfoCallback(MF);
65 }
66
67 if (EmitFallbackDiag) {
68 const Function &F = MF.getFunction();
69 DiagnosticInfoISelFallback DiagFallback(F);
70 F.getContext().diagnose(DI: DiagFallback);
71 }
72 return true;
73}
74
75namespace {
76class ResetMachineFunctionLegacy : public MachineFunctionPass {
77 /// Tells whether or not this pass should emit a fallback
78 /// diagnostic when it resets a function.
79 bool EmitFallbackDiag;
80 /// Whether we should abort immediately instead of resetting the function.
81 bool AbortOnFailedISel;
82
83public:
84 static char ID; // Pass identification, replacement for typeid
85 ResetMachineFunctionLegacy(bool EmitFallbackDiag = false,
86 bool AbortOnFailedISel = false)
87 : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
88 AbortOnFailedISel(AbortOnFailedISel) {}
89
90 StringRef getPassName() const override { return "ResetMachineFunction"; }
91
92 void getAnalysisUsage(AnalysisUsage &AU) const override {
93 AU.addPreserved<StackProtector>();
94 MachineFunctionPass::getAnalysisUsage(AU);
95 }
96
97 bool runOnMachineFunction(MachineFunction &MF) override {
98 return runImpl(MF, EmitFallbackDiag, AbortOnFailedISel);
99 }
100};
101} // end anonymous namespace
102
103char ResetMachineFunctionLegacy::ID = 0;
104INITIALIZE_PASS(ResetMachineFunctionLegacy, DEBUG_TYPE,
105 "Reset machine function if ISel failed", false, false)
106
107MachineFunctionPass *
108llvm::createResetMachineFunctionLegacyPass(bool EmitFallbackDiag = false,
109 bool AbortOnFailedISel = false) {
110 return new ResetMachineFunctionLegacy(EmitFallbackDiag, AbortOnFailedISel);
111}
112
113PreservedAnalyses
114ResetMachineFunctionPass::run(MachineFunction &MF,
115 MachineFunctionAnalysisManager &) {
116 if (runImpl(MF, EmitFallbackDiag, AbortOnFailedISel))
117 return getMachineFunctionPassPreservedAnalyses();
118 return PreservedAnalyses::all();
119}
120