1 //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// This file implements a pass that will conditionally reset a machine
10 /// function as if it was just created. This is used to provide a fallback
11 /// mechanism when GlobalISel fails, thus the condition for the reset to
12 /// happen is that the MachineFunction has the FailedISel property.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/StackProtector.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/Support/Debug.h"
26 #define DEBUG_TYPE "reset-machine-function"
28 STATISTIC(NumFunctionsReset
, "Number of functions reset");
29 STATISTIC(NumFunctionsVisited
, "Number of functions visited");
32 class ResetMachineFunction
: public MachineFunctionPass
{
33 /// Tells whether or not this pass should emit a fallback
34 /// diagnostic when it resets a function.
35 bool EmitFallbackDiag
;
36 /// Whether we should abort immediately instead of resetting the function.
37 bool AbortOnFailedISel
;
40 static char ID
; // Pass identification, replacement for typeid
41 ResetMachineFunction(bool EmitFallbackDiag
= false,
42 bool AbortOnFailedISel
= false)
43 : MachineFunctionPass(ID
), EmitFallbackDiag(EmitFallbackDiag
),
44 AbortOnFailedISel(AbortOnFailedISel
) {}
46 StringRef
getPassName() const override
{ return "ResetMachineFunction"; }
48 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
49 AU
.addPreserved
<StackProtector
>();
50 MachineFunctionPass::getAnalysisUsage(AU
);
53 bool runOnMachineFunction(MachineFunction
&MF
) override
{
54 ++NumFunctionsVisited
;
55 // No matter what happened, whether we successfully selected the function
56 // or not, nothing is going to use the vreg types after us. Make sure they
58 auto ClearVRegTypesOnReturn
=
59 make_scope_exit([&MF
]() { MF
.getRegInfo().clearVirtRegTypes(); });
61 if (MF
.getProperties().hasProperty(
62 MachineFunctionProperties::Property::FailedISel
)) {
63 if (AbortOnFailedISel
)
64 report_fatal_error("Instruction selection failed");
65 LLVM_DEBUG(dbgs() << "Resetting: " << MF
.getName() << '\n');
68 if (EmitFallbackDiag
) {
69 const Function
&F
= MF
.getFunction();
70 DiagnosticInfoISelFallback
DiagFallback(F
);
71 F
.getContext().diagnose(DiagFallback
);
79 } // end anonymous namespace
81 char ResetMachineFunction::ID
= 0;
82 INITIALIZE_PASS(ResetMachineFunction
, DEBUG_TYPE
,
83 "Reset machine function if ISel failed", false, false)
86 llvm::createResetMachineFunctionPass(bool EmitFallbackDiag
= false,
87 bool AbortOnFailedISel
= false) {
88 return new ResetMachineFunction(EmitFallbackDiag
, AbortOnFailedISel
);