xref: /llvm-project/llvm/lib/MCA/HardwareUnits/RegisterFile.cpp (revision 4bce783ee332553a880135631c5cb3b40e2517dd)
1 //===--------------------- RegisterFile.cpp ---------------------*- C++ -*-===//
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 /// \file
9 ///
10 /// This file defines a register mapping file class.  This class is responsible
11 /// for managing hardware register files and the tracking of data dependencies
12 /// between registers.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/MCA/HardwareUnits/RegisterFile.h"
17 #include "llvm/MCA/Instruction.h"
18 #include "llvm/Support/Debug.h"
19 
20 #define DEBUG_TYPE "llvm-mca"
21 
22 namespace llvm {
23 namespace mca {
24 
25 RegisterFile::RegisterFile(const MCSchedModel &SM, const MCRegisterInfo &mri,
26                            unsigned NumRegs)
27     : MRI(mri),
28       RegisterMappings(mri.getNumRegs(), {WriteRef(), RegisterRenamingInfo()}),
29       ZeroRegisters(mri.getNumRegs(), false) {
30   initialize(SM, NumRegs);
31 }
32 
33 void RegisterFile::initialize(const MCSchedModel &SM, unsigned NumRegs) {
34   // Create a default register file that "sees" all the machine registers
35   // declared by the target. The number of physical registers in the default
36   // register file is set equal to `NumRegs`. A value of zero for `NumRegs`
37   // means: this register file has an unbounded number of physical registers.
38   RegisterFiles.emplace_back(NumRegs);
39   if (!SM.hasExtraProcessorInfo())
40     return;
41 
42   // For each user defined register file, allocate a RegisterMappingTracker
43   // object. The size of every register file, as well as the mapping between
44   // register files and register classes is specified via tablegen.
45   const MCExtraProcessorInfo &Info = SM.getExtraProcessorInfo();
46 
47   // Skip invalid register file at index 0.
48   for (unsigned I = 1, E = Info.NumRegisterFiles; I < E; ++I) {
49     const MCRegisterFileDesc &RF = Info.RegisterFiles[I];
50     assert(RF.NumPhysRegs && "Invalid PRF with zero physical registers!");
51 
52     // The cost of a register definition is equivalent to the number of
53     // physical registers that are allocated at register renaming stage.
54     unsigned Length = RF.NumRegisterCostEntries;
55     const MCRegisterCostEntry *FirstElt =
56         &Info.RegisterCostTable[RF.RegisterCostEntryIdx];
57     addRegisterFile(RF, ArrayRef<MCRegisterCostEntry>(FirstElt, Length));
58   }
59 }
60 
61 void RegisterFile::cycleStart() {
62   for (RegisterMappingTracker &RMT : RegisterFiles)
63     RMT.NumMoveEliminated = 0;
64 }
65 
66 void RegisterFile::addRegisterFile(const MCRegisterFileDesc &RF,
67                                    ArrayRef<MCRegisterCostEntry> Entries) {
68   // A default register file is always allocated at index #0. That register file
69   // is mainly used to count the total number of mappings created by all
70   // register files at runtime. Users can limit the number of available physical
71   // registers in register file #0 through the command line flag
72   // `-register-file-size`.
73   unsigned RegisterFileIndex = RegisterFiles.size();
74   RegisterFiles.emplace_back(RF.NumPhysRegs, RF.MaxMovesEliminatedPerCycle,
75                              RF.AllowZeroMoveEliminationOnly);
76 
77   // Special case where there is no register class identifier in the set.
78   // An empty set of register classes means: this register file contains all
79   // the physical registers specified by the target.
80   // We optimistically assume that a register can be renamed at the cost of a
81   // single physical register. The constructor of RegisterFile ensures that
82   // a RegisterMapping exists for each logical register defined by the Target.
83   if (Entries.empty())
84     return;
85 
86   // Now update the cost of individual registers.
87   for (const MCRegisterCostEntry &RCE : Entries) {
88     const MCRegisterClass &RC = MRI.getRegClass(RCE.RegisterClassID);
89     for (const MCPhysReg Reg : RC) {
90       RegisterRenamingInfo &Entry = RegisterMappings[Reg].second;
91       IndexPlusCostPairTy &IPC = Entry.IndexPlusCost;
92       if (IPC.first && IPC.first != RegisterFileIndex) {
93         // The only register file that is allowed to overlap is the default
94         // register file at index #0. The analysis is inaccurate if register
95         // files overlap.
96         errs() << "warning: register " << MRI.getName(Reg)
97                << " defined in multiple register files.";
98       }
99       IPC = std::make_pair(RegisterFileIndex, RCE.Cost);
100       Entry.RenameAs = Reg;
101       Entry.AllowMoveElimination = RCE.AllowMoveElimination;
102 
103       // Assume the same cost for each sub-register.
104       for (MCSubRegIterator I(Reg, &MRI); I.isValid(); ++I) {
105         RegisterRenamingInfo &OtherEntry = RegisterMappings[*I].second;
106         if (!OtherEntry.IndexPlusCost.first &&
107             (!OtherEntry.RenameAs ||
108              MRI.isSuperRegister(*I, OtherEntry.RenameAs))) {
109           OtherEntry.IndexPlusCost = IPC;
110           OtherEntry.RenameAs = Reg;
111         }
112       }
113     }
114   }
115 }
116 
117 void RegisterFile::allocatePhysRegs(const RegisterRenamingInfo &Entry,
118                                     MutableArrayRef<unsigned> UsedPhysRegs) {
119   unsigned RegisterFileIndex = Entry.IndexPlusCost.first;
120   unsigned Cost = Entry.IndexPlusCost.second;
121   if (RegisterFileIndex) {
122     RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex];
123     RMT.NumUsedPhysRegs += Cost;
124     UsedPhysRegs[RegisterFileIndex] += Cost;
125   }
126 
127   // Now update the default register mapping tracker.
128   RegisterFiles[0].NumUsedPhysRegs += Cost;
129   UsedPhysRegs[0] += Cost;
130 }
131 
132 void RegisterFile::freePhysRegs(const RegisterRenamingInfo &Entry,
133                                 MutableArrayRef<unsigned> FreedPhysRegs) {
134   unsigned RegisterFileIndex = Entry.IndexPlusCost.first;
135   unsigned Cost = Entry.IndexPlusCost.second;
136   if (RegisterFileIndex) {
137     RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex];
138     RMT.NumUsedPhysRegs -= Cost;
139     FreedPhysRegs[RegisterFileIndex] += Cost;
140   }
141 
142   // Now update the default register mapping tracker.
143   RegisterFiles[0].NumUsedPhysRegs -= Cost;
144   FreedPhysRegs[0] += Cost;
145 }
146 
147 void RegisterFile::addRegisterWrite(WriteRef Write,
148                                     MutableArrayRef<unsigned> UsedPhysRegs) {
149   WriteState &WS = *Write.getWriteState();
150   unsigned RegID = WS.getRegisterID();
151   assert(RegID && "Adding an invalid register definition?");
152 
153   LLVM_DEBUG({
154     dbgs() << "RegisterFile: addRegisterWrite [ " << Write.getSourceIndex()
155            << ", " << MRI.getName(RegID) << "]\n";
156   });
157 
158   // If RenameAs is equal to RegID, then RegID is subject to register renaming
159   // and false dependencies on RegID are all eliminated.
160 
161   // If RenameAs references the invalid register, then we optimistically assume
162   // that it can be renamed. In the absence of tablegen descriptors for register
163   // files, RenameAs is always set to the invalid register ID.  In all other
164   // cases, RenameAs must be either equal to RegID, or it must reference a
165   // super-register of RegID.
166 
167   // If RenameAs is a super-register of RegID, then a write to RegID has always
168   // a false dependency on RenameAs. The only exception is for when the write
169   // implicitly clears the upper portion of the underlying register.
170   // If a write clears its super-registers, then it is renamed as `RenameAs`.
171   bool IsWriteZero = WS.isWriteZero();
172   bool IsEliminated = WS.isEliminated();
173   bool ShouldAllocatePhysRegs = !IsWriteZero && !IsEliminated;
174   const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second;
175   WS.setPRF(RRI.IndexPlusCost.first);
176 
177   if (RRI.RenameAs && RRI.RenameAs != RegID) {
178     RegID = RRI.RenameAs;
179     WriteRef &OtherWrite = RegisterMappings[RegID].first;
180 
181     if (!WS.clearsSuperRegisters()) {
182       // The processor keeps the definition of `RegID` together with register
183       // `RenameAs`. Since this partial write is not renamed, no physical
184       // register is allocated.
185       ShouldAllocatePhysRegs = false;
186 
187       WriteState *OtherWS = OtherWrite.getWriteState();
188       if (OtherWS && (OtherWrite.getSourceIndex() != Write.getSourceIndex())) {
189         // This partial write has a false dependency on RenameAs.
190         assert(!IsEliminated && "Unexpected partial update!");
191         OtherWS->addUser(&WS);
192       }
193     }
194   }
195 
196   // Update zero registers.
197   unsigned ZeroRegisterID =
198       WS.clearsSuperRegisters() ? RegID : WS.getRegisterID();
199   if (IsWriteZero) {
200     ZeroRegisters.setBit(ZeroRegisterID);
201     for (MCSubRegIterator I(ZeroRegisterID, &MRI); I.isValid(); ++I)
202       ZeroRegisters.setBit(*I);
203   } else {
204     ZeroRegisters.clearBit(ZeroRegisterID);
205     for (MCSubRegIterator I(ZeroRegisterID, &MRI); I.isValid(); ++I)
206       ZeroRegisters.clearBit(*I);
207   }
208 
209   // If this is move has been eliminated, then the call to tryEliminateMove
210   // should have already updated all the register mappings.
211   if (!IsEliminated) {
212     // Update the mapping for register RegID including its sub-registers.
213     RegisterMappings[RegID].first = Write;
214     RegisterMappings[RegID].second.AliasRegID = 0U;
215     for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) {
216       RegisterMappings[*I].first = Write;
217       RegisterMappings[*I].second.AliasRegID = 0U;
218     }
219 
220     // No physical registers are allocated for instructions that are optimized
221     // in hardware. For example, zero-latency data-dependency breaking
222     // instructions don't consume physical registers.
223     if (ShouldAllocatePhysRegs)
224       allocatePhysRegs(RegisterMappings[RegID].second, UsedPhysRegs);
225   }
226 
227   if (!WS.clearsSuperRegisters())
228     return;
229 
230   for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) {
231     if (!IsEliminated) {
232       RegisterMappings[*I].first = Write;
233       RegisterMappings[*I].second.AliasRegID = 0U;
234     }
235 
236     if (IsWriteZero)
237       ZeroRegisters.setBit(*I);
238     else
239       ZeroRegisters.clearBit(*I);
240   }
241 }
242 
243 void RegisterFile::removeRegisterWrite(
244     const WriteState &WS, MutableArrayRef<unsigned> FreedPhysRegs) {
245   // Early exit if this write was eliminated. A write eliminated at register
246   // renaming stage generates an alias, and it is not added to the PRF.
247   if (WS.isEliminated())
248     return;
249 
250   unsigned RegID = WS.getRegisterID();
251 
252   assert(RegID != 0 && "Invalidating an already invalid register?");
253   assert(WS.getCyclesLeft() != UNKNOWN_CYCLES &&
254          "Invalidating a write of unknown cycles!");
255   assert(WS.getCyclesLeft() <= 0 && "Invalid cycles left for this write!");
256 
257   bool ShouldFreePhysRegs = !WS.isWriteZero();
258   unsigned RenameAs = RegisterMappings[RegID].second.RenameAs;
259   if (RenameAs && RenameAs != RegID) {
260     RegID = RenameAs;
261 
262     if (!WS.clearsSuperRegisters()) {
263       // Keep the definition of `RegID` together with register `RenameAs`.
264       ShouldFreePhysRegs = false;
265     }
266   }
267 
268   if (ShouldFreePhysRegs)
269     freePhysRegs(RegisterMappings[RegID].second, FreedPhysRegs);
270 
271   WriteRef &WR = RegisterMappings[RegID].first;
272   if (WR.getWriteState() == &WS)
273     WR.invalidate();
274 
275   for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) {
276     WriteRef &OtherWR = RegisterMappings[*I].first;
277     if (OtherWR.getWriteState() == &WS)
278       OtherWR.invalidate();
279   }
280 
281   if (!WS.clearsSuperRegisters())
282     return;
283 
284   for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) {
285     WriteRef &OtherWR = RegisterMappings[*I].first;
286     if (OtherWR.getWriteState() == &WS)
287       OtherWR.invalidate();
288   }
289 }
290 
291 bool RegisterFile::tryEliminateMove(WriteState &WS, ReadState &RS) {
292   const RegisterMapping &RMFrom = RegisterMappings[RS.getRegisterID()];
293   const RegisterMapping &RMTo = RegisterMappings[WS.getRegisterID()];
294 
295   // From and To must be owned by the same PRF.
296   const RegisterRenamingInfo &RRIFrom = RMFrom.second;
297   const RegisterRenamingInfo &RRITo = RMTo.second;
298   unsigned RegisterFileIndex = RRIFrom.IndexPlusCost.first;
299   if (RegisterFileIndex != RRITo.IndexPlusCost.first)
300     return false;
301 
302   // We only allow move elimination for writes that update a full physical
303   // register. On X86, move elimination is possible with 32-bit general purpose
304   // registers because writes to those registers are not partial writes.  If a
305   // register move is a partial write, then we conservatively assume that move
306   // elimination fails, since it would either trigger a partial update, or the
307   // issue of a merge opcode.
308   //
309   // Note that this constraint may be lifted in future.  For example, we could
310   // make this model more flexible, and let users customize the set of registers
311   // (i.e. register classes) that allow move elimination.
312   //
313   // For now, we assume that there is a strong correlation between registers
314   // that allow move elimination, and how those same registers are renamed in
315   // hardware.
316   if (RRITo.RenameAs && RRITo.RenameAs != WS.getRegisterID()) {
317     // Early exit if the PRF doesn't support move elimination for this register.
318     if (!RegisterMappings[RRITo.RenameAs].second.AllowMoveElimination)
319       return false;
320     if (!WS.clearsSuperRegisters())
321       return false;
322   }
323 
324   RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex];
325   if (RMT.MaxMoveEliminatedPerCycle &&
326       RMT.NumMoveEliminated == RMT.MaxMoveEliminatedPerCycle)
327     return false;
328 
329   bool IsZeroMove = ZeroRegisters[RS.getRegisterID()];
330   if (RMT.AllowZeroMoveEliminationOnly && !IsZeroMove)
331     return false;
332 
333   MCPhysReg FromReg = RS.getRegisterID();
334   MCPhysReg ToReg = WS.getRegisterID();
335 
336   // Construct an alias.
337   MCPhysReg AliasReg = FromReg;
338   if (RRIFrom.RenameAs)
339     AliasReg = RRIFrom.RenameAs;
340 
341   const RegisterRenamingInfo &RMAlias = RegisterMappings[AliasReg].second;
342   if (RMAlias.AliasRegID)
343     AliasReg = RMAlias.AliasRegID;
344 
345   if (AliasReg != ToReg) {
346     RegisterMappings[ToReg].second.AliasRegID = AliasReg;
347     for (MCSubRegIterator I(ToReg, &MRI); I.isValid(); ++I)
348       RegisterMappings[*I].second.AliasRegID = AliasReg;
349   }
350 
351   RMT.NumMoveEliminated++;
352   if (IsZeroMove) {
353     WS.setWriteZero();
354     RS.setReadZero();
355   }
356   WS.setEliminated();
357 
358   return true;
359 }
360 
361 void RegisterFile::collectWrites(const ReadState &RS,
362                                  SmallVectorImpl<WriteRef> &Writes) const {
363   unsigned RegID = RS.getRegisterID();
364   assert(RegID && RegID < RegisterMappings.size());
365   LLVM_DEBUG(dbgs() << "RegisterFile: collecting writes for register "
366                     << MRI.getName(RegID) << '\n');
367 
368   // Check if this is an alias.
369   const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second;
370   if (RRI.AliasRegID)
371     RegID = RRI.AliasRegID;
372 
373   const WriteRef &WR = RegisterMappings[RegID].first;
374   if (WR.isValid())
375     Writes.push_back(WR);
376 
377   // Handle potential partial register updates.
378   for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) {
379     const WriteRef &WR = RegisterMappings[*I].first;
380     if (WR.isValid())
381       Writes.push_back(WR);
382   }
383 
384   // Remove duplicate entries and resize the input vector.
385   if (Writes.size() > 1) {
386     sort(Writes, [](const WriteRef &Lhs, const WriteRef &Rhs) {
387       return Lhs.getWriteState() < Rhs.getWriteState();
388     });
389     auto It = std::unique(Writes.begin(), Writes.end());
390     Writes.resize(std::distance(Writes.begin(), It));
391   }
392 
393   LLVM_DEBUG({
394     for (const WriteRef &WR : Writes) {
395       const WriteState &WS = *WR.getWriteState();
396       dbgs() << "[PRF] Found a dependent use of Register "
397              << MRI.getName(WS.getRegisterID()) << " (defined by instruction #"
398              << WR.getSourceIndex() << ")\n";
399     }
400   });
401 }
402 
403 void RegisterFile::addRegisterRead(ReadState &RS,
404                                    const MCSubtargetInfo &STI) const {
405   unsigned RegID = RS.getRegisterID();
406   const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second;
407   RS.setPRF(RRI.IndexPlusCost.first);
408   if (RS.isIndependentFromDef())
409     return;
410 
411   if (ZeroRegisters[RS.getRegisterID()])
412     RS.setReadZero();
413 
414   SmallVector<WriteRef, 4> DependentWrites;
415   collectWrites(RS, DependentWrites);
416   RS.setDependentWrites(DependentWrites.size());
417 
418   // We know that this read depends on all the writes in DependentWrites.
419   // For each write, check if we have ReadAdvance information, and use it
420   // to figure out in how many cycles this read becomes available.
421   const ReadDescriptor &RD = RS.getDescriptor();
422   const MCSchedModel &SM = STI.getSchedModel();
423   const MCSchedClassDesc *SC = SM.getSchedClassDesc(RD.SchedClassID);
424   for (WriteRef &WR : DependentWrites) {
425     WriteState &WS = *WR.getWriteState();
426     unsigned WriteResID = WS.getWriteResourceID();
427     int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID);
428     WS.addUser(&RS, ReadAdvance);
429   }
430 }
431 
432 unsigned RegisterFile::isAvailable(ArrayRef<unsigned> Regs) const {
433   SmallVector<unsigned, 4> NumPhysRegs(getNumRegisterFiles());
434 
435   // Find how many new mappings must be created for each register file.
436   for (const unsigned RegID : Regs) {
437     const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second;
438     const IndexPlusCostPairTy &Entry = RRI.IndexPlusCost;
439     if (Entry.first)
440       NumPhysRegs[Entry.first] += Entry.second;
441     NumPhysRegs[0] += Entry.second;
442   }
443 
444   unsigned Response = 0;
445   for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) {
446     unsigned NumRegs = NumPhysRegs[I];
447     if (!NumRegs)
448       continue;
449 
450     const RegisterMappingTracker &RMT = RegisterFiles[I];
451     if (!RMT.NumPhysRegs) {
452       // The register file has an unbounded number of microarchitectural
453       // registers.
454       continue;
455     }
456 
457     if (RMT.NumPhysRegs < NumRegs) {
458       // The current register file is too small. This may occur if the number of
459       // microarchitectural registers in register file #0 was changed by the
460       // users via flag -reg-file-size. Alternatively, the scheduling model
461       // specified a too small number of registers for this register file.
462       LLVM_DEBUG(dbgs() << "Not enough registers in the register file.\n");
463 
464       // FIXME: Normalize the instruction register count to match the
465       // NumPhysRegs value.  This is a highly unusual case, and is not expected
466       // to occur.  This normalization is hiding an inconsistency in either the
467       // scheduling model or in the value that the user might have specified
468       // for NumPhysRegs.
469       NumRegs = RMT.NumPhysRegs;
470     }
471 
472     if (RMT.NumPhysRegs < (RMT.NumUsedPhysRegs + NumRegs))
473       Response |= (1U << I);
474   }
475 
476   return Response;
477 }
478 
479 #ifndef NDEBUG
480 void RegisterFile::dump() const {
481   for (unsigned I = 0, E = MRI.getNumRegs(); I < E; ++I) {
482     const RegisterMapping &RM = RegisterMappings[I];
483     const RegisterRenamingInfo &RRI = RM.second;
484     if (ZeroRegisters[I]) {
485       dbgs() << MRI.getName(I) << ", " << I
486              << ", PRF=" << RRI.IndexPlusCost.first
487              << ", Cost=" << RRI.IndexPlusCost.second
488              << ", RenameAs=" << RRI.RenameAs << ", IsZero=" << ZeroRegisters[I]
489              << ",";
490       RM.first.dump();
491       dbgs() << '\n';
492     }
493   }
494 
495   for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) {
496     dbgs() << "Register File #" << I;
497     const RegisterMappingTracker &RMT = RegisterFiles[I];
498     dbgs() << "\n  TotalMappings:        " << RMT.NumPhysRegs
499            << "\n  NumUsedMappings:      " << RMT.NumUsedPhysRegs << '\n';
500   }
501 }
502 #endif
503 
504 } // namespace mca
505 } // namespace llvm
506