blob: f604e431289d379dff46e43144d1e6fe682bc41b [file] [log] [blame]
Quentin Colombet380cd882016-09-23 18:38:15 +00001//===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
Quentin Colombet374796d2016-08-27 00:18:31 +00002//
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//===----------------------------------------------------------------------===//
Quentin Colombet380cd882016-09-23 18:38:15 +00009/// \file
10/// This file implements a pass that will conditionally reset a machine
11/// function as if it was just created. This is used to provide a fallback
12/// mechanism when GlobalISel fails, thus the condition for the reset to
13/// happen is that the MachineFunction has the FailedISel property.
Quentin Colombet374796d2016-08-27 00:18:31 +000014//===----------------------------------------------------------------------===//
15
Quentin Colombet0f4c20a2016-09-23 18:38:13 +000016#include "llvm/ADT/Statistic.h"
Quentin Colombet374796d2016-08-27 00:18:31 +000017#include "llvm/CodeGen/Passes.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
Quentin Colombet612cd1f2016-08-31 18:43:01 +000020#include "llvm/IR/DiagnosticInfo.h"
Quentin Colombet374796d2016-08-27 00:18:31 +000021#include "llvm/Support/Debug.h"
22using namespace llvm;
23
24#define DEBUG_TYPE "reset-machine-function"
25
Quentin Colombet0f4c20a2016-09-23 18:38:13 +000026STATISTIC(NumFunctionsReset, "Number of functions reset");
27
Quentin Colombet374796d2016-08-27 00:18:31 +000028namespace {
29 class ResetMachineFunction : public MachineFunctionPass {
Quentin Colombet612cd1f2016-08-31 18:43:01 +000030 /// Tells whether or not this pass should emit a fallback
31 /// diagnostic when it resets a function.
32 bool EmitFallbackDiag;
33
Quentin Colombet374796d2016-08-27 00:18:31 +000034 public:
35 static char ID; // Pass identification, replacement for typeid
Quentin Colombet612cd1f2016-08-31 18:43:01 +000036 ResetMachineFunction(bool EmitFallbackDiag = false)
37 : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag) {}
Quentin Colombet374796d2016-08-27 00:18:31 +000038
39 const char *getPassName() const override {
40 return "ResetMachineFunction";
41 }
42
43 bool runOnMachineFunction(MachineFunction &MF) override {
44 if (MF.getProperties().hasProperty(
45 MachineFunctionProperties::Property::FailedISel)) {
46 DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
Quentin Colombet0f4c20a2016-09-23 18:38:13 +000047 ++NumFunctionsReset;
Quentin Colombet374796d2016-08-27 00:18:31 +000048 MF.reset();
Quentin Colombet612cd1f2016-08-31 18:43:01 +000049 if (EmitFallbackDiag) {
50 const Function &F = *MF.getFunction();
51 DiagnosticInfoISelFallback DiagFallback(F);
52 F.getContext().diagnose(DiagFallback);
53 }
Quentin Colombet374796d2016-08-27 00:18:31 +000054 return true;
55 }
56 return false;
57 }
58
59 };
60} // end anonymous namespace
61
62char ResetMachineFunction::ID = 0;
63INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
64 "reset machine function if ISel failed", false, false)
65
66MachineFunctionPass *
Quentin Colombet612cd1f2016-08-31 18:43:01 +000067llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false) {
68 return new ResetMachineFunction(EmitFallbackDiag);
Quentin Colombet374796d2016-08-27 00:18:31 +000069}