1 //=-- SystemZHazardRecognizer.h - SystemZ Hazard Recognizer -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a hazard recognizer for the SystemZ scheduler.
11 //
12 // This class is used by the SystemZ scheduling strategy to maintain
13 // the state during scheduling, and provide cost functions for
14 // scheduling candidates. This includes:
15 //
16 // * Decoder grouping. A decoder group can maximally hold 3 uops, and
17 // instructions that always begin a new group should be scheduled when
18 // the current decoder group is empty.
19 // * Processor resources usage. It is beneficial to balance the use of
20 // resources.
21 //
22 // A goal is to consider all instructions, also those outside of any
23 // scheduling region. Such instructions are "advanced" past and include
24 // single instructions before a scheduling region, branches etc.
25 //
26 // A block that has only one predecessor continues scheduling with the state
27 // of it (which may be updated by emitting branches).
28 //
29 // ===---------------------------------------------------------------------===//
30 
31 #include "SystemZHazardRecognizer.h"
32 #include "llvm/ADT/Statistic.h"
33 
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "machine-scheduler"
37 
38 // This is the limit of processor resource usage at which the
39 // scheduler should try to look for other instructions (not using the
40 // critical resource).
41 static cl::opt<int> ProcResCostLim("procres-cost-lim", cl::Hidden,
42                                    cl::desc("The OOO window for processor "
43                                             "resources during scheduling."),
44                                    cl::init(8));
45 
46 unsigned SystemZHazardRecognizer::
47 getNumDecoderSlots(SUnit *SU) const {
48   const MCSchedClassDesc *SC = getSchedClass(SU);
49   if (!SC->isValid())
50     return 0; // IMPLICIT_DEF / KILL -- will not make impact in output.
51 
52   if (SC->BeginGroup) {
53     if (!SC->EndGroup)
54       return 2; // Cracked instruction
55     else
56       return 3; // Expanded/group-alone instruction
57   }
58 
59   return 1; // Normal instruction
60 }
61 
62 unsigned SystemZHazardRecognizer::getCurrCycleIdx(SUnit *SU) const {
63   unsigned Idx = CurrGroupSize;
64   if (GrpCount % 2)
65     Idx += 3;
66 
67   if (SU != nullptr && !fitsIntoCurrentGroup(SU)) {
68     if (Idx == 1 || Idx == 2)
69       Idx = 3;
70     else if (Idx == 4 || Idx == 5)
71       Idx = 0;
72   }
73 
74   return Idx;
75 }
76 
77 ScheduleHazardRecognizer::HazardType SystemZHazardRecognizer::
78 getHazardType(SUnit *m, int Stalls) {
79   return (fitsIntoCurrentGroup(m) ? NoHazard : Hazard);
80 }
81 
82 void SystemZHazardRecognizer::Reset() {
83   CurrGroupSize = 0;
84   clearProcResCounters();
85   GrpCount = 0;
86   LastFPdOpCycleIdx = UINT_MAX;
87   LastEmittedMI = nullptr;
88   DEBUG(CurGroupDbg = "";);
89 }
90 
91 bool
92 SystemZHazardRecognizer::fitsIntoCurrentGroup(SUnit *SU) const {
93   const MCSchedClassDesc *SC = getSchedClass(SU);
94   if (!SC->isValid())
95     return true;
96 
97   // A cracked instruction only fits into schedule if the current
98   // group is empty.
99   if (SC->BeginGroup)
100     return (CurrGroupSize == 0);
101 
102   // Since a full group is handled immediately in EmitInstruction(),
103   // SU should fit into current group. NumSlots should be 1 or 0,
104   // since it is not a cracked or expanded instruction.
105   assert ((getNumDecoderSlots(SU) <= 1) && (CurrGroupSize < 3) &&
106           "Expected normal instruction to fit in non-full group!");
107 
108   return true;
109 }
110 
111 void SystemZHazardRecognizer::nextGroup() {
112   if (CurrGroupSize == 0)
113     return;
114 
115   DEBUG(dumpCurrGroup("Completed decode group"));
116   DEBUG(CurGroupDbg = "";);
117 
118   GrpCount++;
119 
120   // Reset counter for next group.
121   CurrGroupSize = 0;
122 
123   // Decrease counters for execution units by one.
124   for (unsigned i = 0; i < SchedModel->getNumProcResourceKinds(); ++i)
125     if (ProcResourceCounters[i] > 0)
126       ProcResourceCounters[i]--;
127 
128   // Clear CriticalResourceIdx if it is now below the threshold.
129   if (CriticalResourceIdx != UINT_MAX &&
130       (ProcResourceCounters[CriticalResourceIdx] <=
131        ProcResCostLim))
132     CriticalResourceIdx = UINT_MAX;
133 
134   DEBUG(dumpState(););
135 }
136 
137 #ifndef NDEBUG // Debug output
138 void SystemZHazardRecognizer::dumpSU(SUnit *SU, raw_ostream &OS) const {
139   OS << "SU(" << SU->NodeNum << "):";
140   OS << TII->getName(SU->getInstr()->getOpcode());
141 
142   const MCSchedClassDesc *SC = getSchedClass(SU);
143   if (!SC->isValid())
144     return;
145 
146   for (TargetSchedModel::ProcResIter
147          PI = SchedModel->getWriteProcResBegin(SC),
148          PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
149     const MCProcResourceDesc &PRD =
150       *SchedModel->getProcResource(PI->ProcResourceIdx);
151     std::string FU(PRD.Name);
152     // trim e.g. Z13_FXaUnit -> FXa
153     FU = FU.substr(FU.find("_") + 1);
154     FU.resize(FU.find("Unit"));
155     OS << "/" << FU;
156 
157     if (PI->Cycles > 1)
158       OS << "(" << PI->Cycles << "cyc)";
159   }
160 
161   if (SC->NumMicroOps > 1)
162     OS << "/" << SC->NumMicroOps << "uops";
163   if (SC->BeginGroup && SC->EndGroup)
164     OS << "/GroupsAlone";
165   else if (SC->BeginGroup)
166     OS << "/BeginsGroup";
167   else if (SC->EndGroup)
168     OS << "/EndsGroup";
169   if (SU->isUnbuffered)
170     OS << "/Unbuffered";
171 }
172 
173 void SystemZHazardRecognizer::dumpCurrGroup(std::string Msg) const {
174   dbgs() << "++ " << Msg;
175   dbgs() << ": ";
176 
177   if (CurGroupDbg.empty())
178     dbgs() << " <empty>\n";
179   else {
180     dbgs() << "{ " << CurGroupDbg << " }";
181     dbgs() << " (" << CurrGroupSize << " decoder slot"
182            << (CurrGroupSize > 1 ? "s":"")
183            << ")\n";
184   }
185 }
186 
187 void SystemZHazardRecognizer::dumpProcResourceCounters() const {
188   bool any = false;
189 
190   for (unsigned i = 0; i < SchedModel->getNumProcResourceKinds(); ++i)
191     if (ProcResourceCounters[i] > 0) {
192       any = true;
193       break;
194     }
195 
196   if (!any)
197     return;
198 
199   dbgs() << "++ | Resource counters: ";
200   for (unsigned i = 0; i < SchedModel->getNumProcResourceKinds(); ++i)
201     if (ProcResourceCounters[i] > 0)
202       dbgs() << SchedModel->getProcResource(i)->Name
203              << ":" << ProcResourceCounters[i] << " ";
204   dbgs() << "\n";
205 
206   if (CriticalResourceIdx != UINT_MAX)
207     dbgs() << "++ | Critical resource: "
208            << SchedModel->getProcResource(CriticalResourceIdx)->Name
209            << "\n";
210 }
211 
212 void SystemZHazardRecognizer::dumpState() const {
213   dumpCurrGroup("| Current decoder group");
214   dbgs() << "++ | Current cycle index: "
215          << getCurrCycleIdx() << "\n";
216   dumpProcResourceCounters();
217   if (LastFPdOpCycleIdx != UINT_MAX)
218     dbgs() << "++ | Last FPd cycle index: " << LastFPdOpCycleIdx << "\n";
219 }
220 
221 #endif //NDEBUG
222 
223 void SystemZHazardRecognizer::clearProcResCounters() {
224   ProcResourceCounters.assign(SchedModel->getNumProcResourceKinds(), 0);
225   CriticalResourceIdx = UINT_MAX;
226 }
227 
228 static inline bool isBranchRetTrap(MachineInstr *MI) {
229   return (MI->isBranch() || MI->isReturn() ||
230           MI->getOpcode() == SystemZ::CondTrap);
231 }
232 
233 // Update state with SU as the next scheduled unit.
234 void SystemZHazardRecognizer::
235 EmitInstruction(SUnit *SU) {
236   const MCSchedClassDesc *SC = getSchedClass(SU);
237   DEBUG(dbgs() << "++ HazardRecognizer emitting "; dumpSU(SU, dbgs());
238         dbgs() << "\n";);
239   DEBUG(dumpCurrGroup("Decode group before emission"););
240 
241   // If scheduling an SU that must begin a new decoder group, move on
242   // to next group.
243   if (!fitsIntoCurrentGroup(SU))
244     nextGroup();
245 
246   DEBUG(raw_string_ostream cgd(CurGroupDbg);
247         if (CurGroupDbg.length())
248           cgd << ", ";
249         dumpSU(SU, cgd););
250 
251   LastEmittedMI = SU->getInstr();
252 
253   // After returning from a call, we don't know much about the state.
254   if (SU->isCall) {
255     DEBUG(dbgs() << "++ Clearing state after call.\n";);
256     Reset();
257     LastEmittedMI = SU->getInstr();
258     return;
259   }
260 
261   // Increase counter for execution unit(s).
262   for (TargetSchedModel::ProcResIter
263          PI = SchedModel->getWriteProcResBegin(SC),
264          PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI) {
265     // Don't handle FPd together with the other resources.
266     if (SchedModel->getProcResource(PI->ProcResourceIdx)->BufferSize == 1)
267       continue;
268     int &CurrCounter =
269       ProcResourceCounters[PI->ProcResourceIdx];
270     CurrCounter += PI->Cycles;
271     // Check if this is now the new critical resource.
272     if ((CurrCounter > ProcResCostLim) &&
273         (CriticalResourceIdx == UINT_MAX ||
274          (PI->ProcResourceIdx != CriticalResourceIdx &&
275           CurrCounter >
276           ProcResourceCounters[CriticalResourceIdx]))) {
277       DEBUG(dbgs() << "++ New critical resource: "
278             << SchedModel->getProcResource(PI->ProcResourceIdx)->Name
279             << "\n";);
280       CriticalResourceIdx = PI->ProcResourceIdx;
281     }
282   }
283 
284   // Make note of an instruction that uses a blocking resource (FPd).
285   if (SU->isUnbuffered) {
286     LastFPdOpCycleIdx = getCurrCycleIdx(SU);
287     DEBUG(dbgs() << "++ Last FPd cycle index: "
288           << LastFPdOpCycleIdx << "\n";);
289   }
290 
291   // Insert SU into current group by increasing number of slots used
292   // in current group.
293   CurrGroupSize += getNumDecoderSlots(SU);
294   assert (CurrGroupSize <= 3);
295 
296   // Check if current group is now full/ended. If so, move on to next
297   // group to be ready to evaluate more candidates.
298   if (CurrGroupSize == 3 || SC->EndGroup)
299     nextGroup();
300 }
301 
302 int SystemZHazardRecognizer::groupingCost(SUnit *SU) const {
303   const MCSchedClassDesc *SC = getSchedClass(SU);
304   if (!SC->isValid())
305     return 0;
306 
307   // If SU begins new group, it can either break a current group early
308   // or fit naturally if current group is empty (negative cost).
309   if (SC->BeginGroup) {
310     if (CurrGroupSize)
311       return 3 - CurrGroupSize;
312     return -1;
313   }
314 
315   // Similarly, a group-ending SU may either fit well (last in group), or
316   // end the group prematurely.
317   if (SC->EndGroup) {
318     unsigned resultingGroupSize =
319       (CurrGroupSize + getNumDecoderSlots(SU));
320     if (resultingGroupSize < 3)
321       return (3 - resultingGroupSize);
322     return -1;
323   }
324 
325   // Most instructions can be placed in any decoder slot.
326   return 0;
327 }
328 
329 bool SystemZHazardRecognizer::isFPdOpPreferred_distance(SUnit *SU) const {
330   assert (SU->isUnbuffered);
331   // If this is the first FPd op, it should be scheduled high.
332   if (LastFPdOpCycleIdx == UINT_MAX)
333     return true;
334   // If this is not the first PFd op, it should go into the other side
335   // of the processor to use the other FPd unit there. This should
336   // generally happen if two FPd ops are placed with 2 other
337   // instructions between them (modulo 6).
338   unsigned SUCycleIdx = getCurrCycleIdx(SU);
339   if (LastFPdOpCycleIdx > SUCycleIdx)
340     return ((LastFPdOpCycleIdx - SUCycleIdx) == 3);
341   return ((SUCycleIdx - LastFPdOpCycleIdx) == 3);
342 }
343 
344 int SystemZHazardRecognizer::
345 resourcesCost(SUnit *SU) {
346   int Cost = 0;
347 
348   const MCSchedClassDesc *SC = getSchedClass(SU);
349   if (!SC->isValid())
350     return 0;
351 
352   // For a FPd op, either return min or max value as indicated by the
353   // distance to any prior FPd op.
354   if (SU->isUnbuffered)
355     Cost = (isFPdOpPreferred_distance(SU) ? INT_MIN : INT_MAX);
356   // For other instructions, give a cost to the use of the critical resource.
357   else if (CriticalResourceIdx != UINT_MAX) {
358     for (TargetSchedModel::ProcResIter
359            PI = SchedModel->getWriteProcResBegin(SC),
360            PE = SchedModel->getWriteProcResEnd(SC); PI != PE; ++PI)
361       if (PI->ProcResourceIdx == CriticalResourceIdx)
362         Cost = PI->Cycles;
363   }
364 
365   return Cost;
366 }
367 
368 void SystemZHazardRecognizer::emitInstruction(MachineInstr *MI,
369                                               bool TakenBranch) {
370   // Make a temporary SUnit.
371   SUnit SU(MI, 0);
372 
373   // Set interesting flags.
374   SU.isCall = MI->isCall();
375 
376   const MCSchedClassDesc *SC = SchedModel->resolveSchedClass(MI);
377   for (const MCWriteProcResEntry &PRE :
378          make_range(SchedModel->getWriteProcResBegin(SC),
379                     SchedModel->getWriteProcResEnd(SC))) {
380     switch (SchedModel->getProcResource(PRE.ProcResourceIdx)->BufferSize) {
381     case 0:
382       SU.hasReservedResource = true;
383       break;
384     case 1:
385       SU.isUnbuffered = true;
386       break;
387     default:
388       break;
389     }
390   }
391 
392   unsigned GroupSizeBeforeEmit = CurrGroupSize;
393   EmitInstruction(&SU);
394 
395   if (!TakenBranch && isBranchRetTrap(MI)) {
396     // NT Branch on second slot ends group.
397     if (GroupSizeBeforeEmit == 1)
398       nextGroup();
399   }
400 
401   if (TakenBranch && CurrGroupSize > 0)
402     nextGroup();
403 
404   assert ((!MI->isTerminator() || isBranchRetTrap(MI)) &&
405           "Scheduler: unhandled terminator!");
406 }
407 
408 void SystemZHazardRecognizer::
409 copyState(SystemZHazardRecognizer *Incoming) {
410   // Current decoder group
411   CurrGroupSize = Incoming->CurrGroupSize;
412   DEBUG(CurGroupDbg = Incoming->CurGroupDbg;);
413 
414   // Processor resources
415   ProcResourceCounters = Incoming->ProcResourceCounters;
416   CriticalResourceIdx = Incoming->CriticalResourceIdx;
417 
418   // FPd
419   LastFPdOpCycleIdx = Incoming->LastFPdOpCycleIdx;
420   GrpCount = Incoming->GrpCount;
421 }
422