Remove redundancy and a level of indirection when creating machine operands
[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)
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(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 /// removeFromParent - This method unlinks 'this' from the containing basic
86 /// block, and returns it, but does not delete it.
87 MachineInstr *MachineInstr::removeFromParent() {
88   assert(getParent() && "Not embedded in a basic block!");
89   getParent()->remove(this);
90   return this;
91 }
92
93
94 /// OperandComplete - Return true if it's illegal to add a new operand
95 ///
96 bool MachineInstr::OperandsComplete() const {
97   int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
98   if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
99     return true;  // Broken: we have all the operands of this instruction!
100   return false;
101 }
102
103 void MachineInstr::dump() const {
104   std::cerr << "  " << *this;
105 }
106
107 static inline void OutputReg(std::ostream &os, unsigned RegNo,
108                              const MRegisterInfo *MRI = 0) {
109   if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
110     if (MRI)
111       os << "%" << MRI->get(RegNo).Name;
112     else
113       os << "%mreg(" << RegNo << ")";
114   } else
115     os << "%reg" << RegNo;
116 }
117
118 static void print(const MachineOperand &MO, std::ostream &OS,
119                   const TargetMachine *TM) {
120   const MRegisterInfo *MRI = 0;
121
122   if (TM) MRI = TM->getRegisterInfo();
123
124   switch (MO.getType()) {
125   case MachineOperand::MO_Register:
126     OutputReg(OS, MO.getReg(), MRI);
127     break;
128   case MachineOperand::MO_Immediate:
129     OS << (long)MO.getImmedValue();
130     break;
131   case MachineOperand::MO_MachineBasicBlock:
132     OS << "mbb<"
133        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
134        << "," << (void*)MO.getMachineBasicBlock() << ">";
135     break;
136   case MachineOperand::MO_FrameIndex:
137     OS << "<fi#" << MO.getFrameIndex() << ">";
138     break;
139   case MachineOperand::MO_ConstantPoolIndex:
140     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
141     break;
142   case MachineOperand::MO_JumpTableIndex:
143     OS << "<jt#" << MO.getJumpTableIndex() << ">";
144     break;
145   case MachineOperand::MO_GlobalAddress:
146     OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
147     if (MO.getOffset()) OS << "+" << MO.getOffset();
148     OS << ">";
149     break;
150   case MachineOperand::MO_ExternalSymbol:
151     OS << "<es:" << MO.getSymbolName();
152     if (MO.getOffset()) OS << "+" << MO.getOffset();
153     OS << ">";
154     break;
155   default:
156     assert(0 && "Unrecognized operand type");
157   }
158 }
159
160 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
161   unsigned StartOp = 0;
162
163    // Specialize printing if op#0 is definition
164   if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
165     ::print(getOperand(0), OS, TM);
166     OS << " = ";
167     ++StartOp;   // Don't print this operand again!
168   }
169
170   // Must check if Target machine is not null because machine BB could not
171   // be attached to a Machine function yet
172   if (TM)
173     OS << TM->getInstrInfo()->getName(getOpcode());
174
175   for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
176     const MachineOperand& mop = getOperand(i);
177     if (i != StartOp)
178       OS << ",";
179     OS << " ";
180     ::print(mop, OS, TM);
181
182     if (mop.isDef())
183       if (mop.isUse())
184         OS << "<def&use>";
185       else
186         OS << "<def>";
187   }
188
189   OS << "\n";
190 }
191
192 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
193   // If the instruction is embedded into a basic block, we can find the target
194   // info for the instruction.
195   if (const MachineBasicBlock *MBB = MI.getParent()) {
196     const MachineFunction *MF = MBB->getParent();
197     if (MF)
198       MI.print(os, &MF->getTarget());
199     else
200       MI.print(os, 0);
201     return os;
202   }
203
204   // Otherwise, print it out in the "raw" format without symbolic register names
205   // and such.
206   os << TargetInstrDescriptors[MI.getOpcode()].Name;
207
208   for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
209     os << "\t" << MI.getOperand(i);
210     if (MI.getOperand(i).isDef())
211       if (MI.getOperand(i).isUse())
212         os << "<d&u>";
213       else
214         os << "<d>";
215   }
216
217   return os << "\n";
218 }
219
220 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
221   switch (MO.getType()) {
222   case MachineOperand::MO_Register:
223     OutputReg(OS, MO.getReg());
224     break;
225   case MachineOperand::MO_Immediate:
226     OS << (long)MO.getImmedValue();
227     break;
228   case MachineOperand::MO_MachineBasicBlock:
229     OS << "<mbb:"
230        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
231        << "@" << (void*)MO.getMachineBasicBlock() << ">";
232     break;
233   case MachineOperand::MO_FrameIndex:
234     OS << "<fi#" << MO.getFrameIndex() << ">";
235     break;
236   case MachineOperand::MO_ConstantPoolIndex:
237     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
238     break;
239   case MachineOperand::MO_JumpTableIndex:
240     OS << "<jt#" << MO.getJumpTableIndex() << ">";
241     break;
242   case MachineOperand::MO_GlobalAddress:
243     OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
244     break;
245   case MachineOperand::MO_ExternalSymbol:
246     OS << "<es:" << MO.getSymbolName() << ">";
247     break;
248   default:
249     assert(0 && "Unrecognized operand type");
250     break;
251   }
252
253   return OS;
254 }