blob: 2a1893cc3d00a2ec80811284781a54e0924e1e5c [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 Lattner3801f6d2002-02-03 07:46:01 +00006#include "llvm/Value.h"
Chris Lattner0be79c62002-10-28 02:28:39 +00007#include "llvm/Target/MachineInstrInfo.h" // FIXME: shouldn't need this!
Chris Lattner697954c2002-01-20 22:54:45 +00008using std::cerr;
Vikram S. Adve5b795912001-08-28 23:02:39 +00009
Ruchira Sasanka69917e22001-10-18 22:40:02 +000010
Vikram S. Adve1885da42001-07-31 21:49:28 +000011// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000012MachineInstr::MachineInstr(MachineOpCode _opCode,
13 OpCodeMask _opCodeMask)
Chris Lattner413746e2002-10-28 20:48:39 +000014 : opCode(_opCode), opCodeMask(_opCodeMask),
15 operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()) {
Vikram S. Adve1885da42001-07-31 21:49:28 +000016 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
17}
18
19// Constructor for instructions with variable #operands
Chris Lattner413746e2002-10-28 20:48:39 +000020MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands,
21 OpCodeMask OpCodeMask)
22 : opCode(OpCode), opCodeMask(OpCodeMask),
23 operands(numOperands, MachineOperand()) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000024}
25
Chris Lattner413746e2002-10-28 20:48:39 +000026// OperandComplete - Return true if it's illegal to add a new operand
27bool MachineInstr::OperandsComplete() const {
28 int NumOperands = TargetInstrDescriptors[opCode].numOperands;
29 if (NumOperands >= 0 && operands.size() >= (unsigned)NumOperands)
30 return true; // Broken!
31 return false;
32}
33
34
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000035//
36// Support for replacing opcode and operands of a MachineInstr in place.
37// This only resets the size of the operand vector and initializes it.
38// The new operands must be set explicitly later.
39//
Chris Lattner413746e2002-10-28 20:48:39 +000040void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands,
41 OpCodeMask Mask) {
42 opCode = Opcode;
43 opCodeMask = Mask;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000044 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +000045 operands.resize(numOperands, MachineOperand());
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000046}
47
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000048void
Chris Lattner413746e2002-10-28 20:48:39 +000049MachineInstr::SetMachineOperandVal(unsigned i,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000050 MachineOperand::MachineOperandType opType,
Chris Lattner572f5c82002-10-28 04:24:49 +000051 Value* V,
Chris Lattner0c0edf82002-07-25 06:17:51 +000052 bool isdef,
53 bool isDefAndUse)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000054{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000055 assert(i < operands.size());
Chris Lattner572f5c82002-10-28 04:24:49 +000056 operands[i].opType = opType;
57 operands[i].value = V;
58 operands[i].regNum = -1;
59 operands[i].flags = 0;
60
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +000061 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
62 operands[i].markDef();
63 if (isDefAndUse)
64 operands[i].markDefAndUse();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000065}
66
67void
Chris Lattner572f5c82002-10-28 04:24:49 +000068MachineInstr::SetMachineOperandConst(unsigned i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000069 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +000070 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000071{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000072 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000073 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
74 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +000075
76 operands[i].opType = operandType;
77 operands[i].value = NULL;
78 operands[i].immedVal = intValue;
79 operands[i].regNum = -1;
80 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000081}
82
83void
Chris Lattner572f5c82002-10-28 04:24:49 +000084MachineInstr::SetMachineOperandReg(unsigned i,
Vikram S. Advec356e562002-03-18 03:35:24 +000085 int regNum,
Chris Lattner2f305982002-10-28 19:46:59 +000086 bool isdef) {
Vikram S. Adve6a175e02001-07-28 04:06:37 +000087 assert(i < operands.size());
Chris Lattner572f5c82002-10-28 04:24:49 +000088
Chris Lattner2f305982002-10-28 19:46:59 +000089 operands[i].opType = MachineOperand::MO_MachineRegister;
Chris Lattner572f5c82002-10-28 04:24:49 +000090 operands[i].value = NULL;
91 operands[i].regNum = regNum;
92 operands[i].flags = 0;
93
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +000094 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
95 operands[i].markDef();
Chris Lattner27a08932002-10-22 23:16:21 +000096 insertUsedReg(regNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000097}
98
99void
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000100MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000101{
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000102 operands[i].setRegForValue(regNum);
Chris Lattner27a08932002-10-22 23:16:21 +0000103 insertUsedReg(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000104}
105
106
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000107// Subsitute all occurrences of Value* oldVal with newVal in all operands
108// and all implicit refs. If defsOnly == true, substitute defs only.
109unsigned
110MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
111{
112 unsigned numSubst = 0;
113
114 // Subsitute operands
115 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
116 if (*O == oldVal)
117 if (!defsOnly || O.isDef())
118 {
119 O.getMachineOperand().value = newVal;
120 ++numSubst;
121 }
122
123 // Subsitute implicit refs
124 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000125 if (getImplicitRef(i) == oldVal)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000126 if (!defsOnly || implicitRefIsDefined(i))
127 {
Chris Lattner27a08932002-10-22 23:16:21 +0000128 implicitRefs[i].Val = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000129 ++numSubst;
130 }
131
132 return numSubst;
133}
134
135
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000136void
137MachineInstr::dump() const
138{
139 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000140}
141
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000142static inline std::ostream&
143OutputValue(std::ostream &os, const Value* val)
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000144{
145 os << "(val ";
146 if (val && val->hasName())
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000147 return os << val->getName() << ")";
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000148 else
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000149 return os << (void*) val << ")"; // print address only
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000150}
151
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000152static inline std::ostream&
153OutputReg(std::ostream &os, unsigned int regNum)
154{
155 return os << "%mreg(" << regNum << ")";
156}
157
Chris Lattner697954c2002-01-20 22:54:45 +0000158std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000159{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000160 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000161
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000162 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000163 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000164 if( minstr.operandIsDefined(i) )
165 os << "*";
166 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000167 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000168 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000169
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000170 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000171 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
172 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000173 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000174 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000175 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000176 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000177 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000178 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000179 }
180 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000181
Chris Lattner697954c2002-01-20 22:54:45 +0000182 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000183}
184
Chris Lattner697954c2002-01-20 22:54:45 +0000185std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000186{
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000187 if (mop.opHiBits32())
188 os << "%lm(";
189 else if (mop.opLoBits32())
190 os << "%lo(";
191 else if (mop.opHiBits64())
192 os << "%hh(";
193 else if (mop.opLoBits64())
194 os << "%hm(";
195
Vikram S. Adve6e447182001-09-18 12:56:28 +0000196 switch(mop.opType)
197 {
198 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve6e447182001-09-18 12:56:28 +0000199 os << "%reg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000200 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000201 if (mop.hasAllocatedReg())
202 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000203 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000204 case MachineOperand::MO_CCRegister:
205 os << "%ccreg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000206 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000207 if (mop.hasAllocatedReg())
208 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000209 break;
210 case MachineOperand::MO_MachineRegister:
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000211 OutputReg(os, mop.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000212 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000213 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000214 os << (long)mop.immedVal;
215 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000216 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000217 os << (long)mop.immedVal;
218 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000219 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000220 {
221 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000222 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000223 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
224 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000225 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000226 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000227 os << (const void*) opVal;
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000228 os << ")";
229 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000230 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000231 default:
232 assert(0 && "Unrecognized operand type");
233 break;
234 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000235
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000236 if (mop.flags &
237 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
238 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
239 os << ")";
240
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000241 return os;
242}