TargetRegisterInfo.h 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- CodeGen/TargetRegisterInfo.h - Target Register Information -*- C++ -*-==//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file describes an abstract interface used to get information about a
  15. // target machines register file. This information is used for a variety of
  16. // purposed, especially register allocation.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CODEGEN_TARGETREGISTERINFO_H
  20. #define LLVM_CODEGEN_TARGETREGISTERINFO_H
  21. #include "llvm/ADT/ArrayRef.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/ADT/iterator_range.h"
  25. #include "llvm/CodeGen/MachineBasicBlock.h"
  26. #include "llvm/IR/CallingConv.h"
  27. #include "llvm/MC/LaneBitmask.h"
  28. #include "llvm/MC/MCRegisterInfo.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/MachineValueType.h"
  31. #include "llvm/Support/MathExtras.h"
  32. #include "llvm/Support/Printable.h"
  33. #include <cassert>
  34. #include <cstdint>
  35. namespace llvm {
  36. class BitVector;
  37. class DIExpression;
  38. class LiveRegMatrix;
  39. class MachineFunction;
  40. class MachineInstr;
  41. class RegScavenger;
  42. class VirtRegMap;
  43. class LiveIntervals;
  44. class LiveInterval;
  45. class TargetRegisterClass {
  46. public:
  47. using iterator = const MCPhysReg *;
  48. using const_iterator = const MCPhysReg *;
  49. using sc_iterator = const TargetRegisterClass* const *;
  50. // Instance variables filled by tablegen, do not use!
  51. const MCRegisterClass *MC;
  52. const uint32_t *SubClassMask;
  53. const uint16_t *SuperRegIndices;
  54. const LaneBitmask LaneMask;
  55. /// Classes with a higher priority value are assigned first by register
  56. /// allocators using a greedy heuristic. The value is in the range [0,31].
  57. const uint8_t AllocationPriority;
  58. // Change allocation priority heuristic used by greedy.
  59. const bool GlobalPriority;
  60. /// Configurable target specific flags.
  61. const uint8_t TSFlags;
  62. /// Whether the class supports two (or more) disjunct subregister indices.
  63. const bool HasDisjunctSubRegs;
  64. /// Whether a combination of subregisters can cover every register in the
  65. /// class. See also the CoveredBySubRegs description in Target.td.
  66. const bool CoveredBySubRegs;
  67. const sc_iterator SuperClasses;
  68. ArrayRef<MCPhysReg> (*OrderFunc)(const MachineFunction&);
  69. /// Return the register class ID number.
  70. unsigned getID() const { return MC->getID(); }
  71. /// begin/end - Return all of the registers in this class.
  72. ///
  73. iterator begin() const { return MC->begin(); }
  74. iterator end() const { return MC->end(); }
  75. /// Return the number of registers in this class.
  76. unsigned getNumRegs() const { return MC->getNumRegs(); }
  77. iterator_range<SmallVectorImpl<MCPhysReg>::const_iterator>
  78. getRegisters() const {
  79. return make_range(MC->begin(), MC->end());
  80. }
  81. /// Return the specified register in the class.
  82. MCRegister getRegister(unsigned i) const {
  83. return MC->getRegister(i);
  84. }
  85. /// Return true if the specified register is included in this register class.
  86. /// This does not include virtual registers.
  87. bool contains(Register Reg) const {
  88. /// FIXME: Historically this function has returned false when given vregs
  89. /// but it should probably only receive physical registers
  90. if (!Reg.isPhysical())
  91. return false;
  92. return MC->contains(Reg.asMCReg());
  93. }
  94. /// Return true if both registers are in this class.
  95. bool contains(Register Reg1, Register Reg2) const {
  96. /// FIXME: Historically this function has returned false when given a vregs
  97. /// but it should probably only receive physical registers
  98. if (!Reg1.isPhysical() || !Reg2.isPhysical())
  99. return false;
  100. return MC->contains(Reg1.asMCReg(), Reg2.asMCReg());
  101. }
  102. /// Return the cost of copying a value between two registers in this class.
  103. /// A negative number means the register class is very expensive
  104. /// to copy e.g. status flag register classes.
  105. int getCopyCost() const { return MC->getCopyCost(); }
  106. /// Return true if this register class may be used to create virtual
  107. /// registers.
  108. bool isAllocatable() const { return MC->isAllocatable(); }
  109. /// Return true if the specified TargetRegisterClass
  110. /// is a proper sub-class of this TargetRegisterClass.
  111. bool hasSubClass(const TargetRegisterClass *RC) const {
  112. return RC != this && hasSubClassEq(RC);
  113. }
  114. /// Returns true if RC is a sub-class of or equal to this class.
  115. bool hasSubClassEq(const TargetRegisterClass *RC) const {
  116. unsigned ID = RC->getID();
  117. return (SubClassMask[ID / 32] >> (ID % 32)) & 1;
  118. }
  119. /// Return true if the specified TargetRegisterClass is a
  120. /// proper super-class of this TargetRegisterClass.
  121. bool hasSuperClass(const TargetRegisterClass *RC) const {
  122. return RC->hasSubClass(this);
  123. }
  124. /// Returns true if RC is a super-class of or equal to this class.
  125. bool hasSuperClassEq(const TargetRegisterClass *RC) const {
  126. return RC->hasSubClassEq(this);
  127. }
  128. /// Returns a bit vector of subclasses, including this one.
  129. /// The vector is indexed by class IDs.
  130. ///
  131. /// To use it, consider the returned array as a chunk of memory that
  132. /// contains an array of bits of size NumRegClasses. Each 32-bit chunk
  133. /// contains a bitset of the ID of the subclasses in big-endian style.
  134. /// I.e., the representation of the memory from left to right at the
  135. /// bit level looks like:
  136. /// [31 30 ... 1 0] [ 63 62 ... 33 32] ...
  137. /// [ XXX NumRegClasses NumRegClasses - 1 ... ]
  138. /// Where the number represents the class ID and XXX bits that
  139. /// should be ignored.
  140. ///
  141. /// See the implementation of hasSubClassEq for an example of how it
  142. /// can be used.
  143. const uint32_t *getSubClassMask() const {
  144. return SubClassMask;
  145. }
  146. /// Returns a 0-terminated list of sub-register indices that project some
  147. /// super-register class into this register class. The list has an entry for
  148. /// each Idx such that:
  149. ///
  150. /// There exists SuperRC where:
  151. /// For all Reg in SuperRC:
  152. /// this->contains(Reg:Idx)
  153. const uint16_t *getSuperRegIndices() const {
  154. return SuperRegIndices;
  155. }
  156. /// Returns a NULL-terminated list of super-classes. The
  157. /// classes are ordered by ID which is also a topological ordering from large
  158. /// to small classes. The list does NOT include the current class.
  159. sc_iterator getSuperClasses() const {
  160. return SuperClasses;
  161. }
  162. /// Return true if this TargetRegisterClass is a subset
  163. /// class of at least one other TargetRegisterClass.
  164. bool isASubClass() const {
  165. return SuperClasses[0] != nullptr;
  166. }
  167. /// Returns the preferred order for allocating registers from this register
  168. /// class in MF. The raw order comes directly from the .td file and may
  169. /// include reserved registers that are not allocatable.
  170. /// Register allocators should also make sure to allocate
  171. /// callee-saved registers only after all the volatiles are used. The
  172. /// RegisterClassInfo class provides filtered allocation orders with
  173. /// callee-saved registers moved to the end.
  174. ///
  175. /// The MachineFunction argument can be used to tune the allocatable
  176. /// registers based on the characteristics of the function, subtarget, or
  177. /// other criteria.
  178. ///
  179. /// By default, this method returns all registers in the class.
  180. ArrayRef<MCPhysReg> getRawAllocationOrder(const MachineFunction &MF) const {
  181. return OrderFunc ? OrderFunc(MF) : ArrayRef(begin(), getNumRegs());
  182. }
  183. /// Returns the combination of all lane masks of register in this class.
  184. /// The lane masks of the registers are the combination of all lane masks
  185. /// of their subregisters. Returns 1 if there are no subregisters.
  186. LaneBitmask getLaneMask() const {
  187. return LaneMask;
  188. }
  189. };
  190. /// Extra information, not in MCRegisterDesc, about registers.
  191. /// These are used by codegen, not by MC.
  192. struct TargetRegisterInfoDesc {
  193. const uint8_t *CostPerUse; // Extra cost of instructions using register.
  194. unsigned NumCosts; // Number of cost values associated with each register.
  195. const bool
  196. *InAllocatableClass; // Register belongs to an allocatable regclass.
  197. };
  198. /// Each TargetRegisterClass has a per register weight, and weight
  199. /// limit which must be less than the limits of its pressure sets.
  200. struct RegClassWeight {
  201. unsigned RegWeight;
  202. unsigned WeightLimit;
  203. };
  204. /// TargetRegisterInfo base class - We assume that the target defines a static
  205. /// array of TargetRegisterDesc objects that represent all of the machine
  206. /// registers that the target has. As such, we simply have to track a pointer
  207. /// to this array so that we can turn register number into a register
  208. /// descriptor.
  209. ///
  210. class TargetRegisterInfo : public MCRegisterInfo {
  211. public:
  212. using regclass_iterator = const TargetRegisterClass * const *;
  213. using vt_iterator = const MVT::SimpleValueType *;
  214. struct RegClassInfo {
  215. unsigned RegSize, SpillSize, SpillAlignment;
  216. vt_iterator VTList;
  217. };
  218. private:
  219. const TargetRegisterInfoDesc *InfoDesc; // Extra desc array for codegen
  220. const char *const *SubRegIndexNames; // Names of subreg indexes.
  221. // Pointer to array of lane masks, one per sub-reg index.
  222. const LaneBitmask *SubRegIndexLaneMasks;
  223. regclass_iterator RegClassBegin, RegClassEnd; // List of regclasses
  224. LaneBitmask CoveringLanes;
  225. const RegClassInfo *const RCInfos;
  226. unsigned HwMode;
  227. protected:
  228. TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
  229. regclass_iterator RCB,
  230. regclass_iterator RCE,
  231. const char *const *SRINames,
  232. const LaneBitmask *SRILaneMasks,
  233. LaneBitmask CoveringLanes,
  234. const RegClassInfo *const RCIs,
  235. unsigned Mode = 0);
  236. virtual ~TargetRegisterInfo();
  237. public:
  238. // Register numbers can represent physical registers, virtual registers, and
  239. // sometimes stack slots. The unsigned values are divided into these ranges:
  240. //
  241. // 0 Not a register, can be used as a sentinel.
  242. // [1;2^30) Physical registers assigned by TableGen.
  243. // [2^30;2^31) Stack slots. (Rarely used.)
  244. // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
  245. //
  246. // Further sentinels can be allocated from the small negative integers.
  247. // DenseMapInfo<unsigned> uses -1u and -2u.
  248. /// Return the size in bits of a register from class RC.
  249. unsigned getRegSizeInBits(const TargetRegisterClass &RC) const {
  250. return getRegClassInfo(RC).RegSize;
  251. }
  252. /// Return the size in bytes of the stack slot allocated to hold a spilled
  253. /// copy of a register from class RC.
  254. unsigned getSpillSize(const TargetRegisterClass &RC) const {
  255. return getRegClassInfo(RC).SpillSize / 8;
  256. }
  257. /// Return the minimum required alignment in bytes for a spill slot for
  258. /// a register of this class.
  259. Align getSpillAlign(const TargetRegisterClass &RC) const {
  260. return Align(getRegClassInfo(RC).SpillAlignment / 8);
  261. }
  262. /// Return true if the given TargetRegisterClass has the ValueType T.
  263. bool isTypeLegalForClass(const TargetRegisterClass &RC, MVT T) const {
  264. for (auto I = legalclasstypes_begin(RC); *I != MVT::Other; ++I)
  265. if (MVT(*I) == T)
  266. return true;
  267. return false;
  268. }
  269. /// Return true if the given TargetRegisterClass is compatible with LLT T.
  270. bool isTypeLegalForClass(const TargetRegisterClass &RC, LLT T) const {
  271. for (auto I = legalclasstypes_begin(RC); *I != MVT::Other; ++I) {
  272. MVT VT(*I);
  273. if (VT == MVT::Untyped)
  274. return true;
  275. if (LLT(VT) == T)
  276. return true;
  277. }
  278. return false;
  279. }
  280. /// Loop over all of the value types that can be represented by values
  281. /// in the given register class.
  282. vt_iterator legalclasstypes_begin(const TargetRegisterClass &RC) const {
  283. return getRegClassInfo(RC).VTList;
  284. }
  285. vt_iterator legalclasstypes_end(const TargetRegisterClass &RC) const {
  286. vt_iterator I = legalclasstypes_begin(RC);
  287. while (*I != MVT::Other)
  288. ++I;
  289. return I;
  290. }
  291. /// Returns the Register Class of a physical register of the given type,
  292. /// picking the most sub register class of the right type that contains this
  293. /// physreg.
  294. const TargetRegisterClass *getMinimalPhysRegClass(MCRegister Reg,
  295. MVT VT = MVT::Other) const;
  296. /// Returns the Register Class of a physical register of the given type,
  297. /// picking the most sub register class of the right type that contains this
  298. /// physreg. If there is no register class compatible with the given type,
  299. /// returns nullptr.
  300. const TargetRegisterClass *getMinimalPhysRegClassLLT(MCRegister Reg,
  301. LLT Ty = LLT()) const;
  302. /// Return the maximal subclass of the given register class that is
  303. /// allocatable or NULL.
  304. const TargetRegisterClass *
  305. getAllocatableClass(const TargetRegisterClass *RC) const;
  306. /// Returns a bitset indexed by register number indicating if a register is
  307. /// allocatable or not. If a register class is specified, returns the subset
  308. /// for the class.
  309. BitVector getAllocatableSet(const MachineFunction &MF,
  310. const TargetRegisterClass *RC = nullptr) const;
  311. /// Get a list of cost values for all registers that correspond to the index
  312. /// returned by RegisterCostTableIndex.
  313. ArrayRef<uint8_t> getRegisterCosts(const MachineFunction &MF) const {
  314. unsigned Idx = getRegisterCostTableIndex(MF);
  315. unsigned NumRegs = getNumRegs();
  316. assert(Idx < InfoDesc->NumCosts && "CostPerUse index out of bounds");
  317. return ArrayRef(&InfoDesc->CostPerUse[Idx * NumRegs], NumRegs);
  318. }
  319. /// Return true if the register is in the allocation of any register class.
  320. bool isInAllocatableClass(MCRegister RegNo) const {
  321. return InfoDesc->InAllocatableClass[RegNo];
  322. }
  323. /// Return the human-readable symbolic target-specific
  324. /// name for the specified SubRegIndex.
  325. const char *getSubRegIndexName(unsigned SubIdx) const {
  326. assert(SubIdx && SubIdx < getNumSubRegIndices() &&
  327. "This is not a subregister index");
  328. return SubRegIndexNames[SubIdx-1];
  329. }
  330. /// Return a bitmask representing the parts of a register that are covered by
  331. /// SubIdx \see LaneBitmask.
  332. ///
  333. /// SubIdx == 0 is allowed, it has the lane mask ~0u.
  334. LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const {
  335. assert(SubIdx < getNumSubRegIndices() && "This is not a subregister index");
  336. return SubRegIndexLaneMasks[SubIdx];
  337. }
  338. /// Try to find one or more subregister indexes to cover \p LaneMask.
  339. ///
  340. /// If this is possible, returns true and appends the best matching set of
  341. /// indexes to \p Indexes. If this is not possible, returns false.
  342. bool getCoveringSubRegIndexes(const MachineRegisterInfo &MRI,
  343. const TargetRegisterClass *RC,
  344. LaneBitmask LaneMask,
  345. SmallVectorImpl<unsigned> &Indexes) const;
  346. /// The lane masks returned by getSubRegIndexLaneMask() above can only be
  347. /// used to determine if sub-registers overlap - they can't be used to
  348. /// determine if a set of sub-registers completely cover another
  349. /// sub-register.
  350. ///
  351. /// The X86 general purpose registers have two lanes corresponding to the
  352. /// sub_8bit and sub_8bit_hi sub-registers. Both sub_32bit and sub_16bit have
  353. /// lane masks '3', but the sub_16bit sub-register doesn't fully cover the
  354. /// sub_32bit sub-register.
  355. ///
  356. /// On the other hand, the ARM NEON lanes fully cover their registers: The
  357. /// dsub_0 sub-register is completely covered by the ssub_0 and ssub_1 lanes.
  358. /// This is related to the CoveredBySubRegs property on register definitions.
  359. ///
  360. /// This function returns a bit mask of lanes that completely cover their
  361. /// sub-registers. More precisely, given:
  362. ///
  363. /// Covering = getCoveringLanes();
  364. /// MaskA = getSubRegIndexLaneMask(SubA);
  365. /// MaskB = getSubRegIndexLaneMask(SubB);
  366. ///
  367. /// If (MaskA & ~(MaskB & Covering)) == 0, then SubA is completely covered by
  368. /// SubB.
  369. LaneBitmask getCoveringLanes() const { return CoveringLanes; }
  370. /// Returns true if the two registers are equal or alias each other.
  371. /// The registers may be virtual registers.
  372. bool regsOverlap(Register RegA, Register RegB) const {
  373. if (RegA == RegB)
  374. return true;
  375. if (RegA.isPhysical() && RegB.isPhysical())
  376. return MCRegisterInfo::regsOverlap(RegA.asMCReg(), RegB.asMCReg());
  377. return false;
  378. }
  379. /// Returns true if Reg contains RegUnit.
  380. bool hasRegUnit(MCRegister Reg, Register RegUnit) const {
  381. for (MCRegUnitIterator Units(Reg, this); Units.isValid(); ++Units)
  382. if (Register(*Units) == RegUnit)
  383. return true;
  384. return false;
  385. }
  386. /// Returns the original SrcReg unless it is the target of a copy-like
  387. /// operation, in which case we chain backwards through all such operations
  388. /// to the ultimate source register. If a physical register is encountered,
  389. /// we stop the search.
  390. virtual Register lookThruCopyLike(Register SrcReg,
  391. const MachineRegisterInfo *MRI) const;
  392. /// Find the original SrcReg unless it is the target of a copy-like operation,
  393. /// in which case we chain backwards through all such operations to the
  394. /// ultimate source register. If a physical register is encountered, we stop
  395. /// the search.
  396. /// Return the original SrcReg if all the definitions in the chain only have
  397. /// one user and not a physical register.
  398. virtual Register
  399. lookThruSingleUseCopyChain(Register SrcReg,
  400. const MachineRegisterInfo *MRI) const;
  401. /// Return a null-terminated list of all of the callee-saved registers on
  402. /// this target. The register should be in the order of desired callee-save
  403. /// stack frame offset. The first register is closest to the incoming stack
  404. /// pointer if stack grows down, and vice versa.
  405. /// Notice: This function does not take into account disabled CSRs.
  406. /// In most cases you will want to use instead the function
  407. /// getCalleeSavedRegs that is implemented in MachineRegisterInfo.
  408. virtual const MCPhysReg*
  409. getCalleeSavedRegs(const MachineFunction *MF) const = 0;
  410. /// Return a mask of call-preserved registers for the given calling convention
  411. /// on the current function. The mask should include all call-preserved
  412. /// aliases. This is used by the register allocator to determine which
  413. /// registers can be live across a call.
  414. ///
  415. /// The mask is an array containing (TRI::getNumRegs()+31)/32 entries.
  416. /// A set bit indicates that all bits of the corresponding register are
  417. /// preserved across the function call. The bit mask is expected to be
  418. /// sub-register complete, i.e. if A is preserved, so are all its
  419. /// sub-registers.
  420. ///
  421. /// Bits are numbered from the LSB, so the bit for physical register Reg can
  422. /// be found as (Mask[Reg / 32] >> Reg % 32) & 1.
  423. ///
  424. /// A NULL pointer means that no register mask will be used, and call
  425. /// instructions should use implicit-def operands to indicate call clobbered
  426. /// registers.
  427. ///
  428. virtual const uint32_t *getCallPreservedMask(const MachineFunction &MF,
  429. CallingConv::ID) const {
  430. // The default mask clobbers everything. All targets should override.
  431. return nullptr;
  432. }
  433. /// Return a register mask for the registers preserved by the unwinder,
  434. /// or nullptr if no custom mask is needed.
  435. virtual const uint32_t *
  436. getCustomEHPadPreservedMask(const MachineFunction &MF) const {
  437. return nullptr;
  438. }
  439. /// Return a register mask that clobbers everything.
  440. virtual const uint32_t *getNoPreservedMask() const {
  441. llvm_unreachable("target does not provide no preserved mask");
  442. }
  443. /// Return a list of all of the registers which are clobbered "inside" a call
  444. /// to the given function. For example, these might be needed for PLT
  445. /// sequences of long-branch veneers.
  446. virtual ArrayRef<MCPhysReg>
  447. getIntraCallClobberedRegs(const MachineFunction *MF) const {
  448. return {};
  449. }
  450. /// Return true if all bits that are set in mask \p mask0 are also set in
  451. /// \p mask1.
  452. bool regmaskSubsetEqual(const uint32_t *mask0, const uint32_t *mask1) const;
  453. /// Return all the call-preserved register masks defined for this target.
  454. virtual ArrayRef<const uint32_t *> getRegMasks() const = 0;
  455. virtual ArrayRef<const char *> getRegMaskNames() const = 0;
  456. /// Returns a bitset indexed by physical register number indicating if a
  457. /// register is a special register that has particular uses and should be
  458. /// considered unavailable at all times, e.g. stack pointer, return address.
  459. /// A reserved register:
  460. /// - is not allocatable
  461. /// - is considered always live
  462. /// - is ignored by liveness tracking
  463. /// It is often necessary to reserve the super registers of a reserved
  464. /// register as well, to avoid them getting allocated indirectly. You may use
  465. /// markSuperRegs() and checkAllSuperRegsMarked() in this case.
  466. virtual BitVector getReservedRegs(const MachineFunction &MF) const = 0;
  467. /// Returns either a string explaining why the given register is reserved for
  468. /// this function, or an empty optional if no explanation has been written.
  469. /// The absence of an explanation does not mean that the register is not
  470. /// reserved (meaning, you should check that PhysReg is in fact reserved
  471. /// before calling this).
  472. virtual std::optional<std::string>
  473. explainReservedReg(const MachineFunction &MF, MCRegister PhysReg) const {
  474. return {};
  475. }
  476. /// Returns false if we can't guarantee that Physreg, specified as an IR asm
  477. /// clobber constraint, will be preserved across the statement.
  478. virtual bool isAsmClobberable(const MachineFunction &MF,
  479. MCRegister PhysReg) const {
  480. return true;
  481. }
  482. /// Returns true if PhysReg cannot be written to in inline asm statements.
  483. virtual bool isInlineAsmReadOnlyReg(const MachineFunction &MF,
  484. unsigned PhysReg) const {
  485. return false;
  486. }
  487. /// Returns true if PhysReg is unallocatable and constant throughout the
  488. /// function. Used by MachineRegisterInfo::isConstantPhysReg().
  489. virtual bool isConstantPhysReg(MCRegister PhysReg) const { return false; }
  490. /// Returns true if the register class is considered divergent.
  491. virtual bool isDivergentRegClass(const TargetRegisterClass *RC) const {
  492. return false;
  493. }
  494. /// Physical registers that may be modified within a function but are
  495. /// guaranteed to be restored before any uses. This is useful for targets that
  496. /// have call sequences where a GOT register may be updated by the caller
  497. /// prior to a call and is guaranteed to be restored (also by the caller)
  498. /// after the call.
  499. virtual bool isCallerPreservedPhysReg(MCRegister PhysReg,
  500. const MachineFunction &MF) const {
  501. return false;
  502. }
  503. /// This is a wrapper around getCallPreservedMask().
  504. /// Return true if the register is preserved after the call.
  505. virtual bool isCalleeSavedPhysReg(MCRegister PhysReg,
  506. const MachineFunction &MF) const;
  507. /// Returns true if PhysReg can be used as an argument to a function.
  508. virtual bool isArgumentRegister(const MachineFunction &MF,
  509. MCRegister PhysReg) const {
  510. return false;
  511. }
  512. /// Returns true if PhysReg is a fixed register.
  513. virtual bool isFixedRegister(const MachineFunction &MF,
  514. MCRegister PhysReg) const {
  515. return false;
  516. }
  517. /// Returns true if PhysReg is a general purpose register.
  518. virtual bool isGeneralPurposeRegister(const MachineFunction &MF,
  519. MCRegister PhysReg) const {
  520. return false;
  521. }
  522. /// Prior to adding the live-out mask to a stackmap or patchpoint
  523. /// instruction, provide the target the opportunity to adjust it (mainly to
  524. /// remove pseudo-registers that should be ignored).
  525. virtual void adjustStackMapLiveOutMask(uint32_t *Mask) const {}
  526. /// Return a super-register of the specified register
  527. /// Reg so its sub-register of index SubIdx is Reg.
  528. MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx,
  529. const TargetRegisterClass *RC) const {
  530. return MCRegisterInfo::getMatchingSuperReg(Reg, SubIdx, RC->MC);
  531. }
  532. /// Return a subclass of the specified register
  533. /// class A so that each register in it has a sub-register of the
  534. /// specified sub-register index which is in the specified register class B.
  535. ///
  536. /// TableGen will synthesize missing A sub-classes.
  537. virtual const TargetRegisterClass *
  538. getMatchingSuperRegClass(const TargetRegisterClass *A,
  539. const TargetRegisterClass *B, unsigned Idx) const;
  540. // For a copy-like instruction that defines a register of class DefRC with
  541. // subreg index DefSubReg, reading from another source with class SrcRC and
  542. // subregister SrcSubReg return true if this is a preferable copy
  543. // instruction or an earlier use should be used.
  544. virtual bool shouldRewriteCopySrc(const TargetRegisterClass *DefRC,
  545. unsigned DefSubReg,
  546. const TargetRegisterClass *SrcRC,
  547. unsigned SrcSubReg) const;
  548. /// Returns the largest legal sub-class of RC that
  549. /// supports the sub-register index Idx.
  550. /// If no such sub-class exists, return NULL.
  551. /// If all registers in RC already have an Idx sub-register, return RC.
  552. ///
  553. /// TableGen generates a version of this function that is good enough in most
  554. /// cases. Targets can override if they have constraints that TableGen
  555. /// doesn't understand. For example, the x86 sub_8bit sub-register index is
  556. /// supported by the full GR32 register class in 64-bit mode, but only by the
  557. /// GR32_ABCD regiister class in 32-bit mode.
  558. ///
  559. /// TableGen will synthesize missing RC sub-classes.
  560. virtual const TargetRegisterClass *
  561. getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const {
  562. assert(Idx == 0 && "Target has no sub-registers");
  563. return RC;
  564. }
  565. /// Return a register class that can be used for a subregister copy from/into
  566. /// \p SuperRC at \p SubRegIdx.
  567. virtual const TargetRegisterClass *
  568. getSubRegisterClass(const TargetRegisterClass *SuperRC,
  569. unsigned SubRegIdx) const {
  570. return nullptr;
  571. }
  572. /// Return the subregister index you get from composing
  573. /// two subregister indices.
  574. ///
  575. /// The special null sub-register index composes as the identity.
  576. ///
  577. /// If R:a:b is the same register as R:c, then composeSubRegIndices(a, b)
  578. /// returns c. Note that composeSubRegIndices does not tell you about illegal
  579. /// compositions. If R does not have a subreg a, or R:a does not have a subreg
  580. /// b, composeSubRegIndices doesn't tell you.
  581. ///
  582. /// The ARM register Q0 has two D subregs dsub_0:D0 and dsub_1:D1. It also has
  583. /// ssub_0:S0 - ssub_3:S3 subregs.
  584. /// If you compose subreg indices dsub_1, ssub_0 you get ssub_2.
  585. unsigned composeSubRegIndices(unsigned a, unsigned b) const {
  586. if (!a) return b;
  587. if (!b) return a;
  588. return composeSubRegIndicesImpl(a, b);
  589. }
  590. /// Transforms a LaneMask computed for one subregister to the lanemask that
  591. /// would have been computed when composing the subsubregisters with IdxA
  592. /// first. @sa composeSubRegIndices()
  593. LaneBitmask composeSubRegIndexLaneMask(unsigned IdxA,
  594. LaneBitmask Mask) const {
  595. if (!IdxA)
  596. return Mask;
  597. return composeSubRegIndexLaneMaskImpl(IdxA, Mask);
  598. }
  599. /// Transform a lanemask given for a virtual register to the corresponding
  600. /// lanemask before using subregister with index \p IdxA.
  601. /// This is the reverse of composeSubRegIndexLaneMask(), assuming Mask is a
  602. /// valie lane mask (no invalid bits set) the following holds:
  603. /// X0 = composeSubRegIndexLaneMask(Idx, Mask)
  604. /// X1 = reverseComposeSubRegIndexLaneMask(Idx, X0)
  605. /// => X1 == Mask
  606. LaneBitmask reverseComposeSubRegIndexLaneMask(unsigned IdxA,
  607. LaneBitmask LaneMask) const {
  608. if (!IdxA)
  609. return LaneMask;
  610. return reverseComposeSubRegIndexLaneMaskImpl(IdxA, LaneMask);
  611. }
  612. /// Debugging helper: dump register in human readable form to dbgs() stream.
  613. static void dumpReg(Register Reg, unsigned SubRegIndex = 0,
  614. const TargetRegisterInfo *TRI = nullptr);
  615. /// Return target defined base register class for a physical register.
  616. /// This is the register class with the lowest BaseClassOrder containing the
  617. /// register.
  618. /// Will be nullptr if the register is not in any base register class.
  619. virtual const TargetRegisterClass *getPhysRegBaseClass(MCRegister Reg) const {
  620. return nullptr;
  621. }
  622. protected:
  623. /// Overridden by TableGen in targets that have sub-registers.
  624. virtual unsigned composeSubRegIndicesImpl(unsigned, unsigned) const {
  625. llvm_unreachable("Target has no sub-registers");
  626. }
  627. /// Overridden by TableGen in targets that have sub-registers.
  628. virtual LaneBitmask
  629. composeSubRegIndexLaneMaskImpl(unsigned, LaneBitmask) const {
  630. llvm_unreachable("Target has no sub-registers");
  631. }
  632. virtual LaneBitmask reverseComposeSubRegIndexLaneMaskImpl(unsigned,
  633. LaneBitmask) const {
  634. llvm_unreachable("Target has no sub-registers");
  635. }
  636. /// Return the register cost table index. This implementation is sufficient
  637. /// for most architectures and can be overriden by targets in case there are
  638. /// multiple cost values associated with each register.
  639. virtual unsigned getRegisterCostTableIndex(const MachineFunction &MF) const {
  640. return 0;
  641. }
  642. public:
  643. /// Find a common super-register class if it exists.
  644. ///
  645. /// Find a register class, SuperRC and two sub-register indices, PreA and
  646. /// PreB, such that:
  647. ///
  648. /// 1. PreA + SubA == PreB + SubB (using composeSubRegIndices()), and
  649. ///
  650. /// 2. For all Reg in SuperRC: Reg:PreA in RCA and Reg:PreB in RCB, and
  651. ///
  652. /// 3. SuperRC->getSize() >= max(RCA->getSize(), RCB->getSize()).
  653. ///
  654. /// SuperRC will be chosen such that no super-class of SuperRC satisfies the
  655. /// requirements, and there is no register class with a smaller spill size
  656. /// that satisfies the requirements.
  657. ///
  658. /// SubA and SubB must not be 0. Use getMatchingSuperRegClass() instead.
  659. ///
  660. /// Either of the PreA and PreB sub-register indices may be returned as 0. In
  661. /// that case, the returned register class will be a sub-class of the
  662. /// corresponding argument register class.
  663. ///
  664. /// The function returns NULL if no register class can be found.
  665. const TargetRegisterClass*
  666. getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
  667. const TargetRegisterClass *RCB, unsigned SubB,
  668. unsigned &PreA, unsigned &PreB) const;
  669. //===--------------------------------------------------------------------===//
  670. // Register Class Information
  671. //
  672. protected:
  673. const RegClassInfo &getRegClassInfo(const TargetRegisterClass &RC) const {
  674. return RCInfos[getNumRegClasses() * HwMode + RC.getID()];
  675. }
  676. public:
  677. /// Register class iterators
  678. regclass_iterator regclass_begin() const { return RegClassBegin; }
  679. regclass_iterator regclass_end() const { return RegClassEnd; }
  680. iterator_range<regclass_iterator> regclasses() const {
  681. return make_range(regclass_begin(), regclass_end());
  682. }
  683. unsigned getNumRegClasses() const {
  684. return (unsigned)(regclass_end()-regclass_begin());
  685. }
  686. /// Returns the register class associated with the enumeration value.
  687. /// See class MCOperandInfo.
  688. const TargetRegisterClass *getRegClass(unsigned i) const {
  689. assert(i < getNumRegClasses() && "Register Class ID out of range");
  690. return RegClassBegin[i];
  691. }
  692. /// Returns the name of the register class.
  693. const char *getRegClassName(const TargetRegisterClass *Class) const {
  694. return MCRegisterInfo::getRegClassName(Class->MC);
  695. }
  696. /// Find the largest common subclass of A and B.
  697. /// Return NULL if there is no common subclass.
  698. const TargetRegisterClass *
  699. getCommonSubClass(const TargetRegisterClass *A,
  700. const TargetRegisterClass *B) const;
  701. /// Returns a TargetRegisterClass used for pointer values.
  702. /// If a target supports multiple different pointer register classes,
  703. /// kind specifies which one is indicated.
  704. virtual const TargetRegisterClass *
  705. getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const {
  706. llvm_unreachable("Target didn't implement getPointerRegClass!");
  707. }
  708. /// Returns a legal register class to copy a register in the specified class
  709. /// to or from. If it is possible to copy the register directly without using
  710. /// a cross register class copy, return the specified RC. Returns NULL if it
  711. /// is not possible to copy between two registers of the specified class.
  712. virtual const TargetRegisterClass *
  713. getCrossCopyRegClass(const TargetRegisterClass *RC) const {
  714. return RC;
  715. }
  716. /// Returns the largest super class of RC that is legal to use in the current
  717. /// sub-target and has the same spill size.
  718. /// The returned register class can be used to create virtual registers which
  719. /// means that all its registers can be copied and spilled.
  720. virtual const TargetRegisterClass *
  721. getLargestLegalSuperClass(const TargetRegisterClass *RC,
  722. const MachineFunction &) const {
  723. /// The default implementation is very conservative and doesn't allow the
  724. /// register allocator to inflate register classes.
  725. return RC;
  726. }
  727. /// Return the register pressure "high water mark" for the specific register
  728. /// class. The scheduler is in high register pressure mode (for the specific
  729. /// register class) if it goes over the limit.
  730. ///
  731. /// Note: this is the old register pressure model that relies on a manually
  732. /// specified representative register class per value type.
  733. virtual unsigned getRegPressureLimit(const TargetRegisterClass *RC,
  734. MachineFunction &MF) const {
  735. return 0;
  736. }
  737. /// Return a heuristic for the machine scheduler to compare the profitability
  738. /// of increasing one register pressure set versus another. The scheduler
  739. /// will prefer increasing the register pressure of the set which returns
  740. /// the largest value for this function.
  741. virtual unsigned getRegPressureSetScore(const MachineFunction &MF,
  742. unsigned PSetID) const {
  743. return PSetID;
  744. }
  745. /// Get the weight in units of pressure for this register class.
  746. virtual const RegClassWeight &getRegClassWeight(
  747. const TargetRegisterClass *RC) const = 0;
  748. /// Returns size in bits of a phys/virtual/generic register.
  749. unsigned getRegSizeInBits(Register Reg, const MachineRegisterInfo &MRI) const;
  750. /// Get the weight in units of pressure for this register unit.
  751. virtual unsigned getRegUnitWeight(unsigned RegUnit) const = 0;
  752. /// Get the number of dimensions of register pressure.
  753. virtual unsigned getNumRegPressureSets() const = 0;
  754. /// Get the name of this register unit pressure set.
  755. virtual const char *getRegPressureSetName(unsigned Idx) const = 0;
  756. /// Get the register unit pressure limit for this dimension.
  757. /// This limit must be adjusted dynamically for reserved registers.
  758. virtual unsigned getRegPressureSetLimit(const MachineFunction &MF,
  759. unsigned Idx) const = 0;
  760. /// Get the dimensions of register pressure impacted by this register class.
  761. /// Returns a -1 terminated array of pressure set IDs.
  762. virtual const int *getRegClassPressureSets(
  763. const TargetRegisterClass *RC) const = 0;
  764. /// Get the dimensions of register pressure impacted by this register unit.
  765. /// Returns a -1 terminated array of pressure set IDs.
  766. virtual const int *getRegUnitPressureSets(unsigned RegUnit) const = 0;
  767. /// Get a list of 'hint' registers that the register allocator should try
  768. /// first when allocating a physical register for the virtual register
  769. /// VirtReg. These registers are effectively moved to the front of the
  770. /// allocation order. If true is returned, regalloc will try to only use
  771. /// hints to the greatest extent possible even if it means spilling.
  772. ///
  773. /// The Order argument is the allocation order for VirtReg's register class
  774. /// as returned from RegisterClassInfo::getOrder(). The hint registers must
  775. /// come from Order, and they must not be reserved.
  776. ///
  777. /// The default implementation of this function will only add target
  778. /// independent register allocation hints. Targets that override this
  779. /// function should typically call this default implementation as well and
  780. /// expect to see generic copy hints added.
  781. virtual bool
  782. getRegAllocationHints(Register VirtReg, ArrayRef<MCPhysReg> Order,
  783. SmallVectorImpl<MCPhysReg> &Hints,
  784. const MachineFunction &MF,
  785. const VirtRegMap *VRM = nullptr,
  786. const LiveRegMatrix *Matrix = nullptr) const;
  787. /// A callback to allow target a chance to update register allocation hints
  788. /// when a register is "changed" (e.g. coalesced) to another register.
  789. /// e.g. On ARM, some virtual registers should target register pairs,
  790. /// if one of pair is coalesced to another register, the allocation hint of
  791. /// the other half of the pair should be changed to point to the new register.
  792. virtual void updateRegAllocHint(Register Reg, Register NewReg,
  793. MachineFunction &MF) const {
  794. // Do nothing.
  795. }
  796. /// Allow the target to reverse allocation order of local live ranges. This
  797. /// will generally allocate shorter local live ranges first. For targets with
  798. /// many registers, this could reduce regalloc compile time by a large
  799. /// factor. It is disabled by default for three reasons:
  800. /// (1) Top-down allocation is simpler and easier to debug for targets that
  801. /// don't benefit from reversing the order.
  802. /// (2) Bottom-up allocation could result in poor evicition decisions on some
  803. /// targets affecting the performance of compiled code.
  804. /// (3) Bottom-up allocation is no longer guaranteed to optimally color.
  805. virtual bool reverseLocalAssignment() const { return false; }
  806. /// Allow the target to override the cost of using a callee-saved register for
  807. /// the first time. Default value of 0 means we will use a callee-saved
  808. /// register if it is available.
  809. virtual unsigned getCSRFirstUseCost() const { return 0; }
  810. /// Returns true if the target requires (and can make use of) the register
  811. /// scavenger.
  812. virtual bool requiresRegisterScavenging(const MachineFunction &MF) const {
  813. return false;
  814. }
  815. /// Returns true if the target wants to use frame pointer based accesses to
  816. /// spill to the scavenger emergency spill slot.
  817. virtual bool useFPForScavengingIndex(const MachineFunction &MF) const {
  818. return true;
  819. }
  820. /// Returns true if the target requires post PEI scavenging of registers for
  821. /// materializing frame index constants.
  822. virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const {
  823. return false;
  824. }
  825. /// Returns true if the target requires using the RegScavenger directly for
  826. /// frame elimination despite using requiresFrameIndexScavenging.
  827. virtual bool requiresFrameIndexReplacementScavenging(
  828. const MachineFunction &MF) const {
  829. return false;
  830. }
  831. /// Returns true if the target wants the LocalStackAllocation pass to be run
  832. /// and virtual base registers used for more efficient stack access.
  833. virtual bool requiresVirtualBaseRegisters(const MachineFunction &MF) const {
  834. return false;
  835. }
  836. /// Return true if target has reserved a spill slot in the stack frame of
  837. /// the given function for the specified register. e.g. On x86, if the frame
  838. /// register is required, the first fixed stack object is reserved as its
  839. /// spill slot. This tells PEI not to create a new stack frame
  840. /// object for the given register. It should be called only after
  841. /// determineCalleeSaves().
  842. virtual bool hasReservedSpillSlot(const MachineFunction &MF, Register Reg,
  843. int &FrameIdx) const {
  844. return false;
  845. }
  846. /// Returns true if the live-ins should be tracked after register allocation.
  847. virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
  848. return true;
  849. }
  850. /// True if the stack can be realigned for the target.
  851. virtual bool canRealignStack(const MachineFunction &MF) const;
  852. /// True if storage within the function requires the stack pointer to be
  853. /// aligned more than the normal calling convention calls for.
  854. virtual bool shouldRealignStack(const MachineFunction &MF) const;
  855. /// True if stack realignment is required and still possible.
  856. bool hasStackRealignment(const MachineFunction &MF) const {
  857. return shouldRealignStack(MF) && canRealignStack(MF);
  858. }
  859. /// Get the offset from the referenced frame index in the instruction,
  860. /// if there is one.
  861. virtual int64_t getFrameIndexInstrOffset(const MachineInstr *MI,
  862. int Idx) const {
  863. return 0;
  864. }
  865. /// Returns true if the instruction's frame index reference would be better
  866. /// served by a base register other than FP or SP.
  867. /// Used by LocalStackFrameAllocation to determine which frame index
  868. /// references it should create new base registers for.
  869. virtual bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
  870. return false;
  871. }
  872. /// Insert defining instruction(s) for a pointer to FrameIdx before
  873. /// insertion point I. Return materialized frame pointer.
  874. virtual Register materializeFrameBaseRegister(MachineBasicBlock *MBB,
  875. int FrameIdx,
  876. int64_t Offset) const {
  877. llvm_unreachable("materializeFrameBaseRegister does not exist on this "
  878. "target");
  879. }
  880. /// Resolve a frame index operand of an instruction
  881. /// to reference the indicated base register plus offset instead.
  882. virtual void resolveFrameIndex(MachineInstr &MI, Register BaseReg,
  883. int64_t Offset) const {
  884. llvm_unreachable("resolveFrameIndex does not exist on this target");
  885. }
  886. /// Determine whether a given base register plus offset immediate is
  887. /// encodable to resolve a frame index.
  888. virtual bool isFrameOffsetLegal(const MachineInstr *MI, Register BaseReg,
  889. int64_t Offset) const {
  890. llvm_unreachable("isFrameOffsetLegal does not exist on this target");
  891. }
  892. /// Gets the DWARF expression opcodes for \p Offset.
  893. virtual void getOffsetOpcodes(const StackOffset &Offset,
  894. SmallVectorImpl<uint64_t> &Ops) const;
  895. /// Prepends a DWARF expression for \p Offset to DIExpression \p Expr.
  896. DIExpression *
  897. prependOffsetExpression(const DIExpression *Expr, unsigned PrependFlags,
  898. const StackOffset &Offset) const;
  899. /// Spill the register so it can be used by the register scavenger.
  900. /// Return true if the register was spilled, false otherwise.
  901. /// If this function does not spill the register, the scavenger
  902. /// will instead spill it to the emergency spill slot.
  903. virtual bool saveScavengerRegister(MachineBasicBlock &MBB,
  904. MachineBasicBlock::iterator I,
  905. MachineBasicBlock::iterator &UseMI,
  906. const TargetRegisterClass *RC,
  907. Register Reg) const {
  908. return false;
  909. }
  910. /// Process frame indices in reverse block order. This changes the behavior of
  911. /// the RegScavenger passed to eliminateFrameIndex. If this is true targets
  912. /// should scavengeRegisterBackwards in eliminateFrameIndex. New targets
  913. /// should prefer reverse scavenging behavior.
  914. virtual bool supportsBackwardScavenger() const { return false; }
  915. /// This method must be overriden to eliminate abstract frame indices from
  916. /// instructions which may use them. The instruction referenced by the
  917. /// iterator contains an MO_FrameIndex operand which must be eliminated by
  918. /// this method. This method may modify or replace the specified instruction,
  919. /// as long as it keeps the iterator pointing at the finished product.
  920. /// SPAdj is the SP adjustment due to call frame setup instruction.
  921. /// FIOperandNum is the FI operand number.
  922. /// Returns true if the current instruction was removed and the iterator
  923. /// is not longer valid
  924. virtual bool eliminateFrameIndex(MachineBasicBlock::iterator MI,
  925. int SPAdj, unsigned FIOperandNum,
  926. RegScavenger *RS = nullptr) const = 0;
  927. /// Return the assembly name for \p Reg.
  928. virtual StringRef getRegAsmName(MCRegister Reg) const {
  929. // FIXME: We are assuming that the assembly name is equal to the TableGen
  930. // name converted to lower case
  931. //
  932. // The TableGen name is the name of the definition for this register in the
  933. // target's tablegen files. For example, the TableGen name of
  934. // def EAX : Register <...>; is "EAX"
  935. return StringRef(getName(Reg));
  936. }
  937. //===--------------------------------------------------------------------===//
  938. /// Subtarget Hooks
  939. /// SrcRC and DstRC will be morphed into NewRC if this returns true.
  940. virtual bool shouldCoalesce(MachineInstr *MI,
  941. const TargetRegisterClass *SrcRC,
  942. unsigned SubReg,
  943. const TargetRegisterClass *DstRC,
  944. unsigned DstSubReg,
  945. const TargetRegisterClass *NewRC,
  946. LiveIntervals &LIS) const
  947. { return true; }
  948. /// Region split has a high compile time cost especially for large live range.
  949. /// This method is used to decide whether or not \p VirtReg should
  950. /// go through this expensive splitting heuristic.
  951. virtual bool shouldRegionSplitForVirtReg(const MachineFunction &MF,
  952. const LiveInterval &VirtReg) const;
  953. /// Last chance recoloring has a high compile time cost especially for
  954. /// targets with a lot of registers.
  955. /// This method is used to decide whether or not \p VirtReg should
  956. /// go through this expensive heuristic.
  957. /// When this target hook is hit, by returning false, there is a high
  958. /// chance that the register allocation will fail altogether (usually with
  959. /// "ran out of registers").
  960. /// That said, this error usually points to another problem in the
  961. /// optimization pipeline.
  962. virtual bool
  963. shouldUseLastChanceRecoloringForVirtReg(const MachineFunction &MF,
  964. const LiveInterval &VirtReg) const {
  965. return true;
  966. }
  967. /// Deferred spilling delays the spill insertion of a virtual register
  968. /// after every other allocation. By deferring the spilling, it is
  969. /// sometimes possible to eliminate that spilling altogether because
  970. /// something else could have been eliminated, thus leaving some space
  971. /// for the virtual register.
  972. /// However, this comes with a compile time impact because it adds one
  973. /// more stage to the greedy register allocator.
  974. /// This method is used to decide whether \p VirtReg should use the deferred
  975. /// spilling stage instead of being spilled right away.
  976. virtual bool
  977. shouldUseDeferredSpillingForVirtReg(const MachineFunction &MF,
  978. const LiveInterval &VirtReg) const {
  979. return false;
  980. }
  981. /// When prioritizing live ranges in register allocation, if this hook returns
  982. /// true then the AllocationPriority of the register class will be treated as
  983. /// more important than whether the range is local to a basic block or global.
  984. virtual bool
  985. regClassPriorityTrumpsGlobalness(const MachineFunction &MF) const {
  986. return false;
  987. }
  988. //===--------------------------------------------------------------------===//
  989. /// Debug information queries.
  990. /// getFrameRegister - This method should return the register used as a base
  991. /// for values allocated in the current stack frame.
  992. virtual Register getFrameRegister(const MachineFunction &MF) const = 0;
  993. /// Mark a register and all its aliases as reserved in the given set.
  994. void markSuperRegs(BitVector &RegisterSet, MCRegister Reg) const;
  995. /// Returns true if for every register in the set all super registers are part
  996. /// of the set as well.
  997. bool checkAllSuperRegsMarked(const BitVector &RegisterSet,
  998. ArrayRef<MCPhysReg> Exceptions = ArrayRef<MCPhysReg>()) const;
  999. virtual const TargetRegisterClass *
  1000. getConstrainedRegClassForOperand(const MachineOperand &MO,
  1001. const MachineRegisterInfo &MRI) const {
  1002. return nullptr;
  1003. }
  1004. /// Returns the physical register number of sub-register "Index"
  1005. /// for physical register RegNo. Return zero if the sub-register does not
  1006. /// exist.
  1007. inline MCRegister getSubReg(MCRegister Reg, unsigned Idx) const {
  1008. return static_cast<const MCRegisterInfo *>(this)->getSubReg(Reg, Idx);
  1009. }
  1010. /// Some targets have non-allocatable registers that aren't technically part
  1011. /// of the explicit callee saved register list, but should be handled as such
  1012. /// in certain cases.
  1013. virtual bool isNonallocatableRegisterCalleeSave(MCRegister Reg) const {
  1014. return false;
  1015. }
  1016. };
  1017. //===----------------------------------------------------------------------===//
  1018. // SuperRegClassIterator
  1019. //===----------------------------------------------------------------------===//
  1020. //
  1021. // Iterate over the possible super-registers for a given register class. The
  1022. // iterator will visit a list of pairs (Idx, Mask) corresponding to the
  1023. // possible classes of super-registers.
  1024. //
  1025. // Each bit mask will have at least one set bit, and each set bit in Mask
  1026. // corresponds to a SuperRC such that:
  1027. //
  1028. // For all Reg in SuperRC: Reg:Idx is in RC.
  1029. //
  1030. // The iterator can include (O, RC->getSubClassMask()) as the first entry which
  1031. // also satisfies the above requirement, assuming Reg:0 == Reg.
  1032. //
  1033. class SuperRegClassIterator {
  1034. const unsigned RCMaskWords;
  1035. unsigned SubReg = 0;
  1036. const uint16_t *Idx;
  1037. const uint32_t *Mask;
  1038. public:
  1039. /// Create a SuperRegClassIterator that visits all the super-register classes
  1040. /// of RC. When IncludeSelf is set, also include the (0, sub-classes) entry.
  1041. SuperRegClassIterator(const TargetRegisterClass *RC,
  1042. const TargetRegisterInfo *TRI,
  1043. bool IncludeSelf = false)
  1044. : RCMaskWords((TRI->getNumRegClasses() + 31) / 32),
  1045. Idx(RC->getSuperRegIndices()), Mask(RC->getSubClassMask()) {
  1046. if (!IncludeSelf)
  1047. ++*this;
  1048. }
  1049. /// Returns true if this iterator is still pointing at a valid entry.
  1050. bool isValid() const { return Idx; }
  1051. /// Returns the current sub-register index.
  1052. unsigned getSubReg() const { return SubReg; }
  1053. /// Returns the bit mask of register classes that getSubReg() projects into
  1054. /// RC.
  1055. /// See TargetRegisterClass::getSubClassMask() for how to use it.
  1056. const uint32_t *getMask() const { return Mask; }
  1057. /// Advance iterator to the next entry.
  1058. void operator++() {
  1059. assert(isValid() && "Cannot move iterator past end.");
  1060. Mask += RCMaskWords;
  1061. SubReg = *Idx++;
  1062. if (!SubReg)
  1063. Idx = nullptr;
  1064. }
  1065. };
  1066. //===----------------------------------------------------------------------===//
  1067. // BitMaskClassIterator
  1068. //===----------------------------------------------------------------------===//
  1069. /// This class encapuslates the logic to iterate over bitmask returned by
  1070. /// the various RegClass related APIs.
  1071. /// E.g., this class can be used to iterate over the subclasses provided by
  1072. /// TargetRegisterClass::getSubClassMask or SuperRegClassIterator::getMask.
  1073. class BitMaskClassIterator {
  1074. /// Total number of register classes.
  1075. const unsigned NumRegClasses;
  1076. /// Base index of CurrentChunk.
  1077. /// In other words, the number of bit we read to get at the
  1078. /// beginning of that chunck.
  1079. unsigned Base = 0;
  1080. /// Adjust base index of CurrentChunk.
  1081. /// Base index + how many bit we read within CurrentChunk.
  1082. unsigned Idx = 0;
  1083. /// Current register class ID.
  1084. unsigned ID = 0;
  1085. /// Mask we are iterating over.
  1086. const uint32_t *Mask;
  1087. /// Current chunk of the Mask we are traversing.
  1088. uint32_t CurrentChunk;
  1089. /// Move ID to the next set bit.
  1090. void moveToNextID() {
  1091. // If the current chunk of memory is empty, move to the next one,
  1092. // while making sure we do not go pass the number of register
  1093. // classes.
  1094. while (!CurrentChunk) {
  1095. // Move to the next chunk.
  1096. Base += 32;
  1097. if (Base >= NumRegClasses) {
  1098. ID = NumRegClasses;
  1099. return;
  1100. }
  1101. CurrentChunk = *++Mask;
  1102. Idx = Base;
  1103. }
  1104. // Otherwise look for the first bit set from the right
  1105. // (representation of the class ID is big endian).
  1106. // See getSubClassMask for more details on the representation.
  1107. unsigned Offset = countTrailingZeros(CurrentChunk);
  1108. // Add the Offset to the adjusted base number of this chunk: Idx.
  1109. // This is the ID of the register class.
  1110. ID = Idx + Offset;
  1111. // Consume the zeros, if any, and the bit we just read
  1112. // so that we are at the right spot for the next call.
  1113. // Do not do Offset + 1 because Offset may be 31 and 32
  1114. // will be UB for the shift, though in that case we could
  1115. // have make the chunk being equal to 0, but that would
  1116. // have introduced a if statement.
  1117. moveNBits(Offset);
  1118. moveNBits(1);
  1119. }
  1120. /// Move \p NumBits Bits forward in CurrentChunk.
  1121. void moveNBits(unsigned NumBits) {
  1122. assert(NumBits < 32 && "Undefined behavior spotted!");
  1123. // Consume the bit we read for the next call.
  1124. CurrentChunk >>= NumBits;
  1125. // Adjust the base for the chunk.
  1126. Idx += NumBits;
  1127. }
  1128. public:
  1129. /// Create a BitMaskClassIterator that visits all the register classes
  1130. /// represented by \p Mask.
  1131. ///
  1132. /// \pre \p Mask != nullptr
  1133. BitMaskClassIterator(const uint32_t *Mask, const TargetRegisterInfo &TRI)
  1134. : NumRegClasses(TRI.getNumRegClasses()), Mask(Mask), CurrentChunk(*Mask) {
  1135. // Move to the first ID.
  1136. moveToNextID();
  1137. }
  1138. /// Returns true if this iterator is still pointing at a valid entry.
  1139. bool isValid() const { return getID() != NumRegClasses; }
  1140. /// Returns the current register class ID.
  1141. unsigned getID() const { return ID; }
  1142. /// Advance iterator to the next entry.
  1143. void operator++() {
  1144. assert(isValid() && "Cannot move iterator past end.");
  1145. moveToNextID();
  1146. }
  1147. };
  1148. // This is useful when building IndexedMaps keyed on virtual registers
  1149. struct VirtReg2IndexFunctor {
  1150. using argument_type = Register;
  1151. unsigned operator()(Register Reg) const {
  1152. return Register::virtReg2Index(Reg);
  1153. }
  1154. };
  1155. /// Prints virtual and physical registers with or without a TRI instance.
  1156. ///
  1157. /// The format is:
  1158. /// %noreg - NoRegister
  1159. /// %5 - a virtual register.
  1160. /// %5:sub_8bit - a virtual register with sub-register index (with TRI).
  1161. /// %eax - a physical register
  1162. /// %physreg17 - a physical register when no TRI instance given.
  1163. ///
  1164. /// Usage: OS << printReg(Reg, TRI, SubRegIdx) << '\n';
  1165. Printable printReg(Register Reg, const TargetRegisterInfo *TRI = nullptr,
  1166. unsigned SubIdx = 0,
  1167. const MachineRegisterInfo *MRI = nullptr);
  1168. /// Create Printable object to print register units on a \ref raw_ostream.
  1169. ///
  1170. /// Register units are named after their root registers:
  1171. ///
  1172. /// al - Single root.
  1173. /// fp0~st7 - Dual roots.
  1174. ///
  1175. /// Usage: OS << printRegUnit(Unit, TRI) << '\n';
  1176. Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI);
  1177. /// Create Printable object to print virtual registers and physical
  1178. /// registers on a \ref raw_ostream.
  1179. Printable printVRegOrUnit(unsigned VRegOrUnit, const TargetRegisterInfo *TRI);
  1180. /// Create Printable object to print register classes or register banks
  1181. /// on a \ref raw_ostream.
  1182. Printable printRegClassOrBank(Register Reg, const MachineRegisterInfo &RegInfo,
  1183. const TargetRegisterInfo *TRI);
  1184. } // end namespace llvm
  1185. #endif // LLVM_CODEGEN_TARGETREGISTERINFO_H
  1186. #ifdef __GNUC__
  1187. #pragma GCC diagnostic pop
  1188. #endif