1374796d6SQuentin Colombet //===-- ResetMachineFunctionPass.cpp - Machine Loop Invariant Code Motion Pass ---------===//
2374796d6SQuentin Colombet //
3374796d6SQuentin Colombet //                     The LLVM Compiler Infrastructure
4374796d6SQuentin Colombet //
5374796d6SQuentin Colombet // This file is distributed under the University of Illinois Open Source
6374796d6SQuentin Colombet // License. See LICENSE.TXT for details.
7374796d6SQuentin Colombet //
8374796d6SQuentin Colombet //===----------------------------------------------------------------------===//
9374796d6SQuentin Colombet //
10374796d6SQuentin Colombet //
11374796d6SQuentin Colombet //===----------------------------------------------------------------------===//
12374796d6SQuentin Colombet 
13374796d6SQuentin Colombet #include "llvm/CodeGen/Passes.h"
14374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunction.h"
15374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunctionPass.h"
16*612cd1fdSQuentin Colombet #include "llvm/IR/DiagnosticInfo.h"
17374796d6SQuentin Colombet #include "llvm/Support/Debug.h"
18374796d6SQuentin Colombet using namespace llvm;
19374796d6SQuentin Colombet 
20374796d6SQuentin Colombet #define DEBUG_TYPE "reset-machine-function"
21374796d6SQuentin Colombet 
22374796d6SQuentin Colombet namespace {
23374796d6SQuentin Colombet   class ResetMachineFunction : public MachineFunctionPass {
24*612cd1fdSQuentin Colombet     /// Tells whether or not this pass should emit a fallback
25*612cd1fdSQuentin Colombet     /// diagnostic when it resets a function.
26*612cd1fdSQuentin Colombet     bool EmitFallbackDiag;
27*612cd1fdSQuentin Colombet 
28374796d6SQuentin Colombet   public:
29374796d6SQuentin Colombet     static char ID; // Pass identification, replacement for typeid
30*612cd1fdSQuentin Colombet     ResetMachineFunction(bool EmitFallbackDiag = false)
31*612cd1fdSQuentin Colombet         : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag) {}
32374796d6SQuentin Colombet 
33374796d6SQuentin Colombet     const char *getPassName() const override {
34374796d6SQuentin Colombet       return "ResetMachineFunction";
35374796d6SQuentin Colombet     }
36374796d6SQuentin Colombet 
37374796d6SQuentin Colombet     bool runOnMachineFunction(MachineFunction &MF) override {
38374796d6SQuentin Colombet       if (MF.getProperties().hasProperty(
39374796d6SQuentin Colombet               MachineFunctionProperties::Property::FailedISel)) {
40374796d6SQuentin Colombet         DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
41374796d6SQuentin Colombet         MF.reset();
42*612cd1fdSQuentin Colombet         if (EmitFallbackDiag) {
43*612cd1fdSQuentin Colombet           const Function &F = *MF.getFunction();
44*612cd1fdSQuentin Colombet           DiagnosticInfoISelFallback DiagFallback(F);
45*612cd1fdSQuentin Colombet           F.getContext().diagnose(DiagFallback);
46*612cd1fdSQuentin Colombet         }
47374796d6SQuentin Colombet         return true;
48374796d6SQuentin Colombet       }
49374796d6SQuentin Colombet       return false;
50374796d6SQuentin Colombet     }
51374796d6SQuentin Colombet 
52374796d6SQuentin Colombet   };
53374796d6SQuentin Colombet } // end anonymous namespace
54374796d6SQuentin Colombet 
55374796d6SQuentin Colombet char ResetMachineFunction::ID = 0;
56374796d6SQuentin Colombet INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
57374796d6SQuentin Colombet                 "reset machine function if ISel failed", false, false)
58374796d6SQuentin Colombet 
59374796d6SQuentin Colombet MachineFunctionPass *
60*612cd1fdSQuentin Colombet llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false) {
61*612cd1fdSQuentin Colombet   return new ResetMachineFunction(EmitFallbackDiag);
62374796d6SQuentin Colombet }
63