TargetRegisterInfo.h 53 KB

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