LLVM  10.0.0svn
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"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/Support/Debug.h"
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "reset-machine-function"
27 
28 STATISTIC(NumFunctionsReset, "Number of functions reset");
29 STATISTIC(NumFunctionsVisited, "Number of functions visited");
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  ++NumFunctionsVisited;
55  // No matter what happened, whether we successfully selected the function
56  // or not, nothing is going to use the vreg types after us. Make sure they
57  // disappear.
58  auto ClearVRegTypesOnReturn =
59  make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
60 
61  if (MF.getProperties().hasProperty(
63  if (AbortOnFailedISel)
64  report_fatal_error("Instruction selection failed");
65  LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
66  ++NumFunctionsReset;
67  MF.reset();
68  if (EmitFallbackDiag) {
69  const Function &F = MF.getFunction();
70  DiagnosticInfoISelFallback DiagFallback(F);
71  F.getContext().diagnose(DiagFallback);
72  }
73  return true;
74  }
75  return false;
76  }
77 
78  };
79 } // end anonymous namespace
80 
82 INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
83  "Reset machine function if ISel failed", false, false)
84 
86 llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
87  bool AbortOnFailedISel = false) {
88  return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
89 }
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:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:58
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:205
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:132
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:48
#define LLVM_DEBUG(X)
Definition: Debug.h:122