MachineInstr.cpp revision 6e994b74921d8519472a087246d7a56731e958d4
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 <iostream> 21 22using namespace llvm; 23 24// Global variable holding an array of descriptors for machine instructions. 25// The actual object needs to be created separately for each target machine. 26// This variable is initialized and reset by class TargetInstrInfo. 27// 28// FIXME: This should be a property of the target so that more than one target 29// at a time can be active... 30// 31namespace llvm { 32 extern const TargetInstrDescriptor *TargetInstrDescriptors; 33} 34 35/// MachineInstr ctor - This constructor only does a _reserve_ of the operands, 36/// not a resize for them. It is expected that if you use this that you call 37/// add* methods below to fill up the operands, instead of the Set methods. 38/// Eventually, the "resizing" ctors will be phased out. 39/// 40MachineInstr::MachineInstr(short opcode, unsigned numOperands) 41 : Opcode(opcode), parent(0) { 42 Operands.reserve(numOperands); 43 // Make sure that we get added to a machine basicblock 44 LeakDetector::addGarbageObject(this); 45} 46 47/// MachineInstr ctor - Work exactly the same as the ctor above, except that the 48/// MachineInstr is created and added to the end of the specified basic block. 49/// 50MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode, 51 unsigned numOperands) 52 : Opcode(opcode), parent(0) { 53 assert(MBB && "Cannot use inserting ctor with null basic block!"); 54 Operands.reserve(numOperands); 55 // Make sure that we get added to a machine basicblock 56 LeakDetector::addGarbageObject(this); 57 MBB->push_back(this); // Add instruction to end of basic block! 58} 59 60/// MachineInstr ctor - Copies MachineInstr arg exactly 61/// 62MachineInstr::MachineInstr(const MachineInstr &MI) { 63 Opcode = MI.getOpcode(); 64 Operands.reserve(MI.getNumOperands()); 65 66 // Add operands 67 for (unsigned i = 0; i != MI.getNumOperands(); ++i) 68 Operands.push_back(MI.getOperand(i)); 69 70 // Set parent, next, and prev to null 71 parent = 0; 72 prev = 0; 73 next = 0; 74} 75 76 77MachineInstr::~MachineInstr() { 78 LeakDetector::removeGarbageObject(this); 79} 80 81/// removeFromParent - This method unlinks 'this' from the containing basic 82/// block, and returns it, but does not delete it. 83MachineInstr *MachineInstr::removeFromParent() { 84 assert(getParent() && "Not embedded in a basic block!"); 85 getParent()->remove(this); 86 return this; 87} 88 89 90/// OperandComplete - Return true if it's illegal to add a new operand 91/// 92bool MachineInstr::OperandsComplete() const { 93 int NumOperands = TargetInstrDescriptors[Opcode].numOperands; 94 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands) 95 return true; // Broken: we have all the operands of this instruction! 96 return false; 97} 98 99void MachineInstr::dump() const { 100 std::cerr << " " << *this; 101} 102 103static inline void OutputReg(std::ostream &os, unsigned RegNo, 104 const MRegisterInfo *MRI = 0) { 105 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) { 106 if (MRI) 107 os << "%" << MRI->get(RegNo).Name; 108 else 109 os << "%mreg(" << RegNo << ")"; 110 } else 111 os << "%reg" << RegNo; 112} 113 114static void print(const MachineOperand &MO, std::ostream &OS, 115 const TargetMachine *TM) { 116 const MRegisterInfo *MRI = 0; 117 118 if (TM) MRI = TM->getRegisterInfo(); 119 120 switch (MO.getType()) { 121 case MachineOperand::MO_Register: 122 OutputReg(OS, MO.getReg(), MRI); 123 break; 124 case MachineOperand::MO_Immediate: 125 OS << (long)MO.getImmedValue(); 126 break; 127 case MachineOperand::MO_MachineBasicBlock: 128 OS << "mbb<" 129 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName() 130 << "," << (void*)MO.getMachineBasicBlock() << ">"; 131 break; 132 case MachineOperand::MO_FrameIndex: 133 OS << "<fi#" << MO.getFrameIndex() << ">"; 134 break; 135 case MachineOperand::MO_ConstantPoolIndex: 136 OS << "<cp#" << MO.getConstantPoolIndex() << ">"; 137 break; 138 case MachineOperand::MO_JumpTableIndex: 139 OS << "<jt#" << MO.getJumpTableIndex() << ">"; 140 break; 141 case MachineOperand::MO_GlobalAddress: 142 OS << "<ga:" << ((Value*)MO.getGlobal())->getName(); 143 if (MO.getOffset()) OS << "+" << MO.getOffset(); 144 OS << ">"; 145 break; 146 case MachineOperand::MO_ExternalSymbol: 147 OS << "<es:" << MO.getSymbolName(); 148 if (MO.getOffset()) OS << "+" << MO.getOffset(); 149 OS << ">"; 150 break; 151 default: 152 assert(0 && "Unrecognized operand type"); 153 } 154} 155 156void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const { 157 unsigned StartOp = 0; 158 159 // Specialize printing if op#0 is definition 160 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) { 161 ::print(getOperand(0), OS, TM); 162 OS << " = "; 163 ++StartOp; // Don't print this operand again! 164 } 165 166 // Must check if Target machine is not null because machine BB could not 167 // be attached to a Machine function yet 168 if (TM) 169 OS << TM->getInstrInfo()->getName(getOpcode()); 170 171 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) { 172 const MachineOperand& mop = getOperand(i); 173 if (i != StartOp) 174 OS << ","; 175 OS << " "; 176 ::print(mop, OS, TM); 177 178 if (mop.isDef()) 179 if (mop.isUse()) 180 OS << "<def&use>"; 181 else 182 OS << "<def>"; 183 } 184 185 OS << "\n"; 186} 187 188std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) { 189 // If the instruction is embedded into a basic block, we can find the target 190 // info for the instruction. 191 if (const MachineBasicBlock *MBB = MI.getParent()) { 192 const MachineFunction *MF = MBB->getParent(); 193 if (MF) 194 MI.print(os, &MF->getTarget()); 195 else 196 MI.print(os, 0); 197 return os; 198 } 199 200 // Otherwise, print it out in the "raw" format without symbolic register names 201 // and such. 202 os << TargetInstrDescriptors[MI.getOpcode()].Name; 203 204 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) { 205 os << "\t" << MI.getOperand(i); 206 if (MI.getOperand(i).isDef()) 207 if (MI.getOperand(i).isUse()) 208 os << "<d&u>"; 209 else 210 os << "<d>"; 211 } 212 213 return os << "\n"; 214} 215 216std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) { 217 switch (MO.getType()) { 218 case MachineOperand::MO_Register: 219 OutputReg(OS, MO.getReg()); 220 break; 221 case MachineOperand::MO_Immediate: 222 OS << (long)MO.getImmedValue(); 223 break; 224 case MachineOperand::MO_MachineBasicBlock: 225 OS << "<mbb:" 226 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName() 227 << "@" << (void*)MO.getMachineBasicBlock() << ">"; 228 break; 229 case MachineOperand::MO_FrameIndex: 230 OS << "<fi#" << MO.getFrameIndex() << ">"; 231 break; 232 case MachineOperand::MO_ConstantPoolIndex: 233 OS << "<cp#" << MO.getConstantPoolIndex() << ">"; 234 break; 235 case MachineOperand::MO_JumpTableIndex: 236 OS << "<jt#" << MO.getJumpTableIndex() << ">"; 237 break; 238 case MachineOperand::MO_GlobalAddress: 239 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">"; 240 break; 241 case MachineOperand::MO_ExternalSymbol: 242 OS << "<es:" << MO.getSymbolName() << ">"; 243 break; 244 default: 245 assert(0 && "Unrecognized operand type"); 246 break; 247 } 248 249 return OS; 250} 251