MachineOperand.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. //===-- llvm/CodeGen/MachineOperand.h - MachineOperand class ----*- 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 contains the declaration of the MachineOperand class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_MACHINEOPERAND_H
  14. #define LLVM_CODEGEN_MACHINEOPERAND_H
  15. #include "llvm/Support/DataTypes.h"
  16. #include <cassert>
  17. namespace llvm {
  18. class BlockAddress;
  19. class ConstantFP;
  20. class ConstantInt;
  21. class GlobalValue;
  22. class MachineBasicBlock;
  23. class MachineInstr;
  24. class MachineRegisterInfo;
  25. class MDNode;
  26. class ModuleSlotTracker;
  27. class TargetMachine;
  28. class TargetRegisterInfo;
  29. class hash_code;
  30. class raw_ostream;
  31. class MCSymbol;
  32. /// MachineOperand class - Representation of each machine instruction operand.
  33. ///
  34. /// This class isn't a POD type because it has a private constructor, but its
  35. /// destructor must be trivial. Functions like MachineInstr::addOperand(),
  36. /// MachineRegisterInfo::moveOperands(), and MF::DeleteMachineInstr() depend on
  37. /// not having to call the MachineOperand destructor.
  38. ///
  39. class MachineOperand {
  40. public:
  41. enum MachineOperandType : unsigned char {
  42. MO_Register, ///< Register operand.
  43. MO_Immediate, ///< Immediate operand
  44. MO_CImmediate, ///< Immediate >64bit operand
  45. MO_FPImmediate, ///< Floating-point immediate operand
  46. MO_MachineBasicBlock, ///< MachineBasicBlock reference
  47. MO_FrameIndex, ///< Abstract Stack Frame Index
  48. MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool
  49. MO_TargetIndex, ///< Target-dependent index+offset operand.
  50. MO_JumpTableIndex, ///< Address of indexed Jump Table for switch
  51. MO_ExternalSymbol, ///< Name of external global symbol
  52. MO_GlobalAddress, ///< Address of a global value
  53. MO_BlockAddress, ///< Address of a basic block
  54. MO_RegisterMask, ///< Mask of preserved registers.
  55. MO_RegisterLiveOut, ///< Mask of live-out registers.
  56. MO_Metadata, ///< Metadata reference (for debug info)
  57. MO_MCSymbol, ///< MCSymbol reference (for debug/eh info)
  58. MO_CFIIndex ///< MCCFIInstruction index.
  59. };
  60. private:
  61. /// OpKind - Specify what kind of operand this is. This discriminates the
  62. /// union.
  63. MachineOperandType OpKind : 8;
  64. /// Subregister number for MO_Register. A value of 0 indicates the
  65. /// MO_Register has no subReg.
  66. ///
  67. /// For all other kinds of operands, this field holds target-specific flags.
  68. unsigned SubReg_TargetFlags : 12;
  69. /// TiedTo - Non-zero when this register operand is tied to another register
  70. /// operand. The encoding of this field is described in the block comment
  71. /// before MachineInstr::tieOperands().
  72. unsigned char TiedTo : 4;
  73. /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
  74. /// operands.
  75. /// IsDef - True if this is a def, false if this is a use of the register.
  76. ///
  77. bool IsDef : 1;
  78. /// IsImp - True if this is an implicit def or use, false if it is explicit.
  79. ///
  80. bool IsImp : 1;
  81. /// IsKill - True if this instruction is the last use of the register on this
  82. /// path through the function. This is only valid on uses of registers.
  83. bool IsKill : 1;
  84. /// IsDead - True if this register is never used by a subsequent instruction.
  85. /// This is only valid on definitions of registers.
  86. bool IsDead : 1;
  87. /// IsUndef - True if this register operand reads an "undef" value, i.e. the
  88. /// read value doesn't matter. This flag can be set on both use and def
  89. /// operands. On a sub-register def operand, it refers to the part of the
  90. /// register that isn't written. On a full-register def operand, it is a
  91. /// noop. See readsReg().
  92. ///
  93. /// This is only valid on registers.
  94. ///
  95. /// Note that an instruction may have multiple <undef> operands referring to
  96. /// the same register. In that case, the instruction may depend on those
  97. /// operands reading the same dont-care value. For example:
  98. ///
  99. /// %vreg1<def> = XOR %vreg2<undef>, %vreg2<undef>
  100. ///
  101. /// Any register can be used for %vreg2, and its value doesn't matter, but
  102. /// the two operands must be the same register.
  103. ///
  104. bool IsUndef : 1;
  105. /// IsInternalRead - True if this operand reads a value that was defined
  106. /// inside the same instruction or bundle. This flag can be set on both use
  107. /// and def operands. On a sub-register def operand, it refers to the part
  108. /// of the register that isn't written. On a full-register def operand, it
  109. /// is a noop.
  110. ///
  111. /// When this flag is set, the instruction bundle must contain at least one
  112. /// other def of the register. If multiple instructions in the bundle define
  113. /// the register, the meaning is target-defined.
  114. bool IsInternalRead : 1;
  115. /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
  116. /// by the MachineInstr before all input registers are read. This is used to
  117. /// model the GCC inline asm '&' constraint modifier.
  118. bool IsEarlyClobber : 1;
  119. /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
  120. /// not a real instruction. Such uses should be ignored during codegen.
  121. bool IsDebug : 1;
  122. /// SmallContents - This really should be part of the Contents union, but
  123. /// lives out here so we can get a better packed struct.
  124. /// MO_Register: Register number.
  125. /// OffsetedInfo: Low bits of offset.
  126. union {
  127. unsigned RegNo; // For MO_Register.
  128. unsigned OffsetLo; // Matches Contents.OffsetedInfo.OffsetHi.
  129. } SmallContents;
  130. /// ParentMI - This is the instruction that this operand is embedded into.
  131. /// This is valid for all operand types, when the operand is in an instr.
  132. MachineInstr *ParentMI;
  133. /// Contents union - This contains the payload for the various operand types.
  134. union {
  135. MachineBasicBlock *MBB; // For MO_MachineBasicBlock.
  136. const ConstantFP *CFP; // For MO_FPImmediate.
  137. const ConstantInt *CI; // For MO_CImmediate. Integers > 64bit.
  138. int64_t ImmVal; // For MO_Immediate.
  139. const uint32_t *RegMask; // For MO_RegisterMask and MO_RegisterLiveOut.
  140. const MDNode *MD; // For MO_Metadata.
  141. MCSymbol *Sym; // For MO_MCSymbol.
  142. unsigned CFIIndex; // For MO_CFI.
  143. struct { // For MO_Register.
  144. // Register number is in SmallContents.RegNo.
  145. MachineOperand *Prev; // Access list for register. See MRI.
  146. MachineOperand *Next;
  147. } Reg;
  148. /// OffsetedInfo - This struct contains the offset and an object identifier.
  149. /// this represent the object as with an optional offset from it.
  150. struct {
  151. union {
  152. int Index; // For MO_*Index - The index itself.
  153. const char *SymbolName; // For MO_ExternalSymbol.
  154. const GlobalValue *GV; // For MO_GlobalAddress.
  155. const BlockAddress *BA; // For MO_BlockAddress.
  156. } Val;
  157. // Low bits of offset are in SmallContents.OffsetLo.
  158. int OffsetHi; // An offset from the object, high 32 bits.
  159. } OffsetedInfo;
  160. } Contents;
  161. explicit MachineOperand(MachineOperandType K)
  162. : OpKind(K), SubReg_TargetFlags(0), ParentMI(nullptr) {}
  163. public:
  164. /// getType - Returns the MachineOperandType for this operand.
  165. ///
  166. MachineOperandType getType() const { return (MachineOperandType)OpKind; }
  167. unsigned getTargetFlags() const {
  168. return isReg() ? 0 : SubReg_TargetFlags;
  169. }
  170. void setTargetFlags(unsigned F) {
  171. assert(!isReg() && "Register operands can't have target flags");
  172. SubReg_TargetFlags = F;
  173. assert(SubReg_TargetFlags == F && "Target flags out of range");
  174. }
  175. void addTargetFlag(unsigned F) {
  176. assert(!isReg() && "Register operands can't have target flags");
  177. SubReg_TargetFlags |= F;
  178. assert((SubReg_TargetFlags & F) && "Target flags out of range");
  179. }
  180. /// getParent - Return the instruction that this operand belongs to.
  181. ///
  182. MachineInstr *getParent() { return ParentMI; }
  183. const MachineInstr *getParent() const { return ParentMI; }
  184. /// clearParent - Reset the parent pointer.
  185. ///
  186. /// The MachineOperand copy constructor also copies ParentMI, expecting the
  187. /// original to be deleted. If a MachineOperand is ever stored outside a
  188. /// MachineInstr, the parent pointer must be cleared.
  189. ///
  190. /// Never call clearParent() on an operand in a MachineInstr.
  191. ///
  192. void clearParent() { ParentMI = nullptr; }
  193. void print(raw_ostream &os, const TargetRegisterInfo *TRI = nullptr) const;
  194. void print(raw_ostream &os, ModuleSlotTracker &MST,
  195. const TargetRegisterInfo *TRI = nullptr) const;
  196. //===--------------------------------------------------------------------===//
  197. // Accessors that tell you what kind of MachineOperand you're looking at.
  198. //===--------------------------------------------------------------------===//
  199. /// isReg - Tests if this is a MO_Register operand.
  200. bool isReg() const { return OpKind == MO_Register; }
  201. /// isImm - Tests if this is a MO_Immediate operand.
  202. bool isImm() const { return OpKind == MO_Immediate; }
  203. /// isCImm - Test if this is a MO_CImmediate operand.
  204. bool isCImm() const { return OpKind == MO_CImmediate; }
  205. /// isFPImm - Tests if this is a MO_FPImmediate operand.
  206. bool isFPImm() const { return OpKind == MO_FPImmediate; }
  207. /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
  208. bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
  209. /// isFI - Tests if this is a MO_FrameIndex operand.
  210. bool isFI() const { return OpKind == MO_FrameIndex; }
  211. /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
  212. bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
  213. /// isTargetIndex - Tests if this is a MO_TargetIndex operand.
  214. bool isTargetIndex() const { return OpKind == MO_TargetIndex; }
  215. /// isJTI - Tests if this is a MO_JumpTableIndex operand.
  216. bool isJTI() const { return OpKind == MO_JumpTableIndex; }
  217. /// isGlobal - Tests if this is a MO_GlobalAddress operand.
  218. bool isGlobal() const { return OpKind == MO_GlobalAddress; }
  219. /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
  220. bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
  221. /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
  222. bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
  223. /// isRegMask - Tests if this is a MO_RegisterMask operand.
  224. bool isRegMask() const { return OpKind == MO_RegisterMask; }
  225. /// isRegLiveOut - Tests if this is a MO_RegisterLiveOut operand.
  226. bool isRegLiveOut() const { return OpKind == MO_RegisterLiveOut; }
  227. /// isMetadata - Tests if this is a MO_Metadata operand.
  228. bool isMetadata() const { return OpKind == MO_Metadata; }
  229. bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
  230. bool isCFIIndex() const { return OpKind == MO_CFIIndex; }
  231. //===--------------------------------------------------------------------===//
  232. // Accessors for Register Operands
  233. //===--------------------------------------------------------------------===//
  234. /// getReg - Returns the register number.
  235. unsigned getReg() const {
  236. assert(isReg() && "This is not a register operand!");
  237. return SmallContents.RegNo;
  238. }
  239. unsigned getSubReg() const {
  240. assert(isReg() && "Wrong MachineOperand accessor");
  241. return SubReg_TargetFlags;
  242. }
  243. bool isUse() const {
  244. assert(isReg() && "Wrong MachineOperand accessor");
  245. return !IsDef;
  246. }
  247. bool isDef() const {
  248. assert(isReg() && "Wrong MachineOperand accessor");
  249. return IsDef;
  250. }
  251. bool isImplicit() const {
  252. assert(isReg() && "Wrong MachineOperand accessor");
  253. return IsImp;
  254. }
  255. bool isDead() const {
  256. assert(isReg() && "Wrong MachineOperand accessor");
  257. return IsDead;
  258. }
  259. bool isKill() const {
  260. assert(isReg() && "Wrong MachineOperand accessor");
  261. return IsKill;
  262. }
  263. bool isUndef() const {
  264. assert(isReg() && "Wrong MachineOperand accessor");
  265. return IsUndef;
  266. }
  267. bool isInternalRead() const {
  268. assert(isReg() && "Wrong MachineOperand accessor");
  269. return IsInternalRead;
  270. }
  271. bool isEarlyClobber() const {
  272. assert(isReg() && "Wrong MachineOperand accessor");
  273. return IsEarlyClobber;
  274. }
  275. bool isTied() const {
  276. assert(isReg() && "Wrong MachineOperand accessor");
  277. return TiedTo;
  278. }
  279. bool isDebug() const {
  280. assert(isReg() && "Wrong MachineOperand accessor");
  281. return IsDebug;
  282. }
  283. /// readsReg - Returns true if this operand reads the previous value of its
  284. /// register. A use operand with the <undef> flag set doesn't read its
  285. /// register. A sub-register def implicitly reads the other parts of the
  286. /// register being redefined unless the <undef> flag is set.
  287. ///
  288. /// This refers to reading the register value from before the current
  289. /// instruction or bundle. Internal bundle reads are not included.
  290. bool readsReg() const {
  291. assert(isReg() && "Wrong MachineOperand accessor");
  292. return !isUndef() && !isInternalRead() && (isUse() || getSubReg());
  293. }
  294. //===--------------------------------------------------------------------===//
  295. // Mutators for Register Operands
  296. //===--------------------------------------------------------------------===//
  297. /// Change the register this operand corresponds to.
  298. ///
  299. void setReg(unsigned Reg);
  300. void setSubReg(unsigned subReg) {
  301. assert(isReg() && "Wrong MachineOperand accessor");
  302. SubReg_TargetFlags = subReg;
  303. assert(SubReg_TargetFlags == subReg && "SubReg out of range");
  304. }
  305. /// substVirtReg - Substitute the current register with the virtual
  306. /// subregister Reg:SubReg. Take any existing SubReg index into account,
  307. /// using TargetRegisterInfo to compose the subreg indices if necessary.
  308. /// Reg must be a virtual register, SubIdx can be 0.
  309. ///
  310. void substVirtReg(unsigned Reg, unsigned SubIdx, const TargetRegisterInfo&);
  311. /// substPhysReg - Substitute the current register with the physical register
  312. /// Reg, taking any existing SubReg into account. For instance,
  313. /// substPhysReg(%EAX) will change %reg1024:sub_8bit to %AL.
  314. ///
  315. void substPhysReg(unsigned Reg, const TargetRegisterInfo&);
  316. void setIsUse(bool Val = true) { setIsDef(!Val); }
  317. void setIsDef(bool Val = true);
  318. void setImplicit(bool Val = true) {
  319. assert(isReg() && "Wrong MachineOperand accessor");
  320. IsImp = Val;
  321. }
  322. void setIsKill(bool Val = true) {
  323. assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
  324. assert((!Val || !isDebug()) && "Marking a debug operation as kill");
  325. IsKill = Val;
  326. }
  327. void setIsDead(bool Val = true) {
  328. assert(isReg() && IsDef && "Wrong MachineOperand accessor");
  329. IsDead = Val;
  330. }
  331. void setIsUndef(bool Val = true) {
  332. assert(isReg() && "Wrong MachineOperand accessor");
  333. IsUndef = Val;
  334. }
  335. void setIsInternalRead(bool Val = true) {
  336. assert(isReg() && "Wrong MachineOperand accessor");
  337. IsInternalRead = Val;
  338. }
  339. void setIsEarlyClobber(bool Val = true) {
  340. assert(isReg() && IsDef && "Wrong MachineOperand accessor");
  341. IsEarlyClobber = Val;
  342. }
  343. void setIsDebug(bool Val = true) {
  344. assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
  345. IsDebug = Val;
  346. }
  347. //===--------------------------------------------------------------------===//
  348. // Accessors for various operand types.
  349. //===--------------------------------------------------------------------===//
  350. int64_t getImm() const {
  351. assert(isImm() && "Wrong MachineOperand accessor");
  352. return Contents.ImmVal;
  353. }
  354. const ConstantInt *getCImm() const {
  355. assert(isCImm() && "Wrong MachineOperand accessor");
  356. return Contents.CI;
  357. }
  358. const ConstantFP *getFPImm() const {
  359. assert(isFPImm() && "Wrong MachineOperand accessor");
  360. return Contents.CFP;
  361. }
  362. MachineBasicBlock *getMBB() const {
  363. assert(isMBB() && "Wrong MachineOperand accessor");
  364. return Contents.MBB;
  365. }
  366. int getIndex() const {
  367. assert((isFI() || isCPI() || isTargetIndex() || isJTI()) &&
  368. "Wrong MachineOperand accessor");
  369. return Contents.OffsetedInfo.Val.Index;
  370. }
  371. const GlobalValue *getGlobal() const {
  372. assert(isGlobal() && "Wrong MachineOperand accessor");
  373. return Contents.OffsetedInfo.Val.GV;
  374. }
  375. const BlockAddress *getBlockAddress() const {
  376. assert(isBlockAddress() && "Wrong MachineOperand accessor");
  377. return Contents.OffsetedInfo.Val.BA;
  378. }
  379. MCSymbol *getMCSymbol() const {
  380. assert(isMCSymbol() && "Wrong MachineOperand accessor");
  381. return Contents.Sym;
  382. }
  383. unsigned getCFIIndex() const {
  384. assert(isCFIIndex() && "Wrong MachineOperand accessor");
  385. return Contents.CFIIndex;
  386. }
  387. /// Return the offset from the symbol in this operand. This always returns 0
  388. /// for ExternalSymbol operands.
  389. int64_t getOffset() const {
  390. assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() ||
  391. isTargetIndex() || isBlockAddress()) &&
  392. "Wrong MachineOperand accessor");
  393. return int64_t(uint64_t(Contents.OffsetedInfo.OffsetHi) << 32) |
  394. SmallContents.OffsetLo;
  395. }
  396. const char *getSymbolName() const {
  397. assert(isSymbol() && "Wrong MachineOperand accessor");
  398. return Contents.OffsetedInfo.Val.SymbolName;
  399. }
  400. /// clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
  401. /// It is sometimes necessary to detach the register mask pointer from its
  402. /// machine operand. This static method can be used for such detached bit
  403. /// mask pointers.
  404. static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg) {
  405. // See TargetRegisterInfo.h.
  406. assert(PhysReg < (1u << 30) && "Not a physical register");
  407. return !(RegMask[PhysReg / 32] & (1u << PhysReg % 32));
  408. }
  409. /// clobbersPhysReg - Returns true if this RegMask operand clobbers PhysReg.
  410. bool clobbersPhysReg(unsigned PhysReg) const {
  411. return clobbersPhysReg(getRegMask(), PhysReg);
  412. }
  413. /// getRegMask - Returns a bit mask of registers preserved by this RegMask
  414. /// operand.
  415. const uint32_t *getRegMask() const {
  416. assert(isRegMask() && "Wrong MachineOperand accessor");
  417. return Contents.RegMask;
  418. }
  419. /// getRegLiveOut - Returns a bit mask of live-out registers.
  420. const uint32_t *getRegLiveOut() const {
  421. assert(isRegLiveOut() && "Wrong MachineOperand accessor");
  422. return Contents.RegMask;
  423. }
  424. const MDNode *getMetadata() const {
  425. assert(isMetadata() && "Wrong MachineOperand accessor");
  426. return Contents.MD;
  427. }
  428. //===--------------------------------------------------------------------===//
  429. // Mutators for various operand types.
  430. //===--------------------------------------------------------------------===//
  431. void setImm(int64_t immVal) {
  432. assert(isImm() && "Wrong MachineOperand mutator");
  433. Contents.ImmVal = immVal;
  434. }
  435. void setFPImm(const ConstantFP *CFP) {
  436. assert(isFPImm() && "Wrong MachineOperand mutator");
  437. Contents.CFP = CFP;
  438. }
  439. void setOffset(int64_t Offset) {
  440. assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() ||
  441. isTargetIndex() || isBlockAddress()) &&
  442. "Wrong MachineOperand accessor");
  443. SmallContents.OffsetLo = unsigned(Offset);
  444. Contents.OffsetedInfo.OffsetHi = int(Offset >> 32);
  445. }
  446. void setIndex(int Idx) {
  447. assert((isFI() || isCPI() || isTargetIndex() || isJTI()) &&
  448. "Wrong MachineOperand accessor");
  449. Contents.OffsetedInfo.Val.Index = Idx;
  450. }
  451. void setMBB(MachineBasicBlock *MBB) {
  452. assert(isMBB() && "Wrong MachineOperand accessor");
  453. Contents.MBB = MBB;
  454. }
  455. //===--------------------------------------------------------------------===//
  456. // Other methods.
  457. //===--------------------------------------------------------------------===//
  458. /// isIdenticalTo - Return true if this operand is identical to the specified
  459. /// operand. Note: This method ignores isKill and isDead properties.
  460. bool isIdenticalTo(const MachineOperand &Other) const;
  461. /// \brief MachineOperand hash_value overload.
  462. ///
  463. /// Note that this includes the same information in the hash that
  464. /// isIdenticalTo uses for comparison. It is thus suited for use in hash
  465. /// tables which use that function for equality comparisons only.
  466. friend hash_code hash_value(const MachineOperand &MO);
  467. /// ChangeToImmediate - Replace this operand with a new immediate operand of
  468. /// the specified value. If an operand is known to be an immediate already,
  469. /// the setImm method should be used.
  470. void ChangeToImmediate(int64_t ImmVal);
  471. /// ChangeToFPImmediate - Replace this operand with a new FP immediate operand
  472. /// of the specified value. If an operand is known to be an FP immediate
  473. /// already, the setFPImm method should be used.
  474. void ChangeToFPImmediate(const ConstantFP *FPImm);
  475. /// ChangeToES - Replace this operand with a new external symbol operand.
  476. void ChangeToES(const char *SymName, unsigned char TargetFlags = 0);
  477. /// ChangeToMCSymbol - Replace this operand with a new MC symbol operand.
  478. void ChangeToMCSymbol(MCSymbol *Sym);
  479. /// ChangeToRegister - Replace this operand with a new register operand of
  480. /// the specified value. If an operand is known to be an register already,
  481. /// the setReg method should be used.
  482. void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
  483. bool isKill = false, bool isDead = false,
  484. bool isUndef = false, bool isDebug = false);
  485. //===--------------------------------------------------------------------===//
  486. // Construction methods.
  487. //===--------------------------------------------------------------------===//
  488. static MachineOperand CreateImm(int64_t Val) {
  489. MachineOperand Op(MachineOperand::MO_Immediate);
  490. Op.setImm(Val);
  491. return Op;
  492. }
  493. static MachineOperand CreateCImm(const ConstantInt *CI) {
  494. MachineOperand Op(MachineOperand::MO_CImmediate);
  495. Op.Contents.CI = CI;
  496. return Op;
  497. }
  498. static MachineOperand CreateFPImm(const ConstantFP *CFP) {
  499. MachineOperand Op(MachineOperand::MO_FPImmediate);
  500. Op.Contents.CFP = CFP;
  501. return Op;
  502. }
  503. static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
  504. bool isKill = false, bool isDead = false,
  505. bool isUndef = false,
  506. bool isEarlyClobber = false,
  507. unsigned SubReg = 0,
  508. bool isDebug = false,
  509. bool isInternalRead = false) {
  510. assert(!(isDead && !isDef) && "Dead flag on non-def");
  511. assert(!(isKill && isDef) && "Kill flag on def");
  512. MachineOperand Op(MachineOperand::MO_Register);
  513. Op.IsDef = isDef;
  514. Op.IsImp = isImp;
  515. Op.IsKill = isKill;
  516. Op.IsDead = isDead;
  517. Op.IsUndef = isUndef;
  518. Op.IsInternalRead = isInternalRead;
  519. Op.IsEarlyClobber = isEarlyClobber;
  520. Op.TiedTo = 0;
  521. Op.IsDebug = isDebug;
  522. Op.SmallContents.RegNo = Reg;
  523. Op.Contents.Reg.Prev = nullptr;
  524. Op.Contents.Reg.Next = nullptr;
  525. Op.setSubReg(SubReg);
  526. return Op;
  527. }
  528. static MachineOperand CreateMBB(MachineBasicBlock *MBB,
  529. unsigned char TargetFlags = 0) {
  530. MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
  531. Op.setMBB(MBB);
  532. Op.setTargetFlags(TargetFlags);
  533. return Op;
  534. }
  535. static MachineOperand CreateFI(int Idx) {
  536. MachineOperand Op(MachineOperand::MO_FrameIndex);
  537. Op.setIndex(Idx);
  538. return Op;
  539. }
  540. static MachineOperand CreateCPI(unsigned Idx, int Offset,
  541. unsigned char TargetFlags = 0) {
  542. MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
  543. Op.setIndex(Idx);
  544. Op.setOffset(Offset);
  545. Op.setTargetFlags(TargetFlags);
  546. return Op;
  547. }
  548. static MachineOperand CreateTargetIndex(unsigned Idx, int64_t Offset,
  549. unsigned char TargetFlags = 0) {
  550. MachineOperand Op(MachineOperand::MO_TargetIndex);
  551. Op.setIndex(Idx);
  552. Op.setOffset(Offset);
  553. Op.setTargetFlags(TargetFlags);
  554. return Op;
  555. }
  556. static MachineOperand CreateJTI(unsigned Idx,
  557. unsigned char TargetFlags = 0) {
  558. MachineOperand Op(MachineOperand::MO_JumpTableIndex);
  559. Op.setIndex(Idx);
  560. Op.setTargetFlags(TargetFlags);
  561. return Op;
  562. }
  563. static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset,
  564. unsigned char TargetFlags = 0) {
  565. MachineOperand Op(MachineOperand::MO_GlobalAddress);
  566. Op.Contents.OffsetedInfo.Val.GV = GV;
  567. Op.setOffset(Offset);
  568. Op.setTargetFlags(TargetFlags);
  569. return Op;
  570. }
  571. static MachineOperand CreateES(const char *SymName,
  572. unsigned char TargetFlags = 0) {
  573. MachineOperand Op(MachineOperand::MO_ExternalSymbol);
  574. Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
  575. Op.setOffset(0); // Offset is always 0.
  576. Op.setTargetFlags(TargetFlags);
  577. return Op;
  578. }
  579. static MachineOperand CreateBA(const BlockAddress *BA, int64_t Offset,
  580. unsigned char TargetFlags = 0) {
  581. MachineOperand Op(MachineOperand::MO_BlockAddress);
  582. Op.Contents.OffsetedInfo.Val.BA = BA;
  583. Op.setOffset(Offset);
  584. Op.setTargetFlags(TargetFlags);
  585. return Op;
  586. }
  587. /// CreateRegMask - Creates a register mask operand referencing Mask. The
  588. /// operand does not take ownership of the memory referenced by Mask, it must
  589. /// remain valid for the lifetime of the operand.
  590. ///
  591. /// A RegMask operand represents a set of non-clobbered physical registers on
  592. /// an instruction that clobbers many registers, typically a call. The bit
  593. /// mask has a bit set for each physreg that is preserved by this
  594. /// instruction, as described in the documentation for
  595. /// TargetRegisterInfo::getCallPreservedMask().
  596. ///
  597. /// Any physreg with a 0 bit in the mask is clobbered by the instruction.
  598. ///
  599. static MachineOperand CreateRegMask(const uint32_t *Mask) {
  600. assert(Mask && "Missing register mask");
  601. MachineOperand Op(MachineOperand::MO_RegisterMask);
  602. Op.Contents.RegMask = Mask;
  603. return Op;
  604. }
  605. static MachineOperand CreateRegLiveOut(const uint32_t *Mask) {
  606. assert(Mask && "Missing live-out register mask");
  607. MachineOperand Op(MachineOperand::MO_RegisterLiveOut);
  608. Op.Contents.RegMask = Mask;
  609. return Op;
  610. }
  611. static MachineOperand CreateMetadata(const MDNode *Meta) {
  612. MachineOperand Op(MachineOperand::MO_Metadata);
  613. Op.Contents.MD = Meta;
  614. return Op;
  615. }
  616. static MachineOperand CreateMCSymbol(MCSymbol *Sym,
  617. unsigned char TargetFlags = 0) {
  618. MachineOperand Op(MachineOperand::MO_MCSymbol);
  619. Op.Contents.Sym = Sym;
  620. Op.setOffset(0);
  621. Op.setTargetFlags(TargetFlags);
  622. return Op;
  623. }
  624. static MachineOperand CreateCFIIndex(unsigned CFIIndex) {
  625. MachineOperand Op(MachineOperand::MO_CFIIndex);
  626. Op.Contents.CFIIndex = CFIIndex;
  627. return Op;
  628. }
  629. friend class MachineInstr;
  630. friend class MachineRegisterInfo;
  631. private:
  632. void removeRegFromUses();
  633. //===--------------------------------------------------------------------===//
  634. // Methods for handling register use/def lists.
  635. //===--------------------------------------------------------------------===//
  636. /// isOnRegUseList - Return true if this operand is on a register use/def list
  637. /// or false if not. This can only be called for register operands that are
  638. /// part of a machine instruction.
  639. bool isOnRegUseList() const {
  640. assert(isReg() && "Can only add reg operand to use lists");
  641. return Contents.Reg.Prev != nullptr;
  642. }
  643. };
  644. inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
  645. MO.print(OS, nullptr);
  646. return OS;
  647. }
  648. // See friend declaration above. This additional declaration is required in
  649. // order to compile LLVM with IBM xlC compiler.
  650. hash_code hash_value(const MachineOperand &MO);
  651. } // End llvm namespace
  652. #endif