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/TargetFrameLowering.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
260b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
270b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
280b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
290b57cec5SDimitry Andric #include <algorithm>
300b57cec5SDimitry Andric #include <cassert>
310b57cec5SDimitry Andric #include <cstdint>
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric using namespace llvm;
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric #define DEBUG_TYPE "regalloc"
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric static cl::opt<unsigned>
380b57cec5SDimitry Andric StressRA("stress-regalloc", cl::Hidden, cl::init(0), cl::value_desc("N"),
390b57cec5SDimitry Andric cl::desc("Limit all regclasses to N registers"));
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric RegisterClassInfo::RegisterClassInfo() = default;
420b57cec5SDimitry Andric
runOnMachineFunction(const MachineFunction & mf)430b57cec5SDimitry Andric void RegisterClassInfo::runOnMachineFunction(const MachineFunction &mf) {
440b57cec5SDimitry Andric bool Update = false;
450b57cec5SDimitry Andric MF = &mf;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric // Allocate new array the first time we see a new target.
480b57cec5SDimitry Andric if (MF->getSubtarget().getRegisterInfo() != TRI) {
490b57cec5SDimitry Andric TRI = MF->getSubtarget().getRegisterInfo();
500b57cec5SDimitry Andric RegClass.reset(new RCInfo[TRI->getNumRegClasses()]);
510b57cec5SDimitry Andric Update = true;
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric // Does this MF have different CSRs?
550b57cec5SDimitry Andric assert(TRI && "no register info set");
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric // Get the callee saved registers.
580b57cec5SDimitry Andric const MCPhysReg *CSR = MF->getRegInfo().getCalleeSavedRegs();
590b57cec5SDimitry Andric if (Update || CSR != CalleeSavedRegs) {
600b57cec5SDimitry Andric // Build a CSRAlias map. Every CSR alias saves the last
610b57cec5SDimitry Andric // overlapping CSR.
62480093f4SDimitry Andric CalleeSavedAliases.assign(TRI->getNumRegs(), 0);
630b57cec5SDimitry Andric for (const MCPhysReg *I = CSR; *I; ++I)
640b57cec5SDimitry Andric for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI)
650b57cec5SDimitry Andric CalleeSavedAliases[*AI] = *I;
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric Update = true;
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric CalleeSavedRegs = CSR;
700b57cec5SDimitry Andric
71*5f7ddb14SDimitry Andric RegCosts = TRI->getRegisterCosts(*MF);
72*5f7ddb14SDimitry Andric
730b57cec5SDimitry Andric // Different reserved registers?
740b57cec5SDimitry Andric const BitVector &RR = MF->getRegInfo().getReservedRegs();
750b57cec5SDimitry Andric if (Reserved.size() != RR.size() || RR != Reserved) {
760b57cec5SDimitry Andric Update = true;
770b57cec5SDimitry Andric Reserved = RR;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric // Invalidate cached information from previous function.
810b57cec5SDimitry Andric if (Update) {
820b57cec5SDimitry Andric unsigned NumPSets = TRI->getNumRegPressureSets();
830b57cec5SDimitry Andric PSetLimits.reset(new unsigned[NumPSets]);
840b57cec5SDimitry Andric std::fill(&PSetLimits[0], &PSetLimits[NumPSets], 0);
850b57cec5SDimitry Andric ++Tag;
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric /// compute - Compute the preferred allocation order for RC with reserved
900b57cec5SDimitry Andric /// registers filtered out. Volatile registers come first followed by CSR
910b57cec5SDimitry Andric /// aliases ordered according to the CSR order specified by the target.
compute(const TargetRegisterClass * RC) const920b57cec5SDimitry Andric void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
930b57cec5SDimitry Andric assert(RC && "no register class given");
940b57cec5SDimitry Andric RCInfo &RCI = RegClass[RC->getID()];
950b57cec5SDimitry Andric auto &STI = MF->getSubtarget();
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric // Raw register count, including all reserved regs.
980b57cec5SDimitry Andric unsigned NumRegs = RC->getNumRegs();
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric if (!RCI.Order)
1010b57cec5SDimitry Andric RCI.Order.reset(new MCPhysReg[NumRegs]);
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric unsigned N = 0;
1040b57cec5SDimitry Andric SmallVector<MCPhysReg, 16> CSRAlias;
105*5f7ddb14SDimitry Andric uint8_t MinCost = uint8_t(~0u);
106*5f7ddb14SDimitry Andric uint8_t LastCost = uint8_t(~0u);
1070b57cec5SDimitry Andric unsigned LastCostChange = 0;
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric // FIXME: Once targets reserve registers instead of removing them from the
1100b57cec5SDimitry Andric // allocation order, we can simply use begin/end here.
1110b57cec5SDimitry Andric ArrayRef<MCPhysReg> RawOrder = RC->getRawAllocationOrder(*MF);
1120b57cec5SDimitry Andric for (unsigned i = 0; i != RawOrder.size(); ++i) {
1130b57cec5SDimitry Andric unsigned PhysReg = RawOrder[i];
1140b57cec5SDimitry Andric // Remove reserved registers from the allocation order.
1150b57cec5SDimitry Andric if (Reserved.test(PhysReg))
1160b57cec5SDimitry Andric continue;
117*5f7ddb14SDimitry Andric uint8_t Cost = RegCosts[PhysReg];
1180b57cec5SDimitry Andric MinCost = std::min(MinCost, Cost);
1190b57cec5SDimitry Andric
1200b57cec5SDimitry Andric if (CalleeSavedAliases[PhysReg] &&
1210b57cec5SDimitry Andric !STI.ignoreCSRForAllocationOrder(*MF, PhysReg))
1220b57cec5SDimitry Andric // PhysReg aliases a CSR, save it for later.
1230b57cec5SDimitry Andric CSRAlias.push_back(PhysReg);
1240b57cec5SDimitry Andric else {
1250b57cec5SDimitry Andric if (Cost != LastCost)
1260b57cec5SDimitry Andric LastCostChange = N;
1270b57cec5SDimitry Andric RCI.Order[N++] = PhysReg;
1280b57cec5SDimitry Andric LastCost = Cost;
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric RCI.NumRegs = N + CSRAlias.size();
1320b57cec5SDimitry Andric assert(RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric // CSR aliases go after the volatile registers, preserve the target's order.
1350b57cec5SDimitry Andric for (unsigned i = 0, e = CSRAlias.size(); i != e; ++i) {
1360b57cec5SDimitry Andric unsigned PhysReg = CSRAlias[i];
137*5f7ddb14SDimitry Andric uint8_t Cost = RegCosts[PhysReg];
1380b57cec5SDimitry Andric if (Cost != LastCost)
1390b57cec5SDimitry Andric LastCostChange = N;
1400b57cec5SDimitry Andric RCI.Order[N++] = PhysReg;
1410b57cec5SDimitry Andric LastCost = Cost;
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric
1440b57cec5SDimitry Andric // Register allocator stress test. Clip register class to N registers.
1450b57cec5SDimitry Andric if (StressRA && RCI.NumRegs > StressRA)
1460b57cec5SDimitry Andric RCI.NumRegs = StressRA;
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric // Check if RC is a proper sub-class.
1490b57cec5SDimitry Andric if (const TargetRegisterClass *Super =
1500b57cec5SDimitry Andric TRI->getLargestLegalSuperClass(RC, *MF))
1510b57cec5SDimitry Andric if (Super != RC && getNumAllocatableRegs(Super) > RCI.NumRegs)
1520b57cec5SDimitry Andric RCI.ProperSubClass = true;
1530b57cec5SDimitry Andric
154*5f7ddb14SDimitry Andric RCI.MinCost = MinCost;
1550b57cec5SDimitry Andric RCI.LastCostChange = LastCostChange;
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andric LLVM_DEBUG({
1580b57cec5SDimitry Andric dbgs() << "AllocationOrder(" << TRI->getRegClassName(RC) << ") = [";
1590b57cec5SDimitry Andric for (unsigned I = 0; I != RCI.NumRegs; ++I)
1600b57cec5SDimitry Andric dbgs() << ' ' << printReg(RCI.Order[I], TRI);
1610b57cec5SDimitry Andric dbgs() << (RCI.ProperSubClass ? " ] (sub-class)\n" : " ]\n");
1620b57cec5SDimitry Andric });
1630b57cec5SDimitry Andric
1640b57cec5SDimitry Andric // RCI is now up-to-date.
1650b57cec5SDimitry Andric RCI.Tag = Tag;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric
1680b57cec5SDimitry Andric /// This is not accurate because two overlapping register sets may have some
1690b57cec5SDimitry Andric /// nonoverlapping reserved registers. However, computing the allocation order
1700b57cec5SDimitry Andric /// for all register classes would be too expensive.
computePSetLimit(unsigned Idx) const1710b57cec5SDimitry Andric unsigned RegisterClassInfo::computePSetLimit(unsigned Idx) const {
1720b57cec5SDimitry Andric const TargetRegisterClass *RC = nullptr;
1730b57cec5SDimitry Andric unsigned NumRCUnits = 0;
1740b57cec5SDimitry Andric for (const TargetRegisterClass *C : TRI->regclasses()) {
1750b57cec5SDimitry Andric const int *PSetID = TRI->getRegClassPressureSets(C);
1760b57cec5SDimitry Andric for (; *PSetID != -1; ++PSetID) {
1770b57cec5SDimitry Andric if ((unsigned)*PSetID == Idx)
1780b57cec5SDimitry Andric break;
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric if (*PSetID == -1)
1810b57cec5SDimitry Andric continue;
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric // Found a register class that counts against this pressure set.
1840b57cec5SDimitry Andric // For efficiency, only compute the set order for the largest set.
1850b57cec5SDimitry Andric unsigned NUnits = TRI->getRegClassWeight(C).WeightLimit;
1860b57cec5SDimitry Andric if (!RC || NUnits > NumRCUnits) {
1870b57cec5SDimitry Andric RC = C;
1880b57cec5SDimitry Andric NumRCUnits = NUnits;
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric }
191480093f4SDimitry Andric assert(RC && "Failed to find register class");
1920b57cec5SDimitry Andric compute(RC);
193af732203SDimitry Andric unsigned NAllocatableRegs = getNumAllocatableRegs(RC);
194af732203SDimitry Andric unsigned RegPressureSetLimit = TRI->getRegPressureSetLimit(*MF, Idx);
195af732203SDimitry Andric // If all the regs are reserved, return raw RegPressureSetLimit.
196af732203SDimitry Andric // One example is VRSAVERC in PowerPC.
197af732203SDimitry Andric // Avoid returning zero, getRegPressureSetLimit(Idx) assumes computePSetLimit
198af732203SDimitry Andric // return non-zero value.
199af732203SDimitry Andric if (NAllocatableRegs == 0)
200af732203SDimitry Andric return RegPressureSetLimit;
201af732203SDimitry Andric unsigned NReserved = RC->getNumRegs() - NAllocatableRegs;
202af732203SDimitry Andric return RegPressureSetLimit - TRI->getRegClassWeight(RC).RegWeight * NReserved;
2030b57cec5SDimitry Andric }
204