Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/ARM/ARMOptimizeBarriersPass.cpp
Line
Count
Source
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 "ARMMachineFunctionInfo.h"
13
#include "llvm/ADT/Statistic.h"
14
#include "llvm/CodeGen/MachineFunctionPass.h"
15
using namespace llvm;
16
17
#define DEBUG_TYPE "double barriers"
18
19
STATISTIC(NumDMBsRemoved, "Number of DMBs removed");
20
21
namespace {
22
class ARMOptimizeBarriersPass : public MachineFunctionPass {
23
public:
24
  static char ID;
25
4.91k
  ARMOptimizeBarriersPass() : MachineFunctionPass(ID) {}
26
27
  bool runOnMachineFunction(MachineFunction &Fn) override;
28
29
4.89k
  MachineFunctionProperties getRequiredProperties() const override {
30
4.89k
    return MachineFunctionProperties().set(
31
4.89k
        MachineFunctionProperties::Property::NoVRegs);
32
4.89k
  }
33
34
30.1k
  StringRef getPassName() const override { return "optimise barriers pass"; }
35
};
36
char ARMOptimizeBarriersPass::ID = 0;
37
}
38
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
42
801k
static bool CanMovePastDMB(const MachineInstr *MI) {
43
801k
  return !(MI->mayLoad() ||
44
801k
          
MI->mayStore()703k
||
45
801k
          
MI->hasUnmodeledSideEffects()626k
||
46
801k
          
MI->isCall()482k
||
47
801k
          
MI->isReturn()429k
);
48
801k
}
49
50
25.2k
bool ARMOptimizeBarriersPass::runOnMachineFunction(MachineFunction &MF) {
51
25.2k
  if (skipFunction(MF.getFunction()))
52
11
    return false;
53
25.2k
54
25.2k
  // Vector to store the DMBs we will remove after the first iteration
55
25.2k
  std::vector<MachineInstr *> ToRemove;
56
25.2k
  // DMBType is the Imm value of the first operand. It determines whether it's a
57
25.2k
  // DMB ish, dmb sy, dmb osh, etc
58
25.2k
  int64_t DMBType = -1;
59
25.2k
60
25.2k
  // Find a dmb. If we can move it until the next dmb, tag the second one for
61
25.2k
  // removal
62
101k
  for (auto &MBB : MF) {
63
101k
    // Will be true when we have seen a DMB, and not seen any instruction since
64
101k
    // that cannot move past a DMB
65
101k
    bool IsRemovableNextDMB = false;
66
801k
    for (auto &MI : MBB) {
67
801k
      if (MI.getOpcode() == ARM::DMB) {
68
111
        if (IsRemovableNextDMB) {
69
6
          // If the Imm of this DMB is the same as that of the last DMB, we can
70
6
          // tag this second DMB for removal
71
6
          if (MI.getOperand(0).getImm() == DMBType) {
72
5
            ToRemove.push_back(&MI);
73
5
          } else {
74
1
            // If it has a different DMBType, we cannot remove it, but will scan
75
1
            // for the next DMB, recording this DMB's type as last seen DMB type
76
1
            DMBType = MI.getOperand(0).getImm();
77
1
          }
78
105
        } else {
79
105
          // After we see a DMB, a next one is removable
80
105
          IsRemovableNextDMB = true;
81
105
          DMBType = MI.getOperand(0).getImm();
82
105
        }
83
801k
      } else if (!CanMovePastDMB(&MI)) {
84
387k
        // If we find an instruction unable to pass past a DMB, a next DMB is
85
387k
        // not removable
86
387k
        IsRemovableNextDMB = false;
87
387k
      }
88
801k
    }
89
101k
  }
90
25.2k
  bool Changed = false;
91
25.2k
  // Remove the tagged DMB
92
25.2k
  for (auto MI : ToRemove) {
93
5
    MI->eraseFromParent();
94
5
    ++NumDMBsRemoved;
95
5
    Changed = true;
96
5
  }
97
25.2k
98
25.2k
  return Changed;
99
25.2k
}
100
101
/// createARMOptimizeBarriersPass - Returns an instance of the remove double
102
/// barriers
103
/// pass.
104
4.91k
FunctionPass *llvm::createARMOptimizeBarriersPass() {
105
4.91k
  return new ARMOptimizeBarriersPass();
106
4.91k
}