1 //===-- ARMOptimizeBarriersPass - two DMBs without a memory access in between,
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===------------------------------------------------------------------------------------------===//
11 #include "ARMInstrInfo.h"
12 #include "llvm/ADT/Statistic.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #define DEBUG_TYPE "double barriers"
18 STATISTIC(NumDMBsRemoved
, "Number of DMBs removed");
21 class ARMOptimizeBarriersPass
: public MachineFunctionPass
{
24 ARMOptimizeBarriersPass() : MachineFunctionPass(ID
) {}
26 bool runOnMachineFunction(MachineFunction
&Fn
) override
;
28 MachineFunctionProperties
getRequiredProperties() const override
{
29 return MachineFunctionProperties().set(
30 MachineFunctionProperties::Property::NoVRegs
);
33 StringRef
getPassName() const override
{ return "optimise barriers pass"; }
35 char ARMOptimizeBarriersPass::ID
= 0;
38 // Returns whether the instruction can safely move past a DMB instruction
39 // The current implementation allows this iif MI does not have any possible
41 static bool CanMovePastDMB(const MachineInstr
*MI
) {
42 return !(MI
->mayLoad() ||
44 MI
->hasUnmodeledSideEffects() ||
49 bool ARMOptimizeBarriersPass::runOnMachineFunction(MachineFunction
&MF
) {
50 if (skipFunction(MF
.getFunction()))
53 // Vector to store the DMBs we will remove after the first iteration
54 std::vector
<MachineInstr
*> ToRemove
;
55 // DMBType is the Imm value of the first operand. It determines whether it's a
56 // DMB ish, dmb sy, dmb osh, etc
59 // Find a dmb. If we can move it until the next dmb, tag the second one for
61 for (auto &MBB
: MF
) {
62 // Will be true when we have seen a DMB, and not seen any instruction since
63 // that cannot move past a DMB
64 bool IsRemovableNextDMB
= false;
65 for (auto &MI
: MBB
) {
66 if (MI
.getOpcode() == ARM::DMB
) {
67 if (IsRemovableNextDMB
) {
68 // If the Imm of this DMB is the same as that of the last DMB, we can
69 // tag this second DMB for removal
70 if (MI
.getOperand(0).getImm() == DMBType
) {
71 ToRemove
.push_back(&MI
);
73 // If it has a different DMBType, we cannot remove it, but will scan
74 // for the next DMB, recording this DMB's type as last seen DMB type
75 DMBType
= MI
.getOperand(0).getImm();
78 // After we see a DMB, a next one is removable
79 IsRemovableNextDMB
= true;
80 DMBType
= MI
.getOperand(0).getImm();
82 } else if (!CanMovePastDMB(&MI
)) {
83 // If we find an instruction unable to pass past a DMB, a next DMB is
85 IsRemovableNextDMB
= false;
90 // Remove the tagged DMB
91 for (auto *MI
: ToRemove
) {
92 MI
->eraseFromParent();
100 /// createARMOptimizeBarriersPass - Returns an instance of the remove double
103 FunctionPass
*llvm::createARMOptimizeBarriersPass() {
104 return new ARMOptimizeBarriersPass();