LLVM 20.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
15#include "llvm/ADT/ScopeExit.h"
16#include "llvm/ADT/Statistic.h"
20#include "llvm/CodeGen/Passes.h"
24#include "llvm/Support/Debug.h"
26using namespace llvm;
27
28#define DEBUG_TYPE "reset-machine-function"
29
30STATISTIC(NumFunctionsReset, "Number of functions reset");
31STATISTIC(NumFunctionsVisited, "Number of functions visited");
32
33namespace {
34 class ResetMachineFunction : public MachineFunctionPass {
35 /// Tells whether or not this pass should emit a fallback
36 /// diagnostic when it resets a function.
37 bool EmitFallbackDiag;
38 /// Whether we should abort immediately instead of resetting the function.
39 bool AbortOnFailedISel;
40
41 public:
42 static char ID; // Pass identification, replacement for typeid
43 ResetMachineFunction(bool EmitFallbackDiag = false,
44 bool AbortOnFailedISel = false)
45 : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
46 AbortOnFailedISel(AbortOnFailedISel) {}
47
48 StringRef getPassName() const override { return "ResetMachineFunction"; }
49
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
53 }
54
55 bool runOnMachineFunction(MachineFunction &MF) override {
56 ++NumFunctionsVisited;
57 // No matter what happened, whether we successfully selected the function
58 // or not, nothing is going to use the vreg types after us. Make sure they
59 // disappear.
60 auto ClearVRegTypesOnReturn =
61 make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
62
64 MachineFunctionProperties::Property::FailedISel)) {
65 if (AbortOnFailedISel)
66 report_fatal_error("Instruction selection failed");
67 LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
68 ++NumFunctionsReset;
69 MF.reset();
71
72 const TargetMachine &TM = MF.getTarget();
73 // MRI callback for target specific initializations.
74 TM.registerMachineRegisterInfoCallback(MF);
75
76 if (EmitFallbackDiag) {
77 const Function &F = MF.getFunction();
78 DiagnosticInfoISelFallback DiagFallback(F);
79 F.getContext().diagnose(DiagFallback);
80 }
81 return true;
82 }
83 return false;
84 }
85
86 };
87} // end anonymous namespace
88
89char ResetMachineFunction::ID = 0;
90INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
91 "Reset machine function if ISel failed", false, false)
92
94llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
95 bool AbortOnFailedISel = false) {
96 return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
97}
#define LLVM_DEBUG(...)
Definition: Debug.h:106
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
#define DEBUG_TYPE
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
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:166
Represent the analysis usage information of a pass.
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
bool hasProperty(Property P) const
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
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:77
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
MachineFunctionPass * createResetMachineFunctionPass(bool EmitFallbackDiag, bool AbortOnFailedISel)
This pass resets a MachineFunction when it has the FailedISel property as if it was just created.