blob: 1fde26a1b06efd41f869c2a62263088b25187e0a [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- MachineInstr.cpp --------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00002//
Chris Lattner035dfbe2002-08-09 20:08:06 +00003//===----------------------------------------------------------------------===//
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00004
Chris Lattner822b4fb2001-09-07 17:18:30 +00005#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerddd7fcb2002-10-29 23:19:00 +00006#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner3801f6d2002-02-03 07:46:01 +00007#include "llvm/Value.h"
Chris Lattner0be79c62002-10-28 02:28:39 +00008#include "llvm/Target/MachineInstrInfo.h" // FIXME: shouldn't need this!
Chris Lattner10491642002-10-30 00:48:05 +00009#include "llvm/Target/TargetMachine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000010using std::cerr;
Vikram S. Adve5b795912001-08-28 23:02:39 +000011
Chris Lattnerf1757c42002-10-29 17:40:30 +000012// Global variable holding an array of descriptors for machine instructions.
13// The actual object needs to be created separately for each target machine.
14// This variable is initialized and reset by class MachineInstrInfo.
15//
16// FIXME: This should be a property of the target so that more than one target
17// at a time can be active...
18//
19extern const MachineInstrDescriptor *TargetInstrDescriptors;
Ruchira Sasanka69917e22001-10-18 22:40:02 +000020
Vikram S. Adve1885da42001-07-31 21:49:28 +000021// Constructor for instructions with fixed #operands (nearly all)
Chris Lattner72791222002-10-28 20:59:49 +000022MachineInstr::MachineInstr(MachineOpCode _opCode)
Chris Lattner9a8e4122002-10-28 21:17:20 +000023 : opCode(_opCode),
Vikram S. Advea2bae302002-10-29 19:41:18 +000024 operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()),
25 numImplicitRefs(0)
26{
Vikram S. Adve1885da42001-07-31 21:49:28 +000027 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
28}
29
30// Constructor for instructions with variable #operands
Chris Lattnerb98a53f2002-10-28 21:02:40 +000031MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands)
Vikram S. Advea2bae302002-10-29 19:41:18 +000032 : opCode(OpCode),
33 operands(numOperands, MachineOperand()),
34 numImplicitRefs(0)
35{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000036}
37
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000038/// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
39/// not a resize for them. It is expected that if you use this that you call
40/// add* methods below to fill up the operands, instead of the Set methods.
41/// Eventually, the "resizing" ctors will be phased out.
42///
Chris Lattner72791222002-10-28 20:59:49 +000043MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
Vikram S. Advea2bae302002-10-29 19:41:18 +000044 bool XX, bool YY)
45 : opCode(Opcode),
46 numImplicitRefs(0)
47{
Chris Lattner72791222002-10-28 20:59:49 +000048 operands.reserve(numOperands);
49}
50
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000051/// 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///
54MachineInstr::MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode,
55 unsigned numOperands)
56 : opCode(Opcode),
57 numImplicitRefs(0)
58{
59 assert(MBB && "Cannot use inserting ctor with null basic block!");
60 operands.reserve(numOperands);
61 MBB->push_back(this); // Add instruction to end of basic block!
62}
63
64
Chris Lattner413746e2002-10-28 20:48:39 +000065// OperandComplete - Return true if it's illegal to add a new operand
Vikram S. Advea2bae302002-10-29 19:41:18 +000066bool MachineInstr::OperandsComplete() const
67{
Chris Lattner413746e2002-10-28 20:48:39 +000068 int NumOperands = TargetInstrDescriptors[opCode].numOperands;
Vikram S. Advea2bae302002-10-29 19:41:18 +000069 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Chris Lattner413746e2002-10-28 20:48:39 +000070 return true; // Broken!
71 return false;
72}
73
74
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000075//
76// Support for replacing opcode and operands of a MachineInstr in place.
77// This only resets the size of the operand vector and initializes it.
78// The new operands must be set explicitly later.
79//
Vikram S. Advea2bae302002-10-29 19:41:18 +000080void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands)
81{
82 assert(getNumImplicitRefs() == 0 &&
83 "This is probably broken because implicit refs are going to be lost.");
Chris Lattner413746e2002-10-28 20:48:39 +000084 opCode = Opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000085 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +000086 operands.resize(numOperands, MachineOperand());
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000087}
88
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000089void
Chris Lattner413746e2002-10-28 20:48:39 +000090MachineInstr::SetMachineOperandVal(unsigned i,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000091 MachineOperand::MachineOperandType opType,
Chris Lattner572f5c82002-10-28 04:24:49 +000092 Value* V,
Chris Lattner0c0edf82002-07-25 06:17:51 +000093 bool isdef,
94 bool isDefAndUse)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000095{
Vikram S. Advea2bae302002-10-29 19:41:18 +000096 assert(i < operands.size()); // may be explicit or implicit op
Chris Lattner572f5c82002-10-28 04:24:49 +000097 operands[i].opType = opType;
98 operands[i].value = V;
99 operands[i].regNum = -1;
100 operands[i].flags = 0;
101
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000102 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
103 operands[i].markDef();
104 if (isDefAndUse)
105 operands[i].markDefAndUse();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000106}
107
108void
Chris Lattner572f5c82002-10-28 04:24:49 +0000109MachineInstr::SetMachineOperandConst(unsigned i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000110 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +0000111 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000112{
Vikram S. Advea2bae302002-10-29 19:41:18 +0000113 assert(i < getNumOperands()); // must be explicit op
Vikram S. Advec356e562002-03-18 03:35:24 +0000114 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
115 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +0000116
117 operands[i].opType = operandType;
118 operands[i].value = NULL;
119 operands[i].immedVal = intValue;
120 operands[i].regNum = -1;
121 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000122}
123
124void
Chris Lattner572f5c82002-10-28 04:24:49 +0000125MachineInstr::SetMachineOperandReg(unsigned i,
Vikram S. Advec356e562002-03-18 03:35:24 +0000126 int regNum,
Chris Lattner2f305982002-10-28 19:46:59 +0000127 bool isdef) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000128 assert(i < getNumOperands()); // must be explicit op
Chris Lattner572f5c82002-10-28 04:24:49 +0000129
Chris Lattner2f305982002-10-28 19:46:59 +0000130 operands[i].opType = MachineOperand::MO_MachineRegister;
Chris Lattner572f5c82002-10-28 04:24:49 +0000131 operands[i].value = NULL;
132 operands[i].regNum = regNum;
133 operands[i].flags = 0;
134
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000135 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
136 operands[i].markDef();
Chris Lattner27a08932002-10-22 23:16:21 +0000137 insertUsedReg(regNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000138}
139
140void
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000141MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000142{
Vikram S. Advea2bae302002-10-29 19:41:18 +0000143 assert(i < getNumOperands()); // must be explicit op
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000144 operands[i].setRegForValue(regNum);
Chris Lattner27a08932002-10-22 23:16:21 +0000145 insertUsedReg(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000146}
147
148
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000149// Subsitute all occurrences of Value* oldVal with newVal in all operands
150// and all implicit refs. If defsOnly == true, substitute defs only.
151unsigned
152MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
153{
154 unsigned numSubst = 0;
155
156 // Subsitute operands
157 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
158 if (*O == oldVal)
159 if (!defsOnly || O.isDef())
160 {
161 O.getMachineOperand().value = newVal;
162 ++numSubst;
163 }
164
165 // Subsitute implicit refs
Vikram S. Advea2bae302002-10-29 19:41:18 +0000166 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000167 if (getImplicitRef(i) == oldVal)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000168 if (!defsOnly || implicitRefIsDefined(i))
169 {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000170 getImplicitOp(i).value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000171 ++numSubst;
172 }
173
174 return numSubst;
175}
176
177
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000178void
179MachineInstr::dump() const
180{
181 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000182}
183
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000184static inline std::ostream&
185OutputValue(std::ostream &os, const Value* val)
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000186{
187 os << "(val ";
188 if (val && val->hasName())
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000189 return os << val->getName() << ")";
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000190 else
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000191 return os << (void*) val << ")"; // print address only
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000192}
193
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000194static inline std::ostream&
195OutputReg(std::ostream &os, unsigned int regNum)
196{
197 return os << "%mreg(" << regNum << ")";
198}
199
Chris Lattner10491642002-10-30 00:48:05 +0000200static void print(const MachineOperand &MO, std::ostream &OS,
201 const TargetMachine &TM) {
202 bool CloseParen = true;
203 if (MO.opHiBits32())
204 OS << "%lm(";
205 else if (MO.opLoBits32())
206 OS << "%lo(";
207 else if (MO.opHiBits64())
208 OS << "%hh(";
209 else if (MO.opLoBits64())
210 OS << "%hm(";
211 else
212 CloseParen = false;
213
214 switch (MO.getType()) {
215 case MachineOperand::MO_VirtualRegister:
216 if (MO.getVRegValue()) {
217 OS << "%reg";
218 OutputValue(OS, MO.getVRegValue());
219 if (MO.hasAllocatedReg())
220 OS << "==";
221 }
222 if (MO.hasAllocatedReg())
223 OutputReg(OS, MO.getAllocatedRegNum());
224 break;
225 case MachineOperand::MO_CCRegister:
226 OS << "%ccreg";
227 OutputValue(OS, MO.getVRegValue());
228 if (MO.hasAllocatedReg()) {
229 OS << "==";
230 OutputReg(OS, MO.getAllocatedRegNum());
231 }
232 break;
233 case MachineOperand::MO_MachineRegister:
234 OutputReg(OS, MO.getMachineRegNum());
235 break;
236 case MachineOperand::MO_SignExtendedImmed:
237 OS << (long)MO.getImmedValue();
238 break;
239 case MachineOperand::MO_UnextendedImmed:
240 OS << (long)MO.getImmedValue();
241 break;
242 case MachineOperand::MO_PCRelativeDisp: {
243 const Value* opVal = MO.getVRegValue();
244 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
245 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
246 if (opVal->hasName())
247 OS << opVal->getName();
248 else
249 OS << (const void*) opVal;
250 OS << ")";
251 break;
252 }
253 default:
254 assert(0 && "Unrecognized operand type");
255 }
256
257 if (CloseParen)
258 OS << ")";
259}
260
261void MachineInstr::print(std::ostream &OS, const TargetMachine &TM) {
262 OS << TM.getInstrInfo().getName(getOpcode());
263 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
264 OS << "\t";
265 ::print(getOperand(i), OS, TM);
266
267 if (operandIsDefinedAndUsed(i))
268 OS << "<def&use>";
269 else if (operandIsDefined(i))
270 OS << "<def>";
271 }
272
273 // code for printing implict references
274 if (getNumImplicitRefs()) {
275 OS << "\tImplicitRefs: ";
276 for(unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) {
277 OS << "\t";
278 OutputValue(OS, getImplicitRef(i));
279 if (implicitRefIsDefinedAndUsed(i))
280 OS << "<def&use>";
281 else if (implicitRefIsDefined(i))
282 OS << "<def>";
283 }
284 }
285
286 OS << "\n";
287}
288
289
Chris Lattner697954c2002-01-20 22:54:45 +0000290std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000291{
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000292 os << TargetInstrDescriptors[minstr.opCode].Name;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000293
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000294 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000295 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000296 if( minstr.operandIsDefined(i) )
297 os << "*";
298 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000299 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000300 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000301
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000302 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000303 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
304 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000305 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000306 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000307 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000308 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000309 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000310 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000311 }
312 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000313
Chris Lattner697954c2002-01-20 22:54:45 +0000314 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000315}
316
Chris Lattner697954c2002-01-20 22:54:45 +0000317std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000318{
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000319 if (mop.opHiBits32())
320 os << "%lm(";
321 else if (mop.opLoBits32())
322 os << "%lo(";
323 else if (mop.opHiBits64())
324 os << "%hh(";
325 else if (mop.opLoBits64())
326 os << "%hm(";
327
Chris Lattner10491642002-10-30 00:48:05 +0000328 switch (mop.getType())
Vikram S. Adve6e447182001-09-18 12:56:28 +0000329 {
330 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve6e447182001-09-18 12:56:28 +0000331 os << "%reg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000332 OutputValue(os, mop.getVRegValue());
Chris Lattner10491642002-10-30 00:48:05 +0000333 if (mop.hasAllocatedReg()) {
334 os << "==";
335 OutputReg(os, mop.getAllocatedRegNum());
336 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000337 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000338 case MachineOperand::MO_CCRegister:
339 os << "%ccreg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000340 OutputValue(os, mop.getVRegValue());
Chris Lattner10491642002-10-30 00:48:05 +0000341 if (mop.hasAllocatedReg()) {
342 os << "==";
343 OutputReg(os, mop.getAllocatedRegNum());
344 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000345 break;
346 case MachineOperand::MO_MachineRegister:
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000347 OutputReg(os, mop.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000348 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000349 case MachineOperand::MO_SignExtendedImmed:
Chris Lattner10491642002-10-30 00:48:05 +0000350 os << (long)mop.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000351 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000352 case MachineOperand::MO_UnextendedImmed:
Chris Lattner10491642002-10-30 00:48:05 +0000353 os << (long)mop.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000354 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000355 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000356 {
357 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000358 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000359 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
360 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000361 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000362 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000363 os << (const void*) opVal;
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000364 os << ")";
365 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000366 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000367 default:
368 assert(0 && "Unrecognized operand type");
369 break;
370 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000371
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000372 if (mop.flags &
373 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
374 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
375 os << ")";
376
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000377 return os;
378}