1*380cd88cSQuentin Colombet //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
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 //===----------------------------------------------------------------------===//
9*380cd88cSQuentin Colombet /// \file
10*380cd88cSQuentin Colombet /// This file implements a pass that will conditionally reset a machine
11*380cd88cSQuentin Colombet /// function as if it was just created. This is used to provide a fallback
12*380cd88cSQuentin Colombet /// mechanism when GlobalISel fails, thus the condition for the reset to
13*380cd88cSQuentin Colombet /// happen is that the MachineFunction has the FailedISel property.
14374796d6SQuentin Colombet //===----------------------------------------------------------------------===//
15374796d6SQuentin Colombet 
160f4c20a1SQuentin Colombet #include "llvm/ADT/Statistic.h"
17374796d6SQuentin Colombet #include "llvm/CodeGen/Passes.h"
18374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunction.h"
19374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunctionPass.h"
20612cd1fdSQuentin Colombet #include "llvm/IR/DiagnosticInfo.h"
21374796d6SQuentin Colombet #include "llvm/Support/Debug.h"
22374796d6SQuentin Colombet using namespace llvm;
23374796d6SQuentin Colombet 
24374796d6SQuentin Colombet #define DEBUG_TYPE "reset-machine-function"
25374796d6SQuentin Colombet 
260f4c20a1SQuentin Colombet STATISTIC(NumFunctionsReset, "Number of functions reset");
270f4c20a1SQuentin Colombet 
28374796d6SQuentin Colombet namespace {
29374796d6SQuentin Colombet   class ResetMachineFunction : public MachineFunctionPass {
30612cd1fdSQuentin Colombet     /// Tells whether or not this pass should emit a fallback
31612cd1fdSQuentin Colombet     /// diagnostic when it resets a function.
32612cd1fdSQuentin Colombet     bool EmitFallbackDiag;
33612cd1fdSQuentin Colombet 
34374796d6SQuentin Colombet   public:
35374796d6SQuentin Colombet     static char ID; // Pass identification, replacement for typeid
36612cd1fdSQuentin Colombet     ResetMachineFunction(bool EmitFallbackDiag = false)
37612cd1fdSQuentin Colombet         : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag) {}
38374796d6SQuentin Colombet 
39374796d6SQuentin Colombet     const char *getPassName() const override {
40374796d6SQuentin Colombet       return "ResetMachineFunction";
41374796d6SQuentin Colombet     }
42374796d6SQuentin Colombet 
43374796d6SQuentin Colombet     bool runOnMachineFunction(MachineFunction &MF) override {
44374796d6SQuentin Colombet       if (MF.getProperties().hasProperty(
45374796d6SQuentin Colombet               MachineFunctionProperties::Property::FailedISel)) {
46374796d6SQuentin Colombet         DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
470f4c20a1SQuentin Colombet         ++NumFunctionsReset;
48374796d6SQuentin Colombet         MF.reset();
49612cd1fdSQuentin Colombet         if (EmitFallbackDiag) {
50612cd1fdSQuentin Colombet           const Function &F = *MF.getFunction();
51612cd1fdSQuentin Colombet           DiagnosticInfoISelFallback DiagFallback(F);
52612cd1fdSQuentin Colombet           F.getContext().diagnose(DiagFallback);
53612cd1fdSQuentin Colombet         }
54374796d6SQuentin Colombet         return true;
55374796d6SQuentin Colombet       }
56374796d6SQuentin Colombet       return false;
57374796d6SQuentin Colombet     }
58374796d6SQuentin Colombet 
59374796d6SQuentin Colombet   };
60374796d6SQuentin Colombet } // end anonymous namespace
61374796d6SQuentin Colombet 
62374796d6SQuentin Colombet char ResetMachineFunction::ID = 0;
63374796d6SQuentin Colombet INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
64374796d6SQuentin Colombet                 "reset machine function if ISel failed", false, false)
65374796d6SQuentin Colombet 
66374796d6SQuentin Colombet MachineFunctionPass *
67612cd1fdSQuentin Colombet llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false) {
68612cd1fdSQuentin Colombet   return new ResetMachineFunction(EmitFallbackDiag);
69374796d6SQuentin Colombet }
70