/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/ResetMachineFunctionPass.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | /// \file |
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 | | //===----------------------------------------------------------------------===// |
14 | | |
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" |
24 | | using namespace llvm; |
25 | | |
26 | | #define DEBUG_TYPE "reset-machine-function" |
27 | | |
28 | | STATISTIC(NumFunctionsReset, "Number of functions reset"); |
29 | | STATISTIC(NumFunctionsVisited, "Number of functions visited"); |
30 | | |
31 | | namespace { |
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; |
38 | | |
39 | | public: |
40 | | static char ID; // Pass identification, replacement for typeid |
41 | | ResetMachineFunction(bool EmitFallbackDiag = false, |
42 | | bool AbortOnFailedISel = false) |
43 | | : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag), |
44 | 7.00k | AbortOnFailedISel(AbortOnFailedISel) {} |
45 | | |
46 | 243k | StringRef getPassName() const override { return "ResetMachineFunction"; } |
47 | | |
48 | 6.93k | void getAnalysisUsage(AnalysisUsage &AU) const override { |
49 | 6.93k | AU.addPreserved<StackProtector>(); |
50 | 6.93k | MachineFunctionPass::getAnalysisUsage(AU); |
51 | 6.93k | } |
52 | | |
53 | 236k | bool runOnMachineFunction(MachineFunction &MF) override { |
54 | 236k | ++NumFunctionsVisited; |
55 | 236k | // No matter what happened, whether we successfully selected the function |
56 | 236k | // or not, nothing is going to use the vreg types after us. Make sure they |
57 | 236k | // disappear. |
58 | 236k | auto ClearVRegTypesOnReturn = |
59 | 236k | make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); }); |
60 | 236k | |
61 | 236k | if (MF.getProperties().hasProperty( |
62 | 236k | MachineFunctionProperties::Property::FailedISel)) { |
63 | 16.2k | if (AbortOnFailedISel) |
64 | 0 | report_fatal_error("Instruction selection failed"); |
65 | 16.2k | LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n'); |
66 | 16.2k | ++NumFunctionsReset; |
67 | 16.2k | MF.reset(); |
68 | 16.2k | if (EmitFallbackDiag) { |
69 | 16.1k | const Function &F = MF.getFunction(); |
70 | 16.1k | DiagnosticInfoISelFallback DiagFallback(F); |
71 | 16.1k | F.getContext().diagnose(DiagFallback); |
72 | 16.1k | } |
73 | 16.2k | return true; |
74 | 16.2k | } |
75 | 220k | return false; |
76 | 220k | } |
77 | | |
78 | | }; |
79 | | } // end anonymous namespace |
80 | | |
81 | | char ResetMachineFunction::ID = 0; |
82 | | INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, |
83 | | "Reset machine function if ISel failed", false, false) |
84 | | |
85 | | MachineFunctionPass * |
86 | | llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false, |
87 | 7.00k | bool AbortOnFailedISel = false) { |
88 | 7.00k | return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel); |
89 | 7.00k | } |