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 | |
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" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/Passes.h" |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DiagnosticInfo.h" |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
| 22 | using namespace llvm; |
| 23 | |
| 24 | #define DEBUG_TYPE "reset-machine-function" |
| 25 | |
Quentin Colombet | 0f4c20a | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 26 | STATISTIC(NumFunctionsReset, "Number of functions reset"); |
| 27 | |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | class ResetMachineFunction : public MachineFunctionPass { |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 30 | /// Tells whether or not this pass should emit a fallback |
| 31 | /// diagnostic when it resets a function. |
| 32 | bool EmitFallbackDiag; |
Justin Bogner | 1a314da | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 33 | /// Whether we should abort immediately instead of resetting the function. |
| 34 | bool AbortOnFailedISel; |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 35 | |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 36 | public: |
| 37 | static char ID; // Pass identification, replacement for typeid |
Justin Bogner | 1a314da | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 38 | ResetMachineFunction(bool EmitFallbackDiag = false, |
| 39 | bool AbortOnFailedISel = false) |
| 40 | : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag), |
| 41 | AbortOnFailedISel(AbortOnFailedISel) {} |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 42 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 43 | StringRef getPassName() const override { return "ResetMachineFunction"; } |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 44 | |
| 45 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 46 | if (MF.getProperties().hasProperty( |
| 47 | MachineFunctionProperties::Property::FailedISel)) { |
Justin Bogner | 1a314da | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 48 | if (AbortOnFailedISel) |
| 49 | report_fatal_error("Instruction selection failed"); |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 50 | DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n'); |
Quentin Colombet | 0f4c20a | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 51 | ++NumFunctionsReset; |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 52 | MF.reset(); |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 53 | if (EmitFallbackDiag) { |
| 54 | const Function &F = *MF.getFunction(); |
| 55 | DiagnosticInfoISelFallback DiagFallback(F); |
| 56 | F.getContext().diagnose(DiagFallback); |
| 57 | } |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | }; |
| 64 | } // end anonymous namespace |
| 65 | |
| 66 | char ResetMachineFunction::ID = 0; |
| 67 | INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, |
| 68 | "reset machine function if ISel failed", false, false) |
| 69 | |
| 70 | MachineFunctionPass * |
Justin Bogner | 1a314da | 2017-01-13 23:46:11 +0000 | [diff] [blame] | 71 | llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false, |
| 72 | bool AbortOnFailedISel = false) { |
| 73 | return new ResetMachineFunction(EmitFallbackDiag, AbortOnFailedISel); |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 74 | } |