blob: 67f79d5d228c5adc23f6f552a538091f80c72f71 [file] [log] [blame]
Vikram S. Advea1d14f32001-10-10 20:50:43 +00001// $Id$ -*-c++-*-
2//***************************************************************************
3// File:
4// InstrSelectionSupport.h
5//
6// Purpose:
7// Target-independent instruction selection code.
8// See SparcInstrSelection.cpp for usage.
9//
10// History:
11// 10/10/01 - Vikram Adve - Created
12//**************************************************************************/
13
14#include "llvm/CodeGen/InstrSelectionSupport.h"
15#include "llvm/CodeGen/InstrSelection.h"
16#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
18#include "llvm/CodeGen/MachineCodeForMethod.h"
19#include "llvm/CodeGen/InstrForest.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Target/MachineRegInfo.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000022#include "llvm/ConstantVals.h"
Vikram S. Adve94e40ef2001-10-28 21:46:23 +000023#include "llvm/Method.h"
24#include "llvm/BasicBlock.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000025#include "llvm/Type.h"
26#include "llvm/iMemory.h"
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::vector;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000028
29//*************************** Local Functions ******************************/
30
Vikram S. Advea1d14f32001-10-10 20:50:43 +000031
Vikram S. Adve6d353262001-10-17 23:57:50 +000032static TmpInstruction*
Vikram S. Adve42f63202002-03-18 03:33:43 +000033InsertCodeToLoadConstant(Method* method,
34 Value* opValue,
Vikram S. Adve6d353262001-10-17 23:57:50 +000035 Instruction* vmInstr,
36 vector<MachineInstr*>& loadConstVec,
37 TargetMachine& target)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000038{
Vikram S. Adve6d353262001-10-17 23:57:50 +000039 vector<TmpInstruction*> tempVec;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000040
Vikram S. Adve6d353262001-10-17 23:57:50 +000041 // Create a tmp virtual register to hold the constant.
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000042 TmpInstruction* tmpReg = new TmpInstruction(opValue);
43 MachineCodeForInstruction &MCFI = MachineCodeForInstruction::get(vmInstr);
44 MCFI.addTemp(tmpReg);
Vikram S. Advea1d14f32001-10-10 20:50:43 +000045
Vikram S. Adve42f63202002-03-18 03:33:43 +000046 target.getInstrInfo().CreateCodeToLoadConst(method, opValue, tmpReg,
Vikram S. Adve6d353262001-10-17 23:57:50 +000047 loadConstVec, tempVec);
48
49 // Register the new tmp values created for this m/c instruction sequence
50 for (unsigned i=0; i < tempVec.size(); i++)
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000051 MCFI.addTemp(tempVec[i]);
Vikram S. Adve6d353262001-10-17 23:57:50 +000052
53 // Record the mapping from the tmp VM instruction to machine instruction.
54 // Do this for all machine instructions that were not mapped to any
55 // other temp values created by
56 // tmpReg->addMachineInstruction(loadConstVec.back());
57
58 return tmpReg;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000059}
60
61
Vikram S. Adve6d353262001-10-17 23:57:50 +000062//---------------------------------------------------------------------------
63// Function GetConstantValueAsSignedInt
64//
65// Convenience function to get the value of an integer constant, for an
66// appropriate integer or non-integer type that can be held in an integer.
67// The type of the argument must be the following:
68// Signed or unsigned integer
69// Boolean
70// Pointer
71//
72// isValidConstant is set to true if a valid constant was found.
73//---------------------------------------------------------------------------
74
75int64_t
76GetConstantValueAsSignedInt(const Value *V,
77 bool &isValidConstant)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000078{
Chris Lattnere9bb2df2001-12-03 22:26:30 +000079 if (!isa<Constant>(V))
Vikram S. Advea1d14f32001-10-10 20:50:43 +000080 {
Vikram S. Adve6d353262001-10-17 23:57:50 +000081 isValidConstant = false;
82 return 0;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000083 }
84
Vikram S. Adve6d353262001-10-17 23:57:50 +000085 isValidConstant = true;
86
87 if (V->getType() == Type::BoolTy)
Chris Lattnere9bb2df2001-12-03 22:26:30 +000088 return (int64_t) cast<ConstantBool>(V)->getValue();
Vikram S. Adve6d353262001-10-17 23:57:50 +000089
90 if (V->getType()->isIntegral())
91 {
92 if (V->getType()->isSigned())
Chris Lattnere9bb2df2001-12-03 22:26:30 +000093 return cast<ConstantSInt>(V)->getValue();
Vikram S. Adve6d353262001-10-17 23:57:50 +000094
95 assert(V->getType()->isUnsigned());
Chris Lattnere9bb2df2001-12-03 22:26:30 +000096 uint64_t Val = cast<ConstantUInt>(V)->getValue();
Vikram S. Adve6d353262001-10-17 23:57:50 +000097 if (Val < INT64_MAX) // then safe to cast to signed
98 return (int64_t)Val;
99 }
100
101 isValidConstant = false;
102 return 0;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000103}
104
105
106//---------------------------------------------------------------------------
107// Function: FoldGetElemChain
108//
109// Purpose:
Vikram S. Advec941b872002-03-24 03:37:53 +0000110// Fold a chain of GetElementPtr instructions containing only
111// structure offsets into an equivalent (Pointer, IndexVector) pair.
112// Returns the pointer Value, and stores the resulting IndexVector
113// in argument chainIdxVec.
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000114//---------------------------------------------------------------------------
115
116Value*
117FoldGetElemChain(const InstructionNode* getElemInstrNode,
Vikram S. Advefa248972001-12-15 00:36:32 +0000118 vector<Value*>& chainIdxVec)
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000119{
120 MemAccessInst* getElemInst = (MemAccessInst*)
121 getElemInstrNode->getInstruction();
122
123 // Initialize return values from the incoming instruction
Vikram S. Advec941b872002-03-24 03:37:53 +0000124 Value* ptrVal = NULL;
125 assert(chainIdxVec.size() == 0);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000126
Vikram S. Advec941b872002-03-24 03:37:53 +0000127 // Now chase the chain of getElementInstr instructions, if any.
128 // Check for any array indices and stop there.
129 //
130 const InstrTreeNode* ptrChild = getElemInstrNode;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000131 while (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
132 ptrChild->getOpLabel() == GetElemPtrIdx)
133 {
134 // Child is a GetElemPtr instruction
135 getElemInst = (MemAccessInst*)
136 ((InstructionNode*) ptrChild)->getInstruction();
Vikram S. Advefa248972001-12-15 00:36:32 +0000137 const vector<Value*>& idxVec = getElemInst->copyIndices();
Vikram S. Advec941b872002-03-24 03:37:53 +0000138 bool allStructureOffsets = true;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000139
Vikram S. Advec941b872002-03-24 03:37:53 +0000140 // If it is a struct* access, the first offset must be array index [0],
141 // and all other offsets must be structure (not array) offsets
142 if (!isa<ConstantUInt>(idxVec.front()) ||
143 cast<ConstantUInt>(idxVec.front())->getValue() != 0)
144 allStructureOffsets = false;
145
146 if (allStructureOffsets)
147 for (unsigned int i=1; i < idxVec.size(); i++)
148 if (idxVec[i]->getType() == Type::UIntTy)
149 {
150 allStructureOffsets = false;
151 break;
152 }
153
154 if (allStructureOffsets)
155 { // Get pointer value out of ptrChild and *prepend* its index vector
156 ptrVal = getElemInst->getPointerOperand();
157 chainIdxVec.insert(chainIdxVec.begin(),
158 idxVec.begin()+1, idxVec.end());
159 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
160 // mark so no code is generated
161 }
162 else // cannot fold this getElementPtr instr. or any further ones
163 break;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000164
165 ptrChild = ptrChild->leftChild();
166 }
167
168 return ptrVal;
169}
170
171
172//------------------------------------------------------------------------
173// Function Set2OperandsFromInstr
174// Function Set3OperandsFromInstr
175//
176// For the common case of 2- and 3-operand arithmetic/logical instructions,
177// set the m/c instr. operands directly from the VM instruction's operands.
178// Check whether the first or second operand is 0 and can use a dedicated "0"
179// register.
180// Check whether the second operand should use an immediate field or register.
181// (First and third operands are never immediates for such instructions.)
182//
183// Arguments:
184// canDiscardResult: Specifies that the result operand can be discarded
185// by using the dedicated "0"
186//
187// op1position, op2position and resultPosition: Specify in which position
188// in the machine instruction the 3 operands (arg1, arg2
189// and result) should go.
190//
191// RETURN VALUE: unsigned int flags, where
192// flags & 0x01 => operand 1 is constant and needs a register
193// flags & 0x02 => operand 2 is constant and needs a register
194//------------------------------------------------------------------------
195
196void
197Set2OperandsFromInstr(MachineInstr* minstr,
198 InstructionNode* vmInstrNode,
199 const TargetMachine& target,
200 bool canDiscardResult,
201 int op1Position,
202 int resultPosition)
203{
204 Set3OperandsFromInstr(minstr, vmInstrNode, target,
205 canDiscardResult, op1Position,
206 /*op2Position*/ -1, resultPosition);
207}
208
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000209
210void
211Set3OperandsFromInstr(MachineInstr* minstr,
212 InstructionNode* vmInstrNode,
213 const TargetMachine& target,
214 bool canDiscardResult,
215 int op1Position,
216 int op2Position,
217 int resultPosition)
218{
219 assert(op1Position >= 0);
220 assert(resultPosition >= 0);
221
222 // operand 1
Vikram S. Adve42f63202002-03-18 03:33:43 +0000223 minstr->SetMachineOperandVal(op1Position, MachineOperand::MO_VirtualRegister,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000224 vmInstrNode->leftChild()->getValue());
225
226 // operand 2 (if any)
227 if (op2Position >= 0)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000228 minstr->SetMachineOperandVal(op2Position, MachineOperand::MO_VirtualRegister,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000229 vmInstrNode->rightChild()->getValue());
230
231 // result operand: if it can be discarded, use a dead register if one exists
232 if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000233 minstr->SetMachineOperandReg(resultPosition,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000234 target.getRegInfo().getZeroRegNum());
235 else
Vikram S. Adve42f63202002-03-18 03:33:43 +0000236 minstr->SetMachineOperandVal(resultPosition,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000237 MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
238}
239
240
241MachineOperand::MachineOperandType
242ChooseRegOrImmed(Value* val,
243 MachineOpCode opCode,
244 const TargetMachine& target,
245 bool canUseImmed,
246 unsigned int& getMachineRegNum,
247 int64_t& getImmedValue)
248{
249 MachineOperand::MachineOperandType opType =
250 MachineOperand::MO_VirtualRegister;
251 getMachineRegNum = 0;
252 getImmedValue = 0;
253
254 // Check for the common case first: argument is not constant
255 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000256 Constant *CPV = dyn_cast<Constant>(val);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000257 if (!CPV) return opType;
258
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000259 if (ConstantBool *CPB = dyn_cast<ConstantBool>(CPV))
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000260 {
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000261 if (!CPB->getValue() && target.getRegInfo().getZeroRegNum() >= 0)
262 {
263 getMachineRegNum = target.getRegInfo().getZeroRegNum();
264 return MachineOperand::MO_MachineRegister;
265 }
266
267 getImmedValue = 1;
268 return MachineOperand::MO_SignExtendedImmed;
269 }
270
Vikram S. Advec8117452001-11-14 17:24:49 +0000271 // Otherwise it needs to be an integer or a NULL pointer
272 if (! CPV->getType()->isIntegral() &&
273 ! (CPV->getType()->isPointerType() &&
274 CPV->isNullValue()))
275 return opType;
276
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000277 // Now get the constant value and check if it fits in the IMMED field.
278 // Take advantage of the fact that the max unsigned value will rarely
279 // fit into any IMMED field and ignore that case (i.e., cast smaller
280 // unsigned constants to signed).
281 //
282 int64_t intValue;
Vikram S. Advec8117452001-11-14 17:24:49 +0000283 if (CPV->getType()->isPointerType())
284 {
285 intValue = 0;
286 }
Vikram S. Adve9e29f782001-11-14 17:55:02 +0000287 else if (CPV->getType()->isSigned())
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000288 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000289 intValue = cast<ConstantSInt>(CPV)->getValue();
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000290 }
291 else
292 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000293 uint64_t V = cast<ConstantUInt>(CPV)->getValue();
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000294 if (V >= INT64_MAX) return opType;
295 intValue = (int64_t)V;
296 }
297
298 if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
299 {
300 opType = MachineOperand::MO_MachineRegister;
301 getMachineRegNum = target.getRegInfo().getZeroRegNum();
302 }
303 else if (canUseImmed &&
304 target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
305 {
306 opType = MachineOperand::MO_SignExtendedImmed;
307 getImmedValue = intValue;
308 }
309
310 return opType;
311}
312
Vikram S. Adve6d353262001-10-17 23:57:50 +0000313
314//---------------------------------------------------------------------------
315// Function: FixConstantOperandsForInstr
316//
317// Purpose:
318// Special handling for constant operands of a machine instruction
319// -- if the constant is 0, use the hardwired 0 register, if any;
320// -- if the constant fits in the IMMEDIATE field, use that field;
321// -- else create instructions to put the constant into a register, either
322// directly or by loading explicitly from the constant pool.
323//
324// In the first 2 cases, the operand of `minstr' is modified in place.
325// Returns a vector of machine instructions generated for operands that
326// fall under case 3; these must be inserted before `minstr'.
327//---------------------------------------------------------------------------
328
329vector<MachineInstr*>
330FixConstantOperandsForInstr(Instruction* vmInstr,
331 MachineInstr* minstr,
332 TargetMachine& target)
333{
334 vector<MachineInstr*> loadConstVec;
335
336 const MachineInstrDescriptor& instrDesc =
337 target.getInstrInfo().getDescriptor(minstr->getOpCode());
338
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000339 Method* method = vmInstr->getParent()->getParent();
340
Vikram S. Adve6d353262001-10-17 23:57:50 +0000341 for (unsigned op=0; op < minstr->getNumOperands(); op++)
342 {
343 const MachineOperand& mop = minstr->getOperand(op);
344
345 // skip the result position (for efficiency below) and any other
346 // positions already marked as not a virtual register
347 if (instrDesc.resultPos == (int) op ||
348 mop.getOperandType() != MachineOperand::MO_VirtualRegister ||
349 mop.getVRegValue() == NULL)
350 {
351 continue;
352 }
353
354 Value* opValue = mop.getVRegValue();
355 bool constantThatMustBeLoaded = false;
356
Vikram S. Adve42f63202002-03-18 03:33:43 +0000357 if (Constant *opConst = dyn_cast<Constant>(opValue))
358 {
Vikram S. Adve6d353262001-10-17 23:57:50 +0000359 unsigned int machineRegNum;
360 int64_t immedValue;
361 MachineOperand::MachineOperandType opType =
362 ChooseRegOrImmed(opValue, minstr->getOpCode(), target,
Vikram S. Adve42f63202002-03-18 03:33:43 +0000363 (target.getInstrInfo().getImmedConstantPos(minstr->getOpCode()) == (int) op),
Vikram S. Adve6d353262001-10-17 23:57:50 +0000364 machineRegNum, immedValue);
Vikram S. Adveecd58132001-11-14 18:49:45 +0000365
Vikram S. Adve6d353262001-10-17 23:57:50 +0000366 if (opType == MachineOperand::MO_MachineRegister)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000367 minstr->SetMachineOperandReg(op, machineRegNum);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000368 else if (opType == MachineOperand::MO_VirtualRegister)
369 constantThatMustBeLoaded = true; // load is generated below
370 else
Vikram S. Adve42f63202002-03-18 03:33:43 +0000371 minstr->SetMachineOperandConst(op, opType, immedValue);
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000372 }
373
Vikram S. Adve6d353262001-10-17 23:57:50 +0000374 if (constantThatMustBeLoaded || isa<GlobalValue>(opValue))
375 { // opValue is a constant that must be explicitly loaded into a reg.
Vikram S. Adve42f63202002-03-18 03:33:43 +0000376 TmpInstruction* tmpReg = InsertCodeToLoadConstant(method, opValue, vmInstr,
377 loadConstVec, target);
378 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
379 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000380 }
381 }
382
383 //
384 // Also, check for implicit operands used (not those defined) by the
385 // machine instruction. These include:
386 // -- arguments to a Call
387 // -- return value of a Return
388 // Any such operand that is a constant value needs to be fixed also.
389 // The current instructions with implicit refs (viz., Call and Return)
390 // have no immediate fields, so the constant always needs to be loaded
391 // into a register.
392 //
393 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000394 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000395 isa<GlobalValue>(minstr->getImplicitRef(i)))
396 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000397 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000398 TmpInstruction* tmpReg =
Vikram S. Adve42f63202002-03-18 03:33:43 +0000399 InsertCodeToLoadConstant(method, oldVal, vmInstr, loadConstVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000400 minstr->setImplicitRef(i, tmpReg);
401 }
402
403 return loadConstVec;
404}
405
406