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 
13*0f4c20a1SQuentin Colombet #include "llvm/ADT/Statistic.h"
14374796d6SQuentin Colombet #include "llvm/CodeGen/Passes.h"
15374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunction.h"
16374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunctionPass.h"
17612cd1fdSQuentin Colombet #include "llvm/IR/DiagnosticInfo.h"
18374796d6SQuentin Colombet #include "llvm/Support/Debug.h"
19374796d6SQuentin Colombet using namespace llvm;
20374796d6SQuentin Colombet 
21374796d6SQuentin Colombet #define DEBUG_TYPE "reset-machine-function"
22374796d6SQuentin Colombet 
23*0f4c20a1SQuentin Colombet STATISTIC(NumFunctionsReset, "Number of functions reset");
24*0f4c20a1SQuentin Colombet 
25374796d6SQuentin Colombet namespace {
26374796d6SQuentin Colombet   class ResetMachineFunction : public MachineFunctionPass {
27612cd1fdSQuentin Colombet     /// Tells whether or not this pass should emit a fallback
28612cd1fdSQuentin Colombet     /// diagnostic when it resets a function.
29612cd1fdSQuentin Colombet     bool EmitFallbackDiag;
30612cd1fdSQuentin Colombet 
31374796d6SQuentin Colombet   public:
32374796d6SQuentin Colombet     static char ID; // Pass identification, replacement for typeid
33612cd1fdSQuentin Colombet     ResetMachineFunction(bool EmitFallbackDiag = false)
34612cd1fdSQuentin Colombet         : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag) {}
35374796d6SQuentin Colombet 
36374796d6SQuentin Colombet     const char *getPassName() const override {
37374796d6SQuentin Colombet       return "ResetMachineFunction";
38374796d6SQuentin Colombet     }
39374796d6SQuentin Colombet 
40374796d6SQuentin Colombet     bool runOnMachineFunction(MachineFunction &MF) override {
41374796d6SQuentin Colombet       if (MF.getProperties().hasProperty(
42374796d6SQuentin Colombet               MachineFunctionProperties::Property::FailedISel)) {
43374796d6SQuentin Colombet         DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
44*0f4c20a1SQuentin Colombet         ++NumFunctionsReset;
45374796d6SQuentin Colombet         MF.reset();
46612cd1fdSQuentin Colombet         if (EmitFallbackDiag) {
47612cd1fdSQuentin Colombet           const Function &F = *MF.getFunction();
48612cd1fdSQuentin Colombet           DiagnosticInfoISelFallback DiagFallback(F);
49612cd1fdSQuentin Colombet           F.getContext().diagnose(DiagFallback);
50612cd1fdSQuentin Colombet         }
51374796d6SQuentin Colombet         return true;
52374796d6SQuentin Colombet       }
53374796d6SQuentin Colombet       return false;
54374796d6SQuentin Colombet     }
55374796d6SQuentin Colombet 
56374796d6SQuentin Colombet   };
57374796d6SQuentin Colombet } // end anonymous namespace
58374796d6SQuentin Colombet 
59374796d6SQuentin Colombet char ResetMachineFunction::ID = 0;
60374796d6SQuentin Colombet INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
61374796d6SQuentin Colombet                 "reset machine function if ISel failed", false, false)
62374796d6SQuentin Colombet 
63374796d6SQuentin Colombet MachineFunctionPass *
64612cd1fdSQuentin Colombet llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false) {
65612cd1fdSQuentin Colombet   return new ResetMachineFunction(EmitFallbackDiag);
66374796d6SQuentin Colombet }
67