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
runOnMachineFunction(const MachineFunction & mf)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.
83480093f4SDimitry Andric CalleeSavedAliases.assign(TRI->getNumRegs(), 0);
84bdd1243dSDimitry Andric for (const MCPhysReg *I = CSR; *I; ++I) {
850b57cec5SDimitry Andric for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI)
860b57cec5SDimitry Andric CalleeSavedAliases[*AI] = *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);
9981ad6265SDimitry Andric if (IgnoreCSRForAllocOrder.size() != CSRHintsForAllocOrder.size() ||
10081ad6265SDimitry Andric IgnoreCSRForAllocOrder != CSRHintsForAllocOrder) {
10181ad6265SDimitry Andric Update = true;
10281ad6265SDimitry Andric IgnoreCSRForAllocOrder = CSRHintsForAllocOrder;
10381ad6265SDimitry Andric }
10481ad6265SDimitry Andric
105fe6060f1SDimitry Andric RegCosts = TRI->getRegisterCosts(*MF);
106fe6060f1SDimitry Andric
1070b57cec5SDimitry Andric // Different reserved registers?
1080b57cec5SDimitry Andric const BitVector &RR = MF->getRegInfo().getReservedRegs();
1090b57cec5SDimitry Andric if (Reserved.size() != RR.size() || RR != Reserved) {
1100b57cec5SDimitry Andric Update = true;
1110b57cec5SDimitry Andric Reserved = RR;
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric // Invalidate cached information from previous function.
1150b57cec5SDimitry Andric if (Update) {
1160b57cec5SDimitry Andric unsigned NumPSets = TRI->getNumRegPressureSets();
1170b57cec5SDimitry Andric PSetLimits.reset(new unsigned[NumPSets]);
1180b57cec5SDimitry Andric std::fill(&PSetLimits[0], &PSetLimits[NumPSets], 0);
1190b57cec5SDimitry Andric ++Tag;
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric /// compute - Compute the preferred allocation order for RC with reserved
1240b57cec5SDimitry Andric /// registers filtered out. Volatile registers come first followed by CSR
1250b57cec5SDimitry Andric /// aliases ordered according to the CSR order specified by the target.
compute(const TargetRegisterClass * RC) const1260b57cec5SDimitry Andric void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
1270b57cec5SDimitry Andric assert(RC && "no register class given");
1280b57cec5SDimitry Andric RCInfo &RCI = RegClass[RC->getID()];
1290b57cec5SDimitry Andric auto &STI = MF->getSubtarget();
1300b57cec5SDimitry Andric
1310b57cec5SDimitry Andric // Raw register count, including all reserved regs.
1320b57cec5SDimitry Andric unsigned NumRegs = RC->getNumRegs();
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric if (!RCI.Order)
1350b57cec5SDimitry Andric RCI.Order.reset(new MCPhysReg[NumRegs]);
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric unsigned N = 0;
1380b57cec5SDimitry Andric SmallVector<MCPhysReg, 16> CSRAlias;
139fe6060f1SDimitry Andric uint8_t MinCost = uint8_t(~0u);
140fe6060f1SDimitry Andric uint8_t LastCost = uint8_t(~0u);
1410b57cec5SDimitry Andric unsigned LastCostChange = 0;
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric // FIXME: Once targets reserve registers instead of removing them from the
1440b57cec5SDimitry Andric // allocation order, we can simply use begin/end here.
1450b57cec5SDimitry Andric ArrayRef<MCPhysReg> RawOrder = RC->getRawAllocationOrder(*MF);
1460eae32dcSDimitry Andric for (unsigned PhysReg : RawOrder) {
1470b57cec5SDimitry Andric // Remove reserved registers from the allocation order.
1480b57cec5SDimitry Andric if (Reserved.test(PhysReg))
1490b57cec5SDimitry Andric continue;
150fe6060f1SDimitry Andric uint8_t Cost = RegCosts[PhysReg];
1510b57cec5SDimitry Andric MinCost = std::min(MinCost, Cost);
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric if (CalleeSavedAliases[PhysReg] &&
1540b57cec5SDimitry Andric !STI.ignoreCSRForAllocationOrder(*MF, PhysReg))
1550b57cec5SDimitry Andric // PhysReg aliases a CSR, save it for later.
1560b57cec5SDimitry Andric CSRAlias.push_back(PhysReg);
1570b57cec5SDimitry Andric else {
1580b57cec5SDimitry Andric if (Cost != LastCost)
1590b57cec5SDimitry Andric LastCostChange = N;
1600b57cec5SDimitry Andric RCI.Order[N++] = PhysReg;
1610b57cec5SDimitry Andric LastCost = Cost;
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric RCI.NumRegs = N + CSRAlias.size();
1650b57cec5SDimitry Andric assert(RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric // CSR aliases go after the volatile registers, preserve the target's order.
168*e710425bSDimitry Andric for (unsigned PhysReg : CSRAlias) {
169fe6060f1SDimitry Andric uint8_t Cost = RegCosts[PhysReg];
1700b57cec5SDimitry Andric if (Cost != LastCost)
1710b57cec5SDimitry Andric LastCostChange = N;
1720b57cec5SDimitry Andric RCI.Order[N++] = PhysReg;
1730b57cec5SDimitry Andric LastCost = Cost;
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric // Register allocator stress test. Clip register class to N registers.
1770b57cec5SDimitry Andric if (StressRA && RCI.NumRegs > StressRA)
1780b57cec5SDimitry Andric RCI.NumRegs = StressRA;
1790b57cec5SDimitry Andric
1800b57cec5SDimitry Andric // Check if RC is a proper sub-class.
1810b57cec5SDimitry Andric if (const TargetRegisterClass *Super =
1820b57cec5SDimitry Andric TRI->getLargestLegalSuperClass(RC, *MF))
1830b57cec5SDimitry Andric if (Super != RC && getNumAllocatableRegs(Super) > RCI.NumRegs)
1840b57cec5SDimitry Andric RCI.ProperSubClass = true;
1850b57cec5SDimitry Andric
186fe6060f1SDimitry Andric RCI.MinCost = MinCost;
1870b57cec5SDimitry Andric RCI.LastCostChange = LastCostChange;
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andric LLVM_DEBUG({
1900b57cec5SDimitry Andric dbgs() << "AllocationOrder(" << TRI->getRegClassName(RC) << ") = [";
1910b57cec5SDimitry Andric for (unsigned I = 0; I != RCI.NumRegs; ++I)
1920b57cec5SDimitry Andric dbgs() << ' ' << printReg(RCI.Order[I], TRI);
1930b57cec5SDimitry Andric dbgs() << (RCI.ProperSubClass ? " ] (sub-class)\n" : " ]\n");
1940b57cec5SDimitry Andric });
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric // RCI is now up-to-date.
1970b57cec5SDimitry Andric RCI.Tag = Tag;
1980b57cec5SDimitry Andric }
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andric /// This is not accurate because two overlapping register sets may have some
2010b57cec5SDimitry Andric /// nonoverlapping reserved registers. However, computing the allocation order
2020b57cec5SDimitry Andric /// for all register classes would be too expensive.
computePSetLimit(unsigned Idx) const2030b57cec5SDimitry Andric unsigned RegisterClassInfo::computePSetLimit(unsigned Idx) const {
2040b57cec5SDimitry Andric const TargetRegisterClass *RC = nullptr;
2050b57cec5SDimitry Andric unsigned NumRCUnits = 0;
2060b57cec5SDimitry Andric for (const TargetRegisterClass *C : TRI->regclasses()) {
2070b57cec5SDimitry Andric const int *PSetID = TRI->getRegClassPressureSets(C);
2080b57cec5SDimitry Andric for (; *PSetID != -1; ++PSetID) {
2090b57cec5SDimitry Andric if ((unsigned)*PSetID == Idx)
2100b57cec5SDimitry Andric break;
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric if (*PSetID == -1)
2130b57cec5SDimitry Andric continue;
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric // Found a register class that counts against this pressure set.
2160b57cec5SDimitry Andric // For efficiency, only compute the set order for the largest set.
2170b57cec5SDimitry Andric unsigned NUnits = TRI->getRegClassWeight(C).WeightLimit;
2180b57cec5SDimitry Andric if (!RC || NUnits > NumRCUnits) {
2190b57cec5SDimitry Andric RC = C;
2200b57cec5SDimitry Andric NumRCUnits = NUnits;
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric }
223480093f4SDimitry Andric assert(RC && "Failed to find register class");
2240b57cec5SDimitry Andric compute(RC);
225e8d8bef9SDimitry Andric unsigned NAllocatableRegs = getNumAllocatableRegs(RC);
226e8d8bef9SDimitry Andric unsigned RegPressureSetLimit = TRI->getRegPressureSetLimit(*MF, Idx);
227e8d8bef9SDimitry Andric // If all the regs are reserved, return raw RegPressureSetLimit.
228e8d8bef9SDimitry Andric // One example is VRSAVERC in PowerPC.
229e8d8bef9SDimitry Andric // Avoid returning zero, getRegPressureSetLimit(Idx) assumes computePSetLimit
230e8d8bef9SDimitry Andric // return non-zero value.
231e8d8bef9SDimitry Andric if (NAllocatableRegs == 0)
232e8d8bef9SDimitry Andric return RegPressureSetLimit;
233e8d8bef9SDimitry Andric unsigned NReserved = RC->getNumRegs() - NAllocatableRegs;
234e8d8bef9SDimitry Andric return RegPressureSetLimit - TRI->getRegClassWeight(RC).RegWeight * NReserved;
2350b57cec5SDimitry Andric }
236