blob: e12922e4c87547468f054d97e3207c7612c09b52 [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 Lattner7f74a562002-01-20 22:54:45 +00007using std::cerr;
Vikram S. Adve5f72f422001-08-28 23:02:39 +00008
Ruchira Sasanka59e864e2001-10-18 22:40:02 +00009
Vikram S. Adveff7070b2001-07-31 21:49:28 +000010// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000011MachineInstr::MachineInstr(MachineOpCode _opCode,
12 OpCodeMask _opCodeMask)
13 : opCode(_opCode),
14 opCodeMask(_opCodeMask),
Vikram S. Advebff682d2001-07-28 04:06:37 +000015 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000016{
Vikram S. Adveff7070b2001-07-31 21:49:28 +000017 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
18}
19
20// Constructor for instructions with variable #operands
21MachineInstr::MachineInstr(MachineOpCode _opCode,
22 unsigned numOperands,
23 OpCodeMask _opCodeMask)
24 : opCode(_opCode),
25 opCodeMask(_opCodeMask),
26 operands(numOperands)
27{
Vikram S. Adveab9e5572001-07-21 12:41:50 +000028}
29
Vikram S. Adveab9e5572001-07-21 12:41:50 +000030void
Vikram S. Adve307916c02002-03-18 03:35:24 +000031MachineInstr::SetMachineOperandVal(unsigned int i,
Vikram S. Adve6c013a92002-07-08 22:38:45 +000032 MachineOperand::MachineOperandType opType,
33 Value* _val,
Chris Lattner10073a92002-07-25 06:17:51 +000034 bool isdef,
35 bool isDefAndUse)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000036{
Vikram S. Advebff682d2001-07-28 04:06:37 +000037 assert(i < operands.size());
Vikram S. Adve6c013a92002-07-08 22:38:45 +000038 operands[i].Initialize(opType, _val);
Vikram S. Advef089faa2002-07-10 21:45:04 +000039 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
40 operands[i].markDef();
41 if (isDefAndUse)
42 operands[i].markDefAndUse();
Vikram S. Adveab9e5572001-07-21 12:41:50 +000043}
44
45void
Vikram S. Adve307916c02002-03-18 03:35:24 +000046MachineInstr::SetMachineOperandConst(unsigned int i,
Vikram S. Adveab9e5572001-07-21 12:41:50 +000047 MachineOperand::MachineOperandType operandType,
Vikram S. Adve307916c02002-03-18 03:35:24 +000048 int64_t intValue)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000049{
Vikram S. Advebff682d2001-07-28 04:06:37 +000050 assert(i < operands.size());
Vikram S. Adve307916c02002-03-18 03:35:24 +000051 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
52 "immed. constant cannot be defined");
Vikram S. Adveab9e5572001-07-21 12:41:50 +000053 operands[i].InitializeConst(operandType, intValue);
54}
55
56void
Vikram S. Adve307916c02002-03-18 03:35:24 +000057MachineInstr::SetMachineOperandReg(unsigned int i,
58 int regNum,
Chris Lattner10073a92002-07-25 06:17:51 +000059 bool isdef,
60 bool isDefAndUse,
61 bool isCCReg)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000062{
Vikram S. Advebff682d2001-07-28 04:06:37 +000063 assert(i < operands.size());
Vikram S. Adve307916c02002-03-18 03:35:24 +000064 operands[i].InitializeReg(regNum, isCCReg);
Vikram S. Advef089faa2002-07-10 21:45:04 +000065 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
66 operands[i].markDef();
67 if (isDefAndUse)
68 operands[i].markDefAndUse();
Vikram S. Adve6c013a92002-07-08 22:38:45 +000069 regsUsed.insert(regNum);
Vikram S. Adveab9e5572001-07-21 12:41:50 +000070}
71
72void
Vikram S. Adve6c013a92002-07-08 22:38:45 +000073MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adveab9e5572001-07-21 12:41:50 +000074{
Vikram S. Adve6c013a92002-07-08 22:38:45 +000075 operands[i].setRegForValue(regNum);
76 regsUsed.insert(regNum);
77}
78
79
Vikram S. Advefa99db72002-08-14 16:52:58 +000080// Subsitute all occurrences of Value* oldVal with newVal in all operands
81// and all implicit refs. If defsOnly == true, substitute defs only.
82unsigned
83MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
84{
85 unsigned numSubst = 0;
86
87 // Subsitute operands
88 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
89 if (*O == oldVal)
90 if (!defsOnly || O.isDef())
91 {
92 O.getMachineOperand().value = newVal;
93 ++numSubst;
94 }
95
96 // Subsitute implicit refs
97 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
98 if (implicitRefs[i] == oldVal)
99 if (!defsOnly || implicitRefIsDefined(i))
100 {
101 implicitRefs[i] = newVal;
102 ++numSubst;
103 }
104
105 return numSubst;
106}
107
108
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000109void
110MachineInstr::dump() const
111{
112 cerr << " " << *this;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000113}
114
Vikram S. Adve75113022002-09-16 15:18:53 +0000115static inline std::ostream&
116OutputValue(std::ostream &os, const Value* val)
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000117{
118 os << "(val ";
119 if (val && val->hasName())
Vikram S. Advef089faa2002-07-10 21:45:04 +0000120 return os << val->getName() << ")";
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000121 else
Vikram S. Advef089faa2002-07-10 21:45:04 +0000122 return os << (void*) val << ")"; // print address only
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000123}
124
Vikram S. Adve75113022002-09-16 15:18:53 +0000125static inline std::ostream&
126OutputReg(std::ostream &os, unsigned int regNum)
127{
128 return os << "%mreg(" << regNum << ")";
129}
130
Chris Lattner7f74a562002-01-20 22:54:45 +0000131std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000132{
Vikram S. Advebff682d2001-07-28 04:06:37 +0000133 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000134
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000135 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000136 os << "\t" << minstr.getOperand(i);
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000137 if( minstr.operandIsDefined(i) )
138 os << "*";
139 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasankaec62f242001-11-15 20:46:40 +0000140 os << "*";
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000141 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000142
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000143 // code for printing implict references
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000144 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
145 if( NumOfImpRefs > 0 ) {
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000146 os << "\tImplicit: ";
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000147 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000148 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka64f75672001-11-14 20:05:23 +0000149 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000150 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasankaec62f242001-11-15 20:46:40 +0000151 os << "\t";
Ruchira Sasanka59e864e2001-10-18 22:40:02 +0000152 }
153 }
Vikram S. Adved79d2c32002-04-25 04:31:18 +0000154
Chris Lattner7f74a562002-01-20 22:54:45 +0000155 return os << "\n";
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000156}
157
Chris Lattner7f74a562002-01-20 22:54:45 +0000158std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000159{
Vikram S. Advef089faa2002-07-10 21:45:04 +0000160 if (mop.opHiBits32())
161 os << "%lm(";
162 else if (mop.opLoBits32())
163 os << "%lo(";
164 else if (mop.opHiBits64())
165 os << "%hh(";
166 else if (mop.opLoBits64())
167 os << "%hm(";
168
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000169 switch(mop.opType)
170 {
171 case MachineOperand::MO_VirtualRegister:
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000172 os << "%reg";
Vikram S. Advef089faa2002-07-10 21:45:04 +0000173 OutputValue(os, mop.getVRegValue());
Vikram S. Adve75113022002-09-16 15:18:53 +0000174 if (mop.hasAllocatedReg())
175 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Advef089faa2002-07-10 21:45:04 +0000176 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000177 case MachineOperand::MO_CCRegister:
178 os << "%ccreg";
Vikram S. Advef089faa2002-07-10 21:45:04 +0000179 OutputValue(os, mop.getVRegValue());
Vikram S. Adve75113022002-09-16 15:18:53 +0000180 if (mop.hasAllocatedReg())
181 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Advef089faa2002-07-10 21:45:04 +0000182 break;
183 case MachineOperand::MO_MachineRegister:
Vikram S. Adve75113022002-09-16 15:18:53 +0000184 OutputReg(os, mop.getMachineRegNum());
Vikram S. Advef089faa2002-07-10 21:45:04 +0000185 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000186 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Advef089faa2002-07-10 21:45:04 +0000187 os << (long)mop.immedVal;
188 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000189 case MachineOperand::MO_UnextendedImmed:
Vikram S. Advef089faa2002-07-10 21:45:04 +0000190 os << (long)mop.immedVal;
191 break;
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000192 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee44abbb2001-09-30 23:44:19 +0000193 {
194 const Value* opVal = mop.getVRegValue();
Chris Lattner95f65b62002-04-08 22:01:15 +0000195 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adve6e004c02001-11-12 14:19:47 +0000196 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
197 if (opVal->hasName())
Chris Lattner7f74a562002-01-20 22:54:45 +0000198 os << opVal->getName();
Vikram S. Adve6e004c02001-11-12 14:19:47 +0000199 else
Vikram S. Adve6c013a92002-07-08 22:38:45 +0000200 os << (const void*) opVal;
Vikram S. Advef089faa2002-07-10 21:45:04 +0000201 os << ")";
202 break;
Vikram S. Advee44abbb2001-09-30 23:44:19 +0000203 }
Vikram S. Advebb81dae2001-09-18 12:56:28 +0000204 default:
205 assert(0 && "Unrecognized operand type");
206 break;
207 }
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000208
Vikram S. Advef089faa2002-07-10 21:45:04 +0000209 if (mop.flags &
210 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
211 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
212 os << ")";
213
Vikram S. Adveab9e5572001-07-21 12:41:50 +0000214 return os;
215}