TargetRegisterInfo.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. //=== Target/TargetRegisterInfo.h - Target Register Information -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file describes an abstract interface used to get information about a
  11. // target machines register file. This information is used for a variety of
  12. // purposed, especially register allocation.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_TARGET_TARGETREGISTERINFO_H
  16. #define LLVM_TARGET_TARGETREGISTERINFO_H
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/CodeGen/MachineBasicBlock.h"
  19. #include "llvm/CodeGen/MachineValueType.h"
  20. #include "llvm/IR/CallingConv.h"
  21. #include "llvm/MC/MCRegisterInfo.h"
  22. #include <cassert>
  23. #include <functional>
  24. namespace llvm {
  25. class BitVector;
  26. class MachineFunction;
  27. class RegScavenger;
  28. template<class T> class SmallVectorImpl;
  29. class VirtRegMap;
  30. class raw_ostream;
  31. class TargetRegisterClass {
  32. public:
  33. typedef const MCPhysReg* iterator;
  34. typedef const MCPhysReg* const_iterator;
  35. typedef const MVT::SimpleValueType* vt_iterator;
  36. typedef const TargetRegisterClass* const * sc_iterator;
  37. // Instance variables filled by tablegen, do not use!
  38. const MCRegisterClass *MC;
  39. const vt_iterator VTs;
  40. const uint32_t *SubClassMask;
  41. const uint16_t *SuperRegIndices;
  42. const unsigned LaneMask;
  43. /// Classes with a higher priority value are assigned first by register
  44. /// allocators using a greedy heuristic. The value is in the range [0,63].
  45. const uint8_t AllocationPriority;
  46. /// Whether the class supports two (or more) disjunct subregister indices.
  47. const bool HasDisjunctSubRegs;
  48. const sc_iterator SuperClasses;
  49. ArrayRef<MCPhysReg> (*OrderFunc)(const MachineFunction&);
  50. /// getID() - Return the register class ID number.
  51. ///
  52. unsigned getID() const { return MC->getID(); }
  53. /// begin/end - Return all of the registers in this class.
  54. ///
  55. iterator begin() const { return MC->begin(); }
  56. iterator end() const { return MC->end(); }
  57. /// getNumRegs - Return the number of registers in this class.
  58. ///
  59. unsigned getNumRegs() const { return MC->getNumRegs(); }
  60. /// getRegister - Return the specified register in the class.
  61. ///
  62. unsigned getRegister(unsigned i) const {
  63. return MC->getRegister(i);
  64. }
  65. /// contains - Return true if the specified register is included in this
  66. /// register class. This does not include virtual registers.
  67. bool contains(unsigned Reg) const {
  68. return MC->contains(Reg);
  69. }
  70. /// contains - Return true if both registers are in this class.
  71. bool contains(unsigned Reg1, unsigned Reg2) const {
  72. return MC->contains(Reg1, Reg2);
  73. }
  74. /// getSize - Return the size of the register in bytes, which is also the size
  75. /// of a stack slot allocated to hold a spilled copy of this register.
  76. unsigned getSize() const { return MC->getSize(); }
  77. /// getAlignment - Return the minimum required alignment for a register of
  78. /// this class.
  79. unsigned getAlignment() const { return MC->getAlignment(); }
  80. /// getCopyCost - Return the cost of copying a value between two registers in
  81. /// this class. A negative number means the register class is very expensive
  82. /// to copy e.g. status flag register classes.
  83. int getCopyCost() const { return MC->getCopyCost(); }
  84. /// isAllocatable - Return true if this register class may be used to create
  85. /// virtual registers.
  86. bool isAllocatable() const { return MC->isAllocatable(); }
  87. /// hasType - return true if this TargetRegisterClass has the ValueType vt.
  88. ///
  89. bool hasType(MVT vt) const {
  90. for(int i = 0; VTs[i] != MVT::Other; ++i)
  91. if (MVT(VTs[i]) == vt)
  92. return true;
  93. return false;
  94. }
  95. /// vt_begin / vt_end - Loop over all of the value types that can be
  96. /// represented by values in this register class.
  97. vt_iterator vt_begin() const {
  98. return VTs;
  99. }
  100. vt_iterator vt_end() const {
  101. vt_iterator I = VTs;
  102. while (*I != MVT::Other) ++I;
  103. return I;
  104. }
  105. /// hasSubClass - return true if the specified TargetRegisterClass
  106. /// is a proper sub-class of this TargetRegisterClass.
  107. bool hasSubClass(const TargetRegisterClass *RC) const {
  108. return RC != this && hasSubClassEq(RC);
  109. }
  110. /// hasSubClassEq - Returns true if RC is a sub-class of or equal to this
  111. /// class.
  112. bool hasSubClassEq(const TargetRegisterClass *RC) const {
  113. unsigned ID = RC->getID();
  114. return (SubClassMask[ID / 32] >> (ID % 32)) & 1;
  115. }
  116. /// hasSuperClass - return true if the specified TargetRegisterClass is a
  117. /// proper super-class of this TargetRegisterClass.
  118. bool hasSuperClass(const TargetRegisterClass *RC) const {
  119. return RC->hasSubClass(this);
  120. }
  121. /// hasSuperClassEq - Returns true if RC is a super-class of or equal to this
  122. /// class.
  123. bool hasSuperClassEq(const TargetRegisterClass *RC) const {
  124. return RC->hasSubClassEq(this);
  125. }
  126. /// getSubClassMask - Returns a bit vector of subclasses, including this one.
  127. /// The vector is indexed by class IDs, see hasSubClassEq() above for how to
  128. /// use it.
  129. const uint32_t *getSubClassMask() const {
  130. return SubClassMask;
  131. }
  132. /// getSuperRegIndices - Returns a 0-terminated list of sub-register indices
  133. /// that project some super-register class into this register class. The list
  134. /// has an entry for each Idx such that:
  135. ///
  136. /// There exists SuperRC where:
  137. /// For all Reg in SuperRC:
  138. /// this->contains(Reg:Idx)
  139. ///
  140. const uint16_t *getSuperRegIndices() const {
  141. return SuperRegIndices;
  142. }
  143. /// getSuperClasses - Returns a NULL terminated list of super-classes. The
  144. /// classes are ordered by ID which is also a topological ordering from large
  145. /// to small classes. The list does NOT include the current class.
  146. sc_iterator getSuperClasses() const {
  147. return SuperClasses;
  148. }
  149. /// isASubClass - return true if this TargetRegisterClass is a subset
  150. /// class of at least one other TargetRegisterClass.
  151. bool isASubClass() const {
  152. return SuperClasses[0] != nullptr;
  153. }
  154. /// getRawAllocationOrder - Returns the preferred order for allocating
  155. /// registers from this register class in MF. The raw order comes directly
  156. /// from the .td file and may include reserved registers that are not
  157. /// allocatable. Register allocators should also make sure to allocate
  158. /// callee-saved registers only after all the volatiles are used. The
  159. /// RegisterClassInfo class provides filtered allocation orders with
  160. /// callee-saved registers moved to the end.
  161. ///
  162. /// The MachineFunction argument can be used to tune the allocatable
  163. /// registers based on the characteristics of the function, subtarget, or
  164. /// other criteria.
  165. ///
  166. /// By default, this method returns all registers in the class.
  167. ///
  168. ArrayRef<MCPhysReg> getRawAllocationOrder(const MachineFunction &MF) const {
  169. return OrderFunc ? OrderFunc(MF) : makeArrayRef(begin(), getNumRegs());
  170. }
  171. /// Returns the combination of all lane masks of register in this class.
  172. /// The lane masks of the registers are the combination of all lane masks
  173. /// of their subregisters.
  174. unsigned getLaneMask() const {
  175. return LaneMask;
  176. }
  177. };
  178. /// TargetRegisterInfoDesc - Extra information, not in MCRegisterDesc, about
  179. /// registers. These are used by codegen, not by MC.
  180. struct TargetRegisterInfoDesc {
  181. unsigned CostPerUse; // Extra cost of instructions using register.
  182. bool inAllocatableClass; // Register belongs to an allocatable regclass.
  183. };
  184. /// Each TargetRegisterClass has a per register weight, and weight
  185. /// limit which must be less than the limits of its pressure sets.
  186. struct RegClassWeight {
  187. unsigned RegWeight;
  188. unsigned WeightLimit;
  189. };
  190. /// TargetRegisterInfo base class - We assume that the target defines a static
  191. /// array of TargetRegisterDesc objects that represent all of the machine
  192. /// registers that the target has. As such, we simply have to track a pointer
  193. /// to this array so that we can turn register number into a register
  194. /// descriptor.
  195. ///
  196. class TargetRegisterInfo : public MCRegisterInfo {
  197. public:
  198. typedef const TargetRegisterClass * const * regclass_iterator;
  199. private:
  200. const TargetRegisterInfoDesc *InfoDesc; // Extra desc array for codegen
  201. const char *const *SubRegIndexNames; // Names of subreg indexes.
  202. // Pointer to array of lane masks, one per sub-reg index.
  203. const unsigned *SubRegIndexLaneMasks;
  204. regclass_iterator RegClassBegin, RegClassEnd; // List of regclasses
  205. unsigned CoveringLanes;
  206. protected:
  207. TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
  208. regclass_iterator RegClassBegin,
  209. regclass_iterator RegClassEnd,
  210. const char *const *SRINames,
  211. const unsigned *SRILaneMasks,
  212. unsigned CoveringLanes);
  213. virtual ~TargetRegisterInfo();
  214. public:
  215. // Register numbers can represent physical registers, virtual registers, and
  216. // sometimes stack slots. The unsigned values are divided into these ranges:
  217. //
  218. // 0 Not a register, can be used as a sentinel.
  219. // [1;2^30) Physical registers assigned by TableGen.
  220. // [2^30;2^31) Stack slots. (Rarely used.)
  221. // [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
  222. //
  223. // Further sentinels can be allocated from the small negative integers.
  224. // DenseMapInfo<unsigned> uses -1u and -2u.
  225. /// isStackSlot - Sometimes it is useful the be able to store a non-negative
  226. /// frame index in a variable that normally holds a register. isStackSlot()
  227. /// returns true if Reg is in the range used for stack slots.
  228. ///
  229. /// Note that isVirtualRegister() and isPhysicalRegister() cannot handle stack
  230. /// slots, so if a variable may contains a stack slot, always check
  231. /// isStackSlot() first.
  232. ///
  233. static bool isStackSlot(unsigned Reg) {
  234. return int(Reg) >= (1 << 30);
  235. }
  236. /// stackSlot2Index - Compute the frame index from a register value
  237. /// representing a stack slot.
  238. static int stackSlot2Index(unsigned Reg) {
  239. assert(isStackSlot(Reg) && "Not a stack slot");
  240. return int(Reg - (1u << 30));
  241. }
  242. /// index2StackSlot - Convert a non-negative frame index to a stack slot
  243. /// register value.
  244. static unsigned index2StackSlot(int FI) {
  245. assert(FI >= 0 && "Cannot hold a negative frame index.");
  246. return FI + (1u << 30);
  247. }
  248. /// isPhysicalRegister - Return true if the specified register number is in
  249. /// the physical register namespace.
  250. static bool isPhysicalRegister(unsigned Reg) {
  251. assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
  252. return int(Reg) > 0;
  253. }
  254. /// isVirtualRegister - Return true if the specified register number is in
  255. /// the virtual register namespace.
  256. static bool isVirtualRegister(unsigned Reg) {
  257. assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
  258. return int(Reg) < 0;
  259. }
  260. /// virtReg2Index - Convert a virtual register number to a 0-based index.
  261. /// The first virtual register in a function will get the index 0.
  262. static unsigned virtReg2Index(unsigned Reg) {
  263. assert(isVirtualRegister(Reg) && "Not a virtual register");
  264. return Reg & ~(1u << 31);
  265. }
  266. /// index2VirtReg - Convert a 0-based index to a virtual register number.
  267. /// This is the inverse operation of VirtReg2IndexFunctor below.
  268. static unsigned index2VirtReg(unsigned Index) {
  269. return Index | (1u << 31);
  270. }
  271. /// getMinimalPhysRegClass - Returns the Register Class of a physical
  272. /// register of the given type, picking the most sub register class of
  273. /// the right type that contains this physreg.
  274. const TargetRegisterClass *
  275. getMinimalPhysRegClass(unsigned Reg, MVT VT = MVT::Other) const;
  276. /// getAllocatableClass - Return the maximal subclass of the given register
  277. /// class that is alloctable, or NULL.
  278. const TargetRegisterClass *
  279. getAllocatableClass(const TargetRegisterClass *RC) const;
  280. /// getAllocatableSet - Returns a bitset indexed by register number
  281. /// indicating if a register is allocatable or not. If a register class is
  282. /// specified, returns the subset for the class.
  283. BitVector getAllocatableSet(const MachineFunction &MF,
  284. const TargetRegisterClass *RC = nullptr) const;
  285. /// getCostPerUse - Return the additional cost of using this register instead
  286. /// of other registers in its class.
  287. unsigned getCostPerUse(unsigned RegNo) const {
  288. return InfoDesc[RegNo].CostPerUse;
  289. }
  290. /// isInAllocatableClass - Return true if the register is in the allocation
  291. /// of any register class.
  292. bool isInAllocatableClass(unsigned RegNo) const {
  293. return InfoDesc[RegNo].inAllocatableClass;
  294. }
  295. /// getSubRegIndexName - Return the human-readable symbolic target-specific
  296. /// name for the specified SubRegIndex.
  297. const char *getSubRegIndexName(unsigned SubIdx) const {
  298. assert(SubIdx && SubIdx < getNumSubRegIndices() &&
  299. "This is not a subregister index");
  300. return SubRegIndexNames[SubIdx-1];
  301. }
  302. /// getSubRegIndexLaneMask - Return a bitmask representing the parts of a
  303. /// register that are covered by SubIdx.
  304. ///
  305. /// Lane masks for sub-register indices are similar to register units for
  306. /// physical registers. The individual bits in a lane mask can't be assigned
  307. /// any specific meaning. They can be used to check if two sub-register
  308. /// indices overlap.
  309. ///
  310. /// If the target has a register such that:
  311. ///
  312. /// getSubReg(Reg, A) overlaps getSubReg(Reg, B)
  313. ///
  314. /// then:
  315. ///
  316. /// (getSubRegIndexLaneMask(A) & getSubRegIndexLaneMask(B)) != 0
  317. ///
  318. /// The converse is not necessarily true. If two lane masks have a common
  319. /// bit, the corresponding sub-registers may not overlap, but it can be
  320. /// assumed that they usually will.
  321. /// SubIdx == 0 is allowed, it has the lane mask ~0u.
  322. unsigned getSubRegIndexLaneMask(unsigned SubIdx) const {
  323. assert(SubIdx < getNumSubRegIndices() && "This is not a subregister index");
  324. return SubRegIndexLaneMasks[SubIdx];
  325. }
  326. /// Returns true if the given lane mask is imprecise.
  327. ///
  328. /// LaneMasks as given by getSubRegIndexLaneMask() have a limited number of
  329. /// bits, so for targets with more than 31 disjunct subregister indices there
  330. /// may be cases where:
  331. /// getSubReg(Reg,A) does not overlap getSubReg(Reg,B)
  332. /// but we still have
  333. /// (getSubRegIndexLaneMask(A) & getSubRegIndexLaneMask(B)) != 0.
  334. /// This function returns true in those cases.
  335. static bool isImpreciseLaneMask(unsigned LaneMask) {
  336. return LaneMask & 0x80000000u;
  337. }
  338. /// The lane masks returned by getSubRegIndexLaneMask() above can only be
  339. /// used to determine if sub-registers overlap - they can't be used to
  340. /// determine if a set of sub-registers completely cover another
  341. /// sub-register.
  342. ///
  343. /// The X86 general purpose registers have two lanes corresponding to the
  344. /// sub_8bit and sub_8bit_hi sub-registers. Both sub_32bit and sub_16bit have
  345. /// lane masks '3', but the sub_16bit sub-register doesn't fully cover the
  346. /// sub_32bit sub-register.
  347. ///
  348. /// On the other hand, the ARM NEON lanes fully cover their registers: The
  349. /// dsub_0 sub-register is completely covered by the ssub_0 and ssub_1 lanes.
  350. /// This is related to the CoveredBySubRegs property on register definitions.
  351. ///
  352. /// This function returns a bit mask of lanes that completely cover their
  353. /// sub-registers. More precisely, given:
  354. ///
  355. /// Covering = getCoveringLanes();
  356. /// MaskA = getSubRegIndexLaneMask(SubA);
  357. /// MaskB = getSubRegIndexLaneMask(SubB);
  358. ///
  359. /// If (MaskA & ~(MaskB & Covering)) == 0, then SubA is completely covered by
  360. /// SubB.
  361. unsigned getCoveringLanes() const { return CoveringLanes; }
  362. /// regsOverlap - Returns true if the two registers are equal or alias each
  363. /// other. The registers may be virtual register.
  364. bool regsOverlap(unsigned regA, unsigned regB) const {
  365. if (regA == regB) return true;
  366. if (isVirtualRegister(regA) || isVirtualRegister(regB))
  367. return false;
  368. // Regunits are numerically ordered. Find a common unit.
  369. MCRegUnitIterator RUA(regA, this);
  370. MCRegUnitIterator RUB(regB, this);
  371. do {
  372. if (*RUA == *RUB) return true;
  373. if (*RUA < *RUB) ++RUA;
  374. else ++RUB;
  375. } while (RUA.isValid() && RUB.isValid());
  376. return false;
  377. }
  378. /// hasRegUnit - Returns true if Reg contains RegUnit.
  379. bool hasRegUnit(unsigned Reg, unsigned RegUnit) const {
  380. for (MCRegUnitIterator Units(Reg, this); Units.isValid(); ++Units)
  381. if (*Units == RegUnit)
  382. return true;
  383. return false;
  384. }
  385. /// getCalleeSavedRegs - Return a null-terminated list of all of the
  386. /// callee saved registers on this target. The register should be in the
  387. /// order of desired callee-save stack frame offset. The first register is
  388. /// closest to the incoming stack pointer if stack grows down, and vice versa.
  389. ///
  390. virtual const MCPhysReg*
  391. getCalleeSavedRegs(const MachineFunction *MF) const = 0;
  392. /// getCallPreservedMask - Return a mask of call-preserved registers for the
  393. /// given calling convention on the current function. The mask should
  394. /// include all call-preserved aliases. This is used by the register
  395. /// allocator to determine which registers can be live across a call.
  396. ///
  397. /// The mask is an array containing (TRI::getNumRegs()+31)/32 entries.
  398. /// A set bit indicates that all bits of the corresponding register are
  399. /// preserved across the function call. The bit mask is expected to be
  400. /// sub-register complete, i.e. if A is preserved, so are all its
  401. /// sub-registers.
  402. ///
  403. /// Bits are numbered from the LSB, so the bit for physical register Reg can
  404. /// be found as (Mask[Reg / 32] >> Reg % 32) & 1.
  405. ///
  406. /// A NULL pointer means that no register mask will be used, and call
  407. /// instructions should use implicit-def operands to indicate call clobbered
  408. /// registers.
  409. ///
  410. virtual const uint32_t *getCallPreservedMask(const MachineFunction &MF,
  411. CallingConv::ID) const {
  412. // The default mask clobbers everything. All targets should override.
  413. return nullptr;
  414. }
  415. /// Return all the call-preserved register masks defined for this target.
  416. virtual ArrayRef<const uint32_t *> getRegMasks() const = 0;
  417. virtual ArrayRef<const char *> getRegMaskNames() const = 0;
  418. /// getReservedRegs - Returns a bitset indexed by physical register number
  419. /// indicating if a register is a special register that has particular uses
  420. /// and should be considered unavailable at all times, e.g. SP, RA. This is
  421. /// used by register scavenger to determine what registers are free.
  422. virtual BitVector getReservedRegs(const MachineFunction &MF) const = 0;
  423. /// Prior to adding the live-out mask to a stackmap or patchpoint
  424. /// instruction, provide the target the opportunity to adjust it (mainly to
  425. /// remove pseudo-registers that should be ignored).
  426. virtual void adjustStackMapLiveOutMask(uint32_t *Mask) const { }
  427. /// getMatchingSuperReg - Return a super-register of the specified register
  428. /// Reg so its sub-register of index SubIdx is Reg.
  429. unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
  430. const TargetRegisterClass *RC) const {
  431. return MCRegisterInfo::getMatchingSuperReg(Reg, SubIdx, RC->MC);
  432. }
  433. /// getMatchingSuperRegClass - Return a subclass of the specified register
  434. /// class A so that each register in it has a sub-register of the
  435. /// specified sub-register index which is in the specified register class B.
  436. ///
  437. /// TableGen will synthesize missing A sub-classes.
  438. virtual const TargetRegisterClass *
  439. getMatchingSuperRegClass(const TargetRegisterClass *A,
  440. const TargetRegisterClass *B, unsigned Idx) const;
  441. /// getSubClassWithSubReg - Returns the largest legal sub-class of RC that
  442. /// supports the sub-register index Idx.
  443. /// If no such sub-class exists, return NULL.
  444. /// If all registers in RC already have an Idx sub-register, return RC.
  445. ///
  446. /// TableGen generates a version of this function that is good enough in most
  447. /// cases. Targets can override if they have constraints that TableGen
  448. /// doesn't understand. For example, the x86 sub_8bit sub-register index is
  449. /// supported by the full GR32 register class in 64-bit mode, but only by the
  450. /// GR32_ABCD regiister class in 32-bit mode.
  451. ///
  452. /// TableGen will synthesize missing RC sub-classes.
  453. virtual const TargetRegisterClass *
  454. getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const {
  455. assert(Idx == 0 && "Target has no sub-registers");
  456. return RC;
  457. }
  458. /// composeSubRegIndices - Return the subregister index you get from composing
  459. /// two subregister indices.
  460. ///
  461. /// The special null sub-register index composes as the identity.
  462. ///
  463. /// If R:a:b is the same register as R:c, then composeSubRegIndices(a, b)
  464. /// returns c. Note that composeSubRegIndices does not tell you about illegal
  465. /// compositions. If R does not have a subreg a, or R:a does not have a subreg
  466. /// b, composeSubRegIndices doesn't tell you.
  467. ///
  468. /// The ARM register Q0 has two D subregs dsub_0:D0 and dsub_1:D1. It also has
  469. /// ssub_0:S0 - ssub_3:S3 subregs.
  470. /// If you compose subreg indices dsub_1, ssub_0 you get ssub_2.
  471. ///
  472. unsigned composeSubRegIndices(unsigned a, unsigned b) const {
  473. if (!a) return b;
  474. if (!b) return a;
  475. return composeSubRegIndicesImpl(a, b);
  476. }
  477. /// Transforms a LaneMask computed for one subregister to the lanemask that
  478. /// would have been computed when composing the subsubregisters with IdxA
  479. /// first. @sa composeSubRegIndices()
  480. unsigned composeSubRegIndexLaneMask(unsigned IdxA, unsigned LaneMask) const {
  481. if (!IdxA)
  482. return LaneMask;
  483. return composeSubRegIndexLaneMaskImpl(IdxA, LaneMask);
  484. }
  485. /// Debugging helper: dump register in human readable form to dbgs() stream.
  486. static void dumpReg(unsigned Reg, unsigned SubRegIndex = 0,
  487. const TargetRegisterInfo* TRI = nullptr);
  488. protected:
  489. /// Overridden by TableGen in targets that have sub-registers.
  490. virtual unsigned composeSubRegIndicesImpl(unsigned, unsigned) const {
  491. llvm_unreachable("Target has no sub-registers");
  492. }
  493. /// Overridden by TableGen in targets that have sub-registers.
  494. virtual unsigned
  495. composeSubRegIndexLaneMaskImpl(unsigned, unsigned) const {
  496. llvm_unreachable("Target has no sub-registers");
  497. }
  498. public:
  499. /// getCommonSuperRegClass - Find a common super-register class if it exists.
  500. ///
  501. /// Find a register class, SuperRC and two sub-register indices, PreA and
  502. /// PreB, such that:
  503. ///
  504. /// 1. PreA + SubA == PreB + SubB (using composeSubRegIndices()), and
  505. ///
  506. /// 2. For all Reg in SuperRC: Reg:PreA in RCA and Reg:PreB in RCB, and
  507. ///
  508. /// 3. SuperRC->getSize() >= max(RCA->getSize(), RCB->getSize()).
  509. ///
  510. /// SuperRC will be chosen such that no super-class of SuperRC satisfies the
  511. /// requirements, and there is no register class with a smaller spill size
  512. /// that satisfies the requirements.
  513. ///
  514. /// SubA and SubB must not be 0. Use getMatchingSuperRegClass() instead.
  515. ///
  516. /// Either of the PreA and PreB sub-register indices may be returned as 0. In
  517. /// that case, the returned register class will be a sub-class of the
  518. /// corresponding argument register class.
  519. ///
  520. /// The function returns NULL if no register class can be found.
  521. ///
  522. const TargetRegisterClass*
  523. getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
  524. const TargetRegisterClass *RCB, unsigned SubB,
  525. unsigned &PreA, unsigned &PreB) const;
  526. //===--------------------------------------------------------------------===//
  527. // Register Class Information
  528. //
  529. /// Register class iterators
  530. ///
  531. regclass_iterator regclass_begin() const { return RegClassBegin; }
  532. regclass_iterator regclass_end() const { return RegClassEnd; }
  533. unsigned getNumRegClasses() const {
  534. return (unsigned)(regclass_end()-regclass_begin());
  535. }
  536. /// getRegClass - Returns the register class associated with the enumeration
  537. /// value. See class MCOperandInfo.
  538. const TargetRegisterClass *getRegClass(unsigned i) const {
  539. assert(i < getNumRegClasses() && "Register Class ID out of range");
  540. return RegClassBegin[i];
  541. }
  542. /// getRegClassName - Returns the name of the register class.
  543. const char *getRegClassName(const TargetRegisterClass *Class) const {
  544. return MCRegisterInfo::getRegClassName(Class->MC);
  545. }
  546. /// getCommonSubClass - find the largest common subclass of A and B. Return
  547. /// NULL if there is no common subclass.
  548. const TargetRegisterClass *
  549. getCommonSubClass(const TargetRegisterClass *A,
  550. const TargetRegisterClass *B) const;
  551. /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
  552. /// values. If a target supports multiple different pointer register classes,
  553. /// kind specifies which one is indicated.
  554. virtual const TargetRegisterClass *
  555. getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const {
  556. llvm_unreachable("Target didn't implement getPointerRegClass!");
  557. }
  558. /// getCrossCopyRegClass - Returns a legal register class to copy a register
  559. /// in the specified class to or from. If it is possible to copy the register
  560. /// directly without using a cross register class copy, return the specified
  561. /// RC. Returns NULL if it is not possible to copy between a two registers of
  562. /// the specified class.
  563. virtual const TargetRegisterClass *
  564. getCrossCopyRegClass(const TargetRegisterClass *RC) const {
  565. return RC;
  566. }
  567. /// getLargestLegalSuperClass - Returns the largest super class of RC that is
  568. /// legal to use in the current sub-target and has the same spill size.
  569. /// The returned register class can be used to create virtual registers which
  570. /// means that all its registers can be copied and spilled.
  571. virtual const TargetRegisterClass *
  572. getLargestLegalSuperClass(const TargetRegisterClass *RC,
  573. const MachineFunction &) const {
  574. /// The default implementation is very conservative and doesn't allow the
  575. /// register allocator to inflate register classes.
  576. return RC;
  577. }
  578. /// getRegPressureLimit - Return the register pressure "high water mark" for
  579. /// the specific register class. The scheduler is in high register pressure
  580. /// mode (for the specific register class) if it goes over the limit.
  581. ///
  582. /// Note: this is the old register pressure model that relies on a manually
  583. /// specified representative register class per value type.
  584. virtual unsigned getRegPressureLimit(const TargetRegisterClass *RC,
  585. MachineFunction &MF) const {
  586. return 0;
  587. }
  588. /// Get the weight in units of pressure for this register class.
  589. virtual const RegClassWeight &getRegClassWeight(
  590. const TargetRegisterClass *RC) const = 0;
  591. /// Get the weight in units of pressure for this register unit.
  592. virtual unsigned getRegUnitWeight(unsigned RegUnit) const = 0;
  593. /// Get the number of dimensions of register pressure.
  594. virtual unsigned getNumRegPressureSets() const = 0;
  595. /// Get the name of this register unit pressure set.
  596. virtual const char *getRegPressureSetName(unsigned Idx) const = 0;
  597. /// Get the register unit pressure limit for this dimension.
  598. /// This limit must be adjusted dynamically for reserved registers.
  599. virtual unsigned getRegPressureSetLimit(const MachineFunction &MF,
  600. unsigned Idx) const = 0;
  601. /// Get the dimensions of register pressure impacted by this register class.
  602. /// Returns a -1 terminated array of pressure set IDs.
  603. virtual const int *getRegClassPressureSets(
  604. const TargetRegisterClass *RC) const = 0;
  605. /// Get the dimensions of register pressure impacted by this register unit.
  606. /// Returns a -1 terminated array of pressure set IDs.
  607. virtual const int *getRegUnitPressureSets(unsigned RegUnit) const = 0;
  608. /// Get a list of 'hint' registers that the register allocator should try
  609. /// first when allocating a physical register for the virtual register
  610. /// VirtReg. These registers are effectively moved to the front of the
  611. /// allocation order.
  612. ///
  613. /// The Order argument is the allocation order for VirtReg's register class
  614. /// as returned from RegisterClassInfo::getOrder(). The hint registers must
  615. /// come from Order, and they must not be reserved.
  616. ///
  617. /// The default implementation of this function can resolve
  618. /// target-independent hints provided to MRI::setRegAllocationHint with
  619. /// HintType == 0. Targets that override this function should defer to the
  620. /// default implementation if they have no reason to change the allocation
  621. /// order for VirtReg. There may be target-independent hints.
  622. virtual void getRegAllocationHints(unsigned VirtReg,
  623. ArrayRef<MCPhysReg> Order,
  624. SmallVectorImpl<MCPhysReg> &Hints,
  625. const MachineFunction &MF,
  626. const VirtRegMap *VRM = nullptr) const;
  627. /// updateRegAllocHint - A callback to allow target a chance to update
  628. /// register allocation hints when a register is "changed" (e.g. coalesced)
  629. /// to another register. e.g. On ARM, some virtual registers should target
  630. /// register pairs, if one of pair is coalesced to another register, the
  631. /// allocation hint of the other half of the pair should be changed to point
  632. /// to the new register.
  633. virtual void updateRegAllocHint(unsigned Reg, unsigned NewReg,
  634. MachineFunction &MF) const {
  635. // Do nothing.
  636. }
  637. /// Allow the target to reverse allocation order of local live ranges. This
  638. /// will generally allocate shorter local live ranges first. For targets with
  639. /// many registers, this could reduce regalloc compile time by a large
  640. /// factor. It is disabled by default for three reasons:
  641. /// (1) Top-down allocation is simpler and easier to debug for targets that
  642. /// don't benefit from reversing the order.
  643. /// (2) Bottom-up allocation could result in poor evicition decisions on some
  644. /// targets affecting the performance of compiled code.
  645. /// (3) Bottom-up allocation is no longer guaranteed to optimally color.
  646. virtual bool reverseLocalAssignment() const { return false; }
  647. /// Allow the target to override the cost of using a callee-saved register for
  648. /// the first time. Default value of 0 means we will use a callee-saved
  649. /// register if it is available.
  650. virtual unsigned getCSRFirstUseCost() const { return 0; }
  651. /// requiresRegisterScavenging - returns true if the target requires (and can
  652. /// make use of) the register scavenger.
  653. virtual bool requiresRegisterScavenging(const MachineFunction &MF) const {
  654. return false;
  655. }
  656. /// useFPForScavengingIndex - returns true if the target wants to use
  657. /// frame pointer based accesses to spill to the scavenger emergency spill
  658. /// slot.
  659. virtual bool useFPForScavengingIndex(const MachineFunction &MF) const {
  660. return true;
  661. }
  662. /// requiresFrameIndexScavenging - returns true if the target requires post
  663. /// PEI scavenging of registers for materializing frame index constants.
  664. virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const {
  665. return false;
  666. }
  667. /// requiresVirtualBaseRegisters - Returns true if the target wants the
  668. /// LocalStackAllocation pass to be run and virtual base registers
  669. /// used for more efficient stack access.
  670. virtual bool requiresVirtualBaseRegisters(const MachineFunction &MF) const {
  671. return false;
  672. }
  673. /// hasReservedSpillSlot - Return true if target has reserved a spill slot in
  674. /// the stack frame of the given function for the specified register. e.g. On
  675. /// x86, if the frame register is required, the first fixed stack object is
  676. /// reserved as its spill slot. This tells PEI not to create a new stack frame
  677. /// object for the given register. It should be called only after
  678. /// processFunctionBeforeCalleeSavedScan().
  679. virtual bool hasReservedSpillSlot(const MachineFunction &MF, unsigned Reg,
  680. int &FrameIdx) const {
  681. return false;
  682. }
  683. /// trackLivenessAfterRegAlloc - returns true if the live-ins should be tracked
  684. /// after register allocation.
  685. virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
  686. return false;
  687. }
  688. /// needsStackRealignment - true if storage within the function requires the
  689. /// stack pointer to be aligned more than the normal calling convention calls
  690. /// for.
  691. virtual bool needsStackRealignment(const MachineFunction &MF) const {
  692. return false;
  693. }
  694. /// getFrameIndexInstrOffset - Get the offset from the referenced frame
  695. /// index in the instruction, if there is one.
  696. virtual int64_t getFrameIndexInstrOffset(const MachineInstr *MI,
  697. int Idx) const {
  698. return 0;
  699. }
  700. /// needsFrameBaseReg - Returns true if the instruction's frame index
  701. /// reference would be better served by a base register other than FP
  702. /// or SP. Used by LocalStackFrameAllocation to determine which frame index
  703. /// references it should create new base registers for.
  704. virtual bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
  705. return false;
  706. }
  707. /// materializeFrameBaseRegister - Insert defining instruction(s) for
  708. /// BaseReg to be a pointer to FrameIdx before insertion point I.
  709. virtual void materializeFrameBaseRegister(MachineBasicBlock *MBB,
  710. unsigned BaseReg, int FrameIdx,
  711. int64_t Offset) const {
  712. llvm_unreachable("materializeFrameBaseRegister does not exist on this "
  713. "target");
  714. }
  715. /// resolveFrameIndex - Resolve a frame index operand of an instruction
  716. /// to reference the indicated base register plus offset instead.
  717. virtual void resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
  718. int64_t Offset) const {
  719. llvm_unreachable("resolveFrameIndex does not exist on this target");
  720. }
  721. /// isFrameOffsetLegal - Determine whether a given base register plus offset
  722. /// immediate is encodable to resolve a frame index.
  723. virtual bool isFrameOffsetLegal(const MachineInstr *MI, unsigned BaseReg,
  724. int64_t Offset) const {
  725. llvm_unreachable("isFrameOffsetLegal does not exist on this target");
  726. }
  727. /// saveScavengerRegister - Spill the register so it can be used by the
  728. /// register scavenger. Return true if the register was spilled, false
  729. /// otherwise. If this function does not spill the register, the scavenger
  730. /// will instead spill it to the emergency spill slot.
  731. ///
  732. virtual bool saveScavengerRegister(MachineBasicBlock &MBB,
  733. MachineBasicBlock::iterator I,
  734. MachineBasicBlock::iterator &UseMI,
  735. const TargetRegisterClass *RC,
  736. unsigned Reg) const {
  737. return false;
  738. }
  739. /// eliminateFrameIndex - This method must be overriden to eliminate abstract
  740. /// frame indices from instructions which may use them. The instruction
  741. /// referenced by the iterator contains an MO_FrameIndex operand which must be
  742. /// eliminated by this method. This method may modify or replace the
  743. /// specified instruction, as long as it keeps the iterator pointing at the
  744. /// finished product. SPAdj is the SP adjustment due to call frame setup
  745. /// instruction. FIOperandNum is the FI operand number.
  746. virtual void eliminateFrameIndex(MachineBasicBlock::iterator MI,
  747. int SPAdj, unsigned FIOperandNum,
  748. RegScavenger *RS = nullptr) const = 0;
  749. //===--------------------------------------------------------------------===//
  750. /// Subtarget Hooks
  751. /// \brief SrcRC and DstRC will be morphed into NewRC if this returns true.
  752. virtual bool shouldCoalesce(MachineInstr *MI,
  753. const TargetRegisterClass *SrcRC,
  754. unsigned SubReg,
  755. const TargetRegisterClass *DstRC,
  756. unsigned DstSubReg,
  757. const TargetRegisterClass *NewRC) const
  758. { return true; }
  759. //===--------------------------------------------------------------------===//
  760. /// Debug information queries.
  761. /// getFrameRegister - This method should return the register used as a base
  762. /// for values allocated in the current stack frame.
  763. virtual unsigned getFrameRegister(const MachineFunction &MF) const = 0;
  764. };
  765. //===----------------------------------------------------------------------===//
  766. // SuperRegClassIterator
  767. // //
  768. ///////////////////////////////////////////////////////////////////////////////
  769. //
  770. // Iterate over the possible super-registers for a given register class. The
  771. // iterator will visit a list of pairs (Idx, Mask) corresponding to the
  772. // possible classes of super-registers.
  773. //
  774. // Each bit mask will have at least one set bit, and each set bit in Mask
  775. // corresponds to a SuperRC such that:
  776. //
  777. // For all Reg in SuperRC: Reg:Idx is in RC.
  778. //
  779. // The iterator can include (O, RC->getSubClassMask()) as the first entry which
  780. // also satisfies the above requirement, assuming Reg:0 == Reg.
  781. //
  782. class SuperRegClassIterator {
  783. const unsigned RCMaskWords;
  784. unsigned SubReg;
  785. const uint16_t *Idx;
  786. const uint32_t *Mask;
  787. public:
  788. /// Create a SuperRegClassIterator that visits all the super-register classes
  789. /// of RC. When IncludeSelf is set, also include the (0, sub-classes) entry.
  790. SuperRegClassIterator(const TargetRegisterClass *RC,
  791. const TargetRegisterInfo *TRI,
  792. bool IncludeSelf = false)
  793. : RCMaskWords((TRI->getNumRegClasses() + 31) / 32),
  794. SubReg(0),
  795. Idx(RC->getSuperRegIndices()),
  796. Mask(RC->getSubClassMask()) {
  797. if (!IncludeSelf)
  798. ++*this;
  799. }
  800. /// Returns true if this iterator is still pointing at a valid entry.
  801. bool isValid() const { return Idx; }
  802. /// Returns the current sub-register index.
  803. unsigned getSubReg() const { return SubReg; }
  804. /// Returns the bit mask if register classes that getSubReg() projects into
  805. /// RC.
  806. const uint32_t *getMask() const { return Mask; }
  807. /// Advance iterator to the next entry.
  808. void operator++() {
  809. assert(isValid() && "Cannot move iterator past end.");
  810. Mask += RCMaskWords;
  811. SubReg = *Idx++;
  812. if (!SubReg)
  813. Idx = nullptr;
  814. }
  815. };
  816. // This is useful when building IndexedMaps keyed on virtual registers
  817. struct VirtReg2IndexFunctor : public std::unary_function<unsigned, unsigned> {
  818. unsigned operator()(unsigned Reg) const {
  819. return TargetRegisterInfo::virtReg2Index(Reg);
  820. }
  821. };
  822. /// PrintReg - Helper class for printing registers on a raw_ostream.
  823. /// Prints virtual and physical registers with or without a TRI instance.
  824. ///
  825. /// The format is:
  826. /// %noreg - NoRegister
  827. /// %vreg5 - a virtual register.
  828. /// %vreg5:sub_8bit - a virtual register with sub-register index (with TRI).
  829. /// %EAX - a physical register
  830. /// %physreg17 - a physical register when no TRI instance given.
  831. ///
  832. /// Usage: OS << PrintReg(Reg, TRI) << '\n';
  833. ///
  834. class PrintReg {
  835. const TargetRegisterInfo *TRI;
  836. unsigned Reg;
  837. unsigned SubIdx;
  838. public:
  839. explicit PrintReg(unsigned reg, const TargetRegisterInfo *tri = nullptr,
  840. unsigned subidx = 0)
  841. : TRI(tri), Reg(reg), SubIdx(subidx) {}
  842. void print(raw_ostream&) const;
  843. };
  844. static inline raw_ostream &operator<<(raw_ostream &OS, const PrintReg &PR) {
  845. PR.print(OS);
  846. return OS;
  847. }
  848. /// PrintRegUnit - Helper class for printing register units on a raw_ostream.
  849. ///
  850. /// Register units are named after their root registers:
  851. ///
  852. /// AL - Single root.
  853. /// FP0~ST7 - Dual roots.
  854. ///
  855. /// Usage: OS << PrintRegUnit(Unit, TRI) << '\n';
  856. ///
  857. class PrintRegUnit {
  858. protected:
  859. const TargetRegisterInfo *TRI;
  860. unsigned Unit;
  861. public:
  862. PrintRegUnit(unsigned unit, const TargetRegisterInfo *tri)
  863. : TRI(tri), Unit(unit) {}
  864. void print(raw_ostream&) const;
  865. };
  866. static inline raw_ostream &operator<<(raw_ostream &OS, const PrintRegUnit &PR) {
  867. PR.print(OS);
  868. return OS;
  869. }
  870. /// PrintVRegOrUnit - It is often convenient to track virtual registers and
  871. /// physical register units in the same list.
  872. class PrintVRegOrUnit : protected PrintRegUnit {
  873. public:
  874. PrintVRegOrUnit(unsigned VRegOrUnit, const TargetRegisterInfo *tri)
  875. : PrintRegUnit(VRegOrUnit, tri) {}
  876. void print(raw_ostream&) const;
  877. };
  878. static inline raw_ostream &operator<<(raw_ostream &OS,
  879. const PrintVRegOrUnit &PR) {
  880. PR.print(OS);
  881. return OS;
  882. }
  883. } // End llvm namespace
  884. #endif