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