1 //===-- ResetMachineFunctionPass.cpp - Machine Loop Invariant Code Motion Pass ---------===//
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 //
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/Statistic.h"
14 #include "llvm/CodeGen/Passes.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/IR/DiagnosticInfo.h"
18 #include "llvm/Support/Debug.h"
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "reset-machine-function"
22 
23 STATISTIC(NumFunctionsReset, "Number of functions reset");
24 
25 namespace {
26   class ResetMachineFunction : public MachineFunctionPass {
27     /// Tells whether or not this pass should emit a fallback
28     /// diagnostic when it resets a function.
29     bool EmitFallbackDiag;
30 
31   public:
32     static char ID; // Pass identification, replacement for typeid
33     ResetMachineFunction(bool EmitFallbackDiag = false)
34         : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag) {}
35 
36     const char *getPassName() const override {
37       return "ResetMachineFunction";
38     }
39 
40     bool runOnMachineFunction(MachineFunction &MF) override {
41       if (MF.getProperties().hasProperty(
42               MachineFunctionProperties::Property::FailedISel)) {
43         DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
44         ++NumFunctionsReset;
45         MF.reset();
46         if (EmitFallbackDiag) {
47           const Function &F = *MF.getFunction();
48           DiagnosticInfoISelFallback DiagFallback(F);
49           F.getContext().diagnose(DiagFallback);
50         }
51         return true;
52       }
53       return false;
54     }
55 
56   };
57 } // end anonymous namespace
58 
59 char ResetMachineFunction::ID = 0;
60 INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
61                 "reset machine function if ISel failed", false, false)
62 
63 MachineFunctionPass *
64 llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false) {
65   return new ResetMachineFunction(EmitFallbackDiag);
66 }
67