TargetRegisterInfo.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. //==- TargetRegisterInfo.cpp - Target Register Information Implementation --==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the TargetRegisterInfo interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/TargetRegisterInfo.h"
  13. #include "llvm/ADT/ArrayRef.h"
  14. #include "llvm/ADT/BitVector.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/SmallSet.h"
  17. #include "llvm/ADT/StringExtras.h"
  18. #include "llvm/CodeGen/MachineFrameInfo.h"
  19. #include "llvm/CodeGen/MachineFunction.h"
  20. #include "llvm/CodeGen/MachineRegisterInfo.h"
  21. #include "llvm/CodeGen/LiveInterval.h"
  22. #include "llvm/CodeGen/TargetFrameLowering.h"
  23. #include "llvm/CodeGen/TargetInstrInfo.h"
  24. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  25. #include "llvm/CodeGen/VirtRegMap.h"
  26. #include "llvm/Config/llvm-config.h"
  27. #include "llvm/IR/Attributes.h"
  28. #include "llvm/IR/DebugInfoMetadata.h"
  29. #include "llvm/IR/Function.h"
  30. #include "llvm/MC/MCRegisterInfo.h"
  31. #include "llvm/Support/CommandLine.h"
  32. #include "llvm/Support/Compiler.h"
  33. #include "llvm/Support/Debug.h"
  34. #include "llvm/Support/MachineValueType.h"
  35. #include "llvm/Support/MathExtras.h"
  36. #include "llvm/Support/Printable.h"
  37. #include "llvm/Support/raw_ostream.h"
  38. #include <cassert>
  39. #include <utility>
  40. #define DEBUG_TYPE "target-reg-info"
  41. using namespace llvm;
  42. static cl::opt<unsigned>
  43. HugeSizeForSplit("huge-size-for-split", cl::Hidden,
  44. cl::desc("A threshold of live range size which may cause "
  45. "high compile time cost in global splitting."),
  46. cl::init(5000));
  47. TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
  48. regclass_iterator RCB, regclass_iterator RCE,
  49. const char *const *SRINames,
  50. const LaneBitmask *SRILaneMasks,
  51. LaneBitmask SRICoveringLanes,
  52. const RegClassInfo *const RCIs,
  53. unsigned Mode)
  54. : InfoDesc(ID), SubRegIndexNames(SRINames),
  55. SubRegIndexLaneMasks(SRILaneMasks),
  56. RegClassBegin(RCB), RegClassEnd(RCE),
  57. CoveringLanes(SRICoveringLanes),
  58. RCInfos(RCIs), HwMode(Mode) {
  59. }
  60. TargetRegisterInfo::~TargetRegisterInfo() = default;
  61. bool TargetRegisterInfo::shouldRegionSplitForVirtReg(
  62. const MachineFunction &MF, const LiveInterval &VirtReg) const {
  63. const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
  64. const MachineRegisterInfo &MRI = MF.getRegInfo();
  65. MachineInstr *MI = MRI.getUniqueVRegDef(VirtReg.reg());
  66. if (MI && TII->isTriviallyReMaterializable(*MI) &&
  67. VirtReg.size() > HugeSizeForSplit)
  68. return false;
  69. return true;
  70. }
  71. void TargetRegisterInfo::markSuperRegs(BitVector &RegisterSet,
  72. MCRegister Reg) const {
  73. for (MCSuperRegIterator AI(Reg, this, true); AI.isValid(); ++AI)
  74. RegisterSet.set(*AI);
  75. }
  76. bool TargetRegisterInfo::checkAllSuperRegsMarked(const BitVector &RegisterSet,
  77. ArrayRef<MCPhysReg> Exceptions) const {
  78. // Check that all super registers of reserved regs are reserved as well.
  79. BitVector Checked(getNumRegs());
  80. for (unsigned Reg : RegisterSet.set_bits()) {
  81. if (Checked[Reg])
  82. continue;
  83. for (MCSuperRegIterator SR(Reg, this); SR.isValid(); ++SR) {
  84. if (!RegisterSet[*SR] && !is_contained(Exceptions, Reg)) {
  85. dbgs() << "Error: Super register " << printReg(*SR, this)
  86. << " of reserved register " << printReg(Reg, this)
  87. << " is not reserved.\n";
  88. return false;
  89. }
  90. // We transitively check superregs. So we can remember this for later
  91. // to avoid compiletime explosion in deep register hierarchies.
  92. Checked.set(*SR);
  93. }
  94. }
  95. return true;
  96. }
  97. namespace llvm {
  98. Printable printReg(Register Reg, const TargetRegisterInfo *TRI,
  99. unsigned SubIdx, const MachineRegisterInfo *MRI) {
  100. return Printable([Reg, TRI, SubIdx, MRI](raw_ostream &OS) {
  101. if (!Reg)
  102. OS << "$noreg";
  103. else if (Register::isStackSlot(Reg))
  104. OS << "SS#" << Register::stackSlot2Index(Reg);
  105. else if (Register::isVirtualRegister(Reg)) {
  106. StringRef Name = MRI ? MRI->getVRegName(Reg) : "";
  107. if (Name != "") {
  108. OS << '%' << Name;
  109. } else {
  110. OS << '%' << Register::virtReg2Index(Reg);
  111. }
  112. } else if (!TRI)
  113. OS << '$' << "physreg" << Reg;
  114. else if (Reg < TRI->getNumRegs()) {
  115. OS << '$';
  116. printLowerCase(TRI->getName(Reg), OS);
  117. } else
  118. llvm_unreachable("Register kind is unsupported.");
  119. if (SubIdx) {
  120. if (TRI)
  121. OS << ':' << TRI->getSubRegIndexName(SubIdx);
  122. else
  123. OS << ":sub(" << SubIdx << ')';
  124. }
  125. });
  126. }
  127. Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
  128. return Printable([Unit, TRI](raw_ostream &OS) {
  129. // Generic printout when TRI is missing.
  130. if (!TRI) {
  131. OS << "Unit~" << Unit;
  132. return;
  133. }
  134. // Check for invalid register units.
  135. if (Unit >= TRI->getNumRegUnits()) {
  136. OS << "BadUnit~" << Unit;
  137. return;
  138. }
  139. // Normal units have at least one root.
  140. MCRegUnitRootIterator Roots(Unit, TRI);
  141. assert(Roots.isValid() && "Unit has no roots.");
  142. OS << TRI->getName(*Roots);
  143. for (++Roots; Roots.isValid(); ++Roots)
  144. OS << '~' << TRI->getName(*Roots);
  145. });
  146. }
  147. Printable printVRegOrUnit(unsigned Unit, const TargetRegisterInfo *TRI) {
  148. return Printable([Unit, TRI](raw_ostream &OS) {
  149. if (Register::isVirtualRegister(Unit)) {
  150. OS << '%' << Register::virtReg2Index(Unit);
  151. } else {
  152. OS << printRegUnit(Unit, TRI);
  153. }
  154. });
  155. }
  156. Printable printRegClassOrBank(Register Reg, const MachineRegisterInfo &RegInfo,
  157. const TargetRegisterInfo *TRI) {
  158. return Printable([Reg, &RegInfo, TRI](raw_ostream &OS) {
  159. if (RegInfo.getRegClassOrNull(Reg))
  160. OS << StringRef(TRI->getRegClassName(RegInfo.getRegClass(Reg))).lower();
  161. else if (RegInfo.getRegBankOrNull(Reg))
  162. OS << StringRef(RegInfo.getRegBankOrNull(Reg)->getName()).lower();
  163. else {
  164. OS << "_";
  165. assert((RegInfo.def_empty(Reg) || RegInfo.getType(Reg).isValid()) &&
  166. "Generic registers must have a valid type");
  167. }
  168. });
  169. }
  170. } // end namespace llvm
  171. /// getAllocatableClass - Return the maximal subclass of the given register
  172. /// class that is alloctable, or NULL.
  173. const TargetRegisterClass *
  174. TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
  175. if (!RC || RC->isAllocatable())
  176. return RC;
  177. for (BitMaskClassIterator It(RC->getSubClassMask(), *this); It.isValid();
  178. ++It) {
  179. const TargetRegisterClass *SubRC = getRegClass(It.getID());
  180. if (SubRC->isAllocatable())
  181. return SubRC;
  182. }
  183. return nullptr;
  184. }
  185. /// getMinimalPhysRegClass - Returns the Register Class of a physical
  186. /// register of the given type, picking the most sub register class of
  187. /// the right type that contains this physreg.
  188. const TargetRegisterClass *
  189. TargetRegisterInfo::getMinimalPhysRegClass(MCRegister reg, MVT VT) const {
  190. assert(Register::isPhysicalRegister(reg) &&
  191. "reg must be a physical register");
  192. // Pick the most sub register class of the right type that contains
  193. // this physreg.
  194. const TargetRegisterClass* BestRC = nullptr;
  195. for (const TargetRegisterClass* RC : regclasses()) {
  196. if ((VT == MVT::Other || isTypeLegalForClass(*RC, VT)) &&
  197. RC->contains(reg) && (!BestRC || BestRC->hasSubClass(RC)))
  198. BestRC = RC;
  199. }
  200. assert(BestRC && "Couldn't find the register class");
  201. return BestRC;
  202. }
  203. const TargetRegisterClass *
  204. TargetRegisterInfo::getMinimalPhysRegClassLLT(MCRegister reg, LLT Ty) const {
  205. assert(Register::isPhysicalRegister(reg) &&
  206. "reg must be a physical register");
  207. // Pick the most sub register class of the right type that contains
  208. // this physreg.
  209. const TargetRegisterClass *BestRC = nullptr;
  210. for (const TargetRegisterClass *RC : regclasses()) {
  211. if ((!Ty.isValid() || isTypeLegalForClass(*RC, Ty)) && RC->contains(reg) &&
  212. (!BestRC || BestRC->hasSubClass(RC)))
  213. BestRC = RC;
  214. }
  215. return BestRC;
  216. }
  217. /// getAllocatableSetForRC - Toggle the bits that represent allocatable
  218. /// registers for the specific register class.
  219. static void getAllocatableSetForRC(const MachineFunction &MF,
  220. const TargetRegisterClass *RC, BitVector &R){
  221. assert(RC->isAllocatable() && "invalid for nonallocatable sets");
  222. ArrayRef<MCPhysReg> Order = RC->getRawAllocationOrder(MF);
  223. for (MCPhysReg PR : Order)
  224. R.set(PR);
  225. }
  226. BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
  227. const TargetRegisterClass *RC) const {
  228. BitVector Allocatable(getNumRegs());
  229. if (RC) {
  230. // A register class with no allocatable subclass returns an empty set.
  231. const TargetRegisterClass *SubClass = getAllocatableClass(RC);
  232. if (SubClass)
  233. getAllocatableSetForRC(MF, SubClass, Allocatable);
  234. } else {
  235. for (const TargetRegisterClass *C : regclasses())
  236. if (C->isAllocatable())
  237. getAllocatableSetForRC(MF, C, Allocatable);
  238. }
  239. // Mask out the reserved registers
  240. const MachineRegisterInfo &MRI = MF.getRegInfo();
  241. const BitVector &Reserved = MRI.getReservedRegs();
  242. Allocatable.reset(Reserved);
  243. return Allocatable;
  244. }
  245. static inline
  246. const TargetRegisterClass *firstCommonClass(const uint32_t *A,
  247. const uint32_t *B,
  248. const TargetRegisterInfo *TRI) {
  249. for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
  250. if (unsigned Common = *A++ & *B++)
  251. return TRI->getRegClass(I + countTrailingZeros(Common));
  252. return nullptr;
  253. }
  254. const TargetRegisterClass *
  255. TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
  256. const TargetRegisterClass *B) const {
  257. // First take care of the trivial cases.
  258. if (A == B)
  259. return A;
  260. if (!A || !B)
  261. return nullptr;
  262. // Register classes are ordered topologically, so the largest common
  263. // sub-class it the common sub-class with the smallest ID.
  264. return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this);
  265. }
  266. const TargetRegisterClass *
  267. TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
  268. const TargetRegisterClass *B,
  269. unsigned Idx) const {
  270. assert(A && B && "Missing register class");
  271. assert(Idx && "Bad sub-register index");
  272. // Find Idx in the list of super-register indices.
  273. for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
  274. if (RCI.getSubReg() == Idx)
  275. // The bit mask contains all register classes that are projected into B
  276. // by Idx. Find a class that is also a sub-class of A.
  277. return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this);
  278. return nullptr;
  279. }
  280. const TargetRegisterClass *TargetRegisterInfo::
  281. getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
  282. const TargetRegisterClass *RCB, unsigned SubB,
  283. unsigned &PreA, unsigned &PreB) const {
  284. assert(RCA && SubA && RCB && SubB && "Invalid arguments");
  285. // Search all pairs of sub-register indices that project into RCA and RCB
  286. // respectively. This is quadratic, but usually the sets are very small. On
  287. // most targets like X86, there will only be a single sub-register index
  288. // (e.g., sub_16bit projecting into GR16).
  289. //
  290. // The worst case is a register class like DPR on ARM.
  291. // We have indices dsub_0..dsub_7 projecting into that class.
  292. //
  293. // It is very common that one register class is a sub-register of the other.
  294. // Arrange for RCA to be the larger register so the answer will be found in
  295. // the first iteration. This makes the search linear for the most common
  296. // case.
  297. const TargetRegisterClass *BestRC = nullptr;
  298. unsigned *BestPreA = &PreA;
  299. unsigned *BestPreB = &PreB;
  300. if (getRegSizeInBits(*RCA) < getRegSizeInBits(*RCB)) {
  301. std::swap(RCA, RCB);
  302. std::swap(SubA, SubB);
  303. std::swap(BestPreA, BestPreB);
  304. }
  305. // Also terminate the search one we have found a register class as small as
  306. // RCA.
  307. unsigned MinSize = getRegSizeInBits(*RCA);
  308. for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
  309. unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
  310. for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
  311. // Check if a common super-register class exists for this index pair.
  312. const TargetRegisterClass *RC =
  313. firstCommonClass(IA.getMask(), IB.getMask(), this);
  314. if (!RC || getRegSizeInBits(*RC) < MinSize)
  315. continue;
  316. // The indexes must compose identically: PreA+SubA == PreB+SubB.
  317. unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
  318. if (FinalA != FinalB)
  319. continue;
  320. // Is RC a better candidate than BestRC?
  321. if (BestRC && getRegSizeInBits(*RC) >= getRegSizeInBits(*BestRC))
  322. continue;
  323. // Yes, RC is the smallest super-register seen so far.
  324. BestRC = RC;
  325. *BestPreA = IA.getSubReg();
  326. *BestPreB = IB.getSubReg();
  327. // Bail early if we reached MinSize. We won't find a better candidate.
  328. if (getRegSizeInBits(*BestRC) == MinSize)
  329. return BestRC;
  330. }
  331. }
  332. return BestRC;
  333. }
  334. /// Check if the registers defined by the pair (RegisterClass, SubReg)
  335. /// share the same register file.
  336. static bool shareSameRegisterFile(const TargetRegisterInfo &TRI,
  337. const TargetRegisterClass *DefRC,
  338. unsigned DefSubReg,
  339. const TargetRegisterClass *SrcRC,
  340. unsigned SrcSubReg) {
  341. // Same register class.
  342. if (DefRC == SrcRC)
  343. return true;
  344. // Both operands are sub registers. Check if they share a register class.
  345. unsigned SrcIdx, DefIdx;
  346. if (SrcSubReg && DefSubReg) {
  347. return TRI.getCommonSuperRegClass(SrcRC, SrcSubReg, DefRC, DefSubReg,
  348. SrcIdx, DefIdx) != nullptr;
  349. }
  350. // At most one of the register is a sub register, make it Src to avoid
  351. // duplicating the test.
  352. if (!SrcSubReg) {
  353. std::swap(DefSubReg, SrcSubReg);
  354. std::swap(DefRC, SrcRC);
  355. }
  356. // One of the register is a sub register, check if we can get a superclass.
  357. if (SrcSubReg)
  358. return TRI.getMatchingSuperRegClass(SrcRC, DefRC, SrcSubReg) != nullptr;
  359. // Plain copy.
  360. return TRI.getCommonSubClass(DefRC, SrcRC) != nullptr;
  361. }
  362. bool TargetRegisterInfo::shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
  363. unsigned DefSubReg,
  364. const TargetRegisterClass *SrcRC,
  365. unsigned SrcSubReg) const {
  366. // If this source does not incur a cross register bank copy, use it.
  367. return shareSameRegisterFile(*this, DefRC, DefSubReg, SrcRC, SrcSubReg);
  368. }
  369. // Compute target-independent register allocator hints to help eliminate copies.
  370. bool TargetRegisterInfo::getRegAllocationHints(
  371. Register VirtReg, ArrayRef<MCPhysReg> Order,
  372. SmallVectorImpl<MCPhysReg> &Hints, const MachineFunction &MF,
  373. const VirtRegMap *VRM, const LiveRegMatrix *Matrix) const {
  374. const MachineRegisterInfo &MRI = MF.getRegInfo();
  375. const std::pair<Register, SmallVector<Register, 4>> &Hints_MRI =
  376. MRI.getRegAllocationHints(VirtReg);
  377. SmallSet<Register, 32> HintedRegs;
  378. // First hint may be a target hint.
  379. bool Skip = (Hints_MRI.first != 0);
  380. for (auto Reg : Hints_MRI.second) {
  381. if (Skip) {
  382. Skip = false;
  383. continue;
  384. }
  385. // Target-independent hints are either a physical or a virtual register.
  386. Register Phys = Reg;
  387. if (VRM && Phys.isVirtual())
  388. Phys = VRM->getPhys(Phys);
  389. // Don't add the same reg twice (Hints_MRI may contain multiple virtual
  390. // registers allocated to the same physreg).
  391. if (!HintedRegs.insert(Phys).second)
  392. continue;
  393. // Check that Phys is a valid hint in VirtReg's register class.
  394. if (!Phys.isPhysical())
  395. continue;
  396. if (MRI.isReserved(Phys))
  397. continue;
  398. // Check that Phys is in the allocation order. We shouldn't heed hints
  399. // from VirtReg's register class if they aren't in the allocation order. The
  400. // target probably has a reason for removing the register.
  401. if (!is_contained(Order, Phys))
  402. continue;
  403. // All clear, tell the register allocator to prefer this register.
  404. Hints.push_back(Phys);
  405. }
  406. return false;
  407. }
  408. bool TargetRegisterInfo::isCalleeSavedPhysReg(
  409. MCRegister PhysReg, const MachineFunction &MF) const {
  410. if (PhysReg == 0)
  411. return false;
  412. const uint32_t *callerPreservedRegs =
  413. getCallPreservedMask(MF, MF.getFunction().getCallingConv());
  414. if (callerPreservedRegs) {
  415. assert(Register::isPhysicalRegister(PhysReg) &&
  416. "Expected physical register");
  417. return (callerPreservedRegs[PhysReg / 32] >> PhysReg % 32) & 1;
  418. }
  419. return false;
  420. }
  421. bool TargetRegisterInfo::canRealignStack(const MachineFunction &MF) const {
  422. return !MF.getFunction().hasFnAttribute("no-realign-stack");
  423. }
  424. bool TargetRegisterInfo::shouldRealignStack(const MachineFunction &MF) const {
  425. const MachineFrameInfo &MFI = MF.getFrameInfo();
  426. const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
  427. const Function &F = MF.getFunction();
  428. return F.hasFnAttribute("stackrealign") ||
  429. (MFI.getMaxAlign() > TFI->getStackAlign()) ||
  430. F.hasFnAttribute(Attribute::StackAlignment);
  431. }
  432. bool TargetRegisterInfo::regmaskSubsetEqual(const uint32_t *mask0,
  433. const uint32_t *mask1) const {
  434. unsigned N = (getNumRegs()+31) / 32;
  435. for (unsigned I = 0; I < N; ++I)
  436. if ((mask0[I] & mask1[I]) != mask0[I])
  437. return false;
  438. return true;
  439. }
  440. unsigned
  441. TargetRegisterInfo::getRegSizeInBits(Register Reg,
  442. const MachineRegisterInfo &MRI) const {
  443. const TargetRegisterClass *RC{};
  444. if (Reg.isPhysical()) {
  445. // The size is not directly available for physical registers.
  446. // Instead, we need to access a register class that contains Reg and
  447. // get the size of that register class.
  448. RC = getMinimalPhysRegClass(Reg);
  449. } else {
  450. LLT Ty = MRI.getType(Reg);
  451. unsigned RegSize = Ty.isValid() ? Ty.getSizeInBits() : 0;
  452. // If Reg is not a generic register, query the register class to
  453. // get its size.
  454. if (RegSize)
  455. return RegSize;
  456. // Since Reg is not a generic register, it must have a register class.
  457. RC = MRI.getRegClass(Reg);
  458. }
  459. assert(RC && "Unable to deduce the register class");
  460. return getRegSizeInBits(*RC);
  461. }
  462. bool TargetRegisterInfo::getCoveringSubRegIndexes(
  463. const MachineRegisterInfo &MRI, const TargetRegisterClass *RC,
  464. LaneBitmask LaneMask, SmallVectorImpl<unsigned> &NeededIndexes) const {
  465. SmallVector<unsigned, 8> PossibleIndexes;
  466. unsigned BestIdx = 0;
  467. unsigned BestCover = 0;
  468. for (unsigned Idx = 1, E = getNumSubRegIndices(); Idx < E; ++Idx) {
  469. // Is this index even compatible with the given class?
  470. if (getSubClassWithSubReg(RC, Idx) != RC)
  471. continue;
  472. LaneBitmask SubRegMask = getSubRegIndexLaneMask(Idx);
  473. // Early exit if we found a perfect match.
  474. if (SubRegMask == LaneMask) {
  475. BestIdx = Idx;
  476. break;
  477. }
  478. // The index must not cover any lanes outside \p LaneMask.
  479. if ((SubRegMask & ~LaneMask).any())
  480. continue;
  481. unsigned PopCount = SubRegMask.getNumLanes();
  482. PossibleIndexes.push_back(Idx);
  483. if (PopCount > BestCover) {
  484. BestCover = PopCount;
  485. BestIdx = Idx;
  486. }
  487. }
  488. // Abort if we cannot possibly implement the COPY with the given indexes.
  489. if (BestIdx == 0)
  490. return false;
  491. NeededIndexes.push_back(BestIdx);
  492. // Greedy heuristic: Keep iterating keeping the best covering subreg index
  493. // each time.
  494. LaneBitmask LanesLeft = LaneMask & ~getSubRegIndexLaneMask(BestIdx);
  495. while (LanesLeft.any()) {
  496. unsigned BestIdx = 0;
  497. int BestCover = std::numeric_limits<int>::min();
  498. for (unsigned Idx : PossibleIndexes) {
  499. LaneBitmask SubRegMask = getSubRegIndexLaneMask(Idx);
  500. // Early exit if we found a perfect match.
  501. if (SubRegMask == LanesLeft) {
  502. BestIdx = Idx;
  503. break;
  504. }
  505. // Try to cover as much of the remaining lanes as possible but
  506. // as few of the already covered lanes as possible.
  507. int Cover = (SubRegMask & LanesLeft).getNumLanes() -
  508. (SubRegMask & ~LanesLeft).getNumLanes();
  509. if (Cover > BestCover) {
  510. BestCover = Cover;
  511. BestIdx = Idx;
  512. }
  513. }
  514. if (BestIdx == 0)
  515. return false; // Impossible to handle
  516. NeededIndexes.push_back(BestIdx);
  517. LanesLeft &= ~getSubRegIndexLaneMask(BestIdx);
  518. }
  519. return BestIdx;
  520. }
  521. Register
  522. TargetRegisterInfo::lookThruCopyLike(Register SrcReg,
  523. const MachineRegisterInfo *MRI) const {
  524. while (true) {
  525. const MachineInstr *MI = MRI->getVRegDef(SrcReg);
  526. if (!MI->isCopyLike())
  527. return SrcReg;
  528. Register CopySrcReg;
  529. if (MI->isCopy())
  530. CopySrcReg = MI->getOperand(1).getReg();
  531. else {
  532. assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
  533. CopySrcReg = MI->getOperand(2).getReg();
  534. }
  535. if (!CopySrcReg.isVirtual())
  536. return CopySrcReg;
  537. SrcReg = CopySrcReg;
  538. }
  539. }
  540. Register TargetRegisterInfo::lookThruSingleUseCopyChain(
  541. Register SrcReg, const MachineRegisterInfo *MRI) const {
  542. while (true) {
  543. const MachineInstr *MI = MRI->getVRegDef(SrcReg);
  544. // Found the real definition, return it if it has a single use.
  545. if (!MI->isCopyLike())
  546. return MRI->hasOneNonDBGUse(SrcReg) ? SrcReg : Register();
  547. Register CopySrcReg;
  548. if (MI->isCopy())
  549. CopySrcReg = MI->getOperand(1).getReg();
  550. else {
  551. assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
  552. CopySrcReg = MI->getOperand(2).getReg();
  553. }
  554. // Continue only if the next definition in the chain is for a virtual
  555. // register that has a single use.
  556. if (!CopySrcReg.isVirtual() || !MRI->hasOneNonDBGUse(CopySrcReg))
  557. return Register();
  558. SrcReg = CopySrcReg;
  559. }
  560. }
  561. void TargetRegisterInfo::getOffsetOpcodes(
  562. const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const {
  563. assert(!Offset.getScalable() && "Scalable offsets are not handled");
  564. DIExpression::appendOffset(Ops, Offset.getFixed());
  565. }
  566. DIExpression *
  567. TargetRegisterInfo::prependOffsetExpression(const DIExpression *Expr,
  568. unsigned PrependFlags,
  569. const StackOffset &Offset) const {
  570. assert((PrependFlags &
  571. ~(DIExpression::DerefBefore | DIExpression::DerefAfter |
  572. DIExpression::StackValue | DIExpression::EntryValue)) == 0 &&
  573. "Unsupported prepend flag");
  574. SmallVector<uint64_t, 16> OffsetExpr;
  575. if (PrependFlags & DIExpression::DerefBefore)
  576. OffsetExpr.push_back(dwarf::DW_OP_deref);
  577. getOffsetOpcodes(Offset, OffsetExpr);
  578. if (PrependFlags & DIExpression::DerefAfter)
  579. OffsetExpr.push_back(dwarf::DW_OP_deref);
  580. return DIExpression::prependOpcodes(Expr, OffsetExpr,
  581. PrependFlags & DIExpression::StackValue,
  582. PrependFlags & DIExpression::EntryValue);
  583. }
  584. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  585. LLVM_DUMP_METHOD
  586. void TargetRegisterInfo::dumpReg(Register Reg, unsigned SubRegIndex,
  587. const TargetRegisterInfo *TRI) {
  588. dbgs() << printReg(Reg, TRI, SubRegIndex) << "\n";
  589. }
  590. #endif