MachineOperand.h 38 KB

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