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/Passes.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.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; |
| 33 | |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 34 | public: |
| 35 | static char ID; // Pass identification, replacement for typeid |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 36 | ResetMachineFunction(bool EmitFallbackDiag = false) |
| 37 | : MachineFunctionPass(ID), EmitFallbackDiag(EmitFallbackDiag) {} |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 38 | |
| 39 | const char *getPassName() const override { |
| 40 | return "ResetMachineFunction"; |
| 41 | } |
| 42 | |
| 43 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 44 | if (MF.getProperties().hasProperty( |
| 45 | MachineFunctionProperties::Property::FailedISel)) { |
| 46 | DEBUG(dbgs() << "Reseting: " << MF.getName() << '\n'); |
Quentin Colombet | 0f4c20a | 2016-09-23 18:38:13 +0000 | [diff] [blame] | 47 | ++NumFunctionsReset; |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 48 | MF.reset(); |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 49 | if (EmitFallbackDiag) { |
| 50 | const Function &F = *MF.getFunction(); |
| 51 | DiagnosticInfoISelFallback DiagFallback(F); |
| 52 | F.getContext().diagnose(DiagFallback); |
| 53 | } |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 54 | return true; |
| 55 | } |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | }; |
| 60 | } // end anonymous namespace |
| 61 | |
| 62 | char ResetMachineFunction::ID = 0; |
| 63 | INITIALIZE_PASS(ResetMachineFunction, DEBUG_TYPE, |
| 64 | "reset machine function if ISel failed", false, false) |
| 65 | |
| 66 | MachineFunctionPass * |
Quentin Colombet | 612cd1f | 2016-08-31 18:43:01 +0000 | [diff] [blame] | 67 | llvm::createResetMachineFunctionPass(bool EmitFallbackDiag = false) { |
| 68 | return new ResetMachineFunction(EmitFallbackDiag); |
Quentin Colombet | 374796d | 2016-08-27 00:18:31 +0000 | [diff] [blame] | 69 | } |