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/ADT/FoldingSet.h" 16 #include "llvm/ADT/Hashing.h" 17 #include "llvm/Analysis/AliasAnalysis.h" 18 #include "llvm/CodeGen/MachineConstantPool.h" 19 #include "llvm/CodeGen/MachineFunction.h" 20 #include "llvm/CodeGen/MachineMemOperand.h" 21 #include "llvm/CodeGen/MachineModuleInfo.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/PseudoSourceValue.h" 24 #include "llvm/IR/Constants.h" 25 #include "llvm/IR/DebugInfo.h" 26 #include "llvm/IR/Function.h" 27 #include "llvm/IR/InlineAsm.h" 28 #include "llvm/IR/LLVMContext.h" 29 #include "llvm/IR/Metadata.h" 30 #include "llvm/IR/Module.h" 31 #include "llvm/IR/Type.h" 32 #include "llvm/IR/Value.h" 33 #include "llvm/MC/MCInstrDesc.h" 34 #include "llvm/MC/MCSymbol.h" 35 #include "llvm/Support/Debug.h" 36 #include "llvm/Support/ErrorHandling.h" 37 #include "llvm/Support/MathExtras.h" 38 #include "llvm/Support/raw_ostream.h" 39 #include "llvm/Target/TargetInstrInfo.h" 40 #include "llvm/Target/TargetMachine.h" 41 #include "llvm/Target/TargetRegisterInfo.h" 42 #include "llvm/Target/TargetSubtargetInfo.h" 43 using namespace llvm; 44 45 //===----------------------------------------------------------------------===// 46 // MachineOperand Implementation 47 //===----------------------------------------------------------------------===// 48 49 void MachineOperand::setReg(unsigned Reg) { 50 if (getReg() == Reg) return; // No change. 51 52 // Otherwise, we have to change the register. If this operand is embedded 53 // into a machine function, we need to update the old and new register's 54 // use/def lists. 55 if (MachineInstr *MI = getParent()) 56 if (MachineBasicBlock *MBB = MI->getParent()) 57 if (MachineFunction *MF = MBB->getParent()) { 58 MachineRegisterInfo &MRI = MF->getRegInfo(); 59 MRI.removeRegOperandFromUseList(this); 60 SmallContents.RegNo = Reg; 61 MRI.addRegOperandToUseList(this); 62 return; 63 } 64 65 // Otherwise, just change the register, no problem. :) 66 SmallContents.RegNo = Reg; 67 } 68 69 void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx, 70 const TargetRegisterInfo &TRI) { 71 assert(TargetRegisterInfo::isVirtualRegister(Reg)); 72 if (SubIdx && getSubReg()) 73 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg()); 74 setReg(Reg); 75 if (SubIdx) 76 setSubReg(SubIdx); 77 } 78 79 void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) { 80 assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 81 if (getSubReg()) { 82 Reg = TRI.getSubReg(Reg, getSubReg()); 83 // Note that getSubReg() may return 0 if the sub-register doesn't exist. 84 // That won't happen in legal code. 85 setSubReg(0); 86 } 87 setReg(Reg); 88 } 89 90 /// Change a def to a use, or a use to a def. 91 void MachineOperand::setIsDef(bool Val) { 92 assert(isReg() && "Wrong MachineOperand accessor"); 93 assert((!Val || !isDebug()) && "Marking a debug operation as def"); 94 if (IsDef == Val) 95 return; 96 // MRI may keep uses and defs in different list positions. 97 if (MachineInstr *MI = getParent()) 98 if (MachineBasicBlock *MBB = MI->getParent()) 99 if (MachineFunction *MF = MBB->getParent()) { 100 MachineRegisterInfo &MRI = MF->getRegInfo(); 101 MRI.removeRegOperandFromUseList(this); 102 IsDef = Val; 103 MRI.addRegOperandToUseList(this); 104 return; 105 } 106 IsDef = Val; 107 } 108 109 /// ChangeToImmediate - Replace this operand with a new immediate operand of 110 /// the specified value. If an operand is known to be an immediate already, 111 /// the setImm method should be used. 112 void MachineOperand::ChangeToImmediate(int64_t ImmVal) { 113 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); 114 // If this operand is currently a register operand, and if this is in a 115 // function, deregister the operand from the register's use/def list. 116 if (isReg() && isOnRegUseList()) 117 if (MachineInstr *MI = getParent()) 118 if (MachineBasicBlock *MBB = MI->getParent()) 119 if (MachineFunction *MF = MBB->getParent()) 120 MF->getRegInfo().removeRegOperandFromUseList(this); 121 122 OpKind = MO_Immediate; 123 Contents.ImmVal = ImmVal; 124 } 125 126 /// ChangeToRegister - Replace this operand with a new register operand of 127 /// the specified value. If an operand is known to be an register already, 128 /// the setReg method should be used. 129 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 130 bool isKill, bool isDead, bool isUndef, 131 bool isDebug) { 132 MachineRegisterInfo *RegInfo = nullptr; 133 if (MachineInstr *MI = getParent()) 134 if (MachineBasicBlock *MBB = MI->getParent()) 135 if (MachineFunction *MF = MBB->getParent()) 136 RegInfo = &MF->getRegInfo(); 137 // If this operand is already a register operand, remove it from the 138 // register's use/def lists. 139 bool WasReg = isReg(); 140 if (RegInfo && WasReg) 141 RegInfo->removeRegOperandFromUseList(this); 142 143 // Change this to a register and set the reg#. 144 OpKind = MO_Register; 145 SmallContents.RegNo = Reg; 146 SubReg_TargetFlags = 0; 147 IsDef = isDef; 148 IsImp = isImp; 149 IsKill = isKill; 150 IsDead = isDead; 151 IsUndef = isUndef; 152 IsInternalRead = false; 153 IsEarlyClobber = false; 154 IsDebug = isDebug; 155 // Ensure isOnRegUseList() returns false. 156 Contents.Reg.Prev = nullptr; 157 // Preserve the tie when the operand was already a register. 158 if (!WasReg) 159 TiedTo = 0; 160 161 // If this operand is embedded in a function, add the operand to the 162 // register's use/def list. 163 if (RegInfo) 164 RegInfo->addRegOperandToUseList(this); 165 } 166 167 /// isIdenticalTo - Return true if this operand is identical to the specified 168 /// operand. Note that this should stay in sync with the hash_value overload 169 /// below. 170 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 171 if (getType() != Other.getType() || 172 getTargetFlags() != Other.getTargetFlags()) 173 return false; 174 175 switch (getType()) { 176 case MachineOperand::MO_Register: 177 return getReg() == Other.getReg() && isDef() == Other.isDef() && 178 getSubReg() == Other.getSubReg(); 179 case MachineOperand::MO_Immediate: 180 return getImm() == Other.getImm(); 181 case MachineOperand::MO_CImmediate: 182 return getCImm() == Other.getCImm(); 183 case MachineOperand::MO_FPImmediate: 184 return getFPImm() == Other.getFPImm(); 185 case MachineOperand::MO_MachineBasicBlock: 186 return getMBB() == Other.getMBB(); 187 case MachineOperand::MO_FrameIndex: 188 return getIndex() == Other.getIndex(); 189 case MachineOperand::MO_ConstantPoolIndex: 190 case MachineOperand::MO_TargetIndex: 191 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 192 case MachineOperand::MO_JumpTableIndex: 193 return getIndex() == Other.getIndex(); 194 case MachineOperand::MO_GlobalAddress: 195 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 196 case MachineOperand::MO_ExternalSymbol: 197 return !strcmp(getSymbolName(), Other.getSymbolName()) && 198 getOffset() == Other.getOffset(); 199 case MachineOperand::MO_BlockAddress: 200 return getBlockAddress() == Other.getBlockAddress() && 201 getOffset() == Other.getOffset(); 202 case MachineOperand::MO_RegisterMask: 203 case MachineOperand::MO_RegisterLiveOut: 204 return getRegMask() == Other.getRegMask(); 205 case MachineOperand::MO_MCSymbol: 206 return getMCSymbol() == Other.getMCSymbol(); 207 case MachineOperand::MO_CFIIndex: 208 return getCFIIndex() == Other.getCFIIndex(); 209 case MachineOperand::MO_Metadata: 210 return getMetadata() == Other.getMetadata(); 211 } 212 llvm_unreachable("Invalid machine operand type"); 213 } 214 215 // Note: this must stay exactly in sync with isIdenticalTo above. 216 hash_code llvm::hash_value(const MachineOperand &MO) { 217 switch (MO.getType()) { 218 case MachineOperand::MO_Register: 219 // Register operands don't have target flags. 220 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef()); 221 case MachineOperand::MO_Immediate: 222 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm()); 223 case MachineOperand::MO_CImmediate: 224 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm()); 225 case MachineOperand::MO_FPImmediate: 226 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm()); 227 case MachineOperand::MO_MachineBasicBlock: 228 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB()); 229 case MachineOperand::MO_FrameIndex: 230 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 231 case MachineOperand::MO_ConstantPoolIndex: 232 case MachineOperand::MO_TargetIndex: 233 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(), 234 MO.getOffset()); 235 case MachineOperand::MO_JumpTableIndex: 236 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 237 case MachineOperand::MO_ExternalSymbol: 238 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(), 239 MO.getSymbolName()); 240 case MachineOperand::MO_GlobalAddress: 241 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(), 242 MO.getOffset()); 243 case MachineOperand::MO_BlockAddress: 244 return hash_combine(MO.getType(), MO.getTargetFlags(), 245 MO.getBlockAddress(), MO.getOffset()); 246 case MachineOperand::MO_RegisterMask: 247 case MachineOperand::MO_RegisterLiveOut: 248 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask()); 249 case MachineOperand::MO_Metadata: 250 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata()); 251 case MachineOperand::MO_MCSymbol: 252 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol()); 253 case MachineOperand::MO_CFIIndex: 254 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex()); 255 } 256 llvm_unreachable("Invalid machine operand type"); 257 } 258 259 /// print - Print the specified machine operand. 260 /// 261 void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const { 262 // If the instruction is embedded into a basic block, we can find the 263 // target info for the instruction. 264 if (!TM) 265 if (const MachineInstr *MI = getParent()) 266 if (const MachineBasicBlock *MBB = MI->getParent()) 267 if (const MachineFunction *MF = MBB->getParent()) 268 TM = &MF->getTarget(); 269 const TargetRegisterInfo *TRI = 270 TM ? TM->getSubtargetImpl()->getRegisterInfo() : nullptr; 271 272 switch (getType()) { 273 case MachineOperand::MO_Register: 274 OS << PrintReg(getReg(), TRI, getSubReg()); 275 276 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || 277 isInternalRead() || isEarlyClobber() || isTied()) { 278 OS << '<'; 279 bool NeedComma = false; 280 if (isDef()) { 281 if (NeedComma) OS << ','; 282 if (isEarlyClobber()) 283 OS << "earlyclobber,"; 284 if (isImplicit()) 285 OS << "imp-"; 286 OS << "def"; 287 NeedComma = true; 288 // <def,read-undef> only makes sense when getSubReg() is set. 289 // Don't clutter the output otherwise. 290 if (isUndef() && getSubReg()) 291 OS << ",read-undef"; 292 } else if (isImplicit()) { 293 OS << "imp-use"; 294 NeedComma = true; 295 } 296 297 if (isKill()) { 298 if (NeedComma) OS << ','; 299 OS << "kill"; 300 NeedComma = true; 301 } 302 if (isDead()) { 303 if (NeedComma) OS << ','; 304 OS << "dead"; 305 NeedComma = true; 306 } 307 if (isUndef() && isUse()) { 308 if (NeedComma) OS << ','; 309 OS << "undef"; 310 NeedComma = true; 311 } 312 if (isInternalRead()) { 313 if (NeedComma) OS << ','; 314 OS << "internal"; 315 NeedComma = true; 316 } 317 if (isTied()) { 318 if (NeedComma) OS << ','; 319 OS << "tied"; 320 if (TiedTo != 15) 321 OS << unsigned(TiedTo - 1); 322 } 323 OS << '>'; 324 } 325 break; 326 case MachineOperand::MO_Immediate: 327 OS << getImm(); 328 break; 329 case MachineOperand::MO_CImmediate: 330 getCImm()->getValue().print(OS, false); 331 break; 332 case MachineOperand::MO_FPImmediate: 333 if (getFPImm()->getType()->isFloatTy()) 334 OS << getFPImm()->getValueAPF().convertToFloat(); 335 else 336 OS << getFPImm()->getValueAPF().convertToDouble(); 337 break; 338 case MachineOperand::MO_MachineBasicBlock: 339 OS << "<BB#" << getMBB()->getNumber() << ">"; 340 break; 341 case MachineOperand::MO_FrameIndex: 342 OS << "<fi#" << getIndex() << '>'; 343 break; 344 case MachineOperand::MO_ConstantPoolIndex: 345 OS << "<cp#" << getIndex(); 346 if (getOffset()) OS << "+" << getOffset(); 347 OS << '>'; 348 break; 349 case MachineOperand::MO_TargetIndex: 350 OS << "<ti#" << getIndex(); 351 if (getOffset()) OS << "+" << getOffset(); 352 OS << '>'; 353 break; 354 case MachineOperand::MO_JumpTableIndex: 355 OS << "<jt#" << getIndex() << '>'; 356 break; 357 case MachineOperand::MO_GlobalAddress: 358 OS << "<ga:"; 359 getGlobal()->printAsOperand(OS, /*PrintType=*/false); 360 if (getOffset()) OS << "+" << getOffset(); 361 OS << '>'; 362 break; 363 case MachineOperand::MO_ExternalSymbol: 364 OS << "<es:" << getSymbolName(); 365 if (getOffset()) OS << "+" << getOffset(); 366 OS << '>'; 367 break; 368 case MachineOperand::MO_BlockAddress: 369 OS << '<'; 370 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false); 371 if (getOffset()) OS << "+" << getOffset(); 372 OS << '>'; 373 break; 374 case MachineOperand::MO_RegisterMask: 375 OS << "<regmask>"; 376 break; 377 case MachineOperand::MO_RegisterLiveOut: 378 OS << "<regliveout>"; 379 break; 380 case MachineOperand::MO_Metadata: 381 OS << '<'; 382 getMetadata()->printAsOperand(OS, /*PrintType=*/false); 383 OS << '>'; 384 break; 385 case MachineOperand::MO_MCSymbol: 386 OS << "<MCSym=" << *getMCSymbol() << '>'; 387 break; 388 case MachineOperand::MO_CFIIndex: 389 OS << "<call frame instruction>"; 390 break; 391 } 392 393 if (unsigned TF = getTargetFlags()) 394 OS << "[TF=" << TF << ']'; 395 } 396 397 //===----------------------------------------------------------------------===// 398 // MachineMemOperand Implementation 399 //===----------------------------------------------------------------------===// 400 401 /// getAddrSpace - Return the LLVM IR address space number that this pointer 402 /// points into. 403 unsigned MachinePointerInfo::getAddrSpace() const { 404 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0; 405 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace(); 406 } 407 408 /// getConstantPool - Return a MachinePointerInfo record that refers to the 409 /// constant pool. 410 MachinePointerInfo MachinePointerInfo::getConstantPool() { 411 return MachinePointerInfo(PseudoSourceValue::getConstantPool()); 412 } 413 414 /// getFixedStack - Return a MachinePointerInfo record that refers to the 415 /// the specified FrameIndex. 416 MachinePointerInfo MachinePointerInfo::getFixedStack(int FI, int64_t offset) { 417 return MachinePointerInfo(PseudoSourceValue::getFixedStack(FI), offset); 418 } 419 420 MachinePointerInfo MachinePointerInfo::getJumpTable() { 421 return MachinePointerInfo(PseudoSourceValue::getJumpTable()); 422 } 423 424 MachinePointerInfo MachinePointerInfo::getGOT() { 425 return MachinePointerInfo(PseudoSourceValue::getGOT()); 426 } 427 428 MachinePointerInfo MachinePointerInfo::getStack(int64_t Offset) { 429 return MachinePointerInfo(PseudoSourceValue::getStack(), Offset); 430 } 431 432 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, unsigned f, 433 uint64_t s, unsigned int a, 434 const AAMDNodes &AAInfo, 435 const MDNode *Ranges) 436 : PtrInfo(ptrinfo), Size(s), 437 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)), 438 AAInfo(AAInfo), Ranges(Ranges) { 439 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() || 440 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) && 441 "invalid pointer value"); 442 assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); 443 assert((isLoad() || isStore()) && "Not a load/store!"); 444 } 445 446 /// Profile - Gather unique data for the object. 447 /// 448 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 449 ID.AddInteger(getOffset()); 450 ID.AddInteger(Size); 451 ID.AddPointer(getOpaqueValue()); 452 ID.AddInteger(Flags); 453 } 454 455 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { 456 // The Value and Offset may differ due to CSE. But the flags and size 457 // should be the same. 458 assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); 459 assert(MMO->getSize() == getSize() && "Size mismatch!"); 460 461 if (MMO->getBaseAlignment() >= getBaseAlignment()) { 462 // Update the alignment value. 463 Flags = (Flags & ((1 << MOMaxBits) - 1)) | 464 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits); 465 // Also update the base and offset, because the new alignment may 466 // not be applicable with the old ones. 467 PtrInfo = MMO->PtrInfo; 468 } 469 } 470 471 /// getAlignment - Return the minimum known alignment in bytes of the 472 /// actual memory reference. 473 uint64_t MachineMemOperand::getAlignment() const { 474 return MinAlign(getBaseAlignment(), getOffset()); 475 } 476 477 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) { 478 assert((MMO.isLoad() || MMO.isStore()) && 479 "SV has to be a load, store or both."); 480 481 if (MMO.isVolatile()) 482 OS << "Volatile "; 483 484 if (MMO.isLoad()) 485 OS << "LD"; 486 if (MMO.isStore()) 487 OS << "ST"; 488 OS << MMO.getSize(); 489 490 // Print the address information. 491 OS << "["; 492 if (const Value *V = MMO.getValue()) 493 V->printAsOperand(OS, /*PrintType=*/false); 494 else if (const PseudoSourceValue *PSV = MMO.getPseudoValue()) 495 PSV->printCustom(OS); 496 else 497 OS << "<unknown>"; 498 499 unsigned AS = MMO.getAddrSpace(); 500 if (AS != 0) 501 OS << "(addrspace=" << AS << ')'; 502 503 // If the alignment of the memory reference itself differs from the alignment 504 // of the base pointer, print the base alignment explicitly, next to the base 505 // pointer. 506 if (MMO.getBaseAlignment() != MMO.getAlignment()) 507 OS << "(align=" << MMO.getBaseAlignment() << ")"; 508 509 if (MMO.getOffset() != 0) 510 OS << "+" << MMO.getOffset(); 511 OS << "]"; 512 513 // Print the alignment of the reference. 514 if (MMO.getBaseAlignment() != MMO.getAlignment() || 515 MMO.getBaseAlignment() != MMO.getSize()) 516 OS << "(align=" << MMO.getAlignment() << ")"; 517 518 // Print TBAA info. 519 if (const MDNode *TBAAInfo = MMO.getAAInfo().TBAA) { 520 OS << "(tbaa="; 521 if (TBAAInfo->getNumOperands() > 0) 522 TBAAInfo->getOperand(0)->printAsOperand(OS, /*PrintType=*/false); 523 else 524 OS << "<unknown>"; 525 OS << ")"; 526 } 527 528 // Print AA scope info. 529 if (const MDNode *ScopeInfo = MMO.getAAInfo().Scope) { 530 OS << "(alias.scope="; 531 if (ScopeInfo->getNumOperands() > 0) 532 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) { 533 ScopeInfo->getOperand(i)->printAsOperand(OS, /*PrintType=*/false); 534 if (i != ie-1) 535 OS << ","; 536 } 537 else 538 OS << "<unknown>"; 539 OS << ")"; 540 } 541 542 // Print AA noalias scope info. 543 if (const MDNode *NoAliasInfo = MMO.getAAInfo().NoAlias) { 544 OS << "(noalias="; 545 if (NoAliasInfo->getNumOperands() > 0) 546 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) { 547 NoAliasInfo->getOperand(i)->printAsOperand(OS, /*PrintType=*/false); 548 if (i != ie-1) 549 OS << ","; 550 } 551 else 552 OS << "<unknown>"; 553 OS << ")"; 554 } 555 556 // Print nontemporal info. 557 if (MMO.isNonTemporal()) 558 OS << "(nontemporal)"; 559 560 return OS; 561 } 562 563 //===----------------------------------------------------------------------===// 564 // MachineInstr Implementation 565 //===----------------------------------------------------------------------===// 566 567 void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) { 568 if (MCID->ImplicitDefs) 569 for (const uint16_t *ImpDefs = MCID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 570 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true)); 571 if (MCID->ImplicitUses) 572 for (const uint16_t *ImpUses = MCID->getImplicitUses(); *ImpUses; ++ImpUses) 573 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true)); 574 } 575 576 /// MachineInstr ctor - This constructor creates a MachineInstr and adds the 577 /// implicit operands. It reserves space for the number of operands specified by 578 /// the MCInstrDesc. 579 MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid, 580 const DebugLoc dl, bool NoImp) 581 : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), 582 Flags(0), AsmPrinterFlags(0), 583 NumMemRefs(0), MemRefs(nullptr), debugLoc(dl) { 584 // Reserve space for the expected number of operands. 585 if (unsigned NumOps = MCID->getNumOperands() + 586 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) { 587 CapOperands = OperandCapacity::get(NumOps); 588 Operands = MF.allocateOperandArray(CapOperands); 589 } 590 591 if (!NoImp) 592 addImplicitDefUseOperands(MF); 593 } 594 595 /// MachineInstr ctor - Copies MachineInstr arg exactly 596 /// 597 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) 598 : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0), 599 Flags(0), AsmPrinterFlags(0), 600 NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs), 601 debugLoc(MI.getDebugLoc()) { 602 CapOperands = OperandCapacity::get(MI.getNumOperands()); 603 Operands = MF.allocateOperandArray(CapOperands); 604 605 // Copy operands. 606 for (unsigned i = 0; i != MI.getNumOperands(); ++i) 607 addOperand(MF, MI.getOperand(i)); 608 609 // Copy all the sensible flags. 610 setFlags(MI.Flags); 611 } 612 613 /// getRegInfo - If this instruction is embedded into a MachineFunction, 614 /// return the MachineRegisterInfo object for the current function, otherwise 615 /// return null. 616 MachineRegisterInfo *MachineInstr::getRegInfo() { 617 if (MachineBasicBlock *MBB = getParent()) 618 return &MBB->getParent()->getRegInfo(); 619 return nullptr; 620 } 621 622 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in 623 /// this instruction from their respective use lists. This requires that the 624 /// operands already be on their use lists. 625 void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) { 626 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 627 if (Operands[i].isReg()) 628 MRI.removeRegOperandFromUseList(&Operands[i]); 629 } 630 631 /// AddRegOperandsToUseLists - Add all of the register operands in 632 /// this instruction from their respective use lists. This requires that the 633 /// operands not be on their use lists yet. 634 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) { 635 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 636 if (Operands[i].isReg()) 637 MRI.addRegOperandToUseList(&Operands[i]); 638 } 639 640 void MachineInstr::addOperand(const MachineOperand &Op) { 641 MachineBasicBlock *MBB = getParent(); 642 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs"); 643 MachineFunction *MF = MBB->getParent(); 644 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs"); 645 addOperand(*MF, Op); 646 } 647 648 /// Move NumOps MachineOperands from Src to Dst, with support for overlapping 649 /// ranges. If MRI is non-null also update use-def chains. 650 static void moveOperands(MachineOperand *Dst, MachineOperand *Src, 651 unsigned NumOps, MachineRegisterInfo *MRI) { 652 if (MRI) 653 return MRI->moveOperands(Dst, Src, NumOps); 654 655 // Here it would be convenient to call memmove, so that isn't allowed because 656 // MachineOperand has a constructor and so isn't a POD type. 657 if (Dst < Src) 658 for (unsigned i = 0; i != NumOps; ++i) 659 new (Dst + i) MachineOperand(Src[i]); 660 else 661 for (unsigned i = NumOps; i ; --i) 662 new (Dst + i - 1) MachineOperand(Src[i - 1]); 663 } 664 665 /// addOperand - Add the specified operand to the instruction. If it is an 666 /// implicit operand, it is added to the end of the operand list. If it is 667 /// an explicit operand it is added at the end of the explicit operand list 668 /// (before the first implicit operand). 669 void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) { 670 assert(MCID && "Cannot add operands before providing an instr descriptor"); 671 672 // Check if we're adding one of our existing operands. 673 if (&Op >= Operands && &Op < Operands + NumOperands) { 674 // This is unusual: MI->addOperand(MI->getOperand(i)). 675 // If adding Op requires reallocating or moving existing operands around, 676 // the Op reference could go stale. Support it by copying Op. 677 MachineOperand CopyOp(Op); 678 return addOperand(MF, CopyOp); 679 } 680 681 // Find the insert location for the new operand. Implicit registers go at 682 // the end, everything else goes before the implicit regs. 683 // 684 // FIXME: Allow mixed explicit and implicit operands on inline asm. 685 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as 686 // implicit-defs, but they must not be moved around. See the FIXME in 687 // InstrEmitter.cpp. 688 unsigned OpNo = getNumOperands(); 689 bool isImpReg = Op.isReg() && Op.isImplicit(); 690 if (!isImpReg && !isInlineAsm()) { 691 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) { 692 --OpNo; 693 assert(!Operands[OpNo].isTied() && "Cannot move tied operands"); 694 } 695 } 696 697 #ifndef NDEBUG 698 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata; 699 // OpNo now points as the desired insertion point. Unless this is a variadic 700 // instruction, only implicit regs are allowed beyond MCID->getNumOperands(). 701 // RegMask operands go between the explicit and implicit operands. 702 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() || 703 OpNo < MCID->getNumOperands() || isMetaDataOp) && 704 "Trying to add an operand to a machine instr that is already done!"); 705 #endif 706 707 MachineRegisterInfo *MRI = getRegInfo(); 708 709 // Determine if the Operands array needs to be reallocated. 710 // Save the old capacity and operand array. 711 OperandCapacity OldCap = CapOperands; 712 MachineOperand *OldOperands = Operands; 713 if (!OldOperands || OldCap.getSize() == getNumOperands()) { 714 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1); 715 Operands = MF.allocateOperandArray(CapOperands); 716 // Move the operands before the insertion point. 717 if (OpNo) 718 moveOperands(Operands, OldOperands, OpNo, MRI); 719 } 720 721 // Move the operands following the insertion point. 722 if (OpNo != NumOperands) 723 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo, 724 MRI); 725 ++NumOperands; 726 727 // Deallocate the old operand array. 728 if (OldOperands != Operands && OldOperands) 729 MF.deallocateOperandArray(OldCap, OldOperands); 730 731 // Copy Op into place. It still needs to be inserted into the MRI use lists. 732 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op); 733 NewMO->ParentMI = this; 734 735 // When adding a register operand, tell MRI about it. 736 if (NewMO->isReg()) { 737 // Ensure isOnRegUseList() returns false, regardless of Op's status. 738 NewMO->Contents.Reg.Prev = nullptr; 739 // Ignore existing ties. This is not a property that can be copied. 740 NewMO->TiedTo = 0; 741 // Add the new operand to MRI, but only for instructions in an MBB. 742 if (MRI) 743 MRI->addRegOperandToUseList(NewMO); 744 // The MCID operand information isn't accurate until we start adding 745 // explicit operands. The implicit operands are added first, then the 746 // explicits are inserted before them. 747 if (!isImpReg) { 748 // Tie uses to defs as indicated in MCInstrDesc. 749 if (NewMO->isUse()) { 750 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO); 751 if (DefIdx != -1) 752 tieOperands(DefIdx, OpNo); 753 } 754 // If the register operand is flagged as early, mark the operand as such. 755 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1) 756 NewMO->setIsEarlyClobber(true); 757 } 758 } 759 } 760 761 /// RemoveOperand - Erase an operand from an instruction, leaving it with one 762 /// fewer operand than it started with. 763 /// 764 void MachineInstr::RemoveOperand(unsigned OpNo) { 765 assert(OpNo < getNumOperands() && "Invalid operand number"); 766 untieRegOperand(OpNo); 767 768 #ifndef NDEBUG 769 // Moving tied operands would break the ties. 770 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i) 771 if (Operands[i].isReg()) 772 assert(!Operands[i].isTied() && "Cannot move tied operands"); 773 #endif 774 775 MachineRegisterInfo *MRI = getRegInfo(); 776 if (MRI && Operands[OpNo].isReg()) 777 MRI->removeRegOperandFromUseList(Operands + OpNo); 778 779 // Don't call the MachineOperand destructor. A lot of this code depends on 780 // MachineOperand having a trivial destructor anyway, and adding a call here 781 // wouldn't make it 'destructor-correct'. 782 783 if (unsigned N = NumOperands - 1 - OpNo) 784 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI); 785 --NumOperands; 786 } 787 788 /// addMemOperand - Add a MachineMemOperand to the machine instruction. 789 /// This function should be used only occasionally. The setMemRefs function 790 /// is the primary method for setting up a MachineInstr's MemRefs list. 791 void MachineInstr::addMemOperand(MachineFunction &MF, 792 MachineMemOperand *MO) { 793 mmo_iterator OldMemRefs = MemRefs; 794 unsigned OldNumMemRefs = NumMemRefs; 795 796 unsigned NewNum = NumMemRefs + 1; 797 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum); 798 799 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs); 800 NewMemRefs[NewNum - 1] = MO; 801 setMemRefs(NewMemRefs, NewMemRefs + NewNum); 802 } 803 804 bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const { 805 assert(!isBundledWithPred() && "Must be called on bundle header"); 806 for (MachineBasicBlock::const_instr_iterator MII = this;; ++MII) { 807 if (MII->getDesc().getFlags() & Mask) { 808 if (Type == AnyInBundle) 809 return true; 810 } else { 811 if (Type == AllInBundle && !MII->isBundle()) 812 return false; 813 } 814 // This was the last instruction in the bundle. 815 if (!MII->isBundledWithSucc()) 816 return Type == AllInBundle; 817 } 818 } 819 820 bool MachineInstr::isIdenticalTo(const MachineInstr *Other, 821 MICheckType Check) const { 822 // If opcodes or number of operands are not the same then the two 823 // instructions are obviously not identical. 824 if (Other->getOpcode() != getOpcode() || 825 Other->getNumOperands() != getNumOperands()) 826 return false; 827 828 if (isBundle()) { 829 // Both instructions are bundles, compare MIs inside the bundle. 830 MachineBasicBlock::const_instr_iterator I1 = *this; 831 MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end(); 832 MachineBasicBlock::const_instr_iterator I2 = *Other; 833 MachineBasicBlock::const_instr_iterator E2= Other->getParent()->instr_end(); 834 while (++I1 != E1 && I1->isInsideBundle()) { 835 ++I2; 836 if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(I2, Check)) 837 return false; 838 } 839 } 840 841 // Check operands to make sure they match. 842 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 843 const MachineOperand &MO = getOperand(i); 844 const MachineOperand &OMO = Other->getOperand(i); 845 if (!MO.isReg()) { 846 if (!MO.isIdenticalTo(OMO)) 847 return false; 848 continue; 849 } 850 851 // Clients may or may not want to ignore defs when testing for equality. 852 // For example, machine CSE pass only cares about finding common 853 // subexpressions, so it's safe to ignore virtual register defs. 854 if (MO.isDef()) { 855 if (Check == IgnoreDefs) 856 continue; 857 else if (Check == IgnoreVRegDefs) { 858 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || 859 TargetRegisterInfo::isPhysicalRegister(OMO.getReg())) 860 if (MO.getReg() != OMO.getReg()) 861 return false; 862 } else { 863 if (!MO.isIdenticalTo(OMO)) 864 return false; 865 if (Check == CheckKillDead && MO.isDead() != OMO.isDead()) 866 return false; 867 } 868 } else { 869 if (!MO.isIdenticalTo(OMO)) 870 return false; 871 if (Check == CheckKillDead && MO.isKill() != OMO.isKill()) 872 return false; 873 } 874 } 875 // If DebugLoc does not match then two dbg.values are not identical. 876 if (isDebugValue()) 877 if (!getDebugLoc().isUnknown() && !Other->getDebugLoc().isUnknown() 878 && getDebugLoc() != Other->getDebugLoc()) 879 return false; 880 return true; 881 } 882 883 MachineInstr *MachineInstr::removeFromParent() { 884 assert(getParent() && "Not embedded in a basic block!"); 885 return getParent()->remove(this); 886 } 887 888 MachineInstr *MachineInstr::removeFromBundle() { 889 assert(getParent() && "Not embedded in a basic block!"); 890 return getParent()->remove_instr(this); 891 } 892 893 void MachineInstr::eraseFromParent() { 894 assert(getParent() && "Not embedded in a basic block!"); 895 getParent()->erase(this); 896 } 897 898 void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() { 899 assert(getParent() && "Not embedded in a basic block!"); 900 MachineBasicBlock *MBB = getParent(); 901 MachineFunction *MF = MBB->getParent(); 902 assert(MF && "Not embedded in a function!"); 903 904 MachineInstr *MI = (MachineInstr *)this; 905 MachineRegisterInfo &MRI = MF->getRegInfo(); 906 907 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 908 const MachineOperand &MO = MI->getOperand(i); 909 if (!MO.isReg() || !MO.isDef()) 910 continue; 911 unsigned Reg = MO.getReg(); 912 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 913 continue; 914 MRI.markUsesInDebugValueAsUndef(Reg); 915 } 916 MI->eraseFromParent(); 917 } 918 919 void MachineInstr::eraseFromBundle() { 920 assert(getParent() && "Not embedded in a basic block!"); 921 getParent()->erase_instr(this); 922 } 923 924 /// getNumExplicitOperands - Returns the number of non-implicit operands. 925 /// 926 unsigned MachineInstr::getNumExplicitOperands() const { 927 unsigned NumOperands = MCID->getNumOperands(); 928 if (!MCID->isVariadic()) 929 return NumOperands; 930 931 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { 932 const MachineOperand &MO = getOperand(i); 933 if (!MO.isReg() || !MO.isImplicit()) 934 NumOperands++; 935 } 936 return NumOperands; 937 } 938 939 void MachineInstr::bundleWithPred() { 940 assert(!isBundledWithPred() && "MI is already bundled with its predecessor"); 941 setFlag(BundledPred); 942 MachineBasicBlock::instr_iterator Pred = this; 943 --Pred; 944 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags"); 945 Pred->setFlag(BundledSucc); 946 } 947 948 void MachineInstr::bundleWithSucc() { 949 assert(!isBundledWithSucc() && "MI is already bundled with its successor"); 950 setFlag(BundledSucc); 951 MachineBasicBlock::instr_iterator Succ = this; 952 ++Succ; 953 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags"); 954 Succ->setFlag(BundledPred); 955 } 956 957 void MachineInstr::unbundleFromPred() { 958 assert(isBundledWithPred() && "MI isn't bundled with its predecessor"); 959 clearFlag(BundledPred); 960 MachineBasicBlock::instr_iterator Pred = this; 961 --Pred; 962 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags"); 963 Pred->clearFlag(BundledSucc); 964 } 965 966 void MachineInstr::unbundleFromSucc() { 967 assert(isBundledWithSucc() && "MI isn't bundled with its successor"); 968 clearFlag(BundledSucc); 969 MachineBasicBlock::instr_iterator Succ = this; 970 ++Succ; 971 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags"); 972 Succ->clearFlag(BundledPred); 973 } 974 975 bool MachineInstr::isStackAligningInlineAsm() const { 976 if (isInlineAsm()) { 977 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 978 if (ExtraInfo & InlineAsm::Extra_IsAlignStack) 979 return true; 980 } 981 return false; 982 } 983 984 InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const { 985 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!"); 986 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 987 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0); 988 } 989 990 int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx, 991 unsigned *GroupNo) const { 992 assert(isInlineAsm() && "Expected an inline asm instruction"); 993 assert(OpIdx < getNumOperands() && "OpIdx out of range"); 994 995 // Ignore queries about the initial operands. 996 if (OpIdx < InlineAsm::MIOp_FirstOperand) 997 return -1; 998 999 unsigned Group = 0; 1000 unsigned NumOps; 1001 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e; 1002 i += NumOps) { 1003 const MachineOperand &FlagMO = getOperand(i); 1004 // If we reach the implicit register operands, stop looking. 1005 if (!FlagMO.isImm()) 1006 return -1; 1007 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm()); 1008 if (i + NumOps > OpIdx) { 1009 if (GroupNo) 1010 *GroupNo = Group; 1011 return i; 1012 } 1013 ++Group; 1014 } 1015 return -1; 1016 } 1017 1018 const TargetRegisterClass* 1019 MachineInstr::getRegClassConstraint(unsigned OpIdx, 1020 const TargetInstrInfo *TII, 1021 const TargetRegisterInfo *TRI) const { 1022 assert(getParent() && "Can't have an MBB reference here!"); 1023 assert(getParent()->getParent() && "Can't have an MF reference here!"); 1024 const MachineFunction &MF = *getParent()->getParent(); 1025 1026 // Most opcodes have fixed constraints in their MCInstrDesc. 1027 if (!isInlineAsm()) 1028 return TII->getRegClass(getDesc(), OpIdx, TRI, MF); 1029 1030 if (!getOperand(OpIdx).isReg()) 1031 return nullptr; 1032 1033 // For tied uses on inline asm, get the constraint from the def. 1034 unsigned DefIdx; 1035 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx)) 1036 OpIdx = DefIdx; 1037 1038 // Inline asm stores register class constraints in the flag word. 1039 int FlagIdx = findInlineAsmFlagIdx(OpIdx); 1040 if (FlagIdx < 0) 1041 return nullptr; 1042 1043 unsigned Flag = getOperand(FlagIdx).getImm(); 1044 unsigned RCID; 1045 if (InlineAsm::hasRegClassConstraint(Flag, RCID)) 1046 return TRI->getRegClass(RCID); 1047 1048 // Assume that all registers in a memory operand are pointers. 1049 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem) 1050 return TRI->getPointerRegClass(MF); 1051 1052 return nullptr; 1053 } 1054 1055 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg( 1056 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII, 1057 const TargetRegisterInfo *TRI, bool ExploreBundle) const { 1058 // Check every operands inside the bundle if we have 1059 // been asked to. 1060 if (ExploreBundle) 1061 for (ConstMIBundleOperands OpndIt(this); OpndIt.isValid() && CurRC; 1062 ++OpndIt) 1063 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl( 1064 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI); 1065 else 1066 // Otherwise, just check the current operands. 1067 for (ConstMIOperands OpndIt(this); OpndIt.isValid() && CurRC; ++OpndIt) 1068 CurRC = getRegClassConstraintEffectForVRegImpl(OpndIt.getOperandNo(), Reg, 1069 CurRC, TII, TRI); 1070 return CurRC; 1071 } 1072 1073 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl( 1074 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC, 1075 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const { 1076 assert(CurRC && "Invalid initial register class"); 1077 // Check if Reg is constrained by some of its use/def from MI. 1078 const MachineOperand &MO = getOperand(OpIdx); 1079 if (!MO.isReg() || MO.getReg() != Reg) 1080 return CurRC; 1081 // If yes, accumulate the constraints through the operand. 1082 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI); 1083 } 1084 1085 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect( 1086 unsigned OpIdx, const TargetRegisterClass *CurRC, 1087 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const { 1088 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI); 1089 const MachineOperand &MO = getOperand(OpIdx); 1090 assert(MO.isReg() && 1091 "Cannot get register constraints for non-register operand"); 1092 assert(CurRC && "Invalid initial register class"); 1093 if (unsigned SubIdx = MO.getSubReg()) { 1094 if (OpRC) 1095 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx); 1096 else 1097 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx); 1098 } else if (OpRC) 1099 CurRC = TRI->getCommonSubClass(CurRC, OpRC); 1100 return CurRC; 1101 } 1102 1103 /// Return the number of instructions inside the MI bundle, not counting the 1104 /// header instruction. 1105 unsigned MachineInstr::getBundleSize() const { 1106 MachineBasicBlock::const_instr_iterator I = this; 1107 unsigned Size = 0; 1108 while (I->isBundledWithSucc()) 1109 ++Size, ++I; 1110 return Size; 1111 } 1112 1113 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of 1114 /// the specific register or -1 if it is not found. It further tightens 1115 /// the search criteria to a use that kills the register if isKill is true. 1116 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, 1117 const TargetRegisterInfo *TRI) const { 1118 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1119 const MachineOperand &MO = getOperand(i); 1120 if (!MO.isReg() || !MO.isUse()) 1121 continue; 1122 unsigned MOReg = MO.getReg(); 1123 if (!MOReg) 1124 continue; 1125 if (MOReg == Reg || 1126 (TRI && 1127 TargetRegisterInfo::isPhysicalRegister(MOReg) && 1128 TargetRegisterInfo::isPhysicalRegister(Reg) && 1129 TRI->isSubRegister(MOReg, Reg))) 1130 if (!isKill || MO.isKill()) 1131 return i; 1132 } 1133 return -1; 1134 } 1135 1136 /// readsWritesVirtualRegister - Return a pair of bools (reads, writes) 1137 /// indicating if this instruction reads or writes Reg. This also considers 1138 /// partial defines. 1139 std::pair<bool,bool> 1140 MachineInstr::readsWritesVirtualRegister(unsigned Reg, 1141 SmallVectorImpl<unsigned> *Ops) const { 1142 bool PartDef = false; // Partial redefine. 1143 bool FullDef = false; // Full define. 1144 bool Use = false; 1145 1146 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1147 const MachineOperand &MO = getOperand(i); 1148 if (!MO.isReg() || MO.getReg() != Reg) 1149 continue; 1150 if (Ops) 1151 Ops->push_back(i); 1152 if (MO.isUse()) 1153 Use |= !MO.isUndef(); 1154 else if (MO.getSubReg() && !MO.isUndef()) 1155 // A partial <def,undef> doesn't count as reading the register. 1156 PartDef = true; 1157 else 1158 FullDef = true; 1159 } 1160 // A partial redefine uses Reg unless there is also a full define. 1161 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef); 1162 } 1163 1164 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of 1165 /// the specified register or -1 if it is not found. If isDead is true, defs 1166 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it 1167 /// also checks if there is a def of a super-register. 1168 int 1169 MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap, 1170 const TargetRegisterInfo *TRI) const { 1171 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg); 1172 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1173 const MachineOperand &MO = getOperand(i); 1174 // Accept regmask operands when Overlap is set. 1175 // Ignore them when looking for a specific def operand (Overlap == false). 1176 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg)) 1177 return i; 1178 if (!MO.isReg() || !MO.isDef()) 1179 continue; 1180 unsigned MOReg = MO.getReg(); 1181 bool Found = (MOReg == Reg); 1182 if (!Found && TRI && isPhys && 1183 TargetRegisterInfo::isPhysicalRegister(MOReg)) { 1184 if (Overlap) 1185 Found = TRI->regsOverlap(MOReg, Reg); 1186 else 1187 Found = TRI->isSubRegister(MOReg, Reg); 1188 } 1189 if (Found && (!isDead || MO.isDead())) 1190 return i; 1191 } 1192 return -1; 1193 } 1194 1195 /// findFirstPredOperandIdx() - Find the index of the first operand in the 1196 /// operand list that is used to represent the predicate. It returns -1 if 1197 /// none is found. 1198 int MachineInstr::findFirstPredOperandIdx() const { 1199 // Don't call MCID.findFirstPredOperandIdx() because this variant 1200 // is sometimes called on an instruction that's not yet complete, and 1201 // so the number of operands is less than the MCID indicates. In 1202 // particular, the PTX target does this. 1203 const MCInstrDesc &MCID = getDesc(); 1204 if (MCID.isPredicable()) { 1205 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 1206 if (MCID.OpInfo[i].isPredicate()) 1207 return i; 1208 } 1209 1210 return -1; 1211 } 1212 1213 // MachineOperand::TiedTo is 4 bits wide. 1214 const unsigned TiedMax = 15; 1215 1216 /// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other. 1217 /// 1218 /// Use and def operands can be tied together, indicated by a non-zero TiedTo 1219 /// field. TiedTo can have these values: 1220 /// 1221 /// 0: Operand is not tied to anything. 1222 /// 1 to TiedMax-1: Tied to getOperand(TiedTo-1). 1223 /// TiedMax: Tied to an operand >= TiedMax-1. 1224 /// 1225 /// The tied def must be one of the first TiedMax operands on a normal 1226 /// instruction. INLINEASM instructions allow more tied defs. 1227 /// 1228 void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) { 1229 MachineOperand &DefMO = getOperand(DefIdx); 1230 MachineOperand &UseMO = getOperand(UseIdx); 1231 assert(DefMO.isDef() && "DefIdx must be a def operand"); 1232 assert(UseMO.isUse() && "UseIdx must be a use operand"); 1233 assert(!DefMO.isTied() && "Def is already tied to another use"); 1234 assert(!UseMO.isTied() && "Use is already tied to another def"); 1235 1236 if (DefIdx < TiedMax) 1237 UseMO.TiedTo = DefIdx + 1; 1238 else { 1239 // Inline asm can use the group descriptors to find tied operands, but on 1240 // normal instruction, the tied def must be within the first TiedMax 1241 // operands. 1242 assert(isInlineAsm() && "DefIdx out of range"); 1243 UseMO.TiedTo = TiedMax; 1244 } 1245 1246 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx(). 1247 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax); 1248 } 1249 1250 /// Given the index of a tied register operand, find the operand it is tied to. 1251 /// Defs are tied to uses and vice versa. Returns the index of the tied operand 1252 /// which must exist. 1253 unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const { 1254 const MachineOperand &MO = getOperand(OpIdx); 1255 assert(MO.isTied() && "Operand isn't tied"); 1256 1257 // Normally TiedTo is in range. 1258 if (MO.TiedTo < TiedMax) 1259 return MO.TiedTo - 1; 1260 1261 // Uses on normal instructions can be out of range. 1262 if (!isInlineAsm()) { 1263 // Normal tied defs must be in the 0..TiedMax-1 range. 1264 if (MO.isUse()) 1265 return TiedMax - 1; 1266 // MO is a def. Search for the tied use. 1267 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) { 1268 const MachineOperand &UseMO = getOperand(i); 1269 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1) 1270 return i; 1271 } 1272 llvm_unreachable("Can't find tied use"); 1273 } 1274 1275 // Now deal with inline asm by parsing the operand group descriptor flags. 1276 // Find the beginning of each operand group. 1277 SmallVector<unsigned, 8> GroupIdx; 1278 unsigned OpIdxGroup = ~0u; 1279 unsigned NumOps; 1280 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e; 1281 i += NumOps) { 1282 const MachineOperand &FlagMO = getOperand(i); 1283 assert(FlagMO.isImm() && "Invalid tied operand on inline asm"); 1284 unsigned CurGroup = GroupIdx.size(); 1285 GroupIdx.push_back(i); 1286 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm()); 1287 // OpIdx belongs to this operand group. 1288 if (OpIdx > i && OpIdx < i + NumOps) 1289 OpIdxGroup = CurGroup; 1290 unsigned TiedGroup; 1291 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup)) 1292 continue; 1293 // Operands in this group are tied to operands in TiedGroup which must be 1294 // earlier. Find the number of operands between the two groups. 1295 unsigned Delta = i - GroupIdx[TiedGroup]; 1296 1297 // OpIdx is a use tied to TiedGroup. 1298 if (OpIdxGroup == CurGroup) 1299 return OpIdx - Delta; 1300 1301 // OpIdx is a def tied to this use group. 1302 if (OpIdxGroup == TiedGroup) 1303 return OpIdx + Delta; 1304 } 1305 llvm_unreachable("Invalid tied operand on inline asm"); 1306 } 1307 1308 /// clearKillInfo - Clears kill flags on all operands. 1309 /// 1310 void MachineInstr::clearKillInfo() { 1311 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1312 MachineOperand &MO = getOperand(i); 1313 if (MO.isReg() && MO.isUse()) 1314 MO.setIsKill(false); 1315 } 1316 } 1317 1318 void MachineInstr::substituteRegister(unsigned FromReg, 1319 unsigned ToReg, 1320 unsigned SubIdx, 1321 const TargetRegisterInfo &RegInfo) { 1322 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) { 1323 if (SubIdx) 1324 ToReg = RegInfo.getSubReg(ToReg, SubIdx); 1325 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1326 MachineOperand &MO = getOperand(i); 1327 if (!MO.isReg() || MO.getReg() != FromReg) 1328 continue; 1329 MO.substPhysReg(ToReg, RegInfo); 1330 } 1331 } else { 1332 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1333 MachineOperand &MO = getOperand(i); 1334 if (!MO.isReg() || MO.getReg() != FromReg) 1335 continue; 1336 MO.substVirtReg(ToReg, SubIdx, RegInfo); 1337 } 1338 } 1339 } 1340 1341 /// isSafeToMove - Return true if it is safe to move this instruction. If 1342 /// SawStore is set to true, it means that there is a store (or call) between 1343 /// the instruction's location and its intended destination. 1344 bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, 1345 AliasAnalysis *AA, 1346 bool &SawStore) const { 1347 // Ignore stuff that we obviously can't move. 1348 // 1349 // Treat volatile loads as stores. This is not strictly necessary for 1350 // volatiles, but it is required for atomic loads. It is not allowed to move 1351 // a load across an atomic load with Ordering > Monotonic. 1352 if (mayStore() || isCall() || 1353 (mayLoad() && hasOrderedMemoryRef())) { 1354 SawStore = true; 1355 return false; 1356 } 1357 1358 if (isPosition() || isDebugValue() || isTerminator() || 1359 hasUnmodeledSideEffects()) 1360 return false; 1361 1362 // See if this instruction does a load. If so, we have to guarantee that the 1363 // loaded value doesn't change between the load and the its intended 1364 // destination. The check for isInvariantLoad gives the targe the chance to 1365 // classify the load as always returning a constant, e.g. a constant pool 1366 // load. 1367 if (mayLoad() && !isInvariantLoad(AA)) 1368 // Otherwise, this is a real load. If there is a store between the load and 1369 // end of block, we can't move it. 1370 return !SawStore; 1371 1372 return true; 1373 } 1374 1375 /// hasOrderedMemoryRef - Return true if this instruction may have an ordered 1376 /// or volatile memory reference, or if the information describing the memory 1377 /// reference is not available. Return false if it is known to have no ordered 1378 /// memory references. 1379 bool MachineInstr::hasOrderedMemoryRef() const { 1380 // An instruction known never to access memory won't have a volatile access. 1381 if (!mayStore() && 1382 !mayLoad() && 1383 !isCall() && 1384 !hasUnmodeledSideEffects()) 1385 return false; 1386 1387 // Otherwise, if the instruction has no memory reference information, 1388 // conservatively assume it wasn't preserved. 1389 if (memoperands_empty()) 1390 return true; 1391 1392 // Check the memory reference information for ordered references. 1393 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I) 1394 if (!(*I)->isUnordered()) 1395 return true; 1396 1397 return false; 1398 } 1399 1400 /// isInvariantLoad - Return true if this instruction is loading from a 1401 /// location whose value is invariant across the function. For example, 1402 /// loading a value from the constant pool or from the argument area 1403 /// of a function if it does not change. This should only return true of 1404 /// *all* loads the instruction does are invariant (if it does multiple loads). 1405 bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const { 1406 // If the instruction doesn't load at all, it isn't an invariant load. 1407 if (!mayLoad()) 1408 return false; 1409 1410 // If the instruction has lost its memoperands, conservatively assume that 1411 // it may not be an invariant load. 1412 if (memoperands_empty()) 1413 return false; 1414 1415 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo(); 1416 1417 for (mmo_iterator I = memoperands_begin(), 1418 E = memoperands_end(); I != E; ++I) { 1419 if ((*I)->isVolatile()) return false; 1420 if ((*I)->isStore()) return false; 1421 if ((*I)->isInvariant()) return true; 1422 1423 1424 // A load from a constant PseudoSourceValue is invariant. 1425 if (const PseudoSourceValue *PSV = (*I)->getPseudoValue()) 1426 if (PSV->isConstant(MFI)) 1427 continue; 1428 1429 if (const Value *V = (*I)->getValue()) { 1430 // If we have an AliasAnalysis, ask it whether the memory is constant. 1431 if (AA && AA->pointsToConstantMemory( 1432 AliasAnalysis::Location(V, (*I)->getSize(), 1433 (*I)->getAAInfo()))) 1434 continue; 1435 } 1436 1437 // Otherwise assume conservatively. 1438 return false; 1439 } 1440 1441 // Everything checks out. 1442 return true; 1443 } 1444 1445 /// isConstantValuePHI - If the specified instruction is a PHI that always 1446 /// merges together the same virtual register, return the register, otherwise 1447 /// return 0. 1448 unsigned MachineInstr::isConstantValuePHI() const { 1449 if (!isPHI()) 1450 return 0; 1451 assert(getNumOperands() >= 3 && 1452 "It's illegal to have a PHI without source operands"); 1453 1454 unsigned Reg = getOperand(1).getReg(); 1455 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2) 1456 if (getOperand(i).getReg() != Reg) 1457 return 0; 1458 return Reg; 1459 } 1460 1461 bool MachineInstr::hasUnmodeledSideEffects() const { 1462 if (hasProperty(MCID::UnmodeledSideEffects)) 1463 return true; 1464 if (isInlineAsm()) { 1465 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1466 if (ExtraInfo & InlineAsm::Extra_HasSideEffects) 1467 return true; 1468 } 1469 1470 return false; 1471 } 1472 1473 /// allDefsAreDead - Return true if all the defs of this instruction are dead. 1474 /// 1475 bool MachineInstr::allDefsAreDead() const { 1476 for (unsigned i = 0, e = getNumOperands(); i < e; ++i) { 1477 const MachineOperand &MO = getOperand(i); 1478 if (!MO.isReg() || MO.isUse()) 1479 continue; 1480 if (!MO.isDead()) 1481 return false; 1482 } 1483 return true; 1484 } 1485 1486 /// copyImplicitOps - Copy implicit register operands from specified 1487 /// instruction to this instruction. 1488 void MachineInstr::copyImplicitOps(MachineFunction &MF, 1489 const MachineInstr *MI) { 1490 for (unsigned i = MI->getDesc().getNumOperands(), e = MI->getNumOperands(); 1491 i != e; ++i) { 1492 const MachineOperand &MO = MI->getOperand(i); 1493 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask()) 1494 addOperand(MF, MO); 1495 } 1496 } 1497 1498 void MachineInstr::dump() const { 1499 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1500 dbgs() << " " << *this; 1501 #endif 1502 } 1503 1504 static void printDebugLoc(DebugLoc DL, const MachineFunction *MF, 1505 raw_ostream &CommentOS) { 1506 const LLVMContext &Ctx = MF->getFunction()->getContext(); 1507 DL.print(Ctx, CommentOS); 1508 } 1509 1510 void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM, 1511 bool SkipOpers) const { 1512 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction. 1513 const MachineFunction *MF = nullptr; 1514 const MachineRegisterInfo *MRI = nullptr; 1515 if (const MachineBasicBlock *MBB = getParent()) { 1516 MF = MBB->getParent(); 1517 if (!TM && MF) 1518 TM = &MF->getTarget(); 1519 if (MF) 1520 MRI = &MF->getRegInfo(); 1521 } 1522 1523 // Save a list of virtual registers. 1524 SmallVector<unsigned, 8> VirtRegs; 1525 1526 // Print explicitly defined operands on the left of an assignment syntax. 1527 unsigned StartOp = 0, e = getNumOperands(); 1528 for (; StartOp < e && getOperand(StartOp).isReg() && 1529 getOperand(StartOp).isDef() && 1530 !getOperand(StartOp).isImplicit(); 1531 ++StartOp) { 1532 if (StartOp != 0) OS << ", "; 1533 getOperand(StartOp).print(OS, TM); 1534 unsigned Reg = getOperand(StartOp).getReg(); 1535 if (TargetRegisterInfo::isVirtualRegister(Reg)) 1536 VirtRegs.push_back(Reg); 1537 } 1538 1539 if (StartOp != 0) 1540 OS << " = "; 1541 1542 // Print the opcode name. 1543 if (TM && TM->getSubtargetImpl()->getInstrInfo()) 1544 OS << TM->getSubtargetImpl()->getInstrInfo()->getName(getOpcode()); 1545 else 1546 OS << "UNKNOWN"; 1547 1548 if (SkipOpers) 1549 return; 1550 1551 // Print the rest of the operands. 1552 bool OmittedAnyCallClobbers = false; 1553 bool FirstOp = true; 1554 unsigned AsmDescOp = ~0u; 1555 unsigned AsmOpCount = 0; 1556 1557 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) { 1558 // Print asm string. 1559 OS << " "; 1560 getOperand(InlineAsm::MIOp_AsmString).print(OS, TM); 1561 1562 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack 1563 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 1564 if (ExtraInfo & InlineAsm::Extra_HasSideEffects) 1565 OS << " [sideeffect]"; 1566 if (ExtraInfo & InlineAsm::Extra_MayLoad) 1567 OS << " [mayload]"; 1568 if (ExtraInfo & InlineAsm::Extra_MayStore) 1569 OS << " [maystore]"; 1570 if (ExtraInfo & InlineAsm::Extra_IsAlignStack) 1571 OS << " [alignstack]"; 1572 if (getInlineAsmDialect() == InlineAsm::AD_ATT) 1573 OS << " [attdialect]"; 1574 if (getInlineAsmDialect() == InlineAsm::AD_Intel) 1575 OS << " [inteldialect]"; 1576 1577 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand; 1578 FirstOp = false; 1579 } 1580 1581 1582 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 1583 const MachineOperand &MO = getOperand(i); 1584 1585 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) 1586 VirtRegs.push_back(MO.getReg()); 1587 1588 // Omit call-clobbered registers which aren't used anywhere. This makes 1589 // call instructions much less noisy on targets where calls clobber lots 1590 // of registers. Don't rely on MO.isDead() because we may be called before 1591 // LiveVariables is run, or we may be looking at a non-allocatable reg. 1592 if (MF && isCall() && 1593 MO.isReg() && MO.isImplicit() && MO.isDef()) { 1594 unsigned Reg = MO.getReg(); 1595 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 1596 const MachineRegisterInfo &MRI = MF->getRegInfo(); 1597 if (MRI.use_empty(Reg)) { 1598 bool HasAliasLive = false; 1599 for (MCRegAliasIterator AI( 1600 Reg, TM->getSubtargetImpl()->getRegisterInfo(), true); 1601 AI.isValid(); ++AI) { 1602 unsigned AliasReg = *AI; 1603 if (!MRI.use_empty(AliasReg)) { 1604 HasAliasLive = true; 1605 break; 1606 } 1607 } 1608 if (!HasAliasLive) { 1609 OmittedAnyCallClobbers = true; 1610 continue; 1611 } 1612 } 1613 } 1614 } 1615 1616 if (FirstOp) FirstOp = false; else OS << ","; 1617 OS << " "; 1618 if (i < getDesc().NumOperands) { 1619 const MCOperandInfo &MCOI = getDesc().OpInfo[i]; 1620 if (MCOI.isPredicate()) 1621 OS << "pred:"; 1622 if (MCOI.isOptionalDef()) 1623 OS << "opt:"; 1624 } 1625 if (isDebugValue() && MO.isMetadata()) { 1626 // Pretty print DBG_VALUE instructions. 1627 const MDNode *MD = MO.getMetadata(); 1628 if (const MDString *MDS = dyn_cast<MDString>(MD->getOperand(2))) 1629 OS << "!\"" << MDS->getString() << '\"'; 1630 else 1631 MO.print(OS, TM); 1632 } else if (TM && (isInsertSubreg() || isRegSequence()) && MO.isImm()) { 1633 OS << TM->getSubtargetImpl()->getRegisterInfo()->getSubRegIndexName( 1634 MO.getImm()); 1635 } else if (i == AsmDescOp && MO.isImm()) { 1636 // Pretty print the inline asm operand descriptor. 1637 OS << '$' << AsmOpCount++; 1638 unsigned Flag = MO.getImm(); 1639 switch (InlineAsm::getKind(Flag)) { 1640 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break; 1641 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break; 1642 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break; 1643 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break; 1644 case InlineAsm::Kind_Imm: OS << ":[imm"; break; 1645 case InlineAsm::Kind_Mem: OS << ":[mem"; break; 1646 default: OS << ":[??" << InlineAsm::getKind(Flag); break; 1647 } 1648 1649 unsigned RCID = 0; 1650 if (InlineAsm::hasRegClassConstraint(Flag, RCID)) { 1651 if (TM) 1652 OS << ':' 1653 << TM->getSubtargetImpl() 1654 ->getRegisterInfo() 1655 ->getRegClass(RCID) 1656 ->getName(); 1657 else 1658 OS << ":RC" << RCID; 1659 } 1660 1661 unsigned TiedTo = 0; 1662 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo)) 1663 OS << " tiedto:$" << TiedTo; 1664 1665 OS << ']'; 1666 1667 // Compute the index of the next operand descriptor. 1668 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag); 1669 } else 1670 MO.print(OS, TM); 1671 } 1672 1673 // Briefly indicate whether any call clobbers were omitted. 1674 if (OmittedAnyCallClobbers) { 1675 if (!FirstOp) OS << ","; 1676 OS << " ..."; 1677 } 1678 1679 bool HaveSemi = false; 1680 const unsigned PrintableFlags = FrameSetup; 1681 if (Flags & PrintableFlags) { 1682 if (!HaveSemi) OS << ";"; HaveSemi = true; 1683 OS << " flags: "; 1684 1685 if (Flags & FrameSetup) 1686 OS << "FrameSetup"; 1687 } 1688 1689 if (!memoperands_empty()) { 1690 if (!HaveSemi) OS << ";"; HaveSemi = true; 1691 1692 OS << " mem:"; 1693 for (mmo_iterator i = memoperands_begin(), e = memoperands_end(); 1694 i != e; ++i) { 1695 OS << **i; 1696 if (std::next(i) != e) 1697 OS << " "; 1698 } 1699 } 1700 1701 // Print the regclass of any virtual registers encountered. 1702 if (MRI && !VirtRegs.empty()) { 1703 if (!HaveSemi) OS << ";"; HaveSemi = true; 1704 for (unsigned i = 0; i != VirtRegs.size(); ++i) { 1705 const TargetRegisterClass *RC = MRI->getRegClass(VirtRegs[i]); 1706 OS << " " << RC->getName() << ':' << PrintReg(VirtRegs[i]); 1707 for (unsigned j = i+1; j != VirtRegs.size();) { 1708 if (MRI->getRegClass(VirtRegs[j]) != RC) { 1709 ++j; 1710 continue; 1711 } 1712 if (VirtRegs[i] != VirtRegs[j]) 1713 OS << "," << PrintReg(VirtRegs[j]); 1714 VirtRegs.erase(VirtRegs.begin()+j); 1715 } 1716 } 1717 } 1718 1719 // Print debug location information. 1720 if (isDebugValue() && getOperand(e - 1).isMetadata()) { 1721 if (!HaveSemi) OS << ";"; 1722 DIVariable DV(getOperand(e - 1).getMetadata()); 1723 OS << " line no:" << DV.getLineNumber(); 1724 if (MDNode *InlinedAt = DV.getInlinedAt()) { 1725 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt); 1726 if (!InlinedAtDL.isUnknown() && MF) { 1727 OS << " inlined @[ "; 1728 printDebugLoc(InlinedAtDL, MF, OS); 1729 OS << " ]"; 1730 } 1731 } 1732 } else if (!debugLoc.isUnknown() && MF) { 1733 if (!HaveSemi) OS << ";"; 1734 OS << " dbg:"; 1735 printDebugLoc(debugLoc, MF, OS); 1736 } 1737 1738 OS << '\n'; 1739 } 1740 1741 bool MachineInstr::addRegisterKilled(unsigned IncomingReg, 1742 const TargetRegisterInfo *RegInfo, 1743 bool AddIfNotFound) { 1744 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 1745 bool hasAliases = isPhysReg && 1746 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid(); 1747 bool Found = false; 1748 SmallVector<unsigned,4> DeadOps; 1749 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1750 MachineOperand &MO = getOperand(i); 1751 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) 1752 continue; 1753 unsigned Reg = MO.getReg(); 1754 if (!Reg) 1755 continue; 1756 1757 if (Reg == IncomingReg) { 1758 if (!Found) { 1759 if (MO.isKill()) 1760 // The register is already marked kill. 1761 return true; 1762 if (isPhysReg && isRegTiedToDefOperand(i)) 1763 // Two-address uses of physregs must not be marked kill. 1764 return true; 1765 MO.setIsKill(); 1766 Found = true; 1767 } 1768 } else if (hasAliases && MO.isKill() && 1769 TargetRegisterInfo::isPhysicalRegister(Reg)) { 1770 // A super-register kill already exists. 1771 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 1772 return true; 1773 if (RegInfo->isSubRegister(IncomingReg, Reg)) 1774 DeadOps.push_back(i); 1775 } 1776 } 1777 1778 // Trim unneeded kill operands. 1779 while (!DeadOps.empty()) { 1780 unsigned OpIdx = DeadOps.back(); 1781 if (getOperand(OpIdx).isImplicit()) 1782 RemoveOperand(OpIdx); 1783 else 1784 getOperand(OpIdx).setIsKill(false); 1785 DeadOps.pop_back(); 1786 } 1787 1788 // If not found, this means an alias of one of the operands is killed. Add a 1789 // new implicit operand if required. 1790 if (!Found && AddIfNotFound) { 1791 addOperand(MachineOperand::CreateReg(IncomingReg, 1792 false /*IsDef*/, 1793 true /*IsImp*/, 1794 true /*IsKill*/)); 1795 return true; 1796 } 1797 return Found; 1798 } 1799 1800 void MachineInstr::clearRegisterKills(unsigned Reg, 1801 const TargetRegisterInfo *RegInfo) { 1802 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) 1803 RegInfo = nullptr; 1804 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1805 MachineOperand &MO = getOperand(i); 1806 if (!MO.isReg() || !MO.isUse() || !MO.isKill()) 1807 continue; 1808 unsigned OpReg = MO.getReg(); 1809 if (OpReg == Reg || (RegInfo && RegInfo->isSuperRegister(Reg, OpReg))) 1810 MO.setIsKill(false); 1811 } 1812 } 1813 1814 bool MachineInstr::addRegisterDead(unsigned Reg, 1815 const TargetRegisterInfo *RegInfo, 1816 bool AddIfNotFound) { 1817 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg); 1818 bool hasAliases = isPhysReg && 1819 MCRegAliasIterator(Reg, RegInfo, false).isValid(); 1820 bool Found = false; 1821 SmallVector<unsigned,4> DeadOps; 1822 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1823 MachineOperand &MO = getOperand(i); 1824 if (!MO.isReg() || !MO.isDef()) 1825 continue; 1826 unsigned MOReg = MO.getReg(); 1827 if (!MOReg) 1828 continue; 1829 1830 if (MOReg == Reg) { 1831 MO.setIsDead(); 1832 Found = true; 1833 } else if (hasAliases && MO.isDead() && 1834 TargetRegisterInfo::isPhysicalRegister(MOReg)) { 1835 // There exists a super-register that's marked dead. 1836 if (RegInfo->isSuperRegister(Reg, MOReg)) 1837 return true; 1838 if (RegInfo->isSubRegister(Reg, MOReg)) 1839 DeadOps.push_back(i); 1840 } 1841 } 1842 1843 // Trim unneeded dead operands. 1844 while (!DeadOps.empty()) { 1845 unsigned OpIdx = DeadOps.back(); 1846 if (getOperand(OpIdx).isImplicit()) 1847 RemoveOperand(OpIdx); 1848 else 1849 getOperand(OpIdx).setIsDead(false); 1850 DeadOps.pop_back(); 1851 } 1852 1853 // If not found, this means an alias of one of the operands is dead. Add a 1854 // new implicit operand if required. 1855 if (Found || !AddIfNotFound) 1856 return Found; 1857 1858 addOperand(MachineOperand::CreateReg(Reg, 1859 true /*IsDef*/, 1860 true /*IsImp*/, 1861 false /*IsKill*/, 1862 true /*IsDead*/)); 1863 return true; 1864 } 1865 1866 void MachineInstr::addRegisterDefined(unsigned Reg, 1867 const TargetRegisterInfo *RegInfo) { 1868 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 1869 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo); 1870 if (MO) 1871 return; 1872 } else { 1873 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1874 const MachineOperand &MO = getOperand(i); 1875 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() && 1876 MO.getSubReg() == 0) 1877 return; 1878 } 1879 } 1880 addOperand(MachineOperand::CreateReg(Reg, 1881 true /*IsDef*/, 1882 true /*IsImp*/)); 1883 } 1884 1885 void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs, 1886 const TargetRegisterInfo &TRI) { 1887 bool HasRegMask = false; 1888 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1889 MachineOperand &MO = getOperand(i); 1890 if (MO.isRegMask()) { 1891 HasRegMask = true; 1892 continue; 1893 } 1894 if (!MO.isReg() || !MO.isDef()) continue; 1895 unsigned Reg = MO.getReg(); 1896 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue; 1897 bool Dead = true; 1898 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end(); 1899 I != E; ++I) 1900 if (TRI.regsOverlap(*I, Reg)) { 1901 Dead = false; 1902 break; 1903 } 1904 // If there are no uses, including partial uses, the def is dead. 1905 if (Dead) MO.setIsDead(); 1906 } 1907 1908 // This is a call with a register mask operand. 1909 // Mask clobbers are always dead, so add defs for the non-dead defines. 1910 if (HasRegMask) 1911 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end(); 1912 I != E; ++I) 1913 addRegisterDefined(*I, &TRI); 1914 } 1915 1916 unsigned 1917 MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) { 1918 // Build up a buffer of hash code components. 1919 SmallVector<size_t, 8> HashComponents; 1920 HashComponents.reserve(MI->getNumOperands() + 1); 1921 HashComponents.push_back(MI->getOpcode()); 1922 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 1923 const MachineOperand &MO = MI->getOperand(i); 1924 if (MO.isReg() && MO.isDef() && 1925 TargetRegisterInfo::isVirtualRegister(MO.getReg())) 1926 continue; // Skip virtual register defs. 1927 1928 HashComponents.push_back(hash_value(MO)); 1929 } 1930 return hash_combine_range(HashComponents.begin(), HashComponents.end()); 1931 } 1932 1933 void MachineInstr::emitError(StringRef Msg) const { 1934 // Find the source location cookie. 1935 unsigned LocCookie = 0; 1936 const MDNode *LocMD = nullptr; 1937 for (unsigned i = getNumOperands(); i != 0; --i) { 1938 if (getOperand(i-1).isMetadata() && 1939 (LocMD = getOperand(i-1).getMetadata()) && 1940 LocMD->getNumOperands() != 0) { 1941 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) { 1942 LocCookie = CI->getZExtValue(); 1943 break; 1944 } 1945 } 1946 } 1947 1948 if (const MachineBasicBlock *MBB = getParent()) 1949 if (const MachineFunction *MF = MBB->getParent()) 1950 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg); 1951 report_fatal_error(Msg); 1952 } 1953