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/Constants.h" 00015 #include "llvm/CodeGen/MachineInstr.h" 00016 #include "llvm/Value.h" 00017 #include "llvm/CodeGen/MachineFunction.h" 00018 #include "llvm/CodeGen/MachineRegisterInfo.h" 00019 #include "llvm/CodeGen/PseudoSourceValue.h" 00020 #include "llvm/Target/TargetMachine.h" 00021 #include "llvm/Target/TargetInstrInfo.h" 00022 #include "llvm/Target/TargetInstrDesc.h" 00023 #include "llvm/Target/TargetRegisterInfo.h" 00024 #include "llvm/Support/LeakDetector.h" 00025 #include "llvm/Support/MathExtras.h" 00026 #include "llvm/Support/Streams.h" 00027 #include "llvm/Support/raw_ostream.h" 00028 #include "llvm/ADT/FoldingSet.h" 00029 #include <ostream> 00030 using namespace llvm; 00031 00032 //===----------------------------------------------------------------------===// 00033 // MachineOperand Implementation 00034 //===----------------------------------------------------------------------===// 00035 00036 /// AddRegOperandToRegInfo - Add this register operand to the specified 00037 /// MachineRegisterInfo. If it is null, then the next/prev fields should be 00038 /// explicitly nulled out. 00039 void MachineOperand::AddRegOperandToRegInfo(MachineRegisterInfo *RegInfo) { 00040 assert(isReg() && "Can only add reg operand to use lists"); 00041 00042 // If the reginfo pointer is null, just explicitly null out or next/prev 00043 // pointers, to ensure they are not garbage. 00044 if (RegInfo == 0) { 00045 Contents.Reg.Prev = 0; 00046 Contents.Reg.Next = 0; 00047 return; 00048 } 00049 00050 // Otherwise, add this operand to the head of the registers use/def list. 00051 MachineOperand **Head = &RegInfo->getRegUseDefListHead(getReg()); 00052 00053 // For SSA values, we prefer to keep the definition at the start of the list. 00054 // we do this by skipping over the definition if it is at the head of the 00055 // list. 00056 if (*Head && (*Head)->isDef()) 00057 Head = &(*Head)->Contents.Reg.Next; 00058 00059 Contents.Reg.Next = *Head; 00060 if (Contents.Reg.Next) { 00061 assert(getReg() == Contents.Reg.Next->getReg() && 00062 "Different regs on the same list!"); 00063 Contents.Reg.Next->Contents.Reg.Prev = &Contents.Reg.Next; 00064 } 00065 00066 Contents.Reg.Prev = Head; 00067 *Head = this; 00068 } 00069 00070 void MachineOperand::setReg(unsigned Reg) { 00071 if (getReg() == Reg) return; // No change. 00072 00073 // Otherwise, we have to change the register. If this operand is embedded 00074 // into a machine function, we need to update the old and new register's 00075 // use/def lists. 00076 if (MachineInstr *MI = getParent()) 00077 if (MachineBasicBlock *MBB = MI->getParent()) 00078 if (MachineFunction *MF = MBB->getParent()) { 00079 RemoveRegOperandFromRegInfo(); 00080 Contents.Reg.RegNo = Reg; 00081 AddRegOperandToRegInfo(&MF->getRegInfo()); 00082 return; 00083 } 00084 00085 // Otherwise, just change the register, no problem. :) 00086 Contents.Reg.RegNo = Reg; 00087 } 00088 00089 /// ChangeToImmediate - Replace this operand with a new immediate operand of 00090 /// the specified value. If an operand is known to be an immediate already, 00091 /// the setImm method should be used. 00092 void MachineOperand::ChangeToImmediate(int64_t ImmVal) { 00093 // If this operand is currently a register operand, and if this is in a 00094 // function, deregister the operand from the register's use/def list. 00095 if (isReg() && getParent() && getParent()->getParent() && 00096 getParent()->getParent()->getParent()) 00097 RemoveRegOperandFromRegInfo(); 00098 00099 OpKind = MO_Immediate; 00100 Contents.ImmVal = ImmVal; 00101 } 00102 00103 /// ChangeToRegister - Replace this operand with a new register operand of 00104 /// the specified value. If an operand is known to be an register already, 00105 /// the setReg method should be used. 00106 void MachineOperand::ChangeToRegister(unsigned Reg, bool isDef, bool isImp, 00107 bool isKill, bool isDead) { 00108 // If this operand is already a register operand, use setReg to update the 00109 // register's use/def lists. 00110 if (isReg()) { 00111 assert(!isEarlyClobber()); 00112 setReg(Reg); 00113 } else { 00114 // Otherwise, change this to a register and set the reg#. 00115 OpKind = MO_Register; 00116 Contents.Reg.RegNo = Reg; 00117 00118 // If this operand is embedded in a function, add the operand to the 00119 // register's use/def list. 00120 if (MachineInstr *MI = getParent()) 00121 if (MachineBasicBlock *MBB = MI->getParent()) 00122 if (MachineFunction *MF = MBB->getParent()) 00123 AddRegOperandToRegInfo(&MF->getRegInfo()); 00124 } 00125 00126 IsDef = isDef; 00127 IsImp = isImp; 00128 IsKill = isKill; 00129 IsDead = isDead; 00130 IsEarlyClobber = false; 00131 SubReg = 0; 00132 } 00133 00134 /// isIdenticalTo - Return true if this operand is identical to the specified 00135 /// operand. 00136 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 00137 if (getType() != Other.getType()) return false; 00138 00139 switch (getType()) { 00140 default: assert(0 && "Unrecognized operand type"); 00141 case MachineOperand::MO_Register: 00142 return getReg() == Other.getReg() && isDef() == Other.isDef() && 00143 getSubReg() == Other.getSubReg(); 00144 case MachineOperand::MO_Immediate: 00145 return getImm() == Other.getImm(); 00146 case MachineOperand::MO_FPImmediate: 00147 return getFPImm() == Other.getFPImm(); 00148 case MachineOperand::MO_MachineBasicBlock: 00149 return getMBB() == Other.getMBB(); 00150 case MachineOperand::MO_FrameIndex: 00151 return getIndex() == Other.getIndex(); 00152 case MachineOperand::MO_ConstantPoolIndex: 00153 return getIndex() == Other.getIndex() && getOffset() == Other.getOffset(); 00154 case MachineOperand::MO_JumpTableIndex: 00155 return getIndex() == Other.getIndex(); 00156 case MachineOperand::MO_GlobalAddress: 00157 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 00158 case MachineOperand::MO_ExternalSymbol: 00159 return !strcmp(getSymbolName(), Other.getSymbolName()) && 00160 getOffset() == Other.getOffset(); 00161 } 00162 } 00163 00164 /// print - Print the specified machine operand. 00165 /// 00166 void MachineOperand::print(std::ostream &OS, const TargetMachine *TM) const { 00167 raw_os_ostream RawOS(OS); 00168 print(RawOS, TM); 00169 } 00170 00171 void MachineOperand::print(raw_ostream &OS, const TargetMachine *TM) const { 00172 switch (getType()) { 00173 case MachineOperand::MO_Register: 00174 if (getReg() == 0 || TargetRegisterInfo::isVirtualRegister(getReg())) { 00175 OS << "%reg" << getReg(); 00176 } else { 00177 // If the instruction is embedded into a basic block, we can find the 00178 // target info for the instruction. 00179 if (TM == 0) 00180 if (const MachineInstr *MI = getParent()) 00181 if (const MachineBasicBlock *MBB = MI->getParent()) 00182 if (const MachineFunction *MF = MBB->getParent()) 00183 TM = &MF->getTarget(); 00184 00185 if (TM) 00186 OS << "%" << TM->getRegisterInfo()->get(getReg()).Name; 00187 else 00188 OS << "%mreg" << getReg(); 00189 } 00190 00191 if (getSubReg() != 0) { 00192 OS << ":" << getSubReg(); 00193 } 00194 00195 if (isDef() || isKill() || isDead() || isImplicit() || isEarlyClobber()) { 00196 OS << "<"; 00197 bool NeedComma = false; 00198 if (isImplicit()) { 00199 if (NeedComma) OS << ","; 00200 OS << (isDef() ? "imp-def" : "imp-use"); 00201 NeedComma = true; 00202 } else if (isDef()) { 00203 if (NeedComma) OS << ","; 00204 if (isEarlyClobber()) 00205 OS << "earlyclobber,"; 00206 OS << "def"; 00207 NeedComma = true; 00208 } 00209 if (isKill() || isDead()) { 00210 if (NeedComma) OS << ","; 00211 if (isKill()) OS << "kill"; 00212 if (isDead()) OS << "dead"; 00213 } 00214 OS << ">"; 00215 } 00216 break; 00217 case MachineOperand::MO_Immediate: 00218 OS << getImm(); 00219 break; 00220 case MachineOperand::MO_FPImmediate: 00221 if (getFPImm()->getType() == Type::FloatTy) { 00222 OS << getFPImm()->getValueAPF().convertToFloat(); 00223 } else { 00224 OS << getFPImm()->getValueAPF().convertToDouble(); 00225 } 00226 break; 00227 case MachineOperand::MO_MachineBasicBlock: 00228 OS << "mbb<" 00229 << ((Value*)getMBB()->getBasicBlock())->getName() 00230 << "," << (void*)getMBB() << ">"; 00231 break; 00232 case MachineOperand::MO_FrameIndex: 00233 OS << "<fi#" << getIndex() << ">"; 00234 break; 00235 case MachineOperand::MO_ConstantPoolIndex: 00236 OS << "<cp#" << getIndex(); 00237 if (getOffset()) OS << "+" << getOffset(); 00238 OS << ">"; 00239 break; 00240 case MachineOperand::MO_JumpTableIndex: 00241 OS << "<jt#" << getIndex() << ">"; 00242 break; 00243 case MachineOperand::MO_GlobalAddress: 00244 OS << "<ga:" << ((Value*)getGlobal())->getName(); 00245 if (getOffset()) OS << "+" << getOffset(); 00246 OS << ">"; 00247 break; 00248 case MachineOperand::MO_ExternalSymbol: 00249 OS << "<es:" << getSymbolName(); 00250 if (getOffset()) OS << "+" << getOffset(); 00251 OS << ">"; 00252 break; 00253 default: 00254 assert(0 && "Unrecognized operand type"); 00255 } 00256 } 00257 00258 //===----------------------------------------------------------------------===// 00259 // MachineMemOperand Implementation 00260 //===----------------------------------------------------------------------===// 00261 00262 MachineMemOperand::MachineMemOperand(const Value *v, unsigned int f, 00263 int64_t o, uint64_t s, unsigned int a) 00264 : Offset(o), Size(s), V(v), 00265 Flags((f & 7) | ((Log2_32(a) + 1) << 3)) { 00266 assert(isPowerOf2_32(a) && "Alignment is not a power of 2!"); 00267 assert((isLoad() || isStore()) && "Not a load/store!"); 00268 } 00269 00270 /// Profile - Gather unique data for the object. 00271 /// 00272 void MachineMemOperand::Profile(FoldingSetNodeID &ID) const { 00273 ID.AddInteger(Offset); 00274 ID.AddInteger(Size); 00275 ID.AddPointer(V); 00276 ID.AddInteger(Flags); 00277 } 00278 00279 //===----------------------------------------------------------------------===// 00280 // MachineInstr Implementation 00281 //===----------------------------------------------------------------------===// 00282 00283 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with 00284 /// TID NULL and no operands. 00285 MachineInstr::MachineInstr() 00286 : TID(0), NumImplicitOps(0), Parent(0), debugLoc(DebugLoc::getUnknownLoc()) { 00287 // Make sure that we get added to a machine basicblock 00288 LeakDetector::addGarbageObject(this); 00289 } 00290 00291 void MachineInstr::addImplicitDefUseOperands() { 00292 if (TID->ImplicitDefs) 00293 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) 00294 addOperand(MachineOperand::CreateReg(*ImpDefs, true, true)); 00295 if (TID->ImplicitUses) 00296 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) 00297 addOperand(MachineOperand::CreateReg(*ImpUses, false, true)); 00298 } 00299 00300 /// MachineInstr ctor - This constructor create a MachineInstr and add the 00301 /// implicit operands. It reserves space for number of operands specified by 00302 /// TargetInstrDesc or the numOperands if it is not zero. (for 00303 /// instructions with variable number of operands). 00304 MachineInstr::MachineInstr(const TargetInstrDesc &tid, bool NoImp) 00305 : TID(&tid), NumImplicitOps(0), Parent(0), 00306 debugLoc(DebugLoc::getUnknownLoc()) { 00307 if (!NoImp && TID->getImplicitDefs()) 00308 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 00309 NumImplicitOps++; 00310 if (!NoImp && TID->getImplicitUses()) 00311 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 00312 NumImplicitOps++; 00313 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 00314 if (!NoImp) 00315 addImplicitDefUseOperands(); 00316 // Make sure that we get added to a machine basicblock 00317 LeakDetector::addGarbageObject(this); 00318 } 00319 00320 /// MachineInstr ctor - As above, but with a DebugLoc. 00321 MachineInstr::MachineInstr(const TargetInstrDesc &tid, const DebugLoc dl, 00322 bool NoImp) 00323 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) { 00324 if (!NoImp && TID->getImplicitDefs()) 00325 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 00326 NumImplicitOps++; 00327 if (!NoImp && TID->getImplicitUses()) 00328 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 00329 NumImplicitOps++; 00330 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 00331 if (!NoImp) 00332 addImplicitDefUseOperands(); 00333 // Make sure that we get added to a machine basicblock 00334 LeakDetector::addGarbageObject(this); 00335 } 00336 00337 /// MachineInstr ctor - Work exactly the same as the ctor two above, except 00338 /// that the MachineInstr is created and added to the end of the specified 00339 /// basic block. 00340 /// 00341 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const TargetInstrDesc &tid) 00342 : TID(&tid), NumImplicitOps(0), Parent(0), 00343 debugLoc(DebugLoc::getUnknownLoc()) { 00344 assert(MBB && "Cannot use inserting ctor with null basic block!"); 00345 if (TID->ImplicitDefs) 00346 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 00347 NumImplicitOps++; 00348 if (TID->ImplicitUses) 00349 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 00350 NumImplicitOps++; 00351 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 00352 addImplicitDefUseOperands(); 00353 // Make sure that we get added to a machine basicblock 00354 LeakDetector::addGarbageObject(this); 00355 MBB->push_back(this); // Add instruction to end of basic block! 00356 } 00357 00358 /// MachineInstr ctor - As above, but with a DebugLoc. 00359 /// 00360 MachineInstr::MachineInstr(MachineBasicBlock *MBB, const DebugLoc dl, 00361 const TargetInstrDesc &tid) 00362 : TID(&tid), NumImplicitOps(0), Parent(0), debugLoc(dl) { 00363 assert(MBB && "Cannot use inserting ctor with null basic block!"); 00364 if (TID->ImplicitDefs) 00365 for (const unsigned *ImpDefs = TID->getImplicitDefs(); *ImpDefs; ++ImpDefs) 00366 NumImplicitOps++; 00367 if (TID->ImplicitUses) 00368 for (const unsigned *ImpUses = TID->getImplicitUses(); *ImpUses; ++ImpUses) 00369 NumImplicitOps++; 00370 Operands.reserve(NumImplicitOps + TID->getNumOperands()); 00371 addImplicitDefUseOperands(); 00372 // Make sure that we get added to a machine basicblock 00373 LeakDetector::addGarbageObject(this); 00374 MBB->push_back(this); // Add instruction to end of basic block! 00375 } 00376 00377 /// MachineInstr ctor - Copies MachineInstr arg exactly 00378 /// 00379 MachineInstr::MachineInstr(MachineFunction &MF, const MachineInstr &MI) 00380 : TID(&MI.getDesc()), NumImplicitOps(0), Parent(0), 00381 debugLoc(MI.getDebugLoc()) { 00382 Operands.reserve(MI.getNumOperands()); 00383 00384 // Add operands 00385 for (unsigned i = 0; i != MI.getNumOperands(); ++i) 00386 addOperand(MI.getOperand(i)); 00387 NumImplicitOps = MI.NumImplicitOps; 00388 00389 // Add memory operands. 00390 for (std::list<MachineMemOperand>::const_iterator i = MI.memoperands_begin(), 00391 j = MI.memoperands_end(); i != j; ++i) 00392 addMemOperand(MF, *i); 00393 00394 // Set parent to null. 00395 Parent = 0; 00396 00397 LeakDetector::addGarbageObject(this); 00398 } 00399 00400 MachineInstr::~MachineInstr() { 00401 LeakDetector::removeGarbageObject(this); 00402 assert(MemOperands.empty() && 00403 "MachineInstr being deleted with live memoperands!"); 00404 #ifndef NDEBUG 00405 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 00406 assert(Operands[i].ParentMI == this && "ParentMI mismatch!"); 00407 assert((!Operands[i].isReg() || !Operands[i].isOnRegUseList()) && 00408 "Reg operand def/use list corrupted"); 00409 } 00410 #endif 00411 } 00412 00413 /// getRegInfo - If this instruction is embedded into a MachineFunction, 00414 /// return the MachineRegisterInfo object for the current function, otherwise 00415 /// return null. 00416 MachineRegisterInfo *MachineInstr::getRegInfo() { 00417 if (MachineBasicBlock *MBB = getParent()) 00418 return &MBB->getParent()->getRegInfo(); 00419 return 0; 00420 } 00421 00422 /// RemoveRegOperandsFromUseLists - Unlink all of the register operands in 00423 /// this instruction from their respective use lists. This requires that the 00424 /// operands already be on their use lists. 00425 void MachineInstr::RemoveRegOperandsFromUseLists() { 00426 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 00427 if (Operands[i].isReg()) 00428 Operands[i].RemoveRegOperandFromRegInfo(); 00429 } 00430 } 00431 00432 /// AddRegOperandsToUseLists - Add all of the register operands in 00433 /// this instruction from their respective use lists. This requires that the 00434 /// operands not be on their use lists yet. 00435 void MachineInstr::AddRegOperandsToUseLists(MachineRegisterInfo &RegInfo) { 00436 for (unsigned i = 0, e = Operands.size(); i != e; ++i) { 00437 if (Operands[i].isReg()) 00438 Operands[i].AddRegOperandToRegInfo(&RegInfo); 00439 } 00440 } 00441 00442 00443 /// addOperand - Add the specified operand to the instruction. If it is an 00444 /// implicit operand, it is added to the end of the operand list. If it is 00445 /// an explicit operand it is added at the end of the explicit operand list 00446 /// (before the first implicit operand). 00447 void MachineInstr::addOperand(const MachineOperand &Op) { 00448 bool isImpReg = Op.isReg() && Op.isImplicit(); 00449 assert((isImpReg || !OperandsComplete()) && 00450 "Trying to add an operand to a machine instr that is already done!"); 00451 00452 MachineRegisterInfo *RegInfo = getRegInfo(); 00453 00454 // If we are adding the operand to the end of the list, our job is simpler. 00455 // This is true most of the time, so this is a reasonable optimization. 00456 if (isImpReg || NumImplicitOps == 0) { 00457 // We can only do this optimization if we know that the operand list won't 00458 // reallocate. 00459 if (Operands.empty() || Operands.size()+1 <= Operands.capacity()) { 00460 Operands.push_back(Op); 00461 00462 // Set the parent of the operand. 00463 Operands.back().ParentMI = this; 00464 00465 // If the operand is a register, update the operand's use list. 00466 if (Op.isReg()) 00467 Operands.back().AddRegOperandToRegInfo(RegInfo); 00468 return; 00469 } 00470 } 00471 00472 // Otherwise, we have to insert a real operand before any implicit ones. 00473 unsigned OpNo = Operands.size()-NumImplicitOps; 00474 00475 // If this instruction isn't embedded into a function, then we don't need to 00476 // update any operand lists. 00477 if (RegInfo == 0) { 00478 // Simple insertion, no reginfo update needed for other register operands. 00479 Operands.insert(Operands.begin()+OpNo, Op); 00480 Operands[OpNo].ParentMI = this; 00481 00482 // Do explicitly set the reginfo for this operand though, to ensure the 00483 // next/prev fields are properly nulled out. 00484 if (Operands[OpNo].isReg()) 00485 Operands[OpNo].AddRegOperandToRegInfo(0); 00486 00487 } else if (Operands.size()+1 <= Operands.capacity()) { 00488 // Otherwise, we have to remove register operands from their register use 00489 // list, add the operand, then add the register operands back to their use 00490 // list. This also must handle the case when the operand list reallocates 00491 // to somewhere else. 00492 00493 // If insertion of this operand won't cause reallocation of the operand 00494 // list, just remove the implicit operands, add the operand, then re-add all 00495 // the rest of the operands. 00496 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 00497 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 00498 Operands[i].RemoveRegOperandFromRegInfo(); 00499 } 00500 00501 // Add the operand. If it is a register, add it to the reg list. 00502 Operands.insert(Operands.begin()+OpNo, Op); 00503 Operands[OpNo].ParentMI = this; 00504 00505 if (Operands[OpNo].isReg()) 00506 Operands[OpNo].AddRegOperandToRegInfo(RegInfo); 00507 00508 // Re-add all the implicit ops. 00509 for (unsigned i = OpNo+1, e = Operands.size(); i != e; ++i) { 00510 assert(Operands[i].isReg() && "Should only be an implicit reg!"); 00511 Operands[i].AddRegOperandToRegInfo(RegInfo); 00512 } 00513 } else { 00514 // Otherwise, we will be reallocating the operand list. Remove all reg 00515 // operands from their list, then readd them after the operand list is 00516 // reallocated. 00517 RemoveRegOperandsFromUseLists(); 00518 00519 Operands.insert(Operands.begin()+OpNo, Op); 00520 Operands[OpNo].ParentMI = this; 00521 00522 // Re-add all the operands. 00523 AddRegOperandsToUseLists(*RegInfo); 00524 } 00525 } 00526 00527 /// RemoveOperand - Erase an operand from an instruction, leaving it with one 00528 /// fewer operand than it started with. 00529 /// 00530 void MachineInstr::RemoveOperand(unsigned OpNo) { 00531 assert(OpNo < Operands.size() && "Invalid operand number"); 00532 00533 // Special case removing the last one. 00534 if (OpNo == Operands.size()-1) { 00535 // If needed, remove from the reg def/use list. 00536 if (Operands.back().isReg() && Operands.back().isOnRegUseList()) 00537 Operands.back().RemoveRegOperandFromRegInfo(); 00538 00539 Operands.pop_back(); 00540 return; 00541 } 00542 00543 // Otherwise, we are removing an interior operand. If we have reginfo to 00544 // update, remove all operands that will be shifted down from their reg lists, 00545 // move everything down, then re-add them. 00546 MachineRegisterInfo *RegInfo = getRegInfo(); 00547 if (RegInfo) { 00548 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 00549 if (Operands[i].isReg()) 00550 Operands[i].RemoveRegOperandFromRegInfo(); 00551 } 00552 } 00553 00554 Operands.erase(Operands.begin()+OpNo); 00555 00556 if (RegInfo) { 00557 for (unsigned i = OpNo, e = Operands.size(); i != e; ++i) { 00558 if (Operands[i].isReg()) 00559 Operands[i].AddRegOperandToRegInfo(RegInfo); 00560 } 00561 } 00562 } 00563 00564 /// addMemOperand - Add a MachineMemOperand to the machine instruction, 00565 /// referencing arbitrary storage. 00566 void MachineInstr::addMemOperand(MachineFunction &MF, 00567 const MachineMemOperand &MO) { 00568 MemOperands.push_back(MO); 00569 } 00570 00571 /// clearMemOperands - Erase all of this MachineInstr's MachineMemOperands. 00572 void MachineInstr::clearMemOperands(MachineFunction &MF) { 00573 MemOperands.clear(); 00574 } 00575 00576 00577 /// removeFromParent - This method unlinks 'this' from the containing basic 00578 /// block, and returns it, but does not delete it. 00579 MachineInstr *MachineInstr::removeFromParent() { 00580 assert(getParent() && "Not embedded in a basic block!"); 00581 getParent()->remove(this); 00582 return this; 00583 } 00584 00585 00586 /// eraseFromParent - This method unlinks 'this' from the containing basic 00587 /// block, and deletes it. 00588 void MachineInstr::eraseFromParent() { 00589 assert(getParent() && "Not embedded in a basic block!"); 00590 getParent()->erase(this); 00591 } 00592 00593 00594 /// OperandComplete - Return true if it's illegal to add a new operand 00595 /// 00596 bool MachineInstr::OperandsComplete() const { 00597 unsigned short NumOperands = TID->getNumOperands(); 00598 if (!TID->isVariadic() && getNumOperands()-NumImplicitOps >= NumOperands) 00599 return true; // Broken: we have all the operands of this instruction! 00600 return false; 00601 } 00602 00603 /// getNumExplicitOperands - Returns the number of non-implicit operands. 00604 /// 00605 unsigned MachineInstr::getNumExplicitOperands() const { 00606 unsigned NumOperands = TID->getNumOperands(); 00607 if (!TID->isVariadic()) 00608 return NumOperands; 00609 00610 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) { 00611 const MachineOperand &MO = getOperand(NumOperands); 00612 if (!MO.isReg() || !MO.isImplicit()) 00613 NumOperands++; 00614 } 00615 return NumOperands; 00616 } 00617 00618 00619 /// isLabel - Returns true if the MachineInstr represents a label. 00620 /// 00621 bool MachineInstr::isLabel() const { 00622 return getOpcode() == TargetInstrInfo::DBG_LABEL || 00623 getOpcode() == TargetInstrInfo::EH_LABEL || 00624 getOpcode() == TargetInstrInfo::GC_LABEL; 00625 } 00626 00627 /// isDebugLabel - Returns true if the MachineInstr represents a debug label. 00628 /// 00629 bool MachineInstr::isDebugLabel() const { 00630 return getOpcode() == TargetInstrInfo::DBG_LABEL; 00631 } 00632 00633 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of 00634 /// the specific register or -1 if it is not found. It further tightening 00635 /// the search criteria to a use that kills the register if isKill is true. 00636 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill, 00637 const TargetRegisterInfo *TRI) const { 00638 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 00639 const MachineOperand &MO = getOperand(i); 00640 if (!MO.isReg() || !MO.isUse()) 00641 continue; 00642 unsigned MOReg = MO.getReg(); 00643 if (!MOReg) 00644 continue; 00645 if (MOReg == Reg || 00646 (TRI && 00647 TargetRegisterInfo::isPhysicalRegister(MOReg) && 00648 TargetRegisterInfo::isPhysicalRegister(Reg) && 00649 TRI->isSubRegister(MOReg, Reg))) 00650 if (!isKill || MO.isKill()) 00651 return i; 00652 } 00653 return -1; 00654 } 00655 00656 /// findRegisterDefOperandIdx() - Returns the operand index that is a def of 00657 /// the specified register or -1 if it is not found. If isDead is true, defs 00658 /// that are not dead are skipped. If TargetRegisterInfo is non-null, then it 00659 /// also checks if there is a def of a super-register. 00660 int MachineInstr::findRegisterDefOperandIdx(unsigned Reg, bool isDead, 00661 const TargetRegisterInfo *TRI) const { 00662 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 00663 const MachineOperand &MO = getOperand(i); 00664 if (!MO.isReg() || !MO.isDef()) 00665 continue; 00666 unsigned MOReg = MO.getReg(); 00667 if (MOReg == Reg || 00668 (TRI && 00669 TargetRegisterInfo::isPhysicalRegister(MOReg) && 00670 TargetRegisterInfo::isPhysicalRegister(Reg) && 00671 TRI->isSubRegister(MOReg, Reg))) 00672 if (!isDead || MO.isDead()) 00673 return i; 00674 } 00675 return -1; 00676 } 00677 00678 /// findFirstPredOperandIdx() - Find the index of the first operand in the 00679 /// operand list that is used to represent the predicate. It returns -1 if 00680 /// none is found. 00681 int MachineInstr::findFirstPredOperandIdx() const { 00682 const TargetInstrDesc &TID = getDesc(); 00683 if (TID.isPredicable()) { 00684 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 00685 if (TID.OpInfo[i].isPredicate()) 00686 return i; 00687 } 00688 00689 return -1; 00690 } 00691 00692 /// isRegReDefinedByTwoAddr - Given the index of a register def operand, 00693 /// check if the register def is a re-definition due to two addr elimination. 00694 bool MachineInstr::isRegReDefinedByTwoAddr(unsigned DefIdx) const{ 00695 assert(getOperand(DefIdx).isDef() && "DefIdx is not a def!"); 00696 const TargetInstrDesc &TID = getDesc(); 00697 for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { 00698 const MachineOperand &MO = getOperand(i); 00699 if (MO.isReg() && MO.isUse() && 00700 TID.getOperandConstraint(i, TOI::TIED_TO) == (int)DefIdx) 00701 return true; 00702 } 00703 return false; 00704 } 00705 00706 /// copyKillDeadInfo - Copies kill / dead operand properties from MI. 00707 /// 00708 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { 00709 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00710 const MachineOperand &MO = MI->getOperand(i); 00711 if (!MO.isReg() || (!MO.isKill() && !MO.isDead())) 00712 continue; 00713 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { 00714 MachineOperand &MOp = getOperand(j); 00715 if (!MOp.isIdenticalTo(MO)) 00716 continue; 00717 if (MO.isKill()) 00718 MOp.setIsKill(); 00719 else 00720 MOp.setIsDead(); 00721 break; 00722 } 00723 } 00724 } 00725 00726 /// copyPredicates - Copies predicate operand(s) from MI. 00727 void MachineInstr::copyPredicates(const MachineInstr *MI) { 00728 const TargetInstrDesc &TID = MI->getDesc(); 00729 if (!TID.isPredicable()) 00730 return; 00731 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00732 if (TID.OpInfo[i].isPredicate()) { 00733 // Predicated operands must be last operands. 00734 addOperand(MI->getOperand(i)); 00735 } 00736 } 00737 } 00738 00739 /// isSafeToMove - Return true if it is safe to move this instruction. If 00740 /// SawStore is set to true, it means that there is a store (or call) between 00741 /// the instruction's location and its intended destination. 00742 bool MachineInstr::isSafeToMove(const TargetInstrInfo *TII, 00743 bool &SawStore) const { 00744 // Ignore stuff that we obviously can't move. 00745 if (TID->mayStore() || TID->isCall()) { 00746 SawStore = true; 00747 return false; 00748 } 00749 if (TID->isTerminator() || TID->hasUnmodeledSideEffects()) 00750 return false; 00751 00752 // See if this instruction does a load. If so, we have to guarantee that the 00753 // loaded value doesn't change between the load and the its intended 00754 // destination. The check for isInvariantLoad gives the targe the chance to 00755 // classify the load as always returning a constant, e.g. a constant pool 00756 // load. 00757 if (TID->mayLoad() && !TII->isInvariantLoad(this)) 00758 // Otherwise, this is a real load. If there is a store between the load and 00759 // end of block, or if the laod is volatile, we can't move it. 00760 return !SawStore && !hasVolatileMemoryRef(); 00761 00762 return true; 00763 } 00764 00765 /// isSafeToReMat - Return true if it's safe to rematerialize the specified 00766 /// instruction which defined the specified register instead of copying it. 00767 bool MachineInstr::isSafeToReMat(const TargetInstrInfo *TII, 00768 unsigned DstReg) const { 00769 bool SawStore = false; 00770 if (!getDesc().isRematerializable() || 00771 !TII->isTriviallyReMaterializable(this) || 00772 !isSafeToMove(TII, SawStore)) 00773 return false; 00774 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 00775 const MachineOperand &MO = getOperand(i); 00776 if (!MO.isReg()) 00777 continue; 00778 // FIXME: For now, do not remat any instruction with register operands. 00779 // Later on, we can loosen the restriction is the register operands have 00780 // not been modified between the def and use. Note, this is different from 00781 // MachineSink because the code is no longer in two-address form (at least 00782 // partially). 00783 if (MO.isUse()) 00784 return false; 00785 else if (!MO.isDead() && MO.getReg() != DstReg) 00786 return false; 00787 } 00788 return true; 00789 } 00790 00791 /// hasVolatileMemoryRef - Return true if this instruction may have a 00792 /// volatile memory reference, or if the information describing the 00793 /// memory reference is not available. Return false if it is known to 00794 /// have no volatile memory references. 00795 bool MachineInstr::hasVolatileMemoryRef() const { 00796 // An instruction known never to access memory won't have a volatile access. 00797 if (!TID->mayStore() && 00798 !TID->mayLoad() && 00799 !TID->isCall() && 00800 !TID->hasUnmodeledSideEffects()) 00801 return false; 00802 00803 // Otherwise, if the instruction has no memory reference information, 00804 // conservatively assume it wasn't preserved. 00805 if (memoperands_empty()) 00806 return true; 00807 00808 // Check the memory reference information for volatile references. 00809 for (std::list<MachineMemOperand>::const_iterator I = memoperands_begin(), 00810 E = memoperands_end(); I != E; ++I) 00811 if (I->isVolatile()) 00812 return true; 00813 00814 return false; 00815 } 00816 00817 void MachineInstr::dump() const { 00818 cerr << " " << *this; 00819 } 00820 00821 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const { 00822 raw_os_ostream RawOS(OS); 00823 print(RawOS, TM); 00824 } 00825 00826 void MachineInstr::print(raw_ostream &OS, const TargetMachine *TM) const { 00827 // Specialize printing if op#0 is definition 00828 unsigned StartOp = 0; 00829 if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) { 00830 getOperand(0).print(OS, TM); 00831 OS << " = "; 00832 ++StartOp; // Don't print this operand again! 00833 } 00834 00835 OS << getDesc().getName(); 00836 00837 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 00838 if (i != StartOp) 00839 OS << ","; 00840 OS << " "; 00841 getOperand(i).print(OS, TM); 00842 } 00843 00844 if (!memoperands_empty()) { 00845 OS << ", Mem:"; 00846 for (std::list<MachineMemOperand>::const_iterator i = memoperands_begin(), 00847 e = memoperands_end(); i != e; ++i) { 00848 const MachineMemOperand &MRO = *i; 00849 const Value *V = MRO.getValue(); 00850 00851 assert((MRO.isLoad() || MRO.isStore()) && 00852 "SV has to be a load, store or both."); 00853 00854 if (MRO.isVolatile()) 00855 OS << "Volatile "; 00856 00857 if (MRO.isLoad()) 00858 OS << "LD"; 00859 if (MRO.isStore()) 00860 OS << "ST"; 00861 00862 OS << "(" << MRO.getSize() << "," << MRO.getAlignment() << ") ["; 00863 00864 if (!V) 00865 OS << "<unknown>"; 00866 else if (!V->getName().empty()) 00867 OS << V->getName(); 00868 else if (const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V)) { 00869 PSV->print(OS); 00870 } else 00871 OS << V; 00872 00873 OS << " + " << MRO.getOffset() << "]"; 00874 } 00875 } 00876 00877 OS << "\n"; 00878 } 00879 00880 bool MachineInstr::addRegisterKilled(unsigned IncomingReg, 00881 const TargetRegisterInfo *RegInfo, 00882 bool AddIfNotFound) { 00883 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 00884 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 00885 bool Found = false; 00886 SmallVector<unsigned,4> DeadOps; 00887 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 00888 MachineOperand &MO = getOperand(i); 00889 if (!MO.isReg() || !MO.isUse()) 00890 continue; 00891 unsigned Reg = MO.getReg(); 00892 if (!Reg) 00893 continue; 00894 00895 if (Reg == IncomingReg) { 00896 if (!Found) { 00897 if (MO.isKill()) 00898 // The register is already marked kill. 00899 return true; 00900 MO.setIsKill(); 00901 Found = true; 00902 } 00903 } else if (hasAliases && MO.isKill() && 00904 TargetRegisterInfo::isPhysicalRegister(Reg)) { 00905 // A super-register kill already exists. 00906 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 00907 return true; 00908 if (RegInfo->isSubRegister(IncomingReg, Reg)) 00909 DeadOps.push_back(i); 00910 } 00911 } 00912 00913 // Trim unneeded kill operands. 00914 while (!DeadOps.empty()) { 00915 unsigned OpIdx = DeadOps.back(); 00916 if (getOperand(OpIdx).isImplicit()) 00917 RemoveOperand(OpIdx); 00918 else 00919 getOperand(OpIdx).setIsKill(false); 00920 DeadOps.pop_back(); 00921 } 00922 00923 // If not found, this means an alias of one of the operands is killed. Add a 00924 // new implicit operand if required. 00925 if (!Found && AddIfNotFound) { 00926 addOperand(MachineOperand::CreateReg(IncomingReg, 00927 false /*IsDef*/, 00928 true /*IsImp*/, 00929 true /*IsKill*/)); 00930 return true; 00931 } 00932 return Found; 00933 } 00934 00935 bool MachineInstr::addRegisterDead(unsigned IncomingReg, 00936 const TargetRegisterInfo *RegInfo, 00937 bool AddIfNotFound) { 00938 bool isPhysReg = TargetRegisterInfo::isPhysicalRegister(IncomingReg); 00939 bool hasAliases = isPhysReg && RegInfo->getAliasSet(IncomingReg); 00940 bool Found = false; 00941 SmallVector<unsigned,4> DeadOps; 00942 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 00943 MachineOperand &MO = getOperand(i); 00944 if (!MO.isReg() || !MO.isDef()) 00945 continue; 00946 unsigned Reg = MO.getReg(); 00947 if (!Reg) 00948 continue; 00949 00950 if (Reg == IncomingReg) { 00951 if (!Found) { 00952 if (MO.isDead()) 00953 // The register is already marked dead. 00954 return true; 00955 MO.setIsDead(); 00956 Found = true; 00957 } 00958 } else if (hasAliases && MO.isDead() && 00959 TargetRegisterInfo::isPhysicalRegister(Reg)) { 00960 // There exists a super-register that's marked dead. 00961 if (RegInfo->isSuperRegister(IncomingReg, Reg)) 00962 return true; 00963 if (RegInfo->getSubRegisters(IncomingReg) && 00964 RegInfo->getSuperRegisters(Reg) && 00965 RegInfo->isSubRegister(IncomingReg, Reg)) 00966 DeadOps.push_back(i); 00967 } 00968 } 00969 00970 // Trim unneeded dead operands. 00971 while (!DeadOps.empty()) { 00972 unsigned OpIdx = DeadOps.back(); 00973 if (getOperand(OpIdx).isImplicit()) 00974 RemoveOperand(OpIdx); 00975 else 00976 getOperand(OpIdx).setIsDead(false); 00977 DeadOps.pop_back(); 00978 } 00979 00980 // If not found, this means an alias of one of the operands is dead. Add a 00981 // new implicit operand if required. 00982 if (!Found && AddIfNotFound) { 00983 addOperand(MachineOperand::CreateReg(IncomingReg, 00984 true /*IsDef*/, 00985 true /*IsImp*/, 00986 false /*IsKill*/, 00987 true /*IsDead*/)); 00988 return true; 00989 } 00990 return Found; 00991 }