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