Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:31

0001 //===-- llvm/CodeGen/MachineOperand.h - MachineOperand class ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains the declaration of the MachineOperand class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CODEGEN_MACHINEOPERAND_H
0014 #define LLVM_CODEGEN_MACHINEOPERAND_H
0015 
0016 #include "llvm/ADT/DenseMapInfo.h"
0017 #include "llvm/CodeGen/Register.h"
0018 #include "llvm/IR/Intrinsics.h"
0019 #include <cassert>
0020 
0021 namespace llvm {
0022 
0023 class LLT;
0024 class BlockAddress;
0025 class Constant;
0026 class ConstantFP;
0027 class ConstantInt;
0028 class GlobalValue;
0029 class MachineBasicBlock;
0030 class MachineInstr;
0031 class MachineRegisterInfo;
0032 class MCCFIInstruction;
0033 class MDNode;
0034 class ModuleSlotTracker;
0035 class TargetIntrinsicInfo;
0036 class TargetRegisterInfo;
0037 class hash_code;
0038 class raw_ostream;
0039 class MCSymbol;
0040 
0041 /// MachineOperand class - Representation of each machine instruction operand.
0042 ///
0043 /// This class isn't a POD type because it has a private constructor, but its
0044 /// destructor must be trivial. Functions like MachineInstr::addOperand(),
0045 /// MachineRegisterInfo::moveOperands(), and MF::DeleteMachineInstr() depend on
0046 /// not having to call the MachineOperand destructor.
0047 ///
0048 class MachineOperand {
0049 public:
0050   enum MachineOperandType : unsigned char {
0051     MO_Register,          ///< Register operand.
0052     MO_Immediate,         ///< Immediate operand
0053     MO_CImmediate,        ///< Immediate >64bit operand
0054     MO_FPImmediate,       ///< Floating-point immediate operand
0055     MO_MachineBasicBlock, ///< MachineBasicBlock reference
0056     MO_FrameIndex,        ///< Abstract Stack Frame Index
0057     MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool
0058     MO_TargetIndex,       ///< Target-dependent index+offset operand.
0059     MO_JumpTableIndex,    ///< Address of indexed Jump Table for switch
0060     MO_ExternalSymbol,    ///< Name of external global symbol
0061     MO_GlobalAddress,     ///< Address of a global value
0062     MO_BlockAddress,      ///< Address of a basic block
0063     MO_RegisterMask,      ///< Mask of preserved registers.
0064     MO_RegisterLiveOut,   ///< Mask of live-out registers.
0065     MO_Metadata,          ///< Metadata reference (for debug info)
0066     MO_MCSymbol,          ///< MCSymbol reference (for debug/eh info)
0067     MO_CFIIndex,          ///< MCCFIInstruction index.
0068     MO_IntrinsicID,       ///< Intrinsic ID for ISel
0069     MO_Predicate,         ///< Generic predicate for ISel
0070     MO_ShuffleMask,       ///< Other IR Constant for ISel (shuffle masks)
0071     MO_DbgInstrRef, ///< Integer indices referring to an instruction+operand
0072     MO_Last = MO_DbgInstrRef
0073   };
0074 
0075 private:
0076   /// OpKind - Specify what kind of operand this is.  This discriminates the
0077   /// union.
0078   unsigned OpKind : 8;
0079 
0080   /// Subregister number for MO_Register.  A value of 0 indicates the
0081   /// MO_Register has no subReg.
0082   ///
0083   /// For all other kinds of operands, this field holds target-specific flags.
0084   unsigned SubReg_TargetFlags : 12;
0085 
0086   /// TiedTo - Non-zero when this register operand is tied to another register
0087   /// operand. The encoding of this field is described in the block comment
0088   /// before MachineInstr::tieOperands().
0089   unsigned TiedTo : 4;
0090 
0091   /// IsDef - True if this is a def, false if this is a use of the register.
0092   /// This is only valid on register operands.
0093   ///
0094   unsigned IsDef : 1;
0095 
0096   /// IsImp - True if this is an implicit def or use, false if it is explicit.
0097   /// This is only valid on register opderands.
0098   ///
0099   unsigned IsImp : 1;
0100 
0101   /// IsDeadOrKill
0102   /// For uses: IsKill - Conservatively indicates the last use of a register
0103   /// on this path through the function. A register operand with true value of
0104   /// this flag must be the last use of the register, a register operand with
0105   /// false value may or may not be the last use of the register. After regalloc
0106   /// we can use recomputeLivenessFlags to get precise kill flags.
0107   /// For defs: IsDead - True if this register is never used by a subsequent
0108   /// instruction.
0109   /// This is only valid on register operands.
0110   unsigned IsDeadOrKill : 1;
0111 
0112   /// See isRenamable().
0113   unsigned IsRenamable : 1;
0114 
0115   /// IsUndef - True if this register operand reads an "undef" value, i.e. the
0116   /// read value doesn't matter.  This flag can be set on both use and def
0117   /// operands.  On a sub-register def operand, it refers to the part of the
0118   /// register that isn't written.  On a full-register def operand, it is a
0119   /// noop.  See readsReg().
0120   ///
0121   /// This is only valid on registers.
0122   ///
0123   /// Note that an instruction may have multiple <undef> operands referring to
0124   /// the same register.  In that case, the instruction may depend on those
0125   /// operands reading the same dont-care value.  For example:
0126   ///
0127   ///   %1 = XOR undef %2, undef %2
0128   ///
0129   /// Any register can be used for %2, and its value doesn't matter, but
0130   /// the two operands must be the same register.
0131   ///
0132   unsigned IsUndef : 1;
0133 
0134   /// IsInternalRead - True if this operand reads a value that was defined
0135   /// inside the same instruction or bundle.  This flag can be set on both use
0136   /// and def operands.  On a sub-register def operand, it refers to the part
0137   /// of the register that isn't written.  On a full-register def operand, it
0138   /// is a noop.
0139   ///
0140   /// When this flag is set, the instruction bundle must contain at least one
0141   /// other def of the register.  If multiple instructions in the bundle define
0142   /// the register, the meaning is target-defined.
0143   unsigned IsInternalRead : 1;
0144 
0145   /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
0146   /// by the MachineInstr before all input registers are read.  This is used to
0147   /// model the GCC inline asm '&' constraint modifier.
0148   unsigned IsEarlyClobber : 1;
0149 
0150   /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo,
0151   /// not a real instruction.  Such uses should be ignored during codegen.
0152   unsigned IsDebug : 1;
0153 
0154   /// SmallContents - This really should be part of the Contents union, but
0155   /// lives out here so we can get a better packed struct.
0156   /// MO_Register: Register number.
0157   /// OffsetedInfo: Low bits of offset.
0158   union {
0159     unsigned RegNo;           // For MO_Register.
0160     unsigned OffsetLo;        // Matches Contents.OffsetedInfo.OffsetHi.
0161   } SmallContents;
0162 
0163   /// ParentMI - This is the instruction that this operand is embedded into.
0164   /// This is valid for all operand types, when the operand is in an instr.
0165   MachineInstr *ParentMI = nullptr;
0166 
0167   /// Contents union - This contains the payload for the various operand types.
0168   union ContentsUnion {
0169     ContentsUnion() {}
0170     MachineBasicBlock *MBB;  // For MO_MachineBasicBlock.
0171     const ConstantFP *CFP;   // For MO_FPImmediate.
0172     const ConstantInt *CI;   // For MO_CImmediate. Integers > 64bit.
0173     int64_t ImmVal;          // For MO_Immediate.
0174     const uint32_t *RegMask; // For MO_RegisterMask and MO_RegisterLiveOut.
0175     const MDNode *MD;        // For MO_Metadata.
0176     MCSymbol *Sym;           // For MO_MCSymbol.
0177     unsigned CFIIndex;       // For MO_CFI.
0178     Intrinsic::ID IntrinsicID; // For MO_IntrinsicID.
0179     unsigned Pred;           // For MO_Predicate
0180     ArrayRef<int> ShuffleMask; // For MO_ShuffleMask
0181 
0182     struct {                  // For MO_Register.
0183       // Register number is in SmallContents.RegNo.
0184       MachineOperand *Prev;   // Access list for register. See MRI.
0185       MachineOperand *Next;
0186     } Reg;
0187 
0188     struct { // For MO_DbgInstrRef.
0189       unsigned InstrIdx;
0190       unsigned OpIdx;
0191     } InstrRef;
0192 
0193     /// OffsetedInfo - This struct contains the offset and an object identifier.
0194     /// this represent the object as with an optional offset from it.
0195     struct {
0196       union {
0197         int Index;                // For MO_*Index - The index itself.
0198         const char *SymbolName;   // For MO_ExternalSymbol.
0199         const GlobalValue *GV;    // For MO_GlobalAddress.
0200         const BlockAddress *BA;   // For MO_BlockAddress.
0201       } Val;
0202       // Low bits of offset are in SmallContents.OffsetLo.
0203       int OffsetHi;               // An offset from the object, high 32 bits.
0204     } OffsetedInfo;
0205   } Contents;
0206 
0207   explicit MachineOperand(MachineOperandType K)
0208       : OpKind(K), SubReg_TargetFlags(0) {
0209     // Assert that the layout is what we expect. It's easy to grow this object.
0210     static_assert(alignof(MachineOperand) <= alignof(int64_t),
0211                   "MachineOperand shouldn't be more than 8 byte aligned");
0212     static_assert(sizeof(Contents) <= 2 * sizeof(void *),
0213                   "Contents should be at most two pointers");
0214     static_assert(sizeof(MachineOperand) <=
0215                       alignTo<alignof(int64_t)>(2 * sizeof(unsigned) +
0216                                                 3 * sizeof(void *)),
0217                   "MachineOperand too big. Should be Kind, SmallContents, "
0218                   "ParentMI, and Contents");
0219   }
0220 
0221 public:
0222   /// getType - Returns the MachineOperandType for this operand.
0223   ///
0224   MachineOperandType getType() const { return (MachineOperandType)OpKind; }
0225 
0226   unsigned getTargetFlags() const {
0227     return isReg() ? 0 : SubReg_TargetFlags;
0228   }
0229   void setTargetFlags(unsigned F) {
0230     assert(!isReg() && "Register operands can't have target flags");
0231     SubReg_TargetFlags = F;
0232     assert(SubReg_TargetFlags == F && "Target flags out of range");
0233   }
0234   void addTargetFlag(unsigned F) {
0235     assert(!isReg() && "Register operands can't have target flags");
0236     SubReg_TargetFlags |= F;
0237     assert((SubReg_TargetFlags & F) && "Target flags out of range");
0238   }
0239 
0240 
0241   /// getParent - Return the instruction that this operand belongs to.
0242   ///
0243   MachineInstr *getParent() { return ParentMI; }
0244   const MachineInstr *getParent() const { return ParentMI; }
0245 
0246   /// clearParent - Reset the parent pointer.
0247   ///
0248   /// The MachineOperand copy constructor also copies ParentMI, expecting the
0249   /// original to be deleted. If a MachineOperand is ever stored outside a
0250   /// MachineInstr, the parent pointer must be cleared.
0251   ///
0252   /// Never call clearParent() on an operand in a MachineInstr.
0253   ///
0254   void clearParent() { ParentMI = nullptr; }
0255 
0256   /// Returns the index of this operand in the instruction that it belongs to.
0257   unsigned getOperandNo() const;
0258 
0259   /// Print a subreg index operand.
0260   /// MO_Immediate operands can also be subreg idices. If it's the case, the
0261   /// subreg index name will be printed. MachineInstr::isOperandSubregIdx can be
0262   /// called to check this.
0263   static void printSubRegIdx(raw_ostream &OS, uint64_t Index,
0264                              const TargetRegisterInfo *TRI);
0265 
0266   /// Print operand target flags.
0267   static void printTargetFlags(raw_ostream& OS, const MachineOperand &Op);
0268 
0269   /// Print a MCSymbol as an operand.
0270   static void printSymbol(raw_ostream &OS, MCSymbol &Sym);
0271 
0272   /// Print a stack object reference.
0273   static void printStackObjectReference(raw_ostream &OS, unsigned FrameIndex,
0274                                         bool IsFixed, StringRef Name);
0275 
0276   /// Print the offset with explicit +/- signs.
0277   static void printOperandOffset(raw_ostream &OS, int64_t Offset);
0278 
0279   /// Print an IRSlotNumber.
0280   static void printIRSlotNumber(raw_ostream &OS, int Slot);
0281 
0282   /// Print the MachineOperand to \p os.
0283   /// Providing a valid \p TRI and \p IntrinsicInfo results in a more
0284   /// target-specific printing. If \p TRI and \p IntrinsicInfo are null, the
0285   /// function will try to pick it up from the parent.
0286   void print(raw_ostream &os, const TargetRegisterInfo *TRI = nullptr,
0287              const TargetIntrinsicInfo *IntrinsicInfo = nullptr) const;
0288 
0289   /// More complex way of printing a MachineOperand.
0290   /// \param TypeToPrint specifies the generic type to be printed on uses and
0291   /// defs. It can be determined using MachineInstr::getTypeToPrint.
0292   /// \param OpIdx - specifies the index of the operand in machine instruction.
0293   /// This will be used by target dependent MIR formatter. Could be std::nullopt
0294   /// if the index is unknown, e.g. called by dump().
0295   /// \param PrintDef - whether we want to print `def` on an operand which
0296   /// isDef. Sometimes, if the operand is printed before '=', we don't print
0297   /// `def`.
0298   /// \param IsStandalone - whether we want a verbose output of the MO. This
0299   /// prints extra information that can be easily inferred when printing the
0300   /// whole function, but not when printing only a fragment of it.
0301   /// \param ShouldPrintRegisterTies - whether we want to print register ties.
0302   /// Sometimes they are easily determined by the instruction's descriptor
0303   /// (MachineInstr::hasComplexRegiterTies can determine if it's needed).
0304   /// \param TiedOperandIdx - if we need to print register ties this needs to
0305   /// provide the index of the tied register. If not, it will be ignored.
0306   /// \param TRI - provide more target-specific information to the printer.
0307   /// Unlike the previous function, this one will not try and get the
0308   /// information from it's parent.
0309   /// \param IntrinsicInfo - same as \p TRI.
0310   void print(raw_ostream &os, ModuleSlotTracker &MST, LLT TypeToPrint,
0311              std::optional<unsigned> OpIdx, bool PrintDef, bool IsStandalone,
0312              bool ShouldPrintRegisterTies, unsigned TiedOperandIdx,
0313              const TargetRegisterInfo *TRI,
0314              const TargetIntrinsicInfo *IntrinsicInfo) const;
0315 
0316   /// Same as print(os, TRI, IntrinsicInfo), but allows to specify the low-level
0317   /// type to be printed the same way the full version of print(...) does it.
0318   void print(raw_ostream &os, LLT TypeToPrint,
0319              const TargetRegisterInfo *TRI = nullptr,
0320              const TargetIntrinsicInfo *IntrinsicInfo = nullptr) const;
0321 
0322   void dump() const;
0323 
0324   //===--------------------------------------------------------------------===//
0325   // Accessors that tell you what kind of MachineOperand you're looking at.
0326   //===--------------------------------------------------------------------===//
0327 
0328   /// isReg - Tests if this is a MO_Register operand.
0329   bool isReg() const { return OpKind == MO_Register; }
0330   /// isImm - Tests if this is a MO_Immediate operand.
0331   bool isImm() const { return OpKind == MO_Immediate; }
0332   /// isCImm - Test if this is a MO_CImmediate operand.
0333   bool isCImm() const { return OpKind == MO_CImmediate; }
0334   /// isFPImm - Tests if this is a MO_FPImmediate operand.
0335   bool isFPImm() const { return OpKind == MO_FPImmediate; }
0336   /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
0337   bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
0338   /// isFI - Tests if this is a MO_FrameIndex operand.
0339   bool isFI() const { return OpKind == MO_FrameIndex; }
0340   /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
0341   bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
0342   /// isTargetIndex - Tests if this is a MO_TargetIndex operand.
0343   bool isTargetIndex() const { return OpKind == MO_TargetIndex; }
0344   /// isJTI - Tests if this is a MO_JumpTableIndex operand.
0345   bool isJTI() const { return OpKind == MO_JumpTableIndex; }
0346   /// isGlobal - Tests if this is a MO_GlobalAddress operand.
0347   bool isGlobal() const { return OpKind == MO_GlobalAddress; }
0348   /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
0349   bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
0350   /// isBlockAddress - Tests if this is a MO_BlockAddress operand.
0351   bool isBlockAddress() const { return OpKind == MO_BlockAddress; }
0352   /// isRegMask - Tests if this is a MO_RegisterMask operand.
0353   bool isRegMask() const { return OpKind == MO_RegisterMask; }
0354   /// isRegLiveOut - Tests if this is a MO_RegisterLiveOut operand.
0355   bool isRegLiveOut() const { return OpKind == MO_RegisterLiveOut; }
0356   /// isMetadata - Tests if this is a MO_Metadata operand.
0357   bool isMetadata() const { return OpKind == MO_Metadata; }
0358   bool isMCSymbol() const { return OpKind == MO_MCSymbol; }
0359   bool isDbgInstrRef() const { return OpKind == MO_DbgInstrRef; }
0360   bool isCFIIndex() const { return OpKind == MO_CFIIndex; }
0361   bool isIntrinsicID() const { return OpKind == MO_IntrinsicID; }
0362   bool isPredicate() const { return OpKind == MO_Predicate; }
0363   bool isShuffleMask() const { return OpKind == MO_ShuffleMask; }
0364   //===--------------------------------------------------------------------===//
0365   // Accessors for Register Operands
0366   //===--------------------------------------------------------------------===//
0367 
0368   /// getReg - Returns the register number.
0369   Register getReg() const {
0370     assert(isReg() && "This is not a register operand!");
0371     return Register(SmallContents.RegNo);
0372   }
0373 
0374   unsigned getSubReg() const {
0375     assert(isReg() && "Wrong MachineOperand accessor");
0376     return SubReg_TargetFlags;
0377   }
0378 
0379   bool isUse() const {
0380     assert(isReg() && "Wrong MachineOperand accessor");
0381     return !IsDef;
0382   }
0383 
0384   bool isDef() const {
0385     assert(isReg() && "Wrong MachineOperand accessor");
0386     return IsDef;
0387   }
0388 
0389   bool isImplicit() const {
0390     assert(isReg() && "Wrong MachineOperand accessor");
0391     return IsImp;
0392   }
0393 
0394   bool isDead() const {
0395     assert(isReg() && "Wrong MachineOperand accessor");
0396     return IsDeadOrKill & IsDef;
0397   }
0398 
0399   bool isKill() const {
0400     assert(isReg() && "Wrong MachineOperand accessor");
0401     return IsDeadOrKill & !IsDef;
0402   }
0403 
0404   bool isUndef() const {
0405     assert(isReg() && "Wrong MachineOperand accessor");
0406     return IsUndef;
0407   }
0408 
0409   /// isRenamable - Returns true if this register may be renamed, i.e. it does
0410   /// not generate a value that is somehow read in a way that is not represented
0411   /// by the Machine IR (e.g. to meet an ABI or ISA requirement).  This is only
0412   /// valid on physical register operands.  Virtual registers are assumed to
0413   /// always be renamable regardless of the value of this field.
0414   ///
0415   /// Operands that are renamable can freely be changed to any other register
0416   /// that is a member of the register class returned by
0417   /// MI->getRegClassConstraint().
0418   ///
0419   /// isRenamable can return false for several different reasons:
0420   ///
0421   /// - ABI constraints (since liveness is not always precisely modeled).  We
0422   ///   conservatively handle these cases by setting all physical register
0423   ///   operands that didn’t start out as virtual regs to not be renamable.
0424   ///   Also any physical register operands created after register allocation or
0425   ///   whose register is changed after register allocation will not be
0426   ///   renamable.  This state is tracked in the MachineOperand::IsRenamable
0427   ///   bit.
0428   ///
0429   /// - Opcode/target constraints: for opcodes that have complex register class
0430   ///   requirements (e.g. that depend on other operands/instructions), we set
0431   ///   hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq in the machine opcode
0432   ///   description.  Operands belonging to instructions with opcodes that are
0433   ///   marked hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq return false from
0434   ///   isRenamable().  Additionally, the AllowRegisterRenaming target property
0435   ///   prevents any operands from being marked renamable for targets that don't
0436   ///   have detailed opcode hasExtraSrcRegAllocReq/hasExtraDstRegAllocReq
0437   ///   values.
0438   bool isRenamable() const;
0439 
0440   bool isInternalRead() const {
0441     assert(isReg() && "Wrong MachineOperand accessor");
0442     return IsInternalRead;
0443   }
0444 
0445   bool isEarlyClobber() const {
0446     assert(isReg() && "Wrong MachineOperand accessor");
0447     return IsEarlyClobber;
0448   }
0449 
0450   bool isTied() const {
0451     assert(isReg() && "Wrong MachineOperand accessor");
0452     return TiedTo;
0453   }
0454 
0455   bool isDebug() const {
0456     assert(isReg() && "Wrong MachineOperand accessor");
0457     return IsDebug;
0458   }
0459 
0460   /// readsReg - Returns true if this operand reads the previous value of its
0461   /// register.  A use operand with the <undef> flag set doesn't read its
0462   /// register.  A sub-register def implicitly reads the other parts of the
0463   /// register being redefined unless the <undef> flag is set.
0464   ///
0465   /// This refers to reading the register value from before the current
0466   /// instruction or bundle. Internal bundle reads are not included.
0467   bool readsReg() const {
0468     assert(isReg() && "Wrong MachineOperand accessor");
0469     return !isUndef() && !isInternalRead() && (isUse() || getSubReg());
0470   }
0471 
0472   /// Return true if this operand can validly be appended to an arbitrary
0473   /// operand list. i.e. this behaves like an implicit operand.
0474   bool isValidExcessOperand() const {
0475     if ((isReg() && isImplicit()) || isRegMask())
0476       return true;
0477 
0478     // Debug operands
0479     return isMetadata() || isMCSymbol();
0480   }
0481 
0482   //===--------------------------------------------------------------------===//
0483   // Mutators for Register Operands
0484   //===--------------------------------------------------------------------===//
0485 
0486   /// Change the register this operand corresponds to.
0487   ///
0488   void setReg(Register Reg);
0489 
0490   void setSubReg(unsigned subReg) {
0491     assert(isReg() && "Wrong MachineOperand mutator");
0492     SubReg_TargetFlags = subReg;
0493     assert(SubReg_TargetFlags == subReg && "SubReg out of range");
0494   }
0495 
0496   /// substVirtReg - Substitute the current register with the virtual
0497   /// subregister Reg:SubReg. Take any existing SubReg index into account,
0498   /// using TargetRegisterInfo to compose the subreg indices if necessary.
0499   /// Reg must be a virtual register, SubIdx can be 0.
0500   ///
0501   void substVirtReg(Register Reg, unsigned SubIdx, const TargetRegisterInfo&);
0502 
0503   /// substPhysReg - Substitute the current register with the physical register
0504   /// Reg, taking any existing SubReg into account. For instance,
0505   /// substPhysReg(%eax) will change %reg1024:sub_8bit to %al.
0506   ///
0507   void substPhysReg(MCRegister Reg, const TargetRegisterInfo&);
0508 
0509   void setIsUse(bool Val = true) { setIsDef(!Val); }
0510 
0511   /// Change a def to a use, or a use to a def.
0512   void setIsDef(bool Val = true);
0513 
0514   void setImplicit(bool Val = true) {
0515     assert(isReg() && "Wrong MachineOperand mutator");
0516     IsImp = Val;
0517   }
0518 
0519   void setIsKill(bool Val = true) {
0520     assert(isReg() && !IsDef && "Wrong MachineOperand mutator");
0521     assert((!Val || !isDebug()) && "Marking a debug operation as kill");
0522     IsDeadOrKill = Val;
0523   }
0524 
0525   void setIsDead(bool Val = true) {
0526     assert(isReg() && IsDef && "Wrong MachineOperand mutator");
0527     IsDeadOrKill = Val;
0528   }
0529 
0530   void setIsUndef(bool Val = true) {
0531     assert(isReg() && "Wrong MachineOperand mutator");
0532     IsUndef = Val;
0533   }
0534 
0535   void setIsRenamable(bool Val = true);
0536 
0537   void setIsInternalRead(bool Val = true) {
0538     assert(isReg() && "Wrong MachineOperand mutator");
0539     IsInternalRead = Val;
0540   }
0541 
0542   void setIsEarlyClobber(bool Val = true) {
0543     assert(isReg() && IsDef && "Wrong MachineOperand mutator");
0544     IsEarlyClobber = Val;
0545   }
0546 
0547   void setIsDebug(bool Val = true) {
0548     assert(isReg() && !IsDef && "Wrong MachineOperand mutator");
0549     IsDebug = Val;
0550   }
0551 
0552   //===--------------------------------------------------------------------===//
0553   // Accessors for various operand types.
0554   //===--------------------------------------------------------------------===//
0555 
0556   int64_t getImm() const {
0557     assert(isImm() && "Wrong MachineOperand accessor");
0558     return Contents.ImmVal;
0559   }
0560 
0561   const ConstantInt *getCImm() const {
0562     assert(isCImm() && "Wrong MachineOperand accessor");
0563     return Contents.CI;
0564   }
0565 
0566   const ConstantFP *getFPImm() const {
0567     assert(isFPImm() && "Wrong MachineOperand accessor");
0568     return Contents.CFP;
0569   }
0570 
0571   MachineBasicBlock *getMBB() const {
0572     assert(isMBB() && "Wrong MachineOperand accessor");
0573     return Contents.MBB;
0574   }
0575 
0576   int getIndex() const {
0577     assert((isFI() || isCPI() || isTargetIndex() || isJTI()) &&
0578            "Wrong MachineOperand accessor");
0579     return Contents.OffsetedInfo.Val.Index;
0580   }
0581 
0582   const GlobalValue *getGlobal() const {
0583     assert(isGlobal() && "Wrong MachineOperand accessor");
0584     return Contents.OffsetedInfo.Val.GV;
0585   }
0586 
0587   const BlockAddress *getBlockAddress() const {
0588     assert(isBlockAddress() && "Wrong MachineOperand accessor");
0589     return Contents.OffsetedInfo.Val.BA;
0590   }
0591 
0592   MCSymbol *getMCSymbol() const {
0593     assert(isMCSymbol() && "Wrong MachineOperand accessor");
0594     return Contents.Sym;
0595   }
0596 
0597   unsigned getInstrRefInstrIndex() const {
0598     assert(isDbgInstrRef() && "Wrong MachineOperand accessor");
0599     return Contents.InstrRef.InstrIdx;
0600   }
0601 
0602   unsigned getInstrRefOpIndex() const {
0603     assert(isDbgInstrRef() && "Wrong MachineOperand accessor");
0604     return Contents.InstrRef.OpIdx;
0605   }
0606 
0607   unsigned getCFIIndex() const {
0608     assert(isCFIIndex() && "Wrong MachineOperand accessor");
0609     return Contents.CFIIndex;
0610   }
0611 
0612   Intrinsic::ID getIntrinsicID() const {
0613     assert(isIntrinsicID() && "Wrong MachineOperand accessor");
0614     return Contents.IntrinsicID;
0615   }
0616 
0617   unsigned getPredicate() const {
0618     assert(isPredicate() && "Wrong MachineOperand accessor");
0619     return Contents.Pred;
0620   }
0621 
0622   ArrayRef<int> getShuffleMask() const {
0623     assert(isShuffleMask() && "Wrong MachineOperand accessor");
0624     return Contents.ShuffleMask;
0625   }
0626 
0627   /// Return the offset from the symbol in this operand. This always returns 0
0628   /// for ExternalSymbol operands.
0629   int64_t getOffset() const {
0630     assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() ||
0631             isTargetIndex() || isBlockAddress()) &&
0632            "Wrong MachineOperand accessor");
0633     return int64_t(uint64_t(Contents.OffsetedInfo.OffsetHi) << 32) |
0634            SmallContents.OffsetLo;
0635   }
0636 
0637   const char *getSymbolName() const {
0638     assert(isSymbol() && "Wrong MachineOperand accessor");
0639     return Contents.OffsetedInfo.Val.SymbolName;
0640   }
0641 
0642   /// clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
0643   /// It is sometimes necessary to detach the register mask pointer from its
0644   /// machine operand. This static method can be used for such detached bit
0645   /// mask pointers.
0646   static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg) {
0647     // See TargetRegisterInfo.h.
0648     assert((!PhysReg.isValid() || PhysReg.isPhysical()) &&
0649            "Not a physical register");
0650     return !(RegMask[PhysReg.id() / 32] & (1u << PhysReg.id() % 32));
0651   }
0652 
0653   /// clobbersPhysReg - Returns true if this RegMask operand clobbers PhysReg.
0654   bool clobbersPhysReg(MCRegister PhysReg) const {
0655      return clobbersPhysReg(getRegMask(), PhysReg);
0656   }
0657 
0658   /// getRegMask - Returns a bit mask of registers preserved by this RegMask
0659   /// operand.
0660   const uint32_t *getRegMask() const {
0661     assert(isRegMask() && "Wrong MachineOperand accessor");
0662     return Contents.RegMask;
0663   }
0664 
0665   /// Returns number of elements needed for a regmask array.
0666   static unsigned getRegMaskSize(unsigned NumRegs) {
0667     return (NumRegs + 31) / 32;
0668   }
0669 
0670   /// getRegLiveOut - Returns a bit mask of live-out registers.
0671   const uint32_t *getRegLiveOut() const {
0672     assert(isRegLiveOut() && "Wrong MachineOperand accessor");
0673     return Contents.RegMask;
0674   }
0675 
0676   const MDNode *getMetadata() const {
0677     assert(isMetadata() && "Wrong MachineOperand accessor");
0678     return Contents.MD;
0679   }
0680 
0681   //===--------------------------------------------------------------------===//
0682   // Mutators for various operand types.
0683   //===--------------------------------------------------------------------===//
0684 
0685   void setImm(int64_t immVal) {
0686     assert(isImm() && "Wrong MachineOperand mutator");
0687     Contents.ImmVal = immVal;
0688   }
0689 
0690   void setCImm(const ConstantInt *CI) {
0691     assert(isCImm() && "Wrong MachineOperand mutator");
0692     Contents.CI = CI;
0693   }
0694 
0695   void setFPImm(const ConstantFP *CFP) {
0696     assert(isFPImm() && "Wrong MachineOperand mutator");
0697     Contents.CFP = CFP;
0698   }
0699 
0700   void setOffset(int64_t Offset) {
0701     assert((isGlobal() || isSymbol() || isMCSymbol() || isCPI() ||
0702             isTargetIndex() || isBlockAddress()) &&
0703            "Wrong MachineOperand mutator");
0704     SmallContents.OffsetLo = unsigned(Offset);
0705     Contents.OffsetedInfo.OffsetHi = int(Offset >> 32);
0706   }
0707 
0708   void setIndex(int Idx) {
0709     assert((isFI() || isCPI() || isTargetIndex() || isJTI()) &&
0710            "Wrong MachineOperand mutator");
0711     Contents.OffsetedInfo.Val.Index = Idx;
0712   }
0713 
0714   void setMetadata(const MDNode *MD) {
0715     assert(isMetadata() && "Wrong MachineOperand mutator");
0716     Contents.MD = MD;
0717   }
0718 
0719   void setInstrRefInstrIndex(unsigned InstrIdx) {
0720     assert(isDbgInstrRef() && "Wrong MachineOperand mutator");
0721     Contents.InstrRef.InstrIdx = InstrIdx;
0722   }
0723   void setInstrRefOpIndex(unsigned OpIdx) {
0724     assert(isDbgInstrRef() && "Wrong MachineOperand mutator");
0725     Contents.InstrRef.OpIdx = OpIdx;
0726   }
0727 
0728   void setMBB(MachineBasicBlock *MBB) {
0729     assert(isMBB() && "Wrong MachineOperand mutator");
0730     Contents.MBB = MBB;
0731   }
0732 
0733   /// Sets value of register mask operand referencing Mask.  The
0734   /// operand does not take ownership of the memory referenced by Mask, it must
0735   /// remain valid for the lifetime of the operand. See CreateRegMask().
0736   /// Any physreg with a 0 bit in the mask is clobbered by the instruction.
0737   void setRegMask(const uint32_t *RegMaskPtr) {
0738     assert(isRegMask() && "Wrong MachineOperand mutator");
0739     Contents.RegMask = RegMaskPtr;
0740   }
0741 
0742   void setIntrinsicID(Intrinsic::ID IID) {
0743     assert(isIntrinsicID() && "Wrong MachineOperand mutator");
0744     Contents.IntrinsicID = IID;
0745   }
0746 
0747   void setPredicate(unsigned Predicate) {
0748     assert(isPredicate() && "Wrong MachineOperand mutator");
0749     Contents.Pred = Predicate;
0750   }
0751 
0752   //===--------------------------------------------------------------------===//
0753   // Other methods.
0754   //===--------------------------------------------------------------------===//
0755 
0756   /// Returns true if this operand is identical to the specified operand except
0757   /// for liveness related flags (isKill, isUndef and isDead). Note that this
0758   /// should stay in sync with the hash_value overload below.
0759   bool isIdenticalTo(const MachineOperand &Other) const;
0760 
0761   /// MachineOperand hash_value overload.
0762   ///
0763   /// Note that this includes the same information in the hash that
0764   /// isIdenticalTo uses for comparison. It is thus suited for use in hash
0765   /// tables which use that function for equality comparisons only. This must
0766   /// stay exactly in sync with isIdenticalTo above.
0767   friend hash_code hash_value(const MachineOperand &MO);
0768 
0769   /// ChangeToImmediate - Replace this operand with a new immediate operand of
0770   /// the specified value.  If an operand is known to be an immediate already,
0771   /// the setImm method should be used.
0772   void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags = 0);
0773 
0774   /// ChangeToFPImmediate - Replace this operand with a new FP immediate operand
0775   /// of the specified value.  If an operand is known to be an FP immediate
0776   /// already, the setFPImm method should be used.
0777   void ChangeToFPImmediate(const ConstantFP *FPImm, unsigned TargetFlags = 0);
0778 
0779   /// ChangeToES - Replace this operand with a new external symbol operand.
0780   void ChangeToES(const char *SymName, unsigned TargetFlags = 0);
0781 
0782   /// ChangeToGA - Replace this operand with a new global address operand.
0783   void ChangeToGA(const GlobalValue *GV, int64_t Offset,
0784                   unsigned TargetFlags = 0);
0785 
0786   /// ChangeToBA - Replace this operand with a new block address operand.
0787   void ChangeToBA(const BlockAddress *BA, int64_t Offset,
0788                   unsigned TargetFlags = 0);
0789 
0790   /// ChangeToMCSymbol - Replace this operand with a new MC symbol operand.
0791   void ChangeToMCSymbol(MCSymbol *Sym, unsigned TargetFlags = 0);
0792 
0793   /// Replace this operand with a frame index.
0794   void ChangeToFrameIndex(int Idx, unsigned TargetFlags = 0);
0795 
0796   /// Replace this operand with a target index.
0797   void ChangeToTargetIndex(unsigned Idx, int64_t Offset,
0798                            unsigned TargetFlags = 0);
0799 
0800   /// Replace this operand with an Instruction Reference.
0801   void ChangeToDbgInstrRef(unsigned InstrIdx, unsigned OpIdx,
0802                            unsigned TargetFlags = 0);
0803 
0804   /// ChangeToRegister - Replace this operand with a new register operand of
0805   /// the specified value.  If an operand is known to be an register already,
0806   /// the setReg method should be used.
0807   void ChangeToRegister(Register Reg, bool isDef, bool isImp = false,
0808                         bool isKill = false, bool isDead = false,
0809                         bool isUndef = false, bool isDebug = false);
0810 
0811   /// getTargetIndexName - If this MachineOperand is a TargetIndex that has a
0812   /// name, attempt to get the name. Returns nullptr if the TargetIndex does not
0813   /// have a name. Asserts if MO is not a TargetIndex.
0814   const char *getTargetIndexName() const;
0815 
0816   //===--------------------------------------------------------------------===//
0817   // Construction methods.
0818   //===--------------------------------------------------------------------===//
0819 
0820   static MachineOperand CreateImm(int64_t Val) {
0821     MachineOperand Op(MachineOperand::MO_Immediate);
0822     Op.setImm(Val);
0823     return Op;
0824   }
0825 
0826   static MachineOperand CreateCImm(const ConstantInt *CI) {
0827     MachineOperand Op(MachineOperand::MO_CImmediate);
0828     Op.Contents.CI = CI;
0829     return Op;
0830   }
0831 
0832   static MachineOperand CreateFPImm(const ConstantFP *CFP) {
0833     MachineOperand Op(MachineOperand::MO_FPImmediate);
0834     Op.Contents.CFP = CFP;
0835     return Op;
0836   }
0837 
0838   static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp = false,
0839                                   bool isKill = false, bool isDead = false,
0840                                   bool isUndef = false,
0841                                   bool isEarlyClobber = false,
0842                                   unsigned SubReg = 0, bool isDebug = false,
0843                                   bool isInternalRead = false,
0844                                   bool isRenamable = false) {
0845     assert(!(isDead && !isDef) && "Dead flag on non-def");
0846     assert(!(isKill && isDef) && "Kill flag on def");
0847     MachineOperand Op(MachineOperand::MO_Register);
0848     Op.IsDef = isDef;
0849     Op.IsImp = isImp;
0850     Op.IsDeadOrKill = isKill | isDead;
0851     Op.IsRenamable = isRenamable;
0852     Op.IsUndef = isUndef;
0853     Op.IsInternalRead = isInternalRead;
0854     Op.IsEarlyClobber = isEarlyClobber;
0855     Op.TiedTo = 0;
0856     Op.IsDebug = isDebug;
0857     Op.SmallContents.RegNo = Reg.id();
0858     Op.Contents.Reg.Prev = nullptr;
0859     Op.Contents.Reg.Next = nullptr;
0860     Op.setSubReg(SubReg);
0861     return Op;
0862   }
0863   static MachineOperand CreateMBB(MachineBasicBlock *MBB,
0864                                   unsigned TargetFlags = 0) {
0865     MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
0866     Op.setMBB(MBB);
0867     Op.setTargetFlags(TargetFlags);
0868     return Op;
0869   }
0870   static MachineOperand CreateFI(int Idx) {
0871     MachineOperand Op(MachineOperand::MO_FrameIndex);
0872     Op.setIndex(Idx);
0873     return Op;
0874   }
0875   static MachineOperand CreateCPI(unsigned Idx, int Offset,
0876                                   unsigned TargetFlags = 0) {
0877     MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
0878     Op.setIndex(Idx);
0879     Op.setOffset(Offset);
0880     Op.setTargetFlags(TargetFlags);
0881     return Op;
0882   }
0883   static MachineOperand CreateTargetIndex(unsigned Idx, int64_t Offset,
0884                                           unsigned TargetFlags = 0) {
0885     MachineOperand Op(MachineOperand::MO_TargetIndex);
0886     Op.setIndex(Idx);
0887     Op.setOffset(Offset);
0888     Op.setTargetFlags(TargetFlags);
0889     return Op;
0890   }
0891   static MachineOperand CreateJTI(unsigned Idx, unsigned TargetFlags = 0) {
0892     MachineOperand Op(MachineOperand::MO_JumpTableIndex);
0893     Op.setIndex(Idx);
0894     Op.setTargetFlags(TargetFlags);
0895     return Op;
0896   }
0897   static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset,
0898                                  unsigned TargetFlags = 0) {
0899     MachineOperand Op(MachineOperand::MO_GlobalAddress);
0900     Op.Contents.OffsetedInfo.Val.GV = GV;
0901     Op.setOffset(Offset);
0902     Op.setTargetFlags(TargetFlags);
0903     return Op;
0904   }
0905   static MachineOperand CreateES(const char *SymName,
0906                                  unsigned TargetFlags = 0) {
0907     MachineOperand Op(MachineOperand::MO_ExternalSymbol);
0908     Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
0909     Op.setOffset(0); // Offset is always 0.
0910     Op.setTargetFlags(TargetFlags);
0911     return Op;
0912   }
0913   static MachineOperand CreateBA(const BlockAddress *BA, int64_t Offset,
0914                                  unsigned TargetFlags = 0) {
0915     MachineOperand Op(MachineOperand::MO_BlockAddress);
0916     Op.Contents.OffsetedInfo.Val.BA = BA;
0917     Op.setOffset(Offset);
0918     Op.setTargetFlags(TargetFlags);
0919     return Op;
0920   }
0921   /// CreateRegMask - Creates a register mask operand referencing Mask.  The
0922   /// operand does not take ownership of the memory referenced by Mask, it
0923   /// must remain valid for the lifetime of the operand.
0924   ///
0925   /// A RegMask operand represents a set of non-clobbered physical registers
0926   /// on an instruction that clobbers many registers, typically a call.  The
0927   /// bit mask has a bit set for each physreg that is preserved by this
0928   /// instruction, as described in the documentation for
0929   /// TargetRegisterInfo::getCallPreservedMask().
0930   ///
0931   /// Any physreg with a 0 bit in the mask is clobbered by the instruction.
0932   ///
0933   static MachineOperand CreateRegMask(const uint32_t *Mask) {
0934     assert(Mask && "Missing register mask");
0935     MachineOperand Op(MachineOperand::MO_RegisterMask);
0936     Op.Contents.RegMask = Mask;
0937     return Op;
0938   }
0939   static MachineOperand CreateRegLiveOut(const uint32_t *Mask) {
0940     assert(Mask && "Missing live-out register mask");
0941     MachineOperand Op(MachineOperand::MO_RegisterLiveOut);
0942     Op.Contents.RegMask = Mask;
0943     return Op;
0944   }
0945   static MachineOperand CreateMetadata(const MDNode *Meta) {
0946     MachineOperand Op(MachineOperand::MO_Metadata);
0947     Op.Contents.MD = Meta;
0948     return Op;
0949   }
0950 
0951   static MachineOperand CreateMCSymbol(MCSymbol *Sym,
0952                                        unsigned TargetFlags = 0) {
0953     MachineOperand Op(MachineOperand::MO_MCSymbol);
0954     Op.Contents.Sym = Sym;
0955     Op.setOffset(0);
0956     Op.setTargetFlags(TargetFlags);
0957     return Op;
0958   }
0959 
0960   static MachineOperand CreateDbgInstrRef(unsigned InstrIdx, unsigned OpIdx) {
0961     MachineOperand Op(MachineOperand::MO_DbgInstrRef);
0962     Op.Contents.InstrRef.InstrIdx = InstrIdx;
0963     Op.Contents.InstrRef.OpIdx = OpIdx;
0964     return Op;
0965   }
0966 
0967   static MachineOperand CreateCFIIndex(unsigned CFIIndex) {
0968     MachineOperand Op(MachineOperand::MO_CFIIndex);
0969     Op.Contents.CFIIndex = CFIIndex;
0970     return Op;
0971   }
0972 
0973   static MachineOperand CreateIntrinsicID(Intrinsic::ID ID) {
0974     MachineOperand Op(MachineOperand::MO_IntrinsicID);
0975     Op.Contents.IntrinsicID = ID;
0976     return Op;
0977   }
0978 
0979   static MachineOperand CreatePredicate(unsigned Pred) {
0980     MachineOperand Op(MachineOperand::MO_Predicate);
0981     Op.Contents.Pred = Pred;
0982     return Op;
0983   }
0984 
0985   static MachineOperand CreateShuffleMask(ArrayRef<int> Mask) {
0986     MachineOperand Op(MachineOperand::MO_ShuffleMask);
0987     Op.Contents.ShuffleMask = Mask;
0988     return Op;
0989   }
0990 
0991   friend class MachineInstr;
0992   friend class MachineRegisterInfo;
0993 
0994 private:
0995   // If this operand is currently a register operand, and if this is in a
0996   // function, deregister the operand from the register's use/def list.
0997   void removeRegFromUses();
0998 
0999   /// Artificial kinds for DenseMap usage.
1000   enum : unsigned char {
1001     MO_Empty = MO_Last + 1,
1002     MO_Tombstone,
1003   };
1004 
1005   friend struct DenseMapInfo<MachineOperand>;
1006 
1007   //===--------------------------------------------------------------------===//
1008   // Methods for handling register use/def lists.
1009   //===--------------------------------------------------------------------===//
1010 
1011   /// isOnRegUseList - Return true if this operand is on a register use/def
1012   /// list or false if not.  This can only be called for register operands
1013   /// that are part of a machine instruction.
1014   bool isOnRegUseList() const {
1015     assert(isReg() && "Can only add reg operand to use lists");
1016     return Contents.Reg.Prev != nullptr;
1017   }
1018 };
1019 
1020 template <> struct DenseMapInfo<MachineOperand> {
1021   static MachineOperand getEmptyKey() {
1022     return MachineOperand(static_cast<MachineOperand::MachineOperandType>(
1023         MachineOperand::MO_Empty));
1024   }
1025   static MachineOperand getTombstoneKey() {
1026     return MachineOperand(static_cast<MachineOperand::MachineOperandType>(
1027         MachineOperand::MO_Tombstone));
1028   }
1029   static unsigned getHashValue(const MachineOperand &MO) {
1030     return hash_value(MO);
1031   }
1032   static bool isEqual(const MachineOperand &LHS, const MachineOperand &RHS) {
1033     if (LHS.getType() == static_cast<MachineOperand::MachineOperandType>(
1034                              MachineOperand::MO_Empty) ||
1035         LHS.getType() == static_cast<MachineOperand::MachineOperandType>(
1036                              MachineOperand::MO_Tombstone))
1037       return LHS.getType() == RHS.getType();
1038     return LHS.isIdenticalTo(RHS);
1039   }
1040 };
1041 
1042 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand &MO) {
1043   MO.print(OS);
1044   return OS;
1045 }
1046 
1047 // See friend declaration above. This additional declaration is required in
1048 // order to compile LLVM with IBM xlC compiler.
1049 hash_code hash_value(const MachineOperand &MO);
1050 } // namespace llvm
1051 
1052 #endif