blob: 2d2487e8687643e9e4c3c13c65faccca1558b0a2 [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),
Chris Lattner413746e2002-10-28 20:48:39 +000022 operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()) {
Vikram S. Adve1885da42001-07-31 21:49:28 +000023 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
24}
25
26// Constructor for instructions with variable #operands
Chris Lattnerb98a53f2002-10-28 21:02:40 +000027MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned numOperands)
Chris Lattner9a8e4122002-10-28 21:17:20 +000028 : opCode(OpCode), operands(numOperands, MachineOperand()) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000029}
30
Chris Lattner72791222002-10-28 20:59:49 +000031MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
Chris Lattner9a8e4122002-10-28 21:17:20 +000032 bool XX, bool YY) : opCode(Opcode) {
Chris Lattner72791222002-10-28 20:59:49 +000033 operands.reserve(numOperands);
34}
35
Chris Lattner413746e2002-10-28 20:48:39 +000036// OperandComplete - Return true if it's illegal to add a new operand
37bool MachineInstr::OperandsComplete() const {
38 int NumOperands = TargetInstrDescriptors[opCode].numOperands;
39 if (NumOperands >= 0 && operands.size() >= (unsigned)NumOperands)
40 return true; // Broken!
41 return false;
42}
43
44
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000045//
46// Support for replacing opcode and operands of a MachineInstr in place.
47// This only resets the size of the operand vector and initializes it.
48// The new operands must be set explicitly later.
49//
Chris Lattnerb98a53f2002-10-28 21:02:40 +000050void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands) {
Chris Lattner413746e2002-10-28 20:48:39 +000051 opCode = Opcode;
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000052 operands.clear();
Chris Lattner413746e2002-10-28 20:48:39 +000053 operands.resize(numOperands, MachineOperand());
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000054}
55
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000056void
Chris Lattner413746e2002-10-28 20:48:39 +000057MachineInstr::SetMachineOperandVal(unsigned i,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000058 MachineOperand::MachineOperandType opType,
Chris Lattner572f5c82002-10-28 04:24:49 +000059 Value* V,
Chris Lattner0c0edf82002-07-25 06:17:51 +000060 bool isdef,
61 bool isDefAndUse)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000062{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000063 assert(i < operands.size());
Chris Lattner572f5c82002-10-28 04:24:49 +000064 operands[i].opType = opType;
65 operands[i].value = V;
66 operands[i].regNum = -1;
67 operands[i].flags = 0;
68
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +000069 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
70 operands[i].markDef();
71 if (isDefAndUse)
72 operands[i].markDefAndUse();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000073}
74
75void
Chris Lattner572f5c82002-10-28 04:24:49 +000076MachineInstr::SetMachineOperandConst(unsigned i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000077 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +000078 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000079{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000080 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000081 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
82 "immed. constant cannot be defined");
Chris Lattner572f5c82002-10-28 04:24:49 +000083
84 operands[i].opType = operandType;
85 operands[i].value = NULL;
86 operands[i].immedVal = intValue;
87 operands[i].regNum = -1;
88 operands[i].flags = 0;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000089}
90
91void
Chris Lattner572f5c82002-10-28 04:24:49 +000092MachineInstr::SetMachineOperandReg(unsigned i,
Vikram S. Advec356e562002-03-18 03:35:24 +000093 int regNum,
Chris Lattner2f305982002-10-28 19:46:59 +000094 bool isdef) {
Vikram S. Adve6a175e02001-07-28 04:06:37 +000095 assert(i < operands.size());
Chris Lattner572f5c82002-10-28 04:24:49 +000096
Chris Lattner2f305982002-10-28 19:46:59 +000097 operands[i].opType = MachineOperand::MO_MachineRegister;
Chris Lattner572f5c82002-10-28 04:24:49 +000098 operands[i].value = NULL;
99 operands[i].regNum = regNum;
100 operands[i].flags = 0;
101
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000102 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
103 operands[i].markDef();
Chris Lattner27a08932002-10-22 23:16:21 +0000104 insertUsedReg(regNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000105}
106
107void
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000108MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000109{
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000110 operands[i].setRegForValue(regNum);
Chris Lattner27a08932002-10-22 23:16:21 +0000111 insertUsedReg(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000112}
113
114
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000115// Subsitute all occurrences of Value* oldVal with newVal in all operands
116// and all implicit refs. If defsOnly == true, substitute defs only.
117unsigned
118MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
119{
120 unsigned numSubst = 0;
121
122 // Subsitute operands
123 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
124 if (*O == oldVal)
125 if (!defsOnly || O.isDef())
126 {
127 O.getMachineOperand().value = newVal;
128 ++numSubst;
129 }
130
131 // Subsitute implicit refs
132 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000133 if (getImplicitRef(i) == oldVal)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000134 if (!defsOnly || implicitRefIsDefined(i))
135 {
Chris Lattner27a08932002-10-22 23:16:21 +0000136 implicitRefs[i].Val = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000137 ++numSubst;
138 }
139
140 return numSubst;
141}
142
143
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000144void
145MachineInstr::dump() const
146{
147 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000148}
149
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000150static inline std::ostream&
151OutputValue(std::ostream &os, const Value* val)
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000152{
153 os << "(val ";
154 if (val && val->hasName())
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000155 return os << val->getName() << ")";
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000156 else
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000157 return os << (void*) val << ")"; // print address only
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000158}
159
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000160static inline std::ostream&
161OutputReg(std::ostream &os, unsigned int regNum)
162{
163 return os << "%mreg(" << regNum << ")";
164}
165
Chris Lattner697954c2002-01-20 22:54:45 +0000166std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000167{
Chris Lattnerd9512ca2002-10-29 17:35:39 +0000168 os << TargetInstrDescriptors[minstr.opCode].Name;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000169
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000170 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000171 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000172 if( minstr.operandIsDefined(i) )
173 os << "*";
174 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000175 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000176 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000177
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000178 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000179 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
180 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000181 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000182 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000183 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000184 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000185 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000186 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000187 }
188 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000189
Chris Lattner697954c2002-01-20 22:54:45 +0000190 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000191}
192
Chris Lattner697954c2002-01-20 22:54:45 +0000193std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000194{
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000195 if (mop.opHiBits32())
196 os << "%lm(";
197 else if (mop.opLoBits32())
198 os << "%lo(";
199 else if (mop.opHiBits64())
200 os << "%hh(";
201 else if (mop.opLoBits64())
202 os << "%hm(";
203
Vikram S. Adve6e447182001-09-18 12:56:28 +0000204 switch(mop.opType)
205 {
206 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve6e447182001-09-18 12:56:28 +0000207 os << "%reg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000208 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000209 if (mop.hasAllocatedReg())
210 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000211 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000212 case MachineOperand::MO_CCRegister:
213 os << "%ccreg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000214 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000215 if (mop.hasAllocatedReg())
216 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000217 break;
218 case MachineOperand::MO_MachineRegister:
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000219 OutputReg(os, mop.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000220 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000221 case MachineOperand::MO_SignExtendedImmed:
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_UnextendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000225 os << (long)mop.immedVal;
226 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000227 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000228 {
229 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000230 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000231 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
232 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000233 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000234 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000235 os << (const void*) opVal;
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000236 os << ")";
237 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000238 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000239 default:
240 assert(0 && "Unrecognized operand type");
241 break;
242 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000243
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000244 if (mop.flags &
245 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
246 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
247 os << ")";
248
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000249 return os;
250}