Quentin Colombet | f8e3a51 | 2016-09-23 18:38:15 +0000 | [diff] [blame] | 1 | //===-- ResetMachineFunctionPass.cpp - Reset Machine Function ----*- C++ -*-==// |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Quentin Colombet | f8e3a51 | 2016-09-23 18:38:15 +0000 | [diff] [blame] | 9 | /// \file |
| 10 | /// This file implements a pass that will conditionally reset a machine |
| 11 | /// function as if it was just created. This is used to provide a fallback |
| 12 | /// mechanism when GlobalISel fails, thus the condition for the reset to |
| 13 | /// happen is that the MachineFunction has the FailedISel property. |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Roman Tereshin | 8e63a83 | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ScopeExit.h" |
Quentin Colombet | 76cc1a9 | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Roman Tereshin | 8e63a83 | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Matthias Braun | d79789a | 2018-07-13 00:08:38 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/StackProtector.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/Passes.h" |
Quentin Colombet | 8e21d40 | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DiagnosticInfo.h" |
Quentin Colombet | c47e5db | 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 | 76cc1a9 | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 29 | STATISTIC(NumFunctionsReset, "Number of functions reset"); |
| 30 | |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | class ResetMachineFunction : public MachineFunctionPass { |
Quentin Colombet | 8e21d40 | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 33 | /// Tells whether or not this pass should emit a fallback |
| 34 | /// diagnostic when it resets a function. |
| 35 | bool EmitFallbackDiag; |
Justin Bogner | 669e297 | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 36 | /// Whether we should abort immediately instead of resetting the function. |
| 37 | bool AbortOnFailedISel; |
Quentin Colombet | 8e21d40 | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 38 | |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 39 | public: |
| 40 | static char ID; // Pass identification, replacement for typeid |
Justin Bogner | 669e297 | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 41 | ResetMachineFunction(bool EmitFallbackDiag = false, |
| 42 | bool AbortOnFailedISel = false) |
| 43 | : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag), |
| 44 | AbortOnFailedISel(AbortOnFailedISel) {} |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 45 | |
Mehdi Amini | 67f335d | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 46 | StringRef getPassName() const override { return "ResetMachineFunction"; } |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 47 | |
Matthias Braun | d79789a | 2018-07-13 00:08:38 +0000 | [diff] [blame] | 48 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 49 | AU.addPreserved<StackProtector>(); |
| 50 | MachineFunctionPass::getAnalysisUsage(AU); |
| 51 | } |
| 52 | |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 53 | bool runOnMachineFunction(MachineFunction &MF) override { |
Roman Tereshin | 8e63a83 | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 54 | // No matter what happened, whether we successfully selected the function |
| 55 | // or not, nothing is going to use the vreg types after us. Make sure they |
| 56 | // disappear. |
| 57 | auto ClearVRegTypesOnReturn = |
Roman Tereshin | cb64cbc | 2018-05-23 21:12:02 +0000 | [diff] [blame] | 58 | make_scope_exit([&MF]() { MF.getRegInfo().clearVirtRegTypes(); }); |
Roman Tereshin | 8e63a83 | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 59 | |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 60 | if (MF.getProperties().hasProperty( |
| 61 | MachineFunctionProperties::Property::FailedISel)) { |
Justin Bogner | 669e297 | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 62 | if (AbortOnFailedISel) |
| 63 | report_fatal_error("Instruction selection failed"); |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 64 | LLVM_DEBUG(dbgs() << "Resetting: " << MF.getName() << '\n'); |
Quentin Colombet | 76cc1a9 | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 65 | ++NumFunctionsReset; |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 66 | MF.reset(); |
Quentin Colombet | 8e21d40 | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 67 | if (EmitFallbackDiag) { |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 68 | const Function &F = MF.getFunction(); |
Quentin Colombet | 8e21d40 | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 69 | DiagnosticInfoISelFallback DiagFallback(F); |
| 70 | F.getContext().diagnose(DiagFallback); |
| 71 | } |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 72 | return true; |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | }; |
| 78 | } // end anonymous namespace |
| 79 | |
| 80 | char ResetMachineFunction::ID = 0; |
| 81 | INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, |
Amara Emerson | f2316f6 | 2018-02-02 01:49:59 +0000 | [diff] [blame] | 82 | "Reset machine function if ISel failed", false, false) |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 83 | |
| 84 | MachineFunctionPass * |
Justin Bogner | 669e297 | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 85 | llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false, |
| 86 | bool AbortOnFailedISel = false) { |
| 87 | return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel); |
Quentin Colombet | c47e5db | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 88 | } |