Quentin Colombet | 380cd88 | 2016-09-23 18:38:15 +0000 | [diff] [blame] | 1 | //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==// |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Quentin Colombet | 380cd88 | 2016-09-23 18:38:15 +0000 | [diff] [blame] | 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. |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Roman Tereshin | 3054ece | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ScopeExit.h" |
Quentin Colombet | 0f4c20a | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Roman Tereshin | 3054ece | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/Passes.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 21 | #include "llvm/CodeGen/StackProtector.h" |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DiagnosticInfo.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 23 | #include "llvm/InitializePasses.h" |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | #define DEBUG_TYPE "reset-machine-function" |
| 28 | |
Quentin Colombet | 0f4c20a | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 29 | STATISTIC(NumFunctionsReset, "Number of functions reset"); |
Craig Topper | 66df736 | 2019-03-14 01:13:15 +0000 | [diff] [blame] | 30 | STATISTIC(NumFunctionsVisited, "Number of functions visited"); |
Quentin Colombet | 0f4c20a | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 31 | |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | class ResetMachineFunction : public MachineFunctionPass { |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 34 | /// Tells whether or not this pass should emit a fallback |
| 35 | /// diagnostic when it resets a function. |
| 36 | bool EmitFallbackDiag; |
Justin Bogner | 1a314da | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 37 | /// Whether we should abort immediately instead of resetting the function. |
| 38 | bool AbortOnFailedISel; |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 39 | |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 40 | public: |
| 41 | static char ID; // Pass identification, replacement for typeid |
Justin Bogner | 1a314da | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 42 | ResetMachineFunction(bool EmitFallbackDiag = false, |
| 43 | bool AbortOnFailedISel = false) |
| 44 | : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag), |
| 45 | AbortOnFailedISel(AbortOnFailedISel) {} |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 46 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 47 | StringRef getPassName() const override { return "ResetMachineFunction"; } |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 48 | |
Matthias Braun | 90ad683 | 2018-07-13 00:08:38 +0000 | [diff] [blame] | 49 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 50 | AU.addPreserved<StackProtector>(); |
| 51 | MachineFunctionPass::getAnalysisUsage(AU); |
| 52 | } |
| 53 | |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 54 | bool runOnMachineFunction(MachineFunction &MF) override { |
Craig Topper | 66df736 | 2019-03-14 01:13:15 +0000 | [diff] [blame] | 55 | ++NumFunctionsVisited; |
Roman Tereshin | 3054ece | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 56 | // No matter what happened, whether we successfully selected the function |
| 57 | // or not, nothing is going to use the vreg types after us. Make sure they |
| 58 | // disappear. |
| 59 | auto ClearVRegTypesOnReturn = |
Roman Tereshin | 13229af | 2018-05-23 21:12:02 +0000 | [diff] [blame] | 60 | make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); }); |
Roman Tereshin | 3054ece | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 61 | |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 62 | if (MF.getProperties().hasProperty( |
| 63 | MachineFunctionProperties::Property::FailedISel)) { |
Justin Bogner | 1a314da | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 64 | if (AbortOnFailedISel) |
| 65 | report_fatal_error("Instruction selection failed"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 66 | LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n'); |
Quentin Colombet | 0f4c20a | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 67 | ++NumFunctionsReset; |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 68 | MF.reset(); |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 69 | if (EmitFallbackDiag) { |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 70 | const Function &F = MF.getFunction(); |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 71 | DiagnosticInfoISelFallback DiagFallback(F); |
| 72 | F.getContext().diagnose(DiagFallback); |
| 73 | } |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | }; |
| 80 | } // end anonymous namespace |
| 81 | |
| 82 | char ResetMachineFunction::ID = 0; |
| 83 | INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, |
Amara Emerson | 98af466 | 2018-02-02 01:49:59 +0000 | [diff] [blame] | 84 | "Reset machine function if ISel failed", false, false) |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 85 | |
| 86 | MachineFunctionPass * |
Justin Bogner | 1a314da | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 87 | llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false, |
| 88 | bool AbortOnFailedISel = false) { |
| 89 | return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel); |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 90 | } |