blob: 348895da713f65c70d2149eff3fbe4cda0355496 [file] [log] [blame]
Renato Golind93295e2014-04-02 09:03:43 +00001//===-- ARMOptimizeBarriersPass - two DMBs without a memory access in between,
2//removed one -===//
3//
Chandler Carruth2946cd72019-01-19 08:50:56 +00004// 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
Renato Golind93295e2014-04-02 09:03:43 +00007//
8//===------------------------------------------------------------------------------------------===//
9
Renato Golind93295e2014-04-02 09:03:43 +000010#include "ARM.h"
Renato Golind93295e2014-04-02 09:03:43 +000011#include "ARMInstrInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000012#include "ARMMachineFunctionInfo.h"
Renato Golind93295e2014-04-02 09:03:43 +000013#include "llvm/ADT/Statistic.h"
14#include "llvm/CodeGen/MachineFunctionPass.h"
15using namespace llvm;
16
Chandler Carruth84e68b22014-04-22 02:41:26 +000017#define DEBUG_TYPE "double barriers"
18
Renato Golind93295e2014-04-02 09:03:43 +000019STATISTIC(NumDMBsRemoved, "Number of DMBs removed");
20
21namespace {
22class ARMOptimizeBarriersPass : public MachineFunctionPass {
23public:
24 static char ID;
25 ARMOptimizeBarriersPass() : MachineFunctionPass(ID) {}
26
Craig Topper9d74a5a2014-04-29 07:58:41 +000027 bool runOnMachineFunction(MachineFunction &Fn) override;
Renato Golind93295e2014-04-02 09:03:43 +000028
Derek Schuff1dbf7a52016-04-04 17:09:25 +000029 MachineFunctionProperties getRequiredProperties() const override {
30 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000031 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000032 }
33
Mehdi Amini117296c2016-10-01 02:56:57 +000034 StringRef getPassName() const override { return "optimise barriers pass"; }
Renato Golind93295e2014-04-02 09:03:43 +000035};
36char ARMOptimizeBarriersPass::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000037}
Renato Golind93295e2014-04-02 09:03:43 +000038
39// Returns whether the instruction can safely move past a DMB instruction
40// The current implementation allows this iif MI does not have any possible
41// memory access
42static bool CanMovePastDMB(const MachineInstr *MI) {
43 return !(MI->mayLoad() ||
44 MI->mayStore() ||
45 MI->hasUnmodeledSideEffects() ||
46 MI->isCall() ||
47 MI->isReturn());
48}
49
50bool ARMOptimizeBarriersPass::runOnMachineFunction(MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +000051 if (skipFunction(MF.getFunction()))
Andrew Kaylora2b91112016-04-25 22:01:04 +000052 return false;
53
Renato Golind93295e2014-04-02 09:03:43 +000054 // Vector to store the DMBs we will remove after the first iteration
55 std::vector<MachineInstr *> ToRemove;
56 // DMBType is the Imm value of the first operand. It determines whether it's a
57 // DMB ish, dmb sy, dmb osh, etc
58 int64_t DMBType = -1;
59
60 // Find a dmb. If we can move it until the next dmb, tag the second one for
61 // removal
62 for (auto &MBB : MF) {
63 // Will be true when we have seen a DMB, and not seen any instruction since
64 // that cannot move past a DMB
65 bool IsRemovableNextDMB = false;
66 for (auto &MI : MBB) {
67 if (MI.getOpcode() == ARM::DMB) {
68 if (IsRemovableNextDMB) {
69 // If the Imm of this DMB is the same as that of the last DMB, we can
70 // tag this second DMB for removal
71 if (MI.getOperand(0).getImm() == DMBType) {
72 ToRemove.push_back(&MI);
73 } else {
74 // If it has a different DMBType, we cannot remove it, but will scan
75 // for the next DMB, recording this DMB's type as last seen DMB type
76 DMBType = MI.getOperand(0).getImm();
77 }
78 } else {
79 // After we see a DMB, a next one is removable
80 IsRemovableNextDMB = true;
81 DMBType = MI.getOperand(0).getImm();
82 }
83 } else if (!CanMovePastDMB(&MI)) {
84 // If we find an instruction unable to pass past a DMB, a next DMB is
85 // not removable
86 IsRemovableNextDMB = false;
87 }
88 }
89 }
Craig Topper8297e522017-05-08 18:02:51 +000090 bool Changed = false;
Renato Golind93295e2014-04-02 09:03:43 +000091 // Remove the tagged DMB
92 for (auto MI : ToRemove) {
93 MI->eraseFromParent();
94 ++NumDMBsRemoved;
Craig Topper8297e522017-05-08 18:02:51 +000095 Changed = true;
Renato Golind93295e2014-04-02 09:03:43 +000096 }
97
Craig Topper8297e522017-05-08 18:02:51 +000098 return Changed;
Renato Golind93295e2014-04-02 09:03:43 +000099}
100
101/// createARMOptimizeBarriersPass - Returns an instance of the remove double
102/// barriers
103/// pass.
104FunctionPass *llvm::createARMOptimizeBarriersPass() {
105 return new ARMOptimizeBarriersPass();
106}