1 //===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Methods common to all machine instructions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/MachineInstr.h" 15 #include "llvm/Constants.h" 16 #include "llvm/Function.h" 17 #include "llvm/InlineAsm.h" 18 #include "llvm/Value.h" 19 #include "llvm/Assembly/Writer.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineMemOperand.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/PseudoSourceValue.h" 24 #include "llvm/Target/TargetMachine.h" 25 #include "llvm/Target/TargetInstrInfo.h" 26 #include "llvm/Target/TargetInstrDesc.h" 27 #include "llvm/Target/TargetRegisterInfo.h" 28 #include "llvm/Analysis/AliasAnalysis.h" 29 #include "llvm/Analysis/DebugInfo.h" 30 #include "llvm/Support/ErrorHandling.h" 31 #include "llvm/Support/LeakDetector.h" 32 #include "llvm/Support/MathExtras.h" 33 #include "llvm/Support/raw_ostream.h" 34 #include "llvm/ADT/FoldingSet.h" 35 using namespace llvm; 36 37 //===----------------------------------------------------------------------===// 38 // MachineOperand Implementation 39 //===----------------------------------------------------------------------===// 40 41 /// AddRegOperandToRegInfo - Add this register operand to the specified 42 /// MachineRegisterInfo. If it is null, then the next/prev fields should be 43 /// explicitly nulled out. 44 void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { 45 assert(isReg() && "Can only add reg operand to use lists"); 46 47 // If the reginfo pointer is null, just explicitly null out or next/prev 48 // pointers, to ensure they are not garbage. 49 if (RegInfo == 0) { 50 Contents.Reg.Prev = 0; 51 Contents.Reg.Next = 0; 52 return; 53 } 54 55 // Otherwise, add this operand to the head of the registers use/def list. 56 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); 57 58 // For SSA values, we prefer to keep the definition at the start of the list. 59 // we do this by skipping over the definition if it is at the head of the 60 // list. 61 if (*Head && (*Head)->isDef()) 62 Head = &(*Head)->Contents.Reg.Next; 63 64 Contents.Reg.Next = *Head; 65 if (Contents.Reg.Next) { 66 assert(getReg() == Contents.Reg.Next->getReg() && 67 "Different regs on the same list!"); 68 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; 69 } 70 71 Contents.Reg.Prev = Head; 72 *Head = this; 73 } 74 75 /// RemoveRegOperandFromRegInfo - Remove this register operand from the 76 /// MachineRegisterInfo it is linked with. 77 void MachineOperand::RemoveRegOperandFromRegInfo() { 78 assert(isOnRegUseList() && "Reg operand is not on a use list"); 79 // Unlink this from the doubly linked list of operands. 80 MachineOperand *NextOp = Contents.Reg.Next; 81 *Contents.Reg.Prev = NextOp; 82 if (NextOp) { 83 assert(NextOp->getReg() == getReg() && "Corrupt reg use/def chain!"); 84 NextOp->Contents.Reg.Prev = Contents.Reg.Prev; 85 } 86 Contents.Reg.Prev = 0; 87 Contents.Reg.Next = 0; 88 } 89 90 void MachineOperand::setReg(unsigned Reg) { 91 if (getReg() == Reg) return; // No change. 92 93 // Otherwise, we have to change the register. If this operand is embedded 94 // into a machine function, we need to update the old and new register's 95 // use/def lists. 96 if (MachineInstr *MI = getParent()) 97 if (MachineBasicBlock *MBB = MI->getParent()) 98 if (MachineFunction *MF = MBB->getParent()) { 99 RemoveRegOperandFromRegInfo(); 100 Contents.Reg.RegNo = Reg; 101 AddRegOperandToRegInfo(&MF->getRegInfo()); 102 return; 103 } 104 105 // Otherwise, just change the register, no problem. :) 106 Contents.Reg.RegNo = Reg; 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 // If this operand is currently a register operand, and if this is in a 114 // function, deregister the operand from the register's use/def list. 115 if (isReg() && getParent() && getParent()->getParent() && 116 getParent()->getParent()->getParent()) 117 RemoveRegOperandFromRegInfo(); 118 119 OpKind = MO_Immediate; 120 Contents.ImmVal = ImmVal; 121 } 122 123 /// ChangeToRegister - Replace this operand with a new register operand of 124 /// the specified value. If an operand is known to be an register already, 125 /// the setReg method should be used. 126 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 127 bool isKill, bool isDead, bool isUndef) { 128 // If this operand is already a register operand, use setReg to update the 129 // register's use/def lists. 130 if (isReg()) { 131 assert(!isEarlyClobber()); 132 setReg(Reg); 133 } else { 134 // Otherwise, change this to a register and set the reg#. 135 OpKind = MO_Register; 136 Contents.Reg.RegNo = Reg; 137 138 // If this operand is embedded in a function, add the operand to the 139 // register's use/def list. 140 if (MachineInstr *MI = getParent()) 141 if (MachineBasicBlock *MBB = MI->getParent()) 142 if (MachineFunction *MF = MBB->getParent()) 143 AddRegOperandToRegInfo(&MF->getRegInfo()); 144 } 145 146 IsDef = isDef; 147 IsImp = isImp; 148 IsKill = isKill; 149 IsDead = isDead; 150 IsUndef = isUndef; 151 IsEarlyClobber = false; 152 SubReg = 0; 153 } 154 155 /// isIdenticalTo - Return true if this operand is identical to the specified 156 /// operand. 157 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 158 if (getType() != Other.getType() || 159 getTargetFlags() != Other.getTargetFlags()) 160 return false; 161 162 switch (getType()) { 163 default: llvm_unreachable("Unrecognized operand type"); 164 case MachineOperand::MO_Register: 165 return getReg() == Other.getReg() && isDef() == Other.isDef() && 166 getSubReg() == Other.getSubReg(); 167 case MachineOperand::MO_Immediate: 168 return getImm() == Other.getImm(); 169 case MachineOperand::MO_FPImmediate: 170 return getFPImm() == Other.getFPImm(); 171 case MachineOperand::MO_MachineBasicBlock: 172 return getMBB() == Other.getMBB(); 173 case MachineOperand::MO_FrameIndex: 174 return getIndex() == Other.getIndex(); 175 case MachineOperand::MO_ConstantPoolIndex: 176 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 177 case MachineOperand::MO_JumpTableIndex: 178 return getIndex() == Other.getIndex(); 179 case MachineOperand::MO_GlobalAddress: 180 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 181 case MachineOperand::MO_ExternalSymbol: 182 return !strcmp(getSymbolName(), Other.getSymbolName()) && 183 getOffset() == Other.getOffset(); 184 case MachineOperand::MO_BlockAddress: 185 return getBlockAddress() == Other.getBlockAddress(); 186 } 187 } 188 189 /// print - Print the specified machine operand. 190 /// 191 void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const { 192 switch (getType()) { 193 case MachineOperand::MO_Register: 194 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) { 195 OS << "%reg" << getReg(); 196 } else { 197 // If the instruction is embedded into a basic block, we can find the 198 // target info for the instruction. 199 if (TM == 0) 200 if (const MachineInstr *MI = getParent()) 201 if (const MachineBasicBlock *MBB = MI->getParent()) 202 if (const MachineFunction *MF = MBB->getParent()) 203 TM = &MF->getTarget(); 204 205 if (TM) 206 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; 207 else 208 OS << "%physreg" << getReg(); 209 } 210 211 if (getSubReg() != 0) 212 OS << ':' << getSubReg(); 213 214 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || 215 isEarlyClobber()) { 216 OS << '<'; 217 bool NeedComma = false; 218 if (isDef()) { 219 if (NeedComma) OS << ','; 220 if (isEarlyClobber()) 221 OS << "earlyclobber,"; 222 if (isImplicit()) 223 OS << "imp-"; 224 OS << "def"; 225 NeedComma = true; 226 } else if (isImplicit()) { 227 OS << "imp-use"; 228 NeedComma = true; 229 } 230 231 if (isKill() || isDead() || isUndef()) { 232 if (NeedComma) OS << ','; 233 if (isKill()) OS << "kill"; 234 if (isDead()) OS << "dead"; 235 if (isUndef()) { 236 if (isKill() || isDead()) 237 OS << ','; 238 OS << "undef"; 239 } 240 } 241 OS << '>'; 242 } 243 break; 244 case MachineOperand::MO_Immediate: 245 OS << getImm(); 246 break; 247 case MachineOperand::MO_FPImmediate: 248 if (getFPImm()->getType()->isFloatTy()) 249 OS << getFPImm()->getValueAPF().convertToFloat(); 250 else 251 OS << getFPImm()->getValueAPF().convertToDouble(); 252 break; 253 case MachineOperand::MO_MachineBasicBlock: 254 OS << "<BB#" << getMBB()->getNumber() << ">"; 255 break; 256 case MachineOperand::MO_FrameIndex: 257 OS << "<fi#" << getIndex() << '>'; 258 break; 259 case MachineOperand::MO_ConstantPoolIndex: 260 OS << "<cp#" << getIndex(); 261 if (getOffset()) OS << "+" << getOffset(); 262 OS << '>'; 263 break; 264 case MachineOperand::MO_JumpTableIndex: 265 OS << "<jt#" << getIndex() << '>'; 266 break; 267 case MachineOperand::MO_GlobalAddress: 268 OS << "<ga:"; 269 WriteAsOperand(OS, getGlobal(), /*PrintType=*/false); 270 if (getOffset()) OS << "+" << getOffset(); 271 OS << '>'; 272 break; 273 case MachineOperand::MO_ExternalSymbol: 274 OS << "<es:" << getSymbolName(); 275 if (getOffset()) OS << "+" << getOffset(); 276 OS << '>'; 277 break; 278 case MachineOperand::MO_BlockAddress: 279 OS << "<"; 280 WriteAsOperand(OS, getBlockAddress(), /*PrintType=*/false); 281 OS << '>'; 282 break; 283 default: 284 llvm_unreachable("Unrecognized operand type"); 285 } 286 287 if (unsigned TF = getTargetFlags()) 288 OS << "[TF=" << TF << ']'; 289 } 290 291 //===----------------------------------------------------------------------===// 292 // MachineMemOperand Implementation 293 //===----------------------------------------------------------------------===// 294 295 MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f, 296 int64_t o, uint64_t s, unsigned int a) 297 : Offset(o), Size(s), V(v), 298 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) { 299 assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); 300 assert((isLoad() || isStore()) && "Not a load/store!"); 301 } 302 303 /// Profile - Gather unique data for the object. 304 /// 305 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 306 ID.AddInteger(Offset); 307 ID.AddInteger(Size); 308 ID.AddPointer(V); 309 ID.AddInteger(Flags); 310 } 311 312 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { 313 // The Value and Offset may differ due to CSE. But the flags and size 314 // should be the same. 315 assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); 316 assert(MMO->getSize() == getSize() && "Size mismatch!"); 317 318 if (MMO->getBaseAlignment() >= getBaseAlignment()) { 319 // Update the alignment value. 320 Flags = (Flags & 7) | ((Log2_32(MMO->getBaseAlignment()) + 1) << 3); 321 // Also update the base and offset, because the new alignment may 322 // not be applicable with the old ones. 323 V = MMO->getValue(); 324 Offset = MMO->getOffset(); 325 } 326 } 327 328 /// getAlignment - Return the minimum known alignment in bytes of the 329 /// actual memory reference. 330 uint64_t MachineMemOperand::getAlignment() const { 331 return MinAlign(getBaseAlignment(), getOffset()); 332 } 333 334 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) { 335 assert((MMO.isLoad() || MMO.isStore()) && 336 "SV has to be a load, store or both."); 337 338 if (MMO.isVolatile()) 339 OS << "Volatile "; 340 341 if (MMO.isLoad()) 342 OS << "LD"; 343 if (MMO.isStore()) 344 OS << "ST"; 345 OS << MMO.getSize(); 346 347 // Print the address information. 348 OS << "["; 349 if (!MMO.getValue()) 350 OS << "<unknown>"; 351 else 352 WriteAsOperand(OS, MMO.getValue(), /*PrintType=*/false); 353 354 // If the alignment of the memory reference itself differs from the alignment 355 // of the base pointer, print the base alignment explicitly, next to the base 356 // pointer. 357 if (MMO.getBaseAlignment() != MMO.getAlignment()) 358 OS << "(align=" << MMO.getBaseAlignment() << ")"; 359 360 if (MMO.getOffset() != 0) 361 OS << "+" << MMO.getOffset(); 362 OS << "]"; 363 364 // Print the alignment of the reference. 365 if (MMO.getBaseAlignment() != MMO.getAlignment() || 366 MMO.getBaseAlignment() != MMO.getSize()) 367 OS << "(align=" << MMO.getAlignment() << ")"; 368 369 return OS; 370 } 371 372 //===----------------------------------------------------------------------===// 373 // MachineInstr Implementation 374 //===----------------------------------------------------------------------===// 375 376 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with 377 /// TID NULL and no operands. 378 MachineInstr::MachineInstr() 379 : TID(0), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), 380 Parent(0), debugLoc(DebugLoc::getUnknownLoc()) { 381 // Make sure that we get added to a machine basicblock 382 LeakDetector::addGarbageObject(this); 383 } 384 385 void MachineInstr::addImplicitDefUseOperands() { 386 if (TID->ImplicitDefs) 387 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) 388 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); 389 if (TID->ImplicitUses) 390 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) 391 addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); 392 } 393 394 /// MachineInstr ctor - This constructor create a MachineInstr and add the 395 /// implicit operands. It reserves space for number of operands specified by 396 /// TargetInstrDesc or the numOperands if it is not zero. (for 397 /// instructions with variable number of operands). 398 MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp) 399 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0), 400 debugLoc(DebugLoc::getUnknownLoc()) { 401 if (!NoImp && TID->getImplicitDefs()) 402 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 403 NumImplicitOps++; 404 if (!NoImp && TID->getImplicitUses()) 405 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 406 NumImplicitOps++; 407 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 408 if (!NoImp) 409 addImplicitDefUseOperands(); 410 // Make sure that we get added to a machine basicblock 411 LeakDetector::addGarbageObject(this); 412 } 413 414 /// MachineInstr ctor - As above, but with a DebugLoc. 415 MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl, 416 bool NoImp) 417 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), 418 Parent(0), debugLoc(dl) { 419 if (!NoImp && TID->getImplicitDefs()) 420 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 421 NumImplicitOps++; 422 if (!NoImp && TID->getImplicitUses()) 423 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 424 NumImplicitOps++; 425 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 426 if (!NoImp) 427 addImplicitDefUseOperands(); 428 // Make sure that we get added to a machine basicblock 429 LeakDetector::addGarbageObject(this); 430 } 431 432 /// MachineInstr ctor - Work exactly the same as the ctor two above, except 433 /// that the MachineInstr is created and added to the end of the specified 434 /// basic block. 435 /// 436 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid) 437 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), Parent(0), 438 debugLoc(DebugLoc::getUnknownLoc()) { 439 assert(MBB && "Cannot use inserting ctor with null basic block!"); 440 if (TID->ImplicitDefs) 441 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 442 NumImplicitOps++; 443 if (TID->ImplicitUses) 444 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 445 NumImplicitOps++; 446 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 447 addImplicitDefUseOperands(); 448 // Make sure that we get added to a machine basicblock 449 LeakDetector::addGarbageObject(this); 450 MBB->push_back(this); // Add instruction to end of basic block! 451 } 452 453 /// MachineInstr ctor - As above, but with a DebugLoc. 454 /// 455 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl, 456 const TargetInstrDesc &tid) 457 : TID(&tid), NumImplicitOps(0), MemRefs(0), MemRefsEnd(0), 458 Parent(0), debugLoc(dl) { 459 assert(MBB && "Cannot use inserting ctor with null basic block!"); 460 if (TID->ImplicitDefs) 461 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 462 NumImplicitOps++; 463 if (TID->ImplicitUses) 464 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 465 NumImplicitOps++; 466 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 467 addImplicitDefUseOperands(); 468 // Make sure that we get added to a machine basicblock 469 LeakDetector::addGarbageObject(this); 470 MBB->push_back(this); // Add instruction to end of basic block! 471 } 472 473 /// MachineInstr ctor - Copies MachineInstr arg exactly 474 /// 475 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) 476 : TID(&MI.getDesc()), NumImplicitOps(0), 477 MemRefs(MI.MemRefs), MemRefsEnd(MI.MemRefsEnd), 478 Parent(0), debugLoc(MI.getDebugLoc()) { 479 Operands.reserve(MI.getNumOperands()); 480 481 // Add operands 482 for (unsigned i = 0; i != MI.getNumOperands(); ++i) 483 addOperand(MI.getOperand(i)); 484 NumImplicitOps = MI.NumImplicitOps; 485 486 // Set parent to null. 487 Parent = 0; 488 489 LeakDetector::addGarbageObject(this); 490 } 491 492 MachineInstr::~MachineInstr() { 493 LeakDetector::removeGarbageObject(this); 494 #ifndef NDEBUG 495 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 496 assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); 497 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && 498 "Reg operand def/use list corrupted"); 499 } 500 #endif 501 } 502 503 /// getRegInfo - If this instruction is embedded into a MachineFunction, 504 /// return the MachineRegisterInfo object for the current function, otherwise 505 /// return null. 506 MachineRegisterInfo *MachineInstr::getRegInfo() { 507 if (MachineBasicBlock *MBB = getParent()) 508 return &MBB->getParent()->getRegInfo(); 509 return 0; 510 } 511 512 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in 513 /// this instruction from their respective use lists. This requires that the 514 /// operands already be on their use lists. 515 void MachineInstr::RemoveRegOperandsFromUseLists() { 516 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 517 if (Operands[i].isReg()) 518 Operands[i].RemoveRegOperandFromRegInfo(); 519 } 520 } 521 522 /// AddRegOperandsToUseLists - Add all of the register operands in 523 /// this instruction from their respective use lists. This requires that the 524 /// operands not be on their use lists yet. 525 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { 526 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 527 if (Operands[i].isReg()) 528 Operands[i].AddRegOperandToRegInfo(&RegInfo); 529 } 530 } 531 532 533 /// addOperand - Add the specified operand to the instruction. If it is an 534 /// implicit operand, it is added to the end of the operand list. If it is 535 /// an explicit operand it is added at the end of the explicit operand list 536 /// (before the first implicit operand). 537 void MachineInstr::addOperand(const MachineOperand &Op) { 538 bool isImpReg = Op.isReg() && Op.isImplicit(); 539 assert((isImpReg || !OperandsComplete()) && 540 "Trying to add an operand to a machine instr that is already done!"); 541 542 MachineRegisterInfo *RegInfo = getRegInfo(); 543 544 // If we are adding the operand to the end of the list, our job is simpler. 545 // This is true most of the time, so this is a reasonable optimization. 546 if (isImpReg || NumImplicitOps == 0) { 547 // We can only do this optimization if we know that the operand list won't 548 // reallocate. 549 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { 550 Operands.push_back(Op); 551 552 // Set the parent of the operand. 553 Operands.back().ParentMI = this; 554 555 // If the operand is a register, update the operand's use list. 556 if (Op.isReg()) 557 Operands.back().AddRegOperandToRegInfo(RegInfo); 558 return; 559 } 560 } 561 562 // Otherwise, we have to insert a real operand before any implicit ones. 563 unsigned OpNo = Operands.size()-NumImplicitOps; 564 565 // If this instruction isn't embedded into a function, then we don't need to 566 // update any operand lists. 567 if (RegInfo == 0) { 568 // Simple insertion, no reginfo update needed for other register operands. 569 Operands.insert(Operands.begin()+OpNo, Op); 570 Operands[OpNo].ParentMI = this; 571 572 // Do explicitly set the reginfo for this operand though, to ensure the 573 // next/prev fields are properly nulled out. 574 if (Operands[OpNo].isReg()) 575 Operands[OpNo].AddRegOperandToRegInfo(0); 576 577 } else if (Operands.size()+1 <= Operands.capacity()) { 578 // Otherwise, we have to remove register operands from their register use 579 // list, add the operand, then add the register operands back to their use 580 // list. This also must handle the case when the operand list reallocates 581 // to somewhere else. 582 583 // If insertion of this operand won't cause reallocation of the operand 584 // list, just remove the implicit operands, add the operand, then re-add all 585 // the rest of the operands. 586 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 587 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 588 Operands[i].RemoveRegOperandFromRegInfo(); 589 } 590 591 // Add the operand. If it is a register, add it to the reg list. 592 Operands.insert(Operands.begin()+OpNo, Op); 593 Operands[OpNo].ParentMI = this; 594 595 if (Operands[OpNo].isReg()) 596 Operands[OpNo].AddRegOperandToRegInfo(RegInfo); 597 598 // Re-add all the implicit ops. 599 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { 600 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 601 Operands[i].AddRegOperandToRegInfo(RegInfo); 602 } 603 } else { 604 // Otherwise, we will be reallocating the operand list. Remove all reg 605 // operands from their list, then readd them after the operand list is 606 // reallocated. 607 RemoveRegOperandsFromUseLists(); 608 609 Operands.insert(Operands.begin()+OpNo, Op); 610 Operands[OpNo].ParentMI = this; 611 612 // Re-add all the operands. 613 AddRegOperandsToUseLists(*RegInfo); 614 } 615 } 616 617 /// RemoveOperand - Erase an operand from an instruction, leaving it with one 618 /// fewer operand than it started with. 619 /// 620 void MachineInstr::RemoveOperand(unsigned OpNo) { 621 assert(OpNo < Operands.size() && "Invalid operand number"); 622 623 // Special case removing the last one. 624 if (OpNo == Operands.size()-1) { 625 // If needed, remove from the reg def/use list. 626 if (Operands.back().isReg() && Operands.back().isOnRegUseList()) 627 Operands.back().RemoveRegOperandFromRegInfo(); 628 629 Operands.pop_back(); 630 return; 631 } 632 633 // Otherwise, we are removing an interior operand. If we have reginfo to 634 // update, remove all operands that will be shifted down from their reg lists, 635 // move everything down, then re-add them. 636 MachineRegisterInfo *RegInfo = getRegInfo(); 637 if (RegInfo) { 638 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 639 if (Operands[i].isReg()) 640 Operands[i].RemoveRegOperandFromRegInfo(); 641 } 642 } 643 644 Operands.erase(Operands.begin()+OpNo); 645 646 if (RegInfo) { 647 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 648 if (Operands[i].isReg()) 649 Operands[i].AddRegOperandToRegInfo(RegInfo); 650 } 651 } 652 } 653 654 /// addMemOperand - Add a MachineMemOperand to the machine instruction. 655 /// This function should be used only occasionally. The setMemRefs function 656 /// is the primary method for setting up a MachineInstr's MemRefs list. 657 void MachineInstr::addMemOperand(MachineFunction &MF, 658 MachineMemOperand *MO) { 659 mmo_iterator OldMemRefs = MemRefs; 660 mmo_iterator OldMemRefsEnd = MemRefsEnd; 661 662 size_t NewNum = (MemRefsEnd - MemRefs) + 1; 663 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum); 664 mmo_iterator NewMemRefsEnd = NewMemRefs + NewNum; 665 666 std::copy(OldMemRefs, OldMemRefsEnd, NewMemRefs); 667 NewMemRefs[NewNum - 1] = MO; 668 669 MemRefs = NewMemRefs; 670 MemRefsEnd = NewMemRefsEnd; 671 } 672 673 /// removeFromParent - This method unlinks 'this' from the containing basic 674 /// block, and returns it, but does not delete it. 675 MachineInstr *MachineInstr::removeFromParent() { 676 assert(getParent() && "Not embedded in a basic block!"); 677 getParent()->remove(this); 678 return this; 679 } 680 681 682 /// eraseFromParent - This method unlinks 'this' from the containing basic 683 /// block, and deletes it. 684 void MachineInstr::eraseFromParent() { 685 assert(getParent() && "Not embedded in a basic block!"); 686 getParent()->erase(this); 687 } 688 689 690 /// OperandComplete - Return true if it's illegal to add a new operand 691 /// 692 bool MachineInstr::OperandsComplete() const { 693 unsigned short NumOperands = TID->getNumOperands(); 694 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands) 695 return true; // Broken: we have all the operands of this instruction! 696 return false; 697 } 698 699 /// getNumExplicitOperands - Returns the number of non-implicit operands. 700 /// 701 unsigned MachineInstr::getNumExplicitOperands() const { 702 unsigned NumOperands = TID->getNumOperands(); 703 if (!TID->isVariadic()) 704 return NumOperands; 705 706 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { 707 const MachineOperand &MO = getOperand(i); 708 if (!MO.isReg() || !MO.isImplicit()) 709 NumOperands++; 710 } 711 return NumOperands; 712 } 713 714 715 /// isLabel - Returns true if the MachineInstr represents a label. 716 /// 717 bool MachineInstr::isLabel() const { 718 return getOpcode() == TargetInstrInfo::DBG_LABEL || 719 getOpcode() == TargetInstrInfo::EH_LABEL || 720 getOpcode() == TargetInstrInfo::GC_LABEL; 721 } 722 723 /// isDebugLabel - Returns true if the MachineInstr represents a debug label. 724 /// 725 bool MachineInstr::isDebugLabel() const { 726 return getOpcode() == TargetInstrInfo::DBG_LABEL; 727 } 728 729 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of 730 /// the specific register or -1 if it is not found. It further tightens 731 /// the search criteria to a use that kills the register if isKill is true. 732 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, 733 const TargetRegisterInfo *TRI) const { 734 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 735 const MachineOperand &MO = getOperand(i); 736 if (!MO.isReg() || !MO.isUse()) 737 continue; 738 unsigned MOReg = MO.getReg(); 739 if (!MOReg) 740 continue; 741 if (MOReg == Reg || 742 (TRI && 743 TargetRegisterInfo::isPhysicalRegister(MOReg) && 744 TargetRegisterInfo::isPhysicalRegister(Reg) && 745 TRI->isSubRegister(MOReg, Reg))) 746 if (!isKill || MO.isKill()) 747 return i; 748 } 749 return -1; 750 } 751 752 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of 753 /// the specified register or -1 if it is not found. If isDead is true, defs 754 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it 755 /// also checks if there is a def of a super-register. 756 int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, 757 const TargetRegisterInfo *TRI) const { 758 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 759 const MachineOperand &MO = getOperand(i); 760 if (!MO.isReg() || !MO.isDef()) 761 continue; 762 unsigned MOReg = MO.getReg(); 763 if (MOReg == Reg || 764 (TRI && 765 TargetRegisterInfo::isPhysicalRegister(MOReg) && 766 TargetRegisterInfo::isPhysicalRegister(Reg) && 767 TRI->isSubRegister(MOReg, Reg))) 768 if (!isDead || MO.isDead()) 769 return i; 770 } 771 return -1; 772 } 773 774 /// findFirstPredOperandIdx() - Find the index of the first operand in the 775 /// operand list that is used to represent the predicate. It returns -1 if 776 /// none is found. 777 int MachineInstr::findFirstPredOperandIdx() const { 778 const TargetInstrDesc &TID = getDesc(); 779 if (TID.isPredicable()) { 780 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 781 if (TID.OpInfo[i].isPredicate()) 782 return i; 783 } 784 785 return -1; 786 } 787 788 /// isRegTiedToUseOperand - Given the index of a register def operand, 789 /// check if the register def is tied to a source operand, due to either 790 /// two-address elimination or inline assembly constraints. Returns the 791 /// first tied use operand index by reference is UseOpIdx is not null. 792 bool MachineInstr:: 793 isRegTiedToUseOperand(unsigned DefOpIdx, unsigned *UseOpIdx) const { 794 if (getOpcode() == TargetInstrInfo::INLINEASM) { 795 assert(DefOpIdx >= 2); 796 const MachineOperand &MO = getOperand(DefOpIdx); 797 if (!MO.isReg() || !MO.isDef() || MO.getReg() == 0) 798 return false; 799 // Determine the actual operand index that corresponds to this index. 800 unsigned DefNo = 0; 801 unsigned DefPart = 0; 802 for (unsigned i = 1, e = getNumOperands(); i < e; ) { 803 const MachineOperand &FMO = getOperand(i); 804 // After the normal asm operands there may be additional imp-def regs. 805 if (!FMO.isImm()) 806 return false; 807 // Skip over this def. 808 unsigned NumOps = InlineAsm::getNumOperandRegisters(FMO.getImm()); 809 unsigned PrevDef = i + 1; 810 i = PrevDef + NumOps; 811 if (i > DefOpIdx) { 812 DefPart = DefOpIdx - PrevDef; 813 break; 814 } 815 ++DefNo; 816 } 817 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 818 const MachineOperand &FMO = getOperand(i); 819 if (!FMO.isImm()) 820 continue; 821 if (i+1 >= e || !getOperand(i+1).isReg() || !getOperand(i+1).isUse()) 822 continue; 823 unsigned Idx; 824 if (InlineAsm::isUseOperandTiedToDef(FMO.getImm(), Idx) && 825 Idx == DefNo) { 826 if (UseOpIdx) 827 *UseOpIdx = (unsigned)i + 1 + DefPart; 828 return true; 829 } 830 } 831 return false; 832 } 833 834 assert(getOperand(DefOpIdx).isDef() && "DefOpIdx is not a def!"); 835 const TargetInstrDesc &TID = getDesc(); 836 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { 837 const MachineOperand &MO = getOperand(i); 838 if (MO.isReg() && MO.isUse() && 839 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefOpIdx) { 840 if (UseOpIdx) 841 *UseOpIdx = (unsigned)i; 842 return true; 843 } 844 } 845 return false; 846 } 847 848 /// isRegTiedToDefOperand - Return true if the operand of the specified index 849 /// is a register use and it is tied to an def operand. It also returns the def 850 /// operand index by reference. 851 bool MachineInstr:: 852 isRegTiedToDefOperand(unsigned UseOpIdx, unsigned *DefOpIdx) const { 853 if (getOpcode() == TargetInstrInfo::INLINEASM) { 854 const MachineOperand &MO = getOperand(UseOpIdx); 855 if (!MO.isReg() || !MO.isUse() || MO.getReg() == 0) 856 return false; 857 858 // Find the flag operand corresponding to UseOpIdx 859 unsigned FlagIdx, NumOps=0; 860 for (FlagIdx = 1; FlagIdx < UseOpIdx; FlagIdx += NumOps+1) { 861 const MachineOperand &UFMO = getOperand(FlagIdx); 862 // After the normal asm operands there may be additional imp-def regs. 863 if (!UFMO.isImm()) 864 return false; 865 NumOps = InlineAsm::getNumOperandRegisters(UFMO.getImm()); 866 assert(NumOps < getNumOperands() && "Invalid inline asm flag"); 867 if (UseOpIdx < FlagIdx+NumOps+1) 868 break; 869 } 870 if (FlagIdx >= UseOpIdx) 871 return false; 872 const MachineOperand &UFMO = getOperand(FlagIdx); 873 unsigned DefNo; 874 if (InlineAsm::isUseOperandTiedToDef(UFMO.getImm(), DefNo)) { 875 if (!DefOpIdx) 876 return true; 877 878 unsigned DefIdx = 1; 879 // Remember to adjust the index. First operand is asm string, then there 880 // is a flag for each. 881 while (DefNo) { 882 const MachineOperand &FMO = getOperand(DefIdx); 883 assert(FMO.isImm()); 884 // Skip over this def. 885 DefIdx += InlineAsm::getNumOperandRegisters(FMO.getImm()) + 1; 886 --DefNo; 887 } 888 *DefOpIdx = DefIdx + UseOpIdx - FlagIdx; 889 return true; 890 } 891 return false; 892 } 893 894 const TargetInstrDesc &TID = getDesc(); 895 if (UseOpIdx >= TID.getNumOperands()) 896 return false; 897 const MachineOperand &MO = getOperand(UseOpIdx); 898 if (!MO.isReg() || !MO.isUse()) 899 return false; 900 int DefIdx = TID.getOperandConstraint(UseOpIdx, TOI::TIED_TO); 901 if (DefIdx == -1) 902 return false; 903 if (DefOpIdx) 904 *DefOpIdx = (unsigned)DefIdx; 905 return true; 906 } 907 908 /// copyKillDeadInfo - Copies kill / dead operand properties from MI. 909 /// 910 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { 911 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 912 const MachineOperand &MO = MI->getOperand(i); 913 if (!MO.isReg() || (!MO.isKill() && !MO.isDead())) 914 continue; 915 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { 916 MachineOperand &MOp = getOperand(j); 917 if (!MOp.isIdenticalTo(MO)) 918 continue; 919 if (MO.isKill()) 920 MOp.setIsKill(); 921 else 922 MOp.setIsDead(); 923 break; 924 } 925 } 926 } 927 928 /// copyPredicates - Copies predicate operand(s) from MI. 929 void MachineInstr::copyPredicates(const MachineInstr *MI) { 930 const TargetInstrDesc &TID = MI->getDesc(); 931 if (!TID.isPredicable()) 932 return; 933 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 934 if (TID.OpInfo[i].isPredicate()) { 935 // Predicated operands must be last operands. 936 addOperand(MI->getOperand(i)); 937 } 938 } 939 } 940 941 /// isSafeToMove - Return true if it is safe to move this instruction. If 942 /// SawStore is set to true, it means that there is a store (or call) between 943 /// the instruction's location and its intended destination. 944 bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, 945 bool &SawStore, 946 AliasAnalysis *AA) const { 947 // Ignore stuff that we obviously can't move. 948 if (TID->mayStore() || TID->isCall()) { 949 SawStore = true; 950 return false; 951 } 952 if (TID->isTerminator() || TID->hasUnmodeledSideEffects()) 953 return false; 954 955 // See if this instruction does a load. If so, we have to guarantee that the 956 // loaded value doesn't change between the load and the its intended 957 // destination. The check for isInvariantLoad gives the targe the chance to 958 // classify the load as always returning a constant, e.g. a constant pool 959 // load. 960 if (TID->mayLoad() && !isInvariantLoad(AA)) 961 // Otherwise, this is a real load. If there is a store between the load and 962 // end of block, or if the load is volatile, we can't move it. 963 return !SawStore && !hasVolatileMemoryRef(); 964 965 return true; 966 } 967 968 /// isSafeToReMat - Return true if it's safe to rematerialize the specified 969 /// instruction which defined the specified register instead of copying it. 970 bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII, 971 unsigned DstReg, 972 AliasAnalysis *AA) const { 973 bool SawStore = false; 974 if (!TII->isTriviallyReMaterializable(this, AA) || 975 !isSafeToMove(TII, SawStore, AA)) 976 return false; 977 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 978 const MachineOperand &MO = getOperand(i); 979 if (!MO.isReg()) 980 continue; 981 // FIXME: For now, do not remat any instruction with register operands. 982 // Later on, we can loosen the restriction is the register operands have 983 // not been modified between the def and use. Note, this is different from 984 // MachineSink because the code is no longer in two-address form (at least 985 // partially). 986 if (MO.isUse()) 987 return false; 988 else if (!MO.isDead() && MO.getReg() != DstReg) 989 return false; 990 } 991 return true; 992 } 993 994 /// hasVolatileMemoryRef - Return true if this instruction may have a 995 /// volatile memory reference, or if the information describing the 996 /// memory reference is not available. Return false if it is known to 997 /// have no volatile memory references. 998 bool MachineInstr::hasVolatileMemoryRef() const { 999 // An instruction known never to access memory won't have a volatile access. 1000 if (!TID->mayStore() && 1001 !TID->mayLoad() && 1002 !TID->isCall() && 1003 !TID->hasUnmodeledSideEffects()) 1004 return false; 1005 1006 // Otherwise, if the instruction has no memory reference information, 1007 // conservatively assume it wasn't preserved. 1008 if (memoperands_empty()) 1009 return true; 1010 1011 // Check the memory reference information for volatile references. 1012 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I) 1013 if ((*I)->isVolatile()) 1014 return true; 1015 1016 return false; 1017 } 1018 1019 /// isInvariantLoad - Return true if this instruction is loading from a 1020 /// location whose value is invariant across the function. For example, 1021 /// loading a value from the constant pool or from from the argument area 1022 /// of a function if it does not change. This should only return true of 1023 /// *all* loads the instruction does are invariant (if it does multiple loads). 1024 bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const { 1025 // If the instruction doesn't load at all, it isn't an invariant load. 1026 if (!TID->mayLoad()) 1027 return false; 1028 1029 // If the instruction has lost its memoperands, conservatively assume that 1030 // it may not be an invariant load. 1031 if (memoperands_empty()) 1032 return false; 1033 1034 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo(); 1035 1036 for (mmo_iterator I = memoperands_begin(), 1037 E = memoperands_end(); I != E; ++I) { 1038 if ((*I)->isVolatile()) return false; 1039 if ((*I)->isStore()) return false; 1040 1041 if (const Value *V = (*I)->getValue()) { 1042 // A load from a constant PseudoSourceValue is invariant. 1043 if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) 1044 if (PSV->isConstant(MFI)) 1045 continue; 1046 // If we have an AliasAnalysis, ask it whether the memory is constant. 1047 if (AA && AA->pointsToConstantMemory(V)) 1048 continue; 1049 } 1050 1051 // Otherwise assume conservatively. 1052 return false; 1053 } 1054 1055 // Everything checks out. 1056 return true; 1057 } 1058 1059 void MachineInstr::dump() const { 1060 errs() << " " << *this; 1061 } 1062 1063 void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const { 1064 unsigned StartOp = 0, e = getNumOperands(); 1065 1066 // Print explicitly defined operands on the left of an assignment syntax. 1067 for (; StartOp < e && getOperand(StartOp).isReg() && 1068 getOperand(StartOp).isDef() && 1069 !getOperand(StartOp).isImplicit(); 1070 ++StartOp) { 1071 if (StartOp != 0) OS << ", "; 1072 getOperand(StartOp).print(OS, TM); 1073 } 1074 1075 if (StartOp != 0) 1076 OS << " = "; 1077 1078 // Print the opcode name. 1079 OS << getDesc().getName(); 1080 1081 // Print the rest of the operands. 1082 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 1083 if (i != StartOp) 1084 OS << ","; 1085 OS << " "; 1086 getOperand(i).print(OS, TM); 1087 } 1088 1089 bool HaveSemi = false; 1090 if (!memoperands_empty()) { 1091 if (!HaveSemi) OS << ";"; HaveSemi = true; 1092 1093 OS << " mem:"; 1094 for (mmo_iterator i = memoperands_begin(), e = memoperands_end(); 1095 i != e; ++i) { 1096 OS << **i; 1097 if (next(i) != e) 1098 OS << " "; 1099 } 1100 } 1101 1102 if (!debugLoc.isUnknown()) { 1103 if (!HaveSemi) OS << ";"; HaveSemi = true; 1104 1105 // TODO: print InlinedAtLoc information 1106 1107 const MachineFunction *MF = getParent()->getParent(); 1108 DebugLocTuple DLT = MF->getDebugLocTuple(debugLoc); 1109 DICompileUnit CU(DLT.Scope); 1110 if (!CU.isNull()) 1111 OS << " dbg:" << CU.getDirectory() << '/' << CU.getFilename() << ":" 1112 << DLT.Line << ":" << DLT.Col; 1113 } 1114 1115 OS << "\n"; 1116 } 1117 1118 bool MachineInstr::addRegisterKilled(unsigned IncomingReg, 1119 const TargetRegisterInfo *RegInfo, 1120 bool AddIfNotFound) { 1121 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 1122 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 1123 bool Found = false; 1124 SmallVector<unsigned,4> DeadOps; 1125 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1126 MachineOperand &MO = getOperand(i); 1127 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) 1128 continue; 1129 unsigned Reg = MO.getReg(); 1130 if (!Reg) 1131 continue; 1132 1133 if (Reg == IncomingReg) { 1134 if (!Found) { 1135 if (MO.isKill()) 1136 // The register is already marked kill. 1137 return true; 1138 if (isPhysReg && isRegTiedToDefOperand(i)) 1139 // Two-address uses of physregs must not be marked kill. 1140 return true; 1141 MO.setIsKill(); 1142 Found = true; 1143 } 1144 } else if (hasAliases && MO.isKill() && 1145 TargetRegisterInfo::isPhysicalRegister(Reg)) { 1146 // A super-register kill already exists. 1147 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 1148 return true; 1149 if (RegInfo->isSubRegister(IncomingReg, Reg)) 1150 DeadOps.push_back(i); 1151 } 1152 } 1153 1154 // Trim unneeded kill operands. 1155 while (!DeadOps.empty()) { 1156 unsigned OpIdx = DeadOps.back(); 1157 if (getOperand(OpIdx).isImplicit()) 1158 RemoveOperand(OpIdx); 1159 else 1160 getOperand(OpIdx).setIsKill(false); 1161 DeadOps.pop_back(); 1162 } 1163 1164 // If not found, this means an alias of one of the operands is killed. Add a 1165 // new implicit operand if required. 1166 if (!Found && AddIfNotFound) { 1167 addOperand(MachineOperand::CreateReg(IncomingReg, 1168 false /*IsDef*/, 1169 true /*IsImp*/, 1170 true /*IsKill*/)); 1171 return true; 1172 } 1173 return Found; 1174 } 1175 1176 bool MachineInstr::addRegisterDead(unsigned IncomingReg, 1177 const TargetRegisterInfo *RegInfo, 1178 bool AddIfNotFound) { 1179 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 1180 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 1181 bool Found = false; 1182 SmallVector<unsigned,4> DeadOps; 1183 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 1184 MachineOperand &MO = getOperand(i); 1185 if (!MO.isReg() || !MO.isDef()) 1186 continue; 1187 unsigned Reg = MO.getReg(); 1188 if (!Reg) 1189 continue; 1190 1191 if (Reg == IncomingReg) { 1192 if (!Found) { 1193 if (MO.isDead()) 1194 // The register is already marked dead. 1195 return true; 1196 MO.setIsDead(); 1197 Found = true; 1198 } 1199 } else if (hasAliases && MO.isDead() && 1200 TargetRegisterInfo::isPhysicalRegister(Reg)) { 1201 // There exists a super-register that's marked dead. 1202 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 1203 return true; 1204 if (RegInfo->getSubRegisters(IncomingReg) && 1205 RegInfo->getSuperRegisters(Reg) && 1206 RegInfo->isSubRegister(IncomingReg, Reg)) 1207 DeadOps.push_back(i); 1208 } 1209 } 1210 1211 // Trim unneeded dead operands. 1212 while (!DeadOps.empty()) { 1213 unsigned OpIdx = DeadOps.back(); 1214 if (getOperand(OpIdx).isImplicit()) 1215 RemoveOperand(OpIdx); 1216 else 1217 getOperand(OpIdx).setIsDead(false); 1218 DeadOps.pop_back(); 1219 } 1220 1221 // If not found, this means an alias of one of the operands is dead. Add a 1222 // new implicit operand if required. 1223 if (Found || !AddIfNotFound) 1224 return Found; 1225 1226 addOperand(MachineOperand::CreateReg(IncomingReg, 1227 true /*IsDef*/, 1228 true /*IsImp*/, 1229 false /*IsKill*/, 1230 true /*IsDead*/)); 1231 return true; 1232 } 1233