1 //===-- MachineInstr.cpp --------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Methods common to all machine instructions.
12 // FIXME: Now that MachineInstrs have parent pointers, they should always
13 // print themselves using their MachineFunction's TargetMachine.
15 //===----------------------------------------------------------------------===//
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"
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.
32 // FIXME: This should be a property of the target so that more than one target
33 // at a time can be active...
36 extern const TargetInstrDescriptor *TargetInstrDescriptors;
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.
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);
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.
54 MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
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!
64 /// MachineInstr ctor - Copies MachineInstr arg exactly
66 MachineInstr::MachineInstr(const MachineInstr &MI) {
67 Opcode = MI.getOpcode();
68 operands.reserve(MI.getNumOperands());
71 for (unsigned i = 0; i < MI.getNumOperands(); ++i)
72 operands.push_back(MachineOperand(MI.getOperand(i)));
74 // Set parent, next, and prev to null
81 MachineInstr::~MachineInstr() {
82 LeakDetector::removeGarbageObject(this);
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
88 MachineInstr* MachineInstr::clone() const {
89 return new MachineInstr(*this);
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);
101 /// OperandComplete - Return true if it's illegal to add a new operand
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!
111 MachineInstr::SetMachineOperandConst(unsigned i,
112 MachineOperand::MachineOperandType opTy,
114 assert(i < getNumOperands());
115 operands[i].opType = opTy;
116 operands[i].contents.immedVal = intValue;
117 operands[i].extra.regNum = -1;
118 operands[i].flags = 0;
121 void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) {
122 assert(i < getNumOperands());
124 operands[i].opType = MachineOperand::MO_VirtualRegister;
125 operands[i].contents.GV = NULL;
126 operands[i].extra.regNum = regNum;
129 void MachineInstr::dump() const {
130 std::cerr << " " << *this;
133 static inline std::ostream& OutputValue(std::ostream &os, const Value* val) {
135 os << (void*) val; // print address always
136 if (val && val->hasName())
137 os << " " << val->getName(); // print name also, if available
142 static inline void OutputReg(std::ostream &os, unsigned RegNo,
143 const MRegisterInfo *MRI = 0) {
144 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
146 os << "%" << MRI->get(RegNo).Name;
148 os << "%mreg(" << RegNo << ")";
150 os << "%reg" << RegNo;
153 static void print(const MachineOperand &MO, std::ostream &OS,
154 const TargetMachine *TM) {
155 const MRegisterInfo *MRI = 0;
157 if (TM) MRI = TM->getRegisterInfo();
159 switch (MO.getType()) {
160 case MachineOperand::MO_VirtualRegister:
161 OutputReg(OS, MO.getReg(), MRI);
163 case MachineOperand::MO_Immediate:
164 OS << (long)MO.getImmedValue();
166 case MachineOperand::MO_MachineBasicBlock:
168 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
169 << "," << (void*)MO.getMachineBasicBlock() << ">";
171 case MachineOperand::MO_FrameIndex:
172 OS << "<fi#" << MO.getFrameIndex() << ">";
174 case MachineOperand::MO_ConstantPoolIndex:
175 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
177 case MachineOperand::MO_JumpTableIndex:
178 OS << "<jt#" << MO.getJumpTableIndex() << ">";
180 case MachineOperand::MO_GlobalAddress:
181 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
182 if (MO.getOffset()) OS << "+" << MO.getOffset();
185 case MachineOperand::MO_ExternalSymbol:
186 OS << "<es:" << MO.getSymbolName();
187 if (MO.getOffset()) OS << "+" << MO.getOffset();
191 assert(0 && "Unrecognized operand type");
195 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
196 unsigned StartOp = 0;
198 // Specialize printing if op#0 is definition
199 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
200 ::print(getOperand(0), OS, TM);
202 ++StartOp; // Don't print this operand again!
205 // Must check if Target machine is not null because machine BB could not
206 // be attached to a Machine function yet
208 OS << TM->getInstrInfo()->getName(getOpcode());
210 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
211 const MachineOperand& mop = getOperand(i);
215 ::print(mop, OS, TM);
227 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
228 // If the instruction is embedded into a basic block, we can find the target
229 // info for the instruction.
230 if (const MachineBasicBlock *MBB = MI.getParent()) {
231 const MachineFunction *MF = MBB->getParent();
233 MI.print(os, &MF->getTarget());
239 // Otherwise, print it out in the "raw" format without symbolic register names
241 os << TargetInstrDescriptors[MI.getOpcode()].Name;
243 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
244 os << "\t" << MI.getOperand(i);
245 if (MI.getOperand(i).isDef())
246 if (MI.getOperand(i).isUse())
255 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
256 switch (MO.getType()) {
257 case MachineOperand::MO_VirtualRegister:
258 OutputReg(OS, MO.getReg());
260 case MachineOperand::MO_Immediate:
261 OS << (long)MO.getImmedValue();
263 case MachineOperand::MO_MachineBasicBlock:
265 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
266 << "@" << (void*)MO.getMachineBasicBlock() << ">";
268 case MachineOperand::MO_FrameIndex:
269 OS << "<fi#" << MO.getFrameIndex() << ">";
271 case MachineOperand::MO_ConstantPoolIndex:
272 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
274 case MachineOperand::MO_JumpTableIndex:
275 OS << "<jt#" << MO.getJumpTableIndex() << ">";
277 case MachineOperand::MO_GlobalAddress:
278 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
280 case MachineOperand::MO_ExternalSymbol:
281 OS << "<es:" << MO.getSymbolName() << ">";
284 assert(0 && "Unrecognized operand type");