blob: e5b768eca993e2f5b96b755eca118188a5ed734f [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
Vikram S. Adve5b795912001-08-28 23:02:39 +000015
Chris Lattner822b4fb2001-09-07 17:18:30 +000016#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve5b795912001-08-28 23:02:39 +000017#include "llvm/Method.h"
Chris Lattner68498ce2001-07-21 23:24:48 +000018#include "llvm/ConstPoolVals.h"
19#include "llvm/Instruction.h"
Vikram S. Adve5b795912001-08-28 23:02:39 +000020
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000021
22//************************ Class Implementations **************************/
23
Vikram S. Adve1885da42001-07-31 21:49:28 +000024// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000025MachineInstr::MachineInstr(MachineOpCode _opCode,
26 OpCodeMask _opCodeMask)
27 : opCode(_opCode),
28 opCodeMask(_opCodeMask),
Vikram S. Adve6a175e02001-07-28 04:06:37 +000029 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000030{
Vikram S. Adve1885da42001-07-31 21:49:28 +000031 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
32}
33
34// Constructor for instructions with variable #operands
35MachineInstr::MachineInstr(MachineOpCode _opCode,
36 unsigned numOperands,
37 OpCodeMask _opCodeMask)
38 : opCode(_opCode),
39 opCodeMask(_opCodeMask),
40 operands(numOperands)
41{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000042}
43
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000044void
45MachineInstr::SetMachineOperand(unsigned int i,
46 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000047 Value* _val, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000048{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000049 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000050 operands[i].Initialize(operandType, _val);
Vikram S. Adve149977b2001-08-13 16:32:45 +000051 operands[i].isDef = isdef ||
52 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000053}
54
55void
56MachineInstr::SetMachineOperand(unsigned int i,
57 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000058 int64_t intValue, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000059{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000060 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000061 operands[i].InitializeConst(operandType, intValue);
Vikram S. Adve149977b2001-08-13 16:32:45 +000062 operands[i].isDef = isdef ||
63 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000064}
65
66void
67MachineInstr::SetMachineOperand(unsigned int i,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000068 unsigned int regNum, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000069{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000070 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000071 operands[i].InitializeReg(regNum);
Vikram S. Adve149977b2001-08-13 16:32:45 +000072 operands[i].isDef = isdef ||
73 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000074}
75
76void
Ruchira Sasanka0b03c6a2001-08-07 21:01:23 +000077MachineInstr::dump(unsigned int indent) const
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000078{
79 for (unsigned i=0; i < indent; i++)
80 cout << " ";
81
82 cout << *this;
83}
84
85ostream&
86operator<< (ostream& os, const MachineInstr& minstr)
87{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000088 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000089
90 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++)
91 os << "\t" << minstr.getOperand(i);
92
Vikram S. Adve6a175e02001-07-28 04:06:37 +000093#undef DEBUG_VAL_OP_ITERATOR
94#ifdef DEBUG_VAL_OP_ITERATOR
95 os << endl << "\tValue operands are: ";
96 for (MachineInstr::val_op_const_iterator vo(&minstr); ! vo.done(); ++vo)
97 {
98 const Value* val = *vo;
99 os << val << (vo.isDef()? "(def), " : ", ");
100 }
101 os << endl;
102#endif
103
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000104 return os;
105}
106
Chris Lattnere6fdb112001-09-09 22:26:29 +0000107static inline ostream &OutputOperand(ostream &os, const MachineOperand &mop) {
108 switch (mop.getOperandType()) {
109 case MachineOperand::MO_CCRegister:
110 case MachineOperand::MO_VirtualRegister:
111 return os << "(val " << mop.getVRegValue() << ")";
112 case MachineOperand::MO_MachineRegister:
113 return os << "(" << mop.getMachineRegNum() << ")";
114 default:
115 assert(0 && "Unknown operand type");
116 return os;
117 }
118}
119
120
121ostream &operator<<(ostream &os, const MachineOperand &mop) {
122 switch(mop.opType) {
123 case MachineOperand::MO_VirtualRegister:
124 case MachineOperand::MO_MachineRegister:
125 os << "%reg";
126 return OutputOperand(os, mop);
127 case MachineOperand::MO_CCRegister:
128 os << "%ccreg";
129 return OutputOperand(os, mop);
Chris Lattnere6fdb112001-09-09 22:26:29 +0000130 case MachineOperand::MO_SignExtendedImmed:
131 return os << mop.immedVal;
Chris Lattnere6fdb112001-09-09 22:26:29 +0000132 case MachineOperand::MO_UnextendedImmed:
133 return os << mop.immedVal;
Chris Lattnere6fdb112001-09-09 22:26:29 +0000134 case MachineOperand::MO_PCRelativeDisp:
Chris Lattnerb221a762001-09-10 19:43:38 +0000135 return os << "%disp(label " << mop.getVRegValue() << ")";
Chris Lattnere6fdb112001-09-09 22:26:29 +0000136 default:
137 assert(0 && "Unrecognized operand type");
138 break;
139 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000140
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000141 return os;
142}
143
144
145//---------------------------------------------------------------------------
146// Target-independent utility routines for creating machine instructions
147//---------------------------------------------------------------------------
148
149
150//------------------------------------------------------------------------
151// Function Set2OperandsFromInstr
152// Function Set3OperandsFromInstr
153//
154// For the common case of 2- and 3-operand arithmetic/logical instructions,
155// set the m/c instr. operands directly from the VM instruction's operands.
Chris Lattnerc2a23962001-09-11 23:22:43 +0000156// Check whether the first or second operand is 0 and can use a dedicated "0"
157// register.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000158// Check whether the second operand should use an immediate field or register.
159// (First and third operands are never immediates for such instructions.)
160//
161// Arguments:
162// canDiscardResult: Specifies that the result operand can be discarded
163// by using the dedicated "0"
164//
165// op1position, op2position and resultPosition: Specify in which position
166// in the machine instruction the 3 operands (arg1, arg2
167// and result) should go.
168//
169// RETURN VALUE: unsigned int flags, where
170// flags & 0x01 => operand 1 is constant and needs a register
171// flags & 0x02 => operand 2 is constant and needs a register
172//------------------------------------------------------------------------
173
174void
175Set2OperandsFromInstr(MachineInstr* minstr,
176 InstructionNode* vmInstrNode,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000177 const TargetMachine& target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000178 bool canDiscardResult,
179 int op1Position,
180 int resultPosition)
181{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000182 Set3OperandsFromInstr(minstr, vmInstrNode, target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000183 canDiscardResult, op1Position,
184 /*op2Position*/ -1, resultPosition);
185}
186
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000187#undef REVERT_TO_EXPLICIT_CONSTANT_CHECKS
188#ifdef REVERT_TO_EXPLICIT_CONSTANT_CHECKS
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000189unsigned
190Set3OperandsFromInstrJUNK(MachineInstr* minstr,
191 InstructionNode* vmInstrNode,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000192 const TargetMachine& target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000193 bool canDiscardResult,
194 int op1Position,
195 int op2Position,
196 int resultPosition)
197{
198 assert(op1Position >= 0);
199 assert(resultPosition >= 0);
200
201 unsigned returnFlags = 0x0;
202
Vikram S. Adve5b795912001-08-28 23:02:39 +0000203 // Check if operand 1 is 0. If so, try to use a hardwired 0 register.
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000204 Value* op1Value = vmInstrNode->leftChild()->getValue();
205 bool isValidConstant;
206 int64_t intValue = GetConstantValueAsSignedInt(op1Value, isValidConstant);
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000207 if (isValidConstant && intValue == 0 && target.zeroRegNum >= 0)
208 minstr->SetMachineOperand(op1Position, /*regNum*/ target.zeroRegNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000209 else
210 {
Chris Lattnerb221a762001-09-10 19:43:38 +0000211 if (op1Value->isConstant()) {
212 // value is constant and must be loaded from constant pool
213 returnFlags = returnFlags | (1 << op1Position);
214 }
215 minstr->SetMachineOperand(op1Position, MachineOperand::MO_VirtualRegister,
216 op1Value);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000217 }
218
Vikram S. Adve5b795912001-08-28 23:02:39 +0000219 // Check if operand 2 (if any) fits in the immed. field of the instruction,
220 // or if it is 0 and can use a dedicated machine register
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000221 if (op2Position >= 0)
222 {
223 Value* op2Value = vmInstrNode->rightChild()->getValue();
224 int64_t immedValue;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000225 unsigned int machineRegNum;
226
227 MachineOperand::MachineOperandType
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000228 op2type = ChooseRegOrImmed(op2Value, minstr->getOpCode(), target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000229 /*canUseImmed*/ true,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000230 machineRegNum, immedValue);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000231
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000232 if (op2type == MachineOperand::MO_MachineRegister)
233 minstr->SetMachineOperand(op2Position, machineRegNum);
234 else if (op2type == MachineOperand::MO_VirtualRegister)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000235 {
Chris Lattnerb221a762001-09-10 19:43:38 +0000236 if (op2Value->isConstant()) {
237 // value is constant and must be loaded from constant pool
238 returnFlags = returnFlags | (1 << op2Position);
239 }
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000240 minstr->SetMachineOperand(op2Position, op2type, op2Value);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000241 }
242 else
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000243 {
244 assert(op2type != MO_CCRegister);
245 minstr->SetMachineOperand(op2Position, op2type, immedValue);
246 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000247 }
248
249 // If operand 3 (result) can be discarded, use a dead register if one exists
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000250 if (canDiscardResult && target.zeroRegNum >= 0)
Vikram S. Adve149977b2001-08-13 16:32:45 +0000251 minstr->SetMachineOperand(resultPosition, target.zeroRegNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000252 else
Vikram S. Adve149977b2001-08-13 16:32:45 +0000253 minstr->SetMachineOperand(resultPosition, MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000254
255 return returnFlags;
256}
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000257#endif
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000258
259
260void
261Set3OperandsFromInstr(MachineInstr* minstr,
262 InstructionNode* vmInstrNode,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000263 const TargetMachine& target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000264 bool canDiscardResult,
265 int op1Position,
266 int op2Position,
267 int resultPosition)
268{
269 assert(op1Position >= 0);
270 assert(resultPosition >= 0);
271
272 // operand 1
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000273 minstr->SetMachineOperand(op1Position, MachineOperand::MO_VirtualRegister,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000274 vmInstrNode->leftChild()->getValue());
275
276 // operand 2 (if any)
277 if (op2Position >= 0)
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000278 minstr->SetMachineOperand(op2Position, MachineOperand::MO_VirtualRegister,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000279 vmInstrNode->rightChild()->getValue());
280
281 // result operand: if it can be discarded, use a dead register if one exists
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000282 if (canDiscardResult && target.zeroRegNum >= 0)
Vikram S. Adve149977b2001-08-13 16:32:45 +0000283 minstr->SetMachineOperand(resultPosition, target.zeroRegNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000284 else
Vikram S. Adve149977b2001-08-13 16:32:45 +0000285 minstr->SetMachineOperand(resultPosition, MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000286}
287
288
289MachineOperand::MachineOperandType
290ChooseRegOrImmed(Value* val,
291 MachineOpCode opCode,
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000292 const TargetMachine& target,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000293 bool canUseImmed,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000294 unsigned int& getMachineRegNum,
295 int64_t& getImmedValue)
296{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000297 MachineOperand::MachineOperandType opType =
298 MachineOperand::MO_VirtualRegister;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000299 getMachineRegNum = 0;
300 getImmedValue = 0;
301
302 // Check for the common case first: argument is not constant
303 //
Chris Lattner990f2a52001-09-09 23:01:32 +0000304 ConstPoolVal *CPV = val->castConstant();
305 if (!CPV) return opType;
306
307 if (CPV->getType() == Type::BoolTy) {
308 ConstPoolBool *CPB = (ConstPoolBool*)CPV;
309 if (!CPB->getValue() && target.zeroRegNum >= 0) {
310 getMachineRegNum = target.zeroRegNum;
311 return MachineOperand::MO_MachineRegister;
312 }
313
314 getImmedValue = 1;
315 return MachineOperand::MO_SignExtendedImmed;
316 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000317
Chris Lattner990f2a52001-09-09 23:01:32 +0000318 if (!CPV->getType()->isIntegral()) return opType;
319
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000320 // Now get the constant value and check if it fits in the IMMED field.
321 // Take advantage of the fact that the max unsigned value will rarely
322 // fit into any IMMED field and ignore that case (i.e., cast smaller
323 // unsigned constants to signed).
324 //
Chris Lattner990f2a52001-09-09 23:01:32 +0000325 int64_t intValue;
326 if (CPV->getType()->isSigned()) {
327 intValue = ((ConstPoolSInt*)CPV)->getValue();
328 } else {
329 uint64_t V = ((ConstPoolUInt*)CPV)->getValue();
330 if (V >= INT64_MAX) return opType;
331 intValue = (int64_t)V;
332 }
333
334 if (intValue == 0 && target.zeroRegNum >= 0){
335 opType = MachineOperand::MO_MachineRegister;
336 getMachineRegNum = target.zeroRegNum;
337 } else if (canUseImmed &&
338 target.getInstrInfo().constantFitsInImmedField(opCode, intValue)) {
339 opType = MachineOperand::MO_SignExtendedImmed;
340 getImmedValue = intValue;
341 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000342
343 return opType;
344}
Vikram S. Adve5b795912001-08-28 23:02:39 +0000345
346
347void
Chris Lattner56786d22001-09-09 19:52:23 +0000348PrintMachineInstructions(const Method* method)
Vikram S. Adve5b795912001-08-28 23:02:39 +0000349{
350 cout << "\n" << method->getReturnType()
351 << " \"" << method->getName() << "\"" << endl;
352
353 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
354 {
Chris Lattner56786d22001-09-09 19:52:23 +0000355 const BasicBlock* bb = *BI;
Vikram S. Adve5b795912001-08-28 23:02:39 +0000356 cout << "\n"
357 << (bb->hasName()? bb->getName() : "Label")
358 << " (" << bb << ")" << ":"
359 << endl;
360
Chris Lattner56786d22001-09-09 19:52:23 +0000361 const MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
Vikram S. Adve5b795912001-08-28 23:02:39 +0000362 for (unsigned i=0; i < mvec.size(); i++)
363 cout << "\t" << *mvec[i] << endl;
364 }
365 cout << endl << "End method \"" << method->getName() << "\""
366 << endl << endl;
367}