LLVM 20.0.0git
ARMOptimizeBarriersPass.cpp
Go to the documentation of this file.
1//===-- ARMOptimizeBarriersPass - two DMBs without a memory access in between,
2//removed one -===//
3//
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
7//
8//===------------------------------------------------------------------------------------------===//
9
10#include "ARM.h"
11#include "ARMInstrInfo.h"
12#include "llvm/ADT/Statistic.h"
14using namespace llvm;
15
16#define DEBUG_TYPE "double barriers"
17
18STATISTIC(NumDMBsRemoved, "Number of DMBs removed");
19
20namespace {
21class ARMOptimizeBarriersPass : public MachineFunctionPass {
22public:
23 static char ID;
24 ARMOptimizeBarriersPass() : MachineFunctionPass(ID) {}
25
26 bool runOnMachineFunction(MachineFunction &Fn) override;
27
30 MachineFunctionProperties::Property::NoVRegs);
31 }
32
33 StringRef getPassName() const override { return "optimise barriers pass"; }
34};
35char ARMOptimizeBarriersPass::ID = 0;
36}
37
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
40// memory access
41static bool CanMovePastDMB(const MachineInstr *MI) {
42 return !(MI->mayLoad() ||
43 MI->mayStore() ||
44 MI->hasUnmodeledSideEffects() ||
45 MI->isCall() ||
46 MI->isReturn());
47}
48
49bool ARMOptimizeBarriersPass::runOnMachineFunction(MachineFunction &MF) {
50 if (skipFunction(MF.getFunction()))
51 return false;
52
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
57 int64_t DMBType = -1;
58
59 // Find a dmb. If we can move it until the next dmb, tag the second one for
60 // removal
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);
72 } else {
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();
76 }
77 } else {
78 // After we see a DMB, a next one is removable
79 IsRemovableNextDMB = true;
80 DMBType = MI.getOperand(0).getImm();
81 }
82 } else if (!CanMovePastDMB(&MI)) {
83 // If we find an instruction unable to pass past a DMB, a next DMB is
84 // not removable
85 IsRemovableNextDMB = false;
86 }
87 }
88 }
89 bool Changed = false;
90 // Remove the tagged DMB
91 for (auto *MI : ToRemove) {
92 MI->eraseFromParent();
93 ++NumDMBsRemoved;
94 Changed = true;
95 }
96
97 return Changed;
98}
99
100/// createARMOptimizeBarriersPass - Returns an instance of the remove double
101/// barriers
102/// pass.
104 return new ARMOptimizeBarriersPass();
105}
ReachingDefAnalysis InstSet & ToRemove
static bool CanMovePastDMB(const MachineInstr *MI)
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
Definition: MachineInstr.h:69
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createARMOptimizeBarriersPass()
createARMOptimizeBarriersPass - Returns an instance of the remove double barriers pass.