17330f729Sjoerg //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg /// \file
97330f729Sjoerg /// This file implements a pass that will conditionally reset a machine
107330f729Sjoerg /// function as if it was just created. This is used to provide a fallback
117330f729Sjoerg /// mechanism when GlobalISel fails, thus the condition for the reset to
127330f729Sjoerg /// happen is that the MachineFunction has the FailedISel property.
137330f729Sjoerg //===----------------------------------------------------------------------===//
147330f729Sjoerg
157330f729Sjoerg #include "llvm/ADT/ScopeExit.h"
167330f729Sjoerg #include "llvm/ADT/Statistic.h"
177330f729Sjoerg #include "llvm/CodeGen/MachineFunction.h"
187330f729Sjoerg #include "llvm/CodeGen/MachineFunctionPass.h"
197330f729Sjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
207330f729Sjoerg #include "llvm/CodeGen/Passes.h"
21*82d56013Sjoerg #include "llvm/CodeGen/StackProtector.h"
227330f729Sjoerg #include "llvm/IR/DiagnosticInfo.h"
23*82d56013Sjoerg #include "llvm/InitializePasses.h"
247330f729Sjoerg #include "llvm/Support/Debug.h"
257330f729Sjoerg using namespace llvm;
267330f729Sjoerg
277330f729Sjoerg #define DEBUG_TYPE "reset-machine-function"
287330f729Sjoerg
297330f729Sjoerg STATISTIC(NumFunctionsReset, "Number of functions reset");
307330f729Sjoerg STATISTIC(NumFunctionsVisited, "Number of functions visited");
317330f729Sjoerg
327330f729Sjoerg namespace {
337330f729Sjoerg class ResetMachineFunction : public MachineFunctionPass {
347330f729Sjoerg /// Tells whether or not this pass should emit a fallback
357330f729Sjoerg /// diagnostic when it resets a function.
367330f729Sjoerg bool EmitFallbackDiag;
377330f729Sjoerg /// Whether we should abort immediately instead of resetting the function.
387330f729Sjoerg bool AbortOnFailedISel;
397330f729Sjoerg
407330f729Sjoerg public:
417330f729Sjoerg static char ID; // Pass identification, replacement for typeid
ResetMachineFunction(bool EmitFallbackDiag=false,bool AbortOnFailedISel=false)427330f729Sjoerg ResetMachineFunction(bool EmitFallbackDiag = false,
437330f729Sjoerg bool AbortOnFailedISel = false)
447330f729Sjoerg : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
457330f729Sjoerg AbortOnFailedISel(AbortOnFailedISel) {}
467330f729Sjoerg
getPassName() const477330f729Sjoerg StringRef getPassName() const override { return "ResetMachineFunction"; }
487330f729Sjoerg
getAnalysisUsage(AnalysisUsage & AU) const497330f729Sjoerg void getAnalysisUsage(AnalysisUsage &AU) const override {
507330f729Sjoerg AU.addPreserved<StackProtector>();
517330f729Sjoerg MachineFunctionPass::getAnalysisUsage(AU);
527330f729Sjoerg }
537330f729Sjoerg
runOnMachineFunction(MachineFunction & MF)547330f729Sjoerg bool runOnMachineFunction(MachineFunction &MF) override {
557330f729Sjoerg ++NumFunctionsVisited;
567330f729Sjoerg // No matter what happened, whether we successfully selected the function
577330f729Sjoerg // or not, nothing is going to use the vreg types after us. Make sure they
587330f729Sjoerg // disappear.
597330f729Sjoerg auto ClearVRegTypesOnReturn =
607330f729Sjoerg make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
617330f729Sjoerg
627330f729Sjoerg if (MF.getProperties().hasProperty(
637330f729Sjoerg MachineFunctionProperties::Property::FailedISel)) {
647330f729Sjoerg if (AbortOnFailedISel)
657330f729Sjoerg report_fatal_error("Instruction selection failed");
667330f729Sjoerg LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
677330f729Sjoerg ++NumFunctionsReset;
687330f729Sjoerg MF.reset();
697330f729Sjoerg if (EmitFallbackDiag) {
707330f729Sjoerg const Function &F = MF.getFunction();
717330f729Sjoerg DiagnosticInfoISelFallback DiagFallback(F);
727330f729Sjoerg F.getContext().diagnose(DiagFallback);
737330f729Sjoerg }
747330f729Sjoerg return true;
757330f729Sjoerg }
767330f729Sjoerg return false;
777330f729Sjoerg }
787330f729Sjoerg
797330f729Sjoerg };
807330f729Sjoerg } // end anonymous namespace
817330f729Sjoerg
827330f729Sjoerg char ResetMachineFunction::ID = 0;
837330f729Sjoerg INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
847330f729Sjoerg "Reset machine function if ISel failed", false, false)
857330f729Sjoerg
867330f729Sjoerg MachineFunctionPass *
createResetMachineFunctionPass(bool EmitFallbackDiag=false,bool AbortOnFailedISel=false)877330f729Sjoerg llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
887330f729Sjoerg bool AbortOnFailedISel = false) {
897330f729Sjoerg return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
907330f729Sjoerg }
91