LLVM  7.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"
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 
30 namespace {
31  class ResetMachineFunction : public MachineFunctionPass {
32  /// Tells whether or not this pass should emit a fallback
33  /// diagnostic when it resets a function.
34  bool EmitFallbackDiag;
35  /// Whether we should abort immediately instead of resetting the function.
36  bool AbortOnFailedISel;
37 
38  public:
39  static char ID; // Pass identification, replacement for typeid
40  ResetMachineFunction(bool EmitFallbackDiag = false,
41  bool AbortOnFailedISel = false)
42  : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
43  AbortOnFailedISel(AbortOnFailedISel) {}
44 
45  StringRef getPassName() const override { return "ResetMachineFunction"; }
46 
47  bool runOnMachineFunction(MachineFunction &MF) override {
48  // No matter what happened, whether we successfully selected the function
49  // or not, nothing is going to use the vreg types after us. Make sure they
50  // disappear.
51  auto ClearVRegTypesOnReturn =
52  make_scope_exit([&MF]() { MF.getRegInfo().getVRegToType().clear(); });
53 
54  if (MF.getProperties().hasProperty(
56  if (AbortOnFailedISel)
57  report_fatal_error("Instruction selection failed");
58  DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
59  ++NumFunctionsReset;
60  MF.reset();
61  if (EmitFallbackDiag) {
62  const Function &F = MF.getFunction();
63  DiagnosticInfoISelFallback DiagFallback(F);
64  F.getContext().diagnose(DiagFallback);
65  }
66  return true;
67  }
68  return false;
69  }
70 
71  };
72 } // end anonymous namespace
73 
75 INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
76  "Reset machine function if ISel failed", false, false)
77 
79 llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
80  bool AbortOnFailedISel = false) {
81  return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
82 }
Diagnostic information for ISel fallback path.
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:115
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.
VRegToTypeMap & getVRegToType() const
Accessor for VRegToType.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:193
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
#define DEBUG(X)
Definition: Debug.h:118
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49