17a7e6055SDimitry Andric //===- RegisterClassInfo.cpp - Dynamic Register Class Info ----------------===//
2bd5abe19SDimitry Andric //
3bd5abe19SDimitry Andric // The LLVM Compiler Infrastructure
4bd5abe19SDimitry Andric //
5bd5abe19SDimitry Andric // This file is distributed under the University of Illinois Open Source
6bd5abe19SDimitry Andric // License. See LICENSE.TXT for details.
7bd5abe19SDimitry Andric //
8bd5abe19SDimitry Andric //===----------------------------------------------------------------------===//
9bd5abe19SDimitry Andric //
10bd5abe19SDimitry Andric // This file implements the RegisterClassInfo class which provides dynamic
1191bc56edSDimitry Andric // information about target register classes. Callee-saved vs. caller-saved and
1291bc56edSDimitry Andric // reserved registers depend on calling conventions and other dynamic
1391bc56edSDimitry Andric // information, so some things cannot be determined statically.
14bd5abe19SDimitry Andric //
15bd5abe19SDimitry Andric //===----------------------------------------------------------------------===//
16bd5abe19SDimitry Andric
17db17bf38SDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
187a7e6055SDimitry Andric #include "llvm/ADT/ArrayRef.h"
197a7e6055SDimitry Andric #include "llvm/ADT/BitVector.h"
207a7e6055SDimitry Andric #include "llvm/ADT/SmallVector.h"
213861d79fSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
223861d79fSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
232cab237bSDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
242cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
252cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
267a7e6055SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
27dff0c46cSDimitry Andric #include "llvm/Support/CommandLine.h"
28bd5abe19SDimitry Andric #include "llvm/Support/Debug.h"
29bd5abe19SDimitry Andric #include "llvm/Support/raw_ostream.h"
307a7e6055SDimitry Andric #include <algorithm>
317a7e6055SDimitry Andric #include <cassert>
327a7e6055SDimitry Andric #include <cstdint>
33bd5abe19SDimitry Andric
34bd5abe19SDimitry Andric using namespace llvm;
35bd5abe19SDimitry Andric
3691bc56edSDimitry Andric #define DEBUG_TYPE "regalloc"
3791bc56edSDimitry Andric
38dff0c46cSDimitry Andric static cl::opt<unsigned>
39dff0c46cSDimitry Andric StressRA("stress-regalloc", cl::Hidden, cl::init(0), cl::value_desc("N"),
40dff0c46cSDimitry Andric cl::desc("Limit all regclasses to N registers"));
41dff0c46cSDimitry Andric
427a7e6055SDimitry Andric RegisterClassInfo::RegisterClassInfo() = default;
43bd5abe19SDimitry Andric
runOnMachineFunction(const MachineFunction & mf)44bd5abe19SDimitry Andric void RegisterClassInfo::runOnMachineFunction(const MachineFunction &mf) {
45bd5abe19SDimitry Andric bool Update = false;
46bd5abe19SDimitry Andric MF = &mf;
47bd5abe19SDimitry Andric
48bd5abe19SDimitry Andric // Allocate new array the first time we see a new target.
4939d628a0SDimitry Andric if (MF->getSubtarget().getRegisterInfo() != TRI) {
5039d628a0SDimitry Andric TRI = MF->getSubtarget().getRegisterInfo();
51bd5abe19SDimitry Andric RegClass.reset(new RCInfo[TRI->getNumRegClasses()]);
52bd5abe19SDimitry Andric Update = true;
53bd5abe19SDimitry Andric }
54bd5abe19SDimitry Andric
55bd5abe19SDimitry Andric // Does this MF have different CSRs?
5639d628a0SDimitry Andric assert(TRI && "no register info set");
577a7e6055SDimitry Andric
587a7e6055SDimitry Andric // Get the callee saved registers.
597a7e6055SDimitry Andric const MCPhysReg *CSR = MF->getRegInfo().getCalleeSavedRegs();
607a7e6055SDimitry Andric if (Update || CSR != CalleeSavedRegs) {
617a7e6055SDimitry Andric // Build a CSRAlias map. Every CSR alias saves the last
62bd5abe19SDimitry Andric // overlapping CSR.
637a7e6055SDimitry Andric CalleeSavedAliases.resize(TRI->getNumRegs(), 0);
647a7e6055SDimitry Andric for (const MCPhysReg *I = CSR; *I; ++I)
657a7e6055SDimitry Andric for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI)
667a7e6055SDimitry Andric CalleeSavedAliases[*AI] = *I;
677a7e6055SDimitry Andric
68bd5abe19SDimitry Andric Update = true;
69bd5abe19SDimitry Andric }
707a7e6055SDimitry Andric CalleeSavedRegs = CSR;
71bd5abe19SDimitry Andric
72bd5abe19SDimitry Andric // Different reserved registers?
733861d79fSDimitry Andric const BitVector &RR = MF->getRegInfo().getReservedRegs();
743861d79fSDimitry Andric if (Reserved.size() != RR.size() || RR != Reserved) {
75bd5abe19SDimitry Andric Update = true;
76bd5abe19SDimitry Andric Reserved = RR;
773861d79fSDimitry Andric }
78bd5abe19SDimitry Andric
79bd5abe19SDimitry Andric // Invalidate cached information from previous function.
80*4ba319b5SDimitry Andric if (Update) {
81*4ba319b5SDimitry Andric unsigned NumPSets = TRI->getNumRegPressureSets();
82*4ba319b5SDimitry Andric PSetLimits.reset(new unsigned[NumPSets]);
83*4ba319b5SDimitry Andric std::fill(&PSetLimits[0], &PSetLimits[NumPSets], 0);
84bd5abe19SDimitry Andric ++Tag;
85bd5abe19SDimitry Andric }
86*4ba319b5SDimitry Andric }
87bd5abe19SDimitry Andric
88bd5abe19SDimitry Andric /// compute - Compute the preferred allocation order for RC with reserved
89bd5abe19SDimitry Andric /// registers filtered out. Volatile registers come first followed by CSR
90bd5abe19SDimitry Andric /// aliases ordered according to the CSR order specified by the target.
compute(const TargetRegisterClass * RC) const91bd5abe19SDimitry Andric void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
9239d628a0SDimitry Andric assert(RC && "no register class given");
93bd5abe19SDimitry Andric RCInfo &RCI = RegClass[RC->getID()];
94bd5abe19SDimitry Andric
95bd5abe19SDimitry Andric // Raw register count, including all reserved regs.
96bd5abe19SDimitry Andric unsigned NumRegs = RC->getNumRegs();
97bd5abe19SDimitry Andric
98bd5abe19SDimitry Andric if (!RCI.Order)
99139f7f9bSDimitry Andric RCI.Order.reset(new MCPhysReg[NumRegs]);
100bd5abe19SDimitry Andric
101bd5abe19SDimitry Andric unsigned N = 0;
102139f7f9bSDimitry Andric SmallVector<MCPhysReg, 16> CSRAlias;
103139f7f9bSDimitry Andric unsigned MinCost = 0xff;
104139f7f9bSDimitry Andric unsigned LastCost = ~0u;
105139f7f9bSDimitry Andric unsigned LastCostChange = 0;
106bd5abe19SDimitry Andric
107bd5abe19SDimitry Andric // FIXME: Once targets reserve registers instead of removing them from the
108bd5abe19SDimitry Andric // allocation order, we can simply use begin/end here.
109139f7f9bSDimitry Andric ArrayRef<MCPhysReg> RawOrder = RC->getRawAllocationOrder(*MF);
11017a519f9SDimitry Andric for (unsigned i = 0; i != RawOrder.size(); ++i) {
11117a519f9SDimitry Andric unsigned PhysReg = RawOrder[i];
112bd5abe19SDimitry Andric // Remove reserved registers from the allocation order.
113bd5abe19SDimitry Andric if (Reserved.test(PhysReg))
114bd5abe19SDimitry Andric continue;
115139f7f9bSDimitry Andric unsigned Cost = TRI->getCostPerUse(PhysReg);
116139f7f9bSDimitry Andric MinCost = std::min(MinCost, Cost);
117139f7f9bSDimitry Andric
1187a7e6055SDimitry Andric if (CalleeSavedAliases[PhysReg])
119bd5abe19SDimitry Andric // PhysReg aliases a CSR, save it for later.
120bd5abe19SDimitry Andric CSRAlias.push_back(PhysReg);
121139f7f9bSDimitry Andric else {
122139f7f9bSDimitry Andric if (Cost != LastCost)
123139f7f9bSDimitry Andric LastCostChange = N;
124bd5abe19SDimitry Andric RCI.Order[N++] = PhysReg;
125139f7f9bSDimitry Andric LastCost = Cost;
126139f7f9bSDimitry Andric }
127bd5abe19SDimitry Andric }
128bd5abe19SDimitry Andric RCI.NumRegs = N + CSRAlias.size();
129bd5abe19SDimitry Andric assert(RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
130bd5abe19SDimitry Andric
131bd5abe19SDimitry Andric // CSR aliases go after the volatile registers, preserve the target's order.
132139f7f9bSDimitry Andric for (unsigned i = 0, e = CSRAlias.size(); i != e; ++i) {
133139f7f9bSDimitry Andric unsigned PhysReg = CSRAlias[i];
134139f7f9bSDimitry Andric unsigned Cost = TRI->getCostPerUse(PhysReg);
135139f7f9bSDimitry Andric if (Cost != LastCost)
136139f7f9bSDimitry Andric LastCostChange = N;
137139f7f9bSDimitry Andric RCI.Order[N++] = PhysReg;
138139f7f9bSDimitry Andric LastCost = Cost;
139139f7f9bSDimitry Andric }
140bd5abe19SDimitry Andric
141dff0c46cSDimitry Andric // Register allocator stress test. Clip register class to N registers.
142dff0c46cSDimitry Andric if (StressRA && RCI.NumRegs > StressRA)
143dff0c46cSDimitry Andric RCI.NumRegs = StressRA;
144dff0c46cSDimitry Andric
1456122f3e6SDimitry Andric // Check if RC is a proper sub-class.
146ff0cc061SDimitry Andric if (const TargetRegisterClass *Super =
147ff0cc061SDimitry Andric TRI->getLargestLegalSuperClass(RC, *MF))
1486122f3e6SDimitry Andric if (Super != RC && getNumAllocatableRegs(Super) > RCI.NumRegs)
1496122f3e6SDimitry Andric RCI.ProperSubClass = true;
1506122f3e6SDimitry Andric
151139f7f9bSDimitry Andric RCI.MinCost = uint8_t(MinCost);
152139f7f9bSDimitry Andric RCI.LastCostChange = LastCostChange;
153139f7f9bSDimitry Andric
154*4ba319b5SDimitry Andric LLVM_DEBUG({
15539d628a0SDimitry Andric dbgs() << "AllocationOrder(" << TRI->getRegClassName(RC) << ") = [";
15617a519f9SDimitry Andric for (unsigned I = 0; I != RCI.NumRegs; ++I)
1572cab237bSDimitry Andric dbgs() << ' ' << printReg(RCI.Order[I], TRI);
1586122f3e6SDimitry Andric dbgs() << (RCI.ProperSubClass ? " ] (sub-class)\n" : " ]\n");
159bd5abe19SDimitry Andric });
160bd5abe19SDimitry Andric
161bd5abe19SDimitry Andric // RCI is now up-to-date.
162bd5abe19SDimitry Andric RCI.Tag = Tag;
163bd5abe19SDimitry Andric }
164bd5abe19SDimitry Andric
165f785676fSDimitry Andric /// This is not accurate because two overlapping register sets may have some
166f785676fSDimitry Andric /// nonoverlapping reserved registers. However, computing the allocation order
167f785676fSDimitry Andric /// for all register classes would be too expensive.
computePSetLimit(unsigned Idx) const168f785676fSDimitry Andric unsigned RegisterClassInfo::computePSetLimit(unsigned Idx) const {
16991bc56edSDimitry Andric const TargetRegisterClass *RC = nullptr;
170f785676fSDimitry Andric unsigned NumRCUnits = 0;
1717a7e6055SDimitry Andric for (const TargetRegisterClass *C : TRI->regclasses()) {
1727a7e6055SDimitry Andric const int *PSetID = TRI->getRegClassPressureSets(C);
173f785676fSDimitry Andric for (; *PSetID != -1; ++PSetID) {
174f785676fSDimitry Andric if ((unsigned)*PSetID == Idx)
175f785676fSDimitry Andric break;
176f785676fSDimitry Andric }
177f785676fSDimitry Andric if (*PSetID == -1)
178f785676fSDimitry Andric continue;
179f785676fSDimitry Andric
180f785676fSDimitry Andric // Found a register class that counts against this pressure set.
181f785676fSDimitry Andric // For efficiency, only compute the set order for the largest set.
1827a7e6055SDimitry Andric unsigned NUnits = TRI->getRegClassWeight(C).WeightLimit;
183f785676fSDimitry Andric if (!RC || NUnits > NumRCUnits) {
1847a7e6055SDimitry Andric RC = C;
185f785676fSDimitry Andric NumRCUnits = NUnits;
186f785676fSDimitry Andric }
187f785676fSDimitry Andric }
188f785676fSDimitry Andric compute(RC);
189f785676fSDimitry Andric unsigned NReserved = RC->getNumRegs() - getNumAllocatableRegs(RC);
190ff0cc061SDimitry Andric return TRI->getRegPressureSetLimit(*MF, Idx) -
191ff0cc061SDimitry Andric TRI->getRegClassWeight(RC).RegWeight * NReserved;
192f785676fSDimitry Andric }
193