xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric /// \file
90b57cec5SDimitry Andric /// This file implements a pass that will conditionally reset a machine
100b57cec5SDimitry Andric /// function as if it was just created. This is used to provide a fallback
110b57cec5SDimitry Andric /// mechanism when GlobalISel fails, thus the condition for the reset to
120b57cec5SDimitry Andric /// happen is that the MachineFunction has the FailedISel property.
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/ADT/ScopeExit.h"
160b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
170b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h"
21480093f4SDimitry Andric #include "llvm/CodeGen/StackProtector.h"
220b57cec5SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
23480093f4SDimitry Andric #include "llvm/InitializePasses.h"
240b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
25*06c3fb27SDimitry Andric #include "llvm/Target/TargetMachine.h"
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #define DEBUG_TYPE "reset-machine-function"
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric STATISTIC(NumFunctionsReset, "Number of functions reset");
310b57cec5SDimitry Andric STATISTIC(NumFunctionsVisited, "Number of functions visited");
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric namespace {
340b57cec5SDimitry Andric   class ResetMachineFunction : public MachineFunctionPass {
350b57cec5SDimitry Andric     /// Tells whether or not this pass should emit a fallback
360b57cec5SDimitry Andric     /// diagnostic when it resets a function.
370b57cec5SDimitry Andric     bool EmitFallbackDiag;
380b57cec5SDimitry Andric     /// Whether we should abort immediately instead of resetting the function.
390b57cec5SDimitry Andric     bool AbortOnFailedISel;
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   public:
420b57cec5SDimitry Andric     static char ID; // Pass identification, replacement for typeid
ResetMachineFunction(bool EmitFallbackDiag=false,bool AbortOnFailedISel=false)430b57cec5SDimitry Andric     ResetMachineFunction(bool EmitFallbackDiag = false,
440b57cec5SDimitry Andric                          bool AbortOnFailedISel = false)
450b57cec5SDimitry Andric         : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
460b57cec5SDimitry Andric           AbortOnFailedISel(AbortOnFailedISel) {}
470b57cec5SDimitry Andric 
getPassName() const480b57cec5SDimitry Andric     StringRef getPassName() const override { return "ResetMachineFunction"; }
490b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const500b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
510b57cec5SDimitry Andric       AU.addPreserved<StackProtector>();
520b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
530b57cec5SDimitry Andric     }
540b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)550b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override {
560b57cec5SDimitry Andric       ++NumFunctionsVisited;
570b57cec5SDimitry Andric       // No matter what happened, whether we successfully selected the function
580b57cec5SDimitry Andric       // or not, nothing is going to use the vreg types after us. Make sure they
590b57cec5SDimitry Andric       // disappear.
600b57cec5SDimitry Andric       auto ClearVRegTypesOnReturn =
610b57cec5SDimitry Andric           make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric       if (MF.getProperties().hasProperty(
640b57cec5SDimitry Andric               MachineFunctionProperties::Property::FailedISel)) {
650b57cec5SDimitry Andric         if (AbortOnFailedISel)
660b57cec5SDimitry Andric           report_fatal_error("Instruction selection failed");
670b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
680b57cec5SDimitry Andric         ++NumFunctionsReset;
690b57cec5SDimitry Andric         MF.reset();
70bdd1243dSDimitry Andric         MF.initTargetMachineFunctionInfo(MF.getSubtarget());
71bdd1243dSDimitry Andric 
72*06c3fb27SDimitry Andric         const LLVMTargetMachine &TM = MF.getTarget();
73*06c3fb27SDimitry Andric         // MRI callback for target specific initializations.
74*06c3fb27SDimitry Andric         TM.registerMachineRegisterInfoCallback(MF);
75*06c3fb27SDimitry Andric 
760b57cec5SDimitry Andric         if (EmitFallbackDiag) {
770b57cec5SDimitry Andric           const Function &F = MF.getFunction();
780b57cec5SDimitry Andric           DiagnosticInfoISelFallback DiagFallback(F);
790b57cec5SDimitry Andric           F.getContext().diagnose(DiagFallback);
800b57cec5SDimitry Andric         }
810b57cec5SDimitry Andric         return true;
820b57cec5SDimitry Andric       }
830b57cec5SDimitry Andric       return false;
840b57cec5SDimitry Andric     }
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   };
870b57cec5SDimitry Andric } // end anonymous namespace
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric char ResetMachineFunction::ID = 0;
900b57cec5SDimitry Andric INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
910b57cec5SDimitry Andric                 "Reset machine function if ISel failed", false, false)
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric MachineFunctionPass *
createResetMachineFunctionPass(bool EmitFallbackDiag=false,bool AbortOnFailedISel=false)940b57cec5SDimitry Andric llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
950b57cec5SDimitry Andric                                      bool AbortOnFailedISel = false) {
960b57cec5SDimitry Andric   return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
970b57cec5SDimitry Andric }
98