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