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