blob: 91b5ce7728ea228c0c994e8f7f177df62602d8fb [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
Chris Lattnerf1757c42002-10-29 17:40:30 +000010// Global variable holding an array of descriptors for machine instructions.
11// The actual object needs to be created separately for each target machine.
12// This variable is initialized and reset by class MachineInstrInfo.
13//
14// FIXME: This should be a property of the target so that more than one target
15// at a time can be active...
16//
17extern const MachineInstrDescriptor *TargetInstrDescriptors;
Ruchira Sasanka69917e22001-10-18 22:40:02 +000018
Vikram S. Adve1885da42001-07-31 21:49:28 +000019// Constructor for instructions with fixed #operands (nearly all)
Chris Lattner72791222002-10-28 20:59:49 +000020MachineInstr::MachineInstr(MachineOpCode _opCode)
Chris Lattner9a8e4122002-10-28 21:17:20 +000021 : opCode(_opCode),
Vikram S. Advea2bae302002-10-29 19:41:18 +000022 operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()),
23 numImplicitRefs(0)
24{
Vikram S. Adve1885da42001-07-31 21:49:28 +000025 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
26}
27
28// Constructor for instructions with variable #operands
Chris Lattnerb98a53f2002-10-28 21:02:40 +000029MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands)
Vikram S. Advea2bae302002-10-29 19:41:18 +000030 : opCode(OpCode),
31 operands(numOperands, MachineOperand()),
32 numImplicitRefs(0)
33{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000034}
35
Chris Lattner72791222002-10-28 20:59:49 +000036MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
Vikram S. Advea2bae302002-10-29 19:41:18 +000037 bool XX, bool YY)
38 : opCode(Opcode),
39 numImplicitRefs(0)
40{
Chris Lattner72791222002-10-28 20:59:49 +000041 operands.reserve(numOperands);
42}
43
Chris Lattner413746e2002-10-28 20:48:39 +000044// OperandComplete - Return true if it's illegal to add a new operand
Vikram S. Advea2bae302002-10-29 19:41:18 +000045bool MachineInstr::OperandsComplete() const
46{
Chris Lattner413746e2002-10-28 20:48:39 +000047 int NumOperands = TargetInstrDescriptors[opCode].numOperands;
Vikram S. Advea2bae302002-10-29 19:41:18 +000048 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
Chris Lattner413746e2002-10-28 20:48:39 +000049 return true; // Broken!
50 return false;
51}
52
53
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000054//
55// Support for replacing opcode and operands of a MachineInstr in place.
56// This only resets the size of the operand vector and initializes it.
57// The new operands must be set explicitly later.
58//
Vikram S. Advea2bae302002-10-29 19:41:18 +000059void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands)
60{
61 assert(getNumImplicitRefs() == 0 &&
62 "This is probably broken because implicit refs are going to be lost.");
Chris Lattner413746e2002-10-28 20:48:39 +000063 opCode = Opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000064 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +000065 operands.resize(numOperands, MachineOperand());
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000066}
67
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000068void
Chris Lattner413746e2002-10-28 20:48:39 +000069MachineInstr::SetMachineOperandVal(unsigned i,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000070 MachineOperand::MachineOperandType opType,
Chris Lattner572f5c82002-10-28 04:24:49 +000071 Value* V,
Chris Lattner0c0edf82002-07-25 06:17:51 +000072 bool isdef,
73 bool isDefAndUse)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000074{
Vikram S. Advea2bae302002-10-29 19:41:18 +000075 assert(i < operands.size()); // may be explicit or implicit op
Chris Lattner572f5c82002-10-28 04:24:49 +000076 operands[i].opType = opType;
77 operands[i].value = V;
78 operands[i].regNum = -1;
79 operands[i].flags = 0;
80
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +000081 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
82 operands[i].markDef();
83 if (isDefAndUse)
84 operands[i].markDefAndUse();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000085}
86
87void
Chris Lattner572f5c82002-10-28 04:24:49 +000088MachineInstr::SetMachineOperandConst(unsigned i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000089 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +000090 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000091{
Vikram S. Advea2bae302002-10-29 19:41:18 +000092 assert(i < getNumOperands()); // must be explicit op
Vikram S. Advec356e562002-03-18 03:35:24 +000093 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
94 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +000095
96 operands[i].opType = operandType;
97 operands[i].value = NULL;
98 operands[i].immedVal = intValue;
99 operands[i].regNum = -1;
100 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000101}
102
103void
Chris Lattner572f5c82002-10-28 04:24:49 +0000104MachineInstr::SetMachineOperandReg(unsigned i,
Vikram S. Advec356e562002-03-18 03:35:24 +0000105 int regNum,
Chris Lattner2f305982002-10-28 19:46:59 +0000106 bool isdef) {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000107 assert(i < getNumOperands()); // must be explicit op
Chris Lattner572f5c82002-10-28 04:24:49 +0000108
Chris Lattner2f305982002-10-28 19:46:59 +0000109 operands[i].opType = MachineOperand::MO_MachineRegister;
Chris Lattner572f5c82002-10-28 04:24:49 +0000110 operands[i].value = NULL;
111 operands[i].regNum = regNum;
112 operands[i].flags = 0;
113
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000114 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
115 operands[i].markDef();
Chris Lattner27a08932002-10-22 23:16:21 +0000116 insertUsedReg(regNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000117}
118
119void
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000120MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000121{
Vikram S. Advea2bae302002-10-29 19:41:18 +0000122 assert(i < getNumOperands()); // must be explicit op
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000123 operands[i].setRegForValue(regNum);
Chris Lattner27a08932002-10-22 23:16:21 +0000124 insertUsedReg(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000125}
126
127
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000128// Subsitute all occurrences of Value* oldVal with newVal in all operands
129// and all implicit refs. If defsOnly == true, substitute defs only.
130unsigned
131MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
132{
133 unsigned numSubst = 0;
134
135 // Subsitute operands
136 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
137 if (*O == oldVal)
138 if (!defsOnly || O.isDef())
139 {
140 O.getMachineOperand().value = newVal;
141 ++numSubst;
142 }
143
144 // Subsitute implicit refs
Vikram S. Advea2bae302002-10-29 19:41:18 +0000145 for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000146 if (getImplicitRef(i) == oldVal)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000147 if (!defsOnly || implicitRefIsDefined(i))
148 {
Vikram S. Advea2bae302002-10-29 19:41:18 +0000149 getImplicitOp(i).value = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000150 ++numSubst;
151 }
152
153 return numSubst;
154}
155
156
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000157void
158MachineInstr::dump() const
159{
160 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000161}
162
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000163static inline std::ostream&
164OutputValue(std::ostream &os, const Value* val)
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000165{
166 os << "(val ";
167 if (val && val->hasName())
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000168 return os << val->getName() << ")";
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000169 else
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000170 return os << (void*) val << ")"; // print address only
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000171}
172
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000173static inline std::ostream&
174OutputReg(std::ostream &os, unsigned int regNum)
175{
176 return os << "%mreg(" << regNum << ")";
177}
178
Chris Lattner697954c2002-01-20 22:54:45 +0000179std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000180{
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000181 os << TargetInstrDescriptors[minstr.opCode].Name;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000182
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000183 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000184 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000185 if( minstr.operandIsDefined(i) )
186 os << "*";
187 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000188 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000189 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000190
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000191 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000192 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
193 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000194 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000195 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000196 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000197 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000198 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000199 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000200 }
201 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000202
Chris Lattner697954c2002-01-20 22:54:45 +0000203 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000204}
205
Chris Lattner697954c2002-01-20 22:54:45 +0000206std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000207{
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000208 if (mop.opHiBits32())
209 os << "%lm(";
210 else if (mop.opLoBits32())
211 os << "%lo(";
212 else if (mop.opHiBits64())
213 os << "%hh(";
214 else if (mop.opLoBits64())
215 os << "%hm(";
216
Vikram S. Adve6e447182001-09-18 12:56:28 +0000217 switch(mop.opType)
218 {
219 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve6e447182001-09-18 12:56:28 +0000220 os << "%reg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000221 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000222 if (mop.hasAllocatedReg())
223 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000224 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000225 case MachineOperand::MO_CCRegister:
226 os << "%ccreg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000227 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000228 if (mop.hasAllocatedReg())
229 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000230 break;
231 case MachineOperand::MO_MachineRegister:
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000232 OutputReg(os, mop.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000233 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000234 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000235 os << (long)mop.immedVal;
236 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000237 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000238 os << (long)mop.immedVal;
239 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000240 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000241 {
242 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000243 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000244 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
245 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000246 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000247 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000248 os << (const void*) opVal;
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000249 os << ")";
250 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000251 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000252 default:
253 assert(0 && "Unrecognized operand type");
254 break;
255 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000256
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000257 if (mop.flags &
258 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
259 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
260 os << ")";
261
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000262 return os;
263}