blob: df633c65116fd928871a50e102cdedec74fa1bf4 [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 Lattner697954c2002-01-20 22:54:45 +00007using std::cerr;
Vikram S. Adve5b795912001-08-28 23:02:39 +00008
Ruchira Sasanka69917e22001-10-18 22:40:02 +00009
Vikram S. Adve1885da42001-07-31 21:49:28 +000010// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000011MachineInstr::MachineInstr(MachineOpCode _opCode,
12 OpCodeMask _opCodeMask)
13 : opCode(_opCode),
14 opCodeMask(_opCodeMask),
Vikram S. Adve6a175e02001-07-28 04:06:37 +000015 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000016{
Vikram S. Adve1885da42001-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. Adve70bc4b52001-07-21 12:41:50 +000028}
29
Vikram S. Advee8b57ef2002-09-20 00:47:49 +000030//
31// Support for replacing opcode and operands of a MachineInstr in place.
32// This only resets the size of the operand vector and initializes it.
33// The new operands must be set explicitly later.
34//
35void
36MachineInstr::replace(MachineOpCode _opCode,
37 unsigned numOperands,
38 OpCodeMask _opCodeMask)
39{
40 opCode = _opCode;
41 opCodeMask = _opCodeMask;
42 operands.clear();
43 operands.resize(numOperands);
44}
45
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000046void
Vikram S. Advec356e562002-03-18 03:35:24 +000047MachineInstr::SetMachineOperandVal(unsigned int i,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000048 MachineOperand::MachineOperandType opType,
49 Value* _val,
Chris Lattner0c0edf82002-07-25 06:17:51 +000050 bool isdef,
51 bool isDefAndUse)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000052{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000053 assert(i < operands.size());
Vikram S. Adve7a4be952002-07-08 22:38:45 +000054 operands[i].Initialize(opType, _val);
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +000055 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
56 operands[i].markDef();
57 if (isDefAndUse)
58 operands[i].markDefAndUse();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000059}
60
61void
Vikram S. Advec356e562002-03-18 03:35:24 +000062MachineInstr::SetMachineOperandConst(unsigned int i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000063 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +000064 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000065{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000066 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000067 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
68 "immed. constant cannot be defined");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000069 operands[i].InitializeConst(operandType, intValue);
70}
71
72void
Vikram S. Advec356e562002-03-18 03:35:24 +000073MachineInstr::SetMachineOperandReg(unsigned int i,
74 int regNum,
Chris Lattner0c0edf82002-07-25 06:17:51 +000075 bool isdef,
76 bool isDefAndUse,
77 bool isCCReg)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000078{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000079 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000080 operands[i].InitializeReg(regNum, isCCReg);
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();
Chris Lattner27a08932002-10-22 23:16:21 +000085 insertUsedReg(regNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000086}
87
88void
Vikram S. Adve7a4be952002-07-08 22:38:45 +000089MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000090{
Vikram S. Adve7a4be952002-07-08 22:38:45 +000091 operands[i].setRegForValue(regNum);
Chris Lattner27a08932002-10-22 23:16:21 +000092 insertUsedReg(regNum);
Vikram S. Adve7a4be952002-07-08 22:38:45 +000093}
94
95
Vikram S. Advee2a78e32002-08-14 16:52:58 +000096// Subsitute all occurrences of Value* oldVal with newVal in all operands
97// and all implicit refs. If defsOnly == true, substitute defs only.
98unsigned
99MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
100{
101 unsigned numSubst = 0;
102
103 // Subsitute operands
104 for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
105 if (*O == oldVal)
106 if (!defsOnly || O.isDef())
107 {
108 O.getMachineOperand().value = newVal;
109 ++numSubst;
110 }
111
112 // Subsitute implicit refs
113 for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
Chris Lattner27a08932002-10-22 23:16:21 +0000114 if (getImplicitRef(i) == oldVal)
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000115 if (!defsOnly || implicitRefIsDefined(i))
116 {
Chris Lattner27a08932002-10-22 23:16:21 +0000117 implicitRefs[i].Val = newVal;
Vikram S. Advee2a78e32002-08-14 16:52:58 +0000118 ++numSubst;
119 }
120
121 return numSubst;
122}
123
124
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000125void
126MachineInstr::dump() const
127{
128 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000129}
130
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000131static inline std::ostream&
132OutputValue(std::ostream &os, const Value* val)
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000133{
134 os << "(val ";
135 if (val && val->hasName())
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000136 return os << val->getName() << ")";
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000137 else
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000138 return os << (void*) val << ")"; // print address only
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000139}
140
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000141static inline std::ostream&
142OutputReg(std::ostream &os, unsigned int regNum)
143{
144 return os << "%mreg(" << regNum << ")";
145}
146
Chris Lattner697954c2002-01-20 22:54:45 +0000147std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000148{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000149 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000150
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000151 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000152 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000153 if( minstr.operandIsDefined(i) )
154 os << "*";
155 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000156 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000157 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000158
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000159 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000160 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
161 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000162 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000163 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000164 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000165 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000166 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000167 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000168 }
169 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000170
Chris Lattner697954c2002-01-20 22:54:45 +0000171 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000172}
173
Chris Lattner697954c2002-01-20 22:54:45 +0000174std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000175{
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000176 if (mop.opHiBits32())
177 os << "%lm(";
178 else if (mop.opLoBits32())
179 os << "%lo(";
180 else if (mop.opHiBits64())
181 os << "%hh(";
182 else if (mop.opLoBits64())
183 os << "%hm(";
184
Vikram S. Adve6e447182001-09-18 12:56:28 +0000185 switch(mop.opType)
186 {
187 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve6e447182001-09-18 12:56:28 +0000188 os << "%reg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000189 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000190 if (mop.hasAllocatedReg())
191 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000192 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000193 case MachineOperand::MO_CCRegister:
194 os << "%ccreg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000195 OutputValue(os, mop.getVRegValue());
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000196 if (mop.hasAllocatedReg())
197 os << "==" << OutputReg(os, mop.getAllocatedRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000198 break;
199 case MachineOperand::MO_MachineRegister:
Vikram S. Adve8c6936a2002-09-16 15:18:53 +0000200 OutputReg(os, mop.getMachineRegNum());
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000201 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000202 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000203 os << (long)mop.immedVal;
204 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000205 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000206 os << (long)mop.immedVal;
207 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000208 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000209 {
210 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000211 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000212 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
213 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000214 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000215 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000216 os << (const void*) opVal;
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000217 os << ")";
218 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000219 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000220 default:
221 assert(0 && "Unrecognized operand type");
222 break;
223 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000224
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000225 if (mop.flags &
226 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
227 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
228 os << ")";
229
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000230 return os;
231}