xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/RegisterClassInfo.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- RegisterClassInfo.cpp - Dynamic Register Class Info ----------------===//
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 //
90b57cec5SDimitry Andric // This file implements the RegisterClassInfo class which provides dynamic
100b57cec5SDimitry Andric // information about target register classes. Callee-saved vs. caller-saved and
110b57cec5SDimitry Andric // reserved registers depend on calling conventions and other dynamic
120b57cec5SDimitry Andric // information, so some things cannot be determined statically.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
170b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
180b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
190b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
250b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
260b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
270b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
280b57cec5SDimitry Andric #include <algorithm>
290b57cec5SDimitry Andric #include <cassert>
300b57cec5SDimitry Andric #include <cstdint>
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric using namespace llvm;
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric #define DEBUG_TYPE "regalloc"
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric static cl::opt<unsigned>
370b57cec5SDimitry Andric StressRA("stress-regalloc", cl::Hidden, cl::init(0), cl::value_desc("N"),
380b57cec5SDimitry Andric          cl::desc("Limit all regclasses to N registers"));
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric RegisterClassInfo::RegisterClassInfo() = default;
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric void RegisterClassInfo::runOnMachineFunction(const MachineFunction &mf) {
430b57cec5SDimitry Andric   bool Update = false;
440b57cec5SDimitry Andric   MF = &mf;
450b57cec5SDimitry Andric 
4681ad6265SDimitry Andric   auto &STI = MF->getSubtarget();
4781ad6265SDimitry Andric 
480b57cec5SDimitry Andric   // Allocate new array the first time we see a new target.
4981ad6265SDimitry Andric   if (STI.getRegisterInfo() != TRI) {
5081ad6265SDimitry Andric     TRI = STI.getRegisterInfo();
510b57cec5SDimitry Andric     RegClass.reset(new RCInfo[TRI->getNumRegClasses()]);
520b57cec5SDimitry Andric     Update = true;
530b57cec5SDimitry Andric   }
540b57cec5SDimitry Andric 
55bdd1243dSDimitry Andric   // Test if CSRs have changed from the previous function.
56bdd1243dSDimitry Andric   const MachineRegisterInfo &MRI = MF->getRegInfo();
57bdd1243dSDimitry Andric   const MCPhysReg *CSR = MRI.getCalleeSavedRegs();
58bdd1243dSDimitry Andric   bool CSRChanged = true;
59bdd1243dSDimitry Andric   if (!Update) {
60bdd1243dSDimitry Andric     CSRChanged = false;
61bdd1243dSDimitry Andric     size_t LastSize = LastCalleeSavedRegs.size();
62bdd1243dSDimitry Andric     for (unsigned I = 0;; ++I) {
63bdd1243dSDimitry Andric       if (CSR[I] == 0) {
64bdd1243dSDimitry Andric         CSRChanged = I != LastSize;
65bdd1243dSDimitry Andric         break;
66bdd1243dSDimitry Andric       }
67bdd1243dSDimitry Andric       if (I >= LastSize) {
68bdd1243dSDimitry Andric         CSRChanged = true;
69bdd1243dSDimitry Andric         break;
70bdd1243dSDimitry Andric       }
71bdd1243dSDimitry Andric       if (CSR[I] != LastCalleeSavedRegs[I]) {
72bdd1243dSDimitry Andric         CSRChanged = true;
73bdd1243dSDimitry Andric         break;
74bdd1243dSDimitry Andric       }
75bdd1243dSDimitry Andric     }
76bdd1243dSDimitry Andric   }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   // Get the callee saved registers.
79bdd1243dSDimitry Andric   if (CSRChanged) {
80bdd1243dSDimitry Andric     LastCalleeSavedRegs.clear();
810b57cec5SDimitry Andric     // Build a CSRAlias map. Every CSR alias saves the last
820b57cec5SDimitry Andric     // overlapping CSR.
83*0fca6ea1SDimitry Andric     CalleeSavedAliases.assign(TRI->getNumRegUnits(), 0);
84bdd1243dSDimitry Andric     for (const MCPhysReg *I = CSR; *I; ++I) {
85*0fca6ea1SDimitry Andric       for (MCRegUnit U : TRI->regunits(*I))
86*0fca6ea1SDimitry Andric         CalleeSavedAliases[U] = *I;
87bdd1243dSDimitry Andric       LastCalleeSavedRegs.push_back(*I);
88bdd1243dSDimitry Andric     }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric     Update = true;
910b57cec5SDimitry Andric   }
920b57cec5SDimitry Andric 
9381ad6265SDimitry Andric   // Even if CSR list is same, we could have had a different allocation order
9481ad6265SDimitry Andric   // if ignoreCSRForAllocationOrder is evaluated differently.
9581ad6265SDimitry Andric   BitVector CSRHintsForAllocOrder(TRI->getNumRegs());
9681ad6265SDimitry Andric   for (const MCPhysReg *I = CSR; *I; ++I)
9781ad6265SDimitry Andric     for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI)
9881ad6265SDimitry Andric       CSRHintsForAllocOrder[*AI] = STI.ignoreCSRForAllocationOrder(mf, *AI);
99*0fca6ea1SDimitry Andric   if (IgnoreCSRForAllocOrder != CSRHintsForAllocOrder) {
10081ad6265SDimitry Andric     Update = true;
10181ad6265SDimitry Andric     IgnoreCSRForAllocOrder = CSRHintsForAllocOrder;
10281ad6265SDimitry Andric   }
10381ad6265SDimitry Andric 
104fe6060f1SDimitry Andric   RegCosts = TRI->getRegisterCosts(*MF);
105fe6060f1SDimitry Andric 
1060b57cec5SDimitry Andric   // Different reserved registers?
1070b57cec5SDimitry Andric   const BitVector &RR = MF->getRegInfo().getReservedRegs();
108*0fca6ea1SDimitry Andric   if (RR != Reserved) {
1090b57cec5SDimitry Andric     Update = true;
1100b57cec5SDimitry Andric     Reserved = RR;
1110b57cec5SDimitry Andric   }
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   // Invalidate cached information from previous function.
1140b57cec5SDimitry Andric   if (Update) {
1150b57cec5SDimitry Andric     unsigned NumPSets = TRI->getNumRegPressureSets();
1160b57cec5SDimitry Andric     PSetLimits.reset(new unsigned[NumPSets]);
1170b57cec5SDimitry Andric     std::fill(&PSetLimits[0], &PSetLimits[NumPSets], 0);
1180b57cec5SDimitry Andric     ++Tag;
1190b57cec5SDimitry Andric   }
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric /// compute - Compute the preferred allocation order for RC with reserved
1230b57cec5SDimitry Andric /// registers filtered out. Volatile registers come first followed by CSR
1240b57cec5SDimitry Andric /// aliases ordered according to the CSR order specified by the target.
1250b57cec5SDimitry Andric void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
1260b57cec5SDimitry Andric   assert(RC && "no register class given");
1270b57cec5SDimitry Andric   RCInfo &RCI = RegClass[RC->getID()];
1280b57cec5SDimitry Andric   auto &STI = MF->getSubtarget();
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   // Raw register count, including all reserved regs.
1310b57cec5SDimitry Andric   unsigned NumRegs = RC->getNumRegs();
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   if (!RCI.Order)
1340b57cec5SDimitry Andric     RCI.Order.reset(new MCPhysReg[NumRegs]);
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   unsigned N = 0;
1370b57cec5SDimitry Andric   SmallVector<MCPhysReg, 16> CSRAlias;
138fe6060f1SDimitry Andric   uint8_t MinCost = uint8_t(~0u);
139fe6060f1SDimitry Andric   uint8_t LastCost = uint8_t(~0u);
1400b57cec5SDimitry Andric   unsigned LastCostChange = 0;
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   // FIXME: Once targets reserve registers instead of removing them from the
1430b57cec5SDimitry Andric   // allocation order, we can simply use begin/end here.
1440b57cec5SDimitry Andric   ArrayRef<MCPhysReg> RawOrder = RC->getRawAllocationOrder(*MF);
1450eae32dcSDimitry Andric   for (unsigned PhysReg : RawOrder) {
1460b57cec5SDimitry Andric     // Remove reserved registers from the allocation order.
1470b57cec5SDimitry Andric     if (Reserved.test(PhysReg))
1480b57cec5SDimitry Andric       continue;
149fe6060f1SDimitry Andric     uint8_t Cost = RegCosts[PhysReg];
1500b57cec5SDimitry Andric     MinCost = std::min(MinCost, Cost);
1510b57cec5SDimitry Andric 
152*0fca6ea1SDimitry Andric     if (getLastCalleeSavedAlias(PhysReg) &&
1530b57cec5SDimitry Andric         !STI.ignoreCSRForAllocationOrder(*MF, PhysReg))
1540b57cec5SDimitry Andric       // PhysReg aliases a CSR, save it for later.
1550b57cec5SDimitry Andric       CSRAlias.push_back(PhysReg);
1560b57cec5SDimitry Andric     else {
1570b57cec5SDimitry Andric       if (Cost != LastCost)
1580b57cec5SDimitry Andric         LastCostChange = N;
1590b57cec5SDimitry Andric       RCI.Order[N++] = PhysReg;
1600b57cec5SDimitry Andric       LastCost = Cost;
1610b57cec5SDimitry Andric     }
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric   RCI.NumRegs = N + CSRAlias.size();
1640b57cec5SDimitry Andric   assert(RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   // CSR aliases go after the volatile registers, preserve the target's order.
167cb14a3feSDimitry Andric   for (unsigned PhysReg : CSRAlias) {
168fe6060f1SDimitry Andric     uint8_t Cost = RegCosts[PhysReg];
1690b57cec5SDimitry Andric     if (Cost != LastCost)
1700b57cec5SDimitry Andric       LastCostChange = N;
1710b57cec5SDimitry Andric     RCI.Order[N++] = PhysReg;
1720b57cec5SDimitry Andric     LastCost = Cost;
1730b57cec5SDimitry Andric   }
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   // Register allocator stress test.  Clip register class to N registers.
1760b57cec5SDimitry Andric   if (StressRA && RCI.NumRegs > StressRA)
1770b57cec5SDimitry Andric     RCI.NumRegs = StressRA;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   // Check if RC is a proper sub-class.
1800b57cec5SDimitry Andric   if (const TargetRegisterClass *Super =
1810b57cec5SDimitry Andric           TRI->getLargestLegalSuperClass(RC, *MF))
1820b57cec5SDimitry Andric     if (Super != RC && getNumAllocatableRegs(Super) > RCI.NumRegs)
1830b57cec5SDimitry Andric       RCI.ProperSubClass = true;
1840b57cec5SDimitry Andric 
185fe6060f1SDimitry Andric   RCI.MinCost = MinCost;
1860b57cec5SDimitry Andric   RCI.LastCostChange = LastCostChange;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   LLVM_DEBUG({
1890b57cec5SDimitry Andric     dbgs() << "AllocationOrder(" << TRI->getRegClassName(RC) << ") = [";
1900b57cec5SDimitry Andric     for (unsigned I = 0; I != RCI.NumRegs; ++I)
1910b57cec5SDimitry Andric       dbgs() << ' ' << printReg(RCI.Order[I], TRI);
1920b57cec5SDimitry Andric     dbgs() << (RCI.ProperSubClass ? " ] (sub-class)\n" : " ]\n");
1930b57cec5SDimitry Andric   });
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   // RCI is now up-to-date.
1960b57cec5SDimitry Andric   RCI.Tag = Tag;
1970b57cec5SDimitry Andric }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric /// This is not accurate because two overlapping register sets may have some
2000b57cec5SDimitry Andric /// nonoverlapping reserved registers. However, computing the allocation order
2010b57cec5SDimitry Andric /// for all register classes would be too expensive.
2020b57cec5SDimitry Andric unsigned RegisterClassInfo::computePSetLimit(unsigned Idx) const {
2030b57cec5SDimitry Andric   const TargetRegisterClass *RC = nullptr;
2040b57cec5SDimitry Andric   unsigned NumRCUnits = 0;
2050b57cec5SDimitry Andric   for (const TargetRegisterClass *C : TRI->regclasses()) {
2060b57cec5SDimitry Andric     const int *PSetID = TRI->getRegClassPressureSets(C);
2070b57cec5SDimitry Andric     for (; *PSetID != -1; ++PSetID) {
2080b57cec5SDimitry Andric       if ((unsigned)*PSetID == Idx)
2090b57cec5SDimitry Andric         break;
2100b57cec5SDimitry Andric     }
2110b57cec5SDimitry Andric     if (*PSetID == -1)
2120b57cec5SDimitry Andric       continue;
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric     // Found a register class that counts against this pressure set.
2150b57cec5SDimitry Andric     // For efficiency, only compute the set order for the largest set.
2160b57cec5SDimitry Andric     unsigned NUnits = TRI->getRegClassWeight(C).WeightLimit;
2170b57cec5SDimitry Andric     if (!RC || NUnits > NumRCUnits) {
2180b57cec5SDimitry Andric       RC = C;
2190b57cec5SDimitry Andric       NumRCUnits = NUnits;
2200b57cec5SDimitry Andric     }
2210b57cec5SDimitry Andric   }
222480093f4SDimitry Andric   assert(RC && "Failed to find register class");
2230b57cec5SDimitry Andric   compute(RC);
224e8d8bef9SDimitry Andric   unsigned NAllocatableRegs = getNumAllocatableRegs(RC);
225e8d8bef9SDimitry Andric   unsigned RegPressureSetLimit = TRI->getRegPressureSetLimit(*MF, Idx);
226e8d8bef9SDimitry Andric   // If all the regs are reserved, return raw RegPressureSetLimit.
227e8d8bef9SDimitry Andric   // One example is VRSAVERC in PowerPC.
228e8d8bef9SDimitry Andric   // Avoid returning zero, getRegPressureSetLimit(Idx) assumes computePSetLimit
229e8d8bef9SDimitry Andric   // return non-zero value.
230e8d8bef9SDimitry Andric   if (NAllocatableRegs == 0)
231e8d8bef9SDimitry Andric     return RegPressureSetLimit;
232e8d8bef9SDimitry Andric   unsigned NReserved = RC->getNumRegs() - NAllocatableRegs;
233e8d8bef9SDimitry Andric   return RegPressureSetLimit - TRI->getRegClassWeight(RC).RegWeight * NReserved;
2340b57cec5SDimitry Andric }
235