blob: 72b631baf94a2a69d137f38116e91e2c1b2e8da0 [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
Roman Tereshin3054ece2018-02-28 17:55:45 +000016#include "llvm/ADT/ScopeExit.h"
Quentin Colombet0f4c20a2016-09-23 18:38:13 +000017#include "llvm/ADT/Statistic.h"
Quentin Colombet374796d2016-08-27 00:18:31 +000018#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
Roman Tereshin3054ece2018-02-28 17:55:45 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/CodeGen/Passes.h"
Quentin Colombet612cd1f2016-08-31 18:43:01 +000022#include "llvm/IR/DiagnosticInfo.h"
Quentin Colombet374796d2016-08-27 00:18:31 +000023#include "llvm/Support/Debug.h"
24using namespace llvm;
25
26#define DEBUG_TYPE "reset-machine-function"
27
Quentin Colombet0f4c20a2016-09-23 18:38:13 +000028STATISTIC(NumFunctionsReset, "Number of functions reset");
29
Quentin Colombet374796d2016-08-27 00:18:31 +000030namespace {
31 class ResetMachineFunction : public MachineFunctionPass {
Quentin Colombet612cd1f2016-08-31 18:43:01 +000032 /// Tells whether or not this pass should emit a fallback
33 /// diagnostic when it resets a function.
34 bool EmitFallbackDiag;
Justin Bogner1a314da2017-01-13 23:46:11 +000035 /// Whether we should abort immediately instead of resetting the function.
36 bool AbortOnFailedISel;
Quentin Colombet612cd1f2016-08-31 18:43:01 +000037
Quentin Colombet374796d2016-08-27 00:18:31 +000038 public:
39 static char ID; // Pass identification, replacement for typeid
Justin Bogner1a314da2017-01-13 23:46:11 +000040 ResetMachineFunction(bool EmitFallbackDiag = false,
41 bool AbortOnFailedISel = false)
42 : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
43 AbortOnFailedISel(AbortOnFailedISel) {}
Quentin Colombet374796d2016-08-27 00:18:31 +000044
Mehdi Amini117296c2016-10-01 02:56:57 +000045 StringRef getPassName() const override { return "ResetMachineFunction"; }
Quentin Colombet374796d2016-08-27 00:18:31 +000046
47 bool runOnMachineFunction(MachineFunction &MF) override {
Roman Tereshin3054ece2018-02-28 17:55:45 +000048 // No matter what happened, whether we successfully selected the function
49 // or not, nothing is going to use the vreg types after us. Make sure they
50 // disappear.
51 auto ClearVRegTypesOnReturn =
52 make_scope_exit([&MF]() { MF.getRegInfo().getVRegToType().clear(); });
53
Quentin Colombet374796d2016-08-27 00:18:31 +000054 if (MF.getProperties().hasProperty(
55 MachineFunctionProperties::Property::FailedISel)) {
Justin Bogner1a314da2017-01-13 23:46:11 +000056 if (AbortOnFailedISel)
57 report_fatal_error("Instruction selection failed");
Amara Emerson98af4662018-02-02 01:49:59 +000058 DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
Quentin Colombet0f4c20a2016-09-23 18:38:13 +000059 ++NumFunctionsReset;
Quentin Colombet374796d2016-08-27 00:18:31 +000060 MF.reset();
Quentin Colombet612cd1f2016-08-31 18:43:01 +000061 if (EmitFallbackDiag) {
Matthias Braunf1caa282017-12-15 22:22:58 +000062 const Function &F = MF.getFunction();
Quentin Colombet612cd1f2016-08-31 18:43:01 +000063 DiagnosticInfoISelFallback DiagFallback(F);
64 F.getContext().diagnose(DiagFallback);
65 }
Quentin Colombet374796d2016-08-27 00:18:31 +000066 return true;
67 }
68 return false;
69 }
70
71 };
72} // end anonymous namespace
73
74char ResetMachineFunction::ID = 0;
75INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
Amara Emerson98af4662018-02-02 01:49:59 +000076 "Reset machine function if ISel failed", false, false)
Quentin Colombet374796d2016-08-27 00:18:31 +000077
78MachineFunctionPass *
Justin Bogner1a314da2017-01-13 23:46:11 +000079llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
80 bool AbortOnFailedISel = false) {
81 return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
Quentin Colombet374796d2016-08-27 00:18:31 +000082}