10b57cec5SDimitry Andric //===--------------------- RegisterFile.cpp ---------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// \file 90b57cec5SDimitry Andric /// 100b57cec5SDimitry Andric /// This file defines a register mapping file class. This class is responsible 110b57cec5SDimitry Andric /// for managing hardware register files and the tracking of data dependencies 120b57cec5SDimitry Andric /// between registers. 130b57cec5SDimitry Andric /// 140b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #include "llvm/MCA/HardwareUnits/RegisterFile.h" 170b57cec5SDimitry Andric #include "llvm/MCA/Instruction.h" 180b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric #define DEBUG_TYPE "llvm-mca" 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric namespace llvm { 230b57cec5SDimitry Andric namespace mca { 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric RegisterFile::RegisterFile(const MCSchedModel &SM, const MCRegisterInfo &mri, 260b57cec5SDimitry Andric unsigned NumRegs) 270b57cec5SDimitry Andric : MRI(mri), 280b57cec5SDimitry Andric RegisterMappings(mri.getNumRegs(), {WriteRef(), RegisterRenamingInfo()}), 290b57cec5SDimitry Andric ZeroRegisters(mri.getNumRegs(), false) { 300b57cec5SDimitry Andric initialize(SM, NumRegs); 310b57cec5SDimitry Andric } 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric void RegisterFile::initialize(const MCSchedModel &SM, unsigned NumRegs) { 340b57cec5SDimitry Andric // Create a default register file that "sees" all the machine registers 350b57cec5SDimitry Andric // declared by the target. The number of physical registers in the default 360b57cec5SDimitry Andric // register file is set equal to `NumRegs`. A value of zero for `NumRegs` 370b57cec5SDimitry Andric // means: this register file has an unbounded number of physical registers. 380b57cec5SDimitry Andric RegisterFiles.emplace_back(NumRegs); 390b57cec5SDimitry Andric if (!SM.hasExtraProcessorInfo()) 400b57cec5SDimitry Andric return; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric // For each user defined register file, allocate a RegisterMappingTracker 430b57cec5SDimitry Andric // object. The size of every register file, as well as the mapping between 440b57cec5SDimitry Andric // register files and register classes is specified via tablegen. 450b57cec5SDimitry Andric const MCExtraProcessorInfo &Info = SM.getExtraProcessorInfo(); 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric // Skip invalid register file at index 0. 480b57cec5SDimitry Andric for (unsigned I = 1, E = Info.NumRegisterFiles; I < E; ++I) { 490b57cec5SDimitry Andric const MCRegisterFileDesc &RF = Info.RegisterFiles[I]; 500b57cec5SDimitry Andric assert(RF.NumPhysRegs && "Invalid PRF with zero physical registers!"); 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric // The cost of a register definition is equivalent to the number of 530b57cec5SDimitry Andric // physical registers that are allocated at register renaming stage. 540b57cec5SDimitry Andric unsigned Length = RF.NumRegisterCostEntries; 550b57cec5SDimitry Andric const MCRegisterCostEntry *FirstElt = 560b57cec5SDimitry Andric &Info.RegisterCostTable[RF.RegisterCostEntryIdx]; 570b57cec5SDimitry Andric addRegisterFile(RF, ArrayRef<MCRegisterCostEntry>(FirstElt, Length)); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric void RegisterFile::cycleStart() { 620b57cec5SDimitry Andric for (RegisterMappingTracker &RMT : RegisterFiles) 630b57cec5SDimitry Andric RMT.NumMoveEliminated = 0; 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric void RegisterFile::addRegisterFile(const MCRegisterFileDesc &RF, 670b57cec5SDimitry Andric ArrayRef<MCRegisterCostEntry> Entries) { 680b57cec5SDimitry Andric // A default register file is always allocated at index #0. That register file 690b57cec5SDimitry Andric // is mainly used to count the total number of mappings created by all 700b57cec5SDimitry Andric // register files at runtime. Users can limit the number of available physical 710b57cec5SDimitry Andric // registers in register file #0 through the command line flag 720b57cec5SDimitry Andric // `-register-file-size`. 730b57cec5SDimitry Andric unsigned RegisterFileIndex = RegisterFiles.size(); 740b57cec5SDimitry Andric RegisterFiles.emplace_back(RF.NumPhysRegs, RF.MaxMovesEliminatedPerCycle, 750b57cec5SDimitry Andric RF.AllowZeroMoveEliminationOnly); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric // Special case where there is no register class identifier in the set. 780b57cec5SDimitry Andric // An empty set of register classes means: this register file contains all 790b57cec5SDimitry Andric // the physical registers specified by the target. 800b57cec5SDimitry Andric // We optimistically assume that a register can be renamed at the cost of a 810b57cec5SDimitry Andric // single physical register. The constructor of RegisterFile ensures that 820b57cec5SDimitry Andric // a RegisterMapping exists for each logical register defined by the Target. 830b57cec5SDimitry Andric if (Entries.empty()) 840b57cec5SDimitry Andric return; 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric // Now update the cost of individual registers. 870b57cec5SDimitry Andric for (const MCRegisterCostEntry &RCE : Entries) { 880b57cec5SDimitry Andric const MCRegisterClass &RC = MRI.getRegClass(RCE.RegisterClassID); 890b57cec5SDimitry Andric for (const MCPhysReg Reg : RC) { 900b57cec5SDimitry Andric RegisterRenamingInfo &Entry = RegisterMappings[Reg].second; 910b57cec5SDimitry Andric IndexPlusCostPairTy &IPC = Entry.IndexPlusCost; 920b57cec5SDimitry Andric if (IPC.first && IPC.first != RegisterFileIndex) { 930b57cec5SDimitry Andric // The only register file that is allowed to overlap is the default 940b57cec5SDimitry Andric // register file at index #0. The analysis is inaccurate if register 950b57cec5SDimitry Andric // files overlap. 960b57cec5SDimitry Andric errs() << "warning: register " << MRI.getName(Reg) 970b57cec5SDimitry Andric << " defined in multiple register files."; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric IPC = std::make_pair(RegisterFileIndex, RCE.Cost); 1000b57cec5SDimitry Andric Entry.RenameAs = Reg; 1010b57cec5SDimitry Andric Entry.AllowMoveElimination = RCE.AllowMoveElimination; 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric // Assume the same cost for each sub-register. 1040b57cec5SDimitry Andric for (MCSubRegIterator I(Reg, &MRI); I.isValid(); ++I) { 1050b57cec5SDimitry Andric RegisterRenamingInfo &OtherEntry = RegisterMappings[*I].second; 1060b57cec5SDimitry Andric if (!OtherEntry.IndexPlusCost.first && 1070b57cec5SDimitry Andric (!OtherEntry.RenameAs || 1080b57cec5SDimitry Andric MRI.isSuperRegister(*I, OtherEntry.RenameAs))) { 1090b57cec5SDimitry Andric OtherEntry.IndexPlusCost = IPC; 1100b57cec5SDimitry Andric OtherEntry.RenameAs = Reg; 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric } 1130b57cec5SDimitry Andric } 1140b57cec5SDimitry Andric } 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric void RegisterFile::allocatePhysRegs(const RegisterRenamingInfo &Entry, 1180b57cec5SDimitry Andric MutableArrayRef<unsigned> UsedPhysRegs) { 1190b57cec5SDimitry Andric unsigned RegisterFileIndex = Entry.IndexPlusCost.first; 1200b57cec5SDimitry Andric unsigned Cost = Entry.IndexPlusCost.second; 1210b57cec5SDimitry Andric if (RegisterFileIndex) { 1220b57cec5SDimitry Andric RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; 1230b57cec5SDimitry Andric RMT.NumUsedPhysRegs += Cost; 1240b57cec5SDimitry Andric UsedPhysRegs[RegisterFileIndex] += Cost; 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric // Now update the default register mapping tracker. 1280b57cec5SDimitry Andric RegisterFiles[0].NumUsedPhysRegs += Cost; 1290b57cec5SDimitry Andric UsedPhysRegs[0] += Cost; 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric void RegisterFile::freePhysRegs(const RegisterRenamingInfo &Entry, 1330b57cec5SDimitry Andric MutableArrayRef<unsigned> FreedPhysRegs) { 1340b57cec5SDimitry Andric unsigned RegisterFileIndex = Entry.IndexPlusCost.first; 1350b57cec5SDimitry Andric unsigned Cost = Entry.IndexPlusCost.second; 1360b57cec5SDimitry Andric if (RegisterFileIndex) { 1370b57cec5SDimitry Andric RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; 1380b57cec5SDimitry Andric RMT.NumUsedPhysRegs -= Cost; 1390b57cec5SDimitry Andric FreedPhysRegs[RegisterFileIndex] += Cost; 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric // Now update the default register mapping tracker. 1430b57cec5SDimitry Andric RegisterFiles[0].NumUsedPhysRegs -= Cost; 1440b57cec5SDimitry Andric FreedPhysRegs[0] += Cost; 1450b57cec5SDimitry Andric } 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric void RegisterFile::addRegisterWrite(WriteRef Write, 1480b57cec5SDimitry Andric MutableArrayRef<unsigned> UsedPhysRegs) { 1490b57cec5SDimitry Andric WriteState &WS = *Write.getWriteState(); 1508bcb0991SDimitry Andric MCPhysReg RegID = WS.getRegisterID(); 1510b57cec5SDimitry Andric assert(RegID && "Adding an invalid register definition?"); 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric LLVM_DEBUG({ 1540b57cec5SDimitry Andric dbgs() << "RegisterFile: addRegisterWrite [ " << Write.getSourceIndex() 1550b57cec5SDimitry Andric << ", " << MRI.getName(RegID) << "]\n"; 1560b57cec5SDimitry Andric }); 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric // If RenameAs is equal to RegID, then RegID is subject to register renaming 1590b57cec5SDimitry Andric // and false dependencies on RegID are all eliminated. 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric // If RenameAs references the invalid register, then we optimistically assume 1620b57cec5SDimitry Andric // that it can be renamed. In the absence of tablegen descriptors for register 1630b57cec5SDimitry Andric // files, RenameAs is always set to the invalid register ID. In all other 1640b57cec5SDimitry Andric // cases, RenameAs must be either equal to RegID, or it must reference a 1650b57cec5SDimitry Andric // super-register of RegID. 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric // If RenameAs is a super-register of RegID, then a write to RegID has always 1680b57cec5SDimitry Andric // a false dependency on RenameAs. The only exception is for when the write 1690b57cec5SDimitry Andric // implicitly clears the upper portion of the underlying register. 1700b57cec5SDimitry Andric // If a write clears its super-registers, then it is renamed as `RenameAs`. 1710b57cec5SDimitry Andric bool IsWriteZero = WS.isWriteZero(); 1720b57cec5SDimitry Andric bool IsEliminated = WS.isEliminated(); 1730b57cec5SDimitry Andric bool ShouldAllocatePhysRegs = !IsWriteZero && !IsEliminated; 1740b57cec5SDimitry Andric const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second; 1750b57cec5SDimitry Andric WS.setPRF(RRI.IndexPlusCost.first); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric if (RRI.RenameAs && RRI.RenameAs != RegID) { 1780b57cec5SDimitry Andric RegID = RRI.RenameAs; 1790b57cec5SDimitry Andric WriteRef &OtherWrite = RegisterMappings[RegID].first; 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric if (!WS.clearsSuperRegisters()) { 1820b57cec5SDimitry Andric // The processor keeps the definition of `RegID` together with register 1830b57cec5SDimitry Andric // `RenameAs`. Since this partial write is not renamed, no physical 1840b57cec5SDimitry Andric // register is allocated. 1850b57cec5SDimitry Andric ShouldAllocatePhysRegs = false; 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric WriteState *OtherWS = OtherWrite.getWriteState(); 1880b57cec5SDimitry Andric if (OtherWS && (OtherWrite.getSourceIndex() != Write.getSourceIndex())) { 1890b57cec5SDimitry Andric // This partial write has a false dependency on RenameAs. 1900b57cec5SDimitry Andric assert(!IsEliminated && "Unexpected partial update!"); 1910b57cec5SDimitry Andric OtherWS->addUser(OtherWrite.getSourceIndex(), &WS); 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric // Update zero registers. 1978bcb0991SDimitry Andric MCPhysReg ZeroRegisterID = 1980b57cec5SDimitry Andric WS.clearsSuperRegisters() ? RegID : WS.getRegisterID(); 199*e8d8bef9SDimitry Andric ZeroRegisters.setBitVal(ZeroRegisterID, IsWriteZero); 2000b57cec5SDimitry Andric for (MCSubRegIterator I(ZeroRegisterID, &MRI); I.isValid(); ++I) 201*e8d8bef9SDimitry Andric ZeroRegisters.setBitVal(*I, IsWriteZero); 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric // If this is move has been eliminated, then the call to tryEliminateMove 2040b57cec5SDimitry Andric // should have already updated all the register mappings. 2050b57cec5SDimitry Andric if (!IsEliminated) { 2060b57cec5SDimitry Andric // Update the mapping for register RegID including its sub-registers. 2070b57cec5SDimitry Andric RegisterMappings[RegID].first = Write; 2080b57cec5SDimitry Andric RegisterMappings[RegID].second.AliasRegID = 0U; 2090b57cec5SDimitry Andric for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { 2100b57cec5SDimitry Andric RegisterMappings[*I].first = Write; 2110b57cec5SDimitry Andric RegisterMappings[*I].second.AliasRegID = 0U; 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric // No physical registers are allocated for instructions that are optimized 2150b57cec5SDimitry Andric // in hardware. For example, zero-latency data-dependency breaking 2160b57cec5SDimitry Andric // instructions don't consume physical registers. 2170b57cec5SDimitry Andric if (ShouldAllocatePhysRegs) 2180b57cec5SDimitry Andric allocatePhysRegs(RegisterMappings[RegID].second, UsedPhysRegs); 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric if (!WS.clearsSuperRegisters()) 2220b57cec5SDimitry Andric return; 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) { 2250b57cec5SDimitry Andric if (!IsEliminated) { 2260b57cec5SDimitry Andric RegisterMappings[*I].first = Write; 2270b57cec5SDimitry Andric RegisterMappings[*I].second.AliasRegID = 0U; 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric 230*e8d8bef9SDimitry Andric ZeroRegisters.setBitVal(*I, IsWriteZero); 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric void RegisterFile::removeRegisterWrite( 2350b57cec5SDimitry Andric const WriteState &WS, MutableArrayRef<unsigned> FreedPhysRegs) { 2360b57cec5SDimitry Andric // Early exit if this write was eliminated. A write eliminated at register 2370b57cec5SDimitry Andric // renaming stage generates an alias, and it is not added to the PRF. 2380b57cec5SDimitry Andric if (WS.isEliminated()) 2390b57cec5SDimitry Andric return; 2400b57cec5SDimitry Andric 2418bcb0991SDimitry Andric MCPhysReg RegID = WS.getRegisterID(); 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric assert(RegID != 0 && "Invalidating an already invalid register?"); 2440b57cec5SDimitry Andric assert(WS.getCyclesLeft() != UNKNOWN_CYCLES && 2450b57cec5SDimitry Andric "Invalidating a write of unknown cycles!"); 2460b57cec5SDimitry Andric assert(WS.getCyclesLeft() <= 0 && "Invalid cycles left for this write!"); 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric bool ShouldFreePhysRegs = !WS.isWriteZero(); 2498bcb0991SDimitry Andric MCPhysReg RenameAs = RegisterMappings[RegID].second.RenameAs; 2500b57cec5SDimitry Andric if (RenameAs && RenameAs != RegID) { 2510b57cec5SDimitry Andric RegID = RenameAs; 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric if (!WS.clearsSuperRegisters()) { 2540b57cec5SDimitry Andric // Keep the definition of `RegID` together with register `RenameAs`. 2550b57cec5SDimitry Andric ShouldFreePhysRegs = false; 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric if (ShouldFreePhysRegs) 2600b57cec5SDimitry Andric freePhysRegs(RegisterMappings[RegID].second, FreedPhysRegs); 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric WriteRef &WR = RegisterMappings[RegID].first; 2630b57cec5SDimitry Andric if (WR.getWriteState() == &WS) 2640b57cec5SDimitry Andric WR.invalidate(); 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { 2670b57cec5SDimitry Andric WriteRef &OtherWR = RegisterMappings[*I].first; 2680b57cec5SDimitry Andric if (OtherWR.getWriteState() == &WS) 2690b57cec5SDimitry Andric OtherWR.invalidate(); 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric if (!WS.clearsSuperRegisters()) 2730b57cec5SDimitry Andric return; 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric for (MCSuperRegIterator I(RegID, &MRI); I.isValid(); ++I) { 2760b57cec5SDimitry Andric WriteRef &OtherWR = RegisterMappings[*I].first; 2770b57cec5SDimitry Andric if (OtherWR.getWriteState() == &WS) 2780b57cec5SDimitry Andric OtherWR.invalidate(); 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric bool RegisterFile::tryEliminateMove(WriteState &WS, ReadState &RS) { 2830b57cec5SDimitry Andric const RegisterMapping &RMFrom = RegisterMappings[RS.getRegisterID()]; 2840b57cec5SDimitry Andric const RegisterMapping &RMTo = RegisterMappings[WS.getRegisterID()]; 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric // From and To must be owned by the same PRF. 2870b57cec5SDimitry Andric const RegisterRenamingInfo &RRIFrom = RMFrom.second; 2880b57cec5SDimitry Andric const RegisterRenamingInfo &RRITo = RMTo.second; 2890b57cec5SDimitry Andric unsigned RegisterFileIndex = RRIFrom.IndexPlusCost.first; 2900b57cec5SDimitry Andric if (RegisterFileIndex != RRITo.IndexPlusCost.first) 2910b57cec5SDimitry Andric return false; 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric // We only allow move elimination for writes that update a full physical 2940b57cec5SDimitry Andric // register. On X86, move elimination is possible with 32-bit general purpose 2950b57cec5SDimitry Andric // registers because writes to those registers are not partial writes. If a 2960b57cec5SDimitry Andric // register move is a partial write, then we conservatively assume that move 2970b57cec5SDimitry Andric // elimination fails, since it would either trigger a partial update, or the 2980b57cec5SDimitry Andric // issue of a merge opcode. 2990b57cec5SDimitry Andric // 3000b57cec5SDimitry Andric // Note that this constraint may be lifted in future. For example, we could 3010b57cec5SDimitry Andric // make this model more flexible, and let users customize the set of registers 3020b57cec5SDimitry Andric // (i.e. register classes) that allow move elimination. 3030b57cec5SDimitry Andric // 3040b57cec5SDimitry Andric // For now, we assume that there is a strong correlation between registers 3050b57cec5SDimitry Andric // that allow move elimination, and how those same registers are renamed in 3060b57cec5SDimitry Andric // hardware. 3070b57cec5SDimitry Andric if (RRITo.RenameAs && RRITo.RenameAs != WS.getRegisterID()) { 3080b57cec5SDimitry Andric // Early exit if the PRF doesn't support move elimination for this register. 3090b57cec5SDimitry Andric if (!RegisterMappings[RRITo.RenameAs].second.AllowMoveElimination) 3100b57cec5SDimitry Andric return false; 3110b57cec5SDimitry Andric if (!WS.clearsSuperRegisters()) 3120b57cec5SDimitry Andric return false; 3130b57cec5SDimitry Andric } 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex]; 3160b57cec5SDimitry Andric if (RMT.MaxMoveEliminatedPerCycle && 3170b57cec5SDimitry Andric RMT.NumMoveEliminated == RMT.MaxMoveEliminatedPerCycle) 3180b57cec5SDimitry Andric return false; 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric bool IsZeroMove = ZeroRegisters[RS.getRegisterID()]; 3210b57cec5SDimitry Andric if (RMT.AllowZeroMoveEliminationOnly && !IsZeroMove) 3220b57cec5SDimitry Andric return false; 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric // Construct an alias. 3250b57cec5SDimitry Andric MCPhysReg AliasedReg = 3260b57cec5SDimitry Andric RRIFrom.RenameAs ? RRIFrom.RenameAs : RS.getRegisterID(); 3270b57cec5SDimitry Andric MCPhysReg AliasReg = RRITo.RenameAs ? RRITo.RenameAs : WS.getRegisterID(); 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric const RegisterRenamingInfo &RMAlias = RegisterMappings[AliasedReg].second; 3300b57cec5SDimitry Andric if (RMAlias.AliasRegID) 3310b57cec5SDimitry Andric AliasedReg = RMAlias.AliasRegID; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric RegisterMappings[AliasReg].second.AliasRegID = AliasedReg; 3340b57cec5SDimitry Andric for (MCSubRegIterator I(AliasReg, &MRI); I.isValid(); ++I) 3350b57cec5SDimitry Andric RegisterMappings[*I].second.AliasRegID = AliasedReg; 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric if (IsZeroMove) { 3380b57cec5SDimitry Andric WS.setWriteZero(); 3390b57cec5SDimitry Andric RS.setReadZero(); 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric WS.setEliminated(); 3420b57cec5SDimitry Andric RMT.NumMoveEliminated++; 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric return true; 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric void RegisterFile::collectWrites(const ReadState &RS, 3480b57cec5SDimitry Andric SmallVectorImpl<WriteRef> &Writes) const { 3498bcb0991SDimitry Andric MCPhysReg RegID = RS.getRegisterID(); 3500b57cec5SDimitry Andric assert(RegID && RegID < RegisterMappings.size()); 3510b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "RegisterFile: collecting writes for register " 3520b57cec5SDimitry Andric << MRI.getName(RegID) << '\n'); 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric // Check if this is an alias. 3550b57cec5SDimitry Andric const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second; 3560b57cec5SDimitry Andric if (RRI.AliasRegID) 3570b57cec5SDimitry Andric RegID = RRI.AliasRegID; 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric const WriteRef &WR = RegisterMappings[RegID].first; 3600b57cec5SDimitry Andric if (WR.isValid()) 3610b57cec5SDimitry Andric Writes.push_back(WR); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric // Handle potential partial register updates. 3640b57cec5SDimitry Andric for (MCSubRegIterator I(RegID, &MRI); I.isValid(); ++I) { 3650b57cec5SDimitry Andric const WriteRef &WR = RegisterMappings[*I].first; 3660b57cec5SDimitry Andric if (WR.isValid()) 3670b57cec5SDimitry Andric Writes.push_back(WR); 3680b57cec5SDimitry Andric } 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric // Remove duplicate entries and resize the input vector. 3710b57cec5SDimitry Andric if (Writes.size() > 1) { 3720b57cec5SDimitry Andric sort(Writes, [](const WriteRef &Lhs, const WriteRef &Rhs) { 3730b57cec5SDimitry Andric return Lhs.getWriteState() < Rhs.getWriteState(); 3740b57cec5SDimitry Andric }); 3750b57cec5SDimitry Andric auto It = std::unique(Writes.begin(), Writes.end()); 3760b57cec5SDimitry Andric Writes.resize(std::distance(Writes.begin(), It)); 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric LLVM_DEBUG({ 3800b57cec5SDimitry Andric for (const WriteRef &WR : Writes) { 3810b57cec5SDimitry Andric const WriteState &WS = *WR.getWriteState(); 3820b57cec5SDimitry Andric dbgs() << "[PRF] Found a dependent use of Register " 3830b57cec5SDimitry Andric << MRI.getName(WS.getRegisterID()) << " (defined by instruction #" 3840b57cec5SDimitry Andric << WR.getSourceIndex() << ")\n"; 3850b57cec5SDimitry Andric } 3860b57cec5SDimitry Andric }); 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric void RegisterFile::addRegisterRead(ReadState &RS, 3900b57cec5SDimitry Andric const MCSubtargetInfo &STI) const { 3918bcb0991SDimitry Andric MCPhysReg RegID = RS.getRegisterID(); 3920b57cec5SDimitry Andric const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second; 3930b57cec5SDimitry Andric RS.setPRF(RRI.IndexPlusCost.first); 3940b57cec5SDimitry Andric if (RS.isIndependentFromDef()) 3950b57cec5SDimitry Andric return; 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric if (ZeroRegisters[RS.getRegisterID()]) 3980b57cec5SDimitry Andric RS.setReadZero(); 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric SmallVector<WriteRef, 4> DependentWrites; 4010b57cec5SDimitry Andric collectWrites(RS, DependentWrites); 4020b57cec5SDimitry Andric RS.setDependentWrites(DependentWrites.size()); 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric // We know that this read depends on all the writes in DependentWrites. 4050b57cec5SDimitry Andric // For each write, check if we have ReadAdvance information, and use it 4060b57cec5SDimitry Andric // to figure out in how many cycles this read becomes available. 4070b57cec5SDimitry Andric const ReadDescriptor &RD = RS.getDescriptor(); 4080b57cec5SDimitry Andric const MCSchedModel &SM = STI.getSchedModel(); 4090b57cec5SDimitry Andric const MCSchedClassDesc *SC = SM.getSchedClassDesc(RD.SchedClassID); 4100b57cec5SDimitry Andric for (WriteRef &WR : DependentWrites) { 4110b57cec5SDimitry Andric WriteState &WS = *WR.getWriteState(); 4120b57cec5SDimitry Andric unsigned WriteResID = WS.getWriteResourceID(); 4130b57cec5SDimitry Andric int ReadAdvance = STI.getReadAdvanceCycles(SC, RD.UseIndex, WriteResID); 4140b57cec5SDimitry Andric WS.addUser(WR.getSourceIndex(), &RS, ReadAdvance); 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric 4188bcb0991SDimitry Andric unsigned RegisterFile::isAvailable(ArrayRef<MCPhysReg> Regs) const { 4190b57cec5SDimitry Andric SmallVector<unsigned, 4> NumPhysRegs(getNumRegisterFiles()); 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // Find how many new mappings must be created for each register file. 4228bcb0991SDimitry Andric for (const MCPhysReg RegID : Regs) { 4230b57cec5SDimitry Andric const RegisterRenamingInfo &RRI = RegisterMappings[RegID].second; 4240b57cec5SDimitry Andric const IndexPlusCostPairTy &Entry = RRI.IndexPlusCost; 4250b57cec5SDimitry Andric if (Entry.first) 4260b57cec5SDimitry Andric NumPhysRegs[Entry.first] += Entry.second; 4270b57cec5SDimitry Andric NumPhysRegs[0] += Entry.second; 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric unsigned Response = 0; 4310b57cec5SDimitry Andric for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { 4320b57cec5SDimitry Andric unsigned NumRegs = NumPhysRegs[I]; 4330b57cec5SDimitry Andric if (!NumRegs) 4340b57cec5SDimitry Andric continue; 4350b57cec5SDimitry Andric 4360b57cec5SDimitry Andric const RegisterMappingTracker &RMT = RegisterFiles[I]; 4370b57cec5SDimitry Andric if (!RMT.NumPhysRegs) { 4380b57cec5SDimitry Andric // The register file has an unbounded number of microarchitectural 4390b57cec5SDimitry Andric // registers. 4400b57cec5SDimitry Andric continue; 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric if (RMT.NumPhysRegs < NumRegs) { 4440b57cec5SDimitry Andric // The current register file is too small. This may occur if the number of 4450b57cec5SDimitry Andric // microarchitectural registers in register file #0 was changed by the 4460b57cec5SDimitry Andric // users via flag -reg-file-size. Alternatively, the scheduling model 4470b57cec5SDimitry Andric // specified a too small number of registers for this register file. 4480b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Not enough registers in the register file.\n"); 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric // FIXME: Normalize the instruction register count to match the 4510b57cec5SDimitry Andric // NumPhysRegs value. This is a highly unusual case, and is not expected 4520b57cec5SDimitry Andric // to occur. This normalization is hiding an inconsistency in either the 4530b57cec5SDimitry Andric // scheduling model or in the value that the user might have specified 4540b57cec5SDimitry Andric // for NumPhysRegs. 4550b57cec5SDimitry Andric NumRegs = RMT.NumPhysRegs; 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric if (RMT.NumPhysRegs < (RMT.NumUsedPhysRegs + NumRegs)) 4590b57cec5SDimitry Andric Response |= (1U << I); 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric return Response; 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric #ifndef NDEBUG 4660b57cec5SDimitry Andric void RegisterFile::dump() const { 4670b57cec5SDimitry Andric for (unsigned I = 0, E = MRI.getNumRegs(); I < E; ++I) { 4680b57cec5SDimitry Andric const RegisterMapping &RM = RegisterMappings[I]; 4690b57cec5SDimitry Andric const RegisterRenamingInfo &RRI = RM.second; 4700b57cec5SDimitry Andric if (ZeroRegisters[I]) { 4710b57cec5SDimitry Andric dbgs() << MRI.getName(I) << ", " << I 4720b57cec5SDimitry Andric << ", PRF=" << RRI.IndexPlusCost.first 4730b57cec5SDimitry Andric << ", Cost=" << RRI.IndexPlusCost.second 4740b57cec5SDimitry Andric << ", RenameAs=" << RRI.RenameAs << ", IsZero=" << ZeroRegisters[I] 4750b57cec5SDimitry Andric << ","; 4760b57cec5SDimitry Andric RM.first.dump(); 4770b57cec5SDimitry Andric dbgs() << '\n'; 4780b57cec5SDimitry Andric } 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) { 4820b57cec5SDimitry Andric dbgs() << "Register File #" << I; 4830b57cec5SDimitry Andric const RegisterMappingTracker &RMT = RegisterFiles[I]; 4840b57cec5SDimitry Andric dbgs() << "\n TotalMappings: " << RMT.NumPhysRegs 4850b57cec5SDimitry Andric << "\n NumUsedMappings: " << RMT.NumUsedPhysRegs << '\n'; 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric #endif 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric } // namespace mca 4910b57cec5SDimitry Andric } // namespace llvm 492