blob: 7e4d598a6e0beb80a2ee400db92be077ee838665 [file] [log] [blame]
Renato Golind93295e2014-04-02 09:03:43 +00001//===-- 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 Golind93295e2014-04-02 09:03:43 +000011#include "ARM.h"
Renato Golind93295e2014-04-02 09:03:43 +000012#include "ARMInstrInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000013#include "ARMMachineFunctionInfo.h"
Renato Golind93295e2014-04-02 09:03:43 +000014#include "llvm/ADT/Statistic.h"
15#include "llvm/CodeGen/MachineFunctionPass.h"
16using namespace llvm;
17
Chandler Carruth84e68b22014-04-22 02:41:26 +000018#define DEBUG_TYPE "double barriers"
19
Renato Golind93295e2014-04-02 09:03:43 +000020STATISTIC(NumDMBsRemoved, "Number of DMBs removed");
21
22namespace {
23class ARMOptimizeBarriersPass : public MachineFunctionPass {
24public:
25 static char ID;
26 ARMOptimizeBarriersPass() : MachineFunctionPass(ID) {}
27
Craig Topper9d74a5a2014-04-29 07:58:41 +000028 bool runOnMachineFunction(MachineFunction &Fn) override;
Renato Golind93295e2014-04-02 09:03:43 +000029
Derek Schuff1dbf7a52016-04-04 17:09:25 +000030 MachineFunctionProperties getRequiredProperties() const override {
31 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000032 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000033 }
34
Mehdi Amini117296c2016-10-01 02:56:57 +000035 StringRef getPassName() const override { return "optimise barriers pass"; }
Renato Golind93295e2014-04-02 09:03:43 +000036};
37char ARMOptimizeBarriersPass::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000038}
Renato Golind93295e2014-04-02 09:03:43 +000039
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
42// memory access
43static bool CanMovePastDMB(const MachineInstr *MI) {
44 return !(MI->mayLoad() ||
45 MI->mayStore() ||
46 MI->hasUnmodeledSideEffects() ||
47 MI->isCall() ||
48 MI->isReturn());
49}
50
51bool ARMOptimizeBarriersPass::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylora2b91112016-04-25 22:01:04 +000052 if (skipFunction(*MF.getFunction()))
53 return false;
54
Renato Golind93295e2014-04-02 09:03:43 +000055 // 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
59 int64_t DMBType = -1;
60
61 // Find a dmb. If we can move it until the next dmb, tag the second one for
62 // removal
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);
74 } else {
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();
78 }
79 } else {
80 // After we see a DMB, a next one is removable
81 IsRemovableNextDMB = true;
82 DMBType = MI.getOperand(0).getImm();
83 }
84 } else if (!CanMovePastDMB(&MI)) {
85 // If we find an instruction unable to pass past a DMB, a next DMB is
86 // not removable
87 IsRemovableNextDMB = false;
88 }
89 }
90 }
Craig Topper8297e522017-05-08 18:02:51 +000091 bool Changed = false;
Renato Golind93295e2014-04-02 09:03:43 +000092 // Remove the tagged DMB
93 for (auto MI : ToRemove) {
94 MI->eraseFromParent();
95 ++NumDMBsRemoved;
Craig Topper8297e522017-05-08 18:02:51 +000096 Changed = true;
Renato Golind93295e2014-04-02 09:03:43 +000097 }
98
Craig Topper8297e522017-05-08 18:02:51 +000099 return Changed;
Renato Golind93295e2014-04-02 09:03:43 +0000100}
101
102/// createARMOptimizeBarriersPass - Returns an instance of the remove double
103/// barriers
104/// pass.
105FunctionPass *llvm::createARMOptimizeBarriersPass() {
106 return new ARMOptimizeBarriersPass();
107}