blob: 605fe16c9b114dc67c294f1420ea293cf8a0f11c [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 Lattner697954c2002-01-20 22:54:45 +00009using std::cerr;
Vikram S. Adve5b795912001-08-28 23:02:39 +000010
Chris Lattnerf1757c42002-10-29 17:40:30 +000011// Global variable holding an array of descriptors for machine instructions.
12// The actual object needs to be created separately for each target machine.
13// This variable is initialized and reset by class MachineInstrInfo.
14//
15// FIXME: This should be a property of the target so that more than one target
16// at a time can be active...
17//
18extern const MachineInstrDescriptor *TargetInstrDescriptors;
Ruchira Sasanka69917e22001-10-18 22:40:02 +000019
Vikram S. Adve1885da42001-07-31 21:49:28 +000020// Constructor for instructions with fixed #operands (nearly all)
Chris Lattner72791222002-10-28 20:59:49 +000021MachineInstr::MachineInstr(MachineOpCode _opCode)
Chris Lattner9a8e4122002-10-28 21:17:20 +000022 : opCode(_opCode),
Vikram S. Advea2bae302002-10-29 19:41:18 +000023 operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()),
24 numImplicitRefs(0)
25{
Vikram S. Adve1885da42001-07-31 21:49:28 +000026 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
27}
28
29// Constructor for instructions with variable #operands
Chris Lattnerb98a53f2002-10-28 21:02:40 +000030MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands)
Vikram S. Advea2bae302002-10-29 19:41:18 +000031 : opCode(OpCode),
32 operands(numOperands, MachineOperand()),
33 numImplicitRefs(0)
34{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000035}
36
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000037/// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
38/// not a resize for them. It is expected that if you use this that you call
39/// add* methods below to fill up the operands, instead of the Set methods.
40/// Eventually, the "resizing" ctors will be phased out.
41///
Chris Lattner72791222002-10-28 20:59:49 +000042MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
Vikram S. Advea2bae302002-10-29 19:41:18 +000043 bool XX, bool YY)
44 : opCode(Opcode),
45 numImplicitRefs(0)
46{
Chris Lattner72791222002-10-28 20:59:49 +000047 operands.reserve(numOperands);
48}
49
Chris Lattnerddd7fcb2002-10-29 23:19:00 +000050/// MachineInstr ctor - Work exactly the same as the ctor above, except that the
51/// MachineInstr is created and added to the end of the specified basic block.
52///
53MachineInstr::MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode,
54 unsigned numOperands)
55 : opCode(Opcode),
56 numImplicitRefs(0)
57{
58 assert(MBB && "Cannot use inserting ctor with null basic block!");
59 operands.reserve(numOperands);
60 MBB->push_back(this); // Add instruction to end of basic block!
61}
62
63
Chris Lattner413746e2002-10-28 20:48:39 +000064// OperandComplete - Return true if it's illegal to add a new operand
Vikram S. Advea2bae302002-10-29 19:41:18 +000065bool MachineInstr::OperandsComplete() const
66{
Chris Lattner413746e2002-10-28 20:48:39 +000067 int NumOperands = TargetInstrDescriptors[opCode].numOperands;
Vikram S. Advea2bae302002-10-29 19:41:18 +000068 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Chris Lattner413746e2002-10-28 20:48:39 +000069 return true; // Broken!
70 return false;
71}
72
73
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000074//
75// Support for replacing opcode and operands of a MachineInstr in place.
76// This only resets the size of the operand vector and initializes it.
77// The new operands must be set explicitly later.
78//
Vikram S. Advea2bae302002-10-29 19:41:18 +000079void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands)
80{
81 assert(getNumImplicitRefs() == 0 &&
82 "This is probably broken because implicit refs are going to be lost.");
Chris Lattner413746e2002-10-28 20:48:39 +000083 opCode = Opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000084 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +000085 operands.resize(numOperands, MachineOperand());
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000086}
87
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000088void
Chris Lattner413746e2002-10-28 20:48:39 +000089MachineInstr::SetMachineOperandVal(unsigned i,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000090 MachineOperand::MachineOperandType opType,
Chris Lattner572f5c82002-10-28 04:24:49 +000091 Value* V,
Chris Lattner0c0edf82002-07-25 06:17:51 +000092 bool isdef,
93 bool isDefAndUse)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000094{
Vikram S. Advea2bae302002-10-29 19:41:18 +000095 assert(i < operands.size()); // may be explicit or implicit op
Chris Lattner572f5c82002-10-28 04:24:49 +000096 operands[i].opType = opType;
97 operands[i].value = V;
98 operands[i].regNum = -1;
99 operands[i].flags = 0;
100
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000101 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
102 operands[i].markDef();
103 if (isDefAndUse)
104 operands[i].markDefAndUse();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000105}
106
107void
Chris Lattner572f5c82002-10-28 04:24:49 +0000108MachineInstr::SetMachineOperandConst(unsigned i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000109 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +0000110 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000111{
Vikram S. Advea2bae302002-10-29 19:41:18 +0000112 assert(i < getNumOperands()); // must be explicit op
Vikram S. Advec356e562002-03-18 03:35:24 +0000113 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
114 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +0000115
116 operands[i].opType = operandType;
117 operands[i].value = NULL;
118 operands[i].immedVal = intValue;
119 operands[i].regNum = -1;
120 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000121}
122
123void
Chris Lattner572f5c82002-10-28 04:24:49 +0000124MachineInstr::SetMachineOperandReg(unsigned i,
Vikram S. Advec356e562002-03-18 03:35:24 +0000125 int regNum,
Chris Lattner2f305982002-10-28 19:46:59 +0000126 bool isdef) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000127 assert(i < getNumOperands()); // must be explicit op
Chris Lattner572f5c82002-10-28 04:24:49 +0000128
Chris Lattner2f305982002-10-28 19:46:59 +0000129 operands[i].opType = MachineOperand::MO_MachineRegister;
Chris Lattner572f5c82002-10-28 04:24:49 +0000130 operands[i].value = NULL;
131 operands[i].regNum = regNum;
132 operands[i].flags = 0;
133
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000134 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
135 operands[i].markDef();
Chris Lattner27a08932002-10-22 23:16:21 +0000136 insertUsedReg(regNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000137}
138
139void
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000140MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000141{
Vikram S. Advea2bae302002-10-29 19:41:18 +0000142 assert(i < getNumOperands()); // must be explicit op
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000143 operands[i].setRegForValue(regNum);
Chris Lattner27a08932002-10-22 23:16:21 +0000144 insertUsedReg(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000145}
146
147
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000148// Subsitute all occurrences of Value* oldVal with newVal in all operands
149// and all implicit refs. If defsOnly == true, substitute defs only.
150unsigned
151MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
152{
153 unsigned numSubst = 0;
154
155 // Subsitute operands
156 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
157 if (*O == oldVal)
158 if (!defsOnly || O.isDef())
159 {
160 O.getMachineOperand().value = newVal;
161 ++numSubst;
162 }
163
164 // Subsitute implicit refs
Vikram S. Advea2bae302002-10-29 19:41:18 +0000165 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000166 if (getImplicitRef(i) == oldVal)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000167 if (!defsOnly || implicitRefIsDefined(i))
168 {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000169 getImplicitOp(i).value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000170 ++numSubst;
171 }
172
173 return numSubst;
174}
175
176
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000177void
178MachineInstr::dump() const
179{
180 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000181}
182
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000183static inline std::ostream&
184OutputValue(std::ostream &os, const Value* val)
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000185{
186 os << "(val ";
187 if (val && val->hasName())
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000188 return os << val->getName() << ")";
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000189 else
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000190 return os << (void*) val << ")"; // print address only
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000191}
192
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000193static inline std::ostream&
194OutputReg(std::ostream &os, unsigned int regNum)
195{
196 return os << "%mreg(" << regNum << ")";
197}
198
Chris Lattner697954c2002-01-20 22:54:45 +0000199std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000200{
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000201 os << TargetInstrDescriptors[minstr.opCode].Name;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000202
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000203 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000204 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000205 if( minstr.operandIsDefined(i) )
206 os << "*";
207 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000208 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000209 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000210
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000211 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000212 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
213 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000214 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000215 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000216 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000217 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000218 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000219 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000220 }
221 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000222
Chris Lattner697954c2002-01-20 22:54:45 +0000223 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000224}
225
Chris Lattner697954c2002-01-20 22:54:45 +0000226std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000227{
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000228 if (mop.opHiBits32())
229 os << "%lm(";
230 else if (mop.opLoBits32())
231 os << "%lo(";
232 else if (mop.opHiBits64())
233 os << "%hh(";
234 else if (mop.opLoBits64())
235 os << "%hm(";
236
Vikram S. Adve6e447182001-09-18 12:56:28 +0000237 switch(mop.opType)
238 {
239 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve6e447182001-09-18 12:56:28 +0000240 os << "%reg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000241 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000242 if (mop.hasAllocatedReg())
243 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000244 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000245 case MachineOperand::MO_CCRegister:
246 os << "%ccreg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000247 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000248 if (mop.hasAllocatedReg())
249 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000250 break;
251 case MachineOperand::MO_MachineRegister:
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000252 OutputReg(os, mop.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000253 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000254 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000255 os << (long)mop.immedVal;
256 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000257 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000258 os << (long)mop.immedVal;
259 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000260 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000261 {
262 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000263 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000264 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
265 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000266 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000267 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000268 os << (const void*) opVal;
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000269 os << ")";
270 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000271 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000272 default:
273 assert(0 && "Unrecognized operand type");
274 break;
275 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000276
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000277 if (mop.flags &
278 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
279 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
280 os << ")";
281
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000282 return os;
283}