1 //===-- ARMOptimizeBarriersPass - two DMBs without a memory access in between,
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===------------------------------------------------------------------------------------------===//
12 #include "ARMInstrInfo.h"
13 #include "ARMMachineFunctionInfo.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #define DEBUG_TYPE "double barriers"
20 STATISTIC(NumDMBsRemoved, "Number of DMBs removed");
23 class ARMOptimizeBarriersPass : public MachineFunctionPass {
26 ARMOptimizeBarriersPass() : MachineFunctionPass(ID) {}
28 bool runOnMachineFunction(MachineFunction &Fn) override;
30 MachineFunctionProperties getRequiredProperties() const override {
31 return MachineFunctionProperties().set(
32 MachineFunctionProperties::Property::NoVRegs);
35 StringRef getPassName() const override { return "optimise barriers pass"; }
37 char ARMOptimizeBarriersPass::ID = 0;
40 // Returns whether the instruction can safely move past a DMB instruction
41 // The current implementation allows this iif MI does not have any possible
43 static bool CanMovePastDMB(const MachineInstr *MI) {
44 return !(MI->mayLoad() ||
46 MI->hasUnmodeledSideEffects() ||
51 bool ARMOptimizeBarriersPass::runOnMachineFunction(MachineFunction &MF) {
52 if (skipFunction(*MF.getFunction()))
55 // Vector to store the DMBs we will remove after the first iteration
56 std::vector<MachineInstr *> ToRemove;
57 // DMBType is the Imm value of the first operand. It determines whether it's a
58 // DMB ish, dmb sy, dmb osh, etc
61 // Find a dmb. If we can move it until the next dmb, tag the second one for
63 for (auto &MBB : MF) {
64 // Will be true when we have seen a DMB, and not seen any instruction since
65 // that cannot move past a DMB
66 bool IsRemovableNextDMB = false;
67 for (auto &MI : MBB) {
68 if (MI.getOpcode() == ARM::DMB) {
69 if (IsRemovableNextDMB) {
70 // If the Imm of this DMB is the same as that of the last DMB, we can
71 // tag this second DMB for removal
72 if (MI.getOperand(0).getImm() == DMBType) {
73 ToRemove.push_back(&MI);
75 // If it has a different DMBType, we cannot remove it, but will scan
76 // for the next DMB, recording this DMB's type as last seen DMB type
77 DMBType = MI.getOperand(0).getImm();
80 // After we see a DMB, a next one is removable
81 IsRemovableNextDMB = true;
82 DMBType = MI.getOperand(0).getImm();
84 } else if (!CanMovePastDMB(&MI)) {
85 // If we find an instruction unable to pass past a DMB, a next DMB is
87 IsRemovableNextDMB = false;
91 // Remove the tagged DMB
92 for (auto MI : ToRemove) {
93 MI->eraseFromParent();
97 return NumDMBsRemoved > 0;
100 /// createARMOptimizeBarriersPass - Returns an instance of the remove double
103 FunctionPass *llvm::createARMOptimizeBarriersPass() {
104 return new ARMOptimizeBarriersPass();