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