setOperand should not zap the operand list or add implicit operands to an
[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 //===----------------------------------------------------------------------===//
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
22 using 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 //
31 namespace 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 ///
40 MachineInstr::MachineInstr(short opcode, unsigned numOperands)
41   : Opcode(opcode), NumImplicitOps(0), parent(0) {
42   Operands.reserve(numOperands);
43   // Make sure that we get added to a machine basicblock
44   LeakDetector::addGarbageObject(this);
45 }
46
47 void MachineInstr::addImplicitDefUseOperands(const TargetInstrDescriptor &TID) {
48   if (TID.ImplicitDefs)
49     for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs) {
50       MachineOperand Op;
51       Op.opType = MachineOperand::MO_Register;
52       Op.IsDef = true;
53       Op.IsImp = true;
54       Op.IsKill = false;
55       Op.IsDead = false;
56       Op.contents.RegNo = *ImpDefs;
57       Op.offset = 0;
58       Operands.push_back(Op);
59     }
60   if (TID.ImplicitUses)
61     for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses) {
62       MachineOperand Op;
63       Op.opType = MachineOperand::MO_Register;
64       Op.IsDef = false;
65       Op.IsImp = true;
66       Op.IsKill = false;
67       Op.IsDead = false;
68       Op.contents.RegNo = *ImpUses;
69       Op.offset = 0;
70       Operands.push_back(Op);
71     }
72 }
73
74 /// MachineInstr ctor - This constructor create a MachineInstr and add the
75 /// implicit operands. It reserves space for numOperand operands.
76 MachineInstr::MachineInstr(const TargetInstrInfo &TII, short opcode,
77                            unsigned numOperands)
78   : Opcode(opcode), NumImplicitOps(0), parent(0) {
79   const TargetInstrDescriptor &TID = TII.get(opcode);
80   if (TID.ImplicitDefs)
81     for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs)
82       NumImplicitOps++;
83   if (TID.ImplicitUses)
84     for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses)
85       NumImplicitOps++;
86   Operands.reserve(NumImplicitOps + numOperands);
87   addImplicitDefUseOperands(TID);
88   // Make sure that we get added to a machine basicblock
89   LeakDetector::addGarbageObject(this);
90 }
91
92 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
93 /// MachineInstr is created and added to the end of the specified basic block.
94 ///
95 MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
96                            unsigned numOperands)
97   : Opcode(opcode), NumImplicitOps(0), parent(0) {
98   assert(MBB && "Cannot use inserting ctor with null basic block!");
99   const TargetInstrDescriptor &TID = MBB->getParent()->getTarget().
100     getInstrInfo()->get(opcode);
101   if (TID.ImplicitDefs)
102     for (const unsigned *ImpDefs = TID.ImplicitDefs; *ImpDefs; ++ImpDefs)
103       NumImplicitOps++;
104   if (TID.ImplicitUses)
105     for (const unsigned *ImpUses = TID.ImplicitUses; *ImpUses; ++ImpUses)
106       NumImplicitOps++;
107   Operands.reserve(NumImplicitOps + numOperands);
108   addImplicitDefUseOperands(TID);
109   // Make sure that we get added to a machine basicblock
110   LeakDetector::addGarbageObject(this);
111   MBB->push_back(this);  // Add instruction to end of basic block!
112 }
113
114 /// MachineInstr ctor - Copies MachineInstr arg exactly
115 ///
116 MachineInstr::MachineInstr(const MachineInstr &MI) {
117   Opcode = MI.getOpcode();
118   NumImplicitOps = MI.NumImplicitOps;
119   Operands.reserve(MI.getNumOperands());
120
121   // Add operands
122   for (unsigned i = 0; i != MI.getNumOperands(); ++i)
123     Operands.push_back(MI.getOperand(i));
124
125   // Set parent, next, and prev to null
126   parent = 0;
127   prev = 0;
128   next = 0;
129 }
130
131
132 MachineInstr::~MachineInstr() {
133   LeakDetector::removeGarbageObject(this);
134 }
135
136 /// removeFromParent - This method unlinks 'this' from the containing basic
137 /// block, and returns it, but does not delete it.
138 MachineInstr *MachineInstr::removeFromParent() {
139   assert(getParent() && "Not embedded in a basic block!");
140   getParent()->remove(this);
141   return this;
142 }
143
144
145 /// OperandComplete - Return true if it's illegal to add a new operand
146 ///
147 bool MachineInstr::OperandsComplete() const {
148   int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
149   if ((TargetInstrDescriptors[Opcode].Flags & M_VARIABLE_OPS) == 0 &&
150       getNumOperands()-NumImplicitOps >= (unsigned)NumOperands)
151     return true;  // Broken: we have all the operands of this instruction!
152   return false;
153 }
154
155 /// isIdenticalTo - Return true if this operand is identical to the specified
156 /// operand.
157 bool MachineOperand::isIdenticalTo(const MachineOperand &Other) const {
158   if (getType() != Other.getType()) return false;
159   
160   switch (getType()) {
161   default: assert(0 && "Unrecognized operand type");
162   case MachineOperand::MO_Register:
163     return getReg() == Other.getReg() && isDef() == Other.isDef();
164   case MachineOperand::MO_Immediate:
165     return getImm() == Other.getImm();
166   case MachineOperand::MO_MachineBasicBlock:
167     return getMBB() == Other.getMBB();
168   case MachineOperand::MO_FrameIndex:
169     return getFrameIndex() == Other.getFrameIndex();
170   case MachineOperand::MO_ConstantPoolIndex:
171     return getConstantPoolIndex() == Other.getConstantPoolIndex() &&
172            getOffset() == Other.getOffset();
173   case MachineOperand::MO_JumpTableIndex:
174     return getJumpTableIndex() == Other.getJumpTableIndex();
175   case MachineOperand::MO_GlobalAddress:
176     return getGlobal() == Other.getGlobal() && getOffset() == Other.getOffset();
177   case MachineOperand::MO_ExternalSymbol:
178     return !strcmp(getSymbolName(), Other.getSymbolName()) &&
179            getOffset() == Other.getOffset();
180   }
181 }
182
183 void MachineInstr::dump() const {
184   std::cerr << "  " << *this;
185 }
186
187 static inline void OutputReg(std::ostream &os, unsigned RegNo,
188                              const MRegisterInfo *MRI = 0) {
189   if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
190     if (MRI)
191       os << "%" << MRI->get(RegNo).Name;
192     else
193       os << "%mreg(" << RegNo << ")";
194   } else
195     os << "%reg" << RegNo;
196 }
197
198 static void print(const MachineOperand &MO, std::ostream &OS,
199                   const TargetMachine *TM) {
200   const MRegisterInfo *MRI = 0;
201
202   if (TM) MRI = TM->getRegisterInfo();
203
204   switch (MO.getType()) {
205   case MachineOperand::MO_Register:
206     OutputReg(OS, MO.getReg(), MRI);
207     break;
208   case MachineOperand::MO_Immediate:
209     OS << MO.getImmedValue();
210     break;
211   case MachineOperand::MO_MachineBasicBlock:
212     OS << "mbb<"
213        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
214        << "," << (void*)MO.getMachineBasicBlock() << ">";
215     break;
216   case MachineOperand::MO_FrameIndex:
217     OS << "<fi#" << MO.getFrameIndex() << ">";
218     break;
219   case MachineOperand::MO_ConstantPoolIndex:
220     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
221     break;
222   case MachineOperand::MO_JumpTableIndex:
223     OS << "<jt#" << MO.getJumpTableIndex() << ">";
224     break;
225   case MachineOperand::MO_GlobalAddress:
226     OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
227     if (MO.getOffset()) OS << "+" << MO.getOffset();
228     OS << ">";
229     break;
230   case MachineOperand::MO_ExternalSymbol:
231     OS << "<es:" << MO.getSymbolName();
232     if (MO.getOffset()) OS << "+" << MO.getOffset();
233     OS << ">";
234     break;
235   default:
236     assert(0 && "Unrecognized operand type");
237   }
238 }
239
240 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
241   unsigned StartOp = 0;
242
243    // Specialize printing if op#0 is definition
244   if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
245     ::print(getOperand(0), OS, TM);
246     OS << " = ";
247     ++StartOp;   // Don't print this operand again!
248   }
249
250   // Must check if Target machine is not null because machine BB could not
251   // be attached to a Machine function yet
252   if (TM)
253     OS << TM->getInstrInfo()->getName(getOpcode());
254
255   for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
256     const MachineOperand& mop = getOperand(i);
257     if (i != StartOp)
258       OS << ",";
259     OS << " ";
260     ::print(mop, OS, TM);
261
262     if (mop.isReg()) {
263       if (mop.isDef() || mop.isKill() || mop.isDead() || mop.isImplicit()) {
264         OS << "<";
265         bool NeedComma = false;
266         if (mop.isImplicit()) {
267           OS << (mop.isDef() ? "imp-def" : "imp-use");
268           NeedComma = true;
269         } else if (mop.isDef()) {
270           OS << "def";
271           NeedComma = true;
272         }
273         if (mop.isKill() || mop.isDead()) {
274           if (NeedComma)
275             OS << ",";
276           if (mop.isKill())
277             OS << "kill";
278           if (mop.isDead())
279             OS << "dead";
280         }
281         OS << ">";
282       }
283     }
284   }
285
286   OS << "\n";
287 }
288
289 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
290   // If the instruction is embedded into a basic block, we can find the target
291   // info for the instruction.
292   if (const MachineBasicBlock *MBB = MI.getParent()) {
293     const MachineFunction *MF = MBB->getParent();
294     if (MF)
295       MI.print(os, &MF->getTarget());
296     else
297       MI.print(os, 0);
298     return os;
299   }
300
301   // Otherwise, print it out in the "raw" format without symbolic register names
302   // and such.
303   os << TargetInstrDescriptors[MI.getOpcode()].Name;
304
305   for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
306     os << "\t" << MI.getOperand(i);
307     if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
308       os << "<d>";
309   }
310
311   return os << "\n";
312 }
313
314 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
315   switch (MO.getType()) {
316   case MachineOperand::MO_Register:
317     OutputReg(OS, MO.getReg());
318     break;
319   case MachineOperand::MO_Immediate:
320     OS << (long)MO.getImmedValue();
321     break;
322   case MachineOperand::MO_MachineBasicBlock:
323     OS << "<mbb:"
324        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
325        << "@" << (void*)MO.getMachineBasicBlock() << ">";
326     break;
327   case MachineOperand::MO_FrameIndex:
328     OS << "<fi#" << MO.getFrameIndex() << ">";
329     break;
330   case MachineOperand::MO_ConstantPoolIndex:
331     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
332     break;
333   case MachineOperand::MO_JumpTableIndex:
334     OS << "<jt#" << MO.getJumpTableIndex() << ">";
335     break;
336   case MachineOperand::MO_GlobalAddress:
337     OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
338     break;
339   case MachineOperand::MO_ExternalSymbol:
340     OS << "<es:" << MO.getSymbolName() << ">";
341     break;
342   default:
343     assert(0 && "Unrecognized operand type");
344     break;
345   }
346
347   return OS;
348 }