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 20 namespace llvm { 21 22 class BlockAddress; 23 class ConstantFP; 24 class ConstantInt; 25 class GlobalValue; 26 class MachineBasicBlock; 27 class MachineInstr; 28 class MachineRegisterInfo; 29 class MDNode; 30 class TargetMachine; 31 class TargetRegisterInfo; 32 class raw_ostream; 33 class MCSymbol; 34 35 /// MachineOperand class - Representation of each machine instruction operand. 36 /// 37 class MachineOperand { 38 public: 39 enum MachineOperandType { 40 MO_Register, ///< Register operand. 41 MO_Immediate, ///< Immediate operand 42 MO_CImmediate, ///< Immediate >64bit operand 43 MO_FPImmediate, ///< Floating-point immediate operand 44 MO_MachineBasicBlock, ///< MachineBasicBlock reference 45 MO_FrameIndex, ///< Abstract Stack Frame Index 46 MO_ConstantPoolIndex, ///< Address of indexed Constant in Constant Pool 47 MO_JumpTableIndex, ///< Address of indexed Jump Table for switch 48 MO_ExternalSymbol, ///< Name of external global symbol 49 MO_GlobalAddress, ///< Address of a global value 50 MO_BlockAddress, ///< Address of a basic block 51 MO_Metadata, ///< Metadata reference (for debug info) 52 MO_MCSymbol ///< MCSymbol reference (for debug/eh info) 53 }; 54 55 private: 56 /// OpKind - Specify what kind of operand this is. This discriminates the 57 /// union. 58 unsigned char OpKind; // MachineOperandType 59 60 /// SubReg - Subregister number, only valid for MO_Register. A value of 0 61 /// indicates the MO_Register has no subReg. 62 unsigned char SubReg; 63 64 /// TargetFlags - This is a set of target-specific operand flags. 65 unsigned char TargetFlags; 66 67 /// IsDef/IsImp/IsKill/IsDead flags - These are only valid for MO_Register 68 /// operands. 69 70 /// IsDef - True if this is a def, false if this is a use of the register. 71 /// 72 bool IsDef : 1; 73 74 /// IsImp - True if this is an implicit def or use, false if it is explicit. 75 /// 76 bool IsImp : 1; 77 78 /// IsKill - True if this instruction is the last use of the register on this 79 /// path through the function. This is only valid on uses of registers. 80 bool IsKill : 1; 81 82 /// IsDead - True if this register is never used by a subsequent instruction. 83 /// This is only valid on definitions of registers. 84 bool IsDead : 1; 85 86 /// IsUndef - True if this is a register def / use of "undef", i.e. register 87 /// defined by an IMPLICIT_DEF. This is only valid on registers. 88 bool IsUndef : 1; 89 90 /// IsEarlyClobber - True if this MO_Register 'def' operand is written to 91 /// by the MachineInstr before all input registers are read. This is used to 92 /// model the GCC inline asm '&' constraint modifier. 93 bool IsEarlyClobber : 1; 94 95 /// IsDebug - True if this MO_Register 'use' operand is in a debug pseudo, 96 /// not a real instruction. Such uses should be ignored during codegen. 97 bool IsDebug : 1; 98 99 /// SmallContents - This really should be part of the Contents union, but 100 /// lives out here so we can get a better packed struct. 101 /// MO_Register: Register number. 102 /// OffsetedInfo: Low bits of offset. 103 union { 104 unsigned RegNo; // For MO_Register. 105 unsigned OffsetLo; // Matches Contents.OffsetedInfo.OffsetHi. 106 } SmallContents; 107 108 /// ParentMI - This is the instruction that this operand is embedded into. 109 /// This is valid for all operand types, when the operand is in an instr. 110 MachineInstr *ParentMI; 111 112 /// Contents union - This contains the payload for the various operand types. 113 union { 114 MachineBasicBlock *MBB; // For MO_MachineBasicBlock. 115 const ConstantFP *CFP; // For MO_FPImmediate. 116 const ConstantInt *CI; // For MO_CImmediate. Integers > 64bit. 117 int64_t ImmVal; // For MO_Immediate. 118 const MDNode *MD; // For MO_Metadata. 119 MCSymbol *Sym; // For MO_MCSymbol 120 121 struct { // For MO_Register. 122 // Register number is in SmallContents.RegNo. 123 MachineOperand **Prev; // Access list for register. 124 MachineOperand *Next; 125 } Reg; 126 127 /// OffsetedInfo - This struct contains the offset and an object identifier. 128 /// this represent the object as with an optional offset from it. 129 struct { 130 union { 131 int Index; // For MO_*Index - The index itself. 132 const char *SymbolName; // For MO_ExternalSymbol. 133 const GlobalValue *GV; // For MO_GlobalAddress. 134 const BlockAddress *BA; // For MO_BlockAddress. 135 } Val; 136 // Low bits of offset are in SmallContents.OffsetLo. 137 int OffsetHi; // An offset from the object, high 32 bits. 138 } OffsetedInfo; 139 } Contents; 140 141 explicit MachineOperand(MachineOperandType K) : OpKind(K), ParentMI(0) { 142 TargetFlags = 0; 143 } 144 public: 145 /// getType - Returns the MachineOperandType for this operand. 146 /// 147 MachineOperandType getType() const { return (MachineOperandType)OpKind; } 148 149 unsigned char getTargetFlags() const { return TargetFlags; } 150 void setTargetFlags(unsigned char F) { TargetFlags = F; } 151 void addTargetFlag(unsigned char F) { TargetFlags |= F; } 152 153 154 /// getParent - Return the instruction that this operand belongs to. 155 /// 156 MachineInstr *getParent() { return ParentMI; } 157 const MachineInstr *getParent() const { return ParentMI; } 158 159 /// clearParent - Reset the parent pointer. 160 /// 161 /// The MachineOperand copy constructor also copies ParentMI, expecting the 162 /// original to be deleted. If a MachineOperand is ever stored outside a 163 /// MachineInstr, the parent pointer must be cleared. 164 /// 165 /// Never call clearParent() on an operand in a MachineInstr. 166 /// 167 void clearParent() { ParentMI = 0; } 168 169 void print(raw_ostream &os, const TargetMachine *TM = 0) const; 170 171 //===--------------------------------------------------------------------===// 172 // Accessors that tell you what kind of MachineOperand you're looking at. 173 //===--------------------------------------------------------------------===// 174 175 /// isReg - Tests if this is a MO_Register operand. 176 bool isReg() const { return OpKind == MO_Register; } 177 /// isImm - Tests if this is a MO_Immediate operand. 178 bool isImm() const { return OpKind == MO_Immediate; } 179 /// isCImm - Test if t his is a MO_CImmediate operand. 180 bool isCImm() const { return OpKind == MO_CImmediate; } 181 /// isFPImm - Tests if this is a MO_FPImmediate operand. 182 bool isFPImm() const { return OpKind == MO_FPImmediate; } 183 /// isMBB - Tests if this is a MO_MachineBasicBlock operand. 184 bool isMBB() const { return OpKind == MO_MachineBasicBlock; } 185 /// isFI - Tests if this is a MO_FrameIndex operand. 186 bool isFI() const { return OpKind == MO_FrameIndex; } 187 /// isCPI - Tests if this is a MO_ConstantPoolIndex operand. 188 bool isCPI() const { return OpKind == MO_ConstantPoolIndex; } 189 /// isJTI - Tests if this is a MO_JumpTableIndex operand. 190 bool isJTI() const { return OpKind == MO_JumpTableIndex; } 191 /// isGlobal - Tests if this is a MO_GlobalAddress operand. 192 bool isGlobal() const { return OpKind == MO_GlobalAddress; } 193 /// isSymbol - Tests if this is a MO_ExternalSymbol operand. 194 bool isSymbol() const { return OpKind == MO_ExternalSymbol; } 195 /// isBlockAddress - Tests if this is a MO_BlockAddress operand. 196 bool isBlockAddress() const { return OpKind == MO_BlockAddress; } 197 /// isMetadata - Tests if this is a MO_Metadata operand. 198 bool isMetadata() const { return OpKind == MO_Metadata; } 199 bool isMCSymbol() const { return OpKind == MO_MCSymbol; } 200 201 //===--------------------------------------------------------------------===// 202 // Accessors for Register Operands 203 //===--------------------------------------------------------------------===// 204 205 /// getReg - Returns the register number. 206 unsigned getReg() const { 207 assert(isReg() && "This is not a register operand!"); 208 return SmallContents.RegNo; 209 } 210 211 unsigned getSubReg() const { 212 assert(isReg() && "Wrong MachineOperand accessor"); 213 return (unsigned)SubReg; 214 } 215 216 bool isUse() const { 217 assert(isReg() && "Wrong MachineOperand accessor"); 218 return !IsDef; 219 } 220 221 bool isDef() const { 222 assert(isReg() && "Wrong MachineOperand accessor"); 223 return IsDef; 224 } 225 226 bool isImplicit() const { 227 assert(isReg() && "Wrong MachineOperand accessor"); 228 return IsImp; 229 } 230 231 bool isDead() const { 232 assert(isReg() && "Wrong MachineOperand accessor"); 233 return IsDead; 234 } 235 236 bool isKill() const { 237 assert(isReg() && "Wrong MachineOperand accessor"); 238 return IsKill; 239 } 240 241 bool isUndef() const { 242 assert(isReg() && "Wrong MachineOperand accessor"); 243 return IsUndef; 244 } 245 246 bool isEarlyClobber() const { 247 assert(isReg() && "Wrong MachineOperand accessor"); 248 return IsEarlyClobber; 249 } 250 251 bool isDebug() const { 252 assert(isReg() && "Wrong MachineOperand accessor"); 253 return IsDebug; 254 } 255 256 /// getNextOperandForReg - Return the next MachineOperand in the function that 257 /// uses or defines this register. 258 MachineOperand *getNextOperandForReg() const { 259 assert(isReg() && "This is not a register operand!"); 260 return Contents.Reg.Next; 261 } 262 263 //===--------------------------------------------------------------------===// 264 // Mutators for Register Operands 265 //===--------------------------------------------------------------------===// 266 267 /// Change the register this operand corresponds to. 268 /// 269 void setReg(unsigned Reg); 270 271 void setSubReg(unsigned subReg) { 272 assert(isReg() && "Wrong MachineOperand accessor"); 273 SubReg = (unsigned char)subReg; 274 } 275 276 /// substVirtReg - Substitute the current register with the virtual 277 /// subregister Reg:SubReg. Take any existing SubReg index into account, 278 /// using TargetRegisterInfo to compose the subreg indices if necessary. 279 /// Reg must be a virtual register, SubIdx can be 0. 280 /// 281 void substVirtReg(unsigned Reg, unsigned SubIdx, const TargetRegisterInfo&); 282 283 /// substPhysReg - Substitute the current register with the physical register 284 /// Reg, taking any existing SubReg into account. For instance, 285 /// substPhysReg(%EAX) will change %reg1024:sub_8bit to %AL. 286 /// 287 void substPhysReg(unsigned Reg, const TargetRegisterInfo&); 288 289 void setIsUse(bool Val = true) { 290 assert(isReg() && "Wrong MachineOperand accessor"); 291 assert((Val || !isDebug()) && "Marking a debug operation as def"); 292 IsDef = !Val; 293 } 294 295 void setIsDef(bool Val = true) { 296 assert(isReg() && "Wrong MachineOperand accessor"); 297 assert((!Val || !isDebug()) && "Marking a debug operation as def"); 298 IsDef = Val; 299 } 300 301 void setImplicit(bool Val = true) { 302 assert(isReg() && "Wrong MachineOperand accessor"); 303 IsImp = Val; 304 } 305 306 void setIsKill(bool Val = true) { 307 assert(isReg() && !IsDef && "Wrong MachineOperand accessor"); 308 assert((!Val || !isDebug()) && "Marking a debug operation as kill"); 309 IsKill = Val; 310 } 311 312 void setIsDead(bool Val = true) { 313 assert(isReg() && IsDef && "Wrong MachineOperand accessor"); 314 IsDead = Val; 315 } 316 317 void setIsUndef(bool Val = true) { 318 assert(isReg() && "Wrong MachineOperand accessor"); 319 IsUndef = Val; 320 } 321 322 void setIsEarlyClobber(bool Val = true) { 323 assert(isReg() && IsDef && "Wrong MachineOperand accessor"); 324 IsEarlyClobber = Val; 325 } 326 327 void setIsDebug(bool Val = true) { 328 assert(isReg() && IsDef && "Wrong MachineOperand accessor"); 329 IsDebug = Val; 330 } 331 332 //===--------------------------------------------------------------------===// 333 // Accessors for various operand types. 334 //===--------------------------------------------------------------------===// 335 336 int64_t getImm() const { 337 assert(isImm() && "Wrong MachineOperand accessor"); 338 return Contents.ImmVal; 339 } 340 341 const ConstantInt *getCImm() const { 342 assert(isCImm() && "Wrong MachineOperand accessor"); 343 return Contents.CI; 344 } 345 346 const ConstantFP *getFPImm() const { 347 assert(isFPImm() && "Wrong MachineOperand accessor"); 348 return Contents.CFP; 349 } 350 351 MachineBasicBlock *getMBB() const { 352 assert(isMBB() && "Wrong MachineOperand accessor"); 353 return Contents.MBB; 354 } 355 356 int getIndex() const { 357 assert((isFI() || isCPI() || isJTI()) && 358 "Wrong MachineOperand accessor"); 359 return Contents.OffsetedInfo.Val.Index; 360 } 361 362 const GlobalValue *getGlobal() const { 363 assert(isGlobal() && "Wrong MachineOperand accessor"); 364 return Contents.OffsetedInfo.Val.GV; 365 } 366 367 const BlockAddress *getBlockAddress() const { 368 assert(isBlockAddress() && "Wrong MachineOperand accessor"); 369 return Contents.OffsetedInfo.Val.BA; 370 } 371 372 MCSymbol *getMCSymbol() const { 373 assert(isMCSymbol() && "Wrong MachineOperand accessor"); 374 return Contents.Sym; 375 } 376 377 /// getOffset - Return the offset from the symbol in this operand. This always 378 /// returns 0 for ExternalSymbol operands. 379 int64_t getOffset() const { 380 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) && 381 "Wrong MachineOperand accessor"); 382 return (int64_t(Contents.OffsetedInfo.OffsetHi) << 32) | 383 SmallContents.OffsetLo; 384 } 385 386 const char *getSymbolName() const { 387 assert(isSymbol() && "Wrong MachineOperand accessor"); 388 return Contents.OffsetedInfo.Val.SymbolName; 389 } 390 391 const MDNode *getMetadata() const { 392 assert(isMetadata() && "Wrong MachineOperand accessor"); 393 return Contents.MD; 394 } 395 396 //===--------------------------------------------------------------------===// 397 // Mutators for various operand types. 398 //===--------------------------------------------------------------------===// 399 400 void setImm(int64_t immVal) { 401 assert(isImm() && "Wrong MachineOperand mutator"); 402 Contents.ImmVal = immVal; 403 } 404 405 void setOffset(int64_t Offset) { 406 assert((isGlobal() || isSymbol() || isCPI() || isBlockAddress()) && 407 "Wrong MachineOperand accessor"); 408 SmallContents.OffsetLo = unsigned(Offset); 409 Contents.OffsetedInfo.OffsetHi = int(Offset >> 32); 410 } 411 412 void setIndex(int Idx) { 413 assert((isFI() || isCPI() || isJTI()) && 414 "Wrong MachineOperand accessor"); 415 Contents.OffsetedInfo.Val.Index = Idx; 416 } 417 418 void setMBB(MachineBasicBlock *MBB) { 419 assert(isMBB() && "Wrong MachineOperand accessor"); 420 Contents.MBB = MBB; 421 } 422 423 //===--------------------------------------------------------------------===// 424 // Other methods. 425 //===--------------------------------------------------------------------===// 426 427 /// isIdenticalTo - Return true if this operand is identical to the specified 428 /// operand. Note: This method ignores isKill and isDead properties. 429 bool isIdenticalTo(const MachineOperand &Other) const; 430 431 /// ChangeToImmediate - Replace this operand with a new immediate operand of 432 /// the specified value. If an operand is known to be an immediate already, 433 /// the setImm method should be used. 434 void ChangeToImmediate(int64_t ImmVal); 435 436 /// ChangeToRegister - Replace this operand with a new register operand of 437 /// the specified value. If an operand is known to be an register already, 438 /// the setReg method should be used. 439 void ChangeToRegister(unsigned Reg, bool isDef, bool isImp = false, 440 bool isKill = false, bool isDead = false, 441 bool isUndef = false, bool isDebug = false); 442 443 //===--------------------------------------------------------------------===// 444 // Construction methods. 445 //===--------------------------------------------------------------------===// 446 447 static MachineOperand CreateImm(int64_t Val) { 448 MachineOperand Op(MachineOperand::MO_Immediate); 449 Op.setImm(Val); 450 return Op; 451 } 452 453 static MachineOperand CreateCImm(const ConstantInt *CI) { 454 MachineOperand Op(MachineOperand::MO_CImmediate); 455 Op.Contents.CI = CI; 456 return Op; 457 } 458 459 static MachineOperand CreateFPImm(const ConstantFP *CFP) { 460 MachineOperand Op(MachineOperand::MO_FPImmediate); 461 Op.Contents.CFP = CFP; 462 return Op; 463 } 464 465 static MachineOperand CreateReg(unsigned Reg, bool isDef, bool isImp = false, 466 bool isKill = false, bool isDead = false, 467 bool isUndef = false, 468 bool isEarlyClobber = false, 469 unsigned SubReg = 0, 470 bool isDebug = false) { 471 MachineOperand Op(MachineOperand::MO_Register); 472 Op.IsDef = isDef; 473 Op.IsImp = isImp; 474 Op.IsKill = isKill; 475 Op.IsDead = isDead; 476 Op.IsUndef = isUndef; 477 Op.IsEarlyClobber = isEarlyClobber; 478 Op.IsDebug = isDebug; 479 Op.SmallContents.RegNo = Reg; 480 Op.Contents.Reg.Prev = 0; 481 Op.Contents.Reg.Next = 0; 482 Op.SubReg = SubReg; 483 return Op; 484 } 485 static MachineOperand CreateMBB(MachineBasicBlock *MBB, 486 unsigned char TargetFlags = 0) { 487 MachineOperand Op(MachineOperand::MO_MachineBasicBlock); 488 Op.setMBB(MBB); 489 Op.setTargetFlags(TargetFlags); 490 return Op; 491 } 492 static MachineOperand CreateFI(int Idx) { 493 MachineOperand Op(MachineOperand::MO_FrameIndex); 494 Op.setIndex(Idx); 495 return Op; 496 } 497 static MachineOperand CreateCPI(unsigned Idx, int Offset, 498 unsigned char TargetFlags = 0) { 499 MachineOperand Op(MachineOperand::MO_ConstantPoolIndex); 500 Op.setIndex(Idx); 501 Op.setOffset(Offset); 502 Op.setTargetFlags(TargetFlags); 503 return Op; 504 } 505 static MachineOperand CreateJTI(unsigned Idx, 506 unsigned char TargetFlags = 0) { 507 MachineOperand Op(MachineOperand::MO_JumpTableIndex); 508 Op.setIndex(Idx); 509 Op.setTargetFlags(TargetFlags); 510 return Op; 511 } 512 static MachineOperand CreateGA(const GlobalValue *GV, int64_t Offset, 513 unsigned char TargetFlags = 0) { 514 MachineOperand Op(MachineOperand::MO_GlobalAddress); 515 Op.Contents.OffsetedInfo.Val.GV = GV; 516 Op.setOffset(Offset); 517 Op.setTargetFlags(TargetFlags); 518 return Op; 519 } 520 static MachineOperand CreateES(const char *SymName, 521 unsigned char TargetFlags = 0) { 522 MachineOperand Op(MachineOperand::MO_ExternalSymbol); 523 Op.Contents.OffsetedInfo.Val.SymbolName = SymName; 524 Op.setOffset(0); // Offset is always 0. 525 Op.setTargetFlags(TargetFlags); 526 return Op; 527 } 528 static MachineOperand CreateBA(const BlockAddress *BA, 529 unsigned char TargetFlags = 0) { 530 MachineOperand Op(MachineOperand::MO_BlockAddress); 531 Op.Contents.OffsetedInfo.Val.BA = BA; 532 Op.setOffset(0); // Offset is always 0. 533 Op.setTargetFlags(TargetFlags); 534 return Op; 535 } 536 static MachineOperand CreateMetadata(const MDNode *Meta) { 537 MachineOperand Op(MachineOperand::MO_Metadata); 538 Op.Contents.MD = Meta; 539 return Op; 540 } 541 542 static MachineOperand CreateMCSymbol(MCSymbol *Sym) { 543 MachineOperand Op(MachineOperand::MO_MCSymbol); 544 Op.Contents.Sym = Sym; 545 return Op; 546 } 547 548 friend class MachineInstr; 549 friend class MachineRegisterInfo; 550 private: 551 //===--------------------------------------------------------------------===// 552 // Methods for handling register use/def lists. 553 //===--------------------------------------------------------------------===// 554 555 /// isOnRegUseList - Return true if this operand is on a register use/def list 556 /// or false if not. This can only be called for register operands that are 557 /// part of a machine instruction. 558 bool isOnRegUseList() const { 559 assert(isReg() && "Can only add reg operand to use lists"); 560 return Contents.Reg.Prev != 0; 561 } 562 563 /// AddRegOperandToRegInfo - Add this register operand to the specified 564 /// MachineRegisterInfo. If it is null, then the next/prev fields should be 565 /// explicitly nulled out. 566 void AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo); 567 568 /// RemoveRegOperandFromRegInfo - Remove this register operand from the 569 /// MachineRegisterInfo it is linked with. 570 void RemoveRegOperandFromRegInfo(); 571 }; 572 573 inline raw_ostream &operator<<(raw_ostream &OS, const MachineOperand& MO) { 574 MO.print(OS, 0); 575 return OS; 576 } 577 578 } // End llvm namespace 579 580 #endif 581