LLVM API Documentation
00001 //===-- lib/CodeGen/MachineInstr.cpp --------------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Methods common to all machine instructions. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/MachineInstr.h" 00015 #include "llvm/ADT/FoldingSet.h" 00016 #include "llvm/ADT/Hashing.h" 00017 #include "llvm/Analysis/AliasAnalysis.h" 00018 #include "llvm/CodeGen/MachineConstantPool.h" 00019 #include "llvm/CodeGen/MachineFunction.h" 00020 #include "llvm/CodeGen/MachineMemOperand.h" 00021 #include "llvm/CodeGen/MachineModuleInfo.h" 00022 #include "llvm/CodeGen/MachineRegisterInfo.h" 00023 #include "llvm/CodeGen/PseudoSourceValue.h" 00024 #include "llvm/IR/Constants.h" 00025 #include "llvm/IR/DebugInfo.h" 00026 #include "llvm/IR/Function.h" 00027 #include "llvm/IR/InlineAsm.h" 00028 #include "llvm/IR/LLVMContext.h" 00029 #include "llvm/IR/Metadata.h" 00030 #include "llvm/IR/Module.h" 00031 #include "llvm/IR/Type.h" 00032 #include "llvm/IR/Value.h" 00033 #include "llvm/MC/MCInstrDesc.h" 00034 #include "llvm/MC/MCSymbol.h" 00035 #include "llvm/Support/Debug.h" 00036 #include "llvm/Support/ErrorHandling.h" 00037 #include "llvm/Support/MathExtras.h" 00038 #include "llvm/Support/raw_ostream.h" 00039 #include "llvm/Target/TargetInstrInfo.h" 00040 #include "llvm/Target/TargetMachine.h" 00041 #include "llvm/Target/TargetRegisterInfo.h" 00042 #include "llvm/Target/TargetSubtargetInfo.h" 00043 using namespace llvm; 00044 00045 //===----------------------------------------------------------------------===// 00046 // MachineOperand Implementation 00047 //===----------------------------------------------------------------------===// 00048 00049 void MachineOperand::setReg(unsigned Reg) { 00050 if (getReg() == Reg) return; // No change. 00051 00052 // Otherwise, we have to change the register. If this operand is embedded 00053 // into a machine function, we need to update the old and new register's 00054 // use/def lists. 00055 if (MachineInstr *MI = getParent()) 00056 if (MachineBasicBlock *MBB = MI->getParent()) 00057 if (MachineFunction *MF = MBB->getParent()) { 00058 MachineRegisterInfo &MRI = MF->getRegInfo(); 00059 MRI.removeRegOperandFromUseList(this); 00060 SmallContents.RegNo = Reg; 00061 MRI.addRegOperandToUseList(this); 00062 return; 00063 } 00064 00065 // Otherwise, just change the register, no problem. :) 00066 SmallContents.RegNo = Reg; 00067 } 00068 00069 void MachineOperand::substVirtReg(unsigned Reg, unsigned SubIdx, 00070 const TargetRegisterInfo &TRI) { 00071 assert(TargetRegisterInfo::isVirtualRegister(Reg)); 00072 if (SubIdx && getSubReg()) 00073 SubIdx = TRI.composeSubRegIndices(SubIdx, getSubReg()); 00074 setReg(Reg); 00075 if (SubIdx) 00076 setSubReg(SubIdx); 00077 } 00078 00079 void MachineOperand::substPhysReg(unsigned Reg, const TargetRegisterInfo &TRI) { 00080 assert(TargetRegisterInfo::isPhysicalRegister(Reg)); 00081 if (getSubReg()) { 00082 Reg = TRI.getSubReg(Reg, getSubReg()); 00083 // Note that getSubReg() may return 0 if the sub-register doesn't exist. 00084 // That won't happen in legal code. 00085 setSubReg(0); 00086 } 00087 setReg(Reg); 00088 } 00089 00090 /// Change a def to a use, or a use to a def. 00091 void MachineOperand::setIsDef(bool Val) { 00092 assert(isReg() && "Wrong MachineOperand accessor"); 00093 assert((!Val || !isDebug()) && "Marking a debug operation as def"); 00094 if (IsDef == Val) 00095 return; 00096 // MRI may keep uses and defs in different list positions. 00097 if (MachineInstr *MI = getParent()) 00098 if (MachineBasicBlock *MBB = MI->getParent()) 00099 if (MachineFunction *MF = MBB->getParent()) { 00100 MachineRegisterInfo &MRI = MF->getRegInfo(); 00101 MRI.removeRegOperandFromUseList(this); 00102 IsDef = Val; 00103 MRI.addRegOperandToUseList(this); 00104 return; 00105 } 00106 IsDef = Val; 00107 } 00108 00109 /// ChangeToImmediate - Replace this operand with a new immediate operand of 00110 /// the specified value. If an operand is known to be an immediate already, 00111 /// the setImm method should be used. 00112 void MachineOperand::ChangeToImmediate(int64_t ImmVal) { 00113 assert((!isReg() || !isTied()) && "Cannot change a tied operand into an imm"); 00114 // If this operand is currently a register operand, and if this is in a 00115 // function, deregister the operand from the register's use/def list. 00116 if (isReg() && isOnRegUseList()) 00117 if (MachineInstr *MI = getParent()) 00118 if (MachineBasicBlock *MBB = MI->getParent()) 00119 if (MachineFunction *MF = MBB->getParent()) 00120 MF->getRegInfo().removeRegOperandFromUseList(this); 00121 00122 OpKind = MO_Immediate; 00123 Contents.ImmVal = ImmVal; 00124 } 00125 00126 /// ChangeToRegister - Replace this operand with a new register operand of 00127 /// the specified value. If an operand is known to be an register already, 00128 /// the setReg method should be used. 00129 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 00130 bool isKill, bool isDead, bool isUndef, 00131 bool isDebug) { 00132 MachineRegisterInfo *RegInfo = nullptr; 00133 if (MachineInstr *MI = getParent()) 00134 if (MachineBasicBlock *MBB = MI->getParent()) 00135 if (MachineFunction *MF = MBB->getParent()) 00136 RegInfo = &MF->getRegInfo(); 00137 // If this operand is already a register operand, remove it from the 00138 // register's use/def lists. 00139 bool WasReg = isReg(); 00140 if (RegInfo && WasReg) 00141 RegInfo->removeRegOperandFromUseList(this); 00142 00143 // Change this to a register and set the reg#. 00144 OpKind = MO_Register; 00145 SmallContents.RegNo = Reg; 00146 SubReg_TargetFlags = 0; 00147 IsDef = isDef; 00148 IsImp = isImp; 00149 IsKill = isKill; 00150 IsDead = isDead; 00151 IsUndef = isUndef; 00152 IsInternalRead = false; 00153 IsEarlyClobber = false; 00154 IsDebug = isDebug; 00155 // Ensure isOnRegUseList() returns false. 00156 Contents.Reg.Prev = nullptr; 00157 // Preserve the tie when the operand was already a register. 00158 if (!WasReg) 00159 TiedTo = 0; 00160 00161 // If this operand is embedded in a function, add the operand to the 00162 // register's use/def list. 00163 if (RegInfo) 00164 RegInfo->addRegOperandToUseList(this); 00165 } 00166 00167 /// isIdenticalTo - Return true if this operand is identical to the specified 00168 /// operand. Note that this should stay in sync with the hash_value overload 00169 /// below. 00170 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 00171 if (getType() != Other.getType() || 00172 getTargetFlags() != Other.getTargetFlags()) 00173 return false; 00174 00175 switch (getType()) { 00176 case MachineOperand::MO_Register: 00177 return getReg() == Other.getReg() && isDef() == Other.isDef() && 00178 getSubReg() == Other.getSubReg(); 00179 case MachineOperand::MO_Immediate: 00180 return getImm() == Other.getImm(); 00181 case MachineOperand::MO_CImmediate: 00182 return getCImm() == Other.getCImm(); 00183 case MachineOperand::MO_FPImmediate: 00184 return getFPImm() == Other.getFPImm(); 00185 case MachineOperand::MO_MachineBasicBlock: 00186 return getMBB() == Other.getMBB(); 00187 case MachineOperand::MO_FrameIndex: 00188 return getIndex() == Other.getIndex(); 00189 case MachineOperand::MO_ConstantPoolIndex: 00190 case MachineOperand::MO_TargetIndex: 00191 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 00192 case MachineOperand::MO_JumpTableIndex: 00193 return getIndex() == Other.getIndex(); 00194 case MachineOperand::MO_GlobalAddress: 00195 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 00196 case MachineOperand::MO_ExternalSymbol: 00197 return !strcmp(getSymbolName(), Other.getSymbolName()) && 00198 getOffset() == Other.getOffset(); 00199 case MachineOperand::MO_BlockAddress: 00200 return getBlockAddress() == Other.getBlockAddress() && 00201 getOffset() == Other.getOffset(); 00202 case MachineOperand::MO_RegisterMask: 00203 case MachineOperand::MO_RegisterLiveOut: 00204 return getRegMask() == Other.getRegMask(); 00205 case MachineOperand::MO_MCSymbol: 00206 return getMCSymbol() == Other.getMCSymbol(); 00207 case MachineOperand::MO_CFIIndex: 00208 return getCFIIndex() == Other.getCFIIndex(); 00209 case MachineOperand::MO_Metadata: 00210 return getMetadata() == Other.getMetadata(); 00211 } 00212 llvm_unreachable("Invalid machine operand type"); 00213 } 00214 00215 // Note: this must stay exactly in sync with isIdenticalTo above. 00216 hash_code llvm::hash_value(const MachineOperand &MO) { 00217 switch (MO.getType()) { 00218 case MachineOperand::MO_Register: 00219 // Register operands don't have target flags. 00220 return hash_combine(MO.getType(), MO.getReg(), MO.getSubReg(), MO.isDef()); 00221 case MachineOperand::MO_Immediate: 00222 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getImm()); 00223 case MachineOperand::MO_CImmediate: 00224 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCImm()); 00225 case MachineOperand::MO_FPImmediate: 00226 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getFPImm()); 00227 case MachineOperand::MO_MachineBasicBlock: 00228 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMBB()); 00229 case MachineOperand::MO_FrameIndex: 00230 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 00231 case MachineOperand::MO_ConstantPoolIndex: 00232 case MachineOperand::MO_TargetIndex: 00233 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex(), 00234 MO.getOffset()); 00235 case MachineOperand::MO_JumpTableIndex: 00236 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getIndex()); 00237 case MachineOperand::MO_ExternalSymbol: 00238 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getOffset(), 00239 MO.getSymbolName()); 00240 case MachineOperand::MO_GlobalAddress: 00241 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getGlobal(), 00242 MO.getOffset()); 00243 case MachineOperand::MO_BlockAddress: 00244 return hash_combine(MO.getType(), MO.getTargetFlags(), 00245 MO.getBlockAddress(), MO.getOffset()); 00246 case MachineOperand::MO_RegisterMask: 00247 case MachineOperand::MO_RegisterLiveOut: 00248 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getRegMask()); 00249 case MachineOperand::MO_Metadata: 00250 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMetadata()); 00251 case MachineOperand::MO_MCSymbol: 00252 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getMCSymbol()); 00253 case MachineOperand::MO_CFIIndex: 00254 return hash_combine(MO.getType(), MO.getTargetFlags(), MO.getCFIIndex()); 00255 } 00256 llvm_unreachable("Invalid machine operand type"); 00257 } 00258 00259 /// print - Print the specified machine operand. 00260 /// 00261 void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const { 00262 // If the instruction is embedded into a basic block, we can find the 00263 // target info for the instruction. 00264 if (!TM) 00265 if (const MachineInstr *MI = getParent()) 00266 if (const MachineBasicBlock *MBB = MI->getParent()) 00267 if (const MachineFunction *MF = MBB->getParent()) 00268 TM = &MF->getTarget(); 00269 const TargetRegisterInfo *TRI = 00270 TM ? TM->getSubtargetImpl()->getRegisterInfo() : nullptr; 00271 00272 switch (getType()) { 00273 case MachineOperand::MO_Register: 00274 OS << PrintReg(getReg(), TRI, getSubReg()); 00275 00276 if (isDef() || isKill() || isDead() || isImplicit() || isUndef() || 00277 isInternalRead() || isEarlyClobber() || isTied()) { 00278 OS << '<'; 00279 bool NeedComma = false; 00280 if (isDef()) { 00281 if (NeedComma) OS << ','; 00282 if (isEarlyClobber()) 00283 OS << "earlyclobber,"; 00284 if (isImplicit()) 00285 OS << "imp-"; 00286 OS << "def"; 00287 NeedComma = true; 00288 // <def,read-undef> only makes sense when getSubReg() is set. 00289 // Don't clutter the output otherwise. 00290 if (isUndef() && getSubReg()) 00291 OS << ",read-undef"; 00292 } else if (isImplicit()) { 00293 OS << "imp-use"; 00294 NeedComma = true; 00295 } 00296 00297 if (isKill()) { 00298 if (NeedComma) OS << ','; 00299 OS << "kill"; 00300 NeedComma = true; 00301 } 00302 if (isDead()) { 00303 if (NeedComma) OS << ','; 00304 OS << "dead"; 00305 NeedComma = true; 00306 } 00307 if (isUndef() && isUse()) { 00308 if (NeedComma) OS << ','; 00309 OS << "undef"; 00310 NeedComma = true; 00311 } 00312 if (isInternalRead()) { 00313 if (NeedComma) OS << ','; 00314 OS << "internal"; 00315 NeedComma = true; 00316 } 00317 if (isTied()) { 00318 if (NeedComma) OS << ','; 00319 OS << "tied"; 00320 if (TiedTo != 15) 00321 OS << unsigned(TiedTo - 1); 00322 } 00323 OS << '>'; 00324 } 00325 break; 00326 case MachineOperand::MO_Immediate: 00327 OS << getImm(); 00328 break; 00329 case MachineOperand::MO_CImmediate: 00330 getCImm()->getValue().print(OS, false); 00331 break; 00332 case MachineOperand::MO_FPImmediate: 00333 if (getFPImm()->getType()->isFloatTy()) 00334 OS << getFPImm()->getValueAPF().convertToFloat(); 00335 else 00336 OS << getFPImm()->getValueAPF().convertToDouble(); 00337 break; 00338 case MachineOperand::MO_MachineBasicBlock: 00339 OS << "<BB#" << getMBB()->getNumber() << ">"; 00340 break; 00341 case MachineOperand::MO_FrameIndex: 00342 OS << "<fi#" << getIndex() << '>'; 00343 break; 00344 case MachineOperand::MO_ConstantPoolIndex: 00345 OS << "<cp#" << getIndex(); 00346 if (getOffset()) OS << "+" << getOffset(); 00347 OS << '>'; 00348 break; 00349 case MachineOperand::MO_TargetIndex: 00350 OS << "<ti#" << getIndex(); 00351 if (getOffset()) OS << "+" << getOffset(); 00352 OS << '>'; 00353 break; 00354 case MachineOperand::MO_JumpTableIndex: 00355 OS << "<jt#" << getIndex() << '>'; 00356 break; 00357 case MachineOperand::MO_GlobalAddress: 00358 OS << "<ga:"; 00359 getGlobal()->printAsOperand(OS, /*PrintType=*/false); 00360 if (getOffset()) OS << "+" << getOffset(); 00361 OS << '>'; 00362 break; 00363 case MachineOperand::MO_ExternalSymbol: 00364 OS << "<es:" << getSymbolName(); 00365 if (getOffset()) OS << "+" << getOffset(); 00366 OS << '>'; 00367 break; 00368 case MachineOperand::MO_BlockAddress: 00369 OS << '<'; 00370 getBlockAddress()->printAsOperand(OS, /*PrintType=*/false); 00371 if (getOffset()) OS << "+" << getOffset(); 00372 OS << '>'; 00373 break; 00374 case MachineOperand::MO_RegisterMask: 00375 OS << "<regmask>"; 00376 break; 00377 case MachineOperand::MO_RegisterLiveOut: 00378 OS << "<regliveout>"; 00379 break; 00380 case MachineOperand::MO_Metadata: 00381 OS << '<'; 00382 getMetadata()->printAsOperand(OS, /*PrintType=*/false); 00383 OS << '>'; 00384 break; 00385 case MachineOperand::MO_MCSymbol: 00386 OS << "<MCSym=" << *getMCSymbol() << '>'; 00387 break; 00388 case MachineOperand::MO_CFIIndex: 00389 OS << "<call frame instruction>"; 00390 break; 00391 } 00392 00393 if (unsigned TF = getTargetFlags()) 00394 OS << "[TF=" << TF << ']'; 00395 } 00396 00397 //===----------------------------------------------------------------------===// 00398 // MachineMemOperand Implementation 00399 //===----------------------------------------------------------------------===// 00400 00401 /// getAddrSpace - Return the LLVM IR address space number that this pointer 00402 /// points into. 00403 unsigned MachinePointerInfo::getAddrSpace() const { 00404 if (V.isNull() || V.is<const PseudoSourceValue*>()) return 0; 00405 return cast<PointerType>(V.get<const Value*>()->getType())->getAddressSpace(); 00406 } 00407 00408 /// getConstantPool - Return a MachinePointerInfo record that refers to the 00409 /// constant pool. 00410 MachinePointerInfo MachinePointerInfo::getConstantPool() { 00411 return MachinePointerInfo(PseudoSourceValue::getConstantPool()); 00412 } 00413 00414 /// getFixedStack - Return a MachinePointerInfo record that refers to the 00415 /// the specified FrameIndex. 00416 MachinePointerInfo MachinePointerInfo::getFixedStack(int FI, int64_t offset) { 00417 return MachinePointerInfo(PseudoSourceValue::getFixedStack(FI), offset); 00418 } 00419 00420 MachinePointerInfo MachinePointerInfo::getJumpTable() { 00421 return MachinePointerInfo(PseudoSourceValue::getJumpTable()); 00422 } 00423 00424 MachinePointerInfo MachinePointerInfo::getGOT() { 00425 return MachinePointerInfo(PseudoSourceValue::getGOT()); 00426 } 00427 00428 MachinePointerInfo MachinePointerInfo::getStack(int64_t Offset) { 00429 return MachinePointerInfo(PseudoSourceValue::getStack(), Offset); 00430 } 00431 00432 MachineMemOperand::MachineMemOperand(MachinePointerInfo ptrinfo, unsigned f, 00433 uint64_t s, unsigned int a, 00434 const AAMDNodes &AAInfo, 00435 const MDNode *Ranges) 00436 : PtrInfo(ptrinfo), Size(s), 00437 Flags((f & ((1 << MOMaxBits) - 1)) | ((Log2_32(a) + 1) << MOMaxBits)), 00438 AAInfo(AAInfo), Ranges(Ranges) { 00439 assert((PtrInfo.V.isNull() || PtrInfo.V.is<const PseudoSourceValue*>() || 00440 isa<PointerType>(PtrInfo.V.get<const Value*>()->getType())) && 00441 "invalid pointer value"); 00442 assert(getBaseAlignment() == a && "Alignment is not a power of 2!"); 00443 assert((isLoad() || isStore()) && "Not a load/store!"); 00444 } 00445 00446 /// Profile - Gather unique data for the object. 00447 /// 00448 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 00449 ID.AddInteger(getOffset()); 00450 ID.AddInteger(Size); 00451 ID.AddPointer(getOpaqueValue()); 00452 ID.AddInteger(Flags); 00453 } 00454 00455 void MachineMemOperand::refineAlignment(const MachineMemOperand *MMO) { 00456 // The Value and Offset may differ due to CSE. But the flags and size 00457 // should be the same. 00458 assert(MMO->getFlags() == getFlags() && "Flags mismatch!"); 00459 assert(MMO->getSize() == getSize() && "Size mismatch!"); 00460 00461 if (MMO->getBaseAlignment() >= getBaseAlignment()) { 00462 // Update the alignment value. 00463 Flags = (Flags & ((1 << MOMaxBits) - 1)) | 00464 ((Log2_32(MMO->getBaseAlignment()) + 1) << MOMaxBits); 00465 // Also update the base and offset, because the new alignment may 00466 // not be applicable with the old ones. 00467 PtrInfo = MMO->PtrInfo; 00468 } 00469 } 00470 00471 /// getAlignment - Return the minimum known alignment in bytes of the 00472 /// actual memory reference. 00473 uint64_t MachineMemOperand::getAlignment() const { 00474 return MinAlign(getBaseAlignment(), getOffset()); 00475 } 00476 00477 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineMemOperand &MMO) { 00478 assert((MMO.isLoad() || MMO.isStore()) && 00479 "SV has to be a load, store or both."); 00480 00481 if (MMO.isVolatile()) 00482 OS << "Volatile "; 00483 00484 if (MMO.isLoad()) 00485 OS << "LD"; 00486 if (MMO.isStore()) 00487 OS << "ST"; 00488 OS << MMO.getSize(); 00489 00490 // Print the address information. 00491 OS << "["; 00492 if (const Value *V = MMO.getValue()) 00493 V->printAsOperand(OS, /*PrintType=*/false); 00494 else if (const PseudoSourceValue *PSV = MMO.getPseudoValue()) 00495 PSV->printCustom(OS); 00496 else 00497 OS << "<unknown>"; 00498 00499 unsigned AS = MMO.getAddrSpace(); 00500 if (AS != 0) 00501 OS << "(addrspace=" << AS << ')'; 00502 00503 // If the alignment of the memory reference itself differs from the alignment 00504 // of the base pointer, print the base alignment explicitly, next to the base 00505 // pointer. 00506 if (MMO.getBaseAlignment() != MMO.getAlignment()) 00507 OS << "(align=" << MMO.getBaseAlignment() << ")"; 00508 00509 if (MMO.getOffset() != 0) 00510 OS << "+" << MMO.getOffset(); 00511 OS << "]"; 00512 00513 // Print the alignment of the reference. 00514 if (MMO.getBaseAlignment() != MMO.getAlignment() || 00515 MMO.getBaseAlignment() != MMO.getSize()) 00516 OS << "(align=" << MMO.getAlignment() << ")"; 00517 00518 // Print TBAA info. 00519 if (const MDNode *TBAAInfo = MMO.getAAInfo().TBAA) { 00520 OS << "(tbaa="; 00521 if (TBAAInfo->getNumOperands() > 0) 00522 TBAAInfo->getOperand(0)->printAsOperand(OS, /*PrintType=*/false); 00523 else 00524 OS << "<unknown>"; 00525 OS << ")"; 00526 } 00527 00528 // Print AA scope info. 00529 if (const MDNode *ScopeInfo = MMO.getAAInfo().Scope) { 00530 OS << "(alias.scope="; 00531 if (ScopeInfo->getNumOperands() > 0) 00532 for (unsigned i = 0, ie = ScopeInfo->getNumOperands(); i != ie; ++i) { 00533 ScopeInfo->getOperand(i)->printAsOperand(OS, /*PrintType=*/false); 00534 if (i != ie-1) 00535 OS << ","; 00536 } 00537 else 00538 OS << "<unknown>"; 00539 OS << ")"; 00540 } 00541 00542 // Print AA noalias scope info. 00543 if (const MDNode *NoAliasInfo = MMO.getAAInfo().NoAlias) { 00544 OS << "(noalias="; 00545 if (NoAliasInfo->getNumOperands() > 0) 00546 for (unsigned i = 0, ie = NoAliasInfo->getNumOperands(); i != ie; ++i) { 00547 NoAliasInfo->getOperand(i)->printAsOperand(OS, /*PrintType=*/false); 00548 if (i != ie-1) 00549 OS << ","; 00550 } 00551 else 00552 OS << "<unknown>"; 00553 OS << ")"; 00554 } 00555 00556 // Print nontemporal info. 00557 if (MMO.isNonTemporal()) 00558 OS << "(nontemporal)"; 00559 00560 return OS; 00561 } 00562 00563 //===----------------------------------------------------------------------===// 00564 // MachineInstr Implementation 00565 //===----------------------------------------------------------------------===// 00566 00567 void MachineInstr::addImplicitDefUseOperands(MachineFunction &MF) { 00568 if (MCID->ImplicitDefs) 00569 for (const uint16_t *ImpDefs = MCID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 00570 addOperand(MF, MachineOperand::CreateReg(*ImpDefs, true, true)); 00571 if (MCID->ImplicitUses) 00572 for (const uint16_t *ImpUses = MCID->getImplicitUses(); *ImpUses; ++ImpUses) 00573 addOperand(MF, MachineOperand::CreateReg(*ImpUses, false, true)); 00574 } 00575 00576 /// MachineInstr ctor - This constructor creates a MachineInstr and adds the 00577 /// implicit operands. It reserves space for the number of operands specified by 00578 /// the MCInstrDesc. 00579 MachineInstr::MachineInstr(MachineFunction &MF, const MCInstrDesc &tid, 00580 const DebugLoc dl, bool NoImp) 00581 : MCID(&tid), Parent(nullptr), Operands(nullptr), NumOperands(0), 00582 Flags(0), AsmPrinterFlags(0), 00583 NumMemRefs(0), MemRefs(nullptr), debugLoc(dl) { 00584 // Reserve space for the expected number of operands. 00585 if (unsigned NumOps = MCID->getNumOperands() + 00586 MCID->getNumImplicitDefs() + MCID->getNumImplicitUses()) { 00587 CapOperands = OperandCapacity::get(NumOps); 00588 Operands = MF.allocateOperandArray(CapOperands); 00589 } 00590 00591 if (!NoImp) 00592 addImplicitDefUseOperands(MF); 00593 } 00594 00595 /// MachineInstr ctor - Copies MachineInstr arg exactly 00596 /// 00597 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) 00598 : MCID(&MI.getDesc()), Parent(nullptr), Operands(nullptr), NumOperands(0), 00599 Flags(0), AsmPrinterFlags(0), 00600 NumMemRefs(MI.NumMemRefs), MemRefs(MI.MemRefs), 00601 debugLoc(MI.getDebugLoc()) { 00602 CapOperands = OperandCapacity::get(MI.getNumOperands()); 00603 Operands = MF.allocateOperandArray(CapOperands); 00604 00605 // Copy operands. 00606 for (unsigned i = 0; i != MI.getNumOperands(); ++i) 00607 addOperand(MF, MI.getOperand(i)); 00608 00609 // Copy all the sensible flags. 00610 setFlags(MI.Flags); 00611 } 00612 00613 /// getRegInfo - If this instruction is embedded into a MachineFunction, 00614 /// return the MachineRegisterInfo object for the current function, otherwise 00615 /// return null. 00616 MachineRegisterInfo *MachineInstr::getRegInfo() { 00617 if (MachineBasicBlock *MBB = getParent()) 00618 return &MBB->getParent()->getRegInfo(); 00619 return nullptr; 00620 } 00621 00622 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in 00623 /// this instruction from their respective use lists. This requires that the 00624 /// operands already be on their use lists. 00625 void MachineInstr::RemoveRegOperandsFromUseLists(MachineRegisterInfo &MRI) { 00626 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00627 if (Operands[i].isReg()) 00628 MRI.removeRegOperandFromUseList(&Operands[i]); 00629 } 00630 00631 /// AddRegOperandsToUseLists - Add all of the register operands in 00632 /// this instruction from their respective use lists. This requires that the 00633 /// operands not be on their use lists yet. 00634 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &MRI) { 00635 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00636 if (Operands[i].isReg()) 00637 MRI.addRegOperandToUseList(&Operands[i]); 00638 } 00639 00640 void MachineInstr::addOperand(const MachineOperand &Op) { 00641 MachineBasicBlock *MBB = getParent(); 00642 assert(MBB && "Use MachineInstrBuilder to add operands to dangling instrs"); 00643 MachineFunction *MF = MBB->getParent(); 00644 assert(MF && "Use MachineInstrBuilder to add operands to dangling instrs"); 00645 addOperand(*MF, Op); 00646 } 00647 00648 /// Move NumOps MachineOperands from Src to Dst, with support for overlapping 00649 /// ranges. If MRI is non-null also update use-def chains. 00650 static void moveOperands(MachineOperand *Dst, MachineOperand *Src, 00651 unsigned NumOps, MachineRegisterInfo *MRI) { 00652 if (MRI) 00653 return MRI->moveOperands(Dst, Src, NumOps); 00654 00655 // Here it would be convenient to call memmove, so that isn't allowed because 00656 // MachineOperand has a constructor and so isn't a POD type. 00657 if (Dst < Src) 00658 for (unsigned i = 0; i != NumOps; ++i) 00659 new (Dst + i) MachineOperand(Src[i]); 00660 else 00661 for (unsigned i = NumOps; i ; --i) 00662 new (Dst + i - 1) MachineOperand(Src[i - 1]); 00663 } 00664 00665 /// addOperand - Add the specified operand to the instruction. If it is an 00666 /// implicit operand, it is added to the end of the operand list. If it is 00667 /// an explicit operand it is added at the end of the explicit operand list 00668 /// (before the first implicit operand). 00669 void MachineInstr::addOperand(MachineFunction &MF, const MachineOperand &Op) { 00670 assert(MCID && "Cannot add operands before providing an instr descriptor"); 00671 00672 // Check if we're adding one of our existing operands. 00673 if (&Op >= Operands && &Op < Operands + NumOperands) { 00674 // This is unusual: MI->addOperand(MI->getOperand(i)). 00675 // If adding Op requires reallocating or moving existing operands around, 00676 // the Op reference could go stale. Support it by copying Op. 00677 MachineOperand CopyOp(Op); 00678 return addOperand(MF, CopyOp); 00679 } 00680 00681 // Find the insert location for the new operand. Implicit registers go at 00682 // the end, everything else goes before the implicit regs. 00683 // 00684 // FIXME: Allow mixed explicit and implicit operands on inline asm. 00685 // InstrEmitter::EmitSpecialNode() is marking inline asm clobbers as 00686 // implicit-defs, but they must not be moved around. See the FIXME in 00687 // InstrEmitter.cpp. 00688 unsigned OpNo = getNumOperands(); 00689 bool isImpReg = Op.isReg() && Op.isImplicit(); 00690 if (!isImpReg && !isInlineAsm()) { 00691 while (OpNo && Operands[OpNo-1].isReg() && Operands[OpNo-1].isImplicit()) { 00692 --OpNo; 00693 assert(!Operands[OpNo].isTied() && "Cannot move tied operands"); 00694 } 00695 } 00696 00697 #ifndef NDEBUG 00698 bool isMetaDataOp = Op.getType() == MachineOperand::MO_Metadata; 00699 // OpNo now points as the desired insertion point. Unless this is a variadic 00700 // instruction, only implicit regs are allowed beyond MCID->getNumOperands(). 00701 // RegMask operands go between the explicit and implicit operands. 00702 assert((isImpReg || Op.isRegMask() || MCID->isVariadic() || 00703 OpNo < MCID->getNumOperands() || isMetaDataOp) && 00704 "Trying to add an operand to a machine instr that is already done!"); 00705 #endif 00706 00707 MachineRegisterInfo *MRI = getRegInfo(); 00708 00709 // Determine if the Operands array needs to be reallocated. 00710 // Save the old capacity and operand array. 00711 OperandCapacity OldCap = CapOperands; 00712 MachineOperand *OldOperands = Operands; 00713 if (!OldOperands || OldCap.getSize() == getNumOperands()) { 00714 CapOperands = OldOperands ? OldCap.getNext() : OldCap.get(1); 00715 Operands = MF.allocateOperandArray(CapOperands); 00716 // Move the operands before the insertion point. 00717 if (OpNo) 00718 moveOperands(Operands, OldOperands, OpNo, MRI); 00719 } 00720 00721 // Move the operands following the insertion point. 00722 if (OpNo != NumOperands) 00723 moveOperands(Operands + OpNo + 1, OldOperands + OpNo, NumOperands - OpNo, 00724 MRI); 00725 ++NumOperands; 00726 00727 // Deallocate the old operand array. 00728 if (OldOperands != Operands && OldOperands) 00729 MF.deallocateOperandArray(OldCap, OldOperands); 00730 00731 // Copy Op into place. It still needs to be inserted into the MRI use lists. 00732 MachineOperand *NewMO = new (Operands + OpNo) MachineOperand(Op); 00733 NewMO->ParentMI = this; 00734 00735 // When adding a register operand, tell MRI about it. 00736 if (NewMO->isReg()) { 00737 // Ensure isOnRegUseList() returns false, regardless of Op's status. 00738 NewMO->Contents.Reg.Prev = nullptr; 00739 // Ignore existing ties. This is not a property that can be copied. 00740 NewMO->TiedTo = 0; 00741 // Add the new operand to MRI, but only for instructions in an MBB. 00742 if (MRI) 00743 MRI->addRegOperandToUseList(NewMO); 00744 // The MCID operand information isn't accurate until we start adding 00745 // explicit operands. The implicit operands are added first, then the 00746 // explicits are inserted before them. 00747 if (!isImpReg) { 00748 // Tie uses to defs as indicated in MCInstrDesc. 00749 if (NewMO->isUse()) { 00750 int DefIdx = MCID->getOperandConstraint(OpNo, MCOI::TIED_TO); 00751 if (DefIdx != -1) 00752 tieOperands(DefIdx, OpNo); 00753 } 00754 // If the register operand is flagged as early, mark the operand as such. 00755 if (MCID->getOperandConstraint(OpNo, MCOI::EARLY_CLOBBER) != -1) 00756 NewMO->setIsEarlyClobber(true); 00757 } 00758 } 00759 } 00760 00761 /// RemoveOperand - Erase an operand from an instruction, leaving it with one 00762 /// fewer operand than it started with. 00763 /// 00764 void MachineInstr::RemoveOperand(unsigned OpNo) { 00765 assert(OpNo < getNumOperands() && "Invalid operand number"); 00766 untieRegOperand(OpNo); 00767 00768 #ifndef NDEBUG 00769 // Moving tied operands would break the ties. 00770 for (unsigned i = OpNo + 1, e = getNumOperands(); i != e; ++i) 00771 if (Operands[i].isReg()) 00772 assert(!Operands[i].isTied() && "Cannot move tied operands"); 00773 #endif 00774 00775 MachineRegisterInfo *MRI = getRegInfo(); 00776 if (MRI && Operands[OpNo].isReg()) 00777 MRI->removeRegOperandFromUseList(Operands + OpNo); 00778 00779 // Don't call the MachineOperand destructor. A lot of this code depends on 00780 // MachineOperand having a trivial destructor anyway, and adding a call here 00781 // wouldn't make it 'destructor-correct'. 00782 00783 if (unsigned N = NumOperands - 1 - OpNo) 00784 moveOperands(Operands + OpNo, Operands + OpNo + 1, N, MRI); 00785 --NumOperands; 00786 } 00787 00788 /// addMemOperand - Add a MachineMemOperand to the machine instruction. 00789 /// This function should be used only occasionally. The setMemRefs function 00790 /// is the primary method for setting up a MachineInstr's MemRefs list. 00791 void MachineInstr::addMemOperand(MachineFunction &MF, 00792 MachineMemOperand *MO) { 00793 mmo_iterator OldMemRefs = MemRefs; 00794 unsigned OldNumMemRefs = NumMemRefs; 00795 00796 unsigned NewNum = NumMemRefs + 1; 00797 mmo_iterator NewMemRefs = MF.allocateMemRefsArray(NewNum); 00798 00799 std::copy(OldMemRefs, OldMemRefs + OldNumMemRefs, NewMemRefs); 00800 NewMemRefs[NewNum - 1] = MO; 00801 setMemRefs(NewMemRefs, NewMemRefs + NewNum); 00802 } 00803 00804 bool MachineInstr::hasPropertyInBundle(unsigned Mask, QueryType Type) const { 00805 assert(!isBundledWithPred() && "Must be called on bundle header"); 00806 for (MachineBasicBlock::const_instr_iterator MII = this;; ++MII) { 00807 if (MII->getDesc().getFlags() & Mask) { 00808 if (Type == AnyInBundle) 00809 return true; 00810 } else { 00811 if (Type == AllInBundle && !MII->isBundle()) 00812 return false; 00813 } 00814 // This was the last instruction in the bundle. 00815 if (!MII->isBundledWithSucc()) 00816 return Type == AllInBundle; 00817 } 00818 } 00819 00820 bool MachineInstr::isIdenticalTo(const MachineInstr *Other, 00821 MICheckType Check) const { 00822 // If opcodes or number of operands are not the same then the two 00823 // instructions are obviously not identical. 00824 if (Other->getOpcode() != getOpcode() || 00825 Other->getNumOperands() != getNumOperands()) 00826 return false; 00827 00828 if (isBundle()) { 00829 // Both instructions are bundles, compare MIs inside the bundle. 00830 MachineBasicBlock::const_instr_iterator I1 = *this; 00831 MachineBasicBlock::const_instr_iterator E1 = getParent()->instr_end(); 00832 MachineBasicBlock::const_instr_iterator I2 = *Other; 00833 MachineBasicBlock::const_instr_iterator E2= Other->getParent()->instr_end(); 00834 while (++I1 != E1 && I1->isInsideBundle()) { 00835 ++I2; 00836 if (I2 == E2 || !I2->isInsideBundle() || !I1->isIdenticalTo(I2, Check)) 00837 return false; 00838 } 00839 } 00840 00841 // Check operands to make sure they match. 00842 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 00843 const MachineOperand &MO = getOperand(i); 00844 const MachineOperand &OMO = Other->getOperand(i); 00845 if (!MO.isReg()) { 00846 if (!MO.isIdenticalTo(OMO)) 00847 return false; 00848 continue; 00849 } 00850 00851 // Clients may or may not want to ignore defs when testing for equality. 00852 // For example, machine CSE pass only cares about finding common 00853 // subexpressions, so it's safe to ignore virtual register defs. 00854 if (MO.isDef()) { 00855 if (Check == IgnoreDefs) 00856 continue; 00857 else if (Check == IgnoreVRegDefs) { 00858 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) || 00859 TargetRegisterInfo::isPhysicalRegister(OMO.getReg())) 00860 if (MO.getReg() != OMO.getReg()) 00861 return false; 00862 } else { 00863 if (!MO.isIdenticalTo(OMO)) 00864 return false; 00865 if (Check == CheckKillDead && MO.isDead() != OMO.isDead()) 00866 return false; 00867 } 00868 } else { 00869 if (!MO.isIdenticalTo(OMO)) 00870 return false; 00871 if (Check == CheckKillDead && MO.isKill() != OMO.isKill()) 00872 return false; 00873 } 00874 } 00875 // If DebugLoc does not match then two dbg.values are not identical. 00876 if (isDebugValue()) 00877 if (!getDebugLoc().isUnknown() && !Other->getDebugLoc().isUnknown() 00878 && getDebugLoc() != Other->getDebugLoc()) 00879 return false; 00880 return true; 00881 } 00882 00883 MachineInstr *MachineInstr::removeFromParent() { 00884 assert(getParent() && "Not embedded in a basic block!"); 00885 return getParent()->remove(this); 00886 } 00887 00888 MachineInstr *MachineInstr::removeFromBundle() { 00889 assert(getParent() && "Not embedded in a basic block!"); 00890 return getParent()->remove_instr(this); 00891 } 00892 00893 void MachineInstr::eraseFromParent() { 00894 assert(getParent() && "Not embedded in a basic block!"); 00895 getParent()->erase(this); 00896 } 00897 00898 void MachineInstr::eraseFromParentAndMarkDBGValuesForRemoval() { 00899 assert(getParent() && "Not embedded in a basic block!"); 00900 MachineBasicBlock *MBB = getParent(); 00901 MachineFunction *MF = MBB->getParent(); 00902 assert(MF && "Not embedded in a function!"); 00903 00904 MachineInstr *MI = (MachineInstr *)this; 00905 MachineRegisterInfo &MRI = MF->getRegInfo(); 00906 00907 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00908 const MachineOperand &MO = MI->getOperand(i); 00909 if (!MO.isReg() || !MO.isDef()) 00910 continue; 00911 unsigned Reg = MO.getReg(); 00912 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 00913 continue; 00914 MRI.markUsesInDebugValueAsUndef(Reg); 00915 } 00916 MI->eraseFromParent(); 00917 } 00918 00919 void MachineInstr::eraseFromBundle() { 00920 assert(getParent() && "Not embedded in a basic block!"); 00921 getParent()->erase_instr(this); 00922 } 00923 00924 /// getNumExplicitOperands - Returns the number of non-implicit operands. 00925 /// 00926 unsigned MachineInstr::getNumExplicitOperands() const { 00927 unsigned NumOperands = MCID->getNumOperands(); 00928 if (!MCID->isVariadic()) 00929 return NumOperands; 00930 00931 for (unsigned i = NumOperands, e = getNumOperands(); i != e; ++i) { 00932 const MachineOperand &MO = getOperand(i); 00933 if (!MO.isReg() || !MO.isImplicit()) 00934 NumOperands++; 00935 } 00936 return NumOperands; 00937 } 00938 00939 void MachineInstr::bundleWithPred() { 00940 assert(!isBundledWithPred() && "MI is already bundled with its predecessor"); 00941 setFlag(BundledPred); 00942 MachineBasicBlock::instr_iterator Pred = this; 00943 --Pred; 00944 assert(!Pred->isBundledWithSucc() && "Inconsistent bundle flags"); 00945 Pred->setFlag(BundledSucc); 00946 } 00947 00948 void MachineInstr::bundleWithSucc() { 00949 assert(!isBundledWithSucc() && "MI is already bundled with its successor"); 00950 setFlag(BundledSucc); 00951 MachineBasicBlock::instr_iterator Succ = this; 00952 ++Succ; 00953 assert(!Succ->isBundledWithPred() && "Inconsistent bundle flags"); 00954 Succ->setFlag(BundledPred); 00955 } 00956 00957 void MachineInstr::unbundleFromPred() { 00958 assert(isBundledWithPred() && "MI isn't bundled with its predecessor"); 00959 clearFlag(BundledPred); 00960 MachineBasicBlock::instr_iterator Pred = this; 00961 --Pred; 00962 assert(Pred->isBundledWithSucc() && "Inconsistent bundle flags"); 00963 Pred->clearFlag(BundledSucc); 00964 } 00965 00966 void MachineInstr::unbundleFromSucc() { 00967 assert(isBundledWithSucc() && "MI isn't bundled with its successor"); 00968 clearFlag(BundledSucc); 00969 MachineBasicBlock::instr_iterator Succ = this; 00970 ++Succ; 00971 assert(Succ->isBundledWithPred() && "Inconsistent bundle flags"); 00972 Succ->clearFlag(BundledPred); 00973 } 00974 00975 bool MachineInstr::isStackAligningInlineAsm() const { 00976 if (isInlineAsm()) { 00977 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 00978 if (ExtraInfo & InlineAsm::Extra_IsAlignStack) 00979 return true; 00980 } 00981 return false; 00982 } 00983 00984 InlineAsm::AsmDialect MachineInstr::getInlineAsmDialect() const { 00985 assert(isInlineAsm() && "getInlineAsmDialect() only works for inline asms!"); 00986 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 00987 return InlineAsm::AsmDialect((ExtraInfo & InlineAsm::Extra_AsmDialect) != 0); 00988 } 00989 00990 int MachineInstr::findInlineAsmFlagIdx(unsigned OpIdx, 00991 unsigned *GroupNo) const { 00992 assert(isInlineAsm() && "Expected an inline asm instruction"); 00993 assert(OpIdx < getNumOperands() && "OpIdx out of range"); 00994 00995 // Ignore queries about the initial operands. 00996 if (OpIdx < InlineAsm::MIOp_FirstOperand) 00997 return -1; 00998 00999 unsigned Group = 0; 01000 unsigned NumOps; 01001 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e; 01002 i += NumOps) { 01003 const MachineOperand &FlagMO = getOperand(i); 01004 // If we reach the implicit register operands, stop looking. 01005 if (!FlagMO.isImm()) 01006 return -1; 01007 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm()); 01008 if (i + NumOps > OpIdx) { 01009 if (GroupNo) 01010 *GroupNo = Group; 01011 return i; 01012 } 01013 ++Group; 01014 } 01015 return -1; 01016 } 01017 01018 const TargetRegisterClass* 01019 MachineInstr::getRegClassConstraint(unsigned OpIdx, 01020 const TargetInstrInfo *TII, 01021 const TargetRegisterInfo *TRI) const { 01022 assert(getParent() && "Can't have an MBB reference here!"); 01023 assert(getParent()->getParent() && "Can't have an MF reference here!"); 01024 const MachineFunction &MF = *getParent()->getParent(); 01025 01026 // Most opcodes have fixed constraints in their MCInstrDesc. 01027 if (!isInlineAsm()) 01028 return TII->getRegClass(getDesc(), OpIdx, TRI, MF); 01029 01030 if (!getOperand(OpIdx).isReg()) 01031 return nullptr; 01032 01033 // For tied uses on inline asm, get the constraint from the def. 01034 unsigned DefIdx; 01035 if (getOperand(OpIdx).isUse() && isRegTiedToDefOperand(OpIdx, &DefIdx)) 01036 OpIdx = DefIdx; 01037 01038 // Inline asm stores register class constraints in the flag word. 01039 int FlagIdx = findInlineAsmFlagIdx(OpIdx); 01040 if (FlagIdx < 0) 01041 return nullptr; 01042 01043 unsigned Flag = getOperand(FlagIdx).getImm(); 01044 unsigned RCID; 01045 if (InlineAsm::hasRegClassConstraint(Flag, RCID)) 01046 return TRI->getRegClass(RCID); 01047 01048 // Assume that all registers in a memory operand are pointers. 01049 if (InlineAsm::getKind(Flag) == InlineAsm::Kind_Mem) 01050 return TRI->getPointerRegClass(MF); 01051 01052 return nullptr; 01053 } 01054 01055 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVReg( 01056 unsigned Reg, const TargetRegisterClass *CurRC, const TargetInstrInfo *TII, 01057 const TargetRegisterInfo *TRI, bool ExploreBundle) const { 01058 // Check every operands inside the bundle if we have 01059 // been asked to. 01060 if (ExploreBundle) 01061 for (ConstMIBundleOperands OpndIt(this); OpndIt.isValid() && CurRC; 01062 ++OpndIt) 01063 CurRC = OpndIt->getParent()->getRegClassConstraintEffectForVRegImpl( 01064 OpndIt.getOperandNo(), Reg, CurRC, TII, TRI); 01065 else 01066 // Otherwise, just check the current operands. 01067 for (ConstMIOperands OpndIt(this); OpndIt.isValid() && CurRC; ++OpndIt) 01068 CurRC = getRegClassConstraintEffectForVRegImpl(OpndIt.getOperandNo(), Reg, 01069 CurRC, TII, TRI); 01070 return CurRC; 01071 } 01072 01073 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffectForVRegImpl( 01074 unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC, 01075 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const { 01076 assert(CurRC && "Invalid initial register class"); 01077 // Check if Reg is constrained by some of its use/def from MI. 01078 const MachineOperand &MO = getOperand(OpIdx); 01079 if (!MO.isReg() || MO.getReg() != Reg) 01080 return CurRC; 01081 // If yes, accumulate the constraints through the operand. 01082 return getRegClassConstraintEffect(OpIdx, CurRC, TII, TRI); 01083 } 01084 01085 const TargetRegisterClass *MachineInstr::getRegClassConstraintEffect( 01086 unsigned OpIdx, const TargetRegisterClass *CurRC, 01087 const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const { 01088 const TargetRegisterClass *OpRC = getRegClassConstraint(OpIdx, TII, TRI); 01089 const MachineOperand &MO = getOperand(OpIdx); 01090 assert(MO.isReg() && 01091 "Cannot get register constraints for non-register operand"); 01092 assert(CurRC && "Invalid initial register class"); 01093 if (unsigned SubIdx = MO.getSubReg()) { 01094 if (OpRC) 01095 CurRC = TRI->getMatchingSuperRegClass(CurRC, OpRC, SubIdx); 01096 else 01097 CurRC = TRI->getSubClassWithSubReg(CurRC, SubIdx); 01098 } else if (OpRC) 01099 CurRC = TRI->getCommonSubClass(CurRC, OpRC); 01100 return CurRC; 01101 } 01102 01103 /// Return the number of instructions inside the MI bundle, not counting the 01104 /// header instruction. 01105 unsigned MachineInstr::getBundleSize() const { 01106 MachineBasicBlock::const_instr_iterator I = this; 01107 unsigned Size = 0; 01108 while (I->isBundledWithSucc()) 01109 ++Size, ++I; 01110 return Size; 01111 } 01112 01113 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of 01114 /// the specific register or -1 if it is not found. It further tightens 01115 /// the search criteria to a use that kills the register if isKill is true. 01116 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, 01117 const TargetRegisterInfo *TRI) const { 01118 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01119 const MachineOperand &MO = getOperand(i); 01120 if (!MO.isReg() || !MO.isUse()) 01121 continue; 01122 unsigned MOReg = MO.getReg(); 01123 if (!MOReg) 01124 continue; 01125 if (MOReg == Reg || 01126 (TRI && 01127 TargetRegisterInfo::isPhysicalRegister(MOReg) && 01128 TargetRegisterInfo::isPhysicalRegister(Reg) && 01129 TRI->isSubRegister(MOReg, Reg))) 01130 if (!isKill || MO.isKill()) 01131 return i; 01132 } 01133 return -1; 01134 } 01135 01136 /// readsWritesVirtualRegister - Return a pair of bools (reads, writes) 01137 /// indicating if this instruction reads or writes Reg. This also considers 01138 /// partial defines. 01139 std::pair<bool,bool> 01140 MachineInstr::readsWritesVirtualRegister(unsigned Reg, 01141 SmallVectorImpl<unsigned> *Ops) const { 01142 bool PartDef = false; // Partial redefine. 01143 bool FullDef = false; // Full define. 01144 bool Use = false; 01145 01146 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01147 const MachineOperand &MO = getOperand(i); 01148 if (!MO.isReg() || MO.getReg() != Reg) 01149 continue; 01150 if (Ops) 01151 Ops->push_back(i); 01152 if (MO.isUse()) 01153 Use |= !MO.isUndef(); 01154 else if (MO.getSubReg() && !MO.isUndef()) 01155 // A partial <def,undef> doesn't count as reading the register. 01156 PartDef = true; 01157 else 01158 FullDef = true; 01159 } 01160 // A partial redefine uses Reg unless there is also a full define. 01161 return std::make_pair(Use || (PartDef && !FullDef), PartDef || FullDef); 01162 } 01163 01164 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of 01165 /// the specified register or -1 if it is not found. If isDead is true, defs 01166 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it 01167 /// also checks if there is a def of a super-register. 01168 int 01169 MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, bool Overlap, 01170 const TargetRegisterInfo *TRI) const { 01171 bool isPhys = TargetRegisterInfo::isPhysicalRegister(Reg); 01172 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01173 const MachineOperand &MO = getOperand(i); 01174 // Accept regmask operands when Overlap is set. 01175 // Ignore them when looking for a specific def operand (Overlap == false). 01176 if (isPhys && Overlap && MO.isRegMask() && MO.clobbersPhysReg(Reg)) 01177 return i; 01178 if (!MO.isReg() || !MO.isDef()) 01179 continue; 01180 unsigned MOReg = MO.getReg(); 01181 bool Found = (MOReg == Reg); 01182 if (!Found && TRI && isPhys && 01183 TargetRegisterInfo::isPhysicalRegister(MOReg)) { 01184 if (Overlap) 01185 Found = TRI->regsOverlap(MOReg, Reg); 01186 else 01187 Found = TRI->isSubRegister(MOReg, Reg); 01188 } 01189 if (Found && (!isDead || MO.isDead())) 01190 return i; 01191 } 01192 return -1; 01193 } 01194 01195 /// findFirstPredOperandIdx() - Find the index of the first operand in the 01196 /// operand list that is used to represent the predicate. It returns -1 if 01197 /// none is found. 01198 int MachineInstr::findFirstPredOperandIdx() const { 01199 // Don't call MCID.findFirstPredOperandIdx() because this variant 01200 // is sometimes called on an instruction that's not yet complete, and 01201 // so the number of operands is less than the MCID indicates. In 01202 // particular, the PTX target does this. 01203 const MCInstrDesc &MCID = getDesc(); 01204 if (MCID.isPredicable()) { 01205 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 01206 if (MCID.OpInfo[i].isPredicate()) 01207 return i; 01208 } 01209 01210 return -1; 01211 } 01212 01213 // MachineOperand::TiedTo is 4 bits wide. 01214 const unsigned TiedMax = 15; 01215 01216 /// tieOperands - Mark operands at DefIdx and UseIdx as tied to each other. 01217 /// 01218 /// Use and def operands can be tied together, indicated by a non-zero TiedTo 01219 /// field. TiedTo can have these values: 01220 /// 01221 /// 0: Operand is not tied to anything. 01222 /// 1 to TiedMax-1: Tied to getOperand(TiedTo-1). 01223 /// TiedMax: Tied to an operand >= TiedMax-1. 01224 /// 01225 /// The tied def must be one of the first TiedMax operands on a normal 01226 /// instruction. INLINEASM instructions allow more tied defs. 01227 /// 01228 void MachineInstr::tieOperands(unsigned DefIdx, unsigned UseIdx) { 01229 MachineOperand &DefMO = getOperand(DefIdx); 01230 MachineOperand &UseMO = getOperand(UseIdx); 01231 assert(DefMO.isDef() && "DefIdx must be a def operand"); 01232 assert(UseMO.isUse() && "UseIdx must be a use operand"); 01233 assert(!DefMO.isTied() && "Def is already tied to another use"); 01234 assert(!UseMO.isTied() && "Use is already tied to another def"); 01235 01236 if (DefIdx < TiedMax) 01237 UseMO.TiedTo = DefIdx + 1; 01238 else { 01239 // Inline asm can use the group descriptors to find tied operands, but on 01240 // normal instruction, the tied def must be within the first TiedMax 01241 // operands. 01242 assert(isInlineAsm() && "DefIdx out of range"); 01243 UseMO.TiedTo = TiedMax; 01244 } 01245 01246 // UseIdx can be out of range, we'll search for it in findTiedOperandIdx(). 01247 DefMO.TiedTo = std::min(UseIdx + 1, TiedMax); 01248 } 01249 01250 /// Given the index of a tied register operand, find the operand it is tied to. 01251 /// Defs are tied to uses and vice versa. Returns the index of the tied operand 01252 /// which must exist. 01253 unsigned MachineInstr::findTiedOperandIdx(unsigned OpIdx) const { 01254 const MachineOperand &MO = getOperand(OpIdx); 01255 assert(MO.isTied() && "Operand isn't tied"); 01256 01257 // Normally TiedTo is in range. 01258 if (MO.TiedTo < TiedMax) 01259 return MO.TiedTo - 1; 01260 01261 // Uses on normal instructions can be out of range. 01262 if (!isInlineAsm()) { 01263 // Normal tied defs must be in the 0..TiedMax-1 range. 01264 if (MO.isUse()) 01265 return TiedMax - 1; 01266 // MO is a def. Search for the tied use. 01267 for (unsigned i = TiedMax - 1, e = getNumOperands(); i != e; ++i) { 01268 const MachineOperand &UseMO = getOperand(i); 01269 if (UseMO.isReg() && UseMO.isUse() && UseMO.TiedTo == OpIdx + 1) 01270 return i; 01271 } 01272 llvm_unreachable("Can't find tied use"); 01273 } 01274 01275 // Now deal with inline asm by parsing the operand group descriptor flags. 01276 // Find the beginning of each operand group. 01277 SmallVector<unsigned, 8> GroupIdx; 01278 unsigned OpIdxGroup = ~0u; 01279 unsigned NumOps; 01280 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = getNumOperands(); i < e; 01281 i += NumOps) { 01282 const MachineOperand &FlagMO = getOperand(i); 01283 assert(FlagMO.isImm() && "Invalid tied operand on inline asm"); 01284 unsigned CurGroup = GroupIdx.size(); 01285 GroupIdx.push_back(i); 01286 NumOps = 1 + InlineAsm::getNumOperandRegisters(FlagMO.getImm()); 01287 // OpIdx belongs to this operand group. 01288 if (OpIdx > i && OpIdx < i + NumOps) 01289 OpIdxGroup = CurGroup; 01290 unsigned TiedGroup; 01291 if (!InlineAsm::isUseOperandTiedToDef(FlagMO.getImm(), TiedGroup)) 01292 continue; 01293 // Operands in this group are tied to operands in TiedGroup which must be 01294 // earlier. Find the number of operands between the two groups. 01295 unsigned Delta = i - GroupIdx[TiedGroup]; 01296 01297 // OpIdx is a use tied to TiedGroup. 01298 if (OpIdxGroup == CurGroup) 01299 return OpIdx - Delta; 01300 01301 // OpIdx is a def tied to this use group. 01302 if (OpIdxGroup == TiedGroup) 01303 return OpIdx + Delta; 01304 } 01305 llvm_unreachable("Invalid tied operand on inline asm"); 01306 } 01307 01308 /// clearKillInfo - Clears kill flags on all operands. 01309 /// 01310 void MachineInstr::clearKillInfo() { 01311 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01312 MachineOperand &MO = getOperand(i); 01313 if (MO.isReg() && MO.isUse()) 01314 MO.setIsKill(false); 01315 } 01316 } 01317 01318 void MachineInstr::substituteRegister(unsigned FromReg, 01319 unsigned ToReg, 01320 unsigned SubIdx, 01321 const TargetRegisterInfo &RegInfo) { 01322 if (TargetRegisterInfo::isPhysicalRegister(ToReg)) { 01323 if (SubIdx) 01324 ToReg = RegInfo.getSubReg(ToReg, SubIdx); 01325 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01326 MachineOperand &MO = getOperand(i); 01327 if (!MO.isReg() || MO.getReg() != FromReg) 01328 continue; 01329 MO.substPhysReg(ToReg, RegInfo); 01330 } 01331 } else { 01332 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01333 MachineOperand &MO = getOperand(i); 01334 if (!MO.isReg() || MO.getReg() != FromReg) 01335 continue; 01336 MO.substVirtReg(ToReg, SubIdx, RegInfo); 01337 } 01338 } 01339 } 01340 01341 /// isSafeToMove - Return true if it is safe to move this instruction. If 01342 /// SawStore is set to true, it means that there is a store (or call) between 01343 /// the instruction's location and its intended destination. 01344 bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, 01345 AliasAnalysis *AA, 01346 bool &SawStore) const { 01347 // Ignore stuff that we obviously can't move. 01348 // 01349 // Treat volatile loads as stores. This is not strictly necessary for 01350 // volatiles, but it is required for atomic loads. It is not allowed to move 01351 // a load across an atomic load with Ordering > Monotonic. 01352 if (mayStore() || isCall() || 01353 (mayLoad() && hasOrderedMemoryRef())) { 01354 SawStore = true; 01355 return false; 01356 } 01357 01358 if (isPosition() || isDebugValue() || isTerminator() || 01359 hasUnmodeledSideEffects()) 01360 return false; 01361 01362 // See if this instruction does a load. If so, we have to guarantee that the 01363 // loaded value doesn't change between the load and the its intended 01364 // destination. The check for isInvariantLoad gives the targe the chance to 01365 // classify the load as always returning a constant, e.g. a constant pool 01366 // load. 01367 if (mayLoad() && !isInvariantLoad(AA)) 01368 // Otherwise, this is a real load. If there is a store between the load and 01369 // end of block, we can't move it. 01370 return !SawStore; 01371 01372 return true; 01373 } 01374 01375 /// hasOrderedMemoryRef - Return true if this instruction may have an ordered 01376 /// or volatile memory reference, or if the information describing the memory 01377 /// reference is not available. Return false if it is known to have no ordered 01378 /// memory references. 01379 bool MachineInstr::hasOrderedMemoryRef() const { 01380 // An instruction known never to access memory won't have a volatile access. 01381 if (!mayStore() && 01382 !mayLoad() && 01383 !isCall() && 01384 !hasUnmodeledSideEffects()) 01385 return false; 01386 01387 // Otherwise, if the instruction has no memory reference information, 01388 // conservatively assume it wasn't preserved. 01389 if (memoperands_empty()) 01390 return true; 01391 01392 // Check the memory reference information for ordered references. 01393 for (mmo_iterator I = memoperands_begin(), E = memoperands_end(); I != E; ++I) 01394 if (!(*I)->isUnordered()) 01395 return true; 01396 01397 return false; 01398 } 01399 01400 /// isInvariantLoad - Return true if this instruction is loading from a 01401 /// location whose value is invariant across the function. For example, 01402 /// loading a value from the constant pool or from the argument area 01403 /// of a function if it does not change. This should only return true of 01404 /// *all* loads the instruction does are invariant (if it does multiple loads). 01405 bool MachineInstr::isInvariantLoad(AliasAnalysis *AA) const { 01406 // If the instruction doesn't load at all, it isn't an invariant load. 01407 if (!mayLoad()) 01408 return false; 01409 01410 // If the instruction has lost its memoperands, conservatively assume that 01411 // it may not be an invariant load. 01412 if (memoperands_empty()) 01413 return false; 01414 01415 const MachineFrameInfo *MFI = getParent()->getParent()->getFrameInfo(); 01416 01417 for (mmo_iterator I = memoperands_begin(), 01418 E = memoperands_end(); I != E; ++I) { 01419 if ((*I)->isVolatile()) return false; 01420 if ((*I)->isStore()) return false; 01421 if ((*I)->isInvariant()) return true; 01422 01423 01424 // A load from a constant PseudoSourceValue is invariant. 01425 if (const PseudoSourceValue *PSV = (*I)->getPseudoValue()) 01426 if (PSV->isConstant(MFI)) 01427 continue; 01428 01429 if (const Value *V = (*I)->getValue()) { 01430 // If we have an AliasAnalysis, ask it whether the memory is constant. 01431 if (AA && AA->pointsToConstantMemory( 01432 AliasAnalysis::Location(V, (*I)->getSize(), 01433 (*I)->getAAInfo()))) 01434 continue; 01435 } 01436 01437 // Otherwise assume conservatively. 01438 return false; 01439 } 01440 01441 // Everything checks out. 01442 return true; 01443 } 01444 01445 /// isConstantValuePHI - If the specified instruction is a PHI that always 01446 /// merges together the same virtual register, return the register, otherwise 01447 /// return 0. 01448 unsigned MachineInstr::isConstantValuePHI() const { 01449 if (!isPHI()) 01450 return 0; 01451 assert(getNumOperands() >= 3 && 01452 "It's illegal to have a PHI without source operands"); 01453 01454 unsigned Reg = getOperand(1).getReg(); 01455 for (unsigned i = 3, e = getNumOperands(); i < e; i += 2) 01456 if (getOperand(i).getReg() != Reg) 01457 return 0; 01458 return Reg; 01459 } 01460 01461 bool MachineInstr::hasUnmodeledSideEffects() const { 01462 if (hasProperty(MCID::UnmodeledSideEffects)) 01463 return true; 01464 if (isInlineAsm()) { 01465 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 01466 if (ExtraInfo & InlineAsm::Extra_HasSideEffects) 01467 return true; 01468 } 01469 01470 return false; 01471 } 01472 01473 /// allDefsAreDead - Return true if all the defs of this instruction are dead. 01474 /// 01475 bool MachineInstr::allDefsAreDead() const { 01476 for (unsigned i = 0, e = getNumOperands(); i < e; ++i) { 01477 const MachineOperand &MO = getOperand(i); 01478 if (!MO.isReg() || MO.isUse()) 01479 continue; 01480 if (!MO.isDead()) 01481 return false; 01482 } 01483 return true; 01484 } 01485 01486 /// copyImplicitOps - Copy implicit register operands from specified 01487 /// instruction to this instruction. 01488 void MachineInstr::copyImplicitOps(MachineFunction &MF, 01489 const MachineInstr *MI) { 01490 for (unsigned i = MI->getDesc().getNumOperands(), e = MI->getNumOperands(); 01491 i != e; ++i) { 01492 const MachineOperand &MO = MI->getOperand(i); 01493 if ((MO.isReg() && MO.isImplicit()) || MO.isRegMask()) 01494 addOperand(MF, MO); 01495 } 01496 } 01497 01498 void MachineInstr::dump() const { 01499 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 01500 dbgs() << " " << *this; 01501 #endif 01502 } 01503 01504 static void printDebugLoc(DebugLoc DL, const MachineFunction *MF, 01505 raw_ostream &CommentOS) { 01506 const LLVMContext &Ctx = MF->getFunction()->getContext(); 01507 DL.print(Ctx, CommentOS); 01508 } 01509 01510 void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM, 01511 bool SkipOpers) const { 01512 // We can be a bit tidier if we know the TargetMachine and/or MachineFunction. 01513 const MachineFunction *MF = nullptr; 01514 const MachineRegisterInfo *MRI = nullptr; 01515 if (const MachineBasicBlock *MBB = getParent()) { 01516 MF = MBB->getParent(); 01517 if (!TM && MF) 01518 TM = &MF->getTarget(); 01519 if (MF) 01520 MRI = &MF->getRegInfo(); 01521 } 01522 01523 // Save a list of virtual registers. 01524 SmallVector<unsigned, 8> VirtRegs; 01525 01526 // Print explicitly defined operands on the left of an assignment syntax. 01527 unsigned StartOp = 0, e = getNumOperands(); 01528 for (; StartOp < e && getOperand(StartOp).isReg() && 01529 getOperand(StartOp).isDef() && 01530 !getOperand(StartOp).isImplicit(); 01531 ++StartOp) { 01532 if (StartOp != 0) OS << ", "; 01533 getOperand(StartOp).print(OS, TM); 01534 unsigned Reg = getOperand(StartOp).getReg(); 01535 if (TargetRegisterInfo::isVirtualRegister(Reg)) 01536 VirtRegs.push_back(Reg); 01537 } 01538 01539 if (StartOp != 0) 01540 OS << " = "; 01541 01542 // Print the opcode name. 01543 if (TM && TM->getSubtargetImpl()->getInstrInfo()) 01544 OS << TM->getSubtargetImpl()->getInstrInfo()->getName(getOpcode()); 01545 else 01546 OS << "UNKNOWN"; 01547 01548 if (SkipOpers) 01549 return; 01550 01551 // Print the rest of the operands. 01552 bool OmittedAnyCallClobbers = false; 01553 bool FirstOp = true; 01554 unsigned AsmDescOp = ~0u; 01555 unsigned AsmOpCount = 0; 01556 01557 if (isInlineAsm() && e >= InlineAsm::MIOp_FirstOperand) { 01558 // Print asm string. 01559 OS << " "; 01560 getOperand(InlineAsm::MIOp_AsmString).print(OS, TM); 01561 01562 // Print HasSideEffects, MayLoad, MayStore, IsAlignStack 01563 unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm(); 01564 if (ExtraInfo & InlineAsm::Extra_HasSideEffects) 01565 OS << " [sideeffect]"; 01566 if (ExtraInfo & InlineAsm::Extra_MayLoad) 01567 OS << " [mayload]"; 01568 if (ExtraInfo & InlineAsm::Extra_MayStore) 01569 OS << " [maystore]"; 01570 if (ExtraInfo & InlineAsm::Extra_IsAlignStack) 01571 OS << " [alignstack]"; 01572 if (getInlineAsmDialect() == InlineAsm::AD_ATT) 01573 OS << " [attdialect]"; 01574 if (getInlineAsmDialect() == InlineAsm::AD_Intel) 01575 OS << " [inteldialect]"; 01576 01577 StartOp = AsmDescOp = InlineAsm::MIOp_FirstOperand; 01578 FirstOp = false; 01579 } 01580 01581 01582 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 01583 const MachineOperand &MO = getOperand(i); 01584 01585 if (MO.isReg() && TargetRegisterInfo::isVirtualRegister(MO.getReg())) 01586 VirtRegs.push_back(MO.getReg()); 01587 01588 // Omit call-clobbered registers which aren't used anywhere. This makes 01589 // call instructions much less noisy on targets where calls clobber lots 01590 // of registers. Don't rely on MO.isDead() because we may be called before 01591 // LiveVariables is run, or we may be looking at a non-allocatable reg. 01592 if (MF && isCall() && 01593 MO.isReg() && MO.isImplicit() && MO.isDef()) { 01594 unsigned Reg = MO.getReg(); 01595 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 01596 const MachineRegisterInfo &MRI = MF->getRegInfo(); 01597 if (MRI.use_empty(Reg)) { 01598 bool HasAliasLive = false; 01599 for (MCRegAliasIterator AI( 01600 Reg, TM->getSubtargetImpl()->getRegisterInfo(), true); 01601 AI.isValid(); ++AI) { 01602 unsigned AliasReg = *AI; 01603 if (!MRI.use_empty(AliasReg)) { 01604 HasAliasLive = true; 01605 break; 01606 } 01607 } 01608 if (!HasAliasLive) { 01609 OmittedAnyCallClobbers = true; 01610 continue; 01611 } 01612 } 01613 } 01614 } 01615 01616 if (FirstOp) FirstOp = false; else OS << ","; 01617 OS << " "; 01618 if (i < getDesc().NumOperands) { 01619 const MCOperandInfo &MCOI = getDesc().OpInfo[i]; 01620 if (MCOI.isPredicate()) 01621 OS << "pred:"; 01622 if (MCOI.isOptionalDef()) 01623 OS << "opt:"; 01624 } 01625 if (isDebugValue() && MO.isMetadata()) { 01626 // Pretty print DBG_VALUE instructions. 01627 const MDNode *MD = MO.getMetadata(); 01628 if (const MDString *MDS = dyn_cast<MDString>(MD->getOperand(2))) 01629 OS << "!\"" << MDS->getString() << '\"'; 01630 else 01631 MO.print(OS, TM); 01632 } else if (TM && (isInsertSubreg() || isRegSequence()) && MO.isImm()) { 01633 OS << TM->getSubtargetImpl()->getRegisterInfo()->getSubRegIndexName( 01634 MO.getImm()); 01635 } else if (i == AsmDescOp && MO.isImm()) { 01636 // Pretty print the inline asm operand descriptor. 01637 OS << '$' << AsmOpCount++; 01638 unsigned Flag = MO.getImm(); 01639 switch (InlineAsm::getKind(Flag)) { 01640 case InlineAsm::Kind_RegUse: OS << ":[reguse"; break; 01641 case InlineAsm::Kind_RegDef: OS << ":[regdef"; break; 01642 case InlineAsm::Kind_RegDefEarlyClobber: OS << ":[regdef-ec"; break; 01643 case InlineAsm::Kind_Clobber: OS << ":[clobber"; break; 01644 case InlineAsm::Kind_Imm: OS << ":[imm"; break; 01645 case InlineAsm::Kind_Mem: OS << ":[mem"; break; 01646 default: OS << ":[??" << InlineAsm::getKind(Flag); break; 01647 } 01648 01649 unsigned RCID = 0; 01650 if (InlineAsm::hasRegClassConstraint(Flag, RCID)) { 01651 if (TM) 01652 OS << ':' 01653 << TM->getSubtargetImpl() 01654 ->getRegisterInfo() 01655 ->getRegClass(RCID) 01656 ->getName(); 01657 else 01658 OS << ":RC" << RCID; 01659 } 01660 01661 unsigned TiedTo = 0; 01662 if (InlineAsm::isUseOperandTiedToDef(Flag, TiedTo)) 01663 OS << " tiedto:$" << TiedTo; 01664 01665 OS << ']'; 01666 01667 // Compute the index of the next operand descriptor. 01668 AsmDescOp += 1 + InlineAsm::getNumOperandRegisters(Flag); 01669 } else 01670 MO.print(OS, TM); 01671 } 01672 01673 // Briefly indicate whether any call clobbers were omitted. 01674 if (OmittedAnyCallClobbers) { 01675 if (!FirstOp) OS << ","; 01676 OS << " ..."; 01677 } 01678 01679 bool HaveSemi = false; 01680 const unsigned PrintableFlags = FrameSetup; 01681 if (Flags & PrintableFlags) { 01682 if (!HaveSemi) OS << ";"; HaveSemi = true; 01683 OS << " flags: "; 01684 01685 if (Flags & FrameSetup) 01686 OS << "FrameSetup"; 01687 } 01688 01689 if (!memoperands_empty()) { 01690 if (!HaveSemi) OS << ";"; HaveSemi = true; 01691 01692 OS << " mem:"; 01693 for (mmo_iterator i = memoperands_begin(), e = memoperands_end(); 01694 i != e; ++i) { 01695 OS << **i; 01696 if (std::next(i) != e) 01697 OS << " "; 01698 } 01699 } 01700 01701 // Print the regclass of any virtual registers encountered. 01702 if (MRI && !VirtRegs.empty()) { 01703 if (!HaveSemi) OS << ";"; HaveSemi = true; 01704 for (unsigned i = 0; i != VirtRegs.size(); ++i) { 01705 const TargetRegisterClass *RC = MRI->getRegClass(VirtRegs[i]); 01706 OS << " " << RC->getName() << ':' << PrintReg(VirtRegs[i]); 01707 for (unsigned j = i+1; j != VirtRegs.size();) { 01708 if (MRI->getRegClass(VirtRegs[j]) != RC) { 01709 ++j; 01710 continue; 01711 } 01712 if (VirtRegs[i] != VirtRegs[j]) 01713 OS << "," << PrintReg(VirtRegs[j]); 01714 VirtRegs.erase(VirtRegs.begin()+j); 01715 } 01716 } 01717 } 01718 01719 // Print debug location information. 01720 if (isDebugValue() && getOperand(e - 1).isMetadata()) { 01721 if (!HaveSemi) OS << ";"; 01722 DIVariable DV(getOperand(e - 1).getMetadata()); 01723 OS << " line no:" << DV.getLineNumber(); 01724 if (MDNode *InlinedAt = DV.getInlinedAt()) { 01725 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt); 01726 if (!InlinedAtDL.isUnknown() && MF) { 01727 OS << " inlined @[ "; 01728 printDebugLoc(InlinedAtDL, MF, OS); 01729 OS << " ]"; 01730 } 01731 } 01732 } else if (!debugLoc.isUnknown() && MF) { 01733 if (!HaveSemi) OS << ";"; 01734 OS << " dbg:"; 01735 printDebugLoc(debugLoc, MF, OS); 01736 } 01737 01738 OS << '\n'; 01739 } 01740 01741 bool MachineInstr::addRegisterKilled(unsigned IncomingReg, 01742 const TargetRegisterInfo *RegInfo, 01743 bool AddIfNotFound) { 01744 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 01745 bool hasAliases = isPhysReg && 01746 MCRegAliasIterator(IncomingReg, RegInfo, false).isValid(); 01747 bool Found = false; 01748 SmallVector<unsigned,4> DeadOps; 01749 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01750 MachineOperand &MO = getOperand(i); 01751 if (!MO.isReg() || !MO.isUse() || MO.isUndef()) 01752 continue; 01753 unsigned Reg = MO.getReg(); 01754 if (!Reg) 01755 continue; 01756 01757 if (Reg == IncomingReg) { 01758 if (!Found) { 01759 if (MO.isKill()) 01760 // The register is already marked kill. 01761 return true; 01762 if (isPhysReg && isRegTiedToDefOperand(i)) 01763 // Two-address uses of physregs must not be marked kill. 01764 return true; 01765 MO.setIsKill(); 01766 Found = true; 01767 } 01768 } else if (hasAliases && MO.isKill() && 01769 TargetRegisterInfo::isPhysicalRegister(Reg)) { 01770 // A super-register kill already exists. 01771 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 01772 return true; 01773 if (RegInfo->isSubRegister(IncomingReg, Reg)) 01774 DeadOps.push_back(i); 01775 } 01776 } 01777 01778 // Trim unneeded kill operands. 01779 while (!DeadOps.empty()) { 01780 unsigned OpIdx = DeadOps.back(); 01781 if (getOperand(OpIdx).isImplicit()) 01782 RemoveOperand(OpIdx); 01783 else 01784 getOperand(OpIdx).setIsKill(false); 01785 DeadOps.pop_back(); 01786 } 01787 01788 // If not found, this means an alias of one of the operands is killed. Add a 01789 // new implicit operand if required. 01790 if (!Found && AddIfNotFound) { 01791 addOperand(MachineOperand::CreateReg(IncomingReg, 01792 false /*IsDef*/, 01793 true /*IsImp*/, 01794 true /*IsKill*/)); 01795 return true; 01796 } 01797 return Found; 01798 } 01799 01800 void MachineInstr::clearRegisterKills(unsigned Reg, 01801 const TargetRegisterInfo *RegInfo) { 01802 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) 01803 RegInfo = nullptr; 01804 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01805 MachineOperand &MO = getOperand(i); 01806 if (!MO.isReg() || !MO.isUse() || !MO.isKill()) 01807 continue; 01808 unsigned OpReg = MO.getReg(); 01809 if (OpReg == Reg || (RegInfo && RegInfo->isSuperRegister(Reg, OpReg))) 01810 MO.setIsKill(false); 01811 } 01812 } 01813 01814 bool MachineInstr::addRegisterDead(unsigned Reg, 01815 const TargetRegisterInfo *RegInfo, 01816 bool AddIfNotFound) { 01817 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(Reg); 01818 bool hasAliases = isPhysReg && 01819 MCRegAliasIterator(Reg, RegInfo, false).isValid(); 01820 bool Found = false; 01821 SmallVector<unsigned,4> DeadOps; 01822 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01823 MachineOperand &MO = getOperand(i); 01824 if (!MO.isReg() || !MO.isDef()) 01825 continue; 01826 unsigned MOReg = MO.getReg(); 01827 if (!MOReg) 01828 continue; 01829 01830 if (MOReg == Reg) { 01831 MO.setIsDead(); 01832 Found = true; 01833 } else if (hasAliases && MO.isDead() && 01834 TargetRegisterInfo::isPhysicalRegister(MOReg)) { 01835 // There exists a super-register that's marked dead. 01836 if (RegInfo->isSuperRegister(Reg, MOReg)) 01837 return true; 01838 if (RegInfo->isSubRegister(Reg, MOReg)) 01839 DeadOps.push_back(i); 01840 } 01841 } 01842 01843 // Trim unneeded dead operands. 01844 while (!DeadOps.empty()) { 01845 unsigned OpIdx = DeadOps.back(); 01846 if (getOperand(OpIdx).isImplicit()) 01847 RemoveOperand(OpIdx); 01848 else 01849 getOperand(OpIdx).setIsDead(false); 01850 DeadOps.pop_back(); 01851 } 01852 01853 // If not found, this means an alias of one of the operands is dead. Add a 01854 // new implicit operand if required. 01855 if (Found || !AddIfNotFound) 01856 return Found; 01857 01858 addOperand(MachineOperand::CreateReg(Reg, 01859 true /*IsDef*/, 01860 true /*IsImp*/, 01861 false /*IsKill*/, 01862 true /*IsDead*/)); 01863 return true; 01864 } 01865 01866 void MachineInstr::addRegisterDefined(unsigned Reg, 01867 const TargetRegisterInfo *RegInfo) { 01868 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 01869 MachineOperand *MO = findRegisterDefOperand(Reg, false, RegInfo); 01870 if (MO) 01871 return; 01872 } else { 01873 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01874 const MachineOperand &MO = getOperand(i); 01875 if (MO.isReg() && MO.getReg() == Reg && MO.isDef() && 01876 MO.getSubReg() == 0) 01877 return; 01878 } 01879 } 01880 addOperand(MachineOperand::CreateReg(Reg, 01881 true /*IsDef*/, 01882 true /*IsImp*/)); 01883 } 01884 01885 void MachineInstr::setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs, 01886 const TargetRegisterInfo &TRI) { 01887 bool HasRegMask = false; 01888 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 01889 MachineOperand &MO = getOperand(i); 01890 if (MO.isRegMask()) { 01891 HasRegMask = true; 01892 continue; 01893 } 01894 if (!MO.isReg() || !MO.isDef()) continue; 01895 unsigned Reg = MO.getReg(); 01896 if (!TargetRegisterInfo::isPhysicalRegister(Reg)) continue; 01897 bool Dead = true; 01898 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end(); 01899 I != E; ++I) 01900 if (TRI.regsOverlap(*I, Reg)) { 01901 Dead = false; 01902 break; 01903 } 01904 // If there are no uses, including partial uses, the def is dead. 01905 if (Dead) MO.setIsDead(); 01906 } 01907 01908 // This is a call with a register mask operand. 01909 // Mask clobbers are always dead, so add defs for the non-dead defines. 01910 if (HasRegMask) 01911 for (ArrayRef<unsigned>::iterator I = UsedRegs.begin(), E = UsedRegs.end(); 01912 I != E; ++I) 01913 addRegisterDefined(*I, &TRI); 01914 } 01915 01916 unsigned 01917 MachineInstrExpressionTrait::getHashValue(const MachineInstr* const &MI) { 01918 // Build up a buffer of hash code components. 01919 SmallVector<size_t, 8> HashComponents; 01920 HashComponents.reserve(MI->getNumOperands() + 1); 01921 HashComponents.push_back(MI->getOpcode()); 01922 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 01923 const MachineOperand &MO = MI->getOperand(i); 01924 if (MO.isReg() && MO.isDef() && 01925 TargetRegisterInfo::isVirtualRegister(MO.getReg())) 01926 continue; // Skip virtual register defs. 01927 01928 HashComponents.push_back(hash_value(MO)); 01929 } 01930 return hash_combine_range(HashComponents.begin(), HashComponents.end()); 01931 } 01932 01933 void MachineInstr::emitError(StringRef Msg) const { 01934 // Find the source location cookie. 01935 unsigned LocCookie = 0; 01936 const MDNode *LocMD = nullptr; 01937 for (unsigned i = getNumOperands(); i != 0; --i) { 01938 if (getOperand(i-1).isMetadata() && 01939 (LocMD = getOperand(i-1).getMetadata()) && 01940 LocMD->getNumOperands() != 0) { 01941 if (const ConstantInt *CI = dyn_cast<ConstantInt>(LocMD->getOperand(0))) { 01942 LocCookie = CI->getZExtValue(); 01943 break; 01944 } 01945 } 01946 } 01947 01948 if (const MachineBasicBlock *MBB = getParent()) 01949 if (const MachineFunction *MF = MBB->getParent()) 01950 return MF->getMMI().getModule()->getContext().emitError(LocCookie, Msg); 01951 report_fatal_error(Msg); 01952 }