1//===-- HexagonHazardRecognizer.cpp - Hexagon Post RA Hazard Recognizer ---===//
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// This file defines the hazard recognizer for scheduling on Hexagon.
10// Use a DFA based hazard recognizer.
11//
12//===----------------------------------------------------------------------===//
13
14#include "HexagonHazardRecognizer.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineInstr.h"
17#include "llvm/CodeGen/MachineOperand.h"
18#include "llvm/CodeGen/ScheduleDAG.h"
19#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
21#include <cassert>
22
23using namespace llvm;
24
25#define DEBUG_TYPE "post-RA-sched"
26
27void HexagonHazardRecognizer::Reset() {
28 LLVM_DEBUG(dbgs() << "Reset hazard recognizer\n");
29 Resources->clearResources();
30 PacketNum = 0;
31 UsesDotCur = nullptr;
32 DotCurPNum = -1;
33 UsesLoad = false;
34 PrefVectorStoreNew = nullptr;
35 Weight = nullptr;
36 Activate = nullptr;
37 HMXPartner = nullptr;
38 RegDefs.clear();
39}
40
41ScheduleHazardRecognizer::HazardType
42HexagonHazardRecognizer::getHazardType(SUnit *SU, int stalls) {
43 MachineInstr *MI = SU->getInstr();
44 if (!MI || TII->isZeroCost(Opcode: MI->getOpcode()) || MI->isMetaInstruction())
45 return NoHazard;
46
47 if (!Resources->canReserveResources(MI&: *MI)) {
48 LLVM_DEBUG(dbgs() << "*** Hazard in cycle " << PacketNum << ", " << *MI);
49 HazardType RetVal = Hazard;
50 if (isNewStore(MI&: *MI)) {
51 // The .new store version uses different resources so check if it
52 // causes a hazard.
53 MachineFunction *MF = MI->getParent()->getParent();
54 MachineInstr *NewMI =
55 MF->CreateMachineInstr(MCID: TII->get(Opcode: TII->getDotNewOp(MI: *MI)),
56 DL: MI->getDebugLoc());
57 if (Resources->canReserveResources(MI&: *NewMI))
58 RetVal = NoHazard;
59 LLVM_DEBUG(dbgs() << "*** Try .new version? " << (RetVal == NoHazard)
60 << "\n");
61 MF->deleteMachineInstr(MI: NewMI);
62 }
63 return RetVal;
64 }
65
66 if (SU == UsesDotCur && DotCurPNum != (int)PacketNum) {
67 LLVM_DEBUG(dbgs() << "*** .cur Hazard in cycle " << PacketNum << ", "
68 << *MI);
69 return Hazard;
70 }
71
72 if (mustWaitForHMXPartner(SU)) {
73 LLVM_DEBUG(dbgs() << "*** HMX pair hazard in cycle " << PacketNum << ", "
74 << *MI);
75 return Hazard;
76 }
77
78 return NoHazard;
79}
80
81SUnit *HexagonHazardRecognizer::findHMXPartner(SUnit *SU) const {
82 const MachineInstr &MI = *SU->getInstr();
83 bool IsActivate = TII->isHMXActivateInstr(MI);
84 SUnit *Partner = nullptr;
85 // The pair is formed by the nearest following instruction of the opposite
86 // kind (see HexagonSubtarget::HMXActivateWeightMutation). Both instructions
87 // have side effects, so they are always connected by a dependence edge.
88 for (const SDep &D : SU->Succs) {
89 SUnit *S = D.getSUnit();
90 if (!S->isInstr())
91 continue;
92 const MachineInstr &SI = *S->getInstr();
93 bool Matches =
94 IsActivate ? TII->isHMXWeightInstr(MI: SI) : TII->isHMXActivateInstr(MI: SI);
95 if (Matches && (!Partner || S->NodeNum < Partner->NodeNum))
96 Partner = S;
97 }
98 return Partner;
99}
100
101bool HexagonHazardRecognizer::mustWaitForHMXPartner(SUnit *SU) const {
102 // Nothing to wait for unless exactly one half of a pair has been emitted.
103 if (!!Activate == !!Weight)
104 return false;
105 if (SU == HMXPartner)
106 return false;
107 // Delaying other instructions only helps if the partner can become ready
108 // without them. If the partner still depends on unscheduled instructions
109 // (e.g. a store or another side-effecting instruction between the two),
110 // or no partner was found, waiting would deadlock the scheduler.
111 return HMXPartner && HMXPartner->NumPredsLeft == 0;
112}
113
114void HexagonHazardRecognizer::AdvanceCycle() {
115 LLVM_DEBUG(dbgs() << "Advance cycle, clear state\n");
116 Resources->clearResources();
117 if (DotCurPNum != -1 && DotCurPNum != (int)PacketNum) {
118 UsesDotCur = nullptr;
119 DotCurPNum = -1;
120 }
121 UsesLoad = false;
122 PrefVectorStoreNew = nullptr;
123 if (Weight != nullptr && Activate != nullptr) {
124 Weight = nullptr;
125 Activate = nullptr;
126 HMXPartner = nullptr;
127 }
128 PacketNum++;
129 RegDefs.clear();
130}
131
132/// Handle the cases when we prefer one instruction over another. Case 1 - we
133/// prefer not to generate multiple loads in the packet to avoid a potential
134/// bank conflict. Case 2 - if a packet contains a dot cur instruction, then we
135/// prefer the instruction that can use the dot cur result. However, if the use
136/// is not scheduled in the same packet, then prefer other instructions in the
137/// subsequent packet. Case 3 - we prefer a vector store that can be converted
138/// to a .new store. The packetizer will not generate the .new store if the
139/// store doesn't have resources to fit in the packet (but the .new store may
140/// have resources). We attempt to schedule the store as soon as possible to
141/// help packetize the two instructions together. Case 4 - when no HMX
142/// activation/weight pair is in progress, we prefer other instructions over
143/// starting a new pair.
144bool HexagonHazardRecognizer::ShouldPreferAnother(SUnit *SU) const {
145
146 if (Weight == nullptr && Activate == nullptr)
147 if (TII->isHMXWeightInstr(MI: *SU->getInstr()) ||
148 TII->isHMXActivateInstr(MI: *SU->getInstr()))
149 return true;
150
151 if (PrefVectorStoreNew != nullptr && PrefVectorStoreNew != SU)
152 return true;
153 if (UsesLoad && SU->isInstr() && SU->getInstr()->mayLoad())
154 return true;
155 return UsesDotCur && ((SU == UsesDotCur) ^ (DotCurPNum == (int)PacketNum));
156}
157
158/// Return true if the instruction would be converted to a new value store when
159/// packetized.
160bool HexagonHazardRecognizer::isNewStore(MachineInstr &MI) {
161 if (!TII->mayBeNewStore(MI))
162 return false;
163 MachineOperand &MO = MI.getOperand(i: MI.getNumOperands() - 1);
164 return MO.isReg() && RegDefs.contains(V: MO.getReg());
165}
166
167void HexagonHazardRecognizer::EmitInstruction(SUnit *SU) {
168 MachineInstr *MI = SU->getInstr();
169 if (!MI)
170 return;
171
172 // Keep the set of definitions for each packet, which is used to determine
173 // if a .new can be used.
174 for (const MachineOperand &MO : MI->operands())
175 if (MO.isReg() && MO.isDef() && !MO.isImplicit())
176 RegDefs.insert(V: MO.getReg());
177
178 if (TII->isZeroCost(Opcode: MI->getOpcode()) || MI->isMetaInstruction())
179 return;
180
181 if (!Resources->canReserveResources(MI&: *MI) || isNewStore(MI&: *MI)) {
182 // It must be a .new store since other instructions must be able to be
183 // reserved at this point.
184 assert(TII->mayBeNewStore(*MI) && "Expecting .new store");
185 MachineFunction *MF = MI->getParent()->getParent();
186 MachineInstr *NewMI =
187 MF->CreateMachineInstr(MCID: TII->get(Opcode: TII->getDotNewOp(MI: *MI)),
188 DL: MI->getDebugLoc());
189 if (Resources->canReserveResources(MI&: *NewMI))
190 Resources->reserveResources(MI&: *NewMI);
191 else
192 Resources->reserveResources(MI&: *MI);
193 MF->deleteMachineInstr(MI: NewMI);
194 } else
195 Resources->reserveResources(MI&: *MI);
196 LLVM_DEBUG(dbgs() << " Add instruction " << *MI);
197
198 // When scheduling a dot cur instruction, check if there is an instruction
199 // that can use the dot cur in the same packet. If so, we'll attempt to
200 // schedule it before other instructions. We only do this if the load has a
201 // single zero-latency use.
202 if (TII->mayBeCurLoad(MI: *MI))
203 for (auto &S : SU->Succs)
204 if (S.isAssignedRegDep() && S.getLatency() == 0 &&
205 S.getSUnit()->NumPredsLeft == 1) {
206 UsesDotCur = S.getSUnit();
207 DotCurPNum = PacketNum;
208 break;
209 }
210 if (SU == UsesDotCur) {
211 UsesDotCur = nullptr;
212 DotCurPNum = -1;
213 }
214
215 UsesLoad = MI->mayLoad();
216
217 if (TII->isHVXVec(MI: *MI) && !MI->mayLoad() && !MI->mayStore())
218 for (auto &S : SU->Succs)
219 if (S.isAssignedRegDep() && S.getLatency() == 0 &&
220 TII->mayBeNewStore(MI: *S.getSUnit()->getInstr()) &&
221 Resources->canReserveResources(MI&: *S.getSUnit()->getInstr())) {
222 PrefVectorStoreNew = S.getSUnit();
223 break;
224 }
225 bool IsWeight = TII->isHMXWeightInstr(MI: *MI);
226 bool IsActivate = TII->isHMXActivateInstr(MI: *MI);
227 if (IsWeight || IsActivate) {
228 bool IsFirstHalf = !Weight && !Activate;
229 if (IsWeight)
230 Weight = SU;
231 if (IsActivate)
232 Activate = SU;
233 HMXPartner = IsFirstHalf ? findHMXPartner(SU) : nullptr;
234 }
235}
236