LLVM  14.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"
22 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/InitializePasses.h"
24 #include "llvm/Support/Debug.h"
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "reset-machine-function"
28 
29 STATISTIC(NumFunctionsReset, "Number of functions reset");
30 STATISTIC(NumFunctionsVisited, "Number of functions visited");
31 
32 namespace {
33  class ResetMachineFunction : public MachineFunctionPass {
34  /// Tells whether or not this pass should emit a fallback
35  /// diagnostic when it resets a function.
36  bool EmitFallbackDiag;
37  /// Whether we should abort immediately instead of resetting the function.
38  bool AbortOnFailedISel;
39 
40  public:
41  static char ID; // Pass identification, replacement for typeid
42  ResetMachineFunction(bool EmitFallbackDiag = false,
43  bool AbortOnFailedISel = false)
44  : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
45  AbortOnFailedISel(AbortOnFailedISel) {}
46 
47  StringRef getPassName() const override { return "ResetMachineFunction"; }
48 
49  void getAnalysisUsage(AnalysisUsage &AU) const override {
52  }
53 
54  bool runOnMachineFunction(MachineFunction &MF) override {
55  ++NumFunctionsVisited;
56  // No matter what happened, whether we successfully selected the function
57  // or not, nothing is going to use the vreg types after us. Make sure they
58  // disappear.
59  auto ClearVRegTypesOnReturn =
60  make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
61 
62  if (MF.getProperties().hasProperty(
64  if (AbortOnFailedISel)
65  report_fatal_error("Instruction selection failed");
66  LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
67  ++NumFunctionsReset;
68  MF.reset();
69  if (EmitFallbackDiag) {
70  const Function &F = MF.getFunction();
71  DiagnosticInfoISelFallback DiagFallback(F);
72  F.getContext().diagnose(DiagFallback);
73  }
74  return true;
75  }
76  return false;
77  }
78 
79  };
80 } // end anonymous namespace
81 
83 INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
84  "Reset machine function if ISel failed", false, false)
85 
87 llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
88  bool AbortOnFailedISel = false) {
89  return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
90 }
DEBUG_TYPE
#define DEBUG_TYPE
Definition: ResetMachineFunctionPass.cpp:27
StackProtector.h
llvm::MachineFunctionProperties::hasProperty
bool hasProperty(Property P) const
Definition: MachineFunction.h:169
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::DiagnosticInfoISelFallback
Diagnostic information for ISel fallback path.
Definition: DiagnosticInfo.h:950
INITIALIZE_PASS
INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, "Reset machine function if ISel failed", false, false) MachineFunctionPass *llvm
Definition: ResetMachineFunctionPass.cpp:83
llvm::Function
Definition: Function.h:62
Statistic.h
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:102
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
F
#define F(x, y, z)
Definition: MD5.cpp:56
MachineRegisterInfo.h
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:640
llvm::StackProtector
Definition: StackProtector.h:37
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::MachineFunction::getProperties
const MachineFunctionProperties & getProperties() const
Get the function properties.
Definition: MachineFunction.h:721
llvm::createResetMachineFunctionPass
MachineFunctionPass * createResetMachineFunctionPass(bool EmitFallbackDiag, bool AbortOnFailedISel)
This pass resets a MachineFunction when it has the FailedISel property as if it was just created.
false
Definition: StackSlotColoring.cpp:142
llvm::make_scope_exit
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:58
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
Passes.h
MachineFunctionPass.h
llvm::MachineFunction::getName
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Definition: MachineFunction.cpp:542
llvm::MachineFunction
Definition: MachineFunction.h:234
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition: PassAnalysisSupport.h:98
llvm::MachineFunctionProperties::Property::FailedISel
@ FailedISel
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:596
DiagnosticInfo.h
llvm::MachineFunction::reset
void reset()
Reset the instance as if it was just created.
Definition: MachineFunction.h:555
ScopeExit.h
MachineFunction.h
InitializePasses.h
Debug.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37