xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/TargetRegisterInfo.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //==- TargetRegisterInfo.cpp - Target Register Information Implementation --==//
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 TargetRegisterInfo interface.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
140b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
150b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
160b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
175ffd83dbSDimitry Andric #include "llvm/ADT/SmallSet.h"
180b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
1981ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
2081ad6265SDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
255ffd83dbSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
28*0fca6ea1SDimitry Andric #include "llvm/CodeGenTypes/MachineValueType.h"
290b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
300b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
31e8d8bef9SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
320b57cec5SDimitry Andric #include "llvm/IR/Function.h"
330b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
345ffd83dbSDimitry Andric #include "llvm/Support/CommandLine.h"
350b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
360b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
370b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
380b57cec5SDimitry Andric #include "llvm/Support/Printable.h"
390b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
400b57cec5SDimitry Andric #include <cassert>
410b57cec5SDimitry Andric #include <utility>
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric #define DEBUG_TYPE "target-reg-info"
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric using namespace llvm;
460b57cec5SDimitry Andric 
475ffd83dbSDimitry Andric static cl::opt<unsigned>
485ffd83dbSDimitry Andric     HugeSizeForSplit("huge-size-for-split", cl::Hidden,
495ffd83dbSDimitry Andric                      cl::desc("A threshold of live range size which may cause "
505ffd83dbSDimitry Andric                               "high compile time cost in global splitting."),
515ffd83dbSDimitry Andric                      cl::init(5000));
525ffd83dbSDimitry Andric 
53*0fca6ea1SDimitry Andric TargetRegisterInfo::TargetRegisterInfo(
54*0fca6ea1SDimitry Andric     const TargetRegisterInfoDesc *ID, regclass_iterator RCB,
55*0fca6ea1SDimitry Andric     regclass_iterator RCE, const char *const *SRINames,
56*0fca6ea1SDimitry Andric     const SubRegCoveredBits *SubIdxRanges, const LaneBitmask *SRILaneMasks,
57*0fca6ea1SDimitry Andric     LaneBitmask SRICoveringLanes, const RegClassInfo *const RCIs,
58*0fca6ea1SDimitry Andric     const MVT::SimpleValueType *const RCVTLists, unsigned Mode)
59*0fca6ea1SDimitry Andric     : InfoDesc(ID), SubRegIndexNames(SRINames), SubRegIdxRanges(SubIdxRanges),
60*0fca6ea1SDimitry Andric       SubRegIndexLaneMasks(SRILaneMasks), RegClassBegin(RCB), RegClassEnd(RCE),
61*0fca6ea1SDimitry Andric       CoveringLanes(SRICoveringLanes), RCInfos(RCIs), RCVTLists(RCVTLists),
62*0fca6ea1SDimitry Andric       HwMode(Mode) {}
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric TargetRegisterInfo::~TargetRegisterInfo() = default;
650b57cec5SDimitry Andric 
665ffd83dbSDimitry Andric bool TargetRegisterInfo::shouldRegionSplitForVirtReg(
675ffd83dbSDimitry Andric     const MachineFunction &MF, const LiveInterval &VirtReg) const {
685ffd83dbSDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
695ffd83dbSDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
70e8d8bef9SDimitry Andric   MachineInstr *MI = MRI.getUniqueVRegDef(VirtReg.reg());
715ffd83dbSDimitry Andric   if (MI && TII->isTriviallyReMaterializable(*MI) &&
725ffd83dbSDimitry Andric       VirtReg.size() > HugeSizeForSplit)
735ffd83dbSDimitry Andric     return false;
745ffd83dbSDimitry Andric   return true;
755ffd83dbSDimitry Andric }
765ffd83dbSDimitry Andric 
775ffd83dbSDimitry Andric void TargetRegisterInfo::markSuperRegs(BitVector &RegisterSet,
785ffd83dbSDimitry Andric                                        MCRegister Reg) const {
7906c3fb27SDimitry Andric   for (MCPhysReg SR : superregs_inclusive(Reg))
8006c3fb27SDimitry Andric     RegisterSet.set(SR);
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric bool TargetRegisterInfo::checkAllSuperRegsMarked(const BitVector &RegisterSet,
840b57cec5SDimitry Andric     ArrayRef<MCPhysReg> Exceptions) const {
850b57cec5SDimitry Andric   // Check that all super registers of reserved regs are reserved as well.
860b57cec5SDimitry Andric   BitVector Checked(getNumRegs());
870b57cec5SDimitry Andric   for (unsigned Reg : RegisterSet.set_bits()) {
880b57cec5SDimitry Andric     if (Checked[Reg])
890b57cec5SDimitry Andric       continue;
9006c3fb27SDimitry Andric     for (MCPhysReg SR : superregs(Reg)) {
9106c3fb27SDimitry Andric       if (!RegisterSet[SR] && !is_contained(Exceptions, Reg)) {
9206c3fb27SDimitry Andric         dbgs() << "Error: Super register " << printReg(SR, this)
930b57cec5SDimitry Andric                << " of reserved register " << printReg(Reg, this)
940b57cec5SDimitry Andric                << " is not reserved.\n";
950b57cec5SDimitry Andric         return false;
960b57cec5SDimitry Andric       }
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric       // We transitively check superregs. So we can remember this for later
990b57cec5SDimitry Andric       // to avoid compiletime explosion in deep register hierarchies.
10006c3fb27SDimitry Andric       Checked.set(SR);
1010b57cec5SDimitry Andric     }
1020b57cec5SDimitry Andric   }
1030b57cec5SDimitry Andric   return true;
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric namespace llvm {
1070b57cec5SDimitry Andric 
1088bcb0991SDimitry Andric Printable printReg(Register Reg, const TargetRegisterInfo *TRI,
1090b57cec5SDimitry Andric                    unsigned SubIdx, const MachineRegisterInfo *MRI) {
1100b57cec5SDimitry Andric   return Printable([Reg, TRI, SubIdx, MRI](raw_ostream &OS) {
1110b57cec5SDimitry Andric     if (!Reg)
1120b57cec5SDimitry Andric       OS << "$noreg";
1138bcb0991SDimitry Andric     else if (Register::isStackSlot(Reg))
1148bcb0991SDimitry Andric       OS << "SS#" << Register::stackSlot2Index(Reg);
115bdd1243dSDimitry Andric     else if (Reg.isVirtual()) {
1160b57cec5SDimitry Andric       StringRef Name = MRI ? MRI->getVRegName(Reg) : "";
1170b57cec5SDimitry Andric       if (Name != "") {
1180b57cec5SDimitry Andric         OS << '%' << Name;
1190b57cec5SDimitry Andric       } else {
1208bcb0991SDimitry Andric         OS << '%' << Register::virtReg2Index(Reg);
1210b57cec5SDimitry Andric       }
1228bcb0991SDimitry Andric     } else if (!TRI)
1230b57cec5SDimitry Andric       OS << '$' << "physreg" << Reg;
1240b57cec5SDimitry Andric     else if (Reg < TRI->getNumRegs()) {
1250b57cec5SDimitry Andric       OS << '$';
1260b57cec5SDimitry Andric       printLowerCase(TRI->getName(Reg), OS);
1270b57cec5SDimitry Andric     } else
1280b57cec5SDimitry Andric       llvm_unreachable("Register kind is unsupported.");
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric     if (SubIdx) {
1310b57cec5SDimitry Andric       if (TRI)
1320b57cec5SDimitry Andric         OS << ':' << TRI->getSubRegIndexName(SubIdx);
1330b57cec5SDimitry Andric       else
1340b57cec5SDimitry Andric         OS << ":sub(" << SubIdx << ')';
1350b57cec5SDimitry Andric     }
1360b57cec5SDimitry Andric   });
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
1400b57cec5SDimitry Andric   return Printable([Unit, TRI](raw_ostream &OS) {
1410b57cec5SDimitry Andric     // Generic printout when TRI is missing.
1420b57cec5SDimitry Andric     if (!TRI) {
1430b57cec5SDimitry Andric       OS << "Unit~" << Unit;
1440b57cec5SDimitry Andric       return;
1450b57cec5SDimitry Andric     }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric     // Check for invalid register units.
1480b57cec5SDimitry Andric     if (Unit >= TRI->getNumRegUnits()) {
1490b57cec5SDimitry Andric       OS << "BadUnit~" << Unit;
1500b57cec5SDimitry Andric       return;
1510b57cec5SDimitry Andric     }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric     // Normal units have at least one root.
1540b57cec5SDimitry Andric     MCRegUnitRootIterator Roots(Unit, TRI);
1550b57cec5SDimitry Andric     assert(Roots.isValid() && "Unit has no roots.");
1560b57cec5SDimitry Andric     OS << TRI->getName(*Roots);
1570b57cec5SDimitry Andric     for (++Roots; Roots.isValid(); ++Roots)
1580b57cec5SDimitry Andric       OS << '~' << TRI->getName(*Roots);
1590b57cec5SDimitry Andric   });
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric Printable printVRegOrUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
1630b57cec5SDimitry Andric   return Printable([Unit, TRI](raw_ostream &OS) {
1648bcb0991SDimitry Andric     if (Register::isVirtualRegister(Unit)) {
1658bcb0991SDimitry Andric       OS << '%' << Register::virtReg2Index(Unit);
1660b57cec5SDimitry Andric     } else {
1670b57cec5SDimitry Andric       OS << printRegUnit(Unit, TRI);
1680b57cec5SDimitry Andric     }
1690b57cec5SDimitry Andric   });
1700b57cec5SDimitry Andric }
1710b57cec5SDimitry Andric 
1725ffd83dbSDimitry Andric Printable printRegClassOrBank(Register Reg, const MachineRegisterInfo &RegInfo,
1730b57cec5SDimitry Andric                               const TargetRegisterInfo *TRI) {
1740b57cec5SDimitry Andric   return Printable([Reg, &RegInfo, TRI](raw_ostream &OS) {
1750b57cec5SDimitry Andric     if (RegInfo.getRegClassOrNull(Reg))
1760b57cec5SDimitry Andric       OS << StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
1770b57cec5SDimitry Andric     else if (RegInfo.getRegBankOrNull(Reg))
1780b57cec5SDimitry Andric       OS << StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
1790b57cec5SDimitry Andric     else {
1800b57cec5SDimitry Andric       OS << "_";
1810b57cec5SDimitry Andric       assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
1820b57cec5SDimitry Andric              "Generic registers must have a valid type");
1830b57cec5SDimitry Andric     }
1840b57cec5SDimitry Andric   });
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric } // end namespace llvm
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric /// getAllocatableClass - Return the maximal subclass of the given register
1900b57cec5SDimitry Andric /// class that is alloctable, or NULL.
1910b57cec5SDimitry Andric const TargetRegisterClass *
1920b57cec5SDimitry Andric TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
1930b57cec5SDimitry Andric   if (!RC || RC->isAllocatable())
1940b57cec5SDimitry Andric     return RC;
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric   for (BitMaskClassIterator It(RC->getSubClassMask(), *this); It.isValid();
1970b57cec5SDimitry Andric        ++It) {
1980b57cec5SDimitry Andric     const TargetRegisterClass *SubRC = getRegClass(It.getID());
1990b57cec5SDimitry Andric     if (SubRC->isAllocatable())
2000b57cec5SDimitry Andric       return SubRC;
2010b57cec5SDimitry Andric   }
2020b57cec5SDimitry Andric   return nullptr;
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric /// getMinimalPhysRegClass - Returns the Register Class of a physical
2060b57cec5SDimitry Andric /// register of the given type, picking the most sub register class of
2070b57cec5SDimitry Andric /// the right type that contains this physreg.
2080b57cec5SDimitry Andric const TargetRegisterClass *
2095ffd83dbSDimitry Andric TargetRegisterInfo::getMinimalPhysRegClass(MCRegister reg, MVT VT) const {
2108bcb0991SDimitry Andric   assert(Register::isPhysicalRegister(reg) &&
2118bcb0991SDimitry Andric          "reg must be a physical register");
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   // Pick the most sub register class of the right type that contains
2140b57cec5SDimitry Andric   // this physreg.
2150b57cec5SDimitry Andric   const TargetRegisterClass* BestRC = nullptr;
2160b57cec5SDimitry Andric   for (const TargetRegisterClass* RC : regclasses()) {
2170b57cec5SDimitry Andric     if ((VT == MVT::Other || isTypeLegalForClass(*RC, VT)) &&
2180b57cec5SDimitry Andric         RC->contains(reg) && (!BestRC || BestRC->hasSubClass(RC)))
2190b57cec5SDimitry Andric       BestRC = RC;
2200b57cec5SDimitry Andric   }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   assert(BestRC && "Couldn't find the register class");
2230b57cec5SDimitry Andric   return BestRC;
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric 
226fe6060f1SDimitry Andric const TargetRegisterClass *
227fe6060f1SDimitry Andric TargetRegisterInfo::getMinimalPhysRegClassLLT(MCRegister reg, LLT Ty) const {
228fe6060f1SDimitry Andric   assert(Register::isPhysicalRegister(reg) &&
229fe6060f1SDimitry Andric          "reg must be a physical register");
230fe6060f1SDimitry Andric 
231fe6060f1SDimitry Andric   // Pick the most sub register class of the right type that contains
232fe6060f1SDimitry Andric   // this physreg.
233fe6060f1SDimitry Andric   const TargetRegisterClass *BestRC = nullptr;
234fe6060f1SDimitry Andric   for (const TargetRegisterClass *RC : regclasses()) {
235fe6060f1SDimitry Andric     if ((!Ty.isValid() || isTypeLegalForClass(*RC, Ty)) && RC->contains(reg) &&
236fe6060f1SDimitry Andric         (!BestRC || BestRC->hasSubClass(RC)))
237fe6060f1SDimitry Andric       BestRC = RC;
238fe6060f1SDimitry Andric   }
239fe6060f1SDimitry Andric 
240fe6060f1SDimitry Andric   return BestRC;
241fe6060f1SDimitry Andric }
242fe6060f1SDimitry Andric 
2430b57cec5SDimitry Andric /// getAllocatableSetForRC - Toggle the bits that represent allocatable
2440b57cec5SDimitry Andric /// registers for the specific register class.
2450b57cec5SDimitry Andric static void getAllocatableSetForRC(const MachineFunction &MF,
2460b57cec5SDimitry Andric                                    const TargetRegisterClass *RC, BitVector &R){
2470b57cec5SDimitry Andric   assert(RC->isAllocatable() && "invalid for nonallocatable sets");
2480b57cec5SDimitry Andric   ArrayRef<MCPhysReg> Order = RC->getRawAllocationOrder(MF);
2490eae32dcSDimitry Andric   for (MCPhysReg PR : Order)
2500eae32dcSDimitry Andric     R.set(PR);
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
2540b57cec5SDimitry Andric                                           const TargetRegisterClass *RC) const {
2550b57cec5SDimitry Andric   BitVector Allocatable(getNumRegs());
2560b57cec5SDimitry Andric   if (RC) {
2570b57cec5SDimitry Andric     // A register class with no allocatable subclass returns an empty set.
2580b57cec5SDimitry Andric     const TargetRegisterClass *SubClass = getAllocatableClass(RC);
2590b57cec5SDimitry Andric     if (SubClass)
2600b57cec5SDimitry Andric       getAllocatableSetForRC(MF, SubClass, Allocatable);
2610b57cec5SDimitry Andric   } else {
2620b57cec5SDimitry Andric     for (const TargetRegisterClass *C : regclasses())
2630b57cec5SDimitry Andric       if (C->isAllocatable())
2640b57cec5SDimitry Andric         getAllocatableSetForRC(MF, C, Allocatable);
2650b57cec5SDimitry Andric   }
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric   // Mask out the reserved registers
268fe6060f1SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
269fe6060f1SDimitry Andric   const BitVector &Reserved = MRI.getReservedRegs();
270fe6060f1SDimitry Andric   Allocatable.reset(Reserved);
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   return Allocatable;
2730b57cec5SDimitry Andric }
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric static inline
2760b57cec5SDimitry Andric const TargetRegisterClass *firstCommonClass(const uint32_t *A,
2770b57cec5SDimitry Andric                                             const uint32_t *B,
2788bcb0991SDimitry Andric                                             const TargetRegisterInfo *TRI) {
2790b57cec5SDimitry Andric   for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
2808bcb0991SDimitry Andric     if (unsigned Common = *A++ & *B++)
28106c3fb27SDimitry Andric       return TRI->getRegClass(I + llvm::countr_zero(Common));
2820b57cec5SDimitry Andric   return nullptr;
2830b57cec5SDimitry Andric }
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric const TargetRegisterClass *
2860b57cec5SDimitry Andric TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
2878bcb0991SDimitry Andric                                       const TargetRegisterClass *B) const {
2880b57cec5SDimitry Andric   // First take care of the trivial cases.
2890b57cec5SDimitry Andric   if (A == B)
2900b57cec5SDimitry Andric     return A;
2910b57cec5SDimitry Andric   if (!A || !B)
2920b57cec5SDimitry Andric     return nullptr;
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   // Register classes are ordered topologically, so the largest common
2950b57cec5SDimitry Andric   // sub-class it the common sub-class with the smallest ID.
2968bcb0991SDimitry Andric   return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this);
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric const TargetRegisterClass *
3000b57cec5SDimitry Andric TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
3010b57cec5SDimitry Andric                                              const TargetRegisterClass *B,
3020b57cec5SDimitry Andric                                              unsigned Idx) const {
3030b57cec5SDimitry Andric   assert(A && B && "Missing register class");
3040b57cec5SDimitry Andric   assert(Idx && "Bad sub-register index");
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric   // Find Idx in the list of super-register indices.
3070b57cec5SDimitry Andric   for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
3080b57cec5SDimitry Andric     if (RCI.getSubReg() == Idx)
3090b57cec5SDimitry Andric       // The bit mask contains all register classes that are projected into B
3100b57cec5SDimitry Andric       // by Idx. Find a class that is also a sub-class of A.
3110b57cec5SDimitry Andric       return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this);
3120b57cec5SDimitry Andric   return nullptr;
3130b57cec5SDimitry Andric }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric const TargetRegisterClass *TargetRegisterInfo::
3160b57cec5SDimitry Andric getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
3170b57cec5SDimitry Andric                        const TargetRegisterClass *RCB, unsigned SubB,
3180b57cec5SDimitry Andric                        unsigned &PreA, unsigned &PreB) const {
3190b57cec5SDimitry Andric   assert(RCA && SubA && RCB && SubB && "Invalid arguments");
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   // Search all pairs of sub-register indices that project into RCA and RCB
3220b57cec5SDimitry Andric   // respectively. This is quadratic, but usually the sets are very small. On
3230b57cec5SDimitry Andric   // most targets like X86, there will only be a single sub-register index
3240b57cec5SDimitry Andric   // (e.g., sub_16bit projecting into GR16).
3250b57cec5SDimitry Andric   //
3260b57cec5SDimitry Andric   // The worst case is a register class like DPR on ARM.
3270b57cec5SDimitry Andric   // We have indices dsub_0..dsub_7 projecting into that class.
3280b57cec5SDimitry Andric   //
3290b57cec5SDimitry Andric   // It is very common that one register class is a sub-register of the other.
3300b57cec5SDimitry Andric   // Arrange for RCA to be the larger register so the answer will be found in
3310b57cec5SDimitry Andric   // the first iteration. This makes the search linear for the most common
3320b57cec5SDimitry Andric   // case.
3330b57cec5SDimitry Andric   const TargetRegisterClass *BestRC = nullptr;
3340b57cec5SDimitry Andric   unsigned *BestPreA = &PreA;
3350b57cec5SDimitry Andric   unsigned *BestPreB = &PreB;
3360b57cec5SDimitry Andric   if (getRegSizeInBits(*RCA) < getRegSizeInBits(*RCB)) {
3370b57cec5SDimitry Andric     std::swap(RCA, RCB);
3380b57cec5SDimitry Andric     std::swap(SubA, SubB);
3390b57cec5SDimitry Andric     std::swap(BestPreA, BestPreB);
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   // Also terminate the search one we have found a register class as small as
3430b57cec5SDimitry Andric   // RCA.
3440b57cec5SDimitry Andric   unsigned MinSize = getRegSizeInBits(*RCA);
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
3470b57cec5SDimitry Andric     unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
3480b57cec5SDimitry Andric     for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
3490b57cec5SDimitry Andric       // Check if a common super-register class exists for this index pair.
3500b57cec5SDimitry Andric       const TargetRegisterClass *RC =
3510b57cec5SDimitry Andric         firstCommonClass(IA.getMask(), IB.getMask(), this);
3520b57cec5SDimitry Andric       if (!RC || getRegSizeInBits(*RC) < MinSize)
3530b57cec5SDimitry Andric         continue;
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric       // The indexes must compose identically: PreA+SubA == PreB+SubB.
3560b57cec5SDimitry Andric       unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
3570b57cec5SDimitry Andric       if (FinalA != FinalB)
3580b57cec5SDimitry Andric         continue;
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric       // Is RC a better candidate than BestRC?
3610b57cec5SDimitry Andric       if (BestRC && getRegSizeInBits(*RC) >= getRegSizeInBits(*BestRC))
3620b57cec5SDimitry Andric         continue;
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric       // Yes, RC is the smallest super-register seen so far.
3650b57cec5SDimitry Andric       BestRC = RC;
3660b57cec5SDimitry Andric       *BestPreA = IA.getSubReg();
3670b57cec5SDimitry Andric       *BestPreB = IB.getSubReg();
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric       // Bail early if we reached MinSize. We won't find a better candidate.
3700b57cec5SDimitry Andric       if (getRegSizeInBits(*BestRC) == MinSize)
3710b57cec5SDimitry Andric         return BestRC;
3720b57cec5SDimitry Andric     }
3730b57cec5SDimitry Andric   }
3740b57cec5SDimitry Andric   return BestRC;
3750b57cec5SDimitry Andric }
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric /// Check if the registers defined by the pair (RegisterClass, SubReg)
3780b57cec5SDimitry Andric /// share the same register file.
3790b57cec5SDimitry Andric static bool shareSameRegisterFile(const TargetRegisterInfo &TRI,
3800b57cec5SDimitry Andric                                   const TargetRegisterClass *DefRC,
3810b57cec5SDimitry Andric                                   unsigned DefSubReg,
3820b57cec5SDimitry Andric                                   const TargetRegisterClass *SrcRC,
3830b57cec5SDimitry Andric                                   unsigned SrcSubReg) {
3840b57cec5SDimitry Andric   // Same register class.
3850b57cec5SDimitry Andric   if (DefRC == SrcRC)
3860b57cec5SDimitry Andric     return true;
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric   // Both operands are sub registers. Check if they share a register class.
3890b57cec5SDimitry Andric   unsigned SrcIdx, DefIdx;
3900b57cec5SDimitry Andric   if (SrcSubReg && DefSubReg) {
3910b57cec5SDimitry Andric     return TRI.getCommonSuperRegClass(SrcRC, SrcSubReg, DefRC, DefSubReg,
3920b57cec5SDimitry Andric                                       SrcIdx, DefIdx) != nullptr;
3930b57cec5SDimitry Andric   }
3940b57cec5SDimitry Andric 
3950b57cec5SDimitry Andric   // At most one of the register is a sub register, make it Src to avoid
3960b57cec5SDimitry Andric   // duplicating the test.
3970b57cec5SDimitry Andric   if (!SrcSubReg) {
3980b57cec5SDimitry Andric     std::swap(DefSubReg, SrcSubReg);
3990b57cec5SDimitry Andric     std::swap(DefRC, SrcRC);
4000b57cec5SDimitry Andric   }
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric   // One of the register is a sub register, check if we can get a superclass.
4030b57cec5SDimitry Andric   if (SrcSubReg)
4040b57cec5SDimitry Andric     return TRI.getMatchingSuperRegClass(SrcRC, DefRC, SrcSubReg) != nullptr;
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric   // Plain copy.
4070b57cec5SDimitry Andric   return TRI.getCommonSubClass(DefRC, SrcRC) != nullptr;
4080b57cec5SDimitry Andric }
4090b57cec5SDimitry Andric 
4100b57cec5SDimitry Andric bool TargetRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
4110b57cec5SDimitry Andric                                               unsigned DefSubReg,
4120b57cec5SDimitry Andric                                               const TargetRegisterClass *SrcRC,
4130b57cec5SDimitry Andric                                               unsigned SrcSubReg) const {
4140b57cec5SDimitry Andric   // If this source does not incur a cross register bank copy, use it.
4150b57cec5SDimitry Andric   return shareSameRegisterFile(*this, DefRC, DefSubReg, SrcRC, SrcSubReg);
4160b57cec5SDimitry Andric }
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric // Compute target-independent register allocator hints to help eliminate copies.
4195ffd83dbSDimitry Andric bool TargetRegisterInfo::getRegAllocationHints(
4205ffd83dbSDimitry Andric     Register VirtReg, ArrayRef<MCPhysReg> Order,
4215ffd83dbSDimitry Andric     SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
4225ffd83dbSDimitry Andric     const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
4230b57cec5SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
42406c3fb27SDimitry Andric   const std::pair<unsigned, SmallVector<Register, 4>> &Hints_MRI =
4250b57cec5SDimitry Andric       MRI.getRegAllocationHints(VirtReg);
4260b57cec5SDimitry Andric 
4275ffd83dbSDimitry Andric   SmallSet<Register, 32> HintedRegs;
4280b57cec5SDimitry Andric   // First hint may be a target hint.
4290b57cec5SDimitry Andric   bool Skip = (Hints_MRI.first != 0);
4300b57cec5SDimitry Andric   for (auto Reg : Hints_MRI.second) {
4310b57cec5SDimitry Andric     if (Skip) {
4320b57cec5SDimitry Andric       Skip = false;
4330b57cec5SDimitry Andric       continue;
4340b57cec5SDimitry Andric     }
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric     // Target-independent hints are either a physical or a virtual register.
4375ffd83dbSDimitry Andric     Register Phys = Reg;
4385ffd83dbSDimitry Andric     if (VRM && Phys.isVirtual())
4390b57cec5SDimitry Andric       Phys = VRM->getPhys(Phys);
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric     // Don't add the same reg twice (Hints_MRI may contain multiple virtual
4420b57cec5SDimitry Andric     // registers allocated to the same physreg).
4430b57cec5SDimitry Andric     if (!HintedRegs.insert(Phys).second)
4440b57cec5SDimitry Andric       continue;
4450b57cec5SDimitry Andric     // Check that Phys is a valid hint in VirtReg's register class.
4465ffd83dbSDimitry Andric     if (!Phys.isPhysical())
4470b57cec5SDimitry Andric       continue;
4480b57cec5SDimitry Andric     if (MRI.isReserved(Phys))
4490b57cec5SDimitry Andric       continue;
4500b57cec5SDimitry Andric     // Check that Phys is in the allocation order. We shouldn't heed hints
4510b57cec5SDimitry Andric     // from VirtReg's register class if they aren't in the allocation order. The
4520b57cec5SDimitry Andric     // target probably has a reason for removing the register.
4530b57cec5SDimitry Andric     if (!is_contained(Order, Phys))
4540b57cec5SDimitry Andric       continue;
4550b57cec5SDimitry Andric 
4560b57cec5SDimitry Andric     // All clear, tell the register allocator to prefer this register.
4570b57cec5SDimitry Andric     Hints.push_back(Phys);
4580b57cec5SDimitry Andric   }
4590b57cec5SDimitry Andric   return false;
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric 
4628bcb0991SDimitry Andric bool TargetRegisterInfo::isCalleeSavedPhysReg(
4635ffd83dbSDimitry Andric     MCRegister PhysReg, const MachineFunction &MF) const {
4648bcb0991SDimitry Andric   if (PhysReg == 0)
4658bcb0991SDimitry Andric     return false;
4668bcb0991SDimitry Andric   const uint32_t *callerPreservedRegs =
4678bcb0991SDimitry Andric       getCallPreservedMask(MF, MF.getFunction().getCallingConv());
4688bcb0991SDimitry Andric   if (callerPreservedRegs) {
4698bcb0991SDimitry Andric     assert(Register::isPhysicalRegister(PhysReg) &&
4708bcb0991SDimitry Andric            "Expected physical register");
4718bcb0991SDimitry Andric     return (callerPreservedRegs[PhysReg / 32] >> PhysReg % 32) & 1;
4728bcb0991SDimitry Andric   }
4738bcb0991SDimitry Andric   return false;
4748bcb0991SDimitry Andric }
4758bcb0991SDimitry Andric 
4760b57cec5SDimitry Andric bool TargetRegisterInfo::canRealignStack(const MachineFunction &MF) const {
477*0fca6ea1SDimitry Andric   return MF.getFrameInfo().isStackRealignable();
4780b57cec5SDimitry Andric }
4790b57cec5SDimitry Andric 
480fe6060f1SDimitry Andric bool TargetRegisterInfo::shouldRealignStack(const MachineFunction &MF) const {
481*0fca6ea1SDimitry Andric   return MF.getFrameInfo().shouldRealignStack();
4820b57cec5SDimitry Andric }
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric bool TargetRegisterInfo::regmaskSubsetEqual(const uint32_t *mask0,
4850b57cec5SDimitry Andric                                             const uint32_t *mask1) const {
4860b57cec5SDimitry Andric   unsigned N = (getNumRegs()+31) / 32;
4870b57cec5SDimitry Andric   for (unsigned I = 0; I < N; ++I)
4880b57cec5SDimitry Andric     if ((mask0[I] & mask1[I]) != mask0[I])
4890b57cec5SDimitry Andric       return false;
4900b57cec5SDimitry Andric   return true;
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric 
4935f757f3fSDimitry Andric TypeSize
4945ffd83dbSDimitry Andric TargetRegisterInfo::getRegSizeInBits(Register Reg,
4950b57cec5SDimitry Andric                                      const MachineRegisterInfo &MRI) const {
4960b57cec5SDimitry Andric   const TargetRegisterClass *RC{};
4975ffd83dbSDimitry Andric   if (Reg.isPhysical()) {
4980b57cec5SDimitry Andric     // The size is not directly available for physical registers.
4990b57cec5SDimitry Andric     // Instead, we need to access a register class that contains Reg and
5000b57cec5SDimitry Andric     // get the size of that register class.
5010b57cec5SDimitry Andric     RC = getMinimalPhysRegClass(Reg);
5025f757f3fSDimitry Andric     assert(RC && "Unable to deduce the register class");
5035f757f3fSDimitry Andric     return getRegSizeInBits(*RC);
5040b57cec5SDimitry Andric   }
5055f757f3fSDimitry Andric   LLT Ty = MRI.getType(Reg);
5065f757f3fSDimitry Andric   if (Ty.isValid())
5075f757f3fSDimitry Andric     return Ty.getSizeInBits();
5085f757f3fSDimitry Andric 
5095f757f3fSDimitry Andric   // Since Reg is not a generic register, it may have a register class.
5105f757f3fSDimitry Andric   RC = MRI.getRegClass(Reg);
5110b57cec5SDimitry Andric   assert(RC && "Unable to deduce the register class");
5120b57cec5SDimitry Andric   return getRegSizeInBits(*RC);
5130b57cec5SDimitry Andric }
5140b57cec5SDimitry Andric 
515fe6060f1SDimitry Andric bool TargetRegisterInfo::getCoveringSubRegIndexes(
516fe6060f1SDimitry Andric     const MachineRegisterInfo &MRI, const TargetRegisterClass *RC,
517fe6060f1SDimitry Andric     LaneBitmask LaneMask, SmallVectorImpl<unsigned> &NeededIndexes) const {
518fe6060f1SDimitry Andric   SmallVector<unsigned, 8> PossibleIndexes;
519fe6060f1SDimitry Andric   unsigned BestIdx = 0;
520fe6060f1SDimitry Andric   unsigned BestCover = 0;
521fe6060f1SDimitry Andric 
522fe6060f1SDimitry Andric   for (unsigned Idx = 1, E = getNumSubRegIndices(); Idx < E; ++Idx) {
523fe6060f1SDimitry Andric     // Is this index even compatible with the given class?
524fe6060f1SDimitry Andric     if (getSubClassWithSubReg(RC, Idx) != RC)
525fe6060f1SDimitry Andric       continue;
526fe6060f1SDimitry Andric     LaneBitmask SubRegMask = getSubRegIndexLaneMask(Idx);
527fe6060f1SDimitry Andric     // Early exit if we found a perfect match.
528fe6060f1SDimitry Andric     if (SubRegMask == LaneMask) {
529fe6060f1SDimitry Andric       BestIdx = Idx;
530fe6060f1SDimitry Andric       break;
531fe6060f1SDimitry Andric     }
532fe6060f1SDimitry Andric 
533fe6060f1SDimitry Andric     // The index must not cover any lanes outside \p LaneMask.
534fe6060f1SDimitry Andric     if ((SubRegMask & ~LaneMask).any())
535fe6060f1SDimitry Andric       continue;
536fe6060f1SDimitry Andric 
537fe6060f1SDimitry Andric     unsigned PopCount = SubRegMask.getNumLanes();
538fe6060f1SDimitry Andric     PossibleIndexes.push_back(Idx);
539fe6060f1SDimitry Andric     if (PopCount > BestCover) {
540fe6060f1SDimitry Andric       BestCover = PopCount;
541fe6060f1SDimitry Andric       BestIdx = Idx;
542fe6060f1SDimitry Andric     }
543fe6060f1SDimitry Andric   }
544fe6060f1SDimitry Andric 
545fe6060f1SDimitry Andric   // Abort if we cannot possibly implement the COPY with the given indexes.
546fe6060f1SDimitry Andric   if (BestIdx == 0)
54704eeddc0SDimitry Andric     return false;
548fe6060f1SDimitry Andric 
549fe6060f1SDimitry Andric   NeededIndexes.push_back(BestIdx);
550fe6060f1SDimitry Andric 
551fe6060f1SDimitry Andric   // Greedy heuristic: Keep iterating keeping the best covering subreg index
552fe6060f1SDimitry Andric   // each time.
553fe6060f1SDimitry Andric   LaneBitmask LanesLeft = LaneMask & ~getSubRegIndexLaneMask(BestIdx);
554fe6060f1SDimitry Andric   while (LanesLeft.any()) {
555fe6060f1SDimitry Andric     unsigned BestIdx = 0;
556fe6060f1SDimitry Andric     int BestCover = std::numeric_limits<int>::min();
557fe6060f1SDimitry Andric     for (unsigned Idx : PossibleIndexes) {
558fe6060f1SDimitry Andric       LaneBitmask SubRegMask = getSubRegIndexLaneMask(Idx);
559fe6060f1SDimitry Andric       // Early exit if we found a perfect match.
560fe6060f1SDimitry Andric       if (SubRegMask == LanesLeft) {
561fe6060f1SDimitry Andric         BestIdx = Idx;
562fe6060f1SDimitry Andric         break;
563fe6060f1SDimitry Andric       }
564fe6060f1SDimitry Andric 
565bdd1243dSDimitry Andric       // Do not cover already-covered lanes to avoid creating cycles
566bdd1243dSDimitry Andric       // in copy bundles (= bundle contains copies that write to the
567bdd1243dSDimitry Andric       // registers).
568bdd1243dSDimitry Andric       if ((SubRegMask & ~LanesLeft).any())
569bdd1243dSDimitry Andric         continue;
570bdd1243dSDimitry Andric 
571bdd1243dSDimitry Andric       // Try to cover as many of the remaining lanes as possible.
572bdd1243dSDimitry Andric       const int Cover = (SubRegMask & LanesLeft).getNumLanes();
573fe6060f1SDimitry Andric       if (Cover > BestCover) {
574fe6060f1SDimitry Andric         BestCover = Cover;
575fe6060f1SDimitry Andric         BestIdx = Idx;
576fe6060f1SDimitry Andric       }
577fe6060f1SDimitry Andric     }
578fe6060f1SDimitry Andric 
579fe6060f1SDimitry Andric     if (BestIdx == 0)
58004eeddc0SDimitry Andric       return false; // Impossible to handle
581fe6060f1SDimitry Andric 
582fe6060f1SDimitry Andric     NeededIndexes.push_back(BestIdx);
583fe6060f1SDimitry Andric 
584fe6060f1SDimitry Andric     LanesLeft &= ~getSubRegIndexLaneMask(BestIdx);
585fe6060f1SDimitry Andric   }
586fe6060f1SDimitry Andric 
587fe6060f1SDimitry Andric   return BestIdx;
588fe6060f1SDimitry Andric }
589fe6060f1SDimitry Andric 
590*0fca6ea1SDimitry Andric unsigned TargetRegisterInfo::getSubRegIdxSize(unsigned Idx) const {
591*0fca6ea1SDimitry Andric   assert(Idx && Idx < getNumSubRegIndices() &&
592*0fca6ea1SDimitry Andric          "This is not a subregister index");
593*0fca6ea1SDimitry Andric   return SubRegIdxRanges[HwMode * getNumSubRegIndices() + Idx].Size;
594*0fca6ea1SDimitry Andric }
595*0fca6ea1SDimitry Andric 
596*0fca6ea1SDimitry Andric unsigned TargetRegisterInfo::getSubRegIdxOffset(unsigned Idx) const {
597*0fca6ea1SDimitry Andric   assert(Idx && Idx < getNumSubRegIndices() &&
598*0fca6ea1SDimitry Andric          "This is not a subregister index");
599*0fca6ea1SDimitry Andric   return SubRegIdxRanges[HwMode * getNumSubRegIndices() + Idx].Offset;
600*0fca6ea1SDimitry Andric }
601*0fca6ea1SDimitry Andric 
6025ffd83dbSDimitry Andric Register
6035ffd83dbSDimitry Andric TargetRegisterInfo::lookThruCopyLike(Register SrcReg,
6040b57cec5SDimitry Andric                                      const MachineRegisterInfo *MRI) const {
6050b57cec5SDimitry Andric   while (true) {
6060b57cec5SDimitry Andric     const MachineInstr *MI = MRI->getVRegDef(SrcReg);
6070b57cec5SDimitry Andric     if (!MI->isCopyLike())
6080b57cec5SDimitry Andric       return SrcReg;
6090b57cec5SDimitry Andric 
6105ffd83dbSDimitry Andric     Register CopySrcReg;
6110b57cec5SDimitry Andric     if (MI->isCopy())
6120b57cec5SDimitry Andric       CopySrcReg = MI->getOperand(1).getReg();
6130b57cec5SDimitry Andric     else {
6140b57cec5SDimitry Andric       assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
6150b57cec5SDimitry Andric       CopySrcReg = MI->getOperand(2).getReg();
6160b57cec5SDimitry Andric     }
6170b57cec5SDimitry Andric 
6185ffd83dbSDimitry Andric     if (!CopySrcReg.isVirtual())
6190b57cec5SDimitry Andric       return CopySrcReg;
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric     SrcReg = CopySrcReg;
6220b57cec5SDimitry Andric   }
6230b57cec5SDimitry Andric }
6240b57cec5SDimitry Andric 
625e8d8bef9SDimitry Andric Register TargetRegisterInfo::lookThruSingleUseCopyChain(
626e8d8bef9SDimitry Andric     Register SrcReg, const MachineRegisterInfo *MRI) const {
627e8d8bef9SDimitry Andric   while (true) {
628e8d8bef9SDimitry Andric     const MachineInstr *MI = MRI->getVRegDef(SrcReg);
629e8d8bef9SDimitry Andric     // Found the real definition, return it if it has a single use.
630e8d8bef9SDimitry Andric     if (!MI->isCopyLike())
631e8d8bef9SDimitry Andric       return MRI->hasOneNonDBGUse(SrcReg) ? SrcReg : Register();
632e8d8bef9SDimitry Andric 
633e8d8bef9SDimitry Andric     Register CopySrcReg;
634e8d8bef9SDimitry Andric     if (MI->isCopy())
635e8d8bef9SDimitry Andric       CopySrcReg = MI->getOperand(1).getReg();
636e8d8bef9SDimitry Andric     else {
637e8d8bef9SDimitry Andric       assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
638e8d8bef9SDimitry Andric       CopySrcReg = MI->getOperand(2).getReg();
639e8d8bef9SDimitry Andric     }
640e8d8bef9SDimitry Andric 
641e8d8bef9SDimitry Andric     // Continue only if the next definition in the chain is for a virtual
642e8d8bef9SDimitry Andric     // register that has a single use.
643e8d8bef9SDimitry Andric     if (!CopySrcReg.isVirtual() || !MRI->hasOneNonDBGUse(CopySrcReg))
644e8d8bef9SDimitry Andric       return Register();
645e8d8bef9SDimitry Andric 
646e8d8bef9SDimitry Andric     SrcReg = CopySrcReg;
647e8d8bef9SDimitry Andric   }
648e8d8bef9SDimitry Andric }
649e8d8bef9SDimitry Andric 
650e8d8bef9SDimitry Andric void TargetRegisterInfo::getOffsetOpcodes(
651e8d8bef9SDimitry Andric     const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const {
652e8d8bef9SDimitry Andric   assert(!Offset.getScalable() && "Scalable offsets are not handled");
653e8d8bef9SDimitry Andric   DIExpression::appendOffset(Ops, Offset.getFixed());
654e8d8bef9SDimitry Andric }
655e8d8bef9SDimitry Andric 
656e8d8bef9SDimitry Andric DIExpression *
657e8d8bef9SDimitry Andric TargetRegisterInfo::prependOffsetExpression(const DIExpression *Expr,
658e8d8bef9SDimitry Andric                                             unsigned PrependFlags,
659e8d8bef9SDimitry Andric                                             const StackOffset &Offset) const {
660e8d8bef9SDimitry Andric   assert((PrependFlags &
661e8d8bef9SDimitry Andric           ~(DIExpression::DerefBefore | DIExpression::DerefAfter |
662e8d8bef9SDimitry Andric             DIExpression::StackValue | DIExpression::EntryValue)) == 0 &&
663e8d8bef9SDimitry Andric          "Unsupported prepend flag");
664e8d8bef9SDimitry Andric   SmallVector<uint64_t, 16> OffsetExpr;
665e8d8bef9SDimitry Andric   if (PrependFlags & DIExpression::DerefBefore)
666e8d8bef9SDimitry Andric     OffsetExpr.push_back(dwarf::DW_OP_deref);
667e8d8bef9SDimitry Andric   getOffsetOpcodes(Offset, OffsetExpr);
668e8d8bef9SDimitry Andric   if (PrependFlags & DIExpression::DerefAfter)
669e8d8bef9SDimitry Andric     OffsetExpr.push_back(dwarf::DW_OP_deref);
670e8d8bef9SDimitry Andric   return DIExpression::prependOpcodes(Expr, OffsetExpr,
671e8d8bef9SDimitry Andric                                       PrependFlags & DIExpression::StackValue,
672e8d8bef9SDimitry Andric                                       PrependFlags & DIExpression::EntryValue);
673e8d8bef9SDimitry Andric }
674e8d8bef9SDimitry Andric 
6750b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
6760b57cec5SDimitry Andric LLVM_DUMP_METHOD
6775ffd83dbSDimitry Andric void TargetRegisterInfo::dumpReg(Register Reg, unsigned SubRegIndex,
6780b57cec5SDimitry Andric                                  const TargetRegisterInfo *TRI) {
6790b57cec5SDimitry Andric   dbgs() << printReg(Reg, TRI, SubRegIndex) << "\n";
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric #endif
682