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