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/MachineFunction.h"
18374796d6SQuentin Colombet #include "llvm/CodeGen/MachineFunctionPass.h"
196bda14b3SChandler Carruth #include "llvm/CodeGen/Passes.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;
331a314dacSJustin Bogner     /// Whether we should abort immediately instead of resetting the function.
341a314dacSJustin Bogner     bool AbortOnFailedISel;
35612cd1fdSQuentin Colombet 
36374796d6SQuentin Colombet   public:
37374796d6SQuentin Colombet     static char ID; // Pass identification, replacement for typeid
381a314dacSJustin Bogner     ResetMachineFunction(bool EmitFallbackDiag = false,
391a314dacSJustin Bogner                          bool AbortOnFailedISel = false)
401a314dacSJustin Bogner         : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
411a314dacSJustin Bogner           AbortOnFailedISel(AbortOnFailedISel) {}
42374796d6SQuentin Colombet 
43117296c0SMehdi Amini     StringRef getPassName() const override { return "ResetMachineFunction"; }
44374796d6SQuentin Colombet 
45374796d6SQuentin Colombet     bool runOnMachineFunction(MachineFunction &MF) override {
46374796d6SQuentin Colombet       if (MF.getProperties().hasProperty(
47374796d6SQuentin Colombet               MachineFunctionProperties::Property::FailedISel)) {
481a314dacSJustin Bogner         if (AbortOnFailedISel)
491a314dacSJustin Bogner           report_fatal_error("Instruction selection failed");
50374796d6SQuentin Colombet         DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
510f4c20a1SQuentin Colombet         ++NumFunctionsReset;
52374796d6SQuentin Colombet         MF.reset();
53612cd1fdSQuentin Colombet         if (EmitFallbackDiag) {
54*f1caa283SMatthias Braun           const Function &F = MF.getFunction();
55612cd1fdSQuentin Colombet           DiagnosticInfoISelFallback DiagFallback(F);
56612cd1fdSQuentin Colombet           F.getContext().diagnose(DiagFallback);
57612cd1fdSQuentin Colombet         }
58374796d6SQuentin Colombet         return true;
59374796d6SQuentin Colombet       }
60374796d6SQuentin Colombet       return false;
61374796d6SQuentin Colombet     }
62374796d6SQuentin Colombet 
63374796d6SQuentin Colombet   };
64374796d6SQuentin Colombet } // end anonymous namespace
65374796d6SQuentin Colombet 
66374796d6SQuentin Colombet char ResetMachineFunction::ID = 0;
67374796d6SQuentin Colombet INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
68374796d6SQuentin Colombet                 "reset machine function if ISel failed", false, false)
69374796d6SQuentin Colombet 
70374796d6SQuentin Colombet MachineFunctionPass *
711a314dacSJustin Bogner llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
721a314dacSJustin Bogner                                      bool AbortOnFailedISel = false) {
731a314dacSJustin Bogner   return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
74374796d6SQuentin Colombet }
75