xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
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"
21*480093f4SDimitry Andric #include "llvm/CodeGen/StackProtector.h"
220b57cec5SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
23*480093f4SDimitry Andric #include "llvm/InitializePasses.h"
240b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
250b57cec5SDimitry Andric using namespace llvm;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric #define DEBUG_TYPE "reset-machine-function"
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric STATISTIC(NumFunctionsReset, "Number of functions reset");
300b57cec5SDimitry Andric STATISTIC(NumFunctionsVisited, "Number of functions visited");
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric namespace {
330b57cec5SDimitry Andric   class ResetMachineFunction : public MachineFunctionPass {
340b57cec5SDimitry Andric     /// Tells whether or not this pass should emit a fallback
350b57cec5SDimitry Andric     /// diagnostic when it resets a function.
360b57cec5SDimitry Andric     bool EmitFallbackDiag;
370b57cec5SDimitry Andric     /// Whether we should abort immediately instead of resetting the function.
380b57cec5SDimitry Andric     bool AbortOnFailedISel;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric   public:
410b57cec5SDimitry Andric     static char ID; // Pass identification, replacement for typeid
420b57cec5SDimitry Andric     ResetMachineFunction(bool EmitFallbackDiag = false,
430b57cec5SDimitry Andric                          bool AbortOnFailedISel = false)
440b57cec5SDimitry Andric         : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag),
450b57cec5SDimitry Andric           AbortOnFailedISel(AbortOnFailedISel) {}
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric     StringRef getPassName() const override { return "ResetMachineFunction"; }
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
500b57cec5SDimitry Andric       AU.addPreserved<StackProtector>();
510b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
520b57cec5SDimitry Andric     }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override {
550b57cec5SDimitry Andric       ++NumFunctionsVisited;
560b57cec5SDimitry Andric       // No matter what happened, whether we successfully selected the function
570b57cec5SDimitry Andric       // or not, nothing is going to use the vreg types after us. Make sure they
580b57cec5SDimitry Andric       // disappear.
590b57cec5SDimitry Andric       auto ClearVRegTypesOnReturn =
600b57cec5SDimitry Andric           make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); });
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric       if (MF.getProperties().hasProperty(
630b57cec5SDimitry Andric               MachineFunctionProperties::Property::FailedISel)) {
640b57cec5SDimitry Andric         if (AbortOnFailedISel)
650b57cec5SDimitry Andric           report_fatal_error("Instruction selection failed");
660b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n');
670b57cec5SDimitry Andric         ++NumFunctionsReset;
680b57cec5SDimitry Andric         MF.reset();
690b57cec5SDimitry Andric         if (EmitFallbackDiag) {
700b57cec5SDimitry Andric           const Function &F = MF.getFunction();
710b57cec5SDimitry Andric           DiagnosticInfoISelFallback DiagFallback(F);
720b57cec5SDimitry Andric           F.getContext().diagnose(DiagFallback);
730b57cec5SDimitry Andric         }
740b57cec5SDimitry Andric         return true;
750b57cec5SDimitry Andric       }
760b57cec5SDimitry Andric       return false;
770b57cec5SDimitry Andric     }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   };
800b57cec5SDimitry Andric } // end anonymous namespace
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric char ResetMachineFunction::ID = 0;
830b57cec5SDimitry Andric INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE,
840b57cec5SDimitry Andric                 "Reset machine function if ISel failed", false, false)
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric MachineFunctionPass *
870b57cec5SDimitry Andric llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false,
880b57cec5SDimitry Andric                                      bool AbortOnFailedISel = false) {
890b57cec5SDimitry Andric   return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel);
900b57cec5SDimitry Andric }
91