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