1//===-- HexagonMergeActivateWeight.cpp - Pair HMX activate/weight ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#define DEBUG_TYPE "merge-hmx-activate-weight"
10
11#include "Hexagon.h"
12#include "HexagonInstrInfo.h"
13#include "HexagonSubtarget.h"
14#include "llvm/CodeGen/LiveIntervals.h"
15#include "llvm/CodeGen/MachineBasicBlock.h"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/CodeGen/MachineInstr.h"
19#include "llvm/CodeGen/MachineOperand.h"
20#include "llvm/CodeGen/SlotIndexes.h"
21#include "llvm/CodeGen/TargetRegisterInfo.h"
22#include "llvm/IR/DiagnosticInfo.h"
23#include "llvm/IR/DiagnosticPrinter.h"
24#include "llvm/InitializePasses.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/raw_ostream.h"
27#include <iterator>
28
29using namespace llvm;
30
31namespace llvm {
32
33// A class to generate a warning message about weight and activate
34class DiagnosticInfoWeightActivate : public DiagnosticInfoResourceLimit {
35
36public:
37 DiagnosticInfoWeightActivate(const Function &Fn, unsigned S,
38 DiagnosticSeverity Severity = DS_Warning)
39 : DiagnosticInfoResourceLimit(Fn, "activate weight pair", S, Severity) {}
40
41 void print(DiagnosticPrinter &DP) const override {
42 DP << "In " << getFunction() << ", "
43 << "expected activate and weight pair";
44 }
45
46 static bool classof(const DiagnosticInfo *DI) {
47 return DI->getKind() == DK_ResourceLimit;
48 }
49};
50
51// Return true if \p Moved can be moved past every instruction in the range
52// [Begin, End), in either direction. \p Moved is an HMX activation or weight
53// instruction, which reads memory and updates the HMX state, so it must not be
54// reordered with:
55// - calls, inline asm, barriers or terminators,
56// - side-effecting instructions (e.g. mxclracc, bias/cvt mxmem forms and any
57// other HMX instruction),
58// - volatile or atomic memory accesses,
59// - stores, which may write the memory read by \p Moved,
60// - instructions that redefine a register used by \p Moved, or that read or
61// redefine a register defined by \p Moved.
62static bool canMoveAcross(const MachineInstr &Moved,
63 MachineBasicBlock::const_iterator Begin,
64 MachineBasicBlock::const_iterator End,
65 const TargetRegisterInfo *TRI) {
66 for (const MachineInstr &MI : make_range(x: Begin, y: End)) {
67 if (MI.isDebugInstr() || &MI == &Moved)
68 continue;
69
70 if (MI.isCall() || MI.isInlineAsm() || MI.isBarrier() ||
71 MI.isTerminator() || MI.hasUnmodeledSideEffects() ||
72 MI.hasOrderedMemoryRef() || MI.mayStore()) {
73 LLVM_DEBUG(dbgs() << "Cannot move " << Moved << " past " << MI);
74 return false;
75 }
76
77 for (const MachineOperand &MO : Moved.operands()) {
78 if (!MO.isReg() || !MO.getReg())
79 continue;
80 Register R = MO.getReg();
81 if (MI.modifiesRegister(Reg: R, TRI) ||
82 (MO.isDef() && MI.readsRegister(Reg: R, TRI))) {
83 LLVM_DEBUG(dbgs() << "Register " << printReg(R, TRI)
84 << " prevents moving " << Moved << " past " << MI);
85 return false;
86 }
87 }
88 }
89 return true;
90}
91
92// Move each HMX activation instruction next to its matching weight instruction
93// (or vice versa), so that the packetizer can put them in the same packet, as
94// required by the hardware. Warn about activation or weight instructions that
95// cannot be paired.
96static bool runActivateWeight(MachineFunction &MF, LiveIntervals *LIS) {
97 auto &HST = MF.getSubtarget<HexagonSubtarget>();
98 const HexagonInstrInfo *HII = HST.getInstrInfo();
99 const TargetRegisterInfo *TRI = HST.getRegisterInfo();
100 const Function &F = MF.getFunction();
101 bool Changed = false;
102
103 auto Warn = [&]() {
104 DiagnosticInfoWeightActivate WeightActivate(F, 1);
105 F.getContext().diagnose(DI: WeightActivate);
106 };
107
108 for (MachineBasicBlock &MBB : MF) {
109 // The first instruction of a pair, waiting for its partner.
110 MachineInstr *Pending = nullptr;
111
112 for (MachineInstr &MI : make_early_inc_range(Range&: MBB)) {
113 bool IsActivate = HII->isHMXActivateInstr(MI);
114 if (!IsActivate && !HII->isHMXWeightInstr(MI))
115 continue;
116 if (!Pending) {
117 Pending = &MI;
118 continue;
119 }
120 if (IsActivate == HII->isHMXActivateInstr(MI: *Pending)) {
121 // Two activations or two weights in a row.
122 Warn();
123 Pending = &MI;
124 continue;
125 }
126
127 // Pending and MI form a pair. Sink the first one down to the second,
128 // or, if that is not possible, hoist the second one up to the first.
129 MachineBasicBlock::iterator Between =
130 std::next(x: MachineBasicBlock::iterator(Pending));
131 MachineBasicBlock::iterator Second(MI);
132 if (canMoveAcross(Moved: *Pending, Begin: Between, End: Second, TRI)) {
133 if (Between != Second) {
134 MBB.splice(Where: Second, Other: &MBB, From: Pending);
135 if (LIS)
136 LIS->handleMove(MI&: *Pending, /*UpdateFlags=*/true);
137 Changed = true;
138 }
139 } else if (canMoveAcross(Moved: MI, Begin: Between, End: Second, TRI)) {
140 MBB.splice(Where: Between, Other: &MBB, From: &MI);
141 if (LIS)
142 LIS->handleMove(MI, /*UpdateFlags=*/true);
143 Changed = true;
144 } else {
145 Warn();
146 }
147 Pending = nullptr;
148 }
149
150 if (Pending)
151 Warn();
152 }
153
154 return Changed;
155}
156
157class HexagonMergeActivateWeight : public MachineFunctionPass {
158public:
159 static char ID;
160 HexagonMergeActivateWeight() : MachineFunctionPass(ID) {
161 PassRegistry &Registry = *PassRegistry::getPassRegistry();
162 initializeHexagonMergeActivateWeightPass(Registry);
163 }
164
165 StringRef getPassName() const override {
166 return "Hexagon Merge Activate Weight Instructions After Machine Scheduler";
167 }
168 bool runOnMachineFunction(MachineFunction &MF) override {
169 return runActivateWeight(MF,
170 LIS: &getAnalysis<LiveIntervalsWrapperPass>().getLIS());
171 }
172
173 void getAnalysisUsage(AnalysisUsage &AU) const override {
174 AU.setPreservesCFG();
175 AU.addPreserved<SlotIndexesWrapperPass>();
176 AU.addRequired<LiveIntervalsWrapperPass>();
177 AU.addPreserved<LiveIntervalsWrapperPass>();
178 MachineFunctionPass::getAnalysisUsage(AU);
179 }
180};
181
182char HexagonMergeActivateWeight::ID = 0;
183char &HexagonMergeActivateWeightID = HexagonMergeActivateWeight::ID;
184
185class HexagonMergeActivateWeight2 : public MachineFunctionPass {
186public:
187 static char ID;
188 HexagonMergeActivateWeight2() : MachineFunctionPass(ID) {
189 PassRegistry &Registry = *PassRegistry::getPassRegistry();
190 initializeHexagonMergeActivateWeight2Pass(Registry);
191 }
192
193 StringRef getPassName() const override {
194 return "Hexagon Merge Activate Weight Instructions Before Packetizer";
195 }
196 bool runOnMachineFunction(MachineFunction &MF) override {
197 return runActivateWeight(MF, /*LIS=*/nullptr);
198 }
199};
200
201char HexagonMergeActivateWeight2::ID = 0;
202
203} // namespace llvm
204
205//===----------------------------------------------------------------------===//
206// Public Constructor Functions
207//===----------------------------------------------------------------------===//
208
209INITIALIZE_PASS_BEGIN(HexagonMergeActivateWeight,
210 "hexagon-merge-activate-weight",
211 "Hexagon merge activate weight after machine scheduler",
212 false, false)
213INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
214INITIALIZE_PASS_END(HexagonMergeActivateWeight, "hexagon-merge-activate-weight",
215 "Hexagon merge activate weight after machine scheduler",
216 false, false)
217
218INITIALIZE_PASS(HexagonMergeActivateWeight2, "hexagon-merge-activate-weight-2",
219 "Hexagon merge activate weight before packetizer", false, false)
220
221FunctionPass *llvm::createHexagonMergeActivateWeight() {
222 return new HexagonMergeActivateWeight();
223}
224
225FunctionPass *llvm::createHexagonMergeActivateWeight2() {
226 return new HexagonMergeActivateWeight2();
227}
228