LLVM 24.0.0git
ResetMachineFunctionPass.cpp
Go to the documentation of this file.
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
16#include "llvm/ADT/ScopeExit.h"
17#include "llvm/ADT/Statistic.h"
21#include "llvm/CodeGen/Passes.h"
25#include "llvm/Support/Debug.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("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();
61
62 const TargetMachine &TM = MF.getTarget();
63 // MRI callback for target specific initializations.
65 }
66
67 if (EmitFallbackDiag) {
68 const Function &F = MF.getFunction();
69 DiagnosticInfoISelFallback DiagFallback(F);
70 F.getContext().diagnose(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>();
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
108llvm::createResetMachineFunctionLegacyPass(bool EmitFallbackDiag = false,
109 bool AbortOnFailedISel = false) {
110 return new ResetMachineFunctionLegacy(EmitFallbackDiag, AbortOnFailedISel);
111}
112
116 if (runImpl(MF, EmitFallbackDiag, AbortOnFailedISel))
118 return PreservedAnalyses::all();
119}
static bool runImpl(MachineFunction &MF)
Definition CFIFixup.cpp:304
#define DEBUG_TYPE
#define F(x, y, z)
Definition MD5.cpp:54
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool runImpl(MachineFunction &MF, bool EmitFallbackDiag, bool AbortOnFailedISel)
This file contains the declaration of the ResetMachineFunctionPass class.
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Diagnostic information for ISel fallback path.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineFunctionProperties & resetToInitial()
Reset all properties and re-establish baseline invariants.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void initTargetMachineFunctionInfo(const TargetSubtargetInfo &STI)
Initialize the target specific MachineFunctionInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
void reset()
Reset the instance as if it was just created.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
LLVM_ABI void clearVirtRegTypes()
Remove all types associated to virtual registers (after instruction selection and constraining of all...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &)
Primary interface to the complete machine description for the target machine.
virtual void registerMachineRegisterInfoCallback(MachineFunction &MF) const
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI MachineFunctionPass * createResetMachineFunctionLegacyPass(bool EmitFallbackDiag, bool AbortOnFailedISel)
This pass resets a MachineFunction when it has the FailedISel property as if it was just created.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163