MachineOperand.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/CodeGen/MachineOperand.h - MachineOperand class ----*- 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 contains the declaration of the MachineOperand class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_MACHINEOPERAND_H
  18. #define LLVM_CODEGEN_MACHINEOPERAND_H
  19. #include "llvm/ADT/DenseMapInfo.h"
  20. #include "llvm/CodeGen/Register.h"
  21. #include "llvm/IR/Intrinsics.h"
  22. #include <cassert>
  23. namespace llvm {
  24. class LLT;
  25. class BlockAddress;
  26. class Constant;
  27. class ConstantFP;
  28. class ConstantInt;
  29. class GlobalValue;
  30. class MachineBasicBlock;
  31. class MachineInstr;
  32. class MachineRegisterInfo;
  33. class MCCFIInstruction;
  34. class MDNode;
  35. class ModuleSlotTracker;
  36. class TargetIntrinsicInfo;
  37. class TargetRegisterInfo;
  38. class hash_code;
  39. class raw_ostream;
  40. class MCSymbol;
  41. /// MachineOperand class - Representation of each machine instruction operand.
  42. ///
  43. /// This class isn't a POD type because it has a private constructor, but its
  44. /// destructor must be trivial. Functions like MachineInstr::addOperand(),
  45. /// MachineRegisterInfo::moveOperands(), and MF::DeleteMachineInstr() depend on
  46. /// not having to call the MachineOperand destructor.
  47. ///
  48. class MachineOperand {
  49. public:
  50. enum MachineOperandType : unsigned char {
  51. MO_Register, ///< Register operand.
  52. MO_Immediate, ///< Immediate operand
  53. MO_CImmediate, ///< Immediate >64bit operand
  54. MO_FPImmediate, ///< Floating-point immediate operand
  55. MO_MachineBasicBlock, ///< MachineBasicBlock reference
  56. MO_FrameIndex, ///< Abstract Stack Frame Index
  57. MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool
  58. MO_TargetIndex, ///< Target-dependent index+offset operand.
  59. MO_JumpTableIndex, ///< Address of indexed Jump Table for switch
  60. MO_ExternalSymbol, ///< Name of external global symbol
  61. MO_GlobalAddress, ///< Address of a global value
  62. MO_BlockAddress, ///< Address of a basic block
  63. MO_RegisterMask, ///< Mask of preserved registers.
  64. MO_RegisterLiveOut, ///< Mask of live-out registers.
  65. MO_Metadata, ///< Metadata reference (for debug info)
  66. MO_MCSymbol, ///< MCSymbol reference (for debug/eh info)
  67. MO_CFIIndex, ///< MCCFIInstruction index.
  68. MO_IntrinsicID, ///< Intrinsic ID for ISel
  69. MO_Predicate, ///< Generic predicate for ISel
  70. MO_ShuffleMask, ///< Other IR Constant for ISel (shuffle masks)
  71. MO_DbgInstrRef, ///< Integer indices referring to an instruction+operand
  72. MO_Last = MO_DbgInstrRef
  73. };
  74. private:
  75. /// OpKind - Specify what kind of operand this is. This discriminates the
  76. /// union.
  77. unsigned OpKind : 8;
  78. /// Subregister number for MO_Register. A value of 0 indicates the
  79. /// MO_Register has no subReg.
  80. ///
  81. /// For all other kinds of operands, this field holds target-specific flags.
  82. unsigned SubReg_TargetFlags : 12;
  83. /// TiedTo - Non-zero when this register operand is tied to another register
  84. /// operand. The encoding of this field is described in the block comment
  85. /// before MachineInstr::tieOperands().
  86. unsigned TiedTo : 4;
  87. /// IsDef - True if this is a def, false if this is a use of the register.
  88. /// This is only valid on register operands.
  89. ///
  90. unsigned IsDef : 1;
  91. /// IsImp - True if this is an implicit def or use, false if it is explicit.
  92. /// This is only valid on register opderands.
  93. ///
  94. unsigned IsImp : 1;
  95. /// IsDeadOrKill
  96. /// For uses: IsKill - Conservatively indicates the last use of a register
  97. /// on this path through the function. A register operand with true value of
  98. /// this flag must be the last use of the register, a register operand with
  99. /// false value may or may not be the last use of the register. After regalloc
  100. /// we can use recomputeLivenessFlags to get precise kill flags.
  101. /// For defs: IsDead - True if this register is never used by a subsequent
  102. /// instruction.
  103. /// This is only valid on register operands.
  104. unsigned IsDeadOrKill : 1;
  105. /// See isRenamable().
  106. unsigned IsRenamable : 1;
  107. /// IsUndef - True if this register operand reads an "undef" value, i.e. the
  108. /// read value doesn't matter. This flag can be set on both use and def
  109. /// operands. On a sub-register def operand, it refers to the part of the
  110. /// register that isn't written. On a full-register def operand, it is a
  111. /// noop. See readsReg().
  112. ///
  113. /// This is only valid on registers.
  114. ///
  115. /// Note that an instruction may have multiple <undef> operands referring to
  116. /// the same register. In that case, the instruction may depend on those
  117. /// operands reading the same dont-care value. For example:
  118. ///
  119. /// %1 = XOR undef %2, undef %2
  120. ///
  121. /// Any register can be used for %2, and its value doesn't matter, but
  122. /// the two operands must be the same register.
  123. ///
  124. unsigned IsUndef : 1;
  125. /// IsInternalRead - True if this operand reads a value that was defined
  126. /// inside the same instruction or bundle. This flag can be set on both use
  127. /// and def operands. On a sub-register def operand, it refers to the part
  128. /// of the register that isn't written. On a full-register def operand, it
  129. /// is a noop.
  130. ///
  131. /// When this flag is set, the instruction bundle must contain at least one
  132. /// other def of the register. If multiple instructions in the bundle define
  133. /// the register, the meaning is target-defined.
  134. unsigned IsInternalRead : 1;
  135. /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
  136. /// by the MachineInstr before all input registers are read. This is used to
  137. /// model the GCC inline asm '&' constraint modifier.
  138. unsigned IsEarlyClobber : 1;
  139. /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
  140. /// not a real instruction. Such uses should be ignored during codegen.
  141. unsigned IsDebug : 1;
  142. /// SmallContents - This really should be part of the Contents union, but
  143. /// lives out here so we can get a better packed struct.
  144. /// MO_Register: Register number.
  145. /// OffsetedInfo: Low bits of offset.
  146. union {
  147. unsigned RegNo; // For MO_Register.
  148. unsigned OffsetLo; // Matches Contents.OffsetedInfo.OffsetHi.
  149. } SmallContents;
  150. /// ParentMI - This is the instruction that this operand is embedded into.
  151. /// This is valid for all operand types, when the operand is in an instr.
  152. MachineInstr *ParentMI = nullptr;
  153. /// Contents union - This contains the payload for the various operand types.
  154. union ContentsUnion {
  155. ContentsUnion() {}
  156. MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
  157. const ConstantFP *CFP; // For MO_FPImmediate.
  158. const ConstantInt *CI; // For MO_CImmediate. Integers > 64bit.
  159. int64_t ImmVal; // For MO_Immediate.
  160. const uint32_t *RegMask; // For MO_RegisterMask and MO_RegisterLiveOut.
  161. const MDNode *MD; // For MO_Metadata.
  162. MCSymbol *Sym; // For MO_MCSymbol.
  163. unsigned CFIIndex; // For MO_CFI.
  164. Intrinsic::ID IntrinsicID; // For MO_IntrinsicID.
  165. unsigned Pred; // For MO_Predicate
  166. ArrayRef<int> ShuffleMask; // For MO_ShuffleMask
  167. struct { // For MO_Register.
  168. // Register number is in SmallContents.RegNo.
  169. MachineOperand *Prev; // Access list for register. See MRI.
  170. MachineOperand *Next;
  171. } Reg;
  172. struct { // For MO_DbgInstrRef.
  173. unsigned InstrIdx;
  174. unsigned OpIdx;
  175. } InstrRef;
  176. /// OffsetedInfo - This struct contains the offset and an object identifier.
  177. /// this represent the object as with an optional offset from it.
  178. struct {
  179. union {
  180. int Index; // For MO_*Index - The index itself.
  181. const char *SymbolName; // For MO_ExternalSymbol.
  182. const GlobalValue *GV; // For MO_GlobalAddress.
  183. const BlockAddress *BA; // For MO_BlockAddress.
  184. } Val;
  185. // Low bits of offset are in SmallContents.OffsetLo.
  186. int OffsetHi; // An offset from the object, high 32 bits.
  187. } OffsetedInfo;
  188. } Contents;
  189. explicit MachineOperand(MachineOperandType K)
  190. : OpKind(K), SubReg_TargetFlags(0) {
  191. // Assert that the layout is what we expect. It's easy to grow this object.
  192. static_assert(alignof(MachineOperand) <= alignof(int64_t),
  193. "MachineOperand shouldn't be more than 8 byte aligned");
  194. static_assert(sizeof(Contents) <= 2 * sizeof(void *),
  195. "Contents should be at most two pointers");
  196. static_assert(sizeof(MachineOperand) <=
  197. alignTo<alignof(int64_t)>(2 * sizeof(unsigned) +
  198. 3 * sizeof(void *)),
  199. "MachineOperand too big. Should be Kind, SmallContents, "
  200. "ParentMI, and Contents");
  201. }
  202. public:
  203. /// getType - Returns the MachineOperandType for this operand.
  204. ///
  205. MachineOperandType getType() const { return (MachineOperandType)OpKind; }
  206. unsigned getTargetFlags() const {
  207. return isReg() ? 0 : SubReg_TargetFlags;
  208. }
  209. void setTargetFlags(unsigned F) {
  210. assert(!isReg() && "Register operands can't have target flags");
  211. SubReg_TargetFlags = F;
  212. assert(SubReg_TargetFlags == F && "Target flags out of range");
  213. }
  214. void addTargetFlag(unsigned F) {
  215. assert(!isReg() && "Register operands can't have target flags");
  216. SubReg_TargetFlags |= F;
  217. assert((SubReg_TargetFlags & F) && "Target flags out of range");
  218. }
  219. /// getParent - Return the instruction that this operand belongs to.
  220. ///
  221. MachineInstr *getParent() { return ParentMI; }
  222. const MachineInstr *getParent() const { return ParentMI; }
  223. /// clearParent - Reset the parent pointer.
  224. ///
  225. /// The MachineOperand copy constructor also copies ParentMI, expecting the
  226. /// original to be deleted. If a MachineOperand is ever stored outside a
  227. /// MachineInstr, the parent pointer must be cleared.
  228. ///
  229. /// Never call clearParent() on an operand in a MachineInstr.
  230. ///
  231. void clearParent() { ParentMI = nullptr; }
  232. /// Print a subreg index operand.
  233. /// MO_Immediate operands can also be subreg idices. If it's the case, the
  234. /// subreg index name will be printed. MachineInstr::isOperandSubregIdx can be
  235. /// called to check this.
  236. static void printSubRegIdx(raw_ostream &OS, uint64_t Index,
  237. const TargetRegisterInfo *TRI);
  238. /// Print operand target flags.
  239. static void printTargetFlags(raw_ostream& OS, const MachineOperand &Op);
  240. /// Print a MCSymbol as an operand.
  241. static void printSymbol(raw_ostream &OS, MCSymbol &Sym);
  242. /// Print a stack object reference.
  243. static void printStackObjectReference(raw_ostream &OS, unsigned FrameIndex,
  244. bool IsFixed, StringRef Name);
  245. /// Print the offset with explicit +/- signs.
  246. static void printOperandOffset(raw_ostream &OS, int64_t Offset);
  247. /// Print an IRSlotNumber.
  248. static void printIRSlotNumber(raw_ostream &OS, int Slot);
  249. /// Print the MachineOperand to \p os.
  250. /// Providing a valid \p TRI and \p IntrinsicInfo results in a more
  251. /// target-specific printing. If \p TRI and \p IntrinsicInfo are null, the
  252. /// function will try to pick it up from the parent.
  253. void print(raw_ostream &os, const TargetRegisterInfo *TRI = nullptr,
  254. const TargetIntrinsicInfo *IntrinsicInfo = nullptr) const;
  255. /// More complex way of printing a MachineOperand.
  256. /// \param TypeToPrint specifies the generic type to be printed on uses and
  257. /// defs. It can be determined using MachineInstr::getTypeToPrint.
  258. /// \param OpIdx - specifies the index of the operand in machine instruction.
  259. /// This will be used by target dependent MIR formatter. Could be std::nullopt
  260. /// if the index is unknown, e.g. called by dump().
  261. /// \param PrintDef - whether we want to print `def` on an operand which
  262. /// isDef. Sometimes, if the operand is printed before '=', we don't print
  263. /// `def`.
  264. /// \param IsStandalone - whether we want a verbose output of the MO. This
  265. /// prints extra information that can be easily inferred when printing the
  266. /// whole function, but not when printing only a fragment of it.
  267. /// \param ShouldPrintRegisterTies - whether we want to print register ties.
  268. /// Sometimes they are easily determined by the instruction's descriptor
  269. /// (MachineInstr::hasComplexRegiterTies can determine if it's needed).
  270. /// \param TiedOperandIdx - if we need to print register ties this needs to
  271. /// provide the index of the tied register. If not, it will be ignored.
  272. /// \param TRI - provide more target-specific information to the printer.
  273. /// Unlike the previous function, this one will not try and get the
  274. /// information from it's parent.
  275. /// \param IntrinsicInfo - same as \p TRI.
  276. void print(raw_ostream &os, ModuleSlotTracker &MST, LLT TypeToPrint,
  277. std::optional<unsigned> OpIdx, bool PrintDef, bool IsStandalone,
  278. bool ShouldPrintRegisterTies, unsigned TiedOperandIdx,
  279. const TargetRegisterInfo *TRI,
  280. const TargetIntrinsicInfo *IntrinsicInfo) const;
  281. /// Same as print(os, TRI, IntrinsicInfo), but allows to specify the low-level
  282. /// type to be printed the same way the full version of print(...) does it.
  283. void print(raw_ostream &os, LLT TypeToPrint,
  284. const TargetRegisterInfo *TRI = nullptr,
  285. const TargetIntrinsicInfo *IntrinsicInfo = nullptr) const;
  286. void dump() const;
  287. //===--------------------------------------------------------------------===//
  288. // Accessors that tell you what kind of MachineOperand you're looking at.
  289. //===--------------------------------------------------------------------===//
  290. /// isReg - Tests if this is a MO_Register operand.
  291. bool isReg() const { return OpKind == MO_Register; }
  292. /// isImm - Tests if this is a MO_Immediate operand.
  293. bool isImm() const { return OpKind == MO_Immediate; }
  294. /// isCImm - Test if this is a MO_CImmediate operand.
  295. bool isCImm() const { return OpKind == MO_CImmediate; }
  296. /// isFPImm - Tests if this is a MO_FPImmediate operand.
  297. bool isFPImm() const { return OpKind == MO_FPImmediate; }
  298. /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
  299. bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
  300. /// isFI - Tests if this is a MO_FrameIndex operand.
  301. bool isFI() const { return OpKind == MO_FrameIndex; }
  302. /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
  303. bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
  304. /// isTargetIndex - Tests if this is a MO_TargetIndex operand.
  305. bool isTargetIndex() const { return OpKind == MO_TargetIndex; }
  306. /// isJTI - Tests if this is a MO_JumpTableIndex operand.
  307. bool isJTI() const { return OpKind == MO_JumpTableIndex; }
  308. /// isGlobal - Tests if this is a MO_GlobalAddress operand.
  309. bool isGlobal() const { return OpKind == MO_GlobalAddress; }
  310. /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
  311. bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
  312. /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
  313. bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
  314. /// isRegMask - Tests if this is a MO_RegisterMask operand.
  315. bool isRegMask() const { return OpKind == MO_RegisterMask; }
  316. /// isRegLiveOut - Tests if this is a MO_RegisterLiveOut operand.
  317. bool isRegLiveOut() const { return OpKind == MO_RegisterLiveOut; }
  318. /// isMetadata - Tests if this is a MO_Metadata operand.
  319. bool isMetadata() const { return OpKind == MO_Metadata; }
  320. bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
  321. bool isDbgInstrRef() const { return OpKind == MO_DbgInstrRef; }
  322. bool isCFIIndex() const { return OpKind == MO_CFIIndex; }
  323. bool isIntrinsicID() const { return OpKind == MO_IntrinsicID; }
  324. bool isPredicate() const { return OpKind == MO_Predicate; }
  325. bool isShuffleMask() const { return OpKind == MO_ShuffleMask; }
  326. //===--------------------------------------------------------------------===//
  327. // Accessors for Register Operands
  328. //===--------------------------------------------------------------------===//
  329. /// getReg - Returns the register number.
  330. Register getReg() const {
  331. assert(isReg() && "This is not a register operand!");
  332. return Register(SmallContents.RegNo);
  333. }
  334. unsigned getSubReg() const {
  335. assert(isReg() && "Wrong MachineOperand accessor");
  336. return SubReg_TargetFlags;
  337. }
  338. bool isUse() const {
  339. assert(isReg() && "Wrong MachineOperand accessor");
  340. return !IsDef;
  341. }
  342. bool isDef() const {
  343. assert(isReg() && "Wrong MachineOperand accessor");
  344. return IsDef;
  345. }
  346. bool isImplicit() const {
  347. assert(isReg() && "Wrong MachineOperand accessor");
  348. return IsImp;
  349. }
  350. bool isDead() const {
  351. assert(isReg() && "Wrong MachineOperand accessor");
  352. return IsDeadOrKill & IsDef;
  353. }
  354. bool isKill() const {
  355. assert(isReg() && "Wrong MachineOperand accessor");
  356. return IsDeadOrKill & !IsDef;
  357. }
  358. bool isUndef() const {
  359. assert(isReg() && "Wrong MachineOperand accessor");
  360. return IsUndef;
  361. }
  362. /// isRenamable - Returns true if this register may be renamed, i.e. it does
  363. /// not generate a value that is somehow read in a way that is not represented
  364. /// by the Machine IR (e.g. to meet an ABI or ISA requirement). This is only
  365. /// valid on physical register operands. Virtual registers are assumed to
  366. /// always be renamable regardless of the value of this field.
  367. ///
  368. /// Operands that are renamable can freely be changed to any other register
  369. /// that is a member of the register class returned by
  370. /// MI->getRegClassConstraint().
  371. ///
  372. /// isRenamable can return false for several different reasons:
  373. ///
  374. /// - ABI constraints (since liveness is not always precisely modeled). We
  375. /// conservatively handle these cases by setting all physical register
  376. /// operands that didn’t start out as virtual regs to not be renamable.
  377. /// Also any physical register operands created after register allocation or
  378. /// whose register is changed after register allocation will not be
  379. /// renamable. This state is tracked in the MachineOperand::IsRenamable
  380. /// bit.
  381. ///
  382. /// - Opcode/target constraints: for opcodes that have complex register class
  383. /// requirements (e.g. that depend on other operands/instructions), we set
  384. /// hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq in the machine opcode
  385. /// description. Operands belonging to instructions with opcodes that are
  386. /// marked hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq return false from
  387. /// isRenamable(). Additionally, the AllowRegisterRenaming target property
  388. /// prevents any operands from being marked renamable for targets that don't
  389. /// have detailed opcode hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq
  390. /// values.
  391. bool isRenamable() const;
  392. bool isInternalRead() const {
  393. assert(isReg() && "Wrong MachineOperand accessor");
  394. return IsInternalRead;
  395. }
  396. bool isEarlyClobber() const {
  397. assert(isReg() && "Wrong MachineOperand accessor");
  398. return IsEarlyClobber;
  399. }
  400. bool isTied() const {
  401. assert(isReg() && "Wrong MachineOperand accessor");
  402. return TiedTo;
  403. }
  404. bool isDebug() const {
  405. assert(isReg() && "Wrong MachineOperand accessor");
  406. return IsDebug;
  407. }
  408. /// readsReg - Returns true if this operand reads the previous value of its
  409. /// register. A use operand with the <undef> flag set doesn't read its
  410. /// register. A sub-register def implicitly reads the other parts of the
  411. /// register being redefined unless the <undef> flag is set.
  412. ///
  413. /// This refers to reading the register value from before the current
  414. /// instruction or bundle. Internal bundle reads are not included.
  415. bool readsReg() const {
  416. assert(isReg() && "Wrong MachineOperand accessor");
  417. return !isUndef() && !isInternalRead() && (isUse() || getSubReg());
  418. }
  419. /// Return true if this operand can validly be appended to an arbitrary
  420. /// operand list. i.e. this behaves like an implicit operand.
  421. bool isValidExcessOperand() const {
  422. if ((isReg() && isImplicit()) || isRegMask())
  423. return true;
  424. // Debug operands
  425. return isMetadata() || isMCSymbol();
  426. }
  427. //===--------------------------------------------------------------------===//
  428. // Mutators for Register Operands
  429. //===--------------------------------------------------------------------===//
  430. /// Change the register this operand corresponds to.
  431. ///
  432. void setReg(Register Reg);
  433. void setSubReg(unsigned subReg) {
  434. assert(isReg() && "Wrong MachineOperand mutator");
  435. SubReg_TargetFlags = subReg;
  436. assert(SubReg_TargetFlags == subReg && "SubReg out of range");
  437. }
  438. /// substVirtReg - Substitute the current register with the virtual
  439. /// subregister Reg:SubReg. Take any existing SubReg index into account,
  440. /// using TargetRegisterInfo to compose the subreg indices if necessary.
  441. /// Reg must be a virtual register, SubIdx can be 0.
  442. ///
  443. void substVirtReg(Register Reg, unsigned SubIdx, const TargetRegisterInfo&);
  444. /// substPhysReg - Substitute the current register with the physical register
  445. /// Reg, taking any existing SubReg into account. For instance,
  446. /// substPhysReg(%eax) will change %reg1024:sub_8bit to %al.
  447. ///
  448. void substPhysReg(MCRegister Reg, const TargetRegisterInfo&);
  449. void setIsUse(bool Val = true) { setIsDef(!Val); }
  450. /// Change a def to a use, or a use to a def.
  451. void setIsDef(bool Val = true);
  452. void setImplicit(bool Val = true) {
  453. assert(isReg() && "Wrong MachineOperand mutator");
  454. IsImp = Val;
  455. }
  456. void setIsKill(bool Val = true) {
  457. assert(isReg() && !IsDef && "Wrong MachineOperand mutator");
  458. assert((!Val || !isDebug()) && "Marking a debug operation as kill");
  459. IsDeadOrKill = Val;
  460. }
  461. void setIsDead(bool Val = true) {
  462. assert(isReg() && IsDef && "Wrong MachineOperand mutator");
  463. IsDeadOrKill = Val;
  464. }
  465. void setIsUndef(bool Val = true) {
  466. assert(isReg() && "Wrong MachineOperand mutator");
  467. IsUndef = Val;
  468. }
  469. void setIsRenamable(bool Val = true);
  470. void setIsInternalRead(bool Val = true) {
  471. assert(isReg() && "Wrong MachineOperand mutator");
  472. IsInternalRead = Val;
  473. }
  474. void setIsEarlyClobber(bool Val = true) {
  475. assert(isReg() && IsDef && "Wrong MachineOperand mutator");
  476. IsEarlyClobber = Val;
  477. }
  478. void setIsDebug(bool Val = true) {
  479. assert(isReg() && !IsDef && "Wrong MachineOperand mutator");
  480. IsDebug = Val;
  481. }
  482. //===--------------------------------------------------------------------===//
  483. // Accessors for various operand types.
  484. //===--------------------------------------------------------------------===//
  485. int64_t getImm() const {
  486. assert(isImm() && "Wrong MachineOperand accessor");
  487. return Contents.ImmVal;
  488. }
  489. const ConstantInt *getCImm() const {
  490. assert(isCImm() && "Wrong MachineOperand accessor");
  491. return Contents.CI;
  492. }
  493. const ConstantFP *getFPImm() const {
  494. assert(isFPImm() && "Wrong MachineOperand accessor");
  495. return Contents.CFP;
  496. }
  497. MachineBasicBlock *getMBB() const {
  498. assert(isMBB() && "Wrong MachineOperand accessor");
  499. return Contents.MBB;
  500. }
  501. int getIndex() const {
  502. assert((isFI() || isCPI() || isTargetIndex() || isJTI()) &&
  503. "Wrong MachineOperand accessor");
  504. return Contents.OffsetedInfo.Val.Index;
  505. }
  506. const GlobalValue *getGlobal() const {
  507. assert(isGlobal() && "Wrong MachineOperand accessor");
  508. return Contents.OffsetedInfo.Val.GV;
  509. }
  510. const BlockAddress *getBlockAddress() const {
  511. assert(isBlockAddress() && "Wrong MachineOperand accessor");
  512. return Contents.OffsetedInfo.Val.BA;
  513. }
  514. MCSymbol *getMCSymbol() const {
  515. assert(isMCSymbol() && "Wrong MachineOperand accessor");
  516. return Contents.Sym;
  517. }
  518. unsigned getInstrRefInstrIndex() const {
  519. assert(isDbgInstrRef() && "Wrong MachineOperand accessor");
  520. return Contents.InstrRef.InstrIdx;
  521. }
  522. unsigned getInstrRefOpIndex() const {
  523. assert(isDbgInstrRef() && "Wrong MachineOperand accessor");
  524. return Contents.InstrRef.OpIdx;
  525. }
  526. unsigned getCFIIndex() const {
  527. assert(isCFIIndex() && "Wrong MachineOperand accessor");
  528. return Contents.CFIIndex;
  529. }
  530. Intrinsic::ID getIntrinsicID() const {
  531. assert(isIntrinsicID() && "Wrong MachineOperand accessor");
  532. return Contents.IntrinsicID;
  533. }
  534. unsigned getPredicate() const {
  535. assert(isPredicate() && "Wrong MachineOperand accessor");
  536. return Contents.Pred;
  537. }
  538. ArrayRef<int> getShuffleMask() const {
  539. assert(isShuffleMask() && "Wrong MachineOperand accessor");
  540. return Contents.ShuffleMask;
  541. }
  542. /// Return the offset from the symbol in this operand. This always returns 0
  543. /// for ExternalSymbol operands.
  544. int64_t getOffset() const {
  545. assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() ||
  546. isTargetIndex() || isBlockAddress()) &&
  547. "Wrong MachineOperand accessor");
  548. return int64_t(uint64_t(Contents.OffsetedInfo.OffsetHi) << 32) |
  549. SmallContents.OffsetLo;
  550. }
  551. const char *getSymbolName() const {
  552. assert(isSymbol() && "Wrong MachineOperand accessor");
  553. return Contents.OffsetedInfo.Val.SymbolName;
  554. }
  555. /// clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
  556. /// It is sometimes necessary to detach the register mask pointer from its
  557. /// machine operand. This static method can be used for such detached bit
  558. /// mask pointers.
  559. static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg) {
  560. // See TargetRegisterInfo.h.
  561. assert(PhysReg < (1u << 30) && "Not a physical register");
  562. return !(RegMask[PhysReg / 32] & (1u << PhysReg % 32));
  563. }
  564. /// clobbersPhysReg - Returns true if this RegMask operand clobbers PhysReg.
  565. bool clobbersPhysReg(MCRegister PhysReg) const {
  566. return clobbersPhysReg(getRegMask(), PhysReg);
  567. }
  568. /// getRegMask - Returns a bit mask of registers preserved by this RegMask
  569. /// operand.
  570. const uint32_t *getRegMask() const {
  571. assert(isRegMask() && "Wrong MachineOperand accessor");
  572. return Contents.RegMask;
  573. }
  574. /// Returns number of elements needed for a regmask array.
  575. static unsigned getRegMaskSize(unsigned NumRegs) {
  576. return (NumRegs + 31) / 32;
  577. }
  578. /// getRegLiveOut - Returns a bit mask of live-out registers.
  579. const uint32_t *getRegLiveOut() const {
  580. assert(isRegLiveOut() && "Wrong MachineOperand accessor");
  581. return Contents.RegMask;
  582. }
  583. const MDNode *getMetadata() const {
  584. assert(isMetadata() && "Wrong MachineOperand accessor");
  585. return Contents.MD;
  586. }
  587. //===--------------------------------------------------------------------===//
  588. // Mutators for various operand types.
  589. //===--------------------------------------------------------------------===//
  590. void setImm(int64_t immVal) {
  591. assert(isImm() && "Wrong MachineOperand mutator");
  592. Contents.ImmVal = immVal;
  593. }
  594. void setCImm(const ConstantInt *CI) {
  595. assert(isCImm() && "Wrong MachineOperand mutator");
  596. Contents.CI = CI;
  597. }
  598. void setFPImm(const ConstantFP *CFP) {
  599. assert(isFPImm() && "Wrong MachineOperand mutator");
  600. Contents.CFP = CFP;
  601. }
  602. void setOffset(int64_t Offset) {
  603. assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() ||
  604. isTargetIndex() || isBlockAddress()) &&
  605. "Wrong MachineOperand mutator");
  606. SmallContents.OffsetLo = unsigned(Offset);
  607. Contents.OffsetedInfo.OffsetHi = int(Offset >> 32);
  608. }
  609. void setIndex(int Idx) {
  610. assert((isFI() || isCPI() || isTargetIndex() || isJTI()) &&
  611. "Wrong MachineOperand mutator");
  612. Contents.OffsetedInfo.Val.Index = Idx;
  613. }
  614. void setMetadata(const MDNode *MD) {
  615. assert(isMetadata() && "Wrong MachineOperand mutator");
  616. Contents.MD = MD;
  617. }
  618. void setInstrRefInstrIndex(unsigned InstrIdx) {
  619. assert(isDbgInstrRef() && "Wrong MachineOperand mutator");
  620. Contents.InstrRef.InstrIdx = InstrIdx;
  621. }
  622. void setInstrRefOpIndex(unsigned OpIdx) {
  623. assert(isDbgInstrRef() && "Wrong MachineOperand mutator");
  624. Contents.InstrRef.OpIdx = OpIdx;
  625. }
  626. void setMBB(MachineBasicBlock *MBB) {
  627. assert(isMBB() && "Wrong MachineOperand mutator");
  628. Contents.MBB = MBB;
  629. }
  630. /// Sets value of register mask operand referencing Mask. The
  631. /// operand does not take ownership of the memory referenced by Mask, it must
  632. /// remain valid for the lifetime of the operand. See CreateRegMask().
  633. /// Any physreg with a 0 bit in the mask is clobbered by the instruction.
  634. void setRegMask(const uint32_t *RegMaskPtr) {
  635. assert(isRegMask() && "Wrong MachineOperand mutator");
  636. Contents.RegMask = RegMaskPtr;
  637. }
  638. void setIntrinsicID(Intrinsic::ID IID) {
  639. assert(isIntrinsicID() && "Wrong MachineOperand mutator");
  640. Contents.IntrinsicID = IID;
  641. }
  642. void setPredicate(unsigned Predicate) {
  643. assert(isPredicate() && "Wrong MachineOperand mutator");
  644. Contents.Pred = Predicate;
  645. }
  646. //===--------------------------------------------------------------------===//
  647. // Other methods.
  648. //===--------------------------------------------------------------------===//
  649. /// Returns true if this operand is identical to the specified operand except
  650. /// for liveness related flags (isKill, isUndef and isDead). Note that this
  651. /// should stay in sync with the hash_value overload below.
  652. bool isIdenticalTo(const MachineOperand &Other) const;
  653. /// MachineOperand hash_value overload.
  654. ///
  655. /// Note that this includes the same information in the hash that
  656. /// isIdenticalTo uses for comparison. It is thus suited for use in hash
  657. /// tables which use that function for equality comparisons only. This must
  658. /// stay exactly in sync with isIdenticalTo above.
  659. friend hash_code hash_value(const MachineOperand &MO);
  660. /// ChangeToImmediate - Replace this operand with a new immediate operand of
  661. /// the specified value. If an operand is known to be an immediate already,
  662. /// the setImm method should be used.
  663. void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags = 0);
  664. /// ChangeToFPImmediate - Replace this operand with a new FP immediate operand
  665. /// of the specified value. If an operand is known to be an FP immediate
  666. /// already, the setFPImm method should be used.
  667. void ChangeToFPImmediate(const ConstantFP *FPImm, unsigned TargetFlags = 0);
  668. /// ChangeToES - Replace this operand with a new external symbol operand.
  669. void ChangeToES(const char *SymName, unsigned TargetFlags = 0);
  670. /// ChangeToGA - Replace this operand with a new global address operand.
  671. void ChangeToGA(const GlobalValue *GV, int64_t Offset,
  672. unsigned TargetFlags = 0);
  673. /// ChangeToMCSymbol - Replace this operand with a new MC symbol operand.
  674. void ChangeToMCSymbol(MCSymbol *Sym, unsigned TargetFlags = 0);
  675. /// Replace this operand with a frame index.
  676. void ChangeToFrameIndex(int Idx, unsigned TargetFlags = 0);
  677. /// Replace this operand with a target index.
  678. void ChangeToTargetIndex(unsigned Idx, int64_t Offset,
  679. unsigned TargetFlags = 0);
  680. /// Replace this operand with an Instruction Reference.
  681. void ChangeToDbgInstrRef(unsigned InstrIdx, unsigned OpIdx,
  682. unsigned TargetFlags = 0);
  683. /// ChangeToRegister - Replace this operand with a new register operand of
  684. /// the specified value. If an operand is known to be an register already,
  685. /// the setReg method should be used.
  686. void ChangeToRegister(Register Reg, bool isDef, bool isImp = false,
  687. bool isKill = false, bool isDead = false,
  688. bool isUndef = false, bool isDebug = false);
  689. /// getTargetIndexName - If this MachineOperand is a TargetIndex that has a
  690. /// name, attempt to get the name. Returns nullptr if the TargetIndex does not
  691. /// have a name. Asserts if MO is not a TargetIndex.
  692. const char *getTargetIndexName() const;
  693. //===--------------------------------------------------------------------===//
  694. // Construction methods.
  695. //===--------------------------------------------------------------------===//
  696. static MachineOperand CreateImm(int64_t Val) {
  697. MachineOperand Op(MachineOperand::MO_Immediate);
  698. Op.setImm(Val);
  699. return Op;
  700. }
  701. static MachineOperand CreateCImm(const ConstantInt *CI) {
  702. MachineOperand Op(MachineOperand::MO_CImmediate);
  703. Op.Contents.CI = CI;
  704. return Op;
  705. }
  706. static MachineOperand CreateFPImm(const ConstantFP *CFP) {
  707. MachineOperand Op(MachineOperand::MO_FPImmediate);
  708. Op.Contents.CFP = CFP;
  709. return Op;
  710. }
  711. static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp = false,
  712. bool isKill = false, bool isDead = false,
  713. bool isUndef = false,
  714. bool isEarlyClobber = false,
  715. unsigned SubReg = 0, bool isDebug = false,
  716. bool isInternalRead = false,
  717. bool isRenamable = false) {
  718. assert(!(isDead && !isDef) && "Dead flag on non-def");
  719. assert(!(isKill && isDef) && "Kill flag on def");
  720. MachineOperand Op(MachineOperand::MO_Register);
  721. Op.IsDef = isDef;
  722. Op.IsImp = isImp;
  723. Op.IsDeadOrKill = isKill | isDead;
  724. Op.IsRenamable = isRenamable;
  725. Op.IsUndef = isUndef;
  726. Op.IsInternalRead = isInternalRead;
  727. Op.IsEarlyClobber = isEarlyClobber;
  728. Op.TiedTo = 0;
  729. Op.IsDebug = isDebug;
  730. Op.SmallContents.RegNo = Reg;
  731. Op.Contents.Reg.Prev = nullptr;
  732. Op.Contents.Reg.Next = nullptr;
  733. Op.setSubReg(SubReg);
  734. return Op;
  735. }
  736. static MachineOperand CreateMBB(MachineBasicBlock *MBB,
  737. unsigned TargetFlags = 0) {
  738. MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
  739. Op.setMBB(MBB);
  740. Op.setTargetFlags(TargetFlags);
  741. return Op;
  742. }
  743. static MachineOperand CreateFI(int Idx) {
  744. MachineOperand Op(MachineOperand::MO_FrameIndex);
  745. Op.setIndex(Idx);
  746. return Op;
  747. }
  748. static MachineOperand CreateCPI(unsigned Idx, int Offset,
  749. unsigned TargetFlags = 0) {
  750. MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
  751. Op.setIndex(Idx);
  752. Op.setOffset(Offset);
  753. Op.setTargetFlags(TargetFlags);
  754. return Op;
  755. }
  756. static MachineOperand CreateTargetIndex(unsigned Idx, int64_t Offset,
  757. unsigned TargetFlags = 0) {
  758. MachineOperand Op(MachineOperand::MO_TargetIndex);
  759. Op.setIndex(Idx);
  760. Op.setOffset(Offset);
  761. Op.setTargetFlags(TargetFlags);
  762. return Op;
  763. }
  764. static MachineOperand CreateJTI(unsigned Idx, unsigned TargetFlags = 0) {
  765. MachineOperand Op(MachineOperand::MO_JumpTableIndex);
  766. Op.setIndex(Idx);
  767. Op.setTargetFlags(TargetFlags);
  768. return Op;
  769. }
  770. static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset,
  771. unsigned TargetFlags = 0) {
  772. MachineOperand Op(MachineOperand::MO_GlobalAddress);
  773. Op.Contents.OffsetedInfo.Val.GV = GV;
  774. Op.setOffset(Offset);
  775. Op.setTargetFlags(TargetFlags);
  776. return Op;
  777. }
  778. static MachineOperand CreateES(const char *SymName,
  779. unsigned TargetFlags = 0) {
  780. MachineOperand Op(MachineOperand::MO_ExternalSymbol);
  781. Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
  782. Op.setOffset(0); // Offset is always 0.
  783. Op.setTargetFlags(TargetFlags);
  784. return Op;
  785. }
  786. static MachineOperand CreateBA(const BlockAddress *BA, int64_t Offset,
  787. unsigned TargetFlags = 0) {
  788. MachineOperand Op(MachineOperand::MO_BlockAddress);
  789. Op.Contents.OffsetedInfo.Val.BA = BA;
  790. Op.setOffset(Offset);
  791. Op.setTargetFlags(TargetFlags);
  792. return Op;
  793. }
  794. /// CreateRegMask - Creates a register mask operand referencing Mask. The
  795. /// operand does not take ownership of the memory referenced by Mask, it
  796. /// must remain valid for the lifetime of the operand.
  797. ///
  798. /// A RegMask operand represents a set of non-clobbered physical registers
  799. /// on an instruction that clobbers many registers, typically a call. The
  800. /// bit mask has a bit set for each physreg that is preserved by this
  801. /// instruction, as described in the documentation for
  802. /// TargetRegisterInfo::getCallPreservedMask().
  803. ///
  804. /// Any physreg with a 0 bit in the mask is clobbered by the instruction.
  805. ///
  806. static MachineOperand CreateRegMask(const uint32_t *Mask) {
  807. assert(Mask && "Missing register mask");
  808. MachineOperand Op(MachineOperand::MO_RegisterMask);
  809. Op.Contents.RegMask = Mask;
  810. return Op;
  811. }
  812. static MachineOperand CreateRegLiveOut(const uint32_t *Mask) {
  813. assert(Mask && "Missing live-out register mask");
  814. MachineOperand Op(MachineOperand::MO_RegisterLiveOut);
  815. Op.Contents.RegMask = Mask;
  816. return Op;
  817. }
  818. static MachineOperand CreateMetadata(const MDNode *Meta) {
  819. MachineOperand Op(MachineOperand::MO_Metadata);
  820. Op.Contents.MD = Meta;
  821. return Op;
  822. }
  823. static MachineOperand CreateMCSymbol(MCSymbol *Sym,
  824. unsigned TargetFlags = 0) {
  825. MachineOperand Op(MachineOperand::MO_MCSymbol);
  826. Op.Contents.Sym = Sym;
  827. Op.setOffset(0);
  828. Op.setTargetFlags(TargetFlags);
  829. return Op;
  830. }
  831. static MachineOperand CreateDbgInstrRef(unsigned InstrIdx, unsigned OpIdx) {
  832. MachineOperand Op(MachineOperand::MO_DbgInstrRef);
  833. Op.Contents.InstrRef.InstrIdx = InstrIdx;
  834. Op.Contents.InstrRef.OpIdx = OpIdx;
  835. return Op;
  836. }
  837. static MachineOperand CreateCFIIndex(unsigned CFIIndex) {
  838. MachineOperand Op(MachineOperand::MO_CFIIndex);
  839. Op.Contents.CFIIndex = CFIIndex;
  840. return Op;
  841. }
  842. static MachineOperand CreateIntrinsicID(Intrinsic::ID ID) {
  843. MachineOperand Op(MachineOperand::MO_IntrinsicID);
  844. Op.Contents.IntrinsicID = ID;
  845. return Op;
  846. }
  847. static MachineOperand CreatePredicate(unsigned Pred) {
  848. MachineOperand Op(MachineOperand::MO_Predicate);
  849. Op.Contents.Pred = Pred;
  850. return Op;
  851. }
  852. static MachineOperand CreateShuffleMask(ArrayRef<int> Mask) {
  853. MachineOperand Op(MachineOperand::MO_ShuffleMask);
  854. Op.Contents.ShuffleMask = Mask;
  855. return Op;
  856. }
  857. friend class MachineInstr;
  858. friend class MachineRegisterInfo;
  859. private:
  860. // If this operand is currently a register operand, and if this is in a
  861. // function, deregister the operand from the register's use/def list.
  862. void removeRegFromUses();
  863. /// Artificial kinds for DenseMap usage.
  864. enum : unsigned char {
  865. MO_Empty = MO_Last + 1,
  866. MO_Tombstone,
  867. };
  868. friend struct DenseMapInfo<MachineOperand>;
  869. //===--------------------------------------------------------------------===//
  870. // Methods for handling register use/def lists.
  871. //===--------------------------------------------------------------------===//
  872. /// isOnRegUseList - Return true if this operand is on a register use/def
  873. /// list or false if not. This can only be called for register operands
  874. /// that are part of a machine instruction.
  875. bool isOnRegUseList() const {
  876. assert(isReg() && "Can only add reg operand to use lists");
  877. return Contents.Reg.Prev != nullptr;
  878. }
  879. };
  880. template <> struct DenseMapInfo<MachineOperand> {
  881. static MachineOperand getEmptyKey() {
  882. return MachineOperand(static_cast<MachineOperand::MachineOperandType>(
  883. MachineOperand::MO_Empty));
  884. }
  885. static MachineOperand getTombstoneKey() {
  886. return MachineOperand(static_cast<MachineOperand::MachineOperandType>(
  887. MachineOperand::MO_Tombstone));
  888. }
  889. static unsigned getHashValue(const MachineOperand &MO) {
  890. return hash_value(MO);
  891. }
  892. static bool isEqual(const MachineOperand &LHS, const MachineOperand &RHS) {
  893. if (LHS.getType() == static_cast<MachineOperand::MachineOperandType>(
  894. MachineOperand::MO_Empty) ||
  895. LHS.getType() == static_cast<MachineOperand::MachineOperandType>(
  896. MachineOperand::MO_Tombstone))
  897. return LHS.getType() == RHS.getType();
  898. return LHS.isIdenticalTo(RHS);
  899. }
  900. };
  901. inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand &MO) {
  902. MO.print(OS);
  903. return OS;
  904. }
  905. // See friend declaration above. This additional declaration is required in
  906. // order to compile LLVM with IBM xlC compiler.
  907. hash_code hash_value(const MachineOperand &MO);
  908. } // namespace llvm
  909. #endif
  910. #ifdef __GNUC__
  911. #pragma GCC diagnostic pop
  912. #endif