MachineOperand.h revision 014278e6a11fa0767853b831e5bf51b95bf541c5
1//===-- llvm/CodeGen/MachineOperand.h - MachineOperand class ----*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains the declaration of the MachineOperand class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CODEGEN_MACHINEOPERAND_H 15#define LLVM_CODEGEN_MACHINEOPERAND_H 16 17#include "llvm/Support/DataTypes.h" 18#include <cassert> 19#include <iosfwd> 20 21namespace llvm { 22 23class ConstantFP; 24class MachineBasicBlock; 25class GlobalValue; 26class MachineInstr; 27class TargetMachine; 28class MachineRegisterInfo; 29class raw_ostream; 30 31/// MachineOperand class - Representation of each machine instruction operand. 32/// 33class MachineOperand { 34public: 35 enum MachineOperandType { 36 MO_Register, // Register operand. 37 MO_Immediate, // Immediate Operand 38 MO_FPImmediate, 39 MO_MachineBasicBlock, // MachineBasicBlock reference 40 MO_FrameIndex, // Abstract Stack Frame Index 41 MO_ConstantPoolIndex, // Address of indexed Constant in Constant Pool 42 MO_JumpTableIndex, // Address of indexed Jump Table for switch 43 MO_ExternalSymbol, // Name of external global symbol 44 MO_GlobalAddress // Address of a global value 45 }; 46 47private: 48 /// OpKind - Specify what kind of operand this is. This discriminates the 49 /// union. 50 MachineOperandType OpKind : 8; 51 52 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register 53 /// operands. 54 55 /// IsDef - True if this is a def, false if this is a use of the register. 56 /// 57 bool IsDef : 1; 58 59 /// IsImp - True if this is an implicit def or use, false if it is explicit. 60 /// 61 bool IsImp : 1; 62 63 /// IsKill - True if this instruction is the last use of the register on this 64 /// path through the function. This is only valid on uses of registers. 65 bool IsKill : 1; 66 67 /// IsDead - True if this register is never used by a subsequent instruction. 68 /// This is only valid on definitions of registers. 69 bool IsDead : 1; 70 71 /// IsEarlyClobber flag - this is only valid for MO_Register operands in 72 /// an inline asm. 73 74 /// IsEarlyClobber - True if this operand is marked earlyclobber in an 75 /// inline asm. See gcc doc for description of earlyclobber. 76 bool IsEarlyClobber : 1; 77 78 /// SubReg - Subregister number, only valid for MO_Register. A value of 0 79 /// indicates the MO_Register has no subReg. 80 unsigned char SubReg; 81 82 /// ParentMI - This is the instruction that this operand is embedded into. 83 /// This is valid for all operand types, when the operand is in an instr. 84 MachineInstr *ParentMI; 85 86 /// Contents union - This contains the payload for the various operand types. 87 union { 88 MachineBasicBlock *MBB; // For MO_MachineBasicBlock. 89 const ConstantFP *CFP; // For MO_FPImmediate. 90 int64_t ImmVal; // For MO_Immediate. 91 92 struct { // For MO_Register. 93 unsigned RegNo; 94 MachineOperand **Prev; // Access list for register. 95 MachineOperand *Next; 96 } Reg; 97 98 /// OffsetedInfo - This struct contains the offset and an object identifier. 99 /// this represent the object as with an optional offset from it. 100 struct { 101 union { 102 int Index; // For MO_*Index - The index itself. 103 const char *SymbolName; // For MO_ExternalSymbol. 104 GlobalValue *GV; // For MO_GlobalAddress. 105 } Val; 106 int Offset; // An offset from the object. 107 } OffsetedInfo; 108 } Contents; 109 110 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) {} 111public: 112 MachineOperand(const MachineOperand &M) { 113 *this = M; 114 } 115 116 ~MachineOperand() {} 117 118 /// getType - Returns the MachineOperandType for this operand. 119 /// 120 MachineOperandType getType() const { return OpKind; } 121 122 /// getParent - Return the instruction that this operand belongs to. 123 /// 124 MachineInstr *getParent() { return ParentMI; } 125 const MachineInstr *getParent() const { return ParentMI; } 126 127 void print(std::ostream &os, const TargetMachine *TM = 0) const; 128 void print(raw_ostream &os, const TargetMachine *TM = 0) const; 129 130 /// Accessors that tell you what kind of MachineOperand you're looking at. 131 /// 132 bool isRegister() const { return OpKind == MO_Register; } 133 bool isImmediate() const { return OpKind == MO_Immediate; } 134 bool isFPImmediate() const { return OpKind == MO_FPImmediate; } 135 bool isMachineBasicBlock() const { return OpKind == MO_MachineBasicBlock; } 136 bool isFrameIndex() const { return OpKind == MO_FrameIndex; } 137 bool isConstantPoolIndex() const { return OpKind == MO_ConstantPoolIndex; } 138 bool isJumpTableIndex() const { return OpKind == MO_JumpTableIndex; } 139 bool isGlobalAddress() const { return OpKind == MO_GlobalAddress; } 140 bool isExternalSymbol() const { return OpKind == MO_ExternalSymbol; } 141 142 //===--------------------------------------------------------------------===// 143 // Accessors for Register Operands 144 //===--------------------------------------------------------------------===// 145 146 /// getReg - Returns the register number. 147 unsigned getReg() const { 148 assert(isRegister() && "This is not a register operand!"); 149 return Contents.Reg.RegNo; 150 } 151 152 unsigned getSubReg() const { 153 assert(isRegister() && "Wrong MachineOperand accessor"); 154 return (unsigned)SubReg; 155 } 156 157 bool isUse() const { 158 assert(isRegister() && "Wrong MachineOperand accessor"); 159 return !IsDef; 160 } 161 162 bool isDef() const { 163 assert(isRegister() && "Wrong MachineOperand accessor"); 164 return IsDef; 165 } 166 167 bool isImplicit() const { 168 assert(isRegister() && "Wrong MachineOperand accessor"); 169 return IsImp; 170 } 171 172 bool isDead() const { 173 assert(isRegister() && "Wrong MachineOperand accessor"); 174 return IsDead; 175 } 176 177 bool isKill() const { 178 assert(isRegister() && "Wrong MachineOperand accessor"); 179 return IsKill; 180 } 181 182 bool isEarlyClobber() const { 183 assert(isRegister() && "Wrong MachineOperand accessor"); 184 return IsEarlyClobber; 185 } 186 187 /// getNextOperandForReg - Return the next MachineOperand in the function that 188 /// uses or defines this register. 189 MachineOperand *getNextOperandForReg() const { 190 assert(isRegister() && "This is not a register operand!"); 191 return Contents.Reg.Next; 192 } 193 194 //===--------------------------------------------------------------------===// 195 // Mutators for Register Operands 196 //===--------------------------------------------------------------------===// 197 198 /// Change the register this operand corresponds to. 199 /// 200 void setReg(unsigned Reg); 201 202 void setSubReg(unsigned subReg) { 203 assert(isRegister() && "Wrong MachineOperand accessor"); 204 SubReg = (unsigned char)subReg; 205 } 206 207 void setIsUse(bool Val = true) { 208 assert(isRegister() && "Wrong MachineOperand accessor"); 209 IsDef = !Val; 210 } 211 212 void setIsDef(bool Val = true) { 213 assert(isRegister() && "Wrong MachineOperand accessor"); 214 IsDef = Val; 215 } 216 217 void setImplicit(bool Val = true) { 218 assert(isRegister() && "Wrong MachineOperand accessor"); 219 IsImp = Val; 220 } 221 222 void setIsKill(bool Val = true) { 223 assert(isRegister() && !IsDef && "Wrong MachineOperand accessor"); 224 IsKill = Val; 225 } 226 227 void setIsDead(bool Val = true) { 228 assert(isRegister() && IsDef && "Wrong MachineOperand accessor"); 229 IsDead = Val; 230 } 231 232 void setIsEarlyClobber(bool Val = true) { 233 assert(isRegister() && IsDef && "Wrong MachineOperand accessor"); 234 IsEarlyClobber = Val; 235 } 236 237 //===--------------------------------------------------------------------===// 238 // Accessors for various operand types. 239 //===--------------------------------------------------------------------===// 240 241 int64_t getImm() const { 242 assert(isImmediate() && "Wrong MachineOperand accessor"); 243 return Contents.ImmVal; 244 } 245 246 const ConstantFP *getFPImm() const { 247 assert(isFPImmediate() && "Wrong MachineOperand accessor"); 248 return Contents.CFP; 249 } 250 251 MachineBasicBlock *getMBB() const { 252 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor"); 253 return Contents.MBB; 254 } 255 256 int getIndex() const { 257 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) && 258 "Wrong MachineOperand accessor"); 259 return Contents.OffsetedInfo.Val.Index; 260 } 261 262 GlobalValue *getGlobal() const { 263 assert(isGlobalAddress() && "Wrong MachineOperand accessor"); 264 return Contents.OffsetedInfo.Val.GV; 265 } 266 267 int getOffset() const { 268 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) && 269 "Wrong MachineOperand accessor"); 270 return Contents.OffsetedInfo.Offset; 271 } 272 273 const char *getSymbolName() const { 274 assert(isExternalSymbol() && "Wrong MachineOperand accessor"); 275 return Contents.OffsetedInfo.Val.SymbolName; 276 } 277 278 //===--------------------------------------------------------------------===// 279 // Mutators for various operand types. 280 //===--------------------------------------------------------------------===// 281 282 void setImm(int64_t immVal) { 283 assert(isImmediate() && "Wrong MachineOperand mutator"); 284 Contents.ImmVal = immVal; 285 } 286 287 void setOffset(int Offset) { 288 assert((isGlobalAddress() || isExternalSymbol() || isConstantPoolIndex()) && 289 "Wrong MachineOperand accessor"); 290 Contents.OffsetedInfo.Offset = Offset; 291 } 292 293 void setIndex(int Idx) { 294 assert((isFrameIndex() || isConstantPoolIndex() || isJumpTableIndex()) && 295 "Wrong MachineOperand accessor"); 296 Contents.OffsetedInfo.Val.Index = Idx; 297 } 298 299 void setMBB(MachineBasicBlock *MBB) { 300 assert(isMachineBasicBlock() && "Wrong MachineOperand accessor"); 301 Contents.MBB = MBB; 302 } 303 304 //===--------------------------------------------------------------------===// 305 // Other methods. 306 //===--------------------------------------------------------------------===// 307 308 /// isIdenticalTo - Return true if this operand is identical to the specified 309 /// operand. Note: This method ignores isKill and isDead properties. 310 bool isIdenticalTo(const MachineOperand &Other) const; 311 312 /// ChangeToImmediate - Replace this operand with a new immediate operand of 313 /// the specified value. If an operand is known to be an immediate already, 314 /// the setImm method should be used. 315 void ChangeToImmediate(int64_t ImmVal); 316 317 /// ChangeToRegister - Replace this operand with a new register operand of 318 /// the specified value. If an operand is known to be an register already, 319 /// the setReg method should be used. 320 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false, 321 bool isKill = false, bool isDead = false, 322 bool isEarlyClobber = false); 323 324 //===--------------------------------------------------------------------===// 325 // Construction methods. 326 //===--------------------------------------------------------------------===// 327 328 static MachineOperand CreateImm(int64_t Val) { 329 MachineOperand Op(MachineOperand::MO_Immediate); 330 Op.setImm(Val); 331 return Op; 332 } 333 334 static MachineOperand CreateFPImm(const ConstantFP *CFP) { 335 MachineOperand Op(MachineOperand::MO_FPImmediate); 336 Op.Contents.CFP = CFP; 337 return Op; 338 } 339 340 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false, 341 bool isKill = false, bool isDead = false, 342 unsigned SubReg = 0, 343 bool isEarlyClobber = false) { 344 MachineOperand Op(MachineOperand::MO_Register); 345 Op.IsDef = isDef; 346 Op.IsImp = isImp; 347 Op.IsKill = isKill; 348 Op.IsDead = isDead; 349 Op.IsEarlyClobber = isEarlyClobber; 350 Op.Contents.Reg.RegNo = Reg; 351 Op.Contents.Reg.Prev = 0; 352 Op.Contents.Reg.Next = 0; 353 Op.SubReg = SubReg; 354 return Op; 355 } 356 static MachineOperand CreateMBB(MachineBasicBlock *MBB) { 357 MachineOperand Op(MachineOperand::MO_MachineBasicBlock); 358 Op.setMBB(MBB); 359 return Op; 360 } 361 static MachineOperand CreateFI(unsigned Idx) { 362 MachineOperand Op(MachineOperand::MO_FrameIndex); 363 Op.setIndex(Idx); 364 return Op; 365 } 366 static MachineOperand CreateCPI(unsigned Idx, int Offset) { 367 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex); 368 Op.setIndex(Idx); 369 Op.setOffset(Offset); 370 return Op; 371 } 372 static MachineOperand CreateJTI(unsigned Idx) { 373 MachineOperand Op(MachineOperand::MO_JumpTableIndex); 374 Op.setIndex(Idx); 375 return Op; 376 } 377 static MachineOperand CreateGA(GlobalValue *GV, int Offset) { 378 MachineOperand Op(MachineOperand::MO_GlobalAddress); 379 Op.Contents.OffsetedInfo.Val.GV = GV; 380 Op.setOffset(Offset); 381 return Op; 382 } 383 static MachineOperand CreateES(const char *SymName, int Offset = 0) { 384 MachineOperand Op(MachineOperand::MO_ExternalSymbol); 385 Op.Contents.OffsetedInfo.Val.SymbolName = SymName; 386 Op.setOffset(Offset); 387 return Op; 388 } 389 const MachineOperand &operator=(const MachineOperand &MO) { 390 OpKind = MO.OpKind; 391 IsDef = MO.IsDef; 392 IsImp = MO.IsImp; 393 IsKill = MO.IsKill; 394 IsDead = MO.IsDead; 395 IsEarlyClobber = MO.IsEarlyClobber; 396 SubReg = MO.SubReg; 397 ParentMI = MO.ParentMI; 398 Contents = MO.Contents; 399 return *this; 400 } 401 402 friend class MachineInstr; 403 friend class MachineRegisterInfo; 404private: 405 //===--------------------------------------------------------------------===// 406 // Methods for handling register use/def lists. 407 //===--------------------------------------------------------------------===// 408 409 /// isOnRegUseList - Return true if this operand is on a register use/def list 410 /// or false if not. This can only be called for register operands that are 411 /// part of a machine instruction. 412 bool isOnRegUseList() const { 413 assert(isRegister() && "Can only add reg operand to use lists"); 414 return Contents.Reg.Prev != 0; 415 } 416 417 /// AddRegOperandToRegInfo - Add this register operand to the specified 418 /// MachineRegisterInfo. If it is null, then the next/prev fields should be 419 /// explicitly nulled out. 420 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo); 421 422 void RemoveRegOperandFromRegInfo() { 423 assert(isOnRegUseList() && "Reg operand is not on a use list"); 424 // Unlink this from the doubly linked list of operands. 425 MachineOperand *NextOp = Contents.Reg.Next; 426 *Contents.Reg.Prev = NextOp; 427 if (NextOp) { 428 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!"); 429 NextOp->Contents.Reg.Prev = Contents.Reg.Prev; 430 } 431 Contents.Reg.Prev = 0; 432 Contents.Reg.Next = 0; 433 } 434}; 435 436inline std::ostream &operator<<(std::ostream &OS, const MachineOperand &MO) { 437 MO.print(OS, 0); 438 return OS; 439} 440 441inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) { 442 MO.print(OS, 0); 443 return OS; 444} 445 446} // End llvm namespace 447 448#endif 449