blob: 5ec5c2ad05a02afa7278e4cae5d428abe4bcf351 [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 Lattner2a79a092002-10-30 00:58:19 +000010#include "llvm/Target/MRegisterInfo.h"
Chris Lattner697954c2002-01-20 22:54:45 +000011using std::cerr;
Vikram S. Adve5b795912001-08-28 23:02:39 +000012
Chris Lattnerf1757c42002-10-29 17:40:30 +000013// Global variable holding an array of descriptors for machine instructions.
14// The actual object needs to be created separately for each target machine.
15// This variable is initialized and reset by class MachineInstrInfo.
16//
17// FIXME: This should be a property of the target so that more than one target
18// at a time can be active...
19//
20extern const MachineInstrDescriptor *TargetInstrDescriptors;
Ruchira Sasanka69917e22001-10-18 22:40:02 +000021
Vikram S. Adve1885da42001-07-31 21:49:28 +000022// Constructor for instructions with fixed #operands (nearly all)
Chris Lattner72791222002-10-28 20:59:49 +000023MachineInstr::MachineInstr(MachineOpCode _opCode)
Chris Lattner9a8e4122002-10-28 21:17:20 +000024 : opCode(_opCode),
Vikram S. Advea2bae302002-10-29 19:41:18 +000025 operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()),
26 numImplicitRefs(0)
27{
Vikram S. Adve1885da42001-07-31 21:49:28 +000028 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
29}
30
31// Constructor for instructions with variable #operands
Chris Lattnerb98a53f2002-10-28 21:02:40 +000032MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands)
Vikram S. Advea2bae302002-10-29 19:41:18 +000033 : opCode(OpCode),
34 operands(numOperands, MachineOperand()),
35 numImplicitRefs(0)
36{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000037}
38
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000039/// 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///
Chris Lattner72791222002-10-28 20:59:49 +000044MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
Vikram S. Advea2bae302002-10-29 19:41:18 +000045 bool XX, bool YY)
46 : opCode(Opcode),
47 numImplicitRefs(0)
48{
Chris Lattner72791222002-10-28 20:59:49 +000049 operands.reserve(numOperands);
50}
51
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000052/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
53/// MachineInstr is created and added to the end of the specified basic block.
54///
55MachineInstr::MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode,
56 unsigned numOperands)
57 : opCode(Opcode),
58 numImplicitRefs(0)
59{
60 assert(MBB && "Cannot use inserting ctor with null basic block!");
61 operands.reserve(numOperands);
62 MBB->push_back(this); // Add instruction to end of basic block!
63}
64
65
Chris Lattner413746e2002-10-28 20:48:39 +000066// OperandComplete - Return true if it's illegal to add a new operand
Vikram S. Advea2bae302002-10-29 19:41:18 +000067bool MachineInstr::OperandsComplete() const
68{
Chris Lattner413746e2002-10-28 20:48:39 +000069 int NumOperands = TargetInstrDescriptors[opCode].numOperands;
Vikram S. Advea2bae302002-10-29 19:41:18 +000070 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Chris Lattner413746e2002-10-28 20:48:39 +000071 return true; // Broken!
72 return false;
73}
74
75
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000076//
77// Support for replacing opcode and operands of a MachineInstr in place.
78// This only resets the size of the operand vector and initializes it.
79// The new operands must be set explicitly later.
80//
Vikram S. Advea2bae302002-10-29 19:41:18 +000081void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands)
82{
83 assert(getNumImplicitRefs() == 0 &&
84 "This is probably broken because implicit refs are going to be lost.");
Chris Lattner413746e2002-10-28 20:48:39 +000085 opCode = Opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000086 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +000087 operands.resize(numOperands, MachineOperand());
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000088}
89
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000090void
Chris Lattner413746e2002-10-28 20:48:39 +000091MachineInstr::SetMachineOperandVal(unsigned i,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000092 MachineOperand::MachineOperandType opType,
Chris Lattner572f5c82002-10-28 04:24:49 +000093 Value* V,
Chris Lattner0c0edf82002-07-25 06:17:51 +000094 bool isdef,
95 bool isDefAndUse)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000096{
Vikram S. Advea2bae302002-10-29 19:41:18 +000097 assert(i < operands.size()); // may be explicit or implicit op
Chris Lattner572f5c82002-10-28 04:24:49 +000098 operands[i].opType = opType;
99 operands[i].value = V;
100 operands[i].regNum = -1;
101 operands[i].flags = 0;
102
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000103 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
104 operands[i].markDef();
105 if (isDefAndUse)
106 operands[i].markDefAndUse();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000107}
108
109void
Chris Lattner572f5c82002-10-28 04:24:49 +0000110MachineInstr::SetMachineOperandConst(unsigned i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000111 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +0000112 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000113{
Vikram S. Advea2bae302002-10-29 19:41:18 +0000114 assert(i < getNumOperands()); // must be explicit op
Vikram S. Advec356e562002-03-18 03:35:24 +0000115 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
116 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +0000117
118 operands[i].opType = operandType;
119 operands[i].value = NULL;
120 operands[i].immedVal = intValue;
121 operands[i].regNum = -1;
122 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000123}
124
125void
Chris Lattner572f5c82002-10-28 04:24:49 +0000126MachineInstr::SetMachineOperandReg(unsigned i,
Vikram S. Advec356e562002-03-18 03:35:24 +0000127 int regNum,
Chris Lattner2f305982002-10-28 19:46:59 +0000128 bool isdef) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000129 assert(i < getNumOperands()); // must be explicit op
Chris Lattner572f5c82002-10-28 04:24:49 +0000130
Chris Lattner2f305982002-10-28 19:46:59 +0000131 operands[i].opType = MachineOperand::MO_MachineRegister;
Chris Lattner572f5c82002-10-28 04:24:49 +0000132 operands[i].value = NULL;
133 operands[i].regNum = regNum;
134 operands[i].flags = 0;
135
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000136 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
137 operands[i].markDef();
Chris Lattner27a08932002-10-22 23:16:21 +0000138 insertUsedReg(regNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000139}
140
141void
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000142MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000143{
Vikram S. Advea2bae302002-10-29 19:41:18 +0000144 assert(i < getNumOperands()); // must be explicit op
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000145 operands[i].setRegForValue(regNum);
Chris Lattner27a08932002-10-22 23:16:21 +0000146 insertUsedReg(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000147}
148
149
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000150// Subsitute all occurrences of Value* oldVal with newVal in all operands
151// and all implicit refs. If defsOnly == true, substitute defs only.
152unsigned
153MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
154{
155 unsigned numSubst = 0;
156
157 // Subsitute operands
158 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
159 if (*O == oldVal)
160 if (!defsOnly || O.isDef())
161 {
162 O.getMachineOperand().value = newVal;
163 ++numSubst;
164 }
165
166 // Subsitute implicit refs
Vikram S. Advea2bae302002-10-29 19:41:18 +0000167 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000168 if (getImplicitRef(i) == oldVal)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000169 if (!defsOnly || implicitRefIsDefined(i))
170 {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000171 getImplicitOp(i).value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000172 ++numSubst;
173 }
174
175 return numSubst;
176}
177
178
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000179void
180MachineInstr::dump() const
181{
182 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000183}
184
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000185static inline std::ostream&
186OutputValue(std::ostream &os, const Value* val)
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000187{
188 os << "(val ";
189 if (val && val->hasName())
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000190 return os << val->getName() << ")";
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000191 else
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000192 return os << (void*) val << ")"; // print address only
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000193}
194
Chris Lattner2a79a092002-10-30 00:58:19 +0000195static inline void OutputReg(std::ostream &os, unsigned RegNo,
196 const MRegisterInfo *MRI = 0) {
197 if (MRI) {
198 if (RegNo < MRegisterInfo::FirstVirtualRegister)
199 os << "%" << MRI->get(RegNo).Name;
200 else
201 os << "%reg" << RegNo;
202 } else
203 os << "%mreg(" << RegNo << ")";
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000204}
205
Chris Lattner10491642002-10-30 00:48:05 +0000206static void print(const MachineOperand &MO, std::ostream &OS,
207 const TargetMachine &TM) {
Chris Lattner2a79a092002-10-30 00:58:19 +0000208 const MRegisterInfo *MRI = TM.getRegisterInfo();
Chris Lattner10491642002-10-30 00:48:05 +0000209 bool CloseParen = true;
210 if (MO.opHiBits32())
211 OS << "%lm(";
212 else if (MO.opLoBits32())
213 OS << "%lo(";
214 else if (MO.opHiBits64())
215 OS << "%hh(";
216 else if (MO.opLoBits64())
217 OS << "%hm(";
218 else
219 CloseParen = false;
220
221 switch (MO.getType()) {
222 case MachineOperand::MO_VirtualRegister:
223 if (MO.getVRegValue()) {
224 OS << "%reg";
225 OutputValue(OS, MO.getVRegValue());
226 if (MO.hasAllocatedReg())
227 OS << "==";
228 }
229 if (MO.hasAllocatedReg())
Chris Lattner2a79a092002-10-30 00:58:19 +0000230 OutputReg(OS, MO.getAllocatedRegNum(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000231 break;
232 case MachineOperand::MO_CCRegister:
233 OS << "%ccreg";
234 OutputValue(OS, MO.getVRegValue());
235 if (MO.hasAllocatedReg()) {
236 OS << "==";
Chris Lattner2a79a092002-10-30 00:58:19 +0000237 OutputReg(OS, MO.getAllocatedRegNum(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000238 }
239 break;
240 case MachineOperand::MO_MachineRegister:
Chris Lattner2a79a092002-10-30 00:58:19 +0000241 OutputReg(OS, MO.getMachineRegNum(), MRI);
Chris Lattner10491642002-10-30 00:48:05 +0000242 break;
243 case MachineOperand::MO_SignExtendedImmed:
244 OS << (long)MO.getImmedValue();
245 break;
246 case MachineOperand::MO_UnextendedImmed:
247 OS << (long)MO.getImmedValue();
248 break;
249 case MachineOperand::MO_PCRelativeDisp: {
250 const Value* opVal = MO.getVRegValue();
251 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
252 OS << "%disp(" << (isLabel? "label " : "addr-of-val ");
253 if (opVal->hasName())
254 OS << opVal->getName();
255 else
256 OS << (const void*) opVal;
257 OS << ")";
258 break;
259 }
260 default:
261 assert(0 && "Unrecognized operand type");
262 }
263
264 if (CloseParen)
265 OS << ")";
266}
267
268void MachineInstr::print(std::ostream &OS, const TargetMachine &TM) {
Chris Lattner6a592272002-10-30 01:55:38 +0000269 unsigned StartOp = 0;
Chris Lattner10491642002-10-30 00:48:05 +0000270
Chris Lattner6a592272002-10-30 01:55:38 +0000271 // Specialize printing if op#0 is definition
272 if (getNumOperands() && operandIsDefined(0)) {
273 ::print(getOperand(0), OS, TM);
274 OS << " = ";
275 ++StartOp; // Don't print this operand again!
276 }
277 OS << TM.getInstrInfo().getName(getOpcode());
278
279 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
280 if (i != StartOp)
281 OS << ",";
282 OS << " ";
283 ::print(getOperand(i), OS, TM);
284
Chris Lattner10491642002-10-30 00:48:05 +0000285 if (operandIsDefinedAndUsed(i))
286 OS << "<def&use>";
287 else if (operandIsDefined(i))
288 OS << "<def>";
289 }
Chris Lattner6a592272002-10-30 01:55:38 +0000290
Chris Lattner10491642002-10-30 00:48:05 +0000291 // code for printing implict references
292 if (getNumImplicitRefs()) {
293 OS << "\tImplicitRefs: ";
294 for(unsigned i = 0, e = getNumImplicitRefs(); i != e; ++i) {
295 OS << "\t";
296 OutputValue(OS, getImplicitRef(i));
297 if (implicitRefIsDefinedAndUsed(i))
298 OS << "<def&use>";
299 else if (implicitRefIsDefined(i))
300 OS << "<def>";
301 }
302 }
303
304 OS << "\n";
305}
306
307
Chris Lattner697954c2002-01-20 22:54:45 +0000308std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000309{
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000310 os << TargetInstrDescriptors[minstr.opCode].Name;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000311
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000312 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000313 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000314 if( minstr.operandIsDefined(i) )
315 os << "*";
316 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000317 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000318 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000319
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000320 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000321 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
322 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000323 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000324 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000325 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000326 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000327 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000328 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000329 }
330 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000331
Chris Lattner697954c2002-01-20 22:54:45 +0000332 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000333}
334
Chris Lattner697954c2002-01-20 22:54:45 +0000335std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000336{
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000337 if (mop.opHiBits32())
338 os << "%lm(";
339 else if (mop.opLoBits32())
340 os << "%lo(";
341 else if (mop.opHiBits64())
342 os << "%hh(";
343 else if (mop.opLoBits64())
344 os << "%hm(";
345
Chris Lattner10491642002-10-30 00:48:05 +0000346 switch (mop.getType())
Vikram S. Adve6e447182001-09-18 12:56:28 +0000347 {
348 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve6e447182001-09-18 12:56:28 +0000349 os << "%reg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000350 OutputValue(os, mop.getVRegValue());
Chris Lattner10491642002-10-30 00:48:05 +0000351 if (mop.hasAllocatedReg()) {
352 os << "==";
353 OutputReg(os, mop.getAllocatedRegNum());
354 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000355 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000356 case MachineOperand::MO_CCRegister:
357 os << "%ccreg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000358 OutputValue(os, mop.getVRegValue());
Chris Lattner10491642002-10-30 00:48:05 +0000359 if (mop.hasAllocatedReg()) {
360 os << "==";
361 OutputReg(os, mop.getAllocatedRegNum());
362 }
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000363 break;
364 case MachineOperand::MO_MachineRegister:
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000365 OutputReg(os, mop.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000366 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000367 case MachineOperand::MO_SignExtendedImmed:
Chris Lattner10491642002-10-30 00:48:05 +0000368 os << (long)mop.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000369 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000370 case MachineOperand::MO_UnextendedImmed:
Chris Lattner10491642002-10-30 00:48:05 +0000371 os << (long)mop.getImmedValue();
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000372 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000373 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000374 {
375 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000376 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000377 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
378 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000379 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000380 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000381 os << (const void*) opVal;
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000382 os << ")";
383 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000384 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000385 default:
386 assert(0 && "Unrecognized operand type");
387 break;
388 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000389
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000390 if (mop.flags &
391 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
392 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
393 os << ")";
394
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000395 return os;
396}