MachineInstr.cpp revision 2109f502d646a1bafc7b21a14347a71771a7b4cf
1//===-- MachineInstr.cpp --------------------------------------------------===// 2// 3//===----------------------------------------------------------------------===// 4 5#include "llvm/CodeGen/MachineInstr.h" 6#include "llvm/CodeGen/MachineBasicBlock.h" 7#include "llvm/Value.h" 8#include "llvm/Target/TargetMachine.h" 9#include "llvm/Target/MachineInstrInfo.h" 10#include "llvm/Target/MRegisterInfo.h" 11using std::cerr; 12 13// Global variable holding an array of descriptors for machine instructions. 14// The actual object needs to be created separately for each target machine. 15// This variable is initialized and reset by class MachineInstrInfo. 16// 17// FIXME: This should be a property of the target so that more than one target 18// at a time can be active... 19// 20extern const MachineInstrDescriptor *TargetInstrDescriptors; 21 22// Constructor for instructions with fixed #operands (nearly all) 23MachineInstr::MachineInstr(MachineOpCode _opCode) 24 : opCode(_opCode), 25 operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()), 26 numImplicitRefs(0) 27{ 28 assert(TargetInstrDescriptors[_opCode].numOperands >= 0); 29} 30 31// Constructor for instructions with variable #operands 32MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands) 33 : opCode(OpCode), 34 operands(numOperands, MachineOperand()), 35 numImplicitRefs(0) 36{ 37} 38 39/// MachineInstr ctor - This constructor only does a _reserve_ of the operands, 40/// not a resize for them. It is expected that if you use this that you call 41/// add* methods below to fill up the operands, instead of the Set methods. 42/// Eventually, the "resizing" ctors will be phased out. 43/// 44MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands, 45 bool XX, bool YY) 46 : opCode(Opcode), 47 numImplicitRefs(0) 48{ 49 operands.reserve(numOperands); 50} 51 52/// MachineInstr ctor - Work exactly the same as the ctor above, except that the 53/// MachineInstr is created and added to the end of the specified basic block. 54/// 55MachineInstr::MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode, 56 unsigned numOperands) 57 : opCode(Opcode), 58 numImplicitRefs(0) 59{ 60 assert(MBB && "Cannot use inserting ctor with null basic block!"); 61 operands.reserve(numOperands); 62 MBB->push_back(this); // Add instruction to end of basic block! 63} 64 65 66// OperandComplete - Return true if it's illegal to add a new operand 67bool MachineInstr::OperandsComplete() const 68{ 69 int NumOperands = TargetInstrDescriptors[opCode].numOperands; 70 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands) 71 return true; // Broken! 72 return false; 73} 74 75 76// 77// Support for replacing opcode and operands of a MachineInstr in place. 78// This only resets the size of the operand vector and initializes it. 79// The new operands must be set explicitly later. 80// 81void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands) 82{ 83 assert(getNumImplicitRefs() == 0 && 84 "This is probably broken because implicit refs are going to be lost."); 85 opCode = Opcode; 86 operands.clear(); 87 operands.resize(numOperands, MachineOperand()); 88} 89 90void 91MachineInstr::SetMachineOperandVal(unsigned i, 92 MachineOperand::MachineOperandType opType, 93 Value* V, 94 bool isdef, 95 bool isDefAndUse) 96{ 97 assert(i < operands.size()); // may be explicit or implicit op 98 operands[i].opType = opType; 99 operands[i].value = V; 100 operands[i].regNum = -1; 101 102 if (isDefAndUse) 103 operands[i].flags = MachineOperand::DEFUSEFLAG; 104 else if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i) 105 operands[i].flags = MachineOperand::DEFFLAG; 106 else 107 operands[i].flags = 0; 108} 109 110void 111MachineInstr::SetMachineOperandConst(unsigned i, 112 MachineOperand::MachineOperandType operandType, 113 int64_t intValue) 114{ 115 assert(i < getNumOperands()); // must be explicit op 116 assert(TargetInstrDescriptors[opCode].resultPos != (int) i && 117 "immed. constant cannot be defined"); 118 119 operands[i].opType = operandType; 120 operands[i].value = NULL; 121 operands[i].immedVal = intValue; 122 operands[i].regNum = -1; 123 operands[i].flags = 0; 124} 125 126void 127MachineInstr::SetMachineOperandReg(unsigned i, 128 int regNum, 129 bool isdef) { 130 assert(i < getNumOperands()); // must be explicit op 131 132 operands[i].opType = MachineOperand::MO_MachineRegister; 133 operands[i].value = NULL; 134 operands[i].regNum = regNum; 135 136 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i) 137 operands[i].flags = MachineOperand::DEFFLAG; 138 else 139 operands[i].flags = 0; 140 141 insertUsedReg(regNum); 142} 143 144void 145MachineInstr::SetRegForOperand(unsigned i, int regNum) 146{ 147 assert(i < getNumOperands()); // must be explicit op 148 operands[i].setRegForValue(regNum); 149 insertUsedReg(regNum); 150} 151 152 153// Subsitute all occurrences of Value* oldVal with newVal in all operands 154// and all implicit refs. If defsOnly == true, substitute defs only. 155unsigned 156MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly) 157{ 158 unsigned numSubst = 0; 159 160 // Subsitute operands 161 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O) 162 if (*O == oldVal) 163 if (!defsOnly || O.isDef()) 164 { 165 O.getMachineOperand().value = newVal; 166 ++numSubst; 167 } 168 169 // Subsitute implicit refs 170 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i) 171 if (getImplicitRef(i) == oldVal) 172 if (!defsOnly || implicitRefIsDefined(i)) 173 { 174 getImplicitOp(i).value = newVal; 175 ++numSubst; 176 } 177 178 return numSubst; 179} 180 181 182void 183MachineInstr::dump() const 184{ 185 cerr << " " << *this; 186} 187 188static inline std::ostream& 189OutputValue(std::ostream &os, const Value* val) 190{ 191 os << "(val "; 192 if (val && val->hasName()) 193 return os << val->getName() << ")"; 194 else 195 return os << (void*) val << ")"; // print address only 196} 197 198static inline void OutputReg(std::ostream &os, unsigned RegNo, 199 const MRegisterInfo *MRI = 0) { 200 if (MRI) { 201 if (RegNo < MRegisterInfo::FirstVirtualRegister) 202 os << "%" << MRI->get(RegNo).Name; 203 else 204 os << "%reg" << RegNo; 205 } else 206 os << "%mreg(" << RegNo << ")"; 207} 208 209static void print(const MachineOperand &MO, std::ostream &OS, 210 const TargetMachine &TM) { 211 const MRegisterInfo *MRI = TM.getRegisterInfo(); 212 bool CloseParen = true; 213 if (MO.opHiBits32()) 214 OS << "%lm("; 215 else if (MO.opLoBits32()) 216 OS << "%lo("; 217 else if (MO.opHiBits64()) 218 OS << "%hh("; 219 else if (MO.opLoBits64()) 220 OS << "%hm("; 221 else 222 CloseParen = false; 223 224 switch (MO.getType()) { 225 case MachineOperand::MO_VirtualRegister: 226 if (MO.getVRegValue()) { 227 OS << "%reg"; 228 OutputValue(OS, MO.getVRegValue()); 229 if (MO.hasAllocatedReg()) 230 OS << "=="; 231 } 232 if (MO.hasAllocatedReg()) 233 OutputReg(OS, MO.getAllocatedRegNum(), MRI); 234 break; 235 case MachineOperand::MO_CCRegister: 236 OS << "%ccreg"; 237 OutputValue(OS, MO.getVRegValue()); 238 if (MO.hasAllocatedReg()) { 239 OS << "=="; 240 OutputReg(OS, MO.getAllocatedRegNum(), MRI); 241 } 242 break; 243 case MachineOperand::MO_MachineRegister: 244 OutputReg(OS, MO.getMachineRegNum(), MRI); 245 break; 246 case MachineOperand::MO_SignExtendedImmed: 247 OS << (long)MO.getImmedValue(); 248 break; 249 case MachineOperand::MO_UnextendedImmed: 250 OS << (long)MO.getImmedValue(); 251 break; 252 case MachineOperand::MO_PCRelativeDisp: { 253 const Value* opVal = MO.getVRegValue(); 254 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal); 255 OS << "%disp(" << (isLabel? "label " : "addr-of-val "); 256 if (opVal->hasName()) 257 OS << opVal->getName(); 258 else 259 OS << (const void*) opVal; 260 OS << ")"; 261 break; 262 } 263 case MachineOperand::MO_MachineBasicBlock: 264 OS << "bb<" 265 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName() 266 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">"; 267 break; 268 269 default: 270 assert(0 && "Unrecognized operand type"); 271 } 272 273 if (CloseParen) 274 OS << ")"; 275} 276 277void MachineInstr::print(std::ostream &OS, const TargetMachine &TM) const { 278 unsigned StartOp = 0; 279 280 // Specialize printing if op#0 is definition 281 if (getNumOperands() && operandIsDefined(0)) { 282 ::print(getOperand(0), OS, TM); 283 OS << " = "; 284 ++StartOp; // Don't print this operand again! 285 } 286 OS << TM.getInstrInfo().getName(getOpcode()); 287 288 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 289 if (i != StartOp) 290 OS << ","; 291 OS << " "; 292 ::print(getOperand(i), OS, TM); 293 294 if (operandIsDefinedAndUsed(i)) 295 OS << "<def&use>"; 296 else if (operandIsDefined(i)) 297 OS << "<def>"; 298 } 299 300 // code for printing implict references 301 if (getNumImplicitRefs()) { 302 OS << "\tImplicitRefs: "; 303 for(unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) { 304 OS << "\t"; 305 OutputValue(OS, getImplicitRef(i)); 306 if (implicitRefIsDefinedAndUsed(i)) 307 OS << "<def&use>"; 308 else if (implicitRefIsDefined(i)) 309 OS << "<def>"; 310 } 311 } 312 313 OS << "\n"; 314} 315 316 317std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr) 318{ 319 os << TargetInstrDescriptors[minstr.opCode].Name; 320 321 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) { 322 os << "\t" << minstr.getOperand(i); 323 if( minstr.operandIsDefined(i) ) 324 os << "*"; 325 if( minstr.operandIsDefinedAndUsed(i) ) 326 os << "*"; 327 } 328 329 // code for printing implict references 330 unsigned NumOfImpRefs = minstr.getNumImplicitRefs(); 331 if( NumOfImpRefs > 0 ) { 332 os << "\tImplicit: "; 333 for(unsigned z=0; z < NumOfImpRefs; z++) { 334 OutputValue(os, minstr.getImplicitRef(z)); 335 if( minstr.implicitRefIsDefined(z)) os << "*"; 336 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*"; 337 os << "\t"; 338 } 339 } 340 341 return os << "\n"; 342} 343 344std::ostream &operator<<(std::ostream &os, const MachineOperand &MO) 345{ 346 if (MO.opHiBits32()) 347 os << "%lm("; 348 else if (MO.opLoBits32()) 349 os << "%lo("; 350 else if (MO.opHiBits64()) 351 os << "%hh("; 352 else if (MO.opLoBits64()) 353 os << "%hm("; 354 355 switch (MO.getType()) 356 { 357 case MachineOperand::MO_VirtualRegister: 358 os << "%reg"; 359 OutputValue(os, MO.getVRegValue()); 360 if (MO.hasAllocatedReg()) { 361 os << "=="; 362 OutputReg(os, MO.getAllocatedRegNum()); 363 } 364 break; 365 case MachineOperand::MO_CCRegister: 366 os << "%ccreg"; 367 OutputValue(os, MO.getVRegValue()); 368 if (MO.hasAllocatedReg()) { 369 os << "=="; 370 OutputReg(os, MO.getAllocatedRegNum()); 371 } 372 break; 373 case MachineOperand::MO_MachineRegister: 374 OutputReg(os, MO.getMachineRegNum()); 375 break; 376 case MachineOperand::MO_SignExtendedImmed: 377 os << (long)MO.getImmedValue(); 378 break; 379 case MachineOperand::MO_UnextendedImmed: 380 os << (long)MO.getImmedValue(); 381 break; 382 case MachineOperand::MO_PCRelativeDisp: 383 { 384 const Value* opVal = MO.getVRegValue(); 385 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal); 386 os << "%disp(" << (isLabel? "label " : "addr-of-val "); 387 if (opVal->hasName()) 388 os << opVal->getName(); 389 else 390 os << (const void*) opVal; 391 os << ")"; 392 break; 393 } 394 case MachineOperand::MO_MachineBasicBlock: 395 os << "bb<" 396 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName() 397 << "," << (void*)MO.getMachineBasicBlock()->getBasicBlock() << ">"; 398 break; 399 default: 400 assert(0 && "Unrecognized operand type"); 401 break; 402 } 403 404 if (MO.flags & 405 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 | 406 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64)) 407 os << ")"; 408 409 return os; 410} 411