Renato Golin | d93295e | 2014-04-02 09:03:43 +0000 | [diff] [blame] | 1 | //===-- ARMOptimizeBarriersPass - two DMBs without a memory access in between, |
| 2 | //removed one -===// |
| 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===------------------------------------------------------------------------------------------===// |
| 10 | |
Renato Golin | d93295e | 2014-04-02 09:03:43 +0000 | [diff] [blame] | 11 | #include "ARM.h" |
Renato Golin | d93295e | 2014-04-02 09:03:43 +0000 | [diff] [blame] | 12 | #include "ARMInstrInfo.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 13 | #include "ARMMachineFunctionInfo.h" |
Renato Golin | d93295e | 2014-04-02 09:03:43 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Statistic.h" |
| 15 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 16 | using namespace llvm; |
| 17 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "double barriers" |
| 19 | |
Renato Golin | d93295e | 2014-04-02 09:03:43 +0000 | [diff] [blame] | 20 | STATISTIC(NumDMBsRemoved, "Number of DMBs removed"); |
| 21 | |
| 22 | namespace { |
| 23 | class ARMOptimizeBarriersPass : public MachineFunctionPass { |
| 24 | public: |
| 25 | static char ID; |
| 26 | ARMOptimizeBarriersPass() : MachineFunctionPass(ID) {} |
| 27 | |
Craig Topper | 9d74a5a | 2014-04-29 07:58:41 +0000 | [diff] [blame] | 28 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Renato Golin | d93295e | 2014-04-02 09:03:43 +0000 | [diff] [blame] | 29 | |
Craig Topper | 9d74a5a | 2014-04-29 07:58:41 +0000 | [diff] [blame] | 30 | const char *getPassName() const override { |
Renato Golin | d93295e | 2014-04-02 09:03:43 +0000 | [diff] [blame] | 31 | return "optimise barriers pass"; |
| 32 | } |
Renato Golin | d93295e | 2014-04-02 09:03:43 +0000 | [diff] [blame] | 33 | }; |
| 34 | char ARMOptimizeBarriersPass::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 35 | } |
Renato Golin | d93295e | 2014-04-02 09:03:43 +0000 | [diff] [blame] | 36 | |
| 37 | // Returns whether the instruction can safely move past a DMB instruction |
| 38 | // The current implementation allows this iif MI does not have any possible |
| 39 | // memory access |
| 40 | static bool CanMovePastDMB(const MachineInstr *MI) { |
| 41 | return !(MI->mayLoad() || |
| 42 | MI->mayStore() || |
| 43 | MI->hasUnmodeledSideEffects() || |
| 44 | MI->isCall() || |
| 45 | MI->isReturn()); |
| 46 | } |
| 47 | |
| 48 | bool ARMOptimizeBarriersPass::runOnMachineFunction(MachineFunction &MF) { |
| 49 | // Vector to store the DMBs we will remove after the first iteration |
| 50 | std::vector<MachineInstr *> ToRemove; |
| 51 | // DMBType is the Imm value of the first operand. It determines whether it's a |
| 52 | // DMB ish, dmb sy, dmb osh, etc |
| 53 | int64_t DMBType = -1; |
| 54 | |
| 55 | // Find a dmb. If we can move it until the next dmb, tag the second one for |
| 56 | // removal |
| 57 | for (auto &MBB : MF) { |
| 58 | // Will be true when we have seen a DMB, and not seen any instruction since |
| 59 | // that cannot move past a DMB |
| 60 | bool IsRemovableNextDMB = false; |
| 61 | for (auto &MI : MBB) { |
| 62 | if (MI.getOpcode() == ARM::DMB) { |
| 63 | if (IsRemovableNextDMB) { |
| 64 | // If the Imm of this DMB is the same as that of the last DMB, we can |
| 65 | // tag this second DMB for removal |
| 66 | if (MI.getOperand(0).getImm() == DMBType) { |
| 67 | ToRemove.push_back(&MI); |
| 68 | } else { |
| 69 | // If it has a different DMBType, we cannot remove it, but will scan |
| 70 | // for the next DMB, recording this DMB's type as last seen DMB type |
| 71 | DMBType = MI.getOperand(0).getImm(); |
| 72 | } |
| 73 | } else { |
| 74 | // After we see a DMB, a next one is removable |
| 75 | IsRemovableNextDMB = true; |
| 76 | DMBType = MI.getOperand(0).getImm(); |
| 77 | } |
| 78 | } else if (!CanMovePastDMB(&MI)) { |
| 79 | // If we find an instruction unable to pass past a DMB, a next DMB is |
| 80 | // not removable |
| 81 | IsRemovableNextDMB = false; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | // Remove the tagged DMB |
| 86 | for (auto MI : ToRemove) { |
| 87 | MI->eraseFromParent(); |
| 88 | ++NumDMBsRemoved; |
| 89 | } |
| 90 | |
| 91 | return NumDMBsRemoved > 0; |
| 92 | } |
| 93 | |
| 94 | /// createARMOptimizeBarriersPass - Returns an instance of the remove double |
| 95 | /// barriers |
| 96 | /// pass. |
| 97 | FunctionPass *llvm::createARMOptimizeBarriersPass() { |
| 98 | return new ARMOptimizeBarriersPass(); |
| 99 | } |