1 //===-- MachineInstr.cpp --------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Methods common to all machine instructions. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/CodeGen/MachineInstr.h" 15 #include "llvm/CodeGen/MachineFunction.h" 16 #include "llvm/Target/TargetMachine.h" 17 #include "llvm/Target/TargetInstrInfo.h" 18 #include "llvm/Target/MRegisterInfo.h" 19 #include "llvm/Support/LeakDetector.h" 20 #include "llvm/Support/Streams.h" 21 #include <ostream> 22 using namespace llvm; 23 24 /// MachineInstr ctor - This constructor creates a dummy MachineInstr with 25 /// TID NULL and no operands. 26 MachineInstr::MachineInstr() 27 : TID(0), NumImplicitOps(0), parent(0) { 28 // Make sure that we get added to a machine basicblock 29 LeakDetector::addGarbageObject(this); 30 } 31 32 void MachineInstr::addImplicitDefUseOperands() { 33 if (TID->ImplicitDefs) 34 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) 35 addRegOperand(*ImpDefs, true, true); 36 if (TID->ImplicitUses) 37 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) 38 addRegOperand(*ImpUses, false, true); 39 } 40 41 /// MachineInstr ctor - This constructor create a MachineInstr and add the 42 /// implicit operands. It reserves space for number of operands specified by 43 /// TargetInstrDescriptor or the numOperands if it is not zero. (for 44 /// instructions with variable number of operands). 45 MachineInstr::MachineInstr(const TargetInstrDescriptor &tid, bool NoImp) 46 : TID(&tid), NumImplicitOps(0), parent(0) { 47 if (!NoImp && TID->ImplicitDefs) 48 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) 49 NumImplicitOps++; 50 if (!NoImp && TID->ImplicitUses) 51 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) 52 NumImplicitOps++; 53 Operands.reserve(NumImplicitOps + TID->numOperands); 54 if (!NoImp) 55 addImplicitDefUseOperands(); 56 // Make sure that we get added to a machine basicblock 57 LeakDetector::addGarbageObject(this); 58 } 59 60 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the 61 /// MachineInstr is created and added to the end of the specified basic block. 62 /// 63 MachineInstr::MachineInstr(MachineBasicBlock *MBB, 64 const TargetInstrDescriptor &tid) 65 : TID(&tid), NumImplicitOps(0), parent(0) { 66 assert(MBB && "Cannot use inserting ctor with null basic block!"); 67 if (TID->ImplicitDefs) 68 for (const unsigned *ImpDefs = TID->ImplicitDefs; *ImpDefs; ++ImpDefs) 69 NumImplicitOps++; 70 if (TID->ImplicitUses) 71 for (const unsigned *ImpUses = TID->ImplicitUses; *ImpUses; ++ImpUses) 72 NumImplicitOps++; 73 Operands.reserve(NumImplicitOps + TID->numOperands); 74 addImplicitDefUseOperands(); 75 // Make sure that we get added to a machine basicblock 76 LeakDetector::addGarbageObject(this); 77 MBB->push_back(this); // Add instruction to end of basic block! 78 } 79 80 /// MachineInstr ctor - Copies MachineInstr arg exactly 81 /// 82 MachineInstr::MachineInstr(const MachineInstr &MI) { 83 TID = MI.getInstrDescriptor(); 84 NumImplicitOps = MI.NumImplicitOps; 85 Operands.reserve(MI.getNumOperands()); 86 87 // Add operands 88 for (unsigned i = 0; i != MI.getNumOperands(); ++i) 89 Operands.push_back(MI.getOperand(i)); 90 91 // Set parent, next, and prev to null 92 parent = 0; 93 prev = 0; 94 next = 0; 95 } 96 97 98 MachineInstr::~MachineInstr() { 99 LeakDetector::removeGarbageObject(this); 100 } 101 102 /// getOpcode - Returns the opcode of this MachineInstr. 103 /// 104 int MachineInstr::getOpcode() const { 105 return TID->Opcode; 106 } 107 108 /// removeFromParent - This method unlinks 'this' from the containing basic 109 /// block, and returns it, but does not delete it. 110 MachineInstr *MachineInstr::removeFromParent() { 111 assert(getParent() && "Not embedded in a basic block!"); 112 getParent()->remove(this); 113 return this; 114 } 115 116 117 /// OperandComplete - Return true if it's illegal to add a new operand 118 /// 119 bool MachineInstr::OperandsComplete() const { 120 unsigned short NumOperands = TID->numOperands; 121 if ((TID->Flags & M_VARIABLE_OPS) == 0 && 122 getNumOperands()-NumImplicitOps >= NumOperands) 123 return true; // Broken: we have all the operands of this instruction! 124 return false; 125 } 126 127 /// getNumExplicitOperands - Returns the number of non-implicit operands. 128 /// 129 unsigned MachineInstr::getNumExplicitOperands() const { 130 unsigned NumOperands = TID->numOperands; 131 if ((TID->Flags & M_VARIABLE_OPS) == 0) 132 return NumOperands; 133 134 for (unsigned e = getNumOperands(); NumOperands != e; ++NumOperands) { 135 const MachineOperand &MO = getOperand(NumOperands); 136 if (!MO.isRegister() || !MO.isImplicit()) 137 NumOperands++; 138 } 139 return NumOperands; 140 } 141 142 /// isIdenticalTo - Return true if this operand is identical to the specified 143 /// operand. 144 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const { 145 if (getType() != Other.getType()) return false; 146 147 switch (getType()) { 148 default: assert(0 && "Unrecognized operand type"); 149 case MachineOperand::MO_Register: 150 return getReg() == Other.getReg() && isDef() == Other.isDef(); 151 case MachineOperand::MO_Immediate: 152 return getImm() == Other.getImm(); 153 case MachineOperand::MO_MachineBasicBlock: 154 return getMBB() == Other.getMBB(); 155 case MachineOperand::MO_FrameIndex: 156 return getFrameIndex() == Other.getFrameIndex(); 157 case MachineOperand::MO_ConstantPoolIndex: 158 return getConstantPoolIndex() == Other.getConstantPoolIndex() && 159 getOffset() == Other.getOffset(); 160 case MachineOperand::MO_JumpTableIndex: 161 return getJumpTableIndex() == Other.getJumpTableIndex(); 162 case MachineOperand::MO_GlobalAddress: 163 return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset(); 164 case MachineOperand::MO_ExternalSymbol: 165 return !strcmp(getSymbolName(), Other.getSymbolName()) && 166 getOffset() == Other.getOffset(); 167 } 168 } 169 170 /// findRegisterUseOperandIdx() - Returns the MachineOperand that is a use of 171 /// the specific register or -1 if it is not found. It further tightening 172 /// the search criteria to a use that kills the register if isKill is true. 173 int MachineInstr::findRegisterUseOperandIdx(unsigned Reg, bool isKill) const { 174 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 175 const MachineOperand &MO = getOperand(i); 176 if (MO.isRegister() && MO.isUse() && MO.getReg() == Reg) 177 if (!isKill || MO.isKill()) 178 return i; 179 } 180 return -1; 181 } 182 183 /// findRegisterDefOperand() - Returns the MachineOperand that is a def of 184 /// the specific register or NULL if it is not found. 185 MachineOperand *MachineInstr::findRegisterDefOperand(unsigned Reg) { 186 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 187 MachineOperand &MO = getOperand(i); 188 if (MO.isRegister() && MO.isDef() && MO.getReg() == Reg) 189 return &MO; 190 } 191 return NULL; 192 } 193 194 /// findFirstPredOperandIdx() - Find the index of the first operand in the 195 /// operand list that is used to represent the predicate. It returns -1 if 196 /// none is found. 197 int MachineInstr::findFirstPredOperandIdx() const { 198 const TargetInstrDescriptor *TID = getInstrDescriptor(); 199 if (TID->Flags & M_PREDICABLE) { 200 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 201 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) 202 return i; 203 } 204 205 return -1; 206 } 207 208 /// isRegReDefinedByTwoAddr - Returns true if the Reg re-definition is due 209 /// to two addr elimination. 210 bool MachineInstr::isRegReDefinedByTwoAddr(unsigned Reg) const { 211 const TargetInstrDescriptor *TID = getInstrDescriptor(); 212 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 213 const MachineOperand &MO1 = getOperand(i); 214 if (MO1.isRegister() && MO1.isDef() && MO1.getReg() == Reg) { 215 for (unsigned j = i+1; j < e; ++j) { 216 const MachineOperand &MO2 = getOperand(j); 217 if (MO2.isRegister() && MO2.isUse() && MO2.getReg() == Reg && 218 TID->getOperandConstraint(j, TOI::TIED_TO) == (int)i) 219 return true; 220 } 221 } 222 } 223 return false; 224 } 225 226 /// copyKillDeadInfo - Copies kill / dead operand properties from MI. 227 /// 228 void MachineInstr::copyKillDeadInfo(const MachineInstr *MI) { 229 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 230 const MachineOperand &MO = MI->getOperand(i); 231 if (!MO.isRegister() || (!MO.isKill() && !MO.isDead())) 232 continue; 233 for (unsigned j = 0, ee = getNumOperands(); j != ee; ++j) { 234 MachineOperand &MOp = getOperand(j); 235 if (!MOp.isIdenticalTo(MO)) 236 continue; 237 if (MO.isKill()) 238 MOp.setIsKill(); 239 else 240 MOp.setIsDead(); 241 break; 242 } 243 } 244 } 245 246 /// copyPredicates - Copies predicate operand(s) from MI. 247 void MachineInstr::copyPredicates(const MachineInstr *MI) { 248 const TargetInstrDescriptor *TID = MI->getInstrDescriptor(); 249 if (TID->Flags & M_PREDICABLE) { 250 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 251 if ((TID->OpInfo[i].Flags & M_PREDICATE_OPERAND)) { 252 const MachineOperand &MO = MI->getOperand(i); 253 // Predicated operands must be last operands. 254 if (MO.isRegister()) 255 addRegOperand(MO.getReg(), false); 256 else { 257 addImmOperand(MO.getImm()); 258 } 259 } 260 } 261 } 262 } 263 264 void MachineInstr::dump() const { 265 cerr << " " << *this; 266 } 267 268 static inline void OutputReg(std::ostream &os, unsigned RegNo, 269 const MRegisterInfo *MRI = 0) { 270 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) { 271 if (MRI) 272 os << "%" << MRI->get(RegNo).Name; 273 else 274 os << "%mreg(" << RegNo << ")"; 275 } else 276 os << "%reg" << RegNo; 277 } 278 279 static void print(const MachineOperand &MO, std::ostream &OS, 280 const TargetMachine *TM) { 281 const MRegisterInfo *MRI = 0; 282 283 if (TM) MRI = TM->getRegisterInfo(); 284 285 switch (MO.getType()) { 286 case MachineOperand::MO_Register: 287 OutputReg(OS, MO.getReg(), MRI); 288 break; 289 case MachineOperand::MO_Immediate: 290 OS << MO.getImmedValue(); 291 break; 292 case MachineOperand::MO_MachineBasicBlock: 293 OS << "mbb<" 294 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName() 295 << "," << (void*)MO.getMachineBasicBlock() << ">"; 296 break; 297 case MachineOperand::MO_FrameIndex: 298 OS << "<fi#" << MO.getFrameIndex() << ">"; 299 break; 300 case MachineOperand::MO_ConstantPoolIndex: 301 OS << "<cp#" << MO.getConstantPoolIndex() << ">"; 302 break; 303 case MachineOperand::MO_JumpTableIndex: 304 OS << "<jt#" << MO.getJumpTableIndex() << ">"; 305 break; 306 case MachineOperand::MO_GlobalAddress: 307 OS << "<ga:" << ((Value*)MO.getGlobal())->getName(); 308 if (MO.getOffset()) OS << "+" << MO.getOffset(); 309 OS << ">"; 310 break; 311 case MachineOperand::MO_ExternalSymbol: 312 OS << "<es:" << MO.getSymbolName(); 313 if (MO.getOffset()) OS << "+" << MO.getOffset(); 314 OS << ">"; 315 break; 316 default: 317 assert(0 && "Unrecognized operand type"); 318 } 319 } 320 321 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const { 322 unsigned StartOp = 0; 323 324 // Specialize printing if op#0 is definition 325 if (getNumOperands() && getOperand(0).isRegister() && getOperand(0).isDef()) { 326 ::print(getOperand(0), OS, TM); 327 if (getOperand(0).isDead()) 328 OS << "<dead>"; 329 OS << " = "; 330 ++StartOp; // Don't print this operand again! 331 } 332 333 if (TID) 334 OS << TID->Name; 335 336 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 337 const MachineOperand& mop = getOperand(i); 338 if (i != StartOp) 339 OS << ","; 340 OS << " "; 341 ::print(mop, OS, TM); 342 343 if (mop.isRegister()) { 344 if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) { 345 OS << "<"; 346 bool NeedComma = false; 347 if (mop.isImplicit()) { 348 OS << (mop.isDef() ? "imp-def" : "imp-use"); 349 NeedComma = true; 350 } else if (mop.isDef()) { 351 OS << "def"; 352 NeedComma = true; 353 } 354 if (mop.isKill() || mop.isDead()) { 355 if (NeedComma) 356 OS << ","; 357 if (mop.isKill()) 358 OS << "kill"; 359 if (mop.isDead()) 360 OS << "dead"; 361 } 362 OS << ">"; 363 } 364 } 365 } 366 367 OS << "\n"; 368 } 369 370 void MachineInstr::print(std::ostream &os) const { 371 // If the instruction is embedded into a basic block, we can find the target 372 // info for the instruction. 373 if (const MachineBasicBlock *MBB = getParent()) { 374 const MachineFunction *MF = MBB->getParent(); 375 if (MF) 376 print(os, &MF->getTarget()); 377 else 378 print(os, 0); 379 } 380 381 // Otherwise, print it out in the "raw" format without symbolic register names 382 // and such. 383 os << getInstrDescriptor()->Name; 384 385 for (unsigned i = 0, N = getNumOperands(); i < N; i++) { 386 os << "\t" << getOperand(i); 387 if (getOperand(i).isRegister() && getOperand(i).isDef()) 388 os << "<d>"; 389 } 390 391 os << "\n"; 392 } 393 394 void MachineOperand::print(std::ostream &OS) const { 395 switch (getType()) { 396 case MO_Register: 397 OutputReg(OS, getReg()); 398 break; 399 case MO_Immediate: 400 OS << (long)getImmedValue(); 401 break; 402 case MO_MachineBasicBlock: 403 OS << "<mbb:" 404 << ((Value*)getMachineBasicBlock()->getBasicBlock())->getName() 405 << "@" << (void*)getMachineBasicBlock() << ">"; 406 break; 407 case MO_FrameIndex: 408 OS << "<fi#" << getFrameIndex() << ">"; 409 break; 410 case MO_ConstantPoolIndex: 411 OS << "<cp#" << getConstantPoolIndex() << ">"; 412 break; 413 case MO_JumpTableIndex: 414 OS << "<jt#" << getJumpTableIndex() << ">"; 415 break; 416 case MO_GlobalAddress: 417 OS << "<ga:" << ((Value*)getGlobal())->getName() << ">"; 418 break; 419 case MO_ExternalSymbol: 420 OS << "<es:" << getSymbolName() << ">"; 421 break; 422 default: 423 assert(0 && "Unrecognized operand type"); 424 break; 425 } 426 } 427 428