blob: 2e3f73fbaa166c5dfd8596f0082685a19e995953 [file] [log] [blame]
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00001// $Id$
2//***************************************************************************
3// File:
4// MachineInstr.cpp
5//
6// Purpose:
7//
8//
9// Strategy:
10//
11// History:
12// 7/2/01 - Vikram Adve - Created
13//**************************************************************************/
14
Chris Lattner7e583cf2001-07-21 20:58:30 +000015#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner68498ce2001-07-21 23:24:48 +000016#include "llvm/ConstPoolVals.h"
17#include "llvm/Instruction.h"
18#include <strstream>
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000019
20//************************ Class Implementations **************************/
21
Vikram S. Adve1885da42001-07-31 21:49:28 +000022// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000023MachineInstr::MachineInstr(MachineOpCode _opCode,
24 OpCodeMask _opCodeMask)
25 : opCode(_opCode),
26 opCodeMask(_opCodeMask),
Vikram S. Adve6a175e02001-07-28 04:06:37 +000027 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000028{
Vikram S. Adve1885da42001-07-31 21:49:28 +000029 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
30}
31
32// Constructor for instructions with variable #operands
33MachineInstr::MachineInstr(MachineOpCode _opCode,
34 unsigned numOperands,
35 OpCodeMask _opCodeMask)
36 : opCode(_opCode),
37 opCodeMask(_opCodeMask),
38 operands(numOperands)
39{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000040}
41
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000042void
43MachineInstr::SetMachineOperand(unsigned int i,
44 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000045 Value* _val, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000046{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000047 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000048 operands[i].Initialize(operandType, _val);
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000049 operands[i].isDef = isdef;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000050}
51
52void
53MachineInstr::SetMachineOperand(unsigned int i,
54 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000055 int64_t intValue, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000056{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000057 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000058 operands[i].InitializeConst(operandType, intValue);
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000059 operands[i].isDef = isdef;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000060}
61
62void
63MachineInstr::SetMachineOperand(unsigned int i,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000064 unsigned int regNum, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000065{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000066 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000067 operands[i].InitializeReg(regNum);
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000068 operands[i].isDef = isdef;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000069}
70
71void
Ruchira Sasanka0b03c6a2001-08-07 21:01:23 +000072MachineInstr::dump(unsigned int indent) const
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000073{
74 for (unsigned i=0; i < indent; i++)
75 cout << " ";
76
77 cout << *this;
78}
79
80ostream&
81operator<< (ostream& os, const MachineInstr& minstr)
82{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000083 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000084
85 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++)
86 os << "\t" << minstr.getOperand(i);
87
Vikram S. Adve6a175e02001-07-28 04:06:37 +000088#undef DEBUG_VAL_OP_ITERATOR
89#ifdef DEBUG_VAL_OP_ITERATOR
90 os << endl << "\tValue operands are: ";
91 for (MachineInstr::val_op_const_iterator vo(&minstr); ! vo.done(); ++vo)
92 {
93 const Value* val = *vo;
94 os << val << (vo.isDef()? "(def), " : ", ");
95 }
96 os << endl;
97#endif
98
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000099 return os;
100}
101
102ostream&
103operator<< (ostream& os, const MachineOperand& mop)
104{
105 strstream regInfo;
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000106 if (mop.opType == MachineOperand::MO_VirtualRegister)
107 regInfo << "(val " << mop.value << ")" << ends;
108 else if (mop.opType == MachineOperand::MO_MachineRegister)
109 regInfo << "(" << mop.regNum << ")" << ends;
110 else if (mop.opType == MachineOperand::MO_CCRegister)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000111 regInfo << "(val " << mop.value << ")" << ends;
112
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000113 switch(mop.opType)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000114 {
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000115 case MachineOperand::MO_VirtualRegister:
116 case MachineOperand::MO_MachineRegister:
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000117 os << "%reg" << regInfo.str();
118 free(regInfo.str());
119 break;
120
121 case MachineOperand::MO_CCRegister:
122 os << "%ccreg" << regInfo.str();
123 free(regInfo.str());
124 break;
125
126 case MachineOperand::MO_SignExtendedImmed:
127 os << mop.immedVal;
128 break;
129
130 case MachineOperand::MO_UnextendedImmed:
131 os << mop.immedVal;
132 break;
133
134 case MachineOperand::MO_PCRelativeDisp:
135 os << "%disp(label " << mop.value << ")";
136 break;
137
138 default:
139 assert(0 && "Unrecognized operand type");
140 break;
141 }
142
143 return os;
144}
145
146
147//---------------------------------------------------------------------------
148// Target-independent utility routines for creating machine instructions
149//---------------------------------------------------------------------------
150
151
152//------------------------------------------------------------------------
153// Function Set2OperandsFromInstr
154// Function Set3OperandsFromInstr
155//
156// For the common case of 2- and 3-operand arithmetic/logical instructions,
157// set the m/c instr. operands directly from the VM instruction's operands.
158// Check whether the first or second operand is 0 and can use a dedicated "0" register.
159// Check whether the second operand should use an immediate field or register.
160// (First and third operands are never immediates for such instructions.)
161//
162// Arguments:
163// canDiscardResult: Specifies that the result operand can be discarded
164// by using the dedicated "0"
165//
166// op1position, op2position and resultPosition: Specify in which position
167// in the machine instruction the 3 operands (arg1, arg2
168// and result) should go.
169//
170// RETURN VALUE: unsigned int flags, where
171// flags & 0x01 => operand 1 is constant and needs a register
172// flags & 0x02 => operand 2 is constant and needs a register
173//------------------------------------------------------------------------
174
175void
176Set2OperandsFromInstr(MachineInstr* minstr,
177 InstructionNode* vmInstrNode,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000178 const TargetMachine& target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000179 bool canDiscardResult,
180 int op1Position,
181 int resultPosition)
182{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000183 Set3OperandsFromInstr(minstr, vmInstrNode, target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000184 canDiscardResult, op1Position,
185 /*op2Position*/ -1, resultPosition);
186}
187
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000188#undef REVERT_TO_EXPLICIT_CONSTANT_CHECKS
189#ifdef REVERT_TO_EXPLICIT_CONSTANT_CHECKS
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000190unsigned
191Set3OperandsFromInstrJUNK(MachineInstr* minstr,
192 InstructionNode* vmInstrNode,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000193 const TargetMachine& target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000194 bool canDiscardResult,
195 int op1Position,
196 int op2Position,
197 int resultPosition)
198{
199 assert(op1Position >= 0);
200 assert(resultPosition >= 0);
201
202 unsigned returnFlags = 0x0;
203
204 // Check if operand 1 is 0 and if so, try to use the register that gives 0, if any.
205 Value* op1Value = vmInstrNode->leftChild()->getValue();
206 bool isValidConstant;
207 int64_t intValue = GetConstantValueAsSignedInt(op1Value, isValidConstant);
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000208 if (isValidConstant && intValue == 0 && target.zeroRegNum >= 0)
209 minstr->SetMachineOperand(op1Position, /*regNum*/ target.zeroRegNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000210 else
211 {
212 if (op1Value->getValueType() == Value::ConstantVal)
213 {// value is constant and must be loaded from constant pool
214 returnFlags = returnFlags | (1 << op1Position);
215 }
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000216 minstr->SetMachineOperand(op1Position,MachineOperand::MO_VirtualRegister,
217 op1Value);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000218 }
219
220 // Check if operand 2 (if any) fits in the immediate field of the instruction,
221 // of if it is 0 and can use a dedicated machine register
222 if (op2Position >= 0)
223 {
224 Value* op2Value = vmInstrNode->rightChild()->getValue();
225 int64_t immedValue;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000226 unsigned int machineRegNum;
227
228 MachineOperand::MachineOperandType
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000229 op2type = ChooseRegOrImmed(op2Value, minstr->getOpCode(), target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000230 /*canUseImmed*/ true,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000231 machineRegNum, immedValue);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000232
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000233 if (op2type == MachineOperand::MO_MachineRegister)
234 minstr->SetMachineOperand(op2Position, machineRegNum);
235 else if (op2type == MachineOperand::MO_VirtualRegister)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000236 {
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000237 if (op2Value->getValueType() == Value::ConstantVal)
238 {// value is constant and must be loaded from constant pool
239 returnFlags = returnFlags | (1 << op2Position);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000240 }
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000241 minstr->SetMachineOperand(op2Position, op2type, op2Value);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000242 }
243 else
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000244 {
245 assert(op2type != MO_CCRegister);
246 minstr->SetMachineOperand(op2Position, op2type, immedValue);
247 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000248 }
249
250 // If operand 3 (result) can be discarded, use a dead register if one exists
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000251 if (canDiscardResult && target.zeroRegNum >= 0)
Ruchira Sasankab0cdcda2001-08-13 16:26:28 +0000252 minstr->SetMachineOperand(resultPosition, target.zeroRegNum, true);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000253 else
Ruchira Sasankab0cdcda2001-08-13 16:26:28 +0000254 minstr->SetMachineOperand(resultPosition, MachineOperand::MO_VirtualRegister, vmInstrNode->getValue(), true);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000255
256 return returnFlags;
257}
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000258#endif
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000259
260
261void
262Set3OperandsFromInstr(MachineInstr* minstr,
263 InstructionNode* vmInstrNode,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000264 const TargetMachine& target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000265 bool canDiscardResult,
266 int op1Position,
267 int op2Position,
268 int resultPosition)
269{
270 assert(op1Position >= 0);
271 assert(resultPosition >= 0);
272
273 // operand 1
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000274 minstr->SetMachineOperand(op1Position, MachineOperand::MO_VirtualRegister,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000275 vmInstrNode->leftChild()->getValue());
276
277 // operand 2 (if any)
278 if (op2Position >= 0)
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000279 minstr->SetMachineOperand(op2Position, MachineOperand::MO_VirtualRegister,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000280 vmInstrNode->rightChild()->getValue());
281
282 // result operand: if it can be discarded, use a dead register if one exists
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000283 if (canDiscardResult && target.zeroRegNum >= 0)
Ruchira Sasankab0cdcda2001-08-13 16:26:28 +0000284 minstr->SetMachineOperand(resultPosition, target.zeroRegNum, true);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000285 else
Ruchira Sasankab0cdcda2001-08-13 16:26:28 +0000286 minstr->SetMachineOperand(resultPosition, MachineOperand::MO_VirtualRegister, vmInstrNode->getValue(), true);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000287}
288
289
290MachineOperand::MachineOperandType
291ChooseRegOrImmed(Value* val,
292 MachineOpCode opCode,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000293 const TargetMachine& target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000294 bool canUseImmed,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000295 unsigned int& getMachineRegNum,
296 int64_t& getImmedValue)
297{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000298 MachineOperand::MachineOperandType opType =
299 MachineOperand::MO_VirtualRegister;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000300 getMachineRegNum = 0;
301 getImmedValue = 0;
302
303 // Check for the common case first: argument is not constant
304 //
305 if (val->getValueType() != Value::ConstantVal)
306 return opType;
307
308 // Now get the constant value and check if it fits in the IMMED field.
309 // Take advantage of the fact that the max unsigned value will rarely
310 // fit into any IMMED field and ignore that case (i.e., cast smaller
311 // unsigned constants to signed).
312 //
313 bool isValidConstant;
314 int64_t intValue = GetConstantValueAsSignedInt(val, isValidConstant);
315
316 if (isValidConstant)
317 {
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000318 if (intValue == 0 && target.zeroRegNum >= 0)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000319 {
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000320 opType = MachineOperand::MO_MachineRegister;
321 getMachineRegNum = target.zeroRegNum;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000322 }
323 else if (canUseImmed &&
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000324 target.getInstrInfo().constantFitsInImmedField(opCode,intValue))
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000325 {
326 opType = MachineOperand::MO_SignExtendedImmed;
327 getImmedValue = intValue;
328 }
329 }
330
331 return opType;
332}