Remove a bunch more dead V9 specific stuff
[oota-llvm.git] / lib / CodeGen / MachineInstr.cpp
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::SetMachineOperandVal(unsigned i,
111                                         MachineOperand::MachineOperandType opTy,
112                                         Value* V) {
113   assert(i < operands.size());          // may be explicit or implicit op
114   operands[i].opType = opTy;
115   operands[i].contents.value = V;
116   operands[i].extra.regNum = -1;
117 }
118
119 void
120 MachineInstr::SetMachineOperandConst(unsigned i,
121                                      MachineOperand::MachineOperandType opTy,
122                                      int intValue) {
123   assert(i < getNumOperands());          // must be explicit op
124
125   operands[i].opType = opTy;
126   operands[i].contents.value = NULL;
127   operands[i].contents.immedVal = intValue;
128   operands[i].extra.regNum = -1;
129   operands[i].flags = 0;
130 }
131
132 void MachineInstr::SetMachineOperandReg(unsigned i, int regNum) {
133   assert(i < getNumOperands());          // must be explicit op
134
135   operands[i].opType = MachineOperand::MO_VirtualRegister;
136   operands[i].contents.value = NULL;
137   operands[i].extra.regNum = regNum;
138 }
139
140 void MachineInstr::dump() const {
141   std::cerr << "  " << *this;
142 }
143
144 static inline std::ostream& OutputValue(std::ostream &os, const Value* val) {
145   os << "(val ";
146   os << (void*) val;                // print address always
147   if (val && val->hasName())
148     os << " " << val->getName();    // print name also, if available
149   os << ")";
150   return os;
151 }
152
153 static inline void OutputReg(std::ostream &os, unsigned RegNo,
154                              const MRegisterInfo *MRI = 0) {
155   if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
156     if (MRI)
157       os << "%" << MRI->get(RegNo).Name;
158     else
159       os << "%mreg(" << RegNo << ")";
160   } else
161     os << "%reg" << RegNo;
162 }
163
164 static void print(const MachineOperand &MO, std::ostream &OS,
165                   const TargetMachine *TM) {
166   const MRegisterInfo *MRI = 0;
167
168   if (TM) MRI = TM->getRegisterInfo();
169
170   switch (MO.getType()) {
171   case MachineOperand::MO_VirtualRegister:
172     OutputReg(OS, MO.getReg(), MRI);
173     break;
174   case MachineOperand::MO_SignExtendedImmed:
175     OS << (long)MO.getImmedValue();
176     break;
177   case MachineOperand::MO_UnextendedImmed:
178     OS << (long)MO.getImmedValue();
179     break;
180   case MachineOperand::MO_MachineBasicBlock:
181     OS << "mbb<"
182        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
183        << "," << (void*)MO.getMachineBasicBlock() << ">";
184     break;
185   case MachineOperand::MO_FrameIndex:
186     OS << "<fi#" << MO.getFrameIndex() << ">";
187     break;
188   case MachineOperand::MO_ConstantPoolIndex:
189     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
190     break;
191   case MachineOperand::MO_JumpTableIndex:
192     OS << "<jt#" << MO.getJumpTableIndex() << ">";
193     break;
194   case MachineOperand::MO_GlobalAddress:
195     OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
196     if (MO.getOffset()) OS << "+" << MO.getOffset();
197     OS << ">";
198     break;
199   case MachineOperand::MO_ExternalSymbol:
200     OS << "<es:" << MO.getSymbolName();
201     if (MO.getOffset()) OS << "+" << MO.getOffset();
202     OS << ">";
203     break;
204   default:
205     assert(0 && "Unrecognized operand type");
206   }
207 }
208
209 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
210   unsigned StartOp = 0;
211
212    // Specialize printing if op#0 is definition
213   if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
214     ::print(getOperand(0), OS, TM);
215     OS << " = ";
216     ++StartOp;   // Don't print this operand again!
217   }
218
219   // Must check if Target machine is not null because machine BB could not
220   // be attached to a Machine function yet
221   if (TM)
222     OS << TM->getInstrInfo()->getName(getOpcode());
223
224   for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
225     const MachineOperand& mop = getOperand(i);
226     if (i != StartOp)
227       OS << ",";
228     OS << " ";
229     ::print(mop, OS, TM);
230
231     if (mop.isDef())
232       if (mop.isUse())
233         OS << "<def&use>";
234       else
235         OS << "<def>";
236   }
237
238   OS << "\n";
239 }
240
241 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
242   // If the instruction is embedded into a basic block, we can find the target
243   // info for the instruction.
244   if (const MachineBasicBlock *MBB = MI.getParent()) {
245     const MachineFunction *MF = MBB->getParent();
246     if (MF)
247       MI.print(os, &MF->getTarget());
248     else
249       MI.print(os, 0);
250     return os;
251   }
252
253   // Otherwise, print it out in the "raw" format without symbolic register names
254   // and such.
255   os << TargetInstrDescriptors[MI.getOpcode()].Name;
256
257   for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
258     os << "\t" << MI.getOperand(i);
259     if (MI.getOperand(i).isDef())
260       if (MI.getOperand(i).isUse())
261         os << "<d&u>";
262       else
263         os << "<d>";
264   }
265
266   return os << "\n";
267 }
268
269 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
270   switch (MO.getType()) {
271   case MachineOperand::MO_VirtualRegister:
272     OutputReg(OS, MO.getReg());
273     break;
274   case MachineOperand::MO_SignExtendedImmed:
275     OS << (long)MO.getImmedValue();
276     break;
277   case MachineOperand::MO_UnextendedImmed:
278     OS << (long)MO.getImmedValue();
279     break;
280   case MachineOperand::MO_MachineBasicBlock:
281     OS << "<mbb:"
282        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
283        << "@" << (void*)MO.getMachineBasicBlock() << ">";
284     break;
285   case MachineOperand::MO_FrameIndex:
286     OS << "<fi#" << MO.getFrameIndex() << ">";
287     break;
288   case MachineOperand::MO_ConstantPoolIndex:
289     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
290     break;
291   case MachineOperand::MO_JumpTableIndex:
292     OS << "<jt#" << MO.getJumpTableIndex() << ">";
293     break;
294   case MachineOperand::MO_GlobalAddress:
295     OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
296     break;
297   case MachineOperand::MO_ExternalSymbol:
298     OS << "<es:" << MO.getSymbolName() << ">";
299     break;
300   default:
301     assert(0 && "Unrecognized operand type");
302     break;
303   }
304
305   return OS;
306 }