blob: aff6c30e22652e84e66a1b2acbdfc8d703d62ec7 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- InstrSelectionSupport.cpp -----------------------------------------===//
2//
3// Target-independent instruction selection code. See SparcInstrSelection.cpp
4// for usage.
Vikram S. Advea1d14f32001-10-10 20:50:43 +00005//
Chris Lattner035dfbe2002-08-09 20:08:06 +00006//===----------------------------------------------------------------------===//
Vikram S. Advea1d14f32001-10-10 20:50:43 +00007
8#include "llvm/CodeGen/InstrSelectionSupport.h"
9#include "llvm/CodeGen/InstrSelection.h"
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000010#include "llvm/CodeGen/MachineInstr.h"
11#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000012#include "llvm/CodeGen/MachineCodeForInstruction.h"
13#include "llvm/CodeGen/MachineCodeForMethod.h"
14#include "llvm/CodeGen/InstrForest.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000015#include "llvm/Target/TargetMachine.h"
16#include "llvm/Target/MachineRegInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000018#include "llvm/Function.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000019#include "llvm/Type.h"
20#include "llvm/iMemory.h"
Chris Lattner697954c2002-01-20 22:54:45 +000021using std::vector;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000022
23//*************************** Local Functions ******************************/
24
Vikram S. Advea1d14f32001-10-10 20:50:43 +000025
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000026// Generate code to load the constant into a TmpInstruction (virtual reg) and
27// returns the virtual register.
28//
Vikram S. Adve6d353262001-10-17 23:57:50 +000029static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000030InsertCodeToLoadConstant(Function *F,
Vikram S. Adve42f63202002-03-18 03:33:43 +000031 Value* opValue,
Vikram S. Adve6d353262001-10-17 23:57:50 +000032 Instruction* vmInstr,
33 vector<MachineInstr*>& loadConstVec,
34 TargetMachine& target)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000035{
Vikram S. Adve6d353262001-10-17 23:57:50 +000036 // Create a tmp virtual register to hold the constant.
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000037 TmpInstruction* tmpReg = new TmpInstruction(opValue);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000038 MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
39 mcfi.addTemp(tmpReg);
Vikram S. Advea1d14f32001-10-10 20:50:43 +000040
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000041 target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
42 loadConstVec, mcfi);
Vikram S. Adve6d353262001-10-17 23:57:50 +000043
44 // Record the mapping from the tmp VM instruction to machine instruction.
45 // Do this for all machine instructions that were not mapped to any
46 // other temp values created by
47 // tmpReg->addMachineInstruction(loadConstVec.back());
48
49 return tmpReg;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000050}
51
52
Vikram S. Adve6d353262001-10-17 23:57:50 +000053//---------------------------------------------------------------------------
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000054// Function GetConstantValueAsUnsignedInt
Vikram S. Adve6d353262001-10-17 23:57:50 +000055// Function GetConstantValueAsSignedInt
56//
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000057// Convenience functions to get the value of an integral constant, for an
58// appropriate integer or non-integer type that can be held in a signed
59// or unsigned integer respectively. The type of the argument must be
60// the following:
Vikram S. Adve6d353262001-10-17 23:57:50 +000061// Signed or unsigned integer
62// Boolean
63// Pointer
64//
65// isValidConstant is set to true if a valid constant was found.
66//---------------------------------------------------------------------------
67
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000068uint64_t
69GetConstantValueAsUnsignedInt(const Value *V,
70 bool &isValidConstant)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000071{
Vikram S. Adve6d353262001-10-17 23:57:50 +000072 isValidConstant = true;
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000073
74 if (isa<Constant>(V))
Chris Lattner0c4e8862002-09-03 01:08:28 +000075 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
76 return (int64_t)CB->getValue();
77 else if (const ConstantSInt *CS = dyn_cast<ConstantSInt>(V))
78 return (uint64_t)CS->getValue();
79 else if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(V))
80 return CU->getValue();
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000081
Vikram S. Adve6d353262001-10-17 23:57:50 +000082 isValidConstant = false;
83 return 0;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000084}
85
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000086int64_t
87GetConstantValueAsSignedInt(const Value *V,
88 bool &isValidConstant)
89{
90 uint64_t C = GetConstantValueAsUnsignedInt(V, isValidConstant);
91 if (isValidConstant) {
92 if (V->getType()->isSigned() || C < INT64_MAX) // safe to cast to signed
93 return (int64_t) C;
94 else
95 isValidConstant = false;
96 }
97 return 0;
98}
Vikram S. Advea1d14f32001-10-10 20:50:43 +000099
Vikram S. Adve68513332002-08-24 21:00:08 +0000100
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000101//---------------------------------------------------------------------------
102// Function: FoldGetElemChain
103//
104// Purpose:
Vikram S. Advec941b872002-03-24 03:37:53 +0000105// Fold a chain of GetElementPtr instructions containing only
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000106// constant offsets into an equivalent (Pointer, IndexVector) pair.
Vikram S. Advec941b872002-03-24 03:37:53 +0000107// Returns the pointer Value, and stores the resulting IndexVector
Vikram S. Adve68513332002-08-24 21:00:08 +0000108// in argument chainIdxVec. This is a helper function for
109// FoldConstantIndices that does the actual folding.
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000110//---------------------------------------------------------------------------
111
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000112
113// Check for a constant 0.
114inline bool
115IsZero(Value* idx)
116{
117 return (idx == ConstantSInt::getNullValue(idx->getType()));
118}
119
Vikram S. Adve68513332002-08-24 21:00:08 +0000120static Value*
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000121FoldGetElemChain(InstrTreeNode* ptrNode, vector<Value*>& chainIdxVec,
122 bool lastInstHasLeadingNonZero)
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000123{
Vikram S. Adve68513332002-08-24 21:00:08 +0000124 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
Vikram S. Adve68513332002-08-24 21:00:08 +0000125 GetElementPtrInst* gepInst =
Chris Lattner597f81f2002-09-12 20:27:10 +0000126 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
Chris Lattner106ff452002-09-11 01:21:29 +0000127
128 // ptr value is not computed in this tree or ptr value does not come from GEP
129 // instruction
130 if (gepInst == NULL)
Vikram S. Adve68513332002-08-24 21:00:08 +0000131 return NULL;
132
Vikram S. Adve17927792002-03-31 18:56:51 +0000133 // Return NULL if we don't fold any instructions in.
Vikram S. Advec941b872002-03-24 03:37:53 +0000134 Value* ptrVal = NULL;
Vikram S. Adve68513332002-08-24 21:00:08 +0000135
Vikram S. Advec941b872002-03-24 03:37:53 +0000136 // Now chase the chain of getElementInstr instructions, if any.
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000137 // Check for any non-constant indices and stop there.
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000138 // Also, stop if the first index of child is a non-zero array index
139 // and the last index of the current node is a non-array index:
140 // in that case, a non-array declared type is being accessed as an array
141 // which is not type-safe, but could be legal.
Vikram S. Advec941b872002-03-24 03:37:53 +0000142 //
Vikram S. Adve68513332002-08-24 21:00:08 +0000143 InstructionNode* ptrChild = gepNode;
144 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
145 ptrChild->getOpLabel() == GetElemPtrIdx))
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000146 {
147 // Child is a GetElemPtr instruction
Vikram S. Adve68513332002-08-24 21:00:08 +0000148 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
149 User::op_iterator OI, firstIdx = gepInst->idx_begin();
150 User::op_iterator lastIdx = gepInst->idx_end();
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000151 bool allConstantOffsets = true;
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000152
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000153 // The first index of every GEP must be an array index.
154 assert((*firstIdx)->getType() == Type::LongTy &&
155 "INTERNAL ERROR: Structure index for a pointer type!");
156
157 // If the last instruction had a leading non-zero index,
158 // check if the current one ends with an array index. If not,
159 // the code is not type-safe and we would create an illegal GEP
160 // by folding them, so don't fold any more instructions.
161 //
162 if (lastInstHasLeadingNonZero)
163 if (firstIdx != lastIdx && (*(lastIdx-1))->getType() != Type::LongTy)
164 break; // cannot fold in any preceding getElementPtr instrs.
165
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000166 // Check that all offsets are constant for this instruction
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000167 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
168 allConstantOffsets = isa<ConstantInt>(*OI);
169
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000170 if (allConstantOffsets)
Vikram S. Adve17927792002-03-31 18:56:51 +0000171 { // Get pointer value out of ptrChild.
Vikram S. Adve68513332002-08-24 21:00:08 +0000172 ptrVal = gepInst->getPointerOperand();
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000173
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000174 // Remember if it has leading zero index: it will be discarded later.
175 lastInstHasLeadingNonZero = ! IsZero(*firstIdx);
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000176
177 // Insert its index vector at the start, skipping any leading [0]
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000178 chainIdxVec.insert(chainIdxVec.begin(),
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000179 firstIdx + !lastInstHasLeadingNonZero, lastIdx);
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000180
Vikram S. Adve17927792002-03-31 18:56:51 +0000181 // Mark the folded node so no code is generated for it.
Vikram S. Advec941b872002-03-24 03:37:53 +0000182 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000183
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000184 // Get the previous GEP instruction and continue trying to fold
185 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
186 }
187 else // cannot fold this getElementPtr instr. or any preceding ones
188 break;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000189 }
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000190
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000191 // If the first getElementPtr instruction had a leading [0], add it back.
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000192 // Note that this instruction is the *last* one successfully folded above.
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000193 if (ptrVal && ! lastInstHasLeadingNonZero)
Chris Lattner106ff452002-09-11 01:21:29 +0000194 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000195
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000196 return ptrVal;
197}
198
199
Vikram S. Adve68513332002-08-24 21:00:08 +0000200//---------------------------------------------------------------------------
201// Function: GetMemInstArgs
202//
203// Purpose:
204// Get the pointer value and the index vector for a memory operation
205// (GetElementPtr, Load, or Store). If all indices of the given memory
206// operation are constant, fold in constant indices in a chain of
207// preceding GetElementPtr instructions (if any), and return the
208// pointer value of the first instruction in the chain.
209// All folded instructions are marked so no code is generated for them.
210//
211// Return values:
212// Returns the pointer Value to use.
213// Returns the resulting IndexVector in idxVec.
214// Returns true/false in allConstantIndices if all indices are/aren't const.
215//---------------------------------------------------------------------------
216
Vikram S. Adve68513332002-08-24 21:00:08 +0000217Value*
218GetMemInstArgs(const InstructionNode* memInstrNode,
219 vector<Value*>& idxVec,
220 bool& allConstantIndices)
221{
222 allConstantIndices = true;
223 Instruction* memInst = memInstrNode->getInstruction();
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000224 assert(idxVec.size() == 0 && "Need empty vector to return indices");
Vikram S. Adve68513332002-08-24 21:00:08 +0000225
226 // If there is a GetElemPtr instruction to fold in to this instr,
227 // it must be in the left child for Load and GetElemPtr, and in the
228 // right child for Store instructions.
229 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
230 ? memInstrNode->rightChild()
231 : memInstrNode->leftChild());
232
233 // Default pointer is the one from the current instruction.
234 Value* ptrVal = ptrChild->getValue();
235
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000236 // GEP is the only indexed memory instruction. Extract its index vector.
237 // Also, if all indices are constant and first index is zero, try to fold
238 // in preceding GEPs with all constant indices.
Vikram S. Adve68513332002-08-24 21:00:08 +0000239 GetElementPtrInst* gepI = dyn_cast<GetElementPtrInst>(memInst);
Vikram S. Adve68513332002-08-24 21:00:08 +0000240 if (gepI)
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000241 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
Vikram S. Adve68513332002-08-24 21:00:08 +0000242 allConstantIndices && OI != OE; ++OI)
243 if (! isa<Constant>(*OI))
244 allConstantIndices = false; // note: this also terminates loop!
245
246 // If we have only constant indices, fold chains of constant indices
247 // in this and any preceding GetElemPtr instructions.
248 bool foldedGEPs = false;
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000249 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
Vikram S. Adve68513332002-08-24 21:00:08 +0000250 if (allConstantIndices)
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000251 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx))
Vikram S. Adve68513332002-08-24 21:00:08 +0000252 {
253 ptrVal = newPtr;
254 foldedGEPs = true;
Vikram S. Adve68513332002-08-24 21:00:08 +0000255 }
256
257 // Append the index vector of the current instruction, if any.
258 // Skip the leading [0] index if preceding GEPs were folded into this.
259 if (gepI)
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000260 idxVec.insert(idxVec.end(),
261 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
262 gepI->idx_end());
Vikram S. Adve68513332002-08-24 21:00:08 +0000263
264 return ptrVal;
265}
266
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000267//------------------------------------------------------------------------
268// Function Set2OperandsFromInstr
269// Function Set3OperandsFromInstr
270//
271// For the common case of 2- and 3-operand arithmetic/logical instructions,
272// set the m/c instr. operands directly from the VM instruction's operands.
273// Check whether the first or second operand is 0 and can use a dedicated "0"
274// register.
275// Check whether the second operand should use an immediate field or register.
276// (First and third operands are never immediates for such instructions.)
277//
278// Arguments:
279// canDiscardResult: Specifies that the result operand can be discarded
280// by using the dedicated "0"
281//
282// op1position, op2position and resultPosition: Specify in which position
283// in the machine instruction the 3 operands (arg1, arg2
284// and result) should go.
285//
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000286//------------------------------------------------------------------------
287
288void
289Set2OperandsFromInstr(MachineInstr* minstr,
290 InstructionNode* vmInstrNode,
291 const TargetMachine& target,
292 bool canDiscardResult,
293 int op1Position,
294 int resultPosition)
295{
296 Set3OperandsFromInstr(minstr, vmInstrNode, target,
297 canDiscardResult, op1Position,
298 /*op2Position*/ -1, resultPosition);
299}
300
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000301
302void
303Set3OperandsFromInstr(MachineInstr* minstr,
304 InstructionNode* vmInstrNode,
305 const TargetMachine& target,
306 bool canDiscardResult,
307 int op1Position,
308 int op2Position,
309 int resultPosition)
310{
311 assert(op1Position >= 0);
312 assert(resultPosition >= 0);
313
314 // operand 1
Vikram S. Adve42f63202002-03-18 03:33:43 +0000315 minstr->SetMachineOperandVal(op1Position, MachineOperand::MO_VirtualRegister,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000316 vmInstrNode->leftChild()->getValue());
317
318 // operand 2 (if any)
319 if (op2Position >= 0)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000320 minstr->SetMachineOperandVal(op2Position, MachineOperand::MO_VirtualRegister,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000321 vmInstrNode->rightChild()->getValue());
322
323 // result operand: if it can be discarded, use a dead register if one exists
324 if (canDiscardResult && target.getRegInfo().getZeroRegNum() >= 0)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000325 minstr->SetMachineOperandReg(resultPosition,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000326 target.getRegInfo().getZeroRegNum());
327 else
Vikram S. Adve42f63202002-03-18 03:33:43 +0000328 minstr->SetMachineOperandVal(resultPosition,
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000329 MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
330}
331
332
333MachineOperand::MachineOperandType
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000334ChooseRegOrImmed(int64_t intValue,
335 bool isSigned,
336 MachineOpCode opCode,
337 const TargetMachine& target,
338 bool canUseImmed,
339 unsigned int& getMachineRegNum,
340 int64_t& getImmedValue)
341{
342 MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister;
343 getMachineRegNum = 0;
344 getImmedValue = 0;
345
346 if (canUseImmed &&
347 target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
348 {
349 opType = isSigned? MachineOperand::MO_SignExtendedImmed
350 : MachineOperand::MO_UnextendedImmed;
351 getImmedValue = intValue;
352 }
353 else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
354 {
355 opType = MachineOperand::MO_MachineRegister;
356 getMachineRegNum = target.getRegInfo().getZeroRegNum();
357 }
358
359 return opType;
360}
361
362
363MachineOperand::MachineOperandType
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000364ChooseRegOrImmed(Value* val,
365 MachineOpCode opCode,
366 const TargetMachine& target,
367 bool canUseImmed,
368 unsigned int& getMachineRegNum,
369 int64_t& getImmedValue)
370{
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000371 getMachineRegNum = 0;
372 getImmedValue = 0;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000373
374 // To use reg or immed, constant needs to be integer, bool, or a NULL pointer
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000375 Constant *CPV = dyn_cast<Constant>(val);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000376 if (CPV == NULL ||
377 (! CPV->getType()->isIntegral() &&
378 ! (isa<PointerType>(CPV->getType()) && CPV->isNullValue())))
379 return MachineOperand::MO_VirtualRegister;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000380
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000381 // Now get the constant value and check if it fits in the IMMED field.
382 // Take advantage of the fact that the max unsigned value will rarely
383 // fit into any IMMED field and ignore that case (i.e., cast smaller
384 // unsigned constants to signed).
385 //
386 int64_t intValue;
Chris Lattner9b625032002-05-06 16:15:30 +0000387 if (isa<PointerType>(CPV->getType()))
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000388 intValue = 0; // We checked above that it is NULL
389 else if (ConstantBool* CB = dyn_cast<ConstantBool>(CPV))
390 intValue = (int64_t) CB->getValue();
Vikram S. Adve9e29f782001-11-14 17:55:02 +0000391 else if (CPV->getType()->isSigned())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000392 intValue = cast<ConstantSInt>(CPV)->getValue();
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000393 else
Vikram S. Adve1c10f172002-09-27 14:26:20 +0000394 { // get the int value and sign-extend if original was less than 64 bits
395 intValue = (int64_t) cast<ConstantUInt>(CPV)->getValue();
396 switch(CPV->getType()->getPrimitiveID())
397 {
398 case Type::UByteTyID: intValue = (int64_t) (int8_t) intValue; break;
399 case Type::UShortTyID: intValue = (int64_t) (short) intValue; break;
400 case Type::UIntTyID: intValue = (int64_t) (int) intValue; break;
401 default: break;
402 }
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000403 }
404
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000405 return ChooseRegOrImmed(intValue, CPV->getType()->isSigned(),
406 opCode, target, canUseImmed,
407 getMachineRegNum, getImmedValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000408}
409
Vikram S. Adve6d353262001-10-17 23:57:50 +0000410
411//---------------------------------------------------------------------------
412// Function: FixConstantOperandsForInstr
413//
414// Purpose:
415// Special handling for constant operands of a machine instruction
416// -- if the constant is 0, use the hardwired 0 register, if any;
417// -- if the constant fits in the IMMEDIATE field, use that field;
418// -- else create instructions to put the constant into a register, either
419// directly or by loading explicitly from the constant pool.
420//
421// In the first 2 cases, the operand of `minstr' is modified in place.
422// Returns a vector of machine instructions generated for operands that
423// fall under case 3; these must be inserted before `minstr'.
424//---------------------------------------------------------------------------
425
426vector<MachineInstr*>
427FixConstantOperandsForInstr(Instruction* vmInstr,
428 MachineInstr* minstr,
429 TargetMachine& target)
430{
431 vector<MachineInstr*> loadConstVec;
432
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000433 MachineOpCode opCode = minstr->getOpCode();
434 const MachineInstrInfo& instrInfo = target.getInstrInfo();
435 const MachineInstrDescriptor& instrDesc = instrInfo.getDescriptor(opCode);
436 int immedPos = instrInfo.getImmedConstantPos(opCode);
437
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000438 Function *F = vmInstr->getParent()->getParent();
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000439
Vikram S. Adve6d353262001-10-17 23:57:50 +0000440 for (unsigned op=0; op < minstr->getNumOperands(); op++)
441 {
442 const MachineOperand& mop = minstr->getOperand(op);
443
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000444 // Skip the result position, preallocated machine registers, or operands
445 // that cannot be constants (CC regs or PC-relative displacements)
446 if (instrDesc.resultPos == (int) op ||
447 mop.getOperandType() == MachineOperand::MO_MachineRegister ||
448 mop.getOperandType() == MachineOperand::MO_CCRegister ||
449 mop.getOperandType() == MachineOperand::MO_PCRelativeDisp)
450 continue;
451
Vikram S. Adve6d353262001-10-17 23:57:50 +0000452 bool constantThatMustBeLoaded = false;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000453 unsigned int machineRegNum = 0;
454 int64_t immedValue = 0;
455 Value* opValue = NULL;
456 MachineOperand::MachineOperandType opType =
457 MachineOperand::MO_VirtualRegister;
458
459 // Operand may be a virtual register or a compile-time constant
460 if (mop.getOperandType() == MachineOperand::MO_VirtualRegister)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000461 {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000462 assert(mop.getVRegValue() != NULL);
463 opValue = mop.getVRegValue();
464 if (Constant *opConst = dyn_cast<Constant>(opValue))
465 {
466 opType = ChooseRegOrImmed(opConst, opCode, target,
467 (immedPos == (int)op), machineRegNum, immedValue);
468 if (opType == MachineOperand::MO_VirtualRegister)
469 constantThatMustBeLoaded = true;
470 }
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000471 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000472 else
473 {
474 assert(mop.getOperandType() == MachineOperand::MO_SignExtendedImmed ||
475 mop.getOperandType() == MachineOperand::MO_UnextendedImmed);
476
477 bool isSigned = (mop.getOperandType() ==
478 MachineOperand::MO_SignExtendedImmed);
479
480 // Bit-selection flags indicate an instruction that is extracting
481 // bits from its operand so ignore this even if it is a big constant.
482 if (mop.opHiBits32() || mop.opLoBits32() ||
483 mop.opHiBits64() || mop.opLoBits64())
484 continue;
485
486 opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
487 opCode, target, (immedPos == (int)op),
488 machineRegNum, immedValue);
489
490 if (opType == mop.getOperandType())
491 continue; // no change: this is the most common case
492
493 if (opType == MachineOperand::MO_VirtualRegister)
494 {
495 constantThatMustBeLoaded = true;
496 opValue = isSigned
Chris Lattner82f05d82002-09-17 17:23:09 +0000497 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
498 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000499 }
500 }
501
502 if (opType == MachineOperand::MO_MachineRegister)
503 minstr->SetMachineOperandReg(op, machineRegNum);
504 else if (opType == MachineOperand::MO_SignExtendedImmed ||
505 opType == MachineOperand::MO_UnextendedImmed)
506 minstr->SetMachineOperandConst(op, opType, immedValue);
507 else if (constantThatMustBeLoaded ||
508 (opValue && isa<GlobalValue>(opValue)))
509 { // opValue is a constant that must be explicitly loaded into a reg
510 assert(opValue);
511 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
512 loadConstVec, target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000513 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
514 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000515 }
516 }
517
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000518 // Also, check for implicit operands used by the machine instruction
519 // (no need to check those defined since they cannot be constants).
520 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000521 // -- arguments to a Call
522 // -- return value of a Return
523 // Any such operand that is a constant value needs to be fixed also.
524 // The current instructions with implicit refs (viz., Call and Return)
525 // have no immediate fields, so the constant always needs to be loaded
526 // into a register.
527 //
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000528 bool isCall = instrInfo.isCall(opCode);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000529 unsigned lastCallArgNum = 0; // unused if not a call
530 CallArgsDescriptor* argDesc = NULL; // unused if not a call
531 if (isCall)
532 argDesc = CallArgsDescriptor::get(minstr);
533
Vikram S. Adve6d353262001-10-17 23:57:50 +0000534 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000535 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000536 isa<GlobalValue>(minstr->getImplicitRef(i)))
537 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000538 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000539 TmpInstruction* tmpReg =
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000540 InsertCodeToLoadConstant(F, oldVal, vmInstr, loadConstVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000541 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000542
543 if (isCall)
544 { // find and replace the argument in the CallArgsDescriptor
545 unsigned i=lastCallArgNum;
546 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
547 ++i;
548 assert(i < argDesc->getNumArgs() &&
549 "Constant operands to a call *must* be in the arg list");
550 lastCallArgNum = i;
551 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
552 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000553 }
554
555 return loadConstVec;
556}
557
558