blob: cd439e3aee5091c493397ec44da11ed108cfa915 [file] [log] [blame]
Chris Lattner959a5fb2002-08-09 20:08:06 +00001//===-- MachineInstr.cpp --------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +00002//
Chris Lattner959a5fb2002-08-09 20:08:06 +00003//===----------------------------------------------------------------------===//
Vikram S. Adveab9e5572001-07-21 12:41:50 +00004
Chris Lattner23fcc082001-09-07 17:18:30 +00005#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner4cec1472002-02-03 07:46:01 +00006#include "llvm/Value.h"
Chris Lattner9668c8c2002-10-28 02:28:39 +00007#include "llvm/Target/MachineInstrInfo.h" // FIXME: shouldn't need this!
Chris Lattner7f74a562002-01-20 22:54:45 +00008using std::cerr;
Vikram S. Adve5f72f422001-08-28 23:02:39 +00009
Ruchira Sasanka59e864e2001-10-18 22:40:02 +000010
Vikram S. Adveff7070b2001-07-31 21:49:28 +000011// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000012MachineInstr::MachineInstr(MachineOpCode _opCode,
13 OpCodeMask _opCodeMask)
14 : opCode(_opCode),
15 opCodeMask(_opCodeMask),
Vikram S. Advebff682d2001-07-28 04:06:37 +000016 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000017{
Vikram S. Adveff7070b2001-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. Adveab9e5572001-07-21 12:41:50 +000029}
30
Vikram S. Adve97c348d2002-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. Adveab9e5572001-07-21 12:41:50 +000047void
Vikram S. Adve307916c02002-03-18 03:35:24 +000048MachineInstr::SetMachineOperandVal(unsigned int i,
Vikram S. Adve6c013a92002-07-08 22:38:45 +000049 MachineOperand::MachineOperandType opType,
50 Value* _val,
Chris Lattner10073a92002-07-25 06:17:51 +000051 bool isdef,
52 bool isDefAndUse)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000053{
Vikram S. Advebff682d2001-07-28 04:06:37 +000054 assert(i < operands.size());
Vikram S. Adve6c013a92002-07-08 22:38:45 +000055 operands[i].Initialize(opType, _val);
Vikram S. Advef089faa2002-07-10 21:45:04 +000056 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
57 operands[i].markDef();
58 if (isDefAndUse)
59 operands[i].markDefAndUse();
Vikram S. Adveab9e5572001-07-21 12:41:50 +000060}
61
62void
Vikram S. Adve307916c02002-03-18 03:35:24 +000063MachineInstr::SetMachineOperandConst(unsigned int i,
Vikram S. Adveab9e5572001-07-21 12:41:50 +000064 MachineOperand::MachineOperandType operandType,
Vikram S. Adve307916c02002-03-18 03:35:24 +000065 int64_t intValue)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000066{
Vikram S. Advebff682d2001-07-28 04:06:37 +000067 assert(i < operands.size());
Vikram S. Adve307916c02002-03-18 03:35:24 +000068 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
69 "immed. constant cannot be defined");
Vikram S. Adveab9e5572001-07-21 12:41:50 +000070 operands[i].InitializeConst(operandType, intValue);
71}
72
73void
Vikram S. Adve307916c02002-03-18 03:35:24 +000074MachineInstr::SetMachineOperandReg(unsigned int i,
75 int regNum,
Chris Lattner10073a92002-07-25 06:17:51 +000076 bool isdef,
77 bool isDefAndUse,
78 bool isCCReg)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000079{
Vikram S. Advebff682d2001-07-28 04:06:37 +000080 assert(i < operands.size());
Vikram S. Adve307916c02002-03-18 03:35:24 +000081 operands[i].InitializeReg(regNum, isCCReg);
Vikram S. Advef089faa2002-07-10 21:45:04 +000082 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
83 operands[i].markDef();
84 if (isDefAndUse)
85 operands[i].markDefAndUse();
Chris Lattnerce64edd2002-10-22 23:16:21 +000086 insertUsedReg(regNum);
Vikram S. Adveab9e5572001-07-21 12:41:50 +000087}
88
89void
Vikram S. Adve6c013a92002-07-08 22:38:45 +000090MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000091{
Vikram S. Adve6c013a92002-07-08 22:38:45 +000092 operands[i].setRegForValue(regNum);
Chris Lattnerce64edd2002-10-22 23:16:21 +000093 insertUsedReg(regNum);
Vikram S. Adve6c013a92002-07-08 22:38:45 +000094}
95
96
Vikram S. Advefa99db72002-08-14 16:52:58 +000097// Subsitute all occurrences of Value* oldVal with newVal in all operands
98// and all implicit refs. If defsOnly == true, substitute defs only.
99unsigned
100MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
101{
102 unsigned numSubst = 0;
103
104 // Subsitute operands
105 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
106 if (*O == oldVal)
107 if (!defsOnly || O.isDef())
108 {
109 O.getMachineOperand().value = newVal;
110 ++numSubst;
111 }
112
113 // Subsitute implicit refs
114 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
Chris Lattnerce64edd2002-10-22 23:16:21 +0000115 if (getImplicitRef(i) == oldVal)
Vikram S. Advefa99db72002-08-14 16:52:58 +0000116 if (!defsOnly || implicitRefIsDefined(i))
117 {
Chris Lattnerce64edd2002-10-22 23:16:21 +0000118 implicitRefs[i].Val = newVal;
Vikram S. Advefa99db72002-08-14 16:52:58 +0000119 ++numSubst;
120 }
121
122 return numSubst;
123}
124
125
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000126void
127MachineInstr::dump() const
128{
129 cerr << " " << *this;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000130}
131
Vikram S. Adve75113022002-09-16 15:18:53 +0000132static inline std::ostream&
133OutputValue(std::ostream &os, const Value* val)
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000134{
135 os << "(val ";
136 if (val && val->hasName())
Vikram S. Advef089faa2002-07-10 21:45:04 +0000137 return os << val->getName() << ")";
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000138 else
Vikram S. Advef089faa2002-07-10 21:45:04 +0000139 return os << (void*) val << ")"; // print address only
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000140}
141
Vikram S. Adve75113022002-09-16 15:18:53 +0000142static inline std::ostream&
143OutputReg(std::ostream &os, unsigned int regNum)
144{
145 return os << "%mreg(" << regNum << ")";
146}
147
Chris Lattner7f74a562002-01-20 22:54:45 +0000148std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000149{
Vikram S. Advebff682d2001-07-28 04:06:37 +0000150 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000151
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000152 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000153 os << "\t" << minstr.getOperand(i);
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000154 if( minstr.operandIsDefined(i) )
155 os << "*";
156 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasankaec62f242001-11-15 20:46:40 +0000157 os << "*";
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000158 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000159
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000160 // code for printing implict references
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000161 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
162 if( NumOfImpRefs > 0 ) {
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000163 os << "\tImplicit: ";
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000164 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000165 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000166 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000167 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasankaec62f242001-11-15 20:46:40 +0000168 os << "\t";
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000169 }
170 }
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000171
Chris Lattner7f74a562002-01-20 22:54:45 +0000172 return os << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000173}
174
Chris Lattner7f74a562002-01-20 22:54:45 +0000175std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000176{
Vikram S. Advef089faa2002-07-10 21:45:04 +0000177 if (mop.opHiBits32())
178 os << "%lm(";
179 else if (mop.opLoBits32())
180 os << "%lo(";
181 else if (mop.opHiBits64())
182 os << "%hh(";
183 else if (mop.opLoBits64())
184 os << "%hm(";
185
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000186 switch(mop.opType)
187 {
188 case MachineOperand::MO_VirtualRegister:
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000189 os << "%reg";
Vikram S. Advef089faa2002-07-10 21:45:04 +0000190 OutputValue(os, mop.getVRegValue());
Vikram S. Adve75113022002-09-16 15:18:53 +0000191 if (mop.hasAllocatedReg())
192 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Advef089faa2002-07-10 21:45:04 +0000193 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000194 case MachineOperand::MO_CCRegister:
195 os << "%ccreg";
Vikram S. Advef089faa2002-07-10 21:45:04 +0000196 OutputValue(os, mop.getVRegValue());
Vikram S. Adve75113022002-09-16 15:18:53 +0000197 if (mop.hasAllocatedReg())
198 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Advef089faa2002-07-10 21:45:04 +0000199 break;
200 case MachineOperand::MO_MachineRegister:
Vikram S. Adve75113022002-09-16 15:18:53 +0000201 OutputReg(os, mop.getMachineRegNum());
Vikram S. Advef089faa2002-07-10 21:45:04 +0000202 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000203 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Advef089faa2002-07-10 21:45:04 +0000204 os << (long)mop.immedVal;
205 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000206 case MachineOperand::MO_UnextendedImmed:
Vikram S. Advef089faa2002-07-10 21:45:04 +0000207 os << (long)mop.immedVal;
208 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000209 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee44abbb2001-09-30 23:44:19 +0000210 {
211 const Value* opVal = mop.getVRegValue();
Chris Lattner95f65b62002-04-08 22:01:15 +0000212 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adve6e004c02001-11-12 14:19:47 +0000213 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
214 if (opVal->hasName())
Chris Lattner7f74a562002-01-20 22:54:45 +0000215 os << opVal->getName();
Vikram S. Adve6e004c02001-11-12 14:19:47 +0000216 else
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000217 os << (const void*) opVal;
Vikram S. Advef089faa2002-07-10 21:45:04 +0000218 os << ")";
219 break;
Vikram S. Advee44abbb2001-09-30 23:44:19 +0000220 }
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000221 default:
222 assert(0 && "Unrecognized operand type");
223 break;
224 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000225
Vikram S. Advef089faa2002-07-10 21:45:04 +0000226 if (mop.flags &
227 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
228 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
229 os << ")";
230
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000231 return os;
232}