1edd7eaddSDimitry Andric //==- TargetRegisterInfo.cpp - Target Register Information Implementation --==//
2139f7f9bSDimitry Andric //
3139f7f9bSDimitry Andric //                     The LLVM Compiler Infrastructure
4139f7f9bSDimitry Andric //
5139f7f9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
6139f7f9bSDimitry Andric // License. See LICENSE.TXT for details.
7139f7f9bSDimitry Andric //
8139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
9139f7f9bSDimitry Andric //
10139f7f9bSDimitry Andric // This file implements the TargetRegisterInfo interface.
11139f7f9bSDimitry Andric //
12139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
13139f7f9bSDimitry Andric 
142cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
15edd7eaddSDimitry Andric #include "llvm/ADT/ArrayRef.h"
16139f7f9bSDimitry Andric #include "llvm/ADT/BitVector.h"
17*b5893f02SDimitry Andric #include "llvm/ADT/SmallSet.h"
18edd7eaddSDimitry Andric #include "llvm/ADT/STLExtras.h"
192cab237bSDimitry Andric #include "llvm/ADT/StringExtras.h"
207d523365SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
21139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
22139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
232cab237bSDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
242cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
25139f7f9bSDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
264ba319b5SDimitry Andric #include "llvm/Config/llvm-config.h"
27edd7eaddSDimitry Andric #include "llvm/IR/Attributes.h"
287d523365SDimitry Andric #include "llvm/IR/Function.h"
29edd7eaddSDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
30edd7eaddSDimitry Andric #include "llvm/Support/Compiler.h"
3139d628a0SDimitry Andric #include "llvm/Support/Debug.h"
324ba319b5SDimitry Andric #include "llvm/Support/MachineValueType.h"
33edd7eaddSDimitry Andric #include "llvm/Support/MathExtras.h"
34edd7eaddSDimitry Andric #include "llvm/Support/Printable.h"
35139f7f9bSDimitry Andric #include "llvm/Support/raw_ostream.h"
36edd7eaddSDimitry Andric #include <cassert>
37edd7eaddSDimitry Andric #include <utility>
387d523365SDimitry Andric 
397d523365SDimitry Andric #define DEBUG_TYPE "target-reg-info"
40139f7f9bSDimitry Andric 
41139f7f9bSDimitry Andric using namespace llvm;
42139f7f9bSDimitry Andric 
TargetRegisterInfo(const TargetRegisterInfoDesc * ID,regclass_iterator RCB,regclass_iterator RCE,const char * const * SRINames,const LaneBitmask * SRILaneMasks,LaneBitmask SRICoveringLanes,const RegClassInfo * const RCIs,unsigned Mode)43139f7f9bSDimitry Andric TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
44139f7f9bSDimitry Andric                              regclass_iterator RCB, regclass_iterator RCE,
45139f7f9bSDimitry Andric                              const char *const *SRINames,
46d88c1a5aSDimitry Andric                              const LaneBitmask *SRILaneMasks,
472cab237bSDimitry Andric                              LaneBitmask SRICoveringLanes,
482cab237bSDimitry Andric                              const RegClassInfo *const RCIs,
492cab237bSDimitry Andric                              unsigned Mode)
50139f7f9bSDimitry Andric   : InfoDesc(ID), SubRegIndexNames(SRINames),
51139f7f9bSDimitry Andric     SubRegIndexLaneMasks(SRILaneMasks),
52f785676fSDimitry Andric     RegClassBegin(RCB), RegClassEnd(RCE),
532cab237bSDimitry Andric     CoveringLanes(SRICoveringLanes),
542cab237bSDimitry Andric     RCInfos(RCIs), HwMode(Mode) {
55139f7f9bSDimitry Andric }
56139f7f9bSDimitry Andric 
57edd7eaddSDimitry Andric TargetRegisterInfo::~TargetRegisterInfo() = default;
58139f7f9bSDimitry Andric 
markSuperRegs(BitVector & RegisterSet,unsigned Reg) const59d88c1a5aSDimitry Andric void TargetRegisterInfo::markSuperRegs(BitVector &RegisterSet, unsigned Reg)
60d88c1a5aSDimitry Andric     const {
61d88c1a5aSDimitry Andric   for (MCSuperRegIterator AI(Reg, this, true); AI.isValid(); ++AI)
62d88c1a5aSDimitry Andric     RegisterSet.set(*AI);
63d88c1a5aSDimitry Andric }
64d88c1a5aSDimitry Andric 
checkAllSuperRegsMarked(const BitVector & RegisterSet,ArrayRef<MCPhysReg> Exceptions) const65d88c1a5aSDimitry Andric bool TargetRegisterInfo::checkAllSuperRegsMarked(const BitVector &RegisterSet,
66d88c1a5aSDimitry Andric     ArrayRef<MCPhysReg> Exceptions) const {
67d88c1a5aSDimitry Andric   // Check that all super registers of reserved regs are reserved as well.
68d88c1a5aSDimitry Andric   BitVector Checked(getNumRegs());
6960ff8e32SDimitry Andric   for (unsigned Reg : RegisterSet.set_bits()) {
70d88c1a5aSDimitry Andric     if (Checked[Reg])
71d88c1a5aSDimitry Andric       continue;
72d88c1a5aSDimitry Andric     for (MCSuperRegIterator SR(Reg, this); SR.isValid(); ++SR) {
73d88c1a5aSDimitry Andric       if (!RegisterSet[*SR] && !is_contained(Exceptions, Reg)) {
742cab237bSDimitry Andric         dbgs() << "Error: Super register " << printReg(*SR, this)
752cab237bSDimitry Andric                << " of reserved register " << printReg(Reg, this)
76d88c1a5aSDimitry Andric                << " is not reserved.\n";
77d88c1a5aSDimitry Andric         return false;
78d88c1a5aSDimitry Andric       }
79d88c1a5aSDimitry Andric 
80d88c1a5aSDimitry Andric       // We transitively check superregs. So we can remember this for later
81d88c1a5aSDimitry Andric       // to avoid compiletime explosion in deep register hierarchies.
82d88c1a5aSDimitry Andric       Checked.set(*SR);
83d88c1a5aSDimitry Andric     }
84d88c1a5aSDimitry Andric   }
85d88c1a5aSDimitry Andric   return true;
86d88c1a5aSDimitry Andric }
87d88c1a5aSDimitry Andric 
887d523365SDimitry Andric namespace llvm {
897d523365SDimitry Andric 
printReg(unsigned Reg,const TargetRegisterInfo * TRI,unsigned SubIdx,const MachineRegisterInfo * MRI)902cab237bSDimitry Andric Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI,
914ba319b5SDimitry Andric                    unsigned SubIdx, const MachineRegisterInfo *MRI) {
924ba319b5SDimitry Andric   return Printable([Reg, TRI, SubIdx, MRI](raw_ostream &OS) {
93139f7f9bSDimitry Andric     if (!Reg)
944ba319b5SDimitry Andric       OS << "$noreg";
95139f7f9bSDimitry Andric     else if (TargetRegisterInfo::isStackSlot(Reg))
96139f7f9bSDimitry Andric       OS << "SS#" << TargetRegisterInfo::stackSlot2Index(Reg);
974ba319b5SDimitry Andric     else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
984ba319b5SDimitry Andric       StringRef Name = MRI ? MRI->getVRegName(Reg) : "";
994ba319b5SDimitry Andric       if (Name != "") {
1004ba319b5SDimitry Andric         OS << '%' << Name;
1014ba319b5SDimitry Andric       } else {
1022cab237bSDimitry Andric         OS << '%' << TargetRegisterInfo::virtReg2Index(Reg);
1034ba319b5SDimitry Andric       }
1044ba319b5SDimitry Andric     }
1052cab237bSDimitry Andric     else if (!TRI)
1064ba319b5SDimitry Andric       OS << '$' << "physreg" << Reg;
1072cab237bSDimitry Andric     else if (Reg < TRI->getNumRegs()) {
1084ba319b5SDimitry Andric       OS << '$';
1092cab237bSDimitry Andric       printLowerCase(TRI->getName(Reg), OS);
1102cab237bSDimitry Andric     } else
1112cab237bSDimitry Andric       llvm_unreachable("Register kind is unsupported.");
1122cab237bSDimitry Andric 
113139f7f9bSDimitry Andric     if (SubIdx) {
114139f7f9bSDimitry Andric       if (TRI)
115139f7f9bSDimitry Andric         OS << ':' << TRI->getSubRegIndexName(SubIdx);
116139f7f9bSDimitry Andric       else
117139f7f9bSDimitry Andric         OS << ":sub(" << SubIdx << ')';
118139f7f9bSDimitry Andric     }
1197d523365SDimitry Andric   });
120139f7f9bSDimitry Andric }
121139f7f9bSDimitry Andric 
printRegUnit(unsigned Unit,const TargetRegisterInfo * TRI)1222cab237bSDimitry Andric Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
1237d523365SDimitry Andric   return Printable([Unit, TRI](raw_ostream &OS) {
124139f7f9bSDimitry Andric     // Generic printout when TRI is missing.
125139f7f9bSDimitry Andric     if (!TRI) {
126139f7f9bSDimitry Andric       OS << "Unit~" << Unit;
127139f7f9bSDimitry Andric       return;
128139f7f9bSDimitry Andric     }
129139f7f9bSDimitry Andric 
130139f7f9bSDimitry Andric     // Check for invalid register units.
131139f7f9bSDimitry Andric     if (Unit >= TRI->getNumRegUnits()) {
132139f7f9bSDimitry Andric       OS << "BadUnit~" << Unit;
133139f7f9bSDimitry Andric       return;
134139f7f9bSDimitry Andric     }
135139f7f9bSDimitry Andric 
136139f7f9bSDimitry Andric     // Normal units have at least one root.
137139f7f9bSDimitry Andric     MCRegUnitRootIterator Roots(Unit, TRI);
138139f7f9bSDimitry Andric     assert(Roots.isValid() && "Unit has no roots.");
139139f7f9bSDimitry Andric     OS << TRI->getName(*Roots);
140139f7f9bSDimitry Andric     for (++Roots; Roots.isValid(); ++Roots)
141139f7f9bSDimitry Andric       OS << '~' << TRI->getName(*Roots);
1427d523365SDimitry Andric   });
143139f7f9bSDimitry Andric }
144139f7f9bSDimitry Andric 
printVRegOrUnit(unsigned Unit,const TargetRegisterInfo * TRI)1452cab237bSDimitry Andric Printable printVRegOrUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
1467d523365SDimitry Andric   return Printable([Unit, TRI](raw_ostream &OS) {
147f785676fSDimitry Andric     if (TRI && TRI->isVirtualRegister(Unit)) {
1482cab237bSDimitry Andric       OS << '%' << TargetRegisterInfo::virtReg2Index(Unit);
1497d523365SDimitry Andric     } else {
1502cab237bSDimitry Andric       OS << printRegUnit(Unit, TRI);
1512cab237bSDimitry Andric     }
1522cab237bSDimitry Andric   });
1532cab237bSDimitry Andric }
1542cab237bSDimitry Andric 
printRegClassOrBank(unsigned Reg,const MachineRegisterInfo & RegInfo,const TargetRegisterInfo * TRI)1552cab237bSDimitry Andric Printable printRegClassOrBank(unsigned Reg, const MachineRegisterInfo &RegInfo,
1562cab237bSDimitry Andric                               const TargetRegisterInfo *TRI) {
1572cab237bSDimitry Andric   return Printable([Reg, &RegInfo, TRI](raw_ostream &OS) {
1582cab237bSDimitry Andric     if (RegInfo.getRegClassOrNull(Reg))
1592cab237bSDimitry Andric       OS << StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
1602cab237bSDimitry Andric     else if (RegInfo.getRegBankOrNull(Reg))
1612cab237bSDimitry Andric       OS << StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
1622cab237bSDimitry Andric     else {
1632cab237bSDimitry Andric       OS << "_";
1642cab237bSDimitry Andric       assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
1652cab237bSDimitry Andric              "Generic registers must have a valid type");
166f785676fSDimitry Andric     }
1677d523365SDimitry Andric   });
168f785676fSDimitry Andric }
169f785676fSDimitry Andric 
170edd7eaddSDimitry Andric } // end namespace llvm
1717d523365SDimitry Andric 
172139f7f9bSDimitry Andric /// getAllocatableClass - Return the maximal subclass of the given register
173139f7f9bSDimitry Andric /// class that is alloctable, or NULL.
174139f7f9bSDimitry Andric const TargetRegisterClass *
getAllocatableClass(const TargetRegisterClass * RC) const175139f7f9bSDimitry Andric TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
176139f7f9bSDimitry Andric   if (!RC || RC->isAllocatable())
177139f7f9bSDimitry Andric     return RC;
178139f7f9bSDimitry Andric 
1793ca95b02SDimitry Andric   for (BitMaskClassIterator It(RC->getSubClassMask(), *this); It.isValid();
1803ca95b02SDimitry Andric        ++It) {
1813ca95b02SDimitry Andric     const TargetRegisterClass *SubRC = getRegClass(It.getID());
182139f7f9bSDimitry Andric     if (SubRC->isAllocatable())
183139f7f9bSDimitry Andric       return SubRC;
184139f7f9bSDimitry Andric   }
18591bc56edSDimitry Andric   return nullptr;
186139f7f9bSDimitry Andric }
187139f7f9bSDimitry Andric 
188139f7f9bSDimitry Andric /// getMinimalPhysRegClass - Returns the Register Class of a physical
189139f7f9bSDimitry Andric /// register of the given type, picking the most sub register class of
190139f7f9bSDimitry Andric /// the right type that contains this physreg.
191139f7f9bSDimitry Andric const TargetRegisterClass *
getMinimalPhysRegClass(unsigned reg,MVT VT) const19239d628a0SDimitry Andric TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, MVT VT) const {
193139f7f9bSDimitry Andric   assert(isPhysicalRegister(reg) && "reg must be a physical register");
194139f7f9bSDimitry Andric 
195139f7f9bSDimitry Andric   // Pick the most sub register class of the right type that contains
196139f7f9bSDimitry Andric   // this physreg.
19791bc56edSDimitry Andric   const TargetRegisterClass* BestRC = nullptr;
1987a7e6055SDimitry Andric   for (const TargetRegisterClass* RC : regclasses()) {
19951690af2SDimitry Andric     if ((VT == MVT::Other || isTypeLegalForClass(*RC, VT)) &&
20051690af2SDimitry Andric         RC->contains(reg) && (!BestRC || BestRC->hasSubClass(RC)))
201139f7f9bSDimitry Andric       BestRC = RC;
202139f7f9bSDimitry Andric   }
203139f7f9bSDimitry Andric 
204139f7f9bSDimitry Andric   assert(BestRC && "Couldn't find the register class");
205139f7f9bSDimitry Andric   return BestRC;
206139f7f9bSDimitry Andric }
207139f7f9bSDimitry Andric 
208139f7f9bSDimitry Andric /// getAllocatableSetForRC - Toggle the bits that represent allocatable
209139f7f9bSDimitry Andric /// registers for the specific register class.
getAllocatableSetForRC(const MachineFunction & MF,const TargetRegisterClass * RC,BitVector & R)210139f7f9bSDimitry Andric static void getAllocatableSetForRC(const MachineFunction &MF,
211139f7f9bSDimitry Andric                                    const TargetRegisterClass *RC, BitVector &R){
212139f7f9bSDimitry Andric   assert(RC->isAllocatable() && "invalid for nonallocatable sets");
21391bc56edSDimitry Andric   ArrayRef<MCPhysReg> Order = RC->getRawAllocationOrder(MF);
214139f7f9bSDimitry Andric   for (unsigned i = 0; i != Order.size(); ++i)
215139f7f9bSDimitry Andric     R.set(Order[i]);
216139f7f9bSDimitry Andric }
217139f7f9bSDimitry Andric 
getAllocatableSet(const MachineFunction & MF,const TargetRegisterClass * RC) const218139f7f9bSDimitry Andric BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
219139f7f9bSDimitry Andric                                           const TargetRegisterClass *RC) const {
220139f7f9bSDimitry Andric   BitVector Allocatable(getNumRegs());
221139f7f9bSDimitry Andric   if (RC) {
222139f7f9bSDimitry Andric     // A register class with no allocatable subclass returns an empty set.
223139f7f9bSDimitry Andric     const TargetRegisterClass *SubClass = getAllocatableClass(RC);
224139f7f9bSDimitry Andric     if (SubClass)
225139f7f9bSDimitry Andric       getAllocatableSetForRC(MF, SubClass, Allocatable);
226139f7f9bSDimitry Andric   } else {
2277a7e6055SDimitry Andric     for (const TargetRegisterClass *C : regclasses())
2287a7e6055SDimitry Andric       if (C->isAllocatable())
2297a7e6055SDimitry Andric         getAllocatableSetForRC(MF, C, Allocatable);
230139f7f9bSDimitry Andric   }
231139f7f9bSDimitry Andric 
232139f7f9bSDimitry Andric   // Mask out the reserved registers
233139f7f9bSDimitry Andric   BitVector Reserved = getReservedRegs(MF);
234139f7f9bSDimitry Andric   Allocatable &= Reserved.flip();
235139f7f9bSDimitry Andric 
236139f7f9bSDimitry Andric   return Allocatable;
237139f7f9bSDimitry Andric }
238139f7f9bSDimitry Andric 
239139f7f9bSDimitry Andric static inline
firstCommonClass(const uint32_t * A,const uint32_t * B,const TargetRegisterInfo * TRI,const MVT::SimpleValueType SVT=MVT::SimpleValueType::Any)240139f7f9bSDimitry Andric const TargetRegisterClass *firstCommonClass(const uint32_t *A,
241139f7f9bSDimitry Andric                                             const uint32_t *B,
2427d523365SDimitry Andric                                             const TargetRegisterInfo *TRI,
2437d523365SDimitry Andric                                             const MVT::SimpleValueType SVT =
2447d523365SDimitry Andric                                             MVT::SimpleValueType::Any) {
2457d523365SDimitry Andric   const MVT VT(SVT);
246139f7f9bSDimitry Andric   for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
2477d523365SDimitry Andric     if (unsigned Common = *A++ & *B++) {
2487d523365SDimitry Andric       const TargetRegisterClass *RC =
2497d523365SDimitry Andric           TRI->getRegClass(I + countTrailingZeros(Common));
25051690af2SDimitry Andric       if (SVT == MVT::SimpleValueType::Any || TRI->isTypeLegalForClass(*RC, VT))
2517d523365SDimitry Andric         return RC;
2527d523365SDimitry Andric     }
25391bc56edSDimitry Andric   return nullptr;
254139f7f9bSDimitry Andric }
255139f7f9bSDimitry Andric 
256139f7f9bSDimitry Andric const TargetRegisterClass *
getCommonSubClass(const TargetRegisterClass * A,const TargetRegisterClass * B,const MVT::SimpleValueType SVT) const257139f7f9bSDimitry Andric TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
2587d523365SDimitry Andric                                       const TargetRegisterClass *B,
2597d523365SDimitry Andric                                       const MVT::SimpleValueType SVT) const {
260139f7f9bSDimitry Andric   // First take care of the trivial cases.
261139f7f9bSDimitry Andric   if (A == B)
262139f7f9bSDimitry Andric     return A;
263139f7f9bSDimitry Andric   if (!A || !B)
26491bc56edSDimitry Andric     return nullptr;
265139f7f9bSDimitry Andric 
266139f7f9bSDimitry Andric   // Register classes are ordered topologically, so the largest common
267139f7f9bSDimitry Andric   // sub-class it the common sub-class with the smallest ID.
2687d523365SDimitry Andric   return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this, SVT);
269139f7f9bSDimitry Andric }
270139f7f9bSDimitry Andric 
271139f7f9bSDimitry Andric const TargetRegisterClass *
getMatchingSuperRegClass(const TargetRegisterClass * A,const TargetRegisterClass * B,unsigned Idx) const272139f7f9bSDimitry Andric TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
273139f7f9bSDimitry Andric                                              const TargetRegisterClass *B,
274139f7f9bSDimitry Andric                                              unsigned Idx) const {
275139f7f9bSDimitry Andric   assert(A && B && "Missing register class");
276139f7f9bSDimitry Andric   assert(Idx && "Bad sub-register index");
277139f7f9bSDimitry Andric 
278139f7f9bSDimitry Andric   // Find Idx in the list of super-register indices.
279139f7f9bSDimitry Andric   for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
280139f7f9bSDimitry Andric     if (RCI.getSubReg() == Idx)
281139f7f9bSDimitry Andric       // The bit mask contains all register classes that are projected into B
282139f7f9bSDimitry Andric       // by Idx. Find a class that is also a sub-class of A.
283139f7f9bSDimitry Andric       return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this);
28491bc56edSDimitry Andric   return nullptr;
285139f7f9bSDimitry Andric }
286139f7f9bSDimitry Andric 
287139f7f9bSDimitry Andric const TargetRegisterClass *TargetRegisterInfo::
getCommonSuperRegClass(const TargetRegisterClass * RCA,unsigned SubA,const TargetRegisterClass * RCB,unsigned SubB,unsigned & PreA,unsigned & PreB) const288139f7f9bSDimitry Andric getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
289139f7f9bSDimitry Andric                        const TargetRegisterClass *RCB, unsigned SubB,
290139f7f9bSDimitry Andric                        unsigned &PreA, unsigned &PreB) const {
291139f7f9bSDimitry Andric   assert(RCA && SubA && RCB && SubB && "Invalid arguments");
292139f7f9bSDimitry Andric 
293139f7f9bSDimitry Andric   // Search all pairs of sub-register indices that project into RCA and RCB
294139f7f9bSDimitry Andric   // respectively. This is quadratic, but usually the sets are very small. On
295139f7f9bSDimitry Andric   // most targets like X86, there will only be a single sub-register index
296139f7f9bSDimitry Andric   // (e.g., sub_16bit projecting into GR16).
297139f7f9bSDimitry Andric   //
298139f7f9bSDimitry Andric   // The worst case is a register class like DPR on ARM.
299139f7f9bSDimitry Andric   // We have indices dsub_0..dsub_7 projecting into that class.
300139f7f9bSDimitry Andric   //
301139f7f9bSDimitry Andric   // It is very common that one register class is a sub-register of the other.
302139f7f9bSDimitry Andric   // Arrange for RCA to be the larger register so the answer will be found in
303139f7f9bSDimitry Andric   // the first iteration. This makes the search linear for the most common
304139f7f9bSDimitry Andric   // case.
30591bc56edSDimitry Andric   const TargetRegisterClass *BestRC = nullptr;
306139f7f9bSDimitry Andric   unsigned *BestPreA = &PreA;
307139f7f9bSDimitry Andric   unsigned *BestPreB = &PreB;
30851690af2SDimitry Andric   if (getRegSizeInBits(*RCA) < getRegSizeInBits(*RCB)) {
309139f7f9bSDimitry Andric     std::swap(RCA, RCB);
310139f7f9bSDimitry Andric     std::swap(SubA, SubB);
311139f7f9bSDimitry Andric     std::swap(BestPreA, BestPreB);
312139f7f9bSDimitry Andric   }
313139f7f9bSDimitry Andric 
314139f7f9bSDimitry Andric   // Also terminate the search one we have found a register class as small as
315139f7f9bSDimitry Andric   // RCA.
31651690af2SDimitry Andric   unsigned MinSize = getRegSizeInBits(*RCA);
317139f7f9bSDimitry Andric 
318139f7f9bSDimitry Andric   for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
319139f7f9bSDimitry Andric     unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
320139f7f9bSDimitry Andric     for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
321139f7f9bSDimitry Andric       // Check if a common super-register class exists for this index pair.
322139f7f9bSDimitry Andric       const TargetRegisterClass *RC =
323139f7f9bSDimitry Andric         firstCommonClass(IA.getMask(), IB.getMask(), this);
32451690af2SDimitry Andric       if (!RC || getRegSizeInBits(*RC) < MinSize)
325139f7f9bSDimitry Andric         continue;
326139f7f9bSDimitry Andric 
327139f7f9bSDimitry Andric       // The indexes must compose identically: PreA+SubA == PreB+SubB.
328139f7f9bSDimitry Andric       unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
329139f7f9bSDimitry Andric       if (FinalA != FinalB)
330139f7f9bSDimitry Andric         continue;
331139f7f9bSDimitry Andric 
332139f7f9bSDimitry Andric       // Is RC a better candidate than BestRC?
33351690af2SDimitry Andric       if (BestRC && getRegSizeInBits(*RC) >= getRegSizeInBits(*BestRC))
334139f7f9bSDimitry Andric         continue;
335139f7f9bSDimitry Andric 
336139f7f9bSDimitry Andric       // Yes, RC is the smallest super-register seen so far.
337139f7f9bSDimitry Andric       BestRC = RC;
338139f7f9bSDimitry Andric       *BestPreA = IA.getSubReg();
339139f7f9bSDimitry Andric       *BestPreB = IB.getSubReg();
340139f7f9bSDimitry Andric 
341139f7f9bSDimitry Andric       // Bail early if we reached MinSize. We won't find a better candidate.
34251690af2SDimitry Andric       if (getRegSizeInBits(*BestRC) == MinSize)
343139f7f9bSDimitry Andric         return BestRC;
344139f7f9bSDimitry Andric     }
345139f7f9bSDimitry Andric   }
346139f7f9bSDimitry Andric   return BestRC;
347139f7f9bSDimitry Andric }
348139f7f9bSDimitry Andric 
3494ba319b5SDimitry Andric /// Check if the registers defined by the pair (RegisterClass, SubReg)
3507d523365SDimitry Andric /// share the same register file.
shareSameRegisterFile(const TargetRegisterInfo & TRI,const TargetRegisterClass * DefRC,unsigned DefSubReg,const TargetRegisterClass * SrcRC,unsigned SrcSubReg)3517d523365SDimitry Andric static bool shareSameRegisterFile(const TargetRegisterInfo &TRI,
3527d523365SDimitry Andric                                   const TargetRegisterClass *DefRC,
3537d523365SDimitry Andric                                   unsigned DefSubReg,
3547d523365SDimitry Andric                                   const TargetRegisterClass *SrcRC,
3557d523365SDimitry Andric                                   unsigned SrcSubReg) {
3567d523365SDimitry Andric   // Same register class.
3577d523365SDimitry Andric   if (DefRC == SrcRC)
3587d523365SDimitry Andric     return true;
3597d523365SDimitry Andric 
3607d523365SDimitry Andric   // Both operands are sub registers. Check if they share a register class.
3617d523365SDimitry Andric   unsigned SrcIdx, DefIdx;
3627d523365SDimitry Andric   if (SrcSubReg && DefSubReg) {
3637d523365SDimitry Andric     return TRI.getCommonSuperRegClass(SrcRC, SrcSubReg, DefRC, DefSubReg,
3647d523365SDimitry Andric                                       SrcIdx, DefIdx) != nullptr;
3657d523365SDimitry Andric   }
3667d523365SDimitry Andric 
3677d523365SDimitry Andric   // At most one of the register is a sub register, make it Src to avoid
3687d523365SDimitry Andric   // duplicating the test.
3697d523365SDimitry Andric   if (!SrcSubReg) {
3707d523365SDimitry Andric     std::swap(DefSubReg, SrcSubReg);
3717d523365SDimitry Andric     std::swap(DefRC, SrcRC);
3727d523365SDimitry Andric   }
3737d523365SDimitry Andric 
3747d523365SDimitry Andric   // One of the register is a sub register, check if we can get a superclass.
3757d523365SDimitry Andric   if (SrcSubReg)
3767d523365SDimitry Andric     return TRI.getMatchingSuperRegClass(SrcRC, DefRC, SrcSubReg) != nullptr;
3777d523365SDimitry Andric 
3787d523365SDimitry Andric   // Plain copy.
3797d523365SDimitry Andric   return TRI.getCommonSubClass(DefRC, SrcRC) != nullptr;
3807d523365SDimitry Andric }
3817d523365SDimitry Andric 
shouldRewriteCopySrc(const TargetRegisterClass * DefRC,unsigned DefSubReg,const TargetRegisterClass * SrcRC,unsigned SrcSubReg) const3827d523365SDimitry Andric bool TargetRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
3837d523365SDimitry Andric                                               unsigned DefSubReg,
3847d523365SDimitry Andric                                               const TargetRegisterClass *SrcRC,
3857d523365SDimitry Andric                                               unsigned SrcSubReg) const {
3867d523365SDimitry Andric   // If this source does not incur a cross register bank copy, use it.
3877d523365SDimitry Andric   return shareSameRegisterFile(*this, DefRC, DefSubReg, SrcRC, SrcSubReg);
3887d523365SDimitry Andric }
3897d523365SDimitry Andric 
390139f7f9bSDimitry Andric // Compute target-independent register allocator hints to help eliminate copies.
3912cab237bSDimitry Andric bool
getRegAllocationHints(unsigned VirtReg,ArrayRef<MCPhysReg> Order,SmallVectorImpl<MCPhysReg> & Hints,const MachineFunction & MF,const VirtRegMap * VRM,const LiveRegMatrix * Matrix) const392139f7f9bSDimitry Andric TargetRegisterInfo::getRegAllocationHints(unsigned VirtReg,
393139f7f9bSDimitry Andric                                           ArrayRef<MCPhysReg> Order,
394139f7f9bSDimitry Andric                                           SmallVectorImpl<MCPhysReg> &Hints,
395139f7f9bSDimitry Andric                                           const MachineFunction &MF,
3967d523365SDimitry Andric                                           const VirtRegMap *VRM,
3977d523365SDimitry Andric                                           const LiveRegMatrix *Matrix) const {
398139f7f9bSDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
3992cab237bSDimitry Andric   const std::pair<unsigned, SmallVector<unsigned, 4>> &Hints_MRI =
4002cab237bSDimitry Andric     MRI.getRegAllocationHints(VirtReg);
401139f7f9bSDimitry Andric 
402*b5893f02SDimitry Andric   SmallSet<unsigned, 32> HintedRegs;
4032cab237bSDimitry Andric   // First hint may be a target hint.
4042cab237bSDimitry Andric   bool Skip = (Hints_MRI.first != 0);
4052cab237bSDimitry Andric   for (auto Reg : Hints_MRI.second) {
4062cab237bSDimitry Andric     if (Skip) {
4072cab237bSDimitry Andric       Skip = false;
4082cab237bSDimitry Andric       continue;
4092cab237bSDimitry Andric     }
410139f7f9bSDimitry Andric 
411139f7f9bSDimitry Andric     // Target-independent hints are either a physical or a virtual register.
4122cab237bSDimitry Andric     unsigned Phys = Reg;
413139f7f9bSDimitry Andric     if (VRM && isVirtualRegister(Phys))
414139f7f9bSDimitry Andric       Phys = VRM->getPhys(Phys);
415139f7f9bSDimitry Andric 
416*b5893f02SDimitry Andric     // Don't add the same reg twice (Hints_MRI may contain multiple virtual
417*b5893f02SDimitry Andric     // registers allocated to the same physreg).
418*b5893f02SDimitry Andric     if (!HintedRegs.insert(Phys).second)
419*b5893f02SDimitry Andric       continue;
420139f7f9bSDimitry Andric     // Check that Phys is a valid hint in VirtReg's register class.
421139f7f9bSDimitry Andric     if (!isPhysicalRegister(Phys))
4222cab237bSDimitry Andric       continue;
423139f7f9bSDimitry Andric     if (MRI.isReserved(Phys))
4242cab237bSDimitry Andric       continue;
425139f7f9bSDimitry Andric     // Check that Phys is in the allocation order. We shouldn't heed hints
426139f7f9bSDimitry Andric     // from VirtReg's register class if they aren't in the allocation order. The
427139f7f9bSDimitry Andric     // target probably has a reason for removing the register.
428d88c1a5aSDimitry Andric     if (!is_contained(Order, Phys))
4292cab237bSDimitry Andric       continue;
430139f7f9bSDimitry Andric 
431139f7f9bSDimitry Andric     // All clear, tell the register allocator to prefer this register.
432139f7f9bSDimitry Andric     Hints.push_back(Phys);
433139f7f9bSDimitry Andric   }
4342cab237bSDimitry Andric   return false;
4352cab237bSDimitry Andric }
43639d628a0SDimitry Andric 
canRealignStack(const MachineFunction & MF) const4377d523365SDimitry Andric bool TargetRegisterInfo::canRealignStack(const MachineFunction &MF) const {
4382cab237bSDimitry Andric   return !MF.getFunction().hasFnAttribute("no-realign-stack");
4397d523365SDimitry Andric }
4407d523365SDimitry Andric 
needsStackRealignment(const MachineFunction & MF) const4417d523365SDimitry Andric bool TargetRegisterInfo::needsStackRealignment(
4427d523365SDimitry Andric     const MachineFunction &MF) const {
443d88c1a5aSDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4447d523365SDimitry Andric   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
4452cab237bSDimitry Andric   const Function &F = MF.getFunction();
4467d523365SDimitry Andric   unsigned StackAlign = TFI->getStackAlignment();
447d88c1a5aSDimitry Andric   bool requiresRealignment = ((MFI.getMaxAlignment() > StackAlign) ||
4482cab237bSDimitry Andric                               F.hasFnAttribute(Attribute::StackAlignment));
4492cab237bSDimitry Andric   if (F.hasFnAttribute("stackrealign") || requiresRealignment) {
4507d523365SDimitry Andric     if (canRealignStack(MF))
4517d523365SDimitry Andric       return true;
4524ba319b5SDimitry Andric     LLVM_DEBUG(dbgs() << "Can't realign function's stack: " << F.getName()
4534ba319b5SDimitry Andric                       << "\n");
4547d523365SDimitry Andric   }
4557d523365SDimitry Andric   return false;
4567d523365SDimitry Andric }
4577d523365SDimitry Andric 
regmaskSubsetEqual(const uint32_t * mask0,const uint32_t * mask1) const4583ca95b02SDimitry Andric bool TargetRegisterInfo::regmaskSubsetEqual(const uint32_t *mask0,
4593ca95b02SDimitry Andric                                             const uint32_t *mask1) const {
4603ca95b02SDimitry Andric   unsigned N = (getNumRegs()+31) / 32;
4613ca95b02SDimitry Andric   for (unsigned I = 0; I < N; ++I)
4623ca95b02SDimitry Andric     if ((mask0[I] & mask1[I]) != mask0[I])
4633ca95b02SDimitry Andric       return false;
4643ca95b02SDimitry Andric   return true;
4653ca95b02SDimitry Andric }
4663ca95b02SDimitry Andric 
getRegSizeInBits(unsigned Reg,const MachineRegisterInfo & MRI) const4674ba319b5SDimitry Andric unsigned TargetRegisterInfo::getRegSizeInBits(unsigned Reg,
4684ba319b5SDimitry Andric                                          const MachineRegisterInfo &MRI) const {
4694ba319b5SDimitry Andric   const TargetRegisterClass *RC{};
4704ba319b5SDimitry Andric   if (isPhysicalRegister(Reg)) {
4714ba319b5SDimitry Andric     // The size is not directly available for physical registers.
4724ba319b5SDimitry Andric     // Instead, we need to access a register class that contains Reg and
4734ba319b5SDimitry Andric     // get the size of that register class.
4744ba319b5SDimitry Andric     RC = getMinimalPhysRegClass(Reg);
4754ba319b5SDimitry Andric   } else {
4764ba319b5SDimitry Andric     LLT Ty = MRI.getType(Reg);
4774ba319b5SDimitry Andric     unsigned RegSize = Ty.isValid() ? Ty.getSizeInBits() : 0;
4784ba319b5SDimitry Andric     // If Reg is not a generic register, query the register class to
4794ba319b5SDimitry Andric     // get its size.
4804ba319b5SDimitry Andric     if (RegSize)
4814ba319b5SDimitry Andric       return RegSize;
4824ba319b5SDimitry Andric     // Since Reg is not a generic register, it must have a register class.
4834ba319b5SDimitry Andric     RC = MRI.getRegClass(Reg);
4844ba319b5SDimitry Andric   }
4854ba319b5SDimitry Andric   assert(RC && "Unable to deduce the register class");
4864ba319b5SDimitry Andric   return getRegSizeInBits(*RC);
4874ba319b5SDimitry Andric }
4884ba319b5SDimitry Andric 
4894ba319b5SDimitry Andric unsigned
lookThruCopyLike(unsigned SrcReg,const MachineRegisterInfo * MRI) const4904ba319b5SDimitry Andric TargetRegisterInfo::lookThruCopyLike(unsigned SrcReg,
4914ba319b5SDimitry Andric                                      const MachineRegisterInfo *MRI) const {
4924ba319b5SDimitry Andric   while (true) {
4934ba319b5SDimitry Andric     const MachineInstr *MI = MRI->getVRegDef(SrcReg);
4944ba319b5SDimitry Andric     if (!MI->isCopyLike())
4954ba319b5SDimitry Andric       return SrcReg;
4964ba319b5SDimitry Andric 
4974ba319b5SDimitry Andric     unsigned CopySrcReg;
4984ba319b5SDimitry Andric     if (MI->isCopy())
4994ba319b5SDimitry Andric       CopySrcReg = MI->getOperand(1).getReg();
5004ba319b5SDimitry Andric     else {
5014ba319b5SDimitry Andric       assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
5024ba319b5SDimitry Andric       CopySrcReg = MI->getOperand(2).getReg();
5034ba319b5SDimitry Andric     }
5044ba319b5SDimitry Andric 
5054ba319b5SDimitry Andric     if (!isVirtualRegister(CopySrcReg))
5064ba319b5SDimitry Andric       return CopySrcReg;
5074ba319b5SDimitry Andric 
5084ba319b5SDimitry Andric     SrcReg = CopySrcReg;
5094ba319b5SDimitry Andric   }
5104ba319b5SDimitry Andric }
5114ba319b5SDimitry Andric 
51239d628a0SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
5137a7e6055SDimitry Andric LLVM_DUMP_METHOD
dumpReg(unsigned Reg,unsigned SubRegIndex,const TargetRegisterInfo * TRI)5147a7e6055SDimitry Andric void TargetRegisterInfo::dumpReg(unsigned Reg, unsigned SubRegIndex,
51539d628a0SDimitry Andric                                  const TargetRegisterInfo *TRI) {
5162cab237bSDimitry Andric   dbgs() << printReg(Reg, TRI, SubRegIndex) << "\n";
51739d628a0SDimitry Andric }
51839d628a0SDimitry Andric #endif
519