1 //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/Support/Debug.h"
24 #define DEBUG_TYPE "reset-machine-function"
26 STATISTIC(NumFunctionsReset, "Number of functions reset");
29 class ResetMachineFunction : public MachineFunctionPass {
30 /// Tells whether or not this pass should emit a fallback
31 /// diagnostic when it resets a function.
32 bool EmitFallbackDiag;
35 static char ID; // Pass identification, replacement for typeid
36 ResetMachineFunction(bool EmitFallbackDiag = false)
37 : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag) {}
39 StringRef getPassName() const override { return "ResetMachineFunction"; }
41 bool runOnMachineFunction(MachineFunction &MF) override {
42 if (MF.getProperties().hasProperty(
43 MachineFunctionProperties::Property::FailedISel)) {
44 DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
47 if (EmitFallbackDiag) {
48 const Function &F = *MF.getFunction();
49 DiagnosticInfoISelFallback DiagFallback(F);
50 F.getContext().diagnose(DiagFallback);
58 } // end anonymous namespace
60 char ResetMachineFunction::ID = 0;
61 INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
62 "reset machine function if ISel failed", false, false)
65 llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false) {
66 return new ResetMachineFunction(EmitFallbackDiag);