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