summaryrefslogtreecommitdiff
path: root/lib/Target/Hexagon/HexagonGatherPacketize.cpp
blob: 253f09d12839d1c707c7445c0639a2733445e110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//===- HexagonGatherPacketize.cpp -----------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// This pass ensures that producer and consumer of VTMP are paired in a bundle.
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "gather-packetize"

#include "HexagonTargetMachine.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBundle.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
using namespace llvm;

cl::opt<bool> EnableGatherPacketize(
    "hexagon-enable-gather-packetize", cl::Hidden, cl::init(true),
    cl::desc("Generate gather packets before packetization"));

namespace llvm {
FunctionPass *createHexagonGatherPacketize();
void initializeHexagonGatherPacketizePass(PassRegistry &);
}

namespace {
class HexagonGatherPacketize : public MachineFunctionPass {
public:
  static char ID;
  HexagonGatherPacketize() : MachineFunctionPass(ID) {
    PassRegistry &Registry = *PassRegistry::getPassRegistry();
    initializeHexagonGatherPacketizePass(Registry);
  }

  StringRef getPassName() const override {
    return "Hexagon Gather Packetize Code";
  }
  bool runOnMachineFunction(MachineFunction &Fn) override;
};

char HexagonGatherPacketize::ID = 0;

static inline bool isVtmpDef(const MachineInstr &MI) {
  for (const MachineOperand &MO : MI.operands())
    if (MO.isReg() && MO.isDef() && MO.isImplicit() &&
        (MO.getReg() == Hexagon::VTMP)) {
      return true;
    }
  return false;
}

static inline bool isVtmpUse(const MachineInstr &MI) {
  return (MI.mayStore() && (MI.getOperand(2)).isReg() &&
          ((MI.getOperand(2)).getReg() == Hexagon::VTMP));
}

bool HexagonGatherPacketize::runOnMachineFunction(MachineFunction &Fn) {
  if (!EnableGatherPacketize)
    return false;
  auto &ST = Fn.getSubtarget<HexagonSubtarget>();
  bool HasV65 = ST.hasV65TOps();
  bool UseHVX = ST.useHVXOps();
  if (!(HasV65 & UseHVX))
    return false;

  for (auto &MBB : Fn) {
    bool VtmpDef = false;
    MachineBasicBlock::iterator MII, MIE, DefMII;
    for (MII = MBB.begin(), MIE = MBB.end(); MII != MIE; ++MII) {
      MachineInstr &MI = *MII;
      if (VtmpDef) {
        if (!isVtmpUse(MI))
          continue;
        MBB.splice(std::next(DefMII), &MBB, MII);
        finalizeBundle(MBB, DefMII.getInstrIterator(),
                       std::next(MII).getInstrIterator());
        VtmpDef = false;
        continue;
      }
      if (!(isVtmpDef(MI)))
        continue;
      VtmpDef = true;
      DefMII = MII;
    }
    assert(!VtmpDef && "VTMP producer and consumer not in same block");
  }
  return true;
}
}

//===----------------------------------------------------------------------===//
//                         Public Constructor Functions
//===----------------------------------------------------------------------===//

INITIALIZE_PASS(HexagonGatherPacketize, "hexagon-gather-packetize",
                "Hexagon gather packetize Code", false, false)

FunctionPass *llvm::createHexagonGatherPacketize() {
  return new HexagonGatherPacketize();
}