LLVM  8.0.0svn
ResetMachineFunctionPass.cpp
Go to the documentation of this file.
1 //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// This file implements a pass that will conditionally reset a machine
11 /// function as if it was just created. This is used to provide a fallback
12 /// mechanism when GlobalISel fails, thus the condition for the reset to
13 /// happen is that the MachineFunction has the FailedISel property.
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/ADT/ScopeExit.h"
17 #include "llvm/ADT/Statistic.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/IR/DiagnosticInfo.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 
31 namespace {
32  class ResetMachineFunction : public MachineFunctionPass {
33  /// Tells whether or not this pass should emit a fallback
34  /// diagnostic when it resets a function.
35  bool EmitFallbackDiag;
36  /// Whether we should abort immediately instead of resetting the function.
37  bool AbortOnFailedISel;
38 
39  public:
40  static char ID; // Pass identification, replacement for typeid
41  ResetMachineFunction(bool EmitFallbackDiag = false,
42  bool AbortOnFailedISel = false)
43  : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
44  AbortOnFailedISel(AbortOnFailedISel) {}
45 
46  StringRef getPassName() const override { return "ResetMachineFunction"; }
47 
48  void getAnalysisUsage(AnalysisUsage &AU) const override {
51  }
52 
53  bool runOnMachineFunction(MachineFunction &MF) override {
54  // No matter what happened, whether we successfully selected the function
55  // or not, nothing is going to use the vreg types after us. Make sure they
56  // disappear.
57  auto ClearVRegTypesOnReturn =
58  make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
59 
60  if (MF.getProperties().hasProperty(
62  if (AbortOnFailedISel)
63  report_fatal_error("Instruction selection failed");
64  LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
65  ++NumFunctionsReset;
66  MF.reset();
67  if (EmitFallbackDiag) {
68  const Function &F = MF.getFunction();
69  DiagnosticInfoISelFallback DiagFallback(F);
70  F.getContext().diagnose(DiagFallback);
71  }
72  return true;
73  }
74  return false;
75  }
76 
77  };
78 } // end anonymous namespace
79 
81 INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
82  "Reset machine function if ISel failed", false, false)
83 
85 llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
86  bool AbortOnFailedISel = false) {
87  return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
88 }
Diagnostic information for ISel fallback path.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void clearVirtRegTypes()
Remove all types associated to virtual registers (after instruction selection and constraining of all...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:119
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, "Reset machine function if ISel failed", false, false) MachineFunctionPass *llvm
const MachineFunctionProperties & getProperties() const
Get the function properties.
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
STATISTIC(NumFunctions, "Total number of functions")
#define DEBUG_TYPE
F(f)
MachineFunctionPass * createResetMachineFunctionPass(bool EmitFallbackDiag, bool AbortOnFailedISel)
This pass resets a MachineFunction when it has the FailedISel property as if it was just created...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:194
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
void reset()
Reset the instance as if it was just created.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
bool hasProperty(Property P) const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
#define LLVM_DEBUG(X)
Definition: Debug.h:123