LLVM API Documentation

MachineOperand.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineOperand.h - MachineOperand class ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declaration of the MachineOperand class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CODEGEN_MACHINEOPERAND_H
00015 #define LLVM_CODEGEN_MACHINEOPERAND_H
00016 
00017 #include "llvm/Support/DataTypes.h"
00018 #include <cassert>
00019 #include <iosfwd>
00020 
00021 namespace llvm {
00022   
00023 class ConstantFP;
00024 class MachineBasicBlock;
00025 class GlobalValue;
00026 class MDNode;
00027 class MachineInstr;
00028 class TargetMachine;
00029 class MachineRegisterInfo;
00030 class raw_ostream;
00031   
00032 /// MachineOperand class - Representation of each machine instruction operand.
00033 ///
00034 class MachineOperand {
00035 public:
00036   enum MachineOperandType {
00037     MO_Register,               ///< Register operand.
00038     MO_Immediate,              ///< Immediate operand
00039     MO_FPImmediate,            ///< Floating-point immediate operand
00040     MO_MachineBasicBlock,      ///< MachineBasicBlock reference
00041     MO_FrameIndex,             ///< Abstract Stack Frame Index
00042     MO_ConstantPoolIndex,      ///< Address of indexed Constant in Constant Pool
00043     MO_JumpTableIndex,         ///< Address of indexed Jump Table for switch
00044     MO_ExternalSymbol,         ///< Name of external global symbol
00045     MO_GlobalAddress,          ///< Address of a global value
00046     MO_Metadata                ///< Metadata info
00047   };
00048 
00049 private:
00050   /// OpKind - Specify what kind of operand this is.  This discriminates the
00051   /// union.
00052   unsigned char OpKind; // MachineOperandType
00053   
00054   /// SubReg - Subregister number, only valid for MO_Register.  A value of 0
00055   /// indicates the MO_Register has no subReg.
00056   unsigned char SubReg;
00057   
00058   /// TargetFlags - This is a set of target-specific operand flags.
00059   unsigned char TargetFlags;
00060   
00061   /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register
00062   /// operands.
00063   
00064   /// IsDef - True if this is a def, false if this is a use of the register.
00065   ///
00066   bool IsDef : 1;
00067   
00068   /// IsImp - True if this is an implicit def or use, false if it is explicit.
00069   ///
00070   bool IsImp : 1;
00071 
00072   /// IsKill - True if this instruction is the last use of the register on this
00073   /// path through the function.  This is only valid on uses of registers.
00074   bool IsKill : 1;
00075 
00076   /// IsDead - True if this register is never used by a subsequent instruction.
00077   /// This is only valid on definitions of registers.
00078   bool IsDead : 1;
00079 
00080   /// IsUndef - True if this is a register def / use of "undef", i.e. register
00081   /// defined by an IMPLICIT_DEF. This is only valid on registers.
00082   bool IsUndef : 1;
00083 
00084   /// IsEarlyClobber - True if this MO_Register 'def' operand is written to
00085   /// by the MachineInstr before all input registers are read.  This is used to
00086   /// model the GCC inline asm '&' constraint modifier.
00087   bool IsEarlyClobber : 1;
00088 
00089   /// ParentMI - This is the instruction that this operand is embedded into. 
00090   /// This is valid for all operand types, when the operand is in an instr.
00091   MachineInstr *ParentMI;
00092 
00093   /// Contents union - This contains the payload for the various operand types.
00094   union {
00095     MachineBasicBlock *MBB;   // For MO_MachineBasicBlock.
00096     const ConstantFP *CFP;    // For MO_FPImmediate.
00097     int64_t ImmVal;           // For MO_Immediate.
00098 
00099     struct {                  // For MO_Register.
00100       unsigned RegNo;
00101       MachineOperand **Prev;  // Access list for register.
00102       MachineOperand *Next;
00103     } Reg;
00104     
00105     /// OffsetedInfo - This struct contains the offset and an object identifier.
00106     /// this represent the object as with an optional offset from it.
00107     struct {
00108       union {
00109         int Index;                // For MO_*Index - The index itself.
00110         const char *SymbolName;   // For MO_ExternalSymbol.
00111         GlobalValue *GV;          // For MO_GlobalAddress.
00112         MDNode *Node;             // For MO_Metadata.
00113       } Val;
00114       int64_t Offset;   // An offset from the object.
00115     } OffsetedInfo;
00116   } Contents;
00117   
00118   explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {
00119     TargetFlags = 0;
00120   }
00121 public:
00122   /// getType - Returns the MachineOperandType for this operand.
00123   ///
00124   MachineOperandType getType() const { return (MachineOperandType)OpKind; }
00125   
00126   unsigned char getTargetFlags() const { return TargetFlags; }
00127   void setTargetFlags(unsigned char F) { TargetFlags = F; }
00128   void addTargetFlag(unsigned char F) { TargetFlags |= F; }
00129   
00130 
00131   /// getParent - Return the instruction that this operand belongs to.
00132   ///
00133   MachineInstr *getParent() { return ParentMI; }
00134   const MachineInstr *getParent() const { return ParentMI; }
00135   
00136   void print(std::ostream &os, const TargetMachine *TM = 0) const;
00137   void print(raw_ostream &os, const TargetMachine *TM = 0) const;
00138 
00139   //===--------------------------------------------------------------------===//
00140   // Accessors that tell you what kind of MachineOperand you're looking at.
00141   //===--------------------------------------------------------------------===//
00142 
00143   /// isReg - Tests if this is a MO_Register operand.
00144   bool isReg() const { return OpKind == MO_Register; }
00145   /// isImm - Tests if this is a MO_Immediate operand.
00146   bool isImm() const { return OpKind == MO_Immediate; }
00147   /// isFPImm - Tests if this is a MO_FPImmediate operand.
00148   bool isFPImm() const { return OpKind == MO_FPImmediate; }
00149   /// isMBB - Tests if this is a MO_MachineBasicBlock operand.
00150   bool isMBB() const { return OpKind == MO_MachineBasicBlock; }
00151   /// isFI - Tests if this is a MO_FrameIndex operand.
00152   bool isFI() const { return OpKind == MO_FrameIndex; }
00153   /// isCPI - Tests if this is a MO_ConstantPoolIndex operand.
00154   bool isCPI() const { return OpKind == MO_ConstantPoolIndex; }
00155   /// isJTI - Tests if this is a MO_JumpTableIndex operand.
00156   bool isJTI() const { return OpKind == MO_JumpTableIndex; }
00157   /// isGlobal - Tests if this is a MO_GlobalAddress operand.
00158   bool isGlobal() const { return OpKind == MO_GlobalAddress; }
00159   /// isSymbol - Tests if this is a MO_ExternalSymbol operand.
00160   bool isSymbol() const { return OpKind == MO_ExternalSymbol; }
00161   /// isMetadata - Tests if this is a MO_Metadata operand.
00162   bool isMetadata() const { return OpKind == MO_Metadata; }
00163 
00164   //===--------------------------------------------------------------------===//
00165   // Accessors for Register Operands
00166   //===--------------------------------------------------------------------===//
00167 
00168   /// getReg - Returns the register number.
00169   unsigned getReg() const {
00170     assert(isReg() && "This is not a register operand!");
00171     return Contents.Reg.RegNo;
00172   }
00173   
00174   unsigned getSubReg() const {
00175     assert(isReg() && "Wrong MachineOperand accessor");
00176     return (unsigned)SubReg;
00177   }
00178   
00179   bool isUse() const { 
00180     assert(isReg() && "Wrong MachineOperand accessor");
00181     return !IsDef;
00182   }
00183   
00184   bool isDef() const {
00185     assert(isReg() && "Wrong MachineOperand accessor");
00186     return IsDef;
00187   }
00188   
00189   bool isImplicit() const { 
00190     assert(isReg() && "Wrong MachineOperand accessor");
00191     return IsImp;
00192   }
00193   
00194   bool isDead() const {
00195     assert(isReg() && "Wrong MachineOperand accessor");
00196     return IsDead;
00197   }
00198   
00199   bool isKill() const {
00200     assert(isReg() && "Wrong MachineOperand accessor");
00201     return IsKill;
00202   }
00203   
00204   bool isUndef() const {
00205     assert(isReg() && "Wrong MachineOperand accessor");
00206     return IsUndef;
00207   }
00208   
00209   bool isEarlyClobber() const {
00210     assert(isReg() && "Wrong MachineOperand accessor");
00211     return IsEarlyClobber;
00212   }
00213 
00214   /// getNextOperandForReg - Return the next MachineOperand in the function that
00215   /// uses or defines this register.
00216   MachineOperand *getNextOperandForReg() const {
00217     assert(isReg() && "This is not a register operand!");
00218     return Contents.Reg.Next;
00219   }
00220 
00221   //===--------------------------------------------------------------------===//
00222   // Mutators for Register Operands
00223   //===--------------------------------------------------------------------===//
00224   
00225   /// Change the register this operand corresponds to.
00226   ///
00227   void setReg(unsigned Reg);
00228   
00229   void setSubReg(unsigned subReg) {
00230     assert(isReg() && "Wrong MachineOperand accessor");
00231     SubReg = (unsigned char)subReg;
00232   }
00233   
00234   void setIsUse(bool Val = true) {
00235     assert(isReg() && "Wrong MachineOperand accessor");
00236     IsDef = !Val;
00237   }
00238   
00239   void setIsDef(bool Val = true) {
00240     assert(isReg() && "Wrong MachineOperand accessor");
00241     IsDef = Val;
00242   }
00243 
00244   void setImplicit(bool Val = true) { 
00245     assert(isReg() && "Wrong MachineOperand accessor");
00246     IsImp = Val;
00247   }
00248 
00249   void setIsKill(bool Val = true) {
00250     assert(isReg() && !IsDef && "Wrong MachineOperand accessor");
00251     IsKill = Val;
00252   }
00253   
00254   void setIsDead(bool Val = true) {
00255     assert(isReg() && IsDef && "Wrong MachineOperand accessor");
00256     IsDead = Val;
00257   }
00258 
00259   void setIsUndef(bool Val = true) {
00260     assert(isReg() && "Wrong MachineOperand accessor");
00261     IsUndef = Val;
00262   }
00263   
00264   void setIsEarlyClobber(bool Val = true) {
00265     assert(isReg() && IsDef && "Wrong MachineOperand accessor");
00266     IsEarlyClobber = Val;
00267   }
00268 
00269   //===--------------------------------------------------------------------===//
00270   // Accessors for various operand types.
00271   //===--------------------------------------------------------------------===//
00272   
00273   int64_t getImm() const {
00274     assert(isImm() && "Wrong MachineOperand accessor");
00275     return Contents.ImmVal;
00276   }
00277   
00278   const ConstantFP *getFPImm() const {
00279     assert(isFPImm() && "Wrong MachineOperand accessor");
00280     return Contents.CFP;
00281   }
00282   
00283   MachineBasicBlock *getMBB() const {
00284     assert(isMBB() && "Wrong MachineOperand accessor");
00285     return Contents.MBB;
00286   }
00287 
00288   int getIndex() const {
00289     assert((isFI() || isCPI() || isJTI()) &&
00290            "Wrong MachineOperand accessor");
00291     return Contents.OffsetedInfo.Val.Index;
00292   }
00293   
00294   GlobalValue *getGlobal() const {
00295     assert(isGlobal() && "Wrong MachineOperand accessor");
00296     return Contents.OffsetedInfo.Val.GV;
00297   }
00298   
00299   MDNode *getMDNode() const {
00300     return Contents.OffsetedInfo.Val.Node;
00301   }
00302   
00303   int64_t getOffset() const {
00304     assert((isGlobal() || isSymbol() || isCPI()) &&
00305            "Wrong MachineOperand accessor");
00306     return Contents.OffsetedInfo.Offset;
00307   }
00308   
00309   const char *getSymbolName() const {
00310     assert(isSymbol() && "Wrong MachineOperand accessor");
00311     return Contents.OffsetedInfo.Val.SymbolName;
00312   }
00313   
00314   //===--------------------------------------------------------------------===//
00315   // Mutators for various operand types.
00316   //===--------------------------------------------------------------------===//
00317   
00318   void setImm(int64_t immVal) {
00319     assert(isImm() && "Wrong MachineOperand mutator");
00320     Contents.ImmVal = immVal;
00321   }
00322 
00323   void setOffset(int64_t Offset) {
00324     assert((isGlobal() || isSymbol() || isCPI() || isMetadata()) &&
00325         "Wrong MachineOperand accessor");
00326     Contents.OffsetedInfo.Offset = Offset;
00327   }
00328   
00329   void setIndex(int Idx) {
00330     assert((isFI() || isCPI() || isJTI()) &&
00331            "Wrong MachineOperand accessor");
00332     Contents.OffsetedInfo.Val.Index = Idx;
00333   }
00334   
00335   void setMBB(MachineBasicBlock *MBB) {
00336     assert(isMBB() && "Wrong MachineOperand accessor");
00337     Contents.MBB = MBB;
00338   }
00339   
00340   //===--------------------------------------------------------------------===//
00341   // Other methods.
00342   //===--------------------------------------------------------------------===//
00343   
00344   /// isIdenticalTo - Return true if this operand is identical to the specified
00345   /// operand. Note: This method ignores isKill and isDead properties.
00346   bool isIdenticalTo(const MachineOperand &Other) const;
00347   
00348   /// ChangeToImmediate - Replace this operand with a new immediate operand of
00349   /// the specified value.  If an operand is known to be an immediate already,
00350   /// the setImm method should be used.
00351   void ChangeToImmediate(int64_t ImmVal);
00352   
00353   /// ChangeToRegister - Replace this operand with a new register operand of
00354   /// the specified value.  If an operand is known to be an register already,
00355   /// the setReg method should be used.
00356   void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false,
00357                         bool isKill = false, bool isDead = false,
00358                         bool isUndef = false);
00359   
00360   //===--------------------------------------------------------------------===//
00361   // Construction methods.
00362   //===--------------------------------------------------------------------===//
00363   
00364   static MachineOperand CreateImm(int64_t Val) {
00365     MachineOperand Op(MachineOperand::MO_Immediate);
00366     Op.setImm(Val);
00367     return Op;
00368   }
00369   
00370   static MachineOperand CreateFPImm(const ConstantFP *CFP) {
00371     MachineOperand Op(MachineOperand::MO_FPImmediate);
00372     Op.Contents.CFP = CFP;
00373     return Op;
00374   }
00375   
00376   static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false,
00377                                   bool isKill = false, bool isDead = false,
00378                                   bool isUndef = false,
00379                                   bool isEarlyClobber = false,
00380                                   unsigned SubReg = 0) {
00381     MachineOperand Op(MachineOperand::MO_Register);
00382     Op.IsDef = isDef;
00383     Op.IsImp = isImp;
00384     Op.IsKill = isKill;
00385     Op.IsDead = isDead;
00386     Op.IsUndef = isUndef;
00387     Op.IsEarlyClobber = isEarlyClobber;
00388     Op.Contents.Reg.RegNo = Reg;
00389     Op.Contents.Reg.Prev = 0;
00390     Op.Contents.Reg.Next = 0;
00391     Op.SubReg = SubReg;
00392     return Op;
00393   }
00394   static MachineOperand CreateMBB(MachineBasicBlock *MBB,
00395                                   unsigned char TargetFlags = 0) {
00396     MachineOperand Op(MachineOperand::MO_MachineBasicBlock);
00397     Op.setMBB(MBB);
00398     Op.setTargetFlags(TargetFlags);
00399     return Op;
00400   }
00401   static MachineOperand CreateFI(unsigned Idx) {
00402     MachineOperand Op(MachineOperand::MO_FrameIndex);
00403     Op.setIndex(Idx);
00404     return Op;
00405   }
00406   static MachineOperand CreateCPI(unsigned Idx, int Offset,
00407                                   unsigned char TargetFlags = 0) {
00408     MachineOperand Op(MachineOperand::MO_ConstantPoolIndex);
00409     Op.setIndex(Idx);
00410     Op.setOffset(Offset);
00411     Op.setTargetFlags(TargetFlags);
00412     return Op;
00413   }
00414   static MachineOperand CreateJTI(unsigned Idx,
00415                                   unsigned char TargetFlags = 0) {
00416     MachineOperand Op(MachineOperand::MO_JumpTableIndex);
00417     Op.setIndex(Idx);
00418     Op.setTargetFlags(TargetFlags);
00419     return Op;
00420   }
00421   static MachineOperand CreateGA(GlobalValue *GV, int64_t Offset,
00422                                  unsigned char TargetFlags = 0) {
00423     MachineOperand Op(MachineOperand::MO_GlobalAddress);
00424     Op.Contents.OffsetedInfo.Val.GV = GV;
00425     Op.setOffset(Offset);
00426     Op.setTargetFlags(TargetFlags);
00427     return Op;
00428   }
00429   static MachineOperand CreateMDNode(MDNode *N, int64_t Offset,
00430                                      unsigned char TargetFlags = 0) {
00431     MachineOperand Op(MachineOperand::MO_Metadata);
00432     Op.Contents.OffsetedInfo.Val.Node = N;
00433     Op.setOffset(Offset);
00434     Op.setTargetFlags(TargetFlags);
00435     return Op;
00436   }
00437   static MachineOperand CreateES(const char *SymName, int64_t Offset = 0,
00438                                  unsigned char TargetFlags = 0) {
00439     MachineOperand Op(MachineOperand::MO_ExternalSymbol);
00440     Op.Contents.OffsetedInfo.Val.SymbolName = SymName;
00441     Op.setOffset(Offset);
00442     Op.setTargetFlags(TargetFlags);
00443     return Op;
00444   }
00445 
00446   friend class MachineInstr;
00447   friend class MachineRegisterInfo;
00448 private:
00449   //===--------------------------------------------------------------------===//
00450   // Methods for handling register use/def lists.
00451   //===--------------------------------------------------------------------===//
00452 
00453   /// isOnRegUseList - Return true if this operand is on a register use/def list
00454   /// or false if not.  This can only be called for register operands that are
00455   /// part of a machine instruction.
00456   bool isOnRegUseList() const {
00457     assert(isReg() && "Can only add reg operand to use lists");
00458     return Contents.Reg.Prev != 0;
00459   }
00460   
00461   /// AddRegOperandToRegInfo - Add this register operand to the specified
00462   /// MachineRegisterInfo.  If it is null, then the next/prev fields should be
00463   /// explicitly nulled out.
00464   void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo);
00465 
00466   /// RemoveRegOperandFromRegInfo - Remove this register operand from the
00467   /// MachineRegisterInfo it is linked with.
00468   void RemoveRegOperandFromRegInfo();
00469 };
00470 
00471 inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) {
00472   MO.print(OS, 0);
00473   return OS;
00474 }
00475 
00476 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) {
00477   MO.print(OS, 0);
00478   return OS;
00479 }
00480 
00481 } // End llvm namespace
00482 
00483 #endif