xref: /src/contrib/llvm-project/llvm/lib/CodeGen/TargetRegisterInfo.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
108bbd35aSDimitry Andric //==- TargetRegisterInfo.cpp - Target Register Information Implementation --==//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file implements the TargetRegisterInfo interface.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten 
13044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
1408bbd35aSDimitry Andric #include "llvm/ADT/ArrayRef.h"
15009b1c42SEd Schouten #include "llvm/ADT/BitVector.h"
1608bbd35aSDimitry Andric #include "llvm/ADT/STLExtras.h"
17cfca06d7SDimitry Andric #include "llvm/ADT/SmallSet.h"
18044eb2f6SDimitry Andric #include "llvm/ADT/StringExtras.h"
19145449b1SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
20145449b1SDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
21dd58ef01SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
224a16efa3SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
234a16efa3SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
24044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
25cfca06d7SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
26044eb2f6SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
274a16efa3SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
28ac9a064cSDimitry Andric #include "llvm/CodeGenTypes/MachineValueType.h"
29eb11fae6SDimitry Andric #include "llvm/Config/llvm-config.h"
3008bbd35aSDimitry Andric #include "llvm/IR/Attributes.h"
31b60736ecSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
32dd58ef01SDimitry Andric #include "llvm/IR/Function.h"
3308bbd35aSDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
34cfca06d7SDimitry Andric #include "llvm/Support/CommandLine.h"
3508bbd35aSDimitry Andric #include "llvm/Support/Compiler.h"
3667c32a98SDimitry Andric #include "llvm/Support/Debug.h"
3708bbd35aSDimitry Andric #include "llvm/Support/MathExtras.h"
3808bbd35aSDimitry Andric #include "llvm/Support/Printable.h"
39cf099d11SDimitry Andric #include "llvm/Support/raw_ostream.h"
4008bbd35aSDimitry Andric #include <cassert>
4108bbd35aSDimitry Andric #include <utility>
42dd58ef01SDimitry Andric 
43dd58ef01SDimitry Andric #define DEBUG_TYPE "target-reg-info"
44009b1c42SEd Schouten 
45009b1c42SEd Schouten using namespace llvm;
46009b1c42SEd Schouten 
47cfca06d7SDimitry Andric static cl::opt<unsigned>
48cfca06d7SDimitry Andric     HugeSizeForSplit("huge-size-for-split", cl::Hidden,
49cfca06d7SDimitry Andric                      cl::desc("A threshold of live range size which may cause "
50cfca06d7SDimitry Andric                               "high compile time cost in global splitting."),
51cfca06d7SDimitry Andric                      cl::init(5000));
52cfca06d7SDimitry Andric 
TargetRegisterInfo(const TargetRegisterInfoDesc * ID,regclass_iterator RCB,regclass_iterator RCE,const char * const * SRINames,const SubRegCoveredBits * SubIdxRanges,const LaneBitmask * SRILaneMasks,LaneBitmask SRICoveringLanes,const RegClassInfo * const RCIs,const MVT::SimpleValueType * const RCVTLists,unsigned Mode)53ac9a064cSDimitry Andric TargetRegisterInfo::TargetRegisterInfo(
54ac9a064cSDimitry Andric     const TargetRegisterInfoDesc *ID, regclass_iterator RCB,
55ac9a064cSDimitry Andric     regclass_iterator RCE, const char *const *SRINames,
56ac9a064cSDimitry Andric     const SubRegCoveredBits *SubIdxRanges, const LaneBitmask *SRILaneMasks,
57ac9a064cSDimitry Andric     LaneBitmask SRICoveringLanes, const RegClassInfo *const RCIs,
58ac9a064cSDimitry Andric     const MVT::SimpleValueType *const RCVTLists, unsigned Mode)
59ac9a064cSDimitry Andric     : InfoDesc(ID), SubRegIndexNames(SRINames), SubRegIdxRanges(SubIdxRanges),
60ac9a064cSDimitry Andric       SubRegIndexLaneMasks(SRILaneMasks), RegClassBegin(RCB), RegClassEnd(RCE),
61ac9a064cSDimitry Andric       CoveringLanes(SRICoveringLanes), RCInfos(RCIs), RCVTLists(RCVTLists),
62ac9a064cSDimitry Andric       HwMode(Mode) {}
63009b1c42SEd Schouten 
6408bbd35aSDimitry Andric TargetRegisterInfo::~TargetRegisterInfo() = default;
65009b1c42SEd Schouten 
shouldRegionSplitForVirtReg(const MachineFunction & MF,const LiveInterval & VirtReg) const66cfca06d7SDimitry Andric bool TargetRegisterInfo::shouldRegionSplitForVirtReg(
67cfca06d7SDimitry Andric     const MachineFunction &MF, const LiveInterval &VirtReg) const {
68cfca06d7SDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
69cfca06d7SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
70b60736ecSDimitry Andric   MachineInstr *MI = MRI.getUniqueVRegDef(VirtReg.reg());
71cfca06d7SDimitry Andric   if (MI && TII->isTriviallyReMaterializable(*MI) &&
72cfca06d7SDimitry Andric       VirtReg.size() > HugeSizeForSplit)
73cfca06d7SDimitry Andric     return false;
74cfca06d7SDimitry Andric   return true;
75cfca06d7SDimitry Andric }
76cfca06d7SDimitry Andric 
markSuperRegs(BitVector & RegisterSet,MCRegister Reg) const77cfca06d7SDimitry Andric void TargetRegisterInfo::markSuperRegs(BitVector &RegisterSet,
78cfca06d7SDimitry Andric                                        MCRegister Reg) const {
797fa27ce4SDimitry Andric   for (MCPhysReg SR : superregs_inclusive(Reg))
807fa27ce4SDimitry Andric     RegisterSet.set(SR);
81b915e9e0SDimitry Andric }
82b915e9e0SDimitry Andric 
checkAllSuperRegsMarked(const BitVector & RegisterSet,ArrayRef<MCPhysReg> Exceptions) const83b915e9e0SDimitry Andric bool TargetRegisterInfo::checkAllSuperRegsMarked(const BitVector &RegisterSet,
84b915e9e0SDimitry Andric     ArrayRef<MCPhysReg> Exceptions) const {
85b915e9e0SDimitry Andric   // Check that all super registers of reserved regs are reserved as well.
86b915e9e0SDimitry Andric   BitVector Checked(getNumRegs());
877af96fb3SDimitry Andric   for (unsigned Reg : RegisterSet.set_bits()) {
88b915e9e0SDimitry Andric     if (Checked[Reg])
89b915e9e0SDimitry Andric       continue;
907fa27ce4SDimitry Andric     for (MCPhysReg SR : superregs(Reg)) {
917fa27ce4SDimitry Andric       if (!RegisterSet[SR] && !is_contained(Exceptions, Reg)) {
927fa27ce4SDimitry Andric         dbgs() << "Error: Super register " << printReg(SR, this)
93044eb2f6SDimitry Andric                << " of reserved register " << printReg(Reg, this)
94b915e9e0SDimitry Andric                << " is not reserved.\n";
95b915e9e0SDimitry Andric         return false;
96b915e9e0SDimitry Andric       }
97b915e9e0SDimitry Andric 
98b915e9e0SDimitry Andric       // We transitively check superregs. So we can remember this for later
99b915e9e0SDimitry Andric       // to avoid compiletime explosion in deep register hierarchies.
1007fa27ce4SDimitry Andric       Checked.set(SR);
101b915e9e0SDimitry Andric     }
102b915e9e0SDimitry Andric   }
103b915e9e0SDimitry Andric   return true;
104b915e9e0SDimitry Andric }
105b915e9e0SDimitry Andric 
106dd58ef01SDimitry Andric namespace llvm {
107dd58ef01SDimitry Andric 
printReg(Register Reg,const TargetRegisterInfo * TRI,unsigned SubIdx,const MachineRegisterInfo * MRI)1081d5ae102SDimitry Andric Printable printReg(Register Reg, const TargetRegisterInfo *TRI,
109eb11fae6SDimitry Andric                    unsigned SubIdx, const MachineRegisterInfo *MRI) {
110eb11fae6SDimitry Andric   return Printable([Reg, TRI, SubIdx, MRI](raw_ostream &OS) {
111cf099d11SDimitry Andric     if (!Reg)
112eb11fae6SDimitry Andric       OS << "$noreg";
1131d5ae102SDimitry Andric     else if (Register::isStackSlot(Reg))
1141d5ae102SDimitry Andric       OS << "SS#" << Register::stackSlot2Index(Reg);
115e3b55780SDimitry Andric     else if (Reg.isVirtual()) {
116eb11fae6SDimitry Andric       StringRef Name = MRI ? MRI->getVRegName(Reg) : "";
117eb11fae6SDimitry Andric       if (Name != "") {
118eb11fae6SDimitry Andric         OS << '%' << Name;
119eb11fae6SDimitry Andric       } else {
1201d5ae102SDimitry Andric         OS << '%' << Register::virtReg2Index(Reg);
121eb11fae6SDimitry Andric       }
1221d5ae102SDimitry Andric     } else if (!TRI)
123eb11fae6SDimitry Andric       OS << '$' << "physreg" << Reg;
124044eb2f6SDimitry Andric     else if (Reg < TRI->getNumRegs()) {
125eb11fae6SDimitry Andric       OS << '$';
126044eb2f6SDimitry Andric       printLowerCase(TRI->getName(Reg), OS);
127044eb2f6SDimitry Andric     } else
128044eb2f6SDimitry Andric       llvm_unreachable("Register kind is unsupported.");
129044eb2f6SDimitry Andric 
130cf099d11SDimitry Andric     if (SubIdx) {
131cf099d11SDimitry Andric       if (TRI)
132cf099d11SDimitry Andric         OS << ':' << TRI->getSubRegIndexName(SubIdx);
133cf099d11SDimitry Andric       else
134cf099d11SDimitry Andric         OS << ":sub(" << SubIdx << ')';
135cf099d11SDimitry Andric     }
136dd58ef01SDimitry Andric   });
137cf099d11SDimitry Andric }
138cf099d11SDimitry Andric 
printRegUnit(unsigned Unit,const TargetRegisterInfo * TRI)139044eb2f6SDimitry Andric Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
140dd58ef01SDimitry Andric   return Printable([Unit, TRI](raw_ostream &OS) {
14158b69754SDimitry Andric     // Generic printout when TRI is missing.
14258b69754SDimitry Andric     if (!TRI) {
14358b69754SDimitry Andric       OS << "Unit~" << Unit;
14458b69754SDimitry Andric       return;
14558b69754SDimitry Andric     }
14658b69754SDimitry Andric 
14758b69754SDimitry Andric     // Check for invalid register units.
14858b69754SDimitry Andric     if (Unit >= TRI->getNumRegUnits()) {
14958b69754SDimitry Andric       OS << "BadUnit~" << Unit;
15058b69754SDimitry Andric       return;
15158b69754SDimitry Andric     }
15258b69754SDimitry Andric 
15358b69754SDimitry Andric     // Normal units have at least one root.
15458b69754SDimitry Andric     MCRegUnitRootIterator Roots(Unit, TRI);
15558b69754SDimitry Andric     assert(Roots.isValid() && "Unit has no roots.");
15658b69754SDimitry Andric     OS << TRI->getName(*Roots);
15758b69754SDimitry Andric     for (++Roots; Roots.isValid(); ++Roots)
15858b69754SDimitry Andric       OS << '~' << TRI->getName(*Roots);
159dd58ef01SDimitry Andric   });
16058b69754SDimitry Andric }
16158b69754SDimitry Andric 
printVRegOrUnit(unsigned Unit,const TargetRegisterInfo * TRI)162044eb2f6SDimitry Andric Printable printVRegOrUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
163dd58ef01SDimitry Andric   return Printable([Unit, TRI](raw_ostream &OS) {
1641d5ae102SDimitry Andric     if (Register::isVirtualRegister(Unit)) {
1651d5ae102SDimitry Andric       OS << '%' << Register::virtReg2Index(Unit);
166dd58ef01SDimitry Andric     } else {
167044eb2f6SDimitry Andric       OS << printRegUnit(Unit, TRI);
168044eb2f6SDimitry Andric     }
169044eb2f6SDimitry Andric   });
170044eb2f6SDimitry Andric }
171044eb2f6SDimitry Andric 
printRegClassOrBank(Register Reg,const MachineRegisterInfo & RegInfo,const TargetRegisterInfo * TRI)172cfca06d7SDimitry Andric Printable printRegClassOrBank(Register Reg, const MachineRegisterInfo &RegInfo,
173044eb2f6SDimitry Andric                               const TargetRegisterInfo *TRI) {
174044eb2f6SDimitry Andric   return Printable([Reg, &RegInfo, TRI](raw_ostream &OS) {
175044eb2f6SDimitry Andric     if (RegInfo.getRegClassOrNull(Reg))
176044eb2f6SDimitry Andric       OS << StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
177044eb2f6SDimitry Andric     else if (RegInfo.getRegBankOrNull(Reg))
178044eb2f6SDimitry Andric       OS << StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
179044eb2f6SDimitry Andric     else {
180044eb2f6SDimitry Andric       OS << "_";
181044eb2f6SDimitry Andric       assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
182044eb2f6SDimitry Andric              "Generic registers must have a valid type");
183f8af5cf6SDimitry Andric     }
184dd58ef01SDimitry Andric   });
185f8af5cf6SDimitry Andric }
186f8af5cf6SDimitry Andric 
18708bbd35aSDimitry Andric } // end namespace llvm
188dd58ef01SDimitry Andric 
18958b69754SDimitry Andric /// getAllocatableClass - Return the maximal subclass of the given register
19058b69754SDimitry Andric /// class that is alloctable, or NULL.
19158b69754SDimitry Andric const TargetRegisterClass *
getAllocatableClass(const TargetRegisterClass * RC) const19258b69754SDimitry Andric TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
19358b69754SDimitry Andric   if (!RC || RC->isAllocatable())
19458b69754SDimitry Andric     return RC;
19558b69754SDimitry Andric 
19601095a5dSDimitry Andric   for (BitMaskClassIterator It(RC->getSubClassMask(), *this); It.isValid();
19701095a5dSDimitry Andric        ++It) {
19801095a5dSDimitry Andric     const TargetRegisterClass *SubRC = getRegClass(It.getID());
19958b69754SDimitry Andric     if (SubRC->isAllocatable())
20058b69754SDimitry Andric       return SubRC;
20158b69754SDimitry Andric   }
2025ca98fd9SDimitry Andric   return nullptr;
20358b69754SDimitry Andric }
20458b69754SDimitry Andric 
20566e41e3cSRoman Divacky /// getMinimalPhysRegClass - Returns the Register Class of a physical
20666e41e3cSRoman Divacky /// register of the given type, picking the most sub register class of
20766e41e3cSRoman Divacky /// the right type that contains this physreg.
208009b1c42SEd Schouten const TargetRegisterClass *
getMinimalPhysRegClass(MCRegister reg,MVT VT) const209cfca06d7SDimitry Andric TargetRegisterInfo::getMinimalPhysRegClass(MCRegister reg, MVT VT) const {
2101d5ae102SDimitry Andric   assert(Register::isPhysicalRegister(reg) &&
2111d5ae102SDimitry Andric          "reg must be a physical register");
212009b1c42SEd Schouten 
21366e41e3cSRoman Divacky   // Pick the most sub register class of the right type that contains
214009b1c42SEd Schouten   // this physreg.
2155ca98fd9SDimitry Andric   const TargetRegisterClass* BestRC = nullptr;
21671d5a254SDimitry Andric   for (const TargetRegisterClass* RC : regclasses()) {
21712f3ca4cSDimitry Andric     if ((VT == MVT::Other || isTypeLegalForClass(*RC, VT)) &&
21812f3ca4cSDimitry Andric         RC->contains(reg) && (!BestRC || BestRC->hasSubClass(RC)))
219009b1c42SEd Schouten       BestRC = RC;
220009b1c42SEd Schouten   }
221009b1c42SEd Schouten 
222009b1c42SEd Schouten   assert(BestRC && "Couldn't find the register class");
223009b1c42SEd Schouten   return BestRC;
224009b1c42SEd Schouten }
225009b1c42SEd Schouten 
226344a3780SDimitry Andric const TargetRegisterClass *
getMinimalPhysRegClassLLT(MCRegister reg,LLT Ty) const227344a3780SDimitry Andric TargetRegisterInfo::getMinimalPhysRegClassLLT(MCRegister reg, LLT Ty) const {
228344a3780SDimitry Andric   assert(Register::isPhysicalRegister(reg) &&
229344a3780SDimitry Andric          "reg must be a physical register");
230344a3780SDimitry Andric 
231344a3780SDimitry Andric   // Pick the most sub register class of the right type that contains
232344a3780SDimitry Andric   // this physreg.
233344a3780SDimitry Andric   const TargetRegisterClass *BestRC = nullptr;
234344a3780SDimitry Andric   for (const TargetRegisterClass *RC : regclasses()) {
235344a3780SDimitry Andric     if ((!Ty.isValid() || isTypeLegalForClass(*RC, Ty)) && RC->contains(reg) &&
236344a3780SDimitry Andric         (!BestRC || BestRC->hasSubClass(RC)))
237344a3780SDimitry Andric       BestRC = RC;
238344a3780SDimitry Andric   }
239344a3780SDimitry Andric 
240344a3780SDimitry Andric   return BestRC;
241344a3780SDimitry Andric }
242344a3780SDimitry Andric 
243009b1c42SEd Schouten /// getAllocatableSetForRC - Toggle the bits that represent allocatable
244009b1c42SEd Schouten /// registers for the specific register class.
getAllocatableSetForRC(const MachineFunction & MF,const TargetRegisterClass * RC,BitVector & R)24559850d08SRoman Divacky static void getAllocatableSetForRC(const MachineFunction &MF,
246009b1c42SEd Schouten                                    const TargetRegisterClass *RC, BitVector &R){
24758b69754SDimitry Andric   assert(RC->isAllocatable() && "invalid for nonallocatable sets");
2485ca98fd9SDimitry Andric   ArrayRef<MCPhysReg> Order = RC->getRawAllocationOrder(MF);
24977fc4c14SDimitry Andric   for (MCPhysReg PR : Order)
25077fc4c14SDimitry Andric     R.set(PR);
251009b1c42SEd Schouten }
252009b1c42SEd Schouten 
getAllocatableSet(const MachineFunction & MF,const TargetRegisterClass * RC) const25359850d08SRoman Divacky BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
254009b1c42SEd Schouten                                           const TargetRegisterClass *RC) const {
255411bd29eSDimitry Andric   BitVector Allocatable(getNumRegs());
256009b1c42SEd Schouten   if (RC) {
25758b69754SDimitry Andric     // A register class with no allocatable subclass returns an empty set.
25858b69754SDimitry Andric     const TargetRegisterClass *SubClass = getAllocatableClass(RC);
25958b69754SDimitry Andric     if (SubClass)
26058b69754SDimitry Andric       getAllocatableSetForRC(MF, SubClass, Allocatable);
261d39c594dSDimitry Andric   } else {
26271d5a254SDimitry Andric     for (const TargetRegisterClass *C : regclasses())
26371d5a254SDimitry Andric       if (C->isAllocatable())
26471d5a254SDimitry Andric         getAllocatableSetForRC(MF, C, Allocatable);
265d39c594dSDimitry Andric   }
266d39c594dSDimitry Andric 
267d39c594dSDimitry Andric   // Mask out the reserved registers
268344a3780SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
269344a3780SDimitry Andric   const BitVector &Reserved = MRI.getReservedRegs();
270344a3780SDimitry Andric   Allocatable.reset(Reserved);
271d39c594dSDimitry Andric 
272009b1c42SEd Schouten   return Allocatable;
273009b1c42SEd Schouten }
274009b1c42SEd Schouten 
27558b69754SDimitry Andric static inline
firstCommonClass(const uint32_t * A,const uint32_t * B,const TargetRegisterInfo * TRI)27658b69754SDimitry Andric const TargetRegisterClass *firstCommonClass(const uint32_t *A,
27758b69754SDimitry Andric                                             const uint32_t *B,
2781d5ae102SDimitry Andric                                             const TargetRegisterInfo *TRI) {
27958b69754SDimitry Andric   for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
2801d5ae102SDimitry Andric     if (unsigned Common = *A++ & *B++)
2817fa27ce4SDimitry Andric       return TRI->getRegClass(I + llvm::countr_zero(Common));
2825ca98fd9SDimitry Andric   return nullptr;
28358b69754SDimitry Andric }
28458b69754SDimitry Andric 
285009b1c42SEd Schouten const TargetRegisterClass *
getCommonSubClass(const TargetRegisterClass * A,const TargetRegisterClass * B) const28630815c53SDimitry Andric TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
2871d5ae102SDimitry Andric                                       const TargetRegisterClass *B) const {
28830815c53SDimitry Andric   // First take care of the trivial cases.
289009b1c42SEd Schouten   if (A == B)
290009b1c42SEd Schouten     return A;
291009b1c42SEd Schouten   if (!A || !B)
2925ca98fd9SDimitry Andric     return nullptr;
293009b1c42SEd Schouten 
29430815c53SDimitry Andric   // Register classes are ordered topologically, so the largest common
29530815c53SDimitry Andric   // sub-class it the common sub-class with the smallest ID.
2961d5ae102SDimitry Andric   return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this);
29758b69754SDimitry Andric }
298009b1c42SEd Schouten 
29958b69754SDimitry Andric const TargetRegisterClass *
getMatchingSuperRegClass(const TargetRegisterClass * A,const TargetRegisterClass * B,unsigned Idx) const30058b69754SDimitry Andric TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
30158b69754SDimitry Andric                                              const TargetRegisterClass *B,
30258b69754SDimitry Andric                                              unsigned Idx) const {
30358b69754SDimitry Andric   assert(A && B && "Missing register class");
30458b69754SDimitry Andric   assert(Idx && "Bad sub-register index");
305009b1c42SEd Schouten 
30658b69754SDimitry Andric   // Find Idx in the list of super-register indices.
30758b69754SDimitry Andric   for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
30858b69754SDimitry Andric     if (RCI.getSubReg() == Idx)
30958b69754SDimitry Andric       // The bit mask contains all register classes that are projected into B
31058b69754SDimitry Andric       // by Idx. Find a class that is also a sub-class of A.
31158b69754SDimitry Andric       return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this);
3125ca98fd9SDimitry Andric   return nullptr;
31358b69754SDimitry Andric }
31458b69754SDimitry Andric 
31558b69754SDimitry Andric const TargetRegisterClass *TargetRegisterInfo::
getCommonSuperRegClass(const TargetRegisterClass * RCA,unsigned SubA,const TargetRegisterClass * RCB,unsigned SubB,unsigned & PreA,unsigned & PreB) const31658b69754SDimitry Andric getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
31758b69754SDimitry Andric                        const TargetRegisterClass *RCB, unsigned SubB,
31858b69754SDimitry Andric                        unsigned &PreA, unsigned &PreB) const {
31958b69754SDimitry Andric   assert(RCA && SubA && RCB && SubB && "Invalid arguments");
32058b69754SDimitry Andric 
32158b69754SDimitry Andric   // Search all pairs of sub-register indices that project into RCA and RCB
32258b69754SDimitry Andric   // respectively. This is quadratic, but usually the sets are very small. On
32358b69754SDimitry Andric   // most targets like X86, there will only be a single sub-register index
32458b69754SDimitry Andric   // (e.g., sub_16bit projecting into GR16).
32558b69754SDimitry Andric   //
32658b69754SDimitry Andric   // The worst case is a register class like DPR on ARM.
32758b69754SDimitry Andric   // We have indices dsub_0..dsub_7 projecting into that class.
32858b69754SDimitry Andric   //
32958b69754SDimitry Andric   // It is very common that one register class is a sub-register of the other.
33058b69754SDimitry Andric   // Arrange for RCA to be the larger register so the answer will be found in
33158b69754SDimitry Andric   // the first iteration. This makes the search linear for the most common
33258b69754SDimitry Andric   // case.
3335ca98fd9SDimitry Andric   const TargetRegisterClass *BestRC = nullptr;
33458b69754SDimitry Andric   unsigned *BestPreA = &PreA;
33558b69754SDimitry Andric   unsigned *BestPreB = &PreB;
33612f3ca4cSDimitry Andric   if (getRegSizeInBits(*RCA) < getRegSizeInBits(*RCB)) {
33758b69754SDimitry Andric     std::swap(RCA, RCB);
33858b69754SDimitry Andric     std::swap(SubA, SubB);
33958b69754SDimitry Andric     std::swap(BestPreA, BestPreB);
34058b69754SDimitry Andric   }
34158b69754SDimitry Andric 
34258b69754SDimitry Andric   // Also terminate the search one we have found a register class as small as
34358b69754SDimitry Andric   // RCA.
34412f3ca4cSDimitry Andric   unsigned MinSize = getRegSizeInBits(*RCA);
34558b69754SDimitry Andric 
34658b69754SDimitry Andric   for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
34758b69754SDimitry Andric     unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
34858b69754SDimitry Andric     for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
34958b69754SDimitry Andric       // Check if a common super-register class exists for this index pair.
35058b69754SDimitry Andric       const TargetRegisterClass *RC =
35158b69754SDimitry Andric         firstCommonClass(IA.getMask(), IB.getMask(), this);
35212f3ca4cSDimitry Andric       if (!RC || getRegSizeInBits(*RC) < MinSize)
35358b69754SDimitry Andric         continue;
35458b69754SDimitry Andric 
35558b69754SDimitry Andric       // The indexes must compose identically: PreA+SubA == PreB+SubB.
35658b69754SDimitry Andric       unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
35758b69754SDimitry Andric       if (FinalA != FinalB)
35858b69754SDimitry Andric         continue;
35958b69754SDimitry Andric 
36058b69754SDimitry Andric       // Is RC a better candidate than BestRC?
36112f3ca4cSDimitry Andric       if (BestRC && getRegSizeInBits(*RC) >= getRegSizeInBits(*BestRC))
36258b69754SDimitry Andric         continue;
36358b69754SDimitry Andric 
36458b69754SDimitry Andric       // Yes, RC is the smallest super-register seen so far.
36558b69754SDimitry Andric       BestRC = RC;
36658b69754SDimitry Andric       *BestPreA = IA.getSubReg();
36758b69754SDimitry Andric       *BestPreB = IB.getSubReg();
36858b69754SDimitry Andric 
36958b69754SDimitry Andric       // Bail early if we reached MinSize. We won't find a better candidate.
37012f3ca4cSDimitry Andric       if (getRegSizeInBits(*BestRC) == MinSize)
37158b69754SDimitry Andric         return BestRC;
37258b69754SDimitry Andric     }
37358b69754SDimitry Andric   }
37458b69754SDimitry Andric   return BestRC;
375009b1c42SEd Schouten }
3764a16efa3SDimitry Andric 
377eb11fae6SDimitry Andric /// Check if the registers defined by the pair (RegisterClass, SubReg)
378dd58ef01SDimitry Andric /// share the same register file.
shareSameRegisterFile(const TargetRegisterInfo & TRI,const TargetRegisterClass * DefRC,unsigned DefSubReg,const TargetRegisterClass * SrcRC,unsigned SrcSubReg)379dd58ef01SDimitry Andric static bool shareSameRegisterFile(const TargetRegisterInfo &TRI,
380dd58ef01SDimitry Andric                                   const TargetRegisterClass *DefRC,
381dd58ef01SDimitry Andric                                   unsigned DefSubReg,
382dd58ef01SDimitry Andric                                   const TargetRegisterClass *SrcRC,
383dd58ef01SDimitry Andric                                   unsigned SrcSubReg) {
384dd58ef01SDimitry Andric   // Same register class.
385dd58ef01SDimitry Andric   if (DefRC == SrcRC)
386dd58ef01SDimitry Andric     return true;
387dd58ef01SDimitry Andric 
388dd58ef01SDimitry Andric   // Both operands are sub registers. Check if they share a register class.
389dd58ef01SDimitry Andric   unsigned SrcIdx, DefIdx;
390dd58ef01SDimitry Andric   if (SrcSubReg && DefSubReg) {
391dd58ef01SDimitry Andric     return TRI.getCommonSuperRegClass(SrcRC, SrcSubReg, DefRC, DefSubReg,
392dd58ef01SDimitry Andric                                       SrcIdx, DefIdx) != nullptr;
393dd58ef01SDimitry Andric   }
394dd58ef01SDimitry Andric 
395dd58ef01SDimitry Andric   // At most one of the register is a sub register, make it Src to avoid
396dd58ef01SDimitry Andric   // duplicating the test.
397dd58ef01SDimitry Andric   if (!SrcSubReg) {
398dd58ef01SDimitry Andric     std::swap(DefSubReg, SrcSubReg);
399dd58ef01SDimitry Andric     std::swap(DefRC, SrcRC);
400dd58ef01SDimitry Andric   }
401dd58ef01SDimitry Andric 
402dd58ef01SDimitry Andric   // One of the register is a sub register, check if we can get a superclass.
403dd58ef01SDimitry Andric   if (SrcSubReg)
404dd58ef01SDimitry Andric     return TRI.getMatchingSuperRegClass(SrcRC, DefRC, SrcSubReg) != nullptr;
405dd58ef01SDimitry Andric 
406dd58ef01SDimitry Andric   // Plain copy.
407dd58ef01SDimitry Andric   return TRI.getCommonSubClass(DefRC, SrcRC) != nullptr;
408dd58ef01SDimitry Andric }
409dd58ef01SDimitry Andric 
shouldRewriteCopySrc(const TargetRegisterClass * DefRC,unsigned DefSubReg,const TargetRegisterClass * SrcRC,unsigned SrcSubReg) const410dd58ef01SDimitry Andric bool TargetRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
411dd58ef01SDimitry Andric                                               unsigned DefSubReg,
412dd58ef01SDimitry Andric                                               const TargetRegisterClass *SrcRC,
413dd58ef01SDimitry Andric                                               unsigned SrcSubReg) const {
414dd58ef01SDimitry Andric   // If this source does not incur a cross register bank copy, use it.
415dd58ef01SDimitry Andric   return shareSameRegisterFile(*this, DefRC, DefSubReg, SrcRC, SrcSubReg);
416dd58ef01SDimitry Andric }
417dd58ef01SDimitry Andric 
4184a16efa3SDimitry Andric // Compute target-independent register allocator hints to help eliminate copies.
getRegAllocationHints(Register VirtReg,ArrayRef<MCPhysReg> Order,SmallVectorImpl<MCPhysReg> & Hints,const MachineFunction & MF,const VirtRegMap * VRM,const LiveRegMatrix * Matrix) const419cfca06d7SDimitry Andric bool TargetRegisterInfo::getRegAllocationHints(
420cfca06d7SDimitry Andric     Register VirtReg, ArrayRef<MCPhysReg> Order,
421cfca06d7SDimitry Andric     SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
422cfca06d7SDimitry Andric     const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
4234a16efa3SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
4247fa27ce4SDimitry Andric   const std::pair<unsigned, SmallVector<Register, 4>> &Hints_MRI =
425044eb2f6SDimitry Andric       MRI.getRegAllocationHints(VirtReg);
4264a16efa3SDimitry Andric 
427cfca06d7SDimitry Andric   SmallSet<Register, 32> HintedRegs;
428044eb2f6SDimitry Andric   // First hint may be a target hint.
429044eb2f6SDimitry Andric   bool Skip = (Hints_MRI.first != 0);
430044eb2f6SDimitry Andric   for (auto Reg : Hints_MRI.second) {
431044eb2f6SDimitry Andric     if (Skip) {
432044eb2f6SDimitry Andric       Skip = false;
433044eb2f6SDimitry Andric       continue;
434044eb2f6SDimitry Andric     }
4354a16efa3SDimitry Andric 
4364a16efa3SDimitry Andric     // Target-independent hints are either a physical or a virtual register.
437cfca06d7SDimitry Andric     Register Phys = Reg;
438cfca06d7SDimitry Andric     if (VRM && Phys.isVirtual())
4394a16efa3SDimitry Andric       Phys = VRM->getPhys(Phys);
4404a16efa3SDimitry Andric 
441e6d15924SDimitry Andric     // Don't add the same reg twice (Hints_MRI may contain multiple virtual
442e6d15924SDimitry Andric     // registers allocated to the same physreg).
443e6d15924SDimitry Andric     if (!HintedRegs.insert(Phys).second)
444e6d15924SDimitry Andric       continue;
4454a16efa3SDimitry Andric     // Check that Phys is a valid hint in VirtReg's register class.
446cfca06d7SDimitry Andric     if (!Phys.isPhysical())
447044eb2f6SDimitry Andric       continue;
4484a16efa3SDimitry Andric     if (MRI.isReserved(Phys))
449044eb2f6SDimitry Andric       continue;
4504a16efa3SDimitry Andric     // Check that Phys is in the allocation order. We shouldn't heed hints
4514a16efa3SDimitry Andric     // from VirtReg's register class if they aren't in the allocation order. The
4524a16efa3SDimitry Andric     // target probably has a reason for removing the register.
453b915e9e0SDimitry Andric     if (!is_contained(Order, Phys))
454044eb2f6SDimitry Andric       continue;
4554a16efa3SDimitry Andric 
4564a16efa3SDimitry Andric     // All clear, tell the register allocator to prefer this register.
4574a16efa3SDimitry Andric     Hints.push_back(Phys);
4584a16efa3SDimitry Andric   }
459044eb2f6SDimitry Andric   return false;
460044eb2f6SDimitry Andric }
46167c32a98SDimitry Andric 
isCalleeSavedPhysReg(MCRegister PhysReg,const MachineFunction & MF) const4621d5ae102SDimitry Andric bool TargetRegisterInfo::isCalleeSavedPhysReg(
463cfca06d7SDimitry Andric     MCRegister PhysReg, const MachineFunction &MF) const {
4641d5ae102SDimitry Andric   if (PhysReg == 0)
4651d5ae102SDimitry Andric     return false;
4661d5ae102SDimitry Andric   const uint32_t *callerPreservedRegs =
4671d5ae102SDimitry Andric       getCallPreservedMask(MF, MF.getFunction().getCallingConv());
4681d5ae102SDimitry Andric   if (callerPreservedRegs) {
4691d5ae102SDimitry Andric     assert(Register::isPhysicalRegister(PhysReg) &&
4701d5ae102SDimitry Andric            "Expected physical register");
4711d5ae102SDimitry Andric     return (callerPreservedRegs[PhysReg / 32] >> PhysReg % 32) & 1;
4721d5ae102SDimitry Andric   }
4731d5ae102SDimitry Andric   return false;
4741d5ae102SDimitry Andric }
4751d5ae102SDimitry Andric 
canRealignStack(const MachineFunction & MF) const476dd58ef01SDimitry Andric bool TargetRegisterInfo::canRealignStack(const MachineFunction &MF) const {
477ac9a064cSDimitry Andric   return MF.getFrameInfo().isStackRealignable();
478dd58ef01SDimitry Andric }
479dd58ef01SDimitry Andric 
shouldRealignStack(const MachineFunction & MF) const480344a3780SDimitry Andric bool TargetRegisterInfo::shouldRealignStack(const MachineFunction &MF) const {
481ac9a064cSDimitry Andric   return MF.getFrameInfo().shouldRealignStack();
482dd58ef01SDimitry Andric }
483dd58ef01SDimitry Andric 
regmaskSubsetEqual(const uint32_t * mask0,const uint32_t * mask1) const48401095a5dSDimitry Andric bool TargetRegisterInfo::regmaskSubsetEqual(const uint32_t *mask0,
48501095a5dSDimitry Andric                                             const uint32_t *mask1) const {
48601095a5dSDimitry Andric   unsigned N = (getNumRegs()+31) / 32;
48701095a5dSDimitry Andric   for (unsigned I = 0; I < N; ++I)
48801095a5dSDimitry Andric     if ((mask0[I] & mask1[I]) != mask0[I])
48901095a5dSDimitry Andric       return false;
49001095a5dSDimitry Andric   return true;
49101095a5dSDimitry Andric }
49201095a5dSDimitry Andric 
493b1c73532SDimitry Andric TypeSize
getRegSizeInBits(Register Reg,const MachineRegisterInfo & MRI) const494cfca06d7SDimitry Andric TargetRegisterInfo::getRegSizeInBits(Register Reg,
495eb11fae6SDimitry Andric                                      const MachineRegisterInfo &MRI) const {
496eb11fae6SDimitry Andric   const TargetRegisterClass *RC{};
497cfca06d7SDimitry Andric   if (Reg.isPhysical()) {
498eb11fae6SDimitry Andric     // The size is not directly available for physical registers.
499eb11fae6SDimitry Andric     // Instead, we need to access a register class that contains Reg and
500eb11fae6SDimitry Andric     // get the size of that register class.
501eb11fae6SDimitry Andric     RC = getMinimalPhysRegClass(Reg);
502b1c73532SDimitry Andric     assert(RC && "Unable to deduce the register class");
503b1c73532SDimitry Andric     return getRegSizeInBits(*RC);
504eb11fae6SDimitry Andric   }
505b1c73532SDimitry Andric   LLT Ty = MRI.getType(Reg);
506b1c73532SDimitry Andric   if (Ty.isValid())
507b1c73532SDimitry Andric     return Ty.getSizeInBits();
508b1c73532SDimitry Andric 
509b1c73532SDimitry Andric   // Since Reg is not a generic register, it may have a register class.
510b1c73532SDimitry Andric   RC = MRI.getRegClass(Reg);
511eb11fae6SDimitry Andric   assert(RC && "Unable to deduce the register class");
512eb11fae6SDimitry Andric   return getRegSizeInBits(*RC);
513eb11fae6SDimitry Andric }
514eb11fae6SDimitry Andric 
getCoveringSubRegIndexes(const MachineRegisterInfo & MRI,const TargetRegisterClass * RC,LaneBitmask LaneMask,SmallVectorImpl<unsigned> & NeededIndexes) const515344a3780SDimitry Andric bool TargetRegisterInfo::getCoveringSubRegIndexes(
516344a3780SDimitry Andric     const MachineRegisterInfo &MRI, const TargetRegisterClass *RC,
517344a3780SDimitry Andric     LaneBitmask LaneMask, SmallVectorImpl<unsigned> &NeededIndexes) const {
518344a3780SDimitry Andric   SmallVector<unsigned, 8> PossibleIndexes;
519344a3780SDimitry Andric   unsigned BestIdx = 0;
520344a3780SDimitry Andric   unsigned BestCover = 0;
521344a3780SDimitry Andric 
522344a3780SDimitry Andric   for (unsigned Idx = 1, E = getNumSubRegIndices(); Idx < E; ++Idx) {
523344a3780SDimitry Andric     // Is this index even compatible with the given class?
524344a3780SDimitry Andric     if (getSubClassWithSubReg(RC, Idx) != RC)
525344a3780SDimitry Andric       continue;
526344a3780SDimitry Andric     LaneBitmask SubRegMask = getSubRegIndexLaneMask(Idx);
527344a3780SDimitry Andric     // Early exit if we found a perfect match.
528344a3780SDimitry Andric     if (SubRegMask == LaneMask) {
529344a3780SDimitry Andric       BestIdx = Idx;
530344a3780SDimitry Andric       break;
531344a3780SDimitry Andric     }
532344a3780SDimitry Andric 
533344a3780SDimitry Andric     // The index must not cover any lanes outside \p LaneMask.
534344a3780SDimitry Andric     if ((SubRegMask & ~LaneMask).any())
535344a3780SDimitry Andric       continue;
536344a3780SDimitry Andric 
537344a3780SDimitry Andric     unsigned PopCount = SubRegMask.getNumLanes();
538344a3780SDimitry Andric     PossibleIndexes.push_back(Idx);
539344a3780SDimitry Andric     if (PopCount > BestCover) {
540344a3780SDimitry Andric       BestCover = PopCount;
541344a3780SDimitry Andric       BestIdx = Idx;
542344a3780SDimitry Andric     }
543344a3780SDimitry Andric   }
544344a3780SDimitry Andric 
545344a3780SDimitry Andric   // Abort if we cannot possibly implement the COPY with the given indexes.
546344a3780SDimitry Andric   if (BestIdx == 0)
5476f8fc217SDimitry Andric     return false;
548344a3780SDimitry Andric 
549344a3780SDimitry Andric   NeededIndexes.push_back(BestIdx);
550344a3780SDimitry Andric 
551344a3780SDimitry Andric   // Greedy heuristic: Keep iterating keeping the best covering subreg index
552344a3780SDimitry Andric   // each time.
553344a3780SDimitry Andric   LaneBitmask LanesLeft = LaneMask & ~getSubRegIndexLaneMask(BestIdx);
554344a3780SDimitry Andric   while (LanesLeft.any()) {
555344a3780SDimitry Andric     unsigned BestIdx = 0;
556344a3780SDimitry Andric     int BestCover = std::numeric_limits<int>::min();
557344a3780SDimitry Andric     for (unsigned Idx : PossibleIndexes) {
558344a3780SDimitry Andric       LaneBitmask SubRegMask = getSubRegIndexLaneMask(Idx);
559344a3780SDimitry Andric       // Early exit if we found a perfect match.
560344a3780SDimitry Andric       if (SubRegMask == LanesLeft) {
561344a3780SDimitry Andric         BestIdx = Idx;
562344a3780SDimitry Andric         break;
563344a3780SDimitry Andric       }
564344a3780SDimitry Andric 
565e3b55780SDimitry Andric       // Do not cover already-covered lanes to avoid creating cycles
566e3b55780SDimitry Andric       // in copy bundles (= bundle contains copies that write to the
567e3b55780SDimitry Andric       // registers).
568e3b55780SDimitry Andric       if ((SubRegMask & ~LanesLeft).any())
569e3b55780SDimitry Andric         continue;
570e3b55780SDimitry Andric 
571e3b55780SDimitry Andric       // Try to cover as many of the remaining lanes as possible.
572e3b55780SDimitry Andric       const int Cover = (SubRegMask & LanesLeft).getNumLanes();
573344a3780SDimitry Andric       if (Cover > BestCover) {
574344a3780SDimitry Andric         BestCover = Cover;
575344a3780SDimitry Andric         BestIdx = Idx;
576344a3780SDimitry Andric       }
577344a3780SDimitry Andric     }
578344a3780SDimitry Andric 
579344a3780SDimitry Andric     if (BestIdx == 0)
5806f8fc217SDimitry Andric       return false; // Impossible to handle
581344a3780SDimitry Andric 
582344a3780SDimitry Andric     NeededIndexes.push_back(BestIdx);
583344a3780SDimitry Andric 
584344a3780SDimitry Andric     LanesLeft &= ~getSubRegIndexLaneMask(BestIdx);
585344a3780SDimitry Andric   }
586344a3780SDimitry Andric 
587344a3780SDimitry Andric   return BestIdx;
588344a3780SDimitry Andric }
589344a3780SDimitry Andric 
getSubRegIdxSize(unsigned Idx) const590ac9a064cSDimitry Andric unsigned TargetRegisterInfo::getSubRegIdxSize(unsigned Idx) const {
591ac9a064cSDimitry Andric   assert(Idx && Idx < getNumSubRegIndices() &&
592ac9a064cSDimitry Andric          "This is not a subregister index");
593ac9a064cSDimitry Andric   return SubRegIdxRanges[HwMode * getNumSubRegIndices() + Idx].Size;
594ac9a064cSDimitry Andric }
595ac9a064cSDimitry Andric 
getSubRegIdxOffset(unsigned Idx) const596ac9a064cSDimitry Andric unsigned TargetRegisterInfo::getSubRegIdxOffset(unsigned Idx) const {
597ac9a064cSDimitry Andric   assert(Idx && Idx < getNumSubRegIndices() &&
598ac9a064cSDimitry Andric          "This is not a subregister index");
599ac9a064cSDimitry Andric   return SubRegIdxRanges[HwMode * getNumSubRegIndices() + Idx].Offset;
600ac9a064cSDimitry Andric }
601ac9a064cSDimitry Andric 
602cfca06d7SDimitry Andric Register
lookThruCopyLike(Register SrcReg,const MachineRegisterInfo * MRI) const603cfca06d7SDimitry Andric TargetRegisterInfo::lookThruCopyLike(Register SrcReg,
604eb11fae6SDimitry Andric                                      const MachineRegisterInfo *MRI) const {
605eb11fae6SDimitry Andric   while (true) {
606eb11fae6SDimitry Andric     const MachineInstr *MI = MRI->getVRegDef(SrcReg);
607eb11fae6SDimitry Andric     if (!MI->isCopyLike())
608eb11fae6SDimitry Andric       return SrcReg;
609eb11fae6SDimitry Andric 
610cfca06d7SDimitry Andric     Register CopySrcReg;
611eb11fae6SDimitry Andric     if (MI->isCopy())
612eb11fae6SDimitry Andric       CopySrcReg = MI->getOperand(1).getReg();
613eb11fae6SDimitry Andric     else {
614eb11fae6SDimitry Andric       assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
615eb11fae6SDimitry Andric       CopySrcReg = MI->getOperand(2).getReg();
616eb11fae6SDimitry Andric     }
617eb11fae6SDimitry Andric 
618cfca06d7SDimitry Andric     if (!CopySrcReg.isVirtual())
619eb11fae6SDimitry Andric       return CopySrcReg;
620eb11fae6SDimitry Andric 
621eb11fae6SDimitry Andric     SrcReg = CopySrcReg;
622eb11fae6SDimitry Andric   }
623eb11fae6SDimitry Andric }
624eb11fae6SDimitry Andric 
lookThruSingleUseCopyChain(Register SrcReg,const MachineRegisterInfo * MRI) const625b60736ecSDimitry Andric Register TargetRegisterInfo::lookThruSingleUseCopyChain(
626b60736ecSDimitry Andric     Register SrcReg, const MachineRegisterInfo *MRI) const {
627b60736ecSDimitry Andric   while (true) {
628b60736ecSDimitry Andric     const MachineInstr *MI = MRI->getVRegDef(SrcReg);
629b60736ecSDimitry Andric     // Found the real definition, return it if it has a single use.
630b60736ecSDimitry Andric     if (!MI->isCopyLike())
631b60736ecSDimitry Andric       return MRI->hasOneNonDBGUse(SrcReg) ? SrcReg : Register();
632b60736ecSDimitry Andric 
633b60736ecSDimitry Andric     Register CopySrcReg;
634b60736ecSDimitry Andric     if (MI->isCopy())
635b60736ecSDimitry Andric       CopySrcReg = MI->getOperand(1).getReg();
636b60736ecSDimitry Andric     else {
637b60736ecSDimitry Andric       assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
638b60736ecSDimitry Andric       CopySrcReg = MI->getOperand(2).getReg();
639b60736ecSDimitry Andric     }
640b60736ecSDimitry Andric 
641b60736ecSDimitry Andric     // Continue only if the next definition in the chain is for a virtual
642b60736ecSDimitry Andric     // register that has a single use.
643b60736ecSDimitry Andric     if (!CopySrcReg.isVirtual() || !MRI->hasOneNonDBGUse(CopySrcReg))
644b60736ecSDimitry Andric       return Register();
645b60736ecSDimitry Andric 
646b60736ecSDimitry Andric     SrcReg = CopySrcReg;
647b60736ecSDimitry Andric   }
648b60736ecSDimitry Andric }
649b60736ecSDimitry Andric 
getOffsetOpcodes(const StackOffset & Offset,SmallVectorImpl<uint64_t> & Ops) const650b60736ecSDimitry Andric void TargetRegisterInfo::getOffsetOpcodes(
651b60736ecSDimitry Andric     const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const {
652b60736ecSDimitry Andric   assert(!Offset.getScalable() && "Scalable offsets are not handled");
653b60736ecSDimitry Andric   DIExpression::appendOffset(Ops, Offset.getFixed());
654b60736ecSDimitry Andric }
655b60736ecSDimitry Andric 
656b60736ecSDimitry Andric DIExpression *
prependOffsetExpression(const DIExpression * Expr,unsigned PrependFlags,const StackOffset & Offset) const657b60736ecSDimitry Andric TargetRegisterInfo::prependOffsetExpression(const DIExpression *Expr,
658b60736ecSDimitry Andric                                             unsigned PrependFlags,
659b60736ecSDimitry Andric                                             const StackOffset &Offset) const {
660b60736ecSDimitry Andric   assert((PrependFlags &
661b60736ecSDimitry Andric           ~(DIExpression::DerefBefore | DIExpression::DerefAfter |
662b60736ecSDimitry Andric             DIExpression::StackValue | DIExpression::EntryValue)) == 0 &&
663b60736ecSDimitry Andric          "Unsupported prepend flag");
664b60736ecSDimitry Andric   SmallVector<uint64_t, 16> OffsetExpr;
665b60736ecSDimitry Andric   if (PrependFlags & DIExpression::DerefBefore)
666b60736ecSDimitry Andric     OffsetExpr.push_back(dwarf::DW_OP_deref);
667b60736ecSDimitry Andric   getOffsetOpcodes(Offset, OffsetExpr);
668b60736ecSDimitry Andric   if (PrependFlags & DIExpression::DerefAfter)
669b60736ecSDimitry Andric     OffsetExpr.push_back(dwarf::DW_OP_deref);
670b60736ecSDimitry Andric   return DIExpression::prependOpcodes(Expr, OffsetExpr,
671b60736ecSDimitry Andric                                       PrependFlags & DIExpression::StackValue,
672b60736ecSDimitry Andric                                       PrependFlags & DIExpression::EntryValue);
673b60736ecSDimitry Andric }
674b60736ecSDimitry Andric 
67567c32a98SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
67671d5a254SDimitry Andric LLVM_DUMP_METHOD
dumpReg(Register Reg,unsigned SubRegIndex,const TargetRegisterInfo * TRI)677cfca06d7SDimitry Andric void TargetRegisterInfo::dumpReg(Register Reg, unsigned SubRegIndex,
67867c32a98SDimitry Andric                                  const TargetRegisterInfo *TRI) {
679044eb2f6SDimitry Andric   dbgs() << printReg(Reg, TRI, SubRegIndex) << "\n";
68067c32a98SDimitry Andric }
68167c32a98SDimitry Andric #endif
682