blob: 451964199ba5a3b28f1bbcac6fd0d9892ea5d986 [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
Mehdi Amini117296c2016-10-01 02:56:57 +000039 StringRef getPassName() const override { return "ResetMachineFunction"; }
Quentin Colombet374796d2016-08-27 00:18:31 +000040
41 bool runOnMachineFunction(MachineFunction &MF) override {
42 if (MF.getProperties().hasProperty(
43 MachineFunctionProperties::Property::FailedISel)) {
44 DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
Quentin Colombet0f4c20a2016-09-23 18:38:13 +000045 ++NumFunctionsReset;
Quentin Colombet374796d2016-08-27 00:18:31 +000046 MF.reset();
Quentin Colombet612cd1f2016-08-31 18:43:01 +000047 if (EmitFallbackDiag) {
48 const Function &F = *MF.getFunction();
49 DiagnosticInfoISelFallback DiagFallback(F);
50 F.getContext().diagnose(DiagFallback);
51 }
Quentin Colombet374796d2016-08-27 00:18:31 +000052 return true;
53 }
54 return false;
55 }
56
57 };
58} // end anonymous namespace
59
60char ResetMachineFunction::ID = 0;
61INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
62 "reset machine function if ISel failed", false, false)
63
64MachineFunctionPass *
Quentin Colombet612cd1f2016-08-31 18:43:01 +000065llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false) {
66 return new ResetMachineFunction(EmitFallbackDiag);
Quentin Colombet374796d2016-08-27 00:18:31 +000067}