1b915e9e0SDimitry Andric //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
2b915e9e0SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b915e9e0SDimitry Andric //
7b915e9e0SDimitry Andric //===----------------------------------------------------------------------===//
8b915e9e0SDimitry Andric /// \file
9b915e9e0SDimitry Andric /// This file implements a pass that will conditionally reset a machine
10b915e9e0SDimitry Andric /// function as if it was just created. This is used to provide a fallback
11b915e9e0SDimitry Andric /// mechanism when GlobalISel fails, thus the condition for the reset to
12b915e9e0SDimitry Andric /// happen is that the MachineFunction has the FailedISel property.
13b915e9e0SDimitry Andric //===----------------------------------------------------------------------===//
14b915e9e0SDimitry Andric
15eb11fae6SDimitry Andric #include "llvm/ADT/ScopeExit.h"
16b915e9e0SDimitry Andric #include "llvm/ADT/Statistic.h"
17b915e9e0SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
18b915e9e0SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
19eb11fae6SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
207ab83427SDimitry Andric #include "llvm/CodeGen/Passes.h"
21706b4fc4SDimitry Andric #include "llvm/CodeGen/StackProtector.h"
22b915e9e0SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
23706b4fc4SDimitry Andric #include "llvm/InitializePasses.h"
24b915e9e0SDimitry Andric #include "llvm/Support/Debug.h"
257fa27ce4SDimitry Andric #include "llvm/Target/TargetMachine.h"
26b915e9e0SDimitry Andric using namespace llvm;
27b915e9e0SDimitry Andric
28b915e9e0SDimitry Andric #define DEBUG_TYPE "reset-machine-function"
29b915e9e0SDimitry Andric
30b915e9e0SDimitry Andric STATISTIC(NumFunctionsReset, "Number of functions reset");
31e6d15924SDimitry Andric STATISTIC(NumFunctionsVisited, "Number of functions visited");
32b915e9e0SDimitry Andric
33b915e9e0SDimitry Andric namespace {
34b915e9e0SDimitry Andric class ResetMachineFunction : public MachineFunctionPass {
35b915e9e0SDimitry Andric /// Tells whether or not this pass should emit a fallback
36b915e9e0SDimitry Andric /// diagnostic when it resets a function.
37b915e9e0SDimitry Andric bool EmitFallbackDiag;
3871d5a254SDimitry Andric /// Whether we should abort immediately instead of resetting the function.
3971d5a254SDimitry Andric bool AbortOnFailedISel;
40b915e9e0SDimitry Andric
41b915e9e0SDimitry Andric public:
42b915e9e0SDimitry Andric static char ID; // Pass identification, replacement for typeid
ResetMachineFunction(bool EmitFallbackDiag=false,bool AbortOnFailedISel=false)4371d5a254SDimitry Andric ResetMachineFunction(bool EmitFallbackDiag = false,
4471d5a254SDimitry Andric bool AbortOnFailedISel = false)
4571d5a254SDimitry Andric : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
4671d5a254SDimitry Andric AbortOnFailedISel(AbortOnFailedISel) {}
47b915e9e0SDimitry Andric
getPassName() const48b915e9e0SDimitry Andric StringRef getPassName() const override { return "ResetMachineFunction"; }
49b915e9e0SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const50eb11fae6SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
51eb11fae6SDimitry Andric AU.addPreserved<StackProtector>();
52eb11fae6SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
53eb11fae6SDimitry Andric }
54eb11fae6SDimitry Andric
runOnMachineFunction(MachineFunction & MF)55b915e9e0SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override {
56e6d15924SDimitry Andric ++NumFunctionsVisited;
57eb11fae6SDimitry Andric // No matter what happened, whether we successfully selected the function
58eb11fae6SDimitry Andric // or not, nothing is going to use the vreg types after us. Make sure they
59eb11fae6SDimitry Andric // disappear.
60eb11fae6SDimitry Andric auto ClearVRegTypesOnReturn =
61eb11fae6SDimitry Andric make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
62eb11fae6SDimitry Andric
63b915e9e0SDimitry Andric if (MF.getProperties().hasProperty(
64b915e9e0SDimitry Andric MachineFunctionProperties::Property::FailedISel)) {
6571d5a254SDimitry Andric if (AbortOnFailedISel)
6671d5a254SDimitry Andric report_fatal_error("Instruction selection failed");
67eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
68b915e9e0SDimitry Andric ++NumFunctionsReset;
69b915e9e0SDimitry Andric MF.reset();
70e3b55780SDimitry Andric MF.initTargetMachineFunctionInfo(MF.getSubtarget());
71e3b55780SDimitry Andric
727fa27ce4SDimitry Andric const LLVMTargetMachine &TM = MF.getTarget();
737fa27ce4SDimitry Andric // MRI callback for target specific initializations.
747fa27ce4SDimitry Andric TM.registerMachineRegisterInfoCallback(MF);
757fa27ce4SDimitry Andric
76b915e9e0SDimitry Andric if (EmitFallbackDiag) {
77044eb2f6SDimitry Andric const Function &F = MF.getFunction();
78b915e9e0SDimitry Andric DiagnosticInfoISelFallback DiagFallback(F);
79b915e9e0SDimitry Andric F.getContext().diagnose(DiagFallback);
80b915e9e0SDimitry Andric }
81b915e9e0SDimitry Andric return true;
82b915e9e0SDimitry Andric }
83b915e9e0SDimitry Andric return false;
84b915e9e0SDimitry Andric }
85b915e9e0SDimitry Andric
86b915e9e0SDimitry Andric };
87b915e9e0SDimitry Andric } // end anonymous namespace
88b915e9e0SDimitry Andric
89b915e9e0SDimitry Andric char ResetMachineFunction::ID = 0;
90b915e9e0SDimitry Andric INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
91eb11fae6SDimitry Andric "Reset machine function if ISel failed", false, false)
92b915e9e0SDimitry Andric
93b915e9e0SDimitry Andric MachineFunctionPass *
createResetMachineFunctionPass(bool EmitFallbackDiag=false,bool AbortOnFailedISel=false)9471d5a254SDimitry Andric llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
9571d5a254SDimitry Andric bool AbortOnFailedISel = false) {
9671d5a254SDimitry Andric return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
97b915e9e0SDimitry Andric }
98