blob: 367774ac8ac5ee25c739be13a1bcc6846c7cebb7 [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)
14 : opCode(_opCode),
15 opCodeMask(_opCodeMask),
Vikram S. Adve6a175e02001-07-28 04:06:37 +000016 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000017{
Vikram S. Adve1885da42001-07-31 21:49:28 +000018 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
19}
20
21// Constructor for instructions with variable #operands
22MachineInstr::MachineInstr(MachineOpCode _opCode,
23 unsigned numOperands,
24 OpCodeMask _opCodeMask)
25 : opCode(_opCode),
26 opCodeMask(_opCodeMask),
27 operands(numOperands)
28{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000029}
30
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000031//
32// Support for replacing opcode and operands of a MachineInstr in place.
33// This only resets the size of the operand vector and initializes it.
34// The new operands must be set explicitly later.
35//
36void
37MachineInstr::replace(MachineOpCode _opCode,
38 unsigned numOperands,
39 OpCodeMask _opCodeMask)
40{
41 opCode = _opCode;
42 opCodeMask = _opCodeMask;
43 operands.clear();
44 operands.resize(numOperands);
45}
46
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000047void
Vikram S. Advec356e562002-03-18 03:35:24 +000048MachineInstr::SetMachineOperandVal(unsigned int i,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000049 MachineOperand::MachineOperandType opType,
Chris Lattner572f5c82002-10-28 04:24:49 +000050 Value* V,
Chris Lattner0c0edf82002-07-25 06:17:51 +000051 bool isdef,
52 bool isDefAndUse)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000053{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000054 assert(i < operands.size());
Chris Lattner572f5c82002-10-28 04:24:49 +000055 operands[i].opType = opType;
56 operands[i].value = V;
57 operands[i].regNum = -1;
58 operands[i].flags = 0;
59
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +000060 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
61 operands[i].markDef();
62 if (isDefAndUse)
63 operands[i].markDefAndUse();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000064}
65
66void
Chris Lattner572f5c82002-10-28 04:24:49 +000067MachineInstr::SetMachineOperandConst(unsigned i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000068 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +000069 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000070{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000071 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000072 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
73 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +000074
75 operands[i].opType = operandType;
76 operands[i].value = NULL;
77 operands[i].immedVal = intValue;
78 operands[i].regNum = -1;
79 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000080}
81
82void
Chris Lattner572f5c82002-10-28 04:24:49 +000083MachineInstr::SetMachineOperandReg(unsigned i,
Vikram S. Advec356e562002-03-18 03:35:24 +000084 int regNum,
Chris Lattner0c0edf82002-07-25 06:17:51 +000085 bool isdef,
86 bool isDefAndUse,
87 bool isCCReg)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000088{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000089 assert(i < operands.size());
Chris Lattner572f5c82002-10-28 04:24:49 +000090
91 operands[i].opType =
92 isCCReg? MachineOperand::MO_CCRegister : MachineOperand::MO_MachineRegister;
93 operands[i].value = NULL;
94 operands[i].regNum = regNum;
95 operands[i].flags = 0;
96
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +000097 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
98 operands[i].markDef();
99 if (isDefAndUse)
100 operands[i].markDefAndUse();
Chris Lattner27a08932002-10-22 23:16:21 +0000101 insertUsedReg(regNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000102}
103
104void
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000105MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000106{
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000107 operands[i].setRegForValue(regNum);
Chris Lattner27a08932002-10-22 23:16:21 +0000108 insertUsedReg(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000109}
110
111
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000112// Subsitute all occurrences of Value* oldVal with newVal in all operands
113// and all implicit refs. If defsOnly == true, substitute defs only.
114unsigned
115MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
116{
117 unsigned numSubst = 0;
118
119 // Subsitute operands
120 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
121 if (*O == oldVal)
122 if (!defsOnly || O.isDef())
123 {
124 O.getMachineOperand().value = newVal;
125 ++numSubst;
126 }
127
128 // Subsitute implicit refs
129 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000130 if (getImplicitRef(i) == oldVal)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000131 if (!defsOnly || implicitRefIsDefined(i))
132 {
Chris Lattner27a08932002-10-22 23:16:21 +0000133 implicitRefs[i].Val = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000134 ++numSubst;
135 }
136
137 return numSubst;
138}
139
140
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000141void
142MachineInstr::dump() const
143{
144 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000145}
146
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000147static inline std::ostream&
148OutputValue(std::ostream &os, const Value* val)
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000149{
150 os << "(val ";
151 if (val && val->hasName())
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000152 return os << val->getName() << ")";
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000153 else
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000154 return os << (void*) val << ")"; // print address only
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000155}
156
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000157static inline std::ostream&
158OutputReg(std::ostream &os, unsigned int regNum)
159{
160 return os << "%mreg(" << regNum << ")";
161}
162
Chris Lattner697954c2002-01-20 22:54:45 +0000163std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000164{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000165 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000166
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000167 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000168 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000169 if( minstr.operandIsDefined(i) )
170 os << "*";
171 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000172 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000173 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000174
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000175 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000176 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
177 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000178 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000179 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000180 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000181 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000182 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000183 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000184 }
185 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000186
Chris Lattner697954c2002-01-20 22:54:45 +0000187 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000188}
189
Chris Lattner697954c2002-01-20 22:54:45 +0000190std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000191{
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000192 if (mop.opHiBits32())
193 os << "%lm(";
194 else if (mop.opLoBits32())
195 os << "%lo(";
196 else if (mop.opHiBits64())
197 os << "%hh(";
198 else if (mop.opLoBits64())
199 os << "%hm(";
200
Vikram S. Adve6e447182001-09-18 12:56:28 +0000201 switch(mop.opType)
202 {
203 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve6e447182001-09-18 12:56:28 +0000204 os << "%reg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000205 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000206 if (mop.hasAllocatedReg())
207 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000208 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000209 case MachineOperand::MO_CCRegister:
210 os << "%ccreg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000211 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000212 if (mop.hasAllocatedReg())
213 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000214 break;
215 case MachineOperand::MO_MachineRegister:
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000216 OutputReg(os, mop.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000217 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000218 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000219 os << (long)mop.immedVal;
220 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000221 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000222 os << (long)mop.immedVal;
223 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000224 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000225 {
226 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000227 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000228 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
229 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000230 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000231 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000232 os << (const void*) opVal;
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000233 os << ")";
234 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000235 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000236 default:
237 assert(0 && "Unrecognized operand type");
238 break;
239 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000240
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000241 if (mop.flags &
242 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
243 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
244 os << ")";
245
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000246 return os;
247}