blob: 01b3db43b2836014fba5e4ffaa5ea0b1215b3291 [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/MachineFunction.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/CodeGen/Passes.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;
Justin Bogner1a314da2017-01-13 23:46:11 +000033 /// Whether we should abort immediately instead of resetting the function.
34 bool AbortOnFailedISel;
Quentin Colombet612cd1f2016-08-31 18:43:01 +000035
Quentin Colombet374796d2016-08-27 00:18:31 +000036 public:
37 static char ID; // Pass identification, replacement for typeid
Justin Bogner1a314da2017-01-13 23:46:11 +000038 ResetMachineFunction(bool EmitFallbackDiag = false,
39 bool AbortOnFailedISel = false)
40 : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
41 AbortOnFailedISel(AbortOnFailedISel) {}
Quentin Colombet374796d2016-08-27 00:18:31 +000042
Mehdi Amini117296c2016-10-01 02:56:57 +000043 StringRef getPassName() const override { return "ResetMachineFunction"; }
Quentin Colombet374796d2016-08-27 00:18:31 +000044
45 bool runOnMachineFunction(MachineFunction &MF) override {
46 if (MF.getProperties().hasProperty(
47 MachineFunctionProperties::Property::FailedISel)) {
Justin Bogner1a314da2017-01-13 23:46:11 +000048 if (AbortOnFailedISel)
49 report_fatal_error("Instruction selection failed");
Quentin Colombet374796d2016-08-27 00:18:31 +000050 DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n');
Quentin Colombet0f4c20a2016-09-23 18:38:13 +000051 ++NumFunctionsReset;
Quentin Colombet374796d2016-08-27 00:18:31 +000052 MF.reset();
Quentin Colombet612cd1f2016-08-31 18:43:01 +000053 if (EmitFallbackDiag) {
54 const Function &F = *MF.getFunction();
55 DiagnosticInfoISelFallback DiagFallback(F);
56 F.getContext().diagnose(DiagFallback);
57 }
Quentin Colombet374796d2016-08-27 00:18:31 +000058 return true;
59 }
60 return false;
61 }
62
63 };
64} // end anonymous namespace
65
66char ResetMachineFunction::ID = 0;
67INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
68 "reset machine function if ISel failed", false, false)
69
70MachineFunctionPass *
Justin Bogner1a314da2017-01-13 23:46:11 +000071llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
72 bool AbortOnFailedISel = false) {
73 return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
Quentin Colombet374796d2016-08-27 00:18:31 +000074}