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"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
225ffd83dbSDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
245ffd83dbSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/VirtRegMap.h"
270b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
280b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
29af732203SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
300b57cec5SDimitry Andric #include "llvm/IR/Function.h"
310b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
325ffd83dbSDimitry Andric #include "llvm/Support/CommandLine.h"
330b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
340b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
350b57cec5SDimitry Andric #include "llvm/Support/MachineValueType.h"
360b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
370b57cec5SDimitry Andric #include "llvm/Support/Printable.h"
380b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
390b57cec5SDimitry Andric #include <cassert>
400b57cec5SDimitry Andric #include <utility>
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric #define DEBUG_TYPE "target-reg-info"
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric using namespace llvm;
450b57cec5SDimitry Andric 
465ffd83dbSDimitry Andric static cl::opt<unsigned>
475ffd83dbSDimitry Andric     HugeSizeForSplit("huge-size-for-split", cl::Hidden,
485ffd83dbSDimitry Andric                      cl::desc("A threshold of live range size which may cause "
495ffd83dbSDimitry Andric                               "high compile time cost in global splitting."),
505ffd83dbSDimitry Andric                      cl::init(5000));
515ffd83dbSDimitry 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)520b57cec5SDimitry Andric TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
530b57cec5SDimitry Andric                              regclass_iterator RCB, regclass_iterator RCE,
540b57cec5SDimitry Andric                              const char *const *SRINames,
550b57cec5SDimitry Andric                              const LaneBitmask *SRILaneMasks,
560b57cec5SDimitry Andric                              LaneBitmask SRICoveringLanes,
570b57cec5SDimitry Andric                              const RegClassInfo *const RCIs,
580b57cec5SDimitry Andric                              unsigned Mode)
590b57cec5SDimitry Andric   : InfoDesc(ID), SubRegIndexNames(SRINames),
600b57cec5SDimitry Andric     SubRegIndexLaneMasks(SRILaneMasks),
610b57cec5SDimitry Andric     RegClassBegin(RCB), RegClassEnd(RCE),
620b57cec5SDimitry Andric     CoveringLanes(SRICoveringLanes),
630b57cec5SDimitry Andric     RCInfos(RCIs), HwMode(Mode) {
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric TargetRegisterInfo::~TargetRegisterInfo() = default;
670b57cec5SDimitry Andric 
shouldRegionSplitForVirtReg(const MachineFunction & MF,const LiveInterval & VirtReg) const685ffd83dbSDimitry Andric bool TargetRegisterInfo::shouldRegionSplitForVirtReg(
695ffd83dbSDimitry Andric     const MachineFunction &MF, const LiveInterval &VirtReg) const {
705ffd83dbSDimitry Andric   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
715ffd83dbSDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
72af732203SDimitry Andric   MachineInstr *MI = MRI.getUniqueVRegDef(VirtReg.reg());
735ffd83dbSDimitry Andric   if (MI && TII->isTriviallyReMaterializable(*MI) &&
745ffd83dbSDimitry Andric       VirtReg.size() > HugeSizeForSplit)
755ffd83dbSDimitry Andric     return false;
765ffd83dbSDimitry Andric   return true;
775ffd83dbSDimitry Andric }
785ffd83dbSDimitry Andric 
markSuperRegs(BitVector & RegisterSet,MCRegister Reg) const795ffd83dbSDimitry Andric void TargetRegisterInfo::markSuperRegs(BitVector &RegisterSet,
805ffd83dbSDimitry Andric                                        MCRegister Reg) const {
810b57cec5SDimitry Andric   for (MCSuperRegIterator AI(Reg, this, true); AI.isValid(); ++AI)
820b57cec5SDimitry Andric     RegisterSet.set(*AI);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric 
checkAllSuperRegsMarked(const BitVector & RegisterSet,ArrayRef<MCPhysReg> Exceptions) const850b57cec5SDimitry Andric bool TargetRegisterInfo::checkAllSuperRegsMarked(const BitVector &RegisterSet,
860b57cec5SDimitry Andric     ArrayRef<MCPhysReg> Exceptions) const {
870b57cec5SDimitry Andric   // Check that all super registers of reserved regs are reserved as well.
880b57cec5SDimitry Andric   BitVector Checked(getNumRegs());
890b57cec5SDimitry Andric   for (unsigned Reg : RegisterSet.set_bits()) {
900b57cec5SDimitry Andric     if (Checked[Reg])
910b57cec5SDimitry Andric       continue;
920b57cec5SDimitry Andric     for (MCSuperRegIterator SR(Reg, this); SR.isValid(); ++SR) {
930b57cec5SDimitry Andric       if (!RegisterSet[*SR] && !is_contained(Exceptions, Reg)) {
940b57cec5SDimitry Andric         dbgs() << "Error: Super register " << printReg(*SR, this)
950b57cec5SDimitry Andric                << " of reserved register " << printReg(Reg, this)
960b57cec5SDimitry Andric                << " is not reserved.\n";
970b57cec5SDimitry Andric         return false;
980b57cec5SDimitry Andric       }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric       // We transitively check superregs. So we can remember this for later
1010b57cec5SDimitry Andric       // to avoid compiletime explosion in deep register hierarchies.
1020b57cec5SDimitry Andric       Checked.set(*SR);
1030b57cec5SDimitry Andric     }
1040b57cec5SDimitry Andric   }
1050b57cec5SDimitry Andric   return true;
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric namespace llvm {
1090b57cec5SDimitry Andric 
printReg(Register Reg,const TargetRegisterInfo * TRI,unsigned SubIdx,const MachineRegisterInfo * MRI)1108bcb0991SDimitry Andric Printable printReg(Register Reg, const TargetRegisterInfo *TRI,
1110b57cec5SDimitry Andric                    unsigned SubIdx, const MachineRegisterInfo *MRI) {
1120b57cec5SDimitry Andric   return Printable([Reg, TRI, SubIdx, MRI](raw_ostream &OS) {
1130b57cec5SDimitry Andric     if (!Reg)
1140b57cec5SDimitry Andric       OS << "$noreg";
1158bcb0991SDimitry Andric     else if (Register::isStackSlot(Reg))
1168bcb0991SDimitry Andric       OS << "SS#" << Register::stackSlot2Index(Reg);
1178bcb0991SDimitry Andric     else if (Register::isVirtualRegister(Reg)) {
1180b57cec5SDimitry Andric       StringRef Name = MRI ? MRI->getVRegName(Reg) : "";
1190b57cec5SDimitry Andric       if (Name != "") {
1200b57cec5SDimitry Andric         OS << '%' << Name;
1210b57cec5SDimitry Andric       } else {
1228bcb0991SDimitry Andric         OS << '%' << Register::virtReg2Index(Reg);
1230b57cec5SDimitry Andric       }
1248bcb0991SDimitry Andric     } else if (!TRI)
1250b57cec5SDimitry Andric       OS << '$' << "physreg" << Reg;
1260b57cec5SDimitry Andric     else if (Reg < TRI->getNumRegs()) {
1270b57cec5SDimitry Andric       OS << '$';
1280b57cec5SDimitry Andric       printLowerCase(TRI->getName(Reg), OS);
1290b57cec5SDimitry Andric     } else
1300b57cec5SDimitry Andric       llvm_unreachable("Register kind is unsupported.");
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric     if (SubIdx) {
1330b57cec5SDimitry Andric       if (TRI)
1340b57cec5SDimitry Andric         OS << ':' << TRI->getSubRegIndexName(SubIdx);
1350b57cec5SDimitry Andric       else
1360b57cec5SDimitry Andric         OS << ":sub(" << SubIdx << ')';
1370b57cec5SDimitry Andric     }
1380b57cec5SDimitry Andric   });
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric 
printRegUnit(unsigned Unit,const TargetRegisterInfo * TRI)1410b57cec5SDimitry Andric Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
1420b57cec5SDimitry Andric   return Printable([Unit, TRI](raw_ostream &OS) {
1430b57cec5SDimitry Andric     // Generic printout when TRI is missing.
1440b57cec5SDimitry Andric     if (!TRI) {
1450b57cec5SDimitry Andric       OS << "Unit~" << Unit;
1460b57cec5SDimitry Andric       return;
1470b57cec5SDimitry Andric     }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric     // Check for invalid register units.
1500b57cec5SDimitry Andric     if (Unit >= TRI->getNumRegUnits()) {
1510b57cec5SDimitry Andric       OS << "BadUnit~" << Unit;
1520b57cec5SDimitry Andric       return;
1530b57cec5SDimitry Andric     }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric     // Normal units have at least one root.
1560b57cec5SDimitry Andric     MCRegUnitRootIterator Roots(Unit, TRI);
1570b57cec5SDimitry Andric     assert(Roots.isValid() && "Unit has no roots.");
1580b57cec5SDimitry Andric     OS << TRI->getName(*Roots);
1590b57cec5SDimitry Andric     for (++Roots; Roots.isValid(); ++Roots)
1600b57cec5SDimitry Andric       OS << '~' << TRI->getName(*Roots);
1610b57cec5SDimitry Andric   });
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric 
printVRegOrUnit(unsigned Unit,const TargetRegisterInfo * TRI)1640b57cec5SDimitry Andric Printable printVRegOrUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
1650b57cec5SDimitry Andric   return Printable([Unit, TRI](raw_ostream &OS) {
1668bcb0991SDimitry Andric     if (Register::isVirtualRegister(Unit)) {
1678bcb0991SDimitry Andric       OS << '%' << Register::virtReg2Index(Unit);
1680b57cec5SDimitry Andric     } else {
1690b57cec5SDimitry Andric       OS << printRegUnit(Unit, TRI);
1700b57cec5SDimitry Andric     }
1710b57cec5SDimitry Andric   });
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric 
printRegClassOrBank(Register Reg,const MachineRegisterInfo & RegInfo,const TargetRegisterInfo * TRI)1745ffd83dbSDimitry Andric Printable printRegClassOrBank(Register Reg, const MachineRegisterInfo &RegInfo,
1750b57cec5SDimitry Andric                               const TargetRegisterInfo *TRI) {
1760b57cec5SDimitry Andric   return Printable([Reg, &RegInfo, TRI](raw_ostream &OS) {
1770b57cec5SDimitry Andric     if (RegInfo.getRegClassOrNull(Reg))
1780b57cec5SDimitry Andric       OS << StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
1790b57cec5SDimitry Andric     else if (RegInfo.getRegBankOrNull(Reg))
1800b57cec5SDimitry Andric       OS << StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
1810b57cec5SDimitry Andric     else {
1820b57cec5SDimitry Andric       OS << "_";
1830b57cec5SDimitry Andric       assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
1840b57cec5SDimitry Andric              "Generic registers must have a valid type");
1850b57cec5SDimitry Andric     }
1860b57cec5SDimitry Andric   });
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric } // end namespace llvm
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric /// getAllocatableClass - Return the maximal subclass of the given register
1920b57cec5SDimitry Andric /// class that is alloctable, or NULL.
1930b57cec5SDimitry Andric const TargetRegisterClass *
getAllocatableClass(const TargetRegisterClass * RC) const1940b57cec5SDimitry Andric TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
1950b57cec5SDimitry Andric   if (!RC || RC->isAllocatable())
1960b57cec5SDimitry Andric     return RC;
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   for (BitMaskClassIterator It(RC->getSubClassMask(), *this); It.isValid();
1990b57cec5SDimitry Andric        ++It) {
2000b57cec5SDimitry Andric     const TargetRegisterClass *SubRC = getRegClass(It.getID());
2010b57cec5SDimitry Andric     if (SubRC->isAllocatable())
2020b57cec5SDimitry Andric       return SubRC;
2030b57cec5SDimitry Andric   }
2040b57cec5SDimitry Andric   return nullptr;
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric /// getMinimalPhysRegClass - Returns the Register Class of a physical
2080b57cec5SDimitry Andric /// register of the given type, picking the most sub register class of
2090b57cec5SDimitry Andric /// the right type that contains this physreg.
2100b57cec5SDimitry Andric const TargetRegisterClass *
getMinimalPhysRegClass(MCRegister reg,MVT VT) const2115ffd83dbSDimitry Andric TargetRegisterInfo::getMinimalPhysRegClass(MCRegister reg, MVT VT) const {
2128bcb0991SDimitry Andric   assert(Register::isPhysicalRegister(reg) &&
2138bcb0991SDimitry Andric          "reg must be a physical register");
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   // Pick the most sub register class of the right type that contains
2160b57cec5SDimitry Andric   // this physreg.
2170b57cec5SDimitry Andric   const TargetRegisterClass* BestRC = nullptr;
2180b57cec5SDimitry Andric   for (const TargetRegisterClass* RC : regclasses()) {
2190b57cec5SDimitry Andric     if ((VT == MVT::Other || isTypeLegalForClass(*RC, VT)) &&
2200b57cec5SDimitry Andric         RC->contains(reg) && (!BestRC || BestRC->hasSubClass(RC)))
2210b57cec5SDimitry Andric       BestRC = RC;
2220b57cec5SDimitry Andric   }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric   assert(BestRC && "Couldn't find the register class");
2250b57cec5SDimitry Andric   return BestRC;
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric 
228*5f7ddb14SDimitry Andric const TargetRegisterClass *
getMinimalPhysRegClassLLT(MCRegister reg,LLT Ty) const229*5f7ddb14SDimitry Andric TargetRegisterInfo::getMinimalPhysRegClassLLT(MCRegister reg, LLT Ty) const {
230*5f7ddb14SDimitry Andric   assert(Register::isPhysicalRegister(reg) &&
231*5f7ddb14SDimitry Andric          "reg must be a physical register");
232*5f7ddb14SDimitry Andric 
233*5f7ddb14SDimitry Andric   // Pick the most sub register class of the right type that contains
234*5f7ddb14SDimitry Andric   // this physreg.
235*5f7ddb14SDimitry Andric   const TargetRegisterClass *BestRC = nullptr;
236*5f7ddb14SDimitry Andric   for (const TargetRegisterClass *RC : regclasses()) {
237*5f7ddb14SDimitry Andric     if ((!Ty.isValid() || isTypeLegalForClass(*RC, Ty)) && RC->contains(reg) &&
238*5f7ddb14SDimitry Andric         (!BestRC || BestRC->hasSubClass(RC)))
239*5f7ddb14SDimitry Andric       BestRC = RC;
240*5f7ddb14SDimitry Andric   }
241*5f7ddb14SDimitry Andric 
242*5f7ddb14SDimitry Andric   return BestRC;
243*5f7ddb14SDimitry Andric }
244*5f7ddb14SDimitry Andric 
2450b57cec5SDimitry Andric /// getAllocatableSetForRC - Toggle the bits that represent allocatable
2460b57cec5SDimitry Andric /// registers for the specific register class.
getAllocatableSetForRC(const MachineFunction & MF,const TargetRegisterClass * RC,BitVector & R)2470b57cec5SDimitry Andric static void getAllocatableSetForRC(const MachineFunction &MF,
2480b57cec5SDimitry Andric                                    const TargetRegisterClass *RC, BitVector &R){
2490b57cec5SDimitry Andric   assert(RC->isAllocatable() && "invalid for nonallocatable sets");
2500b57cec5SDimitry Andric   ArrayRef<MCPhysReg> Order = RC->getRawAllocationOrder(MF);
2510b57cec5SDimitry Andric   for (unsigned i = 0; i != Order.size(); ++i)
2520b57cec5SDimitry Andric     R.set(Order[i]);
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric 
getAllocatableSet(const MachineFunction & MF,const TargetRegisterClass * RC) const2550b57cec5SDimitry Andric BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
2560b57cec5SDimitry Andric                                           const TargetRegisterClass *RC) const {
2570b57cec5SDimitry Andric   BitVector Allocatable(getNumRegs());
2580b57cec5SDimitry Andric   if (RC) {
2590b57cec5SDimitry Andric     // A register class with no allocatable subclass returns an empty set.
2600b57cec5SDimitry Andric     const TargetRegisterClass *SubClass = getAllocatableClass(RC);
2610b57cec5SDimitry Andric     if (SubClass)
2620b57cec5SDimitry Andric       getAllocatableSetForRC(MF, SubClass, Allocatable);
2630b57cec5SDimitry Andric   } else {
2640b57cec5SDimitry Andric     for (const TargetRegisterClass *C : regclasses())
2650b57cec5SDimitry Andric       if (C->isAllocatable())
2660b57cec5SDimitry Andric         getAllocatableSetForRC(MF, C, Allocatable);
2670b57cec5SDimitry Andric   }
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric   // Mask out the reserved registers
270*5f7ddb14SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
271*5f7ddb14SDimitry Andric   const BitVector &Reserved = MRI.getReservedRegs();
272*5f7ddb14SDimitry Andric   Allocatable.reset(Reserved);
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric   return Allocatable;
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric static inline
firstCommonClass(const uint32_t * A,const uint32_t * B,const TargetRegisterInfo * TRI)2780b57cec5SDimitry Andric const TargetRegisterClass *firstCommonClass(const uint32_t *A,
2790b57cec5SDimitry Andric                                             const uint32_t *B,
2808bcb0991SDimitry Andric                                             const TargetRegisterInfo *TRI) {
2810b57cec5SDimitry Andric   for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
2828bcb0991SDimitry Andric     if (unsigned Common = *A++ & *B++)
2838bcb0991SDimitry Andric       return TRI->getRegClass(I + countTrailingZeros(Common));
2840b57cec5SDimitry Andric   return nullptr;
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric const TargetRegisterClass *
getCommonSubClass(const TargetRegisterClass * A,const TargetRegisterClass * B) const2880b57cec5SDimitry Andric TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
2898bcb0991SDimitry Andric                                       const TargetRegisterClass *B) const {
2900b57cec5SDimitry Andric   // First take care of the trivial cases.
2910b57cec5SDimitry Andric   if (A == B)
2920b57cec5SDimitry Andric     return A;
2930b57cec5SDimitry Andric   if (!A || !B)
2940b57cec5SDimitry Andric     return nullptr;
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   // Register classes are ordered topologically, so the largest common
2970b57cec5SDimitry Andric   // sub-class it the common sub-class with the smallest ID.
2988bcb0991SDimitry Andric   return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this);
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric const TargetRegisterClass *
getMatchingSuperRegClass(const TargetRegisterClass * A,const TargetRegisterClass * B,unsigned Idx) const3020b57cec5SDimitry Andric TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
3030b57cec5SDimitry Andric                                              const TargetRegisterClass *B,
3040b57cec5SDimitry Andric                                              unsigned Idx) const {
3050b57cec5SDimitry Andric   assert(A && B && "Missing register class");
3060b57cec5SDimitry Andric   assert(Idx && "Bad sub-register index");
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   // Find Idx in the list of super-register indices.
3090b57cec5SDimitry Andric   for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
3100b57cec5SDimitry Andric     if (RCI.getSubReg() == Idx)
3110b57cec5SDimitry Andric       // The bit mask contains all register classes that are projected into B
3120b57cec5SDimitry Andric       // by Idx. Find a class that is also a sub-class of A.
3130b57cec5SDimitry Andric       return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this);
3140b57cec5SDimitry Andric   return nullptr;
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric const TargetRegisterClass *TargetRegisterInfo::
getCommonSuperRegClass(const TargetRegisterClass * RCA,unsigned SubA,const TargetRegisterClass * RCB,unsigned SubB,unsigned & PreA,unsigned & PreB) const3180b57cec5SDimitry Andric getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
3190b57cec5SDimitry Andric                        const TargetRegisterClass *RCB, unsigned SubB,
3200b57cec5SDimitry Andric                        unsigned &PreA, unsigned &PreB) const {
3210b57cec5SDimitry Andric   assert(RCA && SubA && RCB && SubB && "Invalid arguments");
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   // Search all pairs of sub-register indices that project into RCA and RCB
3240b57cec5SDimitry Andric   // respectively. This is quadratic, but usually the sets are very small. On
3250b57cec5SDimitry Andric   // most targets like X86, there will only be a single sub-register index
3260b57cec5SDimitry Andric   // (e.g., sub_16bit projecting into GR16).
3270b57cec5SDimitry Andric   //
3280b57cec5SDimitry Andric   // The worst case is a register class like DPR on ARM.
3290b57cec5SDimitry Andric   // We have indices dsub_0..dsub_7 projecting into that class.
3300b57cec5SDimitry Andric   //
3310b57cec5SDimitry Andric   // It is very common that one register class is a sub-register of the other.
3320b57cec5SDimitry Andric   // Arrange for RCA to be the larger register so the answer will be found in
3330b57cec5SDimitry Andric   // the first iteration. This makes the search linear for the most common
3340b57cec5SDimitry Andric   // case.
3350b57cec5SDimitry Andric   const TargetRegisterClass *BestRC = nullptr;
3360b57cec5SDimitry Andric   unsigned *BestPreA = &PreA;
3370b57cec5SDimitry Andric   unsigned *BestPreB = &PreB;
3380b57cec5SDimitry Andric   if (getRegSizeInBits(*RCA) < getRegSizeInBits(*RCB)) {
3390b57cec5SDimitry Andric     std::swap(RCA, RCB);
3400b57cec5SDimitry Andric     std::swap(SubA, SubB);
3410b57cec5SDimitry Andric     std::swap(BestPreA, BestPreB);
3420b57cec5SDimitry Andric   }
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric   // Also terminate the search one we have found a register class as small as
3450b57cec5SDimitry Andric   // RCA.
3460b57cec5SDimitry Andric   unsigned MinSize = getRegSizeInBits(*RCA);
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric   for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
3490b57cec5SDimitry Andric     unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
3500b57cec5SDimitry Andric     for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
3510b57cec5SDimitry Andric       // Check if a common super-register class exists for this index pair.
3520b57cec5SDimitry Andric       const TargetRegisterClass *RC =
3530b57cec5SDimitry Andric         firstCommonClass(IA.getMask(), IB.getMask(), this);
3540b57cec5SDimitry Andric       if (!RC || getRegSizeInBits(*RC) < MinSize)
3550b57cec5SDimitry Andric         continue;
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric       // The indexes must compose identically: PreA+SubA == PreB+SubB.
3580b57cec5SDimitry Andric       unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
3590b57cec5SDimitry Andric       if (FinalA != FinalB)
3600b57cec5SDimitry Andric         continue;
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric       // Is RC a better candidate than BestRC?
3630b57cec5SDimitry Andric       if (BestRC && getRegSizeInBits(*RC) >= getRegSizeInBits(*BestRC))
3640b57cec5SDimitry Andric         continue;
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric       // Yes, RC is the smallest super-register seen so far.
3670b57cec5SDimitry Andric       BestRC = RC;
3680b57cec5SDimitry Andric       *BestPreA = IA.getSubReg();
3690b57cec5SDimitry Andric       *BestPreB = IB.getSubReg();
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric       // Bail early if we reached MinSize. We won't find a better candidate.
3720b57cec5SDimitry Andric       if (getRegSizeInBits(*BestRC) == MinSize)
3730b57cec5SDimitry Andric         return BestRC;
3740b57cec5SDimitry Andric     }
3750b57cec5SDimitry Andric   }
3760b57cec5SDimitry Andric   return BestRC;
3770b57cec5SDimitry Andric }
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric /// Check if the registers defined by the pair (RegisterClass, SubReg)
3800b57cec5SDimitry Andric /// share the same register file.
shareSameRegisterFile(const TargetRegisterInfo & TRI,const TargetRegisterClass * DefRC,unsigned DefSubReg,const TargetRegisterClass * SrcRC,unsigned SrcSubReg)3810b57cec5SDimitry Andric static bool shareSameRegisterFile(const TargetRegisterInfo &TRI,
3820b57cec5SDimitry Andric                                   const TargetRegisterClass *DefRC,
3830b57cec5SDimitry Andric                                   unsigned DefSubReg,
3840b57cec5SDimitry Andric                                   const TargetRegisterClass *SrcRC,
3850b57cec5SDimitry Andric                                   unsigned SrcSubReg) {
3860b57cec5SDimitry Andric   // Same register class.
3870b57cec5SDimitry Andric   if (DefRC == SrcRC)
3880b57cec5SDimitry Andric     return true;
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   // Both operands are sub registers. Check if they share a register class.
3910b57cec5SDimitry Andric   unsigned SrcIdx, DefIdx;
3920b57cec5SDimitry Andric   if (SrcSubReg && DefSubReg) {
3930b57cec5SDimitry Andric     return TRI.getCommonSuperRegClass(SrcRC, SrcSubReg, DefRC, DefSubReg,
3940b57cec5SDimitry Andric                                       SrcIdx, DefIdx) != nullptr;
3950b57cec5SDimitry Andric   }
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric   // At most one of the register is a sub register, make it Src to avoid
3980b57cec5SDimitry Andric   // duplicating the test.
3990b57cec5SDimitry Andric   if (!SrcSubReg) {
4000b57cec5SDimitry Andric     std::swap(DefSubReg, SrcSubReg);
4010b57cec5SDimitry Andric     std::swap(DefRC, SrcRC);
4020b57cec5SDimitry Andric   }
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric   // One of the register is a sub register, check if we can get a superclass.
4050b57cec5SDimitry Andric   if (SrcSubReg)
4060b57cec5SDimitry Andric     return TRI.getMatchingSuperRegClass(SrcRC, DefRC, SrcSubReg) != nullptr;
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   // Plain copy.
4090b57cec5SDimitry Andric   return TRI.getCommonSubClass(DefRC, SrcRC) != nullptr;
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric 
shouldRewriteCopySrc(const TargetRegisterClass * DefRC,unsigned DefSubReg,const TargetRegisterClass * SrcRC,unsigned SrcSubReg) const4120b57cec5SDimitry Andric bool TargetRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
4130b57cec5SDimitry Andric                                               unsigned DefSubReg,
4140b57cec5SDimitry Andric                                               const TargetRegisterClass *SrcRC,
4150b57cec5SDimitry Andric                                               unsigned SrcSubReg) const {
4160b57cec5SDimitry Andric   // If this source does not incur a cross register bank copy, use it.
4170b57cec5SDimitry Andric   return shareSameRegisterFile(*this, DefRC, DefSubReg, SrcRC, SrcSubReg);
4180b57cec5SDimitry Andric }
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry 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) const4215ffd83dbSDimitry Andric bool TargetRegisterInfo::getRegAllocationHints(
4225ffd83dbSDimitry Andric     Register VirtReg, ArrayRef<MCPhysReg> Order,
4235ffd83dbSDimitry Andric     SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
4245ffd83dbSDimitry Andric     const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
4250b57cec5SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
4265ffd83dbSDimitry Andric   const std::pair<Register, SmallVector<Register, 4>> &Hints_MRI =
4270b57cec5SDimitry Andric     MRI.getRegAllocationHints(VirtReg);
4280b57cec5SDimitry Andric 
4295ffd83dbSDimitry Andric   SmallSet<Register, 32> HintedRegs;
4300b57cec5SDimitry Andric   // First hint may be a target hint.
4310b57cec5SDimitry Andric   bool Skip = (Hints_MRI.first != 0);
4320b57cec5SDimitry Andric   for (auto Reg : Hints_MRI.second) {
4330b57cec5SDimitry Andric     if (Skip) {
4340b57cec5SDimitry Andric       Skip = false;
4350b57cec5SDimitry Andric       continue;
4360b57cec5SDimitry Andric     }
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric     // Target-independent hints are either a physical or a virtual register.
4395ffd83dbSDimitry Andric     Register Phys = Reg;
4405ffd83dbSDimitry Andric     if (VRM && Phys.isVirtual())
4410b57cec5SDimitry Andric       Phys = VRM->getPhys(Phys);
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric     // Don't add the same reg twice (Hints_MRI may contain multiple virtual
4440b57cec5SDimitry Andric     // registers allocated to the same physreg).
4450b57cec5SDimitry Andric     if (!HintedRegs.insert(Phys).second)
4460b57cec5SDimitry Andric       continue;
4470b57cec5SDimitry Andric     // Check that Phys is a valid hint in VirtReg's register class.
4485ffd83dbSDimitry Andric     if (!Phys.isPhysical())
4490b57cec5SDimitry Andric       continue;
4500b57cec5SDimitry Andric     if (MRI.isReserved(Phys))
4510b57cec5SDimitry Andric       continue;
4520b57cec5SDimitry Andric     // Check that Phys is in the allocation order. We shouldn't heed hints
4530b57cec5SDimitry Andric     // from VirtReg's register class if they aren't in the allocation order. The
4540b57cec5SDimitry Andric     // target probably has a reason for removing the register.
4550b57cec5SDimitry Andric     if (!is_contained(Order, Phys))
4560b57cec5SDimitry Andric       continue;
4570b57cec5SDimitry Andric 
4580b57cec5SDimitry Andric     // All clear, tell the register allocator to prefer this register.
4590b57cec5SDimitry Andric     Hints.push_back(Phys);
4600b57cec5SDimitry Andric   }
4610b57cec5SDimitry Andric   return false;
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric 
isCalleeSavedPhysReg(MCRegister PhysReg,const MachineFunction & MF) const4648bcb0991SDimitry Andric bool TargetRegisterInfo::isCalleeSavedPhysReg(
4655ffd83dbSDimitry Andric     MCRegister PhysReg, const MachineFunction &MF) const {
4668bcb0991SDimitry Andric   if (PhysReg == 0)
4678bcb0991SDimitry Andric     return false;
4688bcb0991SDimitry Andric   const uint32_t *callerPreservedRegs =
4698bcb0991SDimitry Andric       getCallPreservedMask(MF, MF.getFunction().getCallingConv());
4708bcb0991SDimitry Andric   if (callerPreservedRegs) {
4718bcb0991SDimitry Andric     assert(Register::isPhysicalRegister(PhysReg) &&
4728bcb0991SDimitry Andric            "Expected physical register");
4738bcb0991SDimitry Andric     return (callerPreservedRegs[PhysReg / 32] >> PhysReg % 32) & 1;
4748bcb0991SDimitry Andric   }
4758bcb0991SDimitry Andric   return false;
4768bcb0991SDimitry Andric }
4778bcb0991SDimitry Andric 
canRealignStack(const MachineFunction & MF) const4780b57cec5SDimitry Andric bool TargetRegisterInfo::canRealignStack(const MachineFunction &MF) const {
4790b57cec5SDimitry Andric   return !MF.getFunction().hasFnAttribute("no-realign-stack");
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric 
shouldRealignStack(const MachineFunction & MF) const482*5f7ddb14SDimitry Andric bool TargetRegisterInfo::shouldRealignStack(const MachineFunction &MF) const {
4830b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF.getFrameInfo();
4840b57cec5SDimitry Andric   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
4850b57cec5SDimitry Andric   const Function &F = MF.getFunction();
486*5f7ddb14SDimitry Andric   return F.hasFnAttribute("stackrealign") ||
487*5f7ddb14SDimitry Andric          (MFI.getMaxAlign() > TFI->getStackAlign()) ||
488*5f7ddb14SDimitry Andric          F.hasFnAttribute(Attribute::StackAlignment);
4890b57cec5SDimitry Andric }
4900b57cec5SDimitry Andric 
regmaskSubsetEqual(const uint32_t * mask0,const uint32_t * mask1) const4910b57cec5SDimitry Andric bool TargetRegisterInfo::regmaskSubsetEqual(const uint32_t *mask0,
4920b57cec5SDimitry Andric                                             const uint32_t *mask1) const {
4930b57cec5SDimitry Andric   unsigned N = (getNumRegs()+31) / 32;
4940b57cec5SDimitry Andric   for (unsigned I = 0; I < N; ++I)
4950b57cec5SDimitry Andric     if ((mask0[I] & mask1[I]) != mask0[I])
4960b57cec5SDimitry Andric       return false;
4970b57cec5SDimitry Andric   return true;
4980b57cec5SDimitry Andric }
4990b57cec5SDimitry Andric 
5005ffd83dbSDimitry Andric unsigned
getRegSizeInBits(Register Reg,const MachineRegisterInfo & MRI) const5015ffd83dbSDimitry Andric TargetRegisterInfo::getRegSizeInBits(Register Reg,
5020b57cec5SDimitry Andric                                      const MachineRegisterInfo &MRI) const {
5030b57cec5SDimitry Andric   const TargetRegisterClass *RC{};
5045ffd83dbSDimitry Andric   if (Reg.isPhysical()) {
5050b57cec5SDimitry Andric     // The size is not directly available for physical registers.
5060b57cec5SDimitry Andric     // Instead, we need to access a register class that contains Reg and
5070b57cec5SDimitry Andric     // get the size of that register class.
5080b57cec5SDimitry Andric     RC = getMinimalPhysRegClass(Reg);
5090b57cec5SDimitry Andric   } else {
5100b57cec5SDimitry Andric     LLT Ty = MRI.getType(Reg);
5110b57cec5SDimitry Andric     unsigned RegSize = Ty.isValid() ? Ty.getSizeInBits() : 0;
5120b57cec5SDimitry Andric     // If Reg is not a generic register, query the register class to
5130b57cec5SDimitry Andric     // get its size.
5140b57cec5SDimitry Andric     if (RegSize)
5150b57cec5SDimitry Andric       return RegSize;
5160b57cec5SDimitry Andric     // Since Reg is not a generic register, it must have a register class.
5170b57cec5SDimitry Andric     RC = MRI.getRegClass(Reg);
5180b57cec5SDimitry Andric   }
5190b57cec5SDimitry Andric   assert(RC && "Unable to deduce the register class");
5200b57cec5SDimitry Andric   return getRegSizeInBits(*RC);
5210b57cec5SDimitry Andric }
5220b57cec5SDimitry Andric 
getCoveringSubRegIndexes(const MachineRegisterInfo & MRI,const TargetRegisterClass * RC,LaneBitmask LaneMask,SmallVectorImpl<unsigned> & NeededIndexes) const523*5f7ddb14SDimitry Andric bool TargetRegisterInfo::getCoveringSubRegIndexes(
524*5f7ddb14SDimitry Andric     const MachineRegisterInfo &MRI, const TargetRegisterClass *RC,
525*5f7ddb14SDimitry Andric     LaneBitmask LaneMask, SmallVectorImpl<unsigned> &NeededIndexes) const {
526*5f7ddb14SDimitry Andric   SmallVector<unsigned, 8> PossibleIndexes;
527*5f7ddb14SDimitry Andric   unsigned BestIdx = 0;
528*5f7ddb14SDimitry Andric   unsigned BestCover = 0;
529*5f7ddb14SDimitry Andric 
530*5f7ddb14SDimitry Andric   for (unsigned Idx = 1, E = getNumSubRegIndices(); Idx < E; ++Idx) {
531*5f7ddb14SDimitry Andric     // Is this index even compatible with the given class?
532*5f7ddb14SDimitry Andric     if (getSubClassWithSubReg(RC, Idx) != RC)
533*5f7ddb14SDimitry Andric       continue;
534*5f7ddb14SDimitry Andric     LaneBitmask SubRegMask = getSubRegIndexLaneMask(Idx);
535*5f7ddb14SDimitry Andric     // Early exit if we found a perfect match.
536*5f7ddb14SDimitry Andric     if (SubRegMask == LaneMask) {
537*5f7ddb14SDimitry Andric       BestIdx = Idx;
538*5f7ddb14SDimitry Andric       break;
539*5f7ddb14SDimitry Andric     }
540*5f7ddb14SDimitry Andric 
541*5f7ddb14SDimitry Andric     // The index must not cover any lanes outside \p LaneMask.
542*5f7ddb14SDimitry Andric     if ((SubRegMask & ~LaneMask).any())
543*5f7ddb14SDimitry Andric       continue;
544*5f7ddb14SDimitry Andric 
545*5f7ddb14SDimitry Andric     unsigned PopCount = SubRegMask.getNumLanes();
546*5f7ddb14SDimitry Andric     PossibleIndexes.push_back(Idx);
547*5f7ddb14SDimitry Andric     if (PopCount > BestCover) {
548*5f7ddb14SDimitry Andric       BestCover = PopCount;
549*5f7ddb14SDimitry Andric       BestIdx = Idx;
550*5f7ddb14SDimitry Andric     }
551*5f7ddb14SDimitry Andric   }
552*5f7ddb14SDimitry Andric 
553*5f7ddb14SDimitry Andric   // Abort if we cannot possibly implement the COPY with the given indexes.
554*5f7ddb14SDimitry Andric   if (BestIdx == 0)
555*5f7ddb14SDimitry Andric     return 0;
556*5f7ddb14SDimitry Andric 
557*5f7ddb14SDimitry Andric   NeededIndexes.push_back(BestIdx);
558*5f7ddb14SDimitry Andric 
559*5f7ddb14SDimitry Andric   // Greedy heuristic: Keep iterating keeping the best covering subreg index
560*5f7ddb14SDimitry Andric   // each time.
561*5f7ddb14SDimitry Andric   LaneBitmask LanesLeft = LaneMask & ~getSubRegIndexLaneMask(BestIdx);
562*5f7ddb14SDimitry Andric   while (LanesLeft.any()) {
563*5f7ddb14SDimitry Andric     unsigned BestIdx = 0;
564*5f7ddb14SDimitry Andric     int BestCover = std::numeric_limits<int>::min();
565*5f7ddb14SDimitry Andric     for (unsigned Idx : PossibleIndexes) {
566*5f7ddb14SDimitry Andric       LaneBitmask SubRegMask = getSubRegIndexLaneMask(Idx);
567*5f7ddb14SDimitry Andric       // Early exit if we found a perfect match.
568*5f7ddb14SDimitry Andric       if (SubRegMask == LanesLeft) {
569*5f7ddb14SDimitry Andric         BestIdx = Idx;
570*5f7ddb14SDimitry Andric         break;
571*5f7ddb14SDimitry Andric       }
572*5f7ddb14SDimitry Andric 
573*5f7ddb14SDimitry Andric       // Try to cover as much of the remaining lanes as possible but
574*5f7ddb14SDimitry Andric       // as few of the already covered lanes as possible.
575*5f7ddb14SDimitry Andric       int Cover = (SubRegMask & LanesLeft).getNumLanes() -
576*5f7ddb14SDimitry Andric                   (SubRegMask & ~LanesLeft).getNumLanes();
577*5f7ddb14SDimitry Andric       if (Cover > BestCover) {
578*5f7ddb14SDimitry Andric         BestCover = Cover;
579*5f7ddb14SDimitry Andric         BestIdx = Idx;
580*5f7ddb14SDimitry Andric       }
581*5f7ddb14SDimitry Andric     }
582*5f7ddb14SDimitry Andric 
583*5f7ddb14SDimitry Andric     if (BestIdx == 0)
584*5f7ddb14SDimitry Andric       return 0; // Impossible to handle
585*5f7ddb14SDimitry Andric 
586*5f7ddb14SDimitry Andric     NeededIndexes.push_back(BestIdx);
587*5f7ddb14SDimitry Andric 
588*5f7ddb14SDimitry Andric     LanesLeft &= ~getSubRegIndexLaneMask(BestIdx);
589*5f7ddb14SDimitry Andric   }
590*5f7ddb14SDimitry Andric 
591*5f7ddb14SDimitry Andric   return BestIdx;
592*5f7ddb14SDimitry Andric }
593*5f7ddb14SDimitry Andric 
5945ffd83dbSDimitry Andric Register
lookThruCopyLike(Register SrcReg,const MachineRegisterInfo * MRI) const5955ffd83dbSDimitry Andric TargetRegisterInfo::lookThruCopyLike(Register SrcReg,
5960b57cec5SDimitry Andric                                      const MachineRegisterInfo *MRI) const {
5970b57cec5SDimitry Andric   while (true) {
5980b57cec5SDimitry Andric     const MachineInstr *MI = MRI->getVRegDef(SrcReg);
5990b57cec5SDimitry Andric     if (!MI->isCopyLike())
6000b57cec5SDimitry Andric       return SrcReg;
6010b57cec5SDimitry Andric 
6025ffd83dbSDimitry Andric     Register CopySrcReg;
6030b57cec5SDimitry Andric     if (MI->isCopy())
6040b57cec5SDimitry Andric       CopySrcReg = MI->getOperand(1).getReg();
6050b57cec5SDimitry Andric     else {
6060b57cec5SDimitry Andric       assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
6070b57cec5SDimitry Andric       CopySrcReg = MI->getOperand(2).getReg();
6080b57cec5SDimitry Andric     }
6090b57cec5SDimitry Andric 
6105ffd83dbSDimitry Andric     if (!CopySrcReg.isVirtual())
6110b57cec5SDimitry Andric       return CopySrcReg;
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric     SrcReg = CopySrcReg;
6140b57cec5SDimitry Andric   }
6150b57cec5SDimitry Andric }
6160b57cec5SDimitry Andric 
lookThruSingleUseCopyChain(Register SrcReg,const MachineRegisterInfo * MRI) const617af732203SDimitry Andric Register TargetRegisterInfo::lookThruSingleUseCopyChain(
618af732203SDimitry Andric     Register SrcReg, const MachineRegisterInfo *MRI) const {
619af732203SDimitry Andric   while (true) {
620af732203SDimitry Andric     const MachineInstr *MI = MRI->getVRegDef(SrcReg);
621af732203SDimitry Andric     // Found the real definition, return it if it has a single use.
622af732203SDimitry Andric     if (!MI->isCopyLike())
623af732203SDimitry Andric       return MRI->hasOneNonDBGUse(SrcReg) ? SrcReg : Register();
624af732203SDimitry Andric 
625af732203SDimitry Andric     Register CopySrcReg;
626af732203SDimitry Andric     if (MI->isCopy())
627af732203SDimitry Andric       CopySrcReg = MI->getOperand(1).getReg();
628af732203SDimitry Andric     else {
629af732203SDimitry Andric       assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
630af732203SDimitry Andric       CopySrcReg = MI->getOperand(2).getReg();
631af732203SDimitry Andric     }
632af732203SDimitry Andric 
633af732203SDimitry Andric     // Continue only if the next definition in the chain is for a virtual
634af732203SDimitry Andric     // register that has a single use.
635af732203SDimitry Andric     if (!CopySrcReg.isVirtual() || !MRI->hasOneNonDBGUse(CopySrcReg))
636af732203SDimitry Andric       return Register();
637af732203SDimitry Andric 
638af732203SDimitry Andric     SrcReg = CopySrcReg;
639af732203SDimitry Andric   }
640af732203SDimitry Andric }
641af732203SDimitry Andric 
getOffsetOpcodes(const StackOffset & Offset,SmallVectorImpl<uint64_t> & Ops) const642af732203SDimitry Andric void TargetRegisterInfo::getOffsetOpcodes(
643af732203SDimitry Andric     const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const {
644af732203SDimitry Andric   assert(!Offset.getScalable() && "Scalable offsets are not handled");
645af732203SDimitry Andric   DIExpression::appendOffset(Ops, Offset.getFixed());
646af732203SDimitry Andric }
647af732203SDimitry Andric 
648af732203SDimitry Andric DIExpression *
prependOffsetExpression(const DIExpression * Expr,unsigned PrependFlags,const StackOffset & Offset) const649af732203SDimitry Andric TargetRegisterInfo::prependOffsetExpression(const DIExpression *Expr,
650af732203SDimitry Andric                                             unsigned PrependFlags,
651af732203SDimitry Andric                                             const StackOffset &Offset) const {
652af732203SDimitry Andric   assert((PrependFlags &
653af732203SDimitry Andric           ~(DIExpression::DerefBefore | DIExpression::DerefAfter |
654af732203SDimitry Andric             DIExpression::StackValue | DIExpression::EntryValue)) == 0 &&
655af732203SDimitry Andric          "Unsupported prepend flag");
656af732203SDimitry Andric   SmallVector<uint64_t, 16> OffsetExpr;
657af732203SDimitry Andric   if (PrependFlags & DIExpression::DerefBefore)
658af732203SDimitry Andric     OffsetExpr.push_back(dwarf::DW_OP_deref);
659af732203SDimitry Andric   getOffsetOpcodes(Offset, OffsetExpr);
660af732203SDimitry Andric   if (PrependFlags & DIExpression::DerefAfter)
661af732203SDimitry Andric     OffsetExpr.push_back(dwarf::DW_OP_deref);
662af732203SDimitry Andric   return DIExpression::prependOpcodes(Expr, OffsetExpr,
663af732203SDimitry Andric                                       PrependFlags & DIExpression::StackValue,
664af732203SDimitry Andric                                       PrependFlags & DIExpression::EntryValue);
665af732203SDimitry Andric }
666af732203SDimitry Andric 
6670b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
6680b57cec5SDimitry Andric LLVM_DUMP_METHOD
dumpReg(Register Reg,unsigned SubRegIndex,const TargetRegisterInfo * TRI)6695ffd83dbSDimitry Andric void TargetRegisterInfo::dumpReg(Register Reg, unsigned SubRegIndex,
6700b57cec5SDimitry Andric                                  const TargetRegisterInfo *TRI) {
6710b57cec5SDimitry Andric   dbgs() << printReg(Reg, TRI, SubRegIndex) << "\n";
6720b57cec5SDimitry Andric }
6730b57cec5SDimitry Andric #endif
674