LLVM 24.0.0git
HexagonHazardRecognizer.cpp
Go to the documentation of this file.
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
19#include "llvm/Support/Debug.h"
21#include <cassert>
22
23using namespace llvm;
24
25#define DEBUG_TYPE "post-RA-sched"
26
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
43 MachineInstr *MI = SU->getInstr();
44 if (!MI || TII->isZeroCost(MI->getOpcode()) || MI->isMetaInstruction())
45 return NoHazard;
46
47 if (!Resources->canReserveResources(*MI)) {
48 LLVM_DEBUG(dbgs() << "*** Hazard in cycle " << PacketNum << ", " << *MI);
49 HazardType RetVal = Hazard;
50 if (isNewStore(*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(TII->get(TII->getDotNewOp(*MI)),
56 MI->getDebugLoc());
57 if (Resources->canReserveResources(*NewMI))
58 RetVal = NoHazard;
59 LLVM_DEBUG(dbgs() << "*** Try .new version? " << (RetVal == NoHazard)
60 << "\n");
61 MF->deleteMachineInstr(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(SI) : TII->isHMXActivateInstr(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
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.
145
146 if (Weight == nullptr && Activate == nullptr)
147 if (TII->isHMXWeightInstr(*SU->getInstr()) ||
148 TII->isHMXActivateInstr(*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(MI.getNumOperands() - 1);
164 return MO.isReg() && RegDefs.contains(MO.getReg());
165}
166
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(MO.getReg());
177
178 if (TII->isZeroCost(MI->getOpcode()) || MI->isMetaInstruction())
179 return;
180
181 if (!Resources->canReserveResources(*MI) || isNewStore(*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(TII->get(TII->getDotNewOp(*MI)),
188 MI->getDebugLoc());
189 if (Resources->canReserveResources(*NewMI))
190 Resources->reserveResources(*NewMI);
191 else
192 Resources->reserveResources(*MI);
193 MF->deleteMachineInstr(NewMI);
194 } else
195 Resources->reserveResources(*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))
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->mayLoad() && !MI->mayStore())
218 for (auto &S : SU->Succs)
219 if (S.isAssignedRegDep() && S.getLatency() == 0 &&
220 TII->mayBeNewStore(*S.getSUnit()->getInstr()) &&
221 Resources->canReserveResources(*S.getSUnit()->getInstr())) {
222 PrefVectorStoreNew = S.getSUnit();
223 break;
224 }
225 bool IsWeight = TII->isHMXWeightInstr(*MI);
226 bool IsActivate = TII->isHMXActivateInstr(*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}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define LLVM_DEBUG(...)
Definition Debug.h:119
void Reset() override
This callback is invoked when a new block of instructions is about to be scheduled.
void AdvanceCycle() override
This callback is invoked whenever the next top-down instruction to be scheduled cannot issue in the c...
bool ShouldPreferAnother(SUnit *) const override
This callback may be invoked if getHazardType returns NoHazard.
HazardType getHazardType(SUnit *SU, int stalls) override
Return the hazard type of emitting this node.
void EmitInstruction(SUnit *) override
This callback is invoked when an instruction is emitted to be scheduled, to advance the hazard state.
Representation of each machine instruction.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
LLVM_ABI void insert(mop_iterator InsertBefore, ArrayRef< MachineOperand > Ops)
Inserts Ops BEFORE It. Can untie/retie tied operands.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
Scheduling dependency.
Definition ScheduleDAG.h:54
Scheduling unit. This is a node in the scheduling DAG.
bool isInstr() const
Returns true if this SUnit refers to a machine instruction as opposed to an SDNode.
unsigned NodeNum
Entry # of node in the node vector.
unsigned NumPredsLeft
SmallVector< SDep, 4 > Succs
All sunit successors.
MachineInstr * getInstr() const
Returns the representative MachineInstr for this SUnit.
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition SmallSet.h:229
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209