blob: c3c5ca8feadd787fba05ded25f5f998a7aee25f3 [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,
Chris Lattner340bb962002-10-28 04:24:49 +000050 Value* V,
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());
Chris Lattner340bb962002-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. Advef089faa2002-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. Adveab9e5572001-07-21 12:41:50 +000064}
65
66void
Chris Lattner340bb962002-10-28 04:24:49 +000067MachineInstr::SetMachineOperandConst(unsigned i,
Vikram S. Adveab9e5572001-07-21 12:41:50 +000068 MachineOperand::MachineOperandType operandType,
Vikram S. Adve307916c02002-03-18 03:35:24 +000069 int64_t intValue)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000070{
Vikram S. Advebff682d2001-07-28 04:06:37 +000071 assert(i < operands.size());
Vikram S. Adve307916c02002-03-18 03:35:24 +000072 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
73 "immed. constant cannot be defined");
Chris Lattner340bb962002-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. Adveab9e5572001-07-21 12:41:50 +000080}
81
82void
Chris Lattner340bb962002-10-28 04:24:49 +000083MachineInstr::SetMachineOperandReg(unsigned i,
Vikram S. Adve307916c02002-03-18 03:35:24 +000084 int regNum,
Chris Lattner864d2792002-10-28 19:46:59 +000085 bool isdef) {
Vikram S. Advebff682d2001-07-28 04:06:37 +000086 assert(i < operands.size());
Chris Lattner340bb962002-10-28 04:24:49 +000087
Chris Lattner864d2792002-10-28 19:46:59 +000088 operands[i].opType = MachineOperand::MO_MachineRegister;
Chris Lattner340bb962002-10-28 04:24:49 +000089 operands[i].value = NULL;
90 operands[i].regNum = regNum;
91 operands[i].flags = 0;
92
Vikram S. Advef089faa2002-07-10 21:45:04 +000093 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
94 operands[i].markDef();
Chris Lattnerce64edd2002-10-22 23:16:21 +000095 insertUsedReg(regNum);
Vikram S. Adveab9e5572001-07-21 12:41:50 +000096}
97
98void
Vikram S. Adve6c013a92002-07-08 22:38:45 +000099MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000100{
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000101 operands[i].setRegForValue(regNum);
Chris Lattnerce64edd2002-10-22 23:16:21 +0000102 insertUsedReg(regNum);
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000103}
104
105
Vikram S. Advefa99db72002-08-14 16:52:58 +0000106// Subsitute all occurrences of Value* oldVal with newVal in all operands
107// and all implicit refs. If defsOnly == true, substitute defs only.
108unsigned
109MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
110{
111 unsigned numSubst = 0;
112
113 // Subsitute operands
114 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
115 if (*O == oldVal)
116 if (!defsOnly || O.isDef())
117 {
118 O.getMachineOperand().value = newVal;
119 ++numSubst;
120 }
121
122 // Subsitute implicit refs
123 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
Chris Lattnerce64edd2002-10-22 23:16:21 +0000124 if (getImplicitRef(i) == oldVal)
Vikram S. Advefa99db72002-08-14 16:52:58 +0000125 if (!defsOnly || implicitRefIsDefined(i))
126 {
Chris Lattnerce64edd2002-10-22 23:16:21 +0000127 implicitRefs[i].Val = newVal;
Vikram S. Advefa99db72002-08-14 16:52:58 +0000128 ++numSubst;
129 }
130
131 return numSubst;
132}
133
134
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000135void
136MachineInstr::dump() const
137{
138 cerr << " " << *this;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000139}
140
Vikram S. Adve75113022002-09-16 15:18:53 +0000141static inline std::ostream&
142OutputValue(std::ostream &os, const Value* val)
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000143{
144 os << "(val ";
145 if (val && val->hasName())
Vikram S. Advef089faa2002-07-10 21:45:04 +0000146 return os << val->getName() << ")";
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000147 else
Vikram S. Advef089faa2002-07-10 21:45:04 +0000148 return os << (void*) val << ")"; // print address only
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000149}
150
Vikram S. Adve75113022002-09-16 15:18:53 +0000151static inline std::ostream&
152OutputReg(std::ostream &os, unsigned int regNum)
153{
154 return os << "%mreg(" << regNum << ")";
155}
156
Chris Lattner7f74a562002-01-20 22:54:45 +0000157std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000158{
Vikram S. Advebff682d2001-07-28 04:06:37 +0000159 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000160
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000161 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000162 os << "\t" << minstr.getOperand(i);
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000163 if( minstr.operandIsDefined(i) )
164 os << "*";
165 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasankaec62f242001-11-15 20:46:40 +0000166 os << "*";
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000167 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000168
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000169 // code for printing implict references
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000170 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
171 if( NumOfImpRefs > 0 ) {
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000172 os << "\tImplicit: ";
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000173 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000174 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000175 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000176 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasankaec62f242001-11-15 20:46:40 +0000177 os << "\t";
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000178 }
179 }
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000180
Chris Lattner7f74a562002-01-20 22:54:45 +0000181 return os << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000182}
183
Chris Lattner7f74a562002-01-20 22:54:45 +0000184std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000185{
Vikram S. Advef089faa2002-07-10 21:45:04 +0000186 if (mop.opHiBits32())
187 os << "%lm(";
188 else if (mop.opLoBits32())
189 os << "%lo(";
190 else if (mop.opHiBits64())
191 os << "%hh(";
192 else if (mop.opLoBits64())
193 os << "%hm(";
194
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000195 switch(mop.opType)
196 {
197 case MachineOperand::MO_VirtualRegister:
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000198 os << "%reg";
Vikram S. Advef089faa2002-07-10 21:45:04 +0000199 OutputValue(os, mop.getVRegValue());
Vikram S. Adve75113022002-09-16 15:18:53 +0000200 if (mop.hasAllocatedReg())
201 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Advef089faa2002-07-10 21:45:04 +0000202 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000203 case MachineOperand::MO_CCRegister:
204 os << "%ccreg";
Vikram S. Advef089faa2002-07-10 21:45:04 +0000205 OutputValue(os, mop.getVRegValue());
Vikram S. Adve75113022002-09-16 15:18:53 +0000206 if (mop.hasAllocatedReg())
207 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Advef089faa2002-07-10 21:45:04 +0000208 break;
209 case MachineOperand::MO_MachineRegister:
Vikram S. Adve75113022002-09-16 15:18:53 +0000210 OutputReg(os, mop.getMachineRegNum());
Vikram S. Advef089faa2002-07-10 21:45:04 +0000211 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000212 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Advef089faa2002-07-10 21:45:04 +0000213 os << (long)mop.immedVal;
214 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000215 case MachineOperand::MO_UnextendedImmed:
Vikram S. Advef089faa2002-07-10 21:45:04 +0000216 os << (long)mop.immedVal;
217 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000218 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee44abbb2001-09-30 23:44:19 +0000219 {
220 const Value* opVal = mop.getVRegValue();
Chris Lattner95f65b62002-04-08 22:01:15 +0000221 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adve6e004c02001-11-12 14:19:47 +0000222 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
223 if (opVal->hasName())
Chris Lattner7f74a562002-01-20 22:54:45 +0000224 os << opVal->getName();
Vikram S. Adve6e004c02001-11-12 14:19:47 +0000225 else
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000226 os << (const void*) opVal;
Vikram S. Advef089faa2002-07-10 21:45:04 +0000227 os << ")";
228 break;
Vikram S. Advee44abbb2001-09-30 23:44:19 +0000229 }
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000230 default:
231 assert(0 && "Unrecognized operand type");
232 break;
233 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000234
Vikram S. Advef089faa2002-07-10 21:45:04 +0000235 if (mop.flags &
236 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
237 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
238 os << ")";
239
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000240 return os;
241}