1 //===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===// 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 // Methods common to all machine instructions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/MachineInstr.h" 15 #include "llvm/Constants.h" 16 #include "llvm/Function.h" 17 #include "llvm/InlineAsm.h" 18 #include "llvm/Metadata.h" 19 #include "llvm/Type.h" 20 #include "llvm/Value.h" 21 #include "llvm/Assembly/Writer.h" 22 #include "llvm/CodeGen/MachineConstantPool.h" 23 #include "llvm/CodeGen/MachineFunction.h" 24 #include "llvm/CodeGen/MachineMemOperand.h" 25 #include "llvm/CodeGen/MachineRegisterInfo.h" 26 #include "llvm/CodeGen/PseudoSourceValue.h" 27 #include "llvm/MC/MCSymbol.h" 28 #include "llvm/Target/TargetMachine.h" 29 #include "llvm/Target/TargetInstrInfo.h" 30 #include "llvm/Target/TargetInstrDesc.h" 31 #include "llvm/Target/TargetRegisterInfo.h" 32 #include "llvm/Analysis/AliasAnalysis.h" 33 #include "llvm/Analysis/DebugInfo.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/LeakDetector.h" 37 #include "llvm/Support/MathExtras.h" 38 #include "llvm/Support/raw_ostream.h" 39 #include "llvm/ADT/FoldingSet.h" 40 using namespace llvm; 41 42 //===----------------------------------------------------------------------===// 43 // MachineOperand Implementation 44 //===----------------------------------------------------------------------===// 45 46 /// AddRegOperandToRegInfo - Add this register operand to the specified 47 /// MachineRegisterInfo. If it is null, then the next/prev fields should be 48 /// explicitly nulled out. 49 void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { 50 assert(isReg() && "Can only add reg operand to use lists"); 51 52 // If the reginfo pointer is null, just explicitly null out or next/prev 53 // pointers, to ensure they are not garbage. 54 if (RegInfo == 0) { 55 Contents.Reg.Prev = 0; 56 Contents.Reg.Next = 0; 57 return; 58 } 59 60 // Otherwise, add this operand to the head of the registers use/def list. 61 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); 62 63 // For SSA values, we prefer to keep the definition at the start of the list. 64 // we do this by skipping over the definition if it is at the head of the 65 // list. 66 if (*Head && (*Head)->isDef()) 67 Head = &(*Head)->Contents.Reg.Next; 68 69 Contents.Reg.Next = *Head; 70 if (Contents.Reg.Next) { 71 assert(getReg() == Contents.Reg.Next->getReg() && 72 "Different regs on the same list!"); 73 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; 74 } 75 76 Contents.Reg.Prev = Head; 77 *Head = this; 78 } 79 80 /// RemoveRegOperandFromRegInfo - Remove this register operand from the 81 /// MachineRegisterInfo it is linked with. 82 void MachineOperand::RemoveRegOperandFromRegInfo() { 83 assert(isOnRegUseList() && "Reg operand is not on a use list"); 84 // Unlink this from the doubly linked list of operands. 85 MachineOperand *NextOp = Contents.Reg.Next; 86 *Contents.Reg.Prev = NextOp; 87 if (NextOp) { 88 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!"); 89 NextOp->Contents.Reg.Prev = Contents.Reg.Prev; 90 } 91 Contents.Reg.Prev = 0; 92 Contents.Reg.Next = 0; 93 } 94 95 void MachineOperand::setReg(unsigned Reg) { 96 if (getReg() == Reg) return; // No change. 97 98 // Otherwise, we have to change the register. If this operand is embedded 99 // into a machine function, we need to update the old and new register's 100 // use/def lists. 101 if (MachineInstr *MI = getParent()) 102 if (MachineBasicBlock *MBB = MI->getParent()) 103 if (MachineFunction *MF = MBB->getParent()) { 104 RemoveRegOperandFromRegInfo(); 105 Contents.Reg.RegNo = Reg; 106 AddRegOperandToRegInfo(&MF->getRegInfo()); 107 return; 108 } 109 110 // Otherwise, just change the register, no problem. :) 111 Contents.Reg.RegNo = Reg; 112 } 113 114 void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx, 115 const TargetRegisterInfo &TRI) { 116 assert(TargetRegisterInfo::isVirtualRegister(Reg)); 117 if (SubIdx && getSubReg()) 118 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg()); 119 setReg(Reg); 120 if (SubIdx) 121 setSubReg(SubIdx); 122 } 123 124 void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) { 125 assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 126 if (getSubReg()) { 127 Reg = TRI.getSubReg(Reg, getSubReg()); 128 assert(Reg && "Invalid SubReg for physical register"); 129 setSubReg(0); 130 } 131 setReg(Reg); 132 } 133 134 /// ChangeToImmediate - Replace this operand with a new immediate operand of 135 /// the specified value. If an operand is known to be an immediate already, 136 /// the setImm method should be used. 137 void MachineOperand::ChangeToImmediate(int64_t ImmVal) { 138 // If this operand is currently a register operand, and if this is in a 139 // function, deregister the operand from the register's use/def list. 140 if (isReg() && getParent() && getParent()->getParent() && 141 getParent()->getParent()->getParent()) 142 RemoveRegOperandFromRegInfo(); 143 144 OpKind = MO_Immediate; 145 Contents.ImmVal = ImmVal; 146 } 147 148 /// ChangeToRegister - Replace this operand with a new register operand of 149 /// the specified value. If an operand is known to be an register already, 150 /// the setReg method should be used. 151 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 152 bool isKill, bool isDead, bool isUndef, 153 bool isDebug) { 154 // If this operand is already a register operand, use setReg to update the 155 // register's use/def lists. 156 if (isReg()) { 157 assert(!isEarlyClobber()); 158 setReg(Reg); 159 } else { 160 // Otherwise, change this to a register and set the reg#. 161 OpKind = MO_Register; 162 Contents.Reg.RegNo = Reg; 163 164 // If this operand is embedded in a function, add the operand to the 165 // register's use/def list. 166 if (MachineInstr *MI = getParent()) 167 if (MachineBasicBlock *MBB = MI->getParent()) 168 if (MachineFunction *MF = MBB->getParent()) 169 AddRegOperandToRegInfo(&MF->getRegInfo()); 170 } 171 172 IsDef = isDef; 173 IsImp = isImp; 174 IsKill = isKill; 175 IsDead = isDead; 176 IsUndef = isUndef; 177 IsEarlyClobber = false; 178 IsDebug = isDebug; 179 SubReg = 0; 180 } 181 182 /// isIdenticalTo - Return true if this operand is identical to the specified 183 /// operand. 184 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 185 if (getType() != Other.getType() || 186 getTargetFlags() != Other.getTargetFlags()) 187 return false; 188 189 switch (getType()) { 190 default: llvm_unreachable("Unrecognized operand type"); 191 case MachineOperand::MO_Register: 192 return getReg() == Other.getReg() && isDef() == Other.isDef() && 193 getSubReg() == Other.getSubReg(); 194 case MachineOperand::MO_Immediate: 195 return getImm() == Other.getImm(); 196 case MachineOperand::MO_FPImmediate: 197 return getFPImm() == Other.getFPImm(); 198 case MachineOperand::MO_MachineBasicBlock: 199 return getMBB() == Other.getMBB(); 200 case MachineOperand::MO_FrameIndex: 201 return getIndex() == Other.getIndex(); 202 case MachineOperand::MO_ConstantPoolIndex: 203 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 204 case MachineOperand::MO_JumpTableIndex: 205 return getIndex() == Other.getIndex(); 206 case MachineOperand::MO_GlobalAddress: 207 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 208 case MachineOperand::MO_ExternalSymbol: 209 return !strcmp(getSymbolName(), Other.getSymbolName()) && 210 getOffset() == Other.getOffset(); 211 case MachineOperand::MO_BlockAddress: 212 return getBlockAddress() == Other.getBlockAddress(); 213 case MachineOperand::MO_MCSymbol: 214 return getMCSymbol() == Other.getMCSymbol(); 215 case MachineOperand::MO_Metadata: 216 return getMetadata() == Other.getMetadata(); 217 } 218 } 219 220 /// print - Print the specified machine operand. 221 /// 222 void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const { 223 // If the instruction is embedded into a basic block, we can find the 224 // target info for the instruction. 225 if (!TM) 226 if (const MachineInstr *MI = getParent()) 227 if (const MachineBasicBlock *MBB = MI->getParent()) 228 if (const MachineFunction *MF = MBB->getParent()) 229 TM = &MF->getTarget(); 230 231 switch (getType()) { 232 case MachineOperand::MO_Register: 233 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) { 234 OS << "%reg" << getReg(); 235 } else { 236 if (TM) 237 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; 238 else 239 OS << "%physreg" << getReg(); 240 } 241 242 if (getSubReg() != 0) { 243 if (TM) 244 OS << ':' << TM->getRegisterInfo()->getSubRegIndexName(getSubReg()); 245 else 246 OS << ':' << getSubReg(); 247 } 248 249 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || 250 isEarlyClobber()) { 251 OS << '<'; 252 bool NeedComma = false; 253 if (isDef()) { 254 if (NeedComma) OS << ','; 255 if (isEarlyClobber()) 256 OS << "earlyclobber,"; 257 if (isImplicit()) 258 OS << "imp-"; 259 OS << "def"; 260 NeedComma = true; 261 } else if (isImplicit()) { 262 OS << "imp-use"; 263 NeedComma = true; 264 } 265 266 if (isKill() || isDead() || isUndef()) { 267 if (NeedComma) OS << ','; 268 if (isKill()) OS << "kill"; 269 if (isDead()) OS << "dead"; 270 if (isUndef()) { 271 if (isKill() || isDead()) 272 OS << ','; 273 OS << "undef"; 274 } 275 } 276 OS << '>'; 277 } 278 break; 279 case MachineOperand::MO_Immediate: 280 OS << getImm(); 281 break; 282 case MachineOperand::MO_FPImmediate: 283 if (getFPImm()->getType()->isFloatTy()) 284 OS << getFPImm()->getValueAPF().convertToFloat(); 285 else 286 OS << getFPImm()->getValueAPF().convertToDouble(); 287 break; 288 case MachineOperand::MO_MachineBasicBlock: 289 OS << "<BB#" << getMBB()->getNumber() << ">"; 290 break; 291 case MachineOperand::MO_FrameIndex: 292 OS << "<fi#" << getIndex() << '>'; 293 break; 294 case MachineOperand::MO_ConstantPoolIndex: 295 OS << "<cp#" << getIndex(); 296 if (getOffset()) OS << "+" << getOffset(); 297 OS << '>'; 298 break; 299 case MachineOperand::MO_JumpTableIndex: 300 OS << "<jt#" << getIndex() << '>'; 301 break; 302 case MachineOperand::MO_GlobalAddress: 303 OS << "<ga:"; 304 WriteAsOperand(OS, getGlobal(), /*PrintType=*/false); 305 if (getOffset()) OS << "+" << getOffset(); 306 OS << '>'; 307 break; 308 case MachineOperand::MO_ExternalSymbol: 309 OS << "<es:" << getSymbolName(); 310 if (getOffset()) OS << "+" << getOffset(); 311 OS << '>'; 312 break; 313 case MachineOperand::MO_BlockAddress: 314 OS << '<'; 315 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false); 316 OS << '>'; 317 break; 318 case MachineOperand::MO_Metadata: 319 OS << '<'; 320 WriteAsOperand(OS, getMetadata(), /*PrintType=*/false); 321 OS << '>'; 322 break; 323 case MachineOperand::MO_MCSymbol: 324 OS << "<MCSym=" << *getMCSymbol() << '>'; 325 break; 326 default: 327 llvm_unreachable("Unrecognized operand type"); 328 } 329 330 if (unsigned TF = getTargetFlags()) 331 OS << "[TF=" << TF << ']'; 332 } 333 334 //===----------------------------------------------------------------------===// 335 // MachineMemOperand Implementation 336 //===----------------------------------------------------------------------===// 337 338 /// getAddrSpace - Return the LLVM IR address space number that this pointer 339 /// points into. 340 unsigned MachinePointerInfo::getAddrSpace() const { 341 if (V == 0) return 0; 342 return cast<PointerType>(V->getType())->getAddressSpace(); 343 } 344 345 /// getConstantPool - Return a MachinePointerInfo record that refers to the 346 /// constant pool. 347 MachinePointerInfo MachinePointerInfo::getConstantPool() { 348 return MachinePointerInfo(PseudoSourceValue::getConstantPool()); 349 } 350 351 /// getFixedStack - Return a MachinePointerInfo record that refers to the 352 /// the specified FrameIndex. 353 MachinePointerInfo MachinePointerInfo::getFixedStack(int FI, int64_t offset) { 354 return MachinePointerInfo(PseudoSourceValue::getFixedStack(FI), offset); 355 } 356 357 358 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, unsigned f, 359 uint64_t s, unsigned int a) 360 : PtrInfo(ptrinfo), Size(s), 361 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)) { 362 assert((PtrInfo.V == 0 || isa<PointerType>(PtrInfo.V->getType())) && 363 "invalid pointer value"); 364 assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); 365 assert((isLoad() || isStore()) && "Not a load/store!"); 366 } 367 368 /// Profile - Gather unique data for the object. 369 /// 370 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 371 ID.AddInteger(getOffset()); 372 ID.AddInteger(Size); 373 ID.AddPointer(getValue()); 374 ID.AddInteger(Flags); 375 } 376 377 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { 378 // The Value and Offset may differ due to CSE. But the flags and size 379 // should be the same. 380 assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); 381 assert(MMO->getSize() == getSize() && "Size mismatch!"); 382 383 if (MMO->getBaseAlignment() >= getBaseAlignment()) { 384 // Update the alignment value. 385 Flags = (Flags & ((1 << MOMaxBits) - 1)) | 386 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits); 387 // Also update the base and offset, because the new alignment may 388 // not be applicable with the old ones. 389 PtrInfo = MMO->PtrInfo; 390 } 391 } 392 393 /// getAlignment - Return the minimum known alignment in bytes of the 394 /// actual memory reference. 395 uint64_t MachineMemOperand::getAlignment() const { 396 return MinAlign(getBaseAlignment(), getOffset()); 397 } 398 399 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) { 400 assert((MMO.isLoad() || MMO.isStore()) && 401 "SV has to be a load, store or both."); 402 403 if (MMO.isVolatile()) 404 OS << "Volatile "; 405 406 if (MMO.isLoad()) 407 OS << "LD"; 408 if (MMO.isStore()) 409 OS << "ST"; 410 OS << MMO.getSize(); 411 412 // Print the address information. 413 OS << "["; 414 if (!MMO.getValue()) 415 OS << "<unknown>"; 416 else 417 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false); 418 419 // If the alignment of the memory reference itself differs from the alignment 420 // of the base pointer, print the base alignment explicitly, next to the base 421 // pointer. 422 if (MMO.getBaseAlignment() != MMO.getAlignment()) 423 OS << "(align=" << MMO.getBaseAlignment() << ")"; 424 425 if (MMO.getOffset() != 0) 426 OS << "+" << MMO.getOffset(); 427 OS << "]"; 428 429 // Print the alignment of the reference. 430 if (MMO.getBaseAlignment() != MMO.getAlignment() || 431 MMO.getBaseAlignment() != MMO.getSize()) 432 OS << "(align=" << MMO.getAlignment() << ")"; 433 434 return OS; 435 } 436 437 //===----------------------------------------------------------------------===// 438 // MachineInstr Implementation 439 //===----------------------------------------------------------------------===// 440 441 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with 442 /// TID NULL and no operands. 443 MachineInstr::MachineInstr() 444 : TID(0), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0), 445 Parent(0) { 446 // Make sure that we get added to a machine basicblock 447 LeakDetector::addGarbageObject(this); 448 } 449 450 void MachineInstr::addImplicitDefUseOperands() { 451 if (TID->ImplicitDefs) 452 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) 453 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); 454 if (TID->ImplicitUses) 455 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) 456 addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); 457 } 458 459 /// MachineInstr ctor - This constructor creates a MachineInstr and adds the 460 /// implicit operands. It reserves space for the number of operands specified by 461 /// the TargetInstrDesc. 462 MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp) 463 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), 464 MemRefs(0), MemRefsEnd(0), Parent(0) { 465 if (!NoImp) 466 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses(); 467 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 468 if (!NoImp) 469 addImplicitDefUseOperands(); 470 // Make sure that we get added to a machine basicblock 471 LeakDetector::addGarbageObject(this); 472 } 473 474 /// MachineInstr ctor - As above, but with a DebugLoc. 475 MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl, 476 bool NoImp) 477 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0), 478 Parent(0), debugLoc(dl) { 479 if (!NoImp) 480 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses(); 481 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 482 if (!NoImp) 483 addImplicitDefUseOperands(); 484 // Make sure that we get added to a machine basicblock 485 LeakDetector::addGarbageObject(this); 486 } 487 488 /// MachineInstr ctor - Work exactly the same as the ctor two above, except 489 /// that the MachineInstr is created and added to the end of the specified 490 /// basic block. 491 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid) 492 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), 493 MemRefs(0), MemRefsEnd(0), Parent(0) { 494 assert(MBB && "Cannot use inserting ctor with null basic block!"); 495 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses(); 496 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 497 addImplicitDefUseOperands(); 498 // Make sure that we get added to a machine basicblock 499 LeakDetector::addGarbageObject(this); 500 MBB->push_back(this); // Add instruction to end of basic block! 501 } 502 503 /// MachineInstr ctor - As above, but with a DebugLoc. 504 /// 505 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl, 506 const TargetInstrDesc &tid) 507 : TID(&tid), NumImplicitOps(0), AsmPrinterFlags(0), MemRefs(0), MemRefsEnd(0), 508 Parent(0), debugLoc(dl) { 509 assert(MBB && "Cannot use inserting ctor with null basic block!"); 510 NumImplicitOps = TID->getNumImplicitDefs() + TID->getNumImplicitUses(); 511 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 512 addImplicitDefUseOperands(); 513 // Make sure that we get added to a machine basicblock 514 LeakDetector::addGarbageObject(this); 515 MBB->push_back(this); // Add instruction to end of basic block! 516 } 517 518 /// MachineInstr ctor - Copies MachineInstr arg exactly 519 /// 520 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) 521 : TID(&MI.getDesc()), NumImplicitOps(0), AsmPrinterFlags(0), 522 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd), 523 Parent(0), debugLoc(MI.getDebugLoc()) { 524 Operands.reserve(MI.getNumOperands()); 525 526 // Add operands 527 for (unsigned i = 0; i != MI.getNumOperands(); ++i) 528 addOperand(MI.getOperand(i)); 529 NumImplicitOps = MI.NumImplicitOps; 530 531 // Set parent to null. 532 Parent = 0; 533 534 LeakDetector::addGarbageObject(this); 535 } 536 537 MachineInstr::~MachineInstr() { 538 LeakDetector::removeGarbageObject(this); 539 #ifndef NDEBUG 540 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 541 assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); 542 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && 543 "Reg operand def/use list corrupted"); 544 } 545 #endif 546 } 547 548 /// getRegInfo - If this instruction is embedded into a MachineFunction, 549 /// return the MachineRegisterInfo object for the current function, otherwise 550 /// return null. 551 MachineRegisterInfo *MachineInstr::getRegInfo() { 552 if (MachineBasicBlock *MBB = getParent()) 553 return &MBB->getParent()->getRegInfo(); 554 return 0; 555 } 556 557 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in 558 /// this instruction from their respective use lists. This requires that the 559 /// operands already be on their use lists. 560 void MachineInstr::RemoveRegOperandsFromUseLists() { 561 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 562 if (Operands[i].isReg()) 563 Operands[i].RemoveRegOperandFromRegInfo(); 564 } 565 } 566 567 /// AddRegOperandsToUseLists - Add all of the register operands in 568 /// this instruction from their respective use lists. This requires that the 569 /// operands not be on their use lists yet. 570 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { 571 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 572 if (Operands[i].isReg()) 573 Operands[i].AddRegOperandToRegInfo(&RegInfo); 574 } 575 } 576 577 578 /// addOperand - Add the specified operand to the instruction. If it is an 579 /// implicit operand, it is added to the end of the operand list. If it is 580 /// an explicit operand it is added at the end of the explicit operand list 581 /// (before the first implicit operand). 582 void MachineInstr::addOperand(const MachineOperand &Op) { 583 bool isImpReg = Op.isReg() && Op.isImplicit(); 584 assert((isImpReg || !OperandsComplete()) && 585 "Trying to add an operand to a machine instr that is already done!"); 586 587 MachineRegisterInfo *RegInfo = getRegInfo(); 588 589 // If we are adding the operand to the end of the list, our job is simpler. 590 // This is true most of the time, so this is a reasonable optimization. 591 if (isImpReg || NumImplicitOps == 0) { 592 // We can only do this optimization if we know that the operand list won't 593 // reallocate. 594 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { 595 Operands.push_back(Op); 596 597 // Set the parent of the operand. 598 Operands.back().ParentMI = this; 599 600 // If the operand is a register, update the operand's use list. 601 if (Op.isReg()) { 602 Operands.back().AddRegOperandToRegInfo(RegInfo); 603 // If the register operand is flagged as early, mark the operand as such 604 unsigned OpNo = Operands.size() - 1; 605 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) 606 Operands[OpNo].setIsEarlyClobber(true); 607 } 608 return; 609 } 610 } 611 612 // Otherwise, we have to insert a real operand before any implicit ones. 613 unsigned OpNo = Operands.size()-NumImplicitOps; 614 615 // If this instruction isn't embedded into a function, then we don't need to 616 // update any operand lists. 617 if (RegInfo == 0) { 618 // Simple insertion, no reginfo update needed for other register operands. 619 Operands.insert(Operands.begin()+OpNo, Op); 620 Operands[OpNo].ParentMI = this; 621 622 // Do explicitly set the reginfo for this operand though, to ensure the 623 // next/prev fields are properly nulled out. 624 if (Operands[OpNo].isReg()) { 625 Operands[OpNo].AddRegOperandToRegInfo(0); 626 // If the register operand is flagged as early, mark the operand as such 627 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) 628 Operands[OpNo].setIsEarlyClobber(true); 629 } 630 631 } else if (Operands.size()+1 <= Operands.capacity()) { 632 // Otherwise, we have to remove register operands from their register use 633 // list, add the operand, then add the register operands back to their use 634 // list. This also must handle the case when the operand list reallocates 635 // to somewhere else. 636 637 // If insertion of this operand won't cause reallocation of the operand 638 // list, just remove the implicit operands, add the operand, then re-add all 639 // the rest of the operands. 640 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 641 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 642 Operands[i].RemoveRegOperandFromRegInfo(); 643 } 644 645 // Add the operand. If it is a register, add it to the reg list. 646 Operands.insert(Operands.begin()+OpNo, Op); 647 Operands[OpNo].ParentMI = this; 648 649 if (Operands[OpNo].isReg()) { 650 Operands[OpNo].AddRegOperandToRegInfo(RegInfo); 651 // If the register operand is flagged as early, mark the operand as such 652 if (TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) 653 Operands[OpNo].setIsEarlyClobber(true); 654 } 655 656 // Re-add all the implicit ops. 657 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { 658 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 659 Operands[i].AddRegOperandToRegInfo(RegInfo); 660 } 661 } else { 662 // Otherwise, we will be reallocating the operand list. Remove all reg 663 // operands from their list, then readd them after the operand list is 664 // reallocated. 665 RemoveRegOperandsFromUseLists(); 666 667 Operands.insert(Operands.begin()+OpNo, Op); 668 Operands[OpNo].ParentMI = this; 669 670 // Re-add all the operands. 671 AddRegOperandsToUseLists(*RegInfo); 672 673 // If the register operand is flagged as early, mark the operand as such 674 if (Operands[OpNo].isReg() 675 && TID->getOperandConstraint(OpNo, TOI::EARLY_CLOBBER) != -1) 676 Operands[OpNo].setIsEarlyClobber(true); 677 } 678 } 679 680 /// RemoveOperand - Erase an operand from an instruction, leaving it with one 681 /// fewer operand than it started with. 682 /// 683 void MachineInstr::RemoveOperand(unsigned OpNo) { 684 assert(OpNo < Operands.size() && "Invalid operand number"); 685 686 // Special case removing the last one. 687 if (OpNo == Operands.size()-1) { 688 // If needed, remove from the reg def/use list. 689 if (Operands.back().isReg() && Operands.back().isOnRegUseList()) 690 Operands.back().RemoveRegOperandFromRegInfo(); 691 692 Operands.pop_back(); 693 return; 694 } 695 696 // Otherwise, we are removing an interior operand. If we have reginfo to 697 // update, remove all operands that will be shifted down from their reg lists, 698 // move everything down, then re-add them. 699 MachineRegisterInfo *RegInfo = getRegInfo(); 700 if (RegInfo) { 701 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 702 if (Operands[i].isReg()) 703 Operands[i].RemoveRegOperandFromRegInfo(); 704 } 705 } 706 707 Operands.erase(Operands.begin()+OpNo); 708 709 if (RegInfo) { 710 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 711 if (Operands[i].isReg()) 712 Operands[i].AddRegOperandToRegInfo(RegInfo); 713 } 714 } 715 } 716 717 /// addMemOperand - Add a MachineMemOperand to the machine instruction. 718 /// This function should be used only occasionally. The setMemRefs function 719 /// is the primary method for setting up a MachineInstr's MemRefs list. 720 void MachineInstr::addMemOperand(MachineFunction &MF, 721 MachineMemOperand *MO) { 722 mmo_iterator OldMemRefs = MemRefs; 723 mmo_iterator OldMemRefsEnd = MemRefsEnd; 724 725 size_t NewNum = (MemRefsEnd - MemRefs) + 1; 726 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum); 727 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum; 728 729 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs); 730 NewMemRefs[NewNum - 1] = MO; 731 732 MemRefs = NewMemRefs; 733 MemRefsEnd = NewMemRefsEnd; 734 } 735 736 bool MachineInstr::isIdenticalTo(const MachineInstr *Other, 737 MICheckType Check) const { 738 // If opcodes or number of operands are not the same then the two 739 // instructions are obviously not identical. 740 if (Other->getOpcode() != getOpcode() || 741 Other->getNumOperands() != getNumOperands()) 742 return false; 743 744 // Check operands to make sure they match. 745 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 746 const MachineOperand &MO = getOperand(i); 747 const MachineOperand &OMO = Other->getOperand(i); 748 // Clients may or may not want to ignore defs when testing for equality. 749 // For example, machine CSE pass only cares about finding common 750 // subexpressions, so it's safe to ignore virtual register defs. 751 if (Check != CheckDefs && MO.isReg() && MO.isDef()) { 752 if (Check == IgnoreDefs) 753 continue; 754 // Check == IgnoreVRegDefs 755 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || 756 TargetRegisterInfo::isPhysicalRegister(OMO.getReg())) 757 if (MO.getReg() != OMO.getReg()) 758 return false; 759 } else if (!MO.isIdenticalTo(OMO)) 760 return false; 761 } 762 return true; 763 } 764 765 /// removeFromParent - This method unlinks 'this' from the containing basic 766 /// block, and returns it, but does not delete it. 767 MachineInstr *MachineInstr::removeFromParent() { 768 assert(getParent() && "Not embedded in a basic block!"); 769 getParent()->remove(this); 770 return this; 771 } 772 773 774 /// eraseFromParent - This method unlinks 'this' from the containing basic 775 /// block, and deletes it. 776 void MachineInstr::eraseFromParent() { 777 assert(getParent() && "Not embedded in a basic block!"); 778 getParent()->erase(this); 779 } 780 781 782 /// OperandComplete - Return true if it's illegal to add a new operand 783 /// 784 bool MachineInstr::OperandsComplete() const { 785 unsigned short NumOperands = TID->getNumOperands(); 786 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands) 787 return true; // Broken: we have all the operands of this instruction! 788 return false; 789 } 790 791 /// getNumExplicitOperands - Returns the number of non-implicit operands. 792 /// 793 unsigned MachineInstr::getNumExplicitOperands() const { 794 unsigned NumOperands = TID->getNumOperands(); 795 if (!TID->isVariadic()) 796 return NumOperands; 797 798 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { 799 const MachineOperand &MO = getOperand(i); 800 if (!MO.isReg() || !MO.isImplicit()) 801 NumOperands++; 802 } 803 return NumOperands; 804 } 805 806 807 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of 808 /// the specific register or -1 if it is not found. It further tightens 809 /// the search criteria to a use that kills the register if isKill is true. 810 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, 811 const TargetRegisterInfo *TRI) const { 812 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 813 const MachineOperand &MO = getOperand(i); 814 if (!MO.isReg() || !MO.isUse()) 815 continue; 816 unsigned MOReg = MO.getReg(); 817 if (!MOReg) 818 continue; 819 if (MOReg == Reg || 820 (TRI && 821 TargetRegisterInfo::isPhysicalRegister(MOReg) && 822 TargetRegisterInfo::isPhysicalRegister(Reg) && 823 TRI->isSubRegister(MOReg, Reg))) 824 if (!isKill || MO.isKill()) 825 return i; 826 } 827 return -1; 828 } 829 830 /// readsWritesVirtualRegister - Return a pair of bools (reads, writes) 831 /// indicating if this instruction reads or writes Reg. This also considers 832 /// partial defines. 833 std::pair<bool,bool> 834 MachineInstr::readsWritesVirtualRegister(unsigned Reg, 835 SmallVectorImpl<unsigned> *Ops) const { 836 bool PartDef = false; // Partial redefine. 837 bool FullDef = false; // Full define. 838 bool Use = false; 839 840 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 841 const MachineOperand &MO = getOperand(i); 842 if (!MO.isReg() || MO.getReg() != Reg) 843 continue; 844 if (Ops) 845 Ops->push_back(i); 846 if (MO.isUse()) 847 Use |= !MO.isUndef(); 848 else if (MO.getSubReg()) 849 PartDef = true; 850 else 851 FullDef = true; 852 } 853 // A partial redefine uses Reg unless there is also a full define. 854 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef); 855 } 856 857 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of 858 /// the specified register or -1 if it is not found. If isDead is true, defs 859 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it 860 /// also checks if there is a def of a super-register. 861 int 862 MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap, 863 const TargetRegisterInfo *TRI) const { 864 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg); 865 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 866 const MachineOperand &MO = getOperand(i); 867 if (!MO.isReg() || !MO.isDef()) 868 continue; 869 unsigned MOReg = MO.getReg(); 870 bool Found = (MOReg == Reg); 871 if (!Found && TRI && isPhys && 872 TargetRegisterInfo::isPhysicalRegister(MOReg)) { 873 if (Overlap) 874 Found = TRI->regsOverlap(MOReg, Reg); 875 else 876 Found = TRI->isSubRegister(MOReg, Reg); 877 } 878 if (Found && (!isDead || MO.isDead())) 879 return i; 880 } 881 return -1; 882 } 883 884 /// findFirstPredOperandIdx() - Find the index of the first operand in the 885 /// operand list that is used to represent the predicate. It returns -1 if 886 /// none is found. 887 int MachineInstr::findFirstPredOperandIdx() const { 888 const TargetInstrDesc &TID = getDesc(); 889 if (TID.isPredicable()) { 890 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 891 if (TID.OpInfo[i].isPredicate()) 892 return i; 893 } 894 895 return -1; 896 } 897 898 /// isRegTiedToUseOperand - Given the index of a register def operand, 899 /// check if the register def is tied to a source operand, due to either 900 /// two-address elimination or inline assembly constraints. Returns the 901 /// first tied use operand index by reference is UseOpIdx is not null. 902 bool MachineInstr:: 903 isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const { 904 if (isInlineAsm()) { 905 assert(DefOpIdx >= 3); 906 const MachineOperand &MO = getOperand(DefOpIdx); 907 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0) 908 return false; 909 // Determine the actual operand index that corresponds to this index. 910 unsigned DefNo = 0; 911 unsigned DefPart = 0; 912 for (unsigned i = 2, e = getNumOperands(); i < e; ) { 913 const MachineOperand &FMO = getOperand(i); 914 // After the normal asm operands there may be additional imp-def regs. 915 if (!FMO.isImm()) 916 return false; 917 // Skip over this def. 918 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm()); 919 unsigned PrevDef = i + 1; 920 i = PrevDef + NumOps; 921 if (i > DefOpIdx) { 922 DefPart = DefOpIdx - PrevDef; 923 break; 924 } 925 ++DefNo; 926 } 927 for (unsigned i = 2, e = getNumOperands(); i != e; ++i) { 928 const MachineOperand &FMO = getOperand(i); 929 if (!FMO.isImm()) 930 continue; 931 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse()) 932 continue; 933 unsigned Idx; 934 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) && 935 Idx == DefNo) { 936 if (UseOpIdx) 937 *UseOpIdx = (unsigned)i + 1 + DefPart; 938 return true; 939 } 940 } 941 return false; 942 } 943 944 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!"); 945 const TargetInstrDesc &TID = getDesc(); 946 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { 947 const MachineOperand &MO = getOperand(i); 948 if (MO.isReg() && MO.isUse() && 949 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) { 950 if (UseOpIdx) 951 *UseOpIdx = (unsigned)i; 952 return true; 953 } 954 } 955 return false; 956 } 957 958 /// isRegTiedToDefOperand - Return true if the operand of the specified index 959 /// is a register use and it is tied to an def operand. It also returns the def 960 /// operand index by reference. 961 bool MachineInstr:: 962 isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const { 963 if (isInlineAsm()) { 964 const MachineOperand &MO = getOperand(UseOpIdx); 965 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0) 966 return false; 967 968 // Find the flag operand corresponding to UseOpIdx 969 unsigned FlagIdx, NumOps=0; 970 for (FlagIdx = 2; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) { 971 const MachineOperand &UFMO = getOperand(FlagIdx); 972 // After the normal asm operands there may be additional imp-def regs. 973 if (!UFMO.isImm()) 974 return false; 975 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm()); 976 assert(NumOps < getNumOperands() && "Invalid inline asm flag"); 977 if (UseOpIdx < FlagIdx+NumOps+1) 978 break; 979 } 980 if (FlagIdx >= UseOpIdx) 981 return false; 982 const MachineOperand &UFMO = getOperand(FlagIdx); 983 unsigned DefNo; 984 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) { 985 if (!DefOpIdx) 986 return true; 987 988 unsigned DefIdx = 2; 989 // Remember to adjust the index. First operand is asm string, second is 990 // the AlignStack bit, then there is a flag for each. 991 while (DefNo) { 992 const MachineOperand &FMO = getOperand(DefIdx); 993 assert(FMO.isImm()); 994 // Skip over this def. 995 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1; 996 --DefNo; 997 } 998 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx; 999 return true; 1000 } 1001 return false; 1002 } 1003 1004 const TargetInstrDesc &TID = getDesc(); 1005 if (UseOpIdx >= TID.getNumOperands()) 1006 return false; 1007 const MachineOperand &MO = getOperand(UseOpIdx); 1008 if (!MO.isReg() || !MO.isUse()) 1009 return false; 1010 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO); 1011 if (DefIdx == -1) 1012 return false; 1013 if (DefOpIdx) 1014 *DefOpIdx = (unsigned)DefIdx; 1015 return true; 1016 } 1017 1018 /// clearKillInfo - Clears kill flags on all operands. 1019 /// 1020 void MachineInstr::clearKillInfo() { 1021 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1022 MachineOperand &MO = getOperand(i); 1023 if (MO.isReg() && MO.isUse()) 1024 MO.setIsKill(false); 1025 } 1026 } 1027 1028 /// copyKillDeadInfo - Copies kill / dead operand properties from MI. 1029 /// 1030 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { 1031 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1032 const MachineOperand &MO = MI->getOperand(i); 1033 if (!MO.isReg() || (!MO.isKill() && !MO.isDead())) 1034 continue; 1035 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { 1036 MachineOperand &MOp = getOperand(j); 1037 if (!MOp.isIdenticalTo(MO)) 1038 continue; 1039 if (MO.isKill()) 1040 MOp.setIsKill(); 1041 else 1042 MOp.setIsDead(); 1043 break; 1044 } 1045 } 1046 } 1047 1048 /// copyPredicates - Copies predicate operand(s) from MI. 1049 void MachineInstr::copyPredicates(const MachineInstr *MI) { 1050 const TargetInstrDesc &TID = MI->getDesc(); 1051 if (!TID.isPredicable()) 1052 return; 1053 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1054 if (TID.OpInfo[i].isPredicate()) { 1055 // Predicated operands must be last operands. 1056 addOperand(MI->getOperand(i)); 1057 } 1058 } 1059 } 1060 1061 void MachineInstr::substituteRegister(unsigned FromReg, 1062 unsigned ToReg, 1063 unsigned SubIdx, 1064 const TargetRegisterInfo &RegInfo) { 1065 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) { 1066 if (SubIdx) 1067 ToReg = RegInfo.getSubReg(ToReg, SubIdx); 1068 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1069 MachineOperand &MO = getOperand(i); 1070 if (!MO.isReg() || MO.getReg() != FromReg) 1071 continue; 1072 MO.substPhysReg(ToReg, RegInfo); 1073 } 1074 } else { 1075 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1076 MachineOperand &MO = getOperand(i); 1077 if (!MO.isReg() || MO.getReg() != FromReg) 1078 continue; 1079 MO.substVirtReg(ToReg, SubIdx, RegInfo); 1080 } 1081 } 1082 } 1083 1084 /// isSafeToMove - Return true if it is safe to move this instruction. If 1085 /// SawStore is set to true, it means that there is a store (or call) between 1086 /// the instruction's location and its intended destination. 1087 bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, 1088 AliasAnalysis *AA, 1089 bool &SawStore) const { 1090 // Ignore stuff that we obviously can't move. 1091 if (TID->mayStore() || TID->isCall()) { 1092 SawStore = true; 1093 return false; 1094 } 1095 if (TID->isTerminator() || TID->hasUnmodeledSideEffects()) 1096 return false; 1097 1098 // See if this instruction does a load. If so, we have to guarantee that the 1099 // loaded value doesn't change between the load and the its intended 1100 // destination. The check for isInvariantLoad gives the targe the chance to 1101 // classify the load as always returning a constant, e.g. a constant pool 1102 // load. 1103 if (TID->mayLoad() && !isInvariantLoad(AA)) 1104 // Otherwise, this is a real load. If there is a store between the load and 1105 // end of block, or if the load is volatile, we can't move it. 1106 return !SawStore && !hasVolatileMemoryRef(); 1107 1108 return true; 1109 } 1110 1111 /// isSafeToReMat - Return true if it's safe to rematerialize the specified 1112 /// instruction which defined the specified register instead of copying it. 1113 bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII, 1114 AliasAnalysis *AA, 1115 unsigned DstReg) const { 1116 bool SawStore = false; 1117 if (!TII->isTriviallyReMaterializable(this, AA) || 1118 !isSafeToMove(TII, AA, SawStore)) 1119 return false; 1120 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1121 const MachineOperand &MO = getOperand(i); 1122 if (!MO.isReg()) 1123 continue; 1124 // FIXME: For now, do not remat any instruction with register operands. 1125 // Later on, we can loosen the restriction is the register operands have 1126 // not been modified between the def and use. Note, this is different from 1127 // MachineSink because the code is no longer in two-address form (at least 1128 // partially). 1129 if (MO.isUse()) 1130 return false; 1131 else if (!MO.isDead() && MO.getReg() != DstReg) 1132 return false; 1133 } 1134 return true; 1135 } 1136 1137 /// hasVolatileMemoryRef - Return true if this instruction may have a 1138 /// volatile memory reference, or if the information describing the 1139 /// memory reference is not available. Return false if it is known to 1140 /// have no volatile memory references. 1141 bool MachineInstr::hasVolatileMemoryRef() const { 1142 // An instruction known never to access memory won't have a volatile access. 1143 if (!TID->mayStore() && 1144 !TID->mayLoad() && 1145 !TID->isCall() && 1146 !TID->hasUnmodeledSideEffects()) 1147 return false; 1148 1149 // Otherwise, if the instruction has no memory reference information, 1150 // conservatively assume it wasn't preserved. 1151 if (memoperands_empty()) 1152 return true; 1153 1154 // Check the memory reference information for volatile references. 1155 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I) 1156 if ((*I)->isVolatile()) 1157 return true; 1158 1159 return false; 1160 } 1161 1162 /// isInvariantLoad - Return true if this instruction is loading from a 1163 /// location whose value is invariant across the function. For example, 1164 /// loading a value from the constant pool or from the argument area 1165 /// of a function if it does not change. This should only return true of 1166 /// *all* loads the instruction does are invariant (if it does multiple loads). 1167 bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const { 1168 // If the instruction doesn't load at all, it isn't an invariant load. 1169 if (!TID->mayLoad()) 1170 return false; 1171 1172 // If the instruction has lost its memoperands, conservatively assume that 1173 // it may not be an invariant load. 1174 if (memoperands_empty()) 1175 return false; 1176 1177 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo(); 1178 1179 for (mmo_iterator I = memoperands_begin(), 1180 E = memoperands_end(); I != E; ++I) { 1181 if ((*I)->isVolatile()) return false; 1182 if ((*I)->isStore()) return false; 1183 1184 if (const Value *V = (*I)->getValue()) { 1185 // A load from a constant PseudoSourceValue is invariant. 1186 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) 1187 if (PSV->isConstant(MFI)) 1188 continue; 1189 // If we have an AliasAnalysis, ask it whether the memory is constant. 1190 if (AA && AA->pointsToConstantMemory(V)) 1191 continue; 1192 } 1193 1194 // Otherwise assume conservatively. 1195 return false; 1196 } 1197 1198 // Everything checks out. 1199 return true; 1200 } 1201 1202 /// isConstantValuePHI - If the specified instruction is a PHI that always 1203 /// merges together the same virtual register, return the register, otherwise 1204 /// return 0. 1205 unsigned MachineInstr::isConstantValuePHI() const { 1206 if (!isPHI()) 1207 return 0; 1208 assert(getNumOperands() >= 3 && 1209 "It's illegal to have a PHI without source operands"); 1210 1211 unsigned Reg = getOperand(1).getReg(); 1212 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2) 1213 if (getOperand(i).getReg() != Reg) 1214 return 0; 1215 return Reg; 1216 } 1217 1218 /// allDefsAreDead - Return true if all the defs of this instruction are dead. 1219 /// 1220 bool MachineInstr::allDefsAreDead() const { 1221 for (unsigned i = 0, e = getNumOperands(); i < e; ++i) { 1222 const MachineOperand &MO = getOperand(i); 1223 if (!MO.isReg() || MO.isUse()) 1224 continue; 1225 if (!MO.isDead()) 1226 return false; 1227 } 1228 return true; 1229 } 1230 1231 void MachineInstr::dump() const { 1232 dbgs() << " " << *this; 1233 } 1234 1235 static void printDebugLoc(DebugLoc DL, const MachineFunction *MF, 1236 raw_ostream &CommentOS) { 1237 const LLVMContext &Ctx = MF->getFunction()->getContext(); 1238 if (!DL.isUnknown()) { // Print source line info. 1239 DIScope Scope(DL.getScope(Ctx)); 1240 // Omit the directory, because it's likely to be long and uninteresting. 1241 if (Scope.Verify()) 1242 CommentOS << Scope.getFilename(); 1243 else 1244 CommentOS << "<unknown>"; 1245 CommentOS << ':' << DL.getLine(); 1246 if (DL.getCol() != 0) 1247 CommentOS << ':' << DL.getCol(); 1248 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx)); 1249 if (!InlinedAtDL.isUnknown()) { 1250 CommentOS << " @[ "; 1251 printDebugLoc(InlinedAtDL, MF, CommentOS); 1252 CommentOS << " ]"; 1253 } 1254 } 1255 } 1256 1257 void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const { 1258 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction. 1259 const MachineFunction *MF = 0; 1260 const MachineRegisterInfo *MRI = 0; 1261 if (const MachineBasicBlock *MBB = getParent()) { 1262 MF = MBB->getParent(); 1263 if (!TM && MF) 1264 TM = &MF->getTarget(); 1265 if (MF) 1266 MRI = &MF->getRegInfo(); 1267 } 1268 1269 // Save a list of virtual registers. 1270 SmallVector<unsigned, 8> VirtRegs; 1271 1272 // Print explicitly defined operands on the left of an assignment syntax. 1273 unsigned StartOp = 0, e = getNumOperands(); 1274 for (; StartOp < e && getOperand(StartOp).isReg() && 1275 getOperand(StartOp).isDef() && 1276 !getOperand(StartOp).isImplicit(); 1277 ++StartOp) { 1278 if (StartOp != 0) OS << ", "; 1279 getOperand(StartOp).print(OS, TM); 1280 unsigned Reg = getOperand(StartOp).getReg(); 1281 if (Reg && TargetRegisterInfo::isVirtualRegister(Reg)) 1282 VirtRegs.push_back(Reg); 1283 } 1284 1285 if (StartOp != 0) 1286 OS << " = "; 1287 1288 // Print the opcode name. 1289 OS << getDesc().getName(); 1290 1291 // Print the rest of the operands. 1292 bool OmittedAnyCallClobbers = false; 1293 bool FirstOp = true; 1294 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 1295 const MachineOperand &MO = getOperand(i); 1296 1297 if (MO.isReg() && MO.getReg() && 1298 TargetRegisterInfo::isVirtualRegister(MO.getReg())) 1299 VirtRegs.push_back(MO.getReg()); 1300 1301 // Omit call-clobbered registers which aren't used anywhere. This makes 1302 // call instructions much less noisy on targets where calls clobber lots 1303 // of registers. Don't rely on MO.isDead() because we may be called before 1304 // LiveVariables is run, or we may be looking at a non-allocatable reg. 1305 if (MF && getDesc().isCall() && 1306 MO.isReg() && MO.isImplicit() && MO.isDef()) { 1307 unsigned Reg = MO.getReg(); 1308 if (Reg != 0 && TargetRegisterInfo::isPhysicalRegister(Reg)) { 1309 const MachineRegisterInfo &MRI = MF->getRegInfo(); 1310 if (MRI.use_empty(Reg) && !MRI.isLiveOut(Reg)) { 1311 bool HasAliasLive = false; 1312 for (const unsigned *Alias = TM->getRegisterInfo()->getAliasSet(Reg); 1313 unsigned AliasReg = *Alias; ++Alias) 1314 if (!MRI.use_empty(AliasReg) || MRI.isLiveOut(AliasReg)) { 1315 HasAliasLive = true; 1316 break; 1317 } 1318 if (!HasAliasLive) { 1319 OmittedAnyCallClobbers = true; 1320 continue; 1321 } 1322 } 1323 } 1324 } 1325 1326 if (FirstOp) FirstOp = false; else OS << ","; 1327 OS << " "; 1328 if (i < getDesc().NumOperands) { 1329 const TargetOperandInfo &TOI = getDesc().OpInfo[i]; 1330 if (TOI.isPredicate()) 1331 OS << "pred:"; 1332 if (TOI.isOptionalDef()) 1333 OS << "opt:"; 1334 } 1335 if (isDebugValue() && MO.isMetadata()) { 1336 // Pretty print DBG_VALUE instructions. 1337 const MDNode *MD = MO.getMetadata(); 1338 if (const MDString *MDS = dyn_cast<MDString>(MD->getOperand(2))) 1339 OS << "!\"" << MDS->getString() << '\"'; 1340 else 1341 MO.print(OS, TM); 1342 } else if (TM && (isInsertSubreg() || isRegSequence()) && MO.isImm()) { 1343 OS << TM->getRegisterInfo()->getSubRegIndexName(MO.getImm()); 1344 } else 1345 MO.print(OS, TM); 1346 } 1347 1348 // Briefly indicate whether any call clobbers were omitted. 1349 if (OmittedAnyCallClobbers) { 1350 if (!FirstOp) OS << ","; 1351 OS << " ..."; 1352 } 1353 1354 bool HaveSemi = false; 1355 if (!memoperands_empty()) { 1356 if (!HaveSemi) OS << ";"; HaveSemi = true; 1357 1358 OS << " mem:"; 1359 for (mmo_iterator i = memoperands_begin(), e = memoperands_end(); 1360 i != e; ++i) { 1361 OS << **i; 1362 if (llvm::next(i) != e) 1363 OS << " "; 1364 } 1365 } 1366 1367 // Print the regclass of any virtual registers encountered. 1368 if (MRI && !VirtRegs.empty()) { 1369 if (!HaveSemi) OS << ";"; HaveSemi = true; 1370 for (unsigned i = 0; i != VirtRegs.size(); ++i) { 1371 const TargetRegisterClass *RC = MRI->getRegClass(VirtRegs[i]); 1372 OS << " " << RC->getName() << ":%reg" << VirtRegs[i]; 1373 for (unsigned j = i+1; j != VirtRegs.size();) { 1374 if (MRI->getRegClass(VirtRegs[j]) != RC) { 1375 ++j; 1376 continue; 1377 } 1378 if (VirtRegs[i] != VirtRegs[j]) 1379 OS << "," << VirtRegs[j]; 1380 VirtRegs.erase(VirtRegs.begin()+j); 1381 } 1382 } 1383 } 1384 1385 if (!debugLoc.isUnknown() && MF) { 1386 if (!HaveSemi) OS << ";"; 1387 OS << " dbg:"; 1388 printDebugLoc(debugLoc, MF, OS); 1389 } 1390 1391 OS << "\n"; 1392 } 1393 1394 bool MachineInstr::addRegisterKilled(unsigned IncomingReg, 1395 const TargetRegisterInfo *RegInfo, 1396 bool AddIfNotFound) { 1397 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 1398 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 1399 bool Found = false; 1400 SmallVector<unsigned,4> DeadOps; 1401 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1402 MachineOperand &MO = getOperand(i); 1403 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) 1404 continue; 1405 unsigned Reg = MO.getReg(); 1406 if (!Reg) 1407 continue; 1408 1409 if (Reg == IncomingReg) { 1410 if (!Found) { 1411 if (MO.isKill()) 1412 // The register is already marked kill. 1413 return true; 1414 if (isPhysReg && isRegTiedToDefOperand(i)) 1415 // Two-address uses of physregs must not be marked kill. 1416 return true; 1417 MO.setIsKill(); 1418 Found = true; 1419 } 1420 } else if (hasAliases && MO.isKill() && 1421 TargetRegisterInfo::isPhysicalRegister(Reg)) { 1422 // A super-register kill already exists. 1423 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 1424 return true; 1425 if (RegInfo->isSubRegister(IncomingReg, Reg)) 1426 DeadOps.push_back(i); 1427 } 1428 } 1429 1430 // Trim unneeded kill operands. 1431 while (!DeadOps.empty()) { 1432 unsigned OpIdx = DeadOps.back(); 1433 if (getOperand(OpIdx).isImplicit()) 1434 RemoveOperand(OpIdx); 1435 else 1436 getOperand(OpIdx).setIsKill(false); 1437 DeadOps.pop_back(); 1438 } 1439 1440 // If not found, this means an alias of one of the operands is killed. Add a 1441 // new implicit operand if required. 1442 if (!Found && AddIfNotFound) { 1443 addOperand(MachineOperand::CreateReg(IncomingReg, 1444 false /*IsDef*/, 1445 true /*IsImp*/, 1446 true /*IsKill*/)); 1447 return true; 1448 } 1449 return Found; 1450 } 1451 1452 bool MachineInstr::addRegisterDead(unsigned IncomingReg, 1453 const TargetRegisterInfo *RegInfo, 1454 bool AddIfNotFound) { 1455 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 1456 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 1457 bool Found = false; 1458 SmallVector<unsigned,4> DeadOps; 1459 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1460 MachineOperand &MO = getOperand(i); 1461 if (!MO.isReg() || !MO.isDef()) 1462 continue; 1463 unsigned Reg = MO.getReg(); 1464 if (!Reg) 1465 continue; 1466 1467 if (Reg == IncomingReg) { 1468 if (!Found) { 1469 if (MO.isDead()) 1470 // The register is already marked dead. 1471 return true; 1472 MO.setIsDead(); 1473 Found = true; 1474 } 1475 } else if (hasAliases && MO.isDead() && 1476 TargetRegisterInfo::isPhysicalRegister(Reg)) { 1477 // There exists a super-register that's marked dead. 1478 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 1479 return true; 1480 if (RegInfo->getSubRegisters(IncomingReg) && 1481 RegInfo->getSuperRegisters(Reg) && 1482 RegInfo->isSubRegister(IncomingReg, Reg)) 1483 DeadOps.push_back(i); 1484 } 1485 } 1486 1487 // Trim unneeded dead operands. 1488 while (!DeadOps.empty()) { 1489 unsigned OpIdx = DeadOps.back(); 1490 if (getOperand(OpIdx).isImplicit()) 1491 RemoveOperand(OpIdx); 1492 else 1493 getOperand(OpIdx).setIsDead(false); 1494 DeadOps.pop_back(); 1495 } 1496 1497 // If not found, this means an alias of one of the operands is dead. Add a 1498 // new implicit operand if required. 1499 if (Found || !AddIfNotFound) 1500 return Found; 1501 1502 addOperand(MachineOperand::CreateReg(IncomingReg, 1503 true /*IsDef*/, 1504 true /*IsImp*/, 1505 false /*IsKill*/, 1506 true /*IsDead*/)); 1507 return true; 1508 } 1509 1510 void MachineInstr::addRegisterDefined(unsigned IncomingReg, 1511 const TargetRegisterInfo *RegInfo) { 1512 if (TargetRegisterInfo::isPhysicalRegister(IncomingReg)) { 1513 MachineOperand *MO = findRegisterDefOperand(IncomingReg, false, RegInfo); 1514 if (MO) 1515 return; 1516 } else { 1517 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1518 const MachineOperand &MO = getOperand(i); 1519 if (MO.isReg() && MO.getReg() == IncomingReg && MO.isDef() && 1520 MO.getSubReg() == 0) 1521 return; 1522 } 1523 } 1524 addOperand(MachineOperand::CreateReg(IncomingReg, 1525 true /*IsDef*/, 1526 true /*IsImp*/)); 1527 } 1528 1529 void MachineInstr::setPhysRegsDeadExcept(const SmallVectorImpl<unsigned> &UsedRegs, 1530 const TargetRegisterInfo &TRI) { 1531 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1532 MachineOperand &MO = getOperand(i); 1533 if (!MO.isReg() || !MO.isDef()) continue; 1534 unsigned Reg = MO.getReg(); 1535 if (Reg == 0) continue; 1536 bool Dead = true; 1537 for (SmallVectorImpl<unsigned>::const_iterator I = UsedRegs.begin(), 1538 E = UsedRegs.end(); I != E; ++I) 1539 if (TRI.regsOverlap(*I, Reg)) { 1540 Dead = false; 1541 break; 1542 } 1543 // If there are no uses, including partial uses, the def is dead. 1544 if (Dead) MO.setIsDead(); 1545 } 1546 } 1547 1548 unsigned 1549 MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) { 1550 unsigned Hash = MI->getOpcode() * 37; 1551 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1552 const MachineOperand &MO = MI->getOperand(i); 1553 uint64_t Key = (uint64_t)MO.getType() << 32; 1554 switch (MO.getType()) { 1555 default: break; 1556 case MachineOperand::MO_Register: 1557 if (MO.isDef() && MO.getReg() && 1558 TargetRegisterInfo::isVirtualRegister(MO.getReg())) 1559 continue; // Skip virtual register defs. 1560 Key |= MO.getReg(); 1561 break; 1562 case MachineOperand::MO_Immediate: 1563 Key |= MO.getImm(); 1564 break; 1565 case MachineOperand::MO_FrameIndex: 1566 case MachineOperand::MO_ConstantPoolIndex: 1567 case MachineOperand::MO_JumpTableIndex: 1568 Key |= MO.getIndex(); 1569 break; 1570 case MachineOperand::MO_MachineBasicBlock: 1571 Key |= DenseMapInfo<void*>::getHashValue(MO.getMBB()); 1572 break; 1573 case MachineOperand::MO_GlobalAddress: 1574 Key |= DenseMapInfo<void*>::getHashValue(MO.getGlobal()); 1575 break; 1576 case MachineOperand::MO_BlockAddress: 1577 Key |= DenseMapInfo<void*>::getHashValue(MO.getBlockAddress()); 1578 break; 1579 case MachineOperand::MO_MCSymbol: 1580 Key |= DenseMapInfo<void*>::getHashValue(MO.getMCSymbol()); 1581 break; 1582 } 1583 Key += ~(Key << 32); 1584 Key ^= (Key >> 22); 1585 Key += ~(Key << 13); 1586 Key ^= (Key >> 8); 1587 Key += (Key << 3); 1588 Key ^= (Key >> 15); 1589 Key += ~(Key << 27); 1590 Key ^= (Key >> 31); 1591 Hash = (unsigned)Key + Hash * 37; 1592 } 1593 return Hash; 1594 } 1595