1380cd88cSQuentin 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 //===----------------------------------------------------------------------===//
9380cd88cSQuentin Colombet /// \file
10380cd88cSQuentin Colombet /// This file implements a pass that will conditionally reset a machine
11380cd88cSQuentin Colombet /// function as if it was just created. This is used to provide a fallback
12380cd88cSQuentin Colombet /// mechanism when GlobalISel fails, thus the condition for the reset to
13380cd88cSQuentin 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 
39*117296c0SMehdi Amini     StringRef getPassName() const override { return "ResetMachineFunction"; }
40374796d6SQuentin Colombet 
41374796d6SQuentin Colombet     bool runOnMachineFunction(MachineFunction &MF) override {
42374796d6SQuentin Colombet       if (MF.getProperties().hasProperty(
43374796d6SQuentin Colombet               MachineFunctionProperties::Property::FailedISel)) {
44374796d6SQuentin Colombet         DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
450f4c20a1SQuentin Colombet         ++NumFunctionsReset;
46374796d6SQuentin Colombet         MF.reset();
47612cd1fdSQuentin Colombet         if (EmitFallbackDiag) {
48612cd1fdSQuentin Colombet           const Function &F = *MF.getFunction();
49612cd1fdSQuentin Colombet           DiagnosticInfoISelFallback DiagFallback(F);
50612cd1fdSQuentin Colombet           F.getContext().diagnose(DiagFallback);
51612cd1fdSQuentin Colombet         }
52374796d6SQuentin Colombet         return true;
53374796d6SQuentin Colombet       }
54374796d6SQuentin Colombet       return false;
55374796d6SQuentin Colombet     }
56374796d6SQuentin Colombet 
57374796d6SQuentin Colombet   };
58374796d6SQuentin Colombet } // end anonymous namespace
59374796d6SQuentin Colombet 
60374796d6SQuentin Colombet char ResetMachineFunction::ID = 0;
61374796d6SQuentin Colombet INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
62374796d6SQuentin Colombet                 "reset machine function if ISel failed", false, false)
63374796d6SQuentin Colombet 
64374796d6SQuentin Colombet MachineFunctionPass *
65612cd1fdSQuentin Colombet llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false) {
66612cd1fdSQuentin Colombet   return new ResetMachineFunction(EmitFallbackDiag);
67374796d6SQuentin Colombet }
68