blob: 8c38a8f436bb2f273e1a12272e682c7f17ce51ec [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"
Misha Brukmanfce11432002-10-28 00:28:31 +000013#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000014#include "llvm/CodeGen/InstrForest.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000015#include "llvm/Target/TargetMachine.h"
Chris Lattnerd0f166a2002-12-29 03:13:05 +000016#include "llvm/Target/TargetRegInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000017#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000019#include "llvm/Function.h"
Chris Lattnerc5b8b1a2002-10-28 23:54:47 +000020#include "llvm/DerivedTypes.h"
Vikram S. Advea1d14f32001-10-10 20:50:43 +000021#include "llvm/iMemory.h"
Chris Lattner697954c2002-01-20 22:54:45 +000022using std::vector;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000023
24//*************************** Local Functions ******************************/
25
Vikram S. Advea1d14f32001-10-10 20:50:43 +000026
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000027// Generate code to load the constant into a TmpInstruction (virtual reg) and
28// returns the virtual register.
29//
Vikram S. Adve6d353262001-10-17 23:57:50 +000030static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000031InsertCodeToLoadConstant(Function *F,
Vikram S. Adve42f63202002-03-18 03:33:43 +000032 Value* opValue,
Vikram S. Adve6d353262001-10-17 23:57:50 +000033 Instruction* vmInstr,
34 vector<MachineInstr*>& loadConstVec,
35 TargetMachine& target)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000036{
Vikram S. Adve6d353262001-10-17 23:57:50 +000037 // Create a tmp virtual register to hold the constant.
Chris Lattnerfb3b1ec2002-02-03 07:39:06 +000038 TmpInstruction* tmpReg = new TmpInstruction(opValue);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000039 MachineCodeForInstruction &mcfi = MachineCodeForInstruction::get(vmInstr);
40 mcfi.addTemp(tmpReg);
Vikram S. Advea1d14f32001-10-10 20:50:43 +000041
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +000042 target.getInstrInfo().CreateCodeToLoadConst(target, F, opValue, tmpReg,
43 loadConstVec, mcfi);
Vikram S. Adve6d353262001-10-17 23:57:50 +000044
45 // Record the mapping from the tmp VM instruction to machine instruction.
46 // Do this for all machine instructions that were not mapped to any
47 // other temp values created by
48 // tmpReg->addMachineInstruction(loadConstVec.back());
49
50 return tmpReg;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000051}
52
53
Vikram S. Adve6d353262001-10-17 23:57:50 +000054//---------------------------------------------------------------------------
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000055// Function GetConstantValueAsUnsignedInt
Vikram S. Adve6d353262001-10-17 23:57:50 +000056// Function GetConstantValueAsSignedInt
57//
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000058// Convenience functions to get the value of an integral constant, for an
59// appropriate integer or non-integer type that can be held in a signed
60// or unsigned integer respectively. The type of the argument must be
61// the following:
Vikram S. Adve6d353262001-10-17 23:57:50 +000062// Signed or unsigned integer
63// Boolean
64// Pointer
65//
66// isValidConstant is set to true if a valid constant was found.
67//---------------------------------------------------------------------------
68
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000069uint64_t
70GetConstantValueAsUnsignedInt(const Value *V,
71 bool &isValidConstant)
Vikram S. Advea1d14f32001-10-10 20:50:43 +000072{
Vikram S. Adve6d353262001-10-17 23:57:50 +000073 isValidConstant = true;
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000074
75 if (isa<Constant>(V))
Chris Lattner0c4e8862002-09-03 01:08:28 +000076 if (const ConstantBool *CB = dyn_cast<ConstantBool>(V))
77 return (int64_t)CB->getValue();
78 else if (const ConstantSInt *CS = dyn_cast<ConstantSInt>(V))
79 return (uint64_t)CS->getValue();
80 else if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(V))
81 return CU->getValue();
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000082
Vikram S. Adve6d353262001-10-17 23:57:50 +000083 isValidConstant = false;
84 return 0;
Vikram S. Advea1d14f32001-10-10 20:50:43 +000085}
86
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +000087int64_t
88GetConstantValueAsSignedInt(const Value *V,
89 bool &isValidConstant)
90{
91 uint64_t C = GetConstantValueAsUnsignedInt(V, isValidConstant);
92 if (isValidConstant) {
93 if (V->getType()->isSigned() || C < INT64_MAX) // safe to cast to signed
94 return (int64_t) C;
95 else
96 isValidConstant = false;
97 }
98 return 0;
99}
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000100
Vikram S. Adve68513332002-08-24 21:00:08 +0000101
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000102//---------------------------------------------------------------------------
103// Function: FoldGetElemChain
104//
105// Purpose:
Vikram S. Advec941b872002-03-24 03:37:53 +0000106// Fold a chain of GetElementPtr instructions containing only
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000107// constant offsets into an equivalent (Pointer, IndexVector) pair.
Vikram S. Advec941b872002-03-24 03:37:53 +0000108// Returns the pointer Value, and stores the resulting IndexVector
Vikram S. Adve68513332002-08-24 21:00:08 +0000109// in argument chainIdxVec. This is a helper function for
110// FoldConstantIndices that does the actual folding.
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000111//---------------------------------------------------------------------------
112
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000113
114// Check for a constant 0.
115inline bool
116IsZero(Value* idx)
117{
118 return (idx == ConstantSInt::getNullValue(idx->getType()));
119}
120
Vikram S. Adve68513332002-08-24 21:00:08 +0000121static Value*
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000122FoldGetElemChain(InstrTreeNode* ptrNode, vector<Value*>& chainIdxVec,
123 bool lastInstHasLeadingNonZero)
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000124{
Vikram S. Adve68513332002-08-24 21:00:08 +0000125 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
Vikram S. Adve68513332002-08-24 21:00:08 +0000126 GetElementPtrInst* gepInst =
Chris Lattner597f81f2002-09-12 20:27:10 +0000127 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
Chris Lattner106ff452002-09-11 01:21:29 +0000128
129 // ptr value is not computed in this tree or ptr value does not come from GEP
130 // instruction
131 if (gepInst == NULL)
Vikram S. Adve68513332002-08-24 21:00:08 +0000132 return NULL;
133
Vikram S. Adve17927792002-03-31 18:56:51 +0000134 // Return NULL if we don't fold any instructions in.
Vikram S. Advec941b872002-03-24 03:37:53 +0000135 Value* ptrVal = NULL;
Vikram S. Adve68513332002-08-24 21:00:08 +0000136
Vikram S. Advec941b872002-03-24 03:37:53 +0000137 // Now chase the chain of getElementInstr instructions, if any.
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000138 // Check for any non-constant indices and stop there.
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000139 // Also, stop if the first index of child is a non-zero array index
140 // and the last index of the current node is a non-array index:
141 // in that case, a non-array declared type is being accessed as an array
142 // which is not type-safe, but could be legal.
Vikram S. Advec941b872002-03-24 03:37:53 +0000143 //
Vikram S. Adve68513332002-08-24 21:00:08 +0000144 InstructionNode* ptrChild = gepNode;
145 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
146 ptrChild->getOpLabel() == GetElemPtrIdx))
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000147 {
148 // Child is a GetElemPtr instruction
Vikram S. Adve68513332002-08-24 21:00:08 +0000149 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
150 User::op_iterator OI, firstIdx = gepInst->idx_begin();
151 User::op_iterator lastIdx = gepInst->idx_end();
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000152 bool allConstantOffsets = true;
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000153
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000154 // The first index of every GEP must be an array index.
155 assert((*firstIdx)->getType() == Type::LongTy &&
156 "INTERNAL ERROR: Structure index for a pointer type!");
157
Vikram S. Advecf911de2002-10-14 16:30:55 +0000158 // If the last instruction had a leading non-zero index, check if the
159 // current one references a sequential (i.e., indexable) type.
160 // If not, the code is not type-safe and we would create an illegal GEP
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000161 // by folding them, so don't fold any more instructions.
162 //
163 if (lastInstHasLeadingNonZero)
Vikram S. Advecf911de2002-10-14 16:30:55 +0000164 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
165 break; // cannot fold in any preceding getElementPtr instrs.
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000166
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000167 // Check that all offsets are constant for this instruction
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000168 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
169 allConstantOffsets = isa<ConstantInt>(*OI);
170
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000171 if (allConstantOffsets)
Vikram S. Adve17927792002-03-31 18:56:51 +0000172 { // Get pointer value out of ptrChild.
Vikram S. Adve68513332002-08-24 21:00:08 +0000173 ptrVal = gepInst->getPointerOperand();
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000174
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000175 // Remember if it has leading zero index: it will be discarded later.
176 lastInstHasLeadingNonZero = ! IsZero(*firstIdx);
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000177
178 // Insert its index vector at the start, skipping any leading [0]
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000179 chainIdxVec.insert(chainIdxVec.begin(),
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000180 firstIdx + !lastInstHasLeadingNonZero, lastIdx);
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000181
Vikram S. Adve17927792002-03-31 18:56:51 +0000182 // Mark the folded node so no code is generated for it.
Vikram S. Advec941b872002-03-24 03:37:53 +0000183 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000184
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000185 // Get the previous GEP instruction and continue trying to fold
186 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
187 }
188 else // cannot fold this getElementPtr instr. or any preceding ones
189 break;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000190 }
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000191
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000192 // If the first getElementPtr instruction had a leading [0], add it back.
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000193 // Note that this instruction is the *last* one successfully folded above.
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000194 if (ptrVal && ! lastInstHasLeadingNonZero)
Chris Lattner106ff452002-09-11 01:21:29 +0000195 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
Vikram S. Adve1b51b1b2002-08-04 20:49:49 +0000196
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000197 return ptrVal;
198}
199
200
Vikram S. Adve68513332002-08-24 21:00:08 +0000201//---------------------------------------------------------------------------
Vikram S. Advecf911de2002-10-14 16:30:55 +0000202// Function: GetGEPInstArgs
203//
204// Purpose:
205// Helper function for GetMemInstArgs that handles the final getElementPtr
206// instruction used by (or same as) the memory operation.
207// Extracts the indices of the current instruction and tries to fold in
208// preceding ones if all indices of the current one are constant.
209//---------------------------------------------------------------------------
210
211Value*
212GetGEPInstArgs(InstructionNode* gepNode,
213 vector<Value*>& idxVec,
214 bool& allConstantIndices)
215{
216 allConstantIndices = true;
217 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
218
219 // Default pointer is the one from the current instruction.
220 Value* ptrVal = gepI->getPointerOperand();
221 InstrTreeNode* ptrChild = gepNode->leftChild();
222
223 // Extract the index vector of the GEP instructin.
224 // If all indices are constant and first index is zero, try to fold
225 // in preceding GEPs with all constant indices.
226 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
227 allConstantIndices && OI != OE; ++OI)
228 if (! isa<Constant>(*OI))
229 allConstantIndices = false; // note: this also terminates loop!
230
231 // If we have only constant indices, fold chains of constant indices
232 // in this and any preceding GetElemPtr instructions.
233 bool foldedGEPs = false;
234 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
235 if (allConstantIndices)
236 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx))
237 {
238 ptrVal = newPtr;
239 foldedGEPs = true;
240 }
241
242 // Append the index vector of the current instruction.
243 // Skip the leading [0] index if preceding GEPs were folded into this.
244 idxVec.insert(idxVec.end(),
245 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
246 gepI->idx_end());
247
248 return ptrVal;
249}
250
251//---------------------------------------------------------------------------
Vikram S. Adve68513332002-08-24 21:00:08 +0000252// Function: GetMemInstArgs
253//
254// Purpose:
255// Get the pointer value and the index vector for a memory operation
256// (GetElementPtr, Load, or Store). If all indices of the given memory
257// operation are constant, fold in constant indices in a chain of
258// preceding GetElementPtr instructions (if any), and return the
259// pointer value of the first instruction in the chain.
260// All folded instructions are marked so no code is generated for them.
261//
262// Return values:
263// Returns the pointer Value to use.
264// Returns the resulting IndexVector in idxVec.
265// Returns true/false in allConstantIndices if all indices are/aren't const.
266//---------------------------------------------------------------------------
267
Vikram S. Adve68513332002-08-24 21:00:08 +0000268Value*
Vikram S. Advecf911de2002-10-14 16:30:55 +0000269GetMemInstArgs(InstructionNode* memInstrNode,
Vikram S. Adve68513332002-08-24 21:00:08 +0000270 vector<Value*>& idxVec,
271 bool& allConstantIndices)
272{
Vikram S. Advecf911de2002-10-14 16:30:55 +0000273 allConstantIndices = false;
Vikram S. Adve68513332002-08-24 21:00:08 +0000274 Instruction* memInst = memInstrNode->getInstruction();
Vikram S. Adveaebdbe62002-09-29 22:55:05 +0000275 assert(idxVec.size() == 0 && "Need empty vector to return indices");
Vikram S. Adve68513332002-08-24 21:00:08 +0000276
277 // If there is a GetElemPtr instruction to fold in to this instr,
278 // it must be in the left child for Load and GetElemPtr, and in the
279 // right child for Store instructions.
280 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
281 ? memInstrNode->rightChild()
282 : memInstrNode->leftChild());
Vikram S. Advecf911de2002-10-14 16:30:55 +0000283
Vikram S. Adve68513332002-08-24 21:00:08 +0000284 // Default pointer is the one from the current instruction.
285 Value* ptrVal = ptrChild->getValue();
286
Vikram S. Advecf911de2002-10-14 16:30:55 +0000287 // Find the "last" GetElemPtr instruction: this one or the immediate child.
288 // There will be none if this is a load or a store from a scalar pointer.
289 InstructionNode* gepNode = NULL;
290 if (isa<GetElementPtrInst>(memInst))
291 gepNode = memInstrNode;
292 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal))
293 { // Child of load/store is a GEP and memInst is its only use.
294 // Use its indices and mark it as folded.
295 gepNode = cast<InstructionNode>(ptrChild);
296 gepNode->markFoldedIntoParent();
297 }
Vikram S. Adve68513332002-08-24 21:00:08 +0000298
Vikram S. Advecf911de2002-10-14 16:30:55 +0000299 // If there are no indices, return the current pointer.
300 // Else extract the pointer from the GEP and fold the indices.
Chris Lattner04120772003-01-15 19:47:53 +0000301 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
302 : ptrVal;
Vikram S. Adve68513332002-08-24 21:00:08 +0000303}
304
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000305MachineOperand::MachineOperandType
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000306ChooseRegOrImmed(int64_t intValue,
307 bool isSigned,
308 MachineOpCode opCode,
309 const TargetMachine& target,
310 bool canUseImmed,
311 unsigned int& getMachineRegNum,
312 int64_t& getImmedValue)
313{
314 MachineOperand::MachineOperandType opType=MachineOperand::MO_VirtualRegister;
315 getMachineRegNum = 0;
316 getImmedValue = 0;
317
318 if (canUseImmed &&
319 target.getInstrInfo().constantFitsInImmedField(opCode, intValue))
320 {
321 opType = isSigned? MachineOperand::MO_SignExtendedImmed
322 : MachineOperand::MO_UnextendedImmed;
323 getImmedValue = intValue;
324 }
325 else if (intValue == 0 && target.getRegInfo().getZeroRegNum() >= 0)
326 {
327 opType = MachineOperand::MO_MachineRegister;
328 getMachineRegNum = target.getRegInfo().getZeroRegNum();
329 }
330
331 return opType;
332}
333
334
335MachineOperand::MachineOperandType
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000336ChooseRegOrImmed(Value* val,
337 MachineOpCode opCode,
338 const TargetMachine& target,
339 bool canUseImmed,
340 unsigned int& getMachineRegNum,
341 int64_t& getImmedValue)
342{
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000343 getMachineRegNum = 0;
344 getImmedValue = 0;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000345
346 // To use reg or immed, constant needs to be integer, bool, or a NULL pointer
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000347 Constant *CPV = dyn_cast<Constant>(val);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000348 if (CPV == NULL ||
349 (! CPV->getType()->isIntegral() &&
350 ! (isa<PointerType>(CPV->getType()) && CPV->isNullValue())))
351 return MachineOperand::MO_VirtualRegister;
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000352
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000353 // Now get the constant value and check if it fits in the IMMED field.
354 // Take advantage of the fact that the max unsigned value will rarely
355 // fit into any IMMED field and ignore that case (i.e., cast smaller
356 // unsigned constants to signed).
357 //
358 int64_t intValue;
Chris Lattner9b625032002-05-06 16:15:30 +0000359 if (isa<PointerType>(CPV->getType()))
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000360 intValue = 0; // We checked above that it is NULL
361 else if (ConstantBool* CB = dyn_cast<ConstantBool>(CPV))
362 intValue = (int64_t) CB->getValue();
Vikram S. Adve9e29f782001-11-14 17:55:02 +0000363 else if (CPV->getType()->isSigned())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000364 intValue = cast<ConstantSInt>(CPV)->getValue();
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000365 else
Vikram S. Adve1c10f172002-09-27 14:26:20 +0000366 { // get the int value and sign-extend if original was less than 64 bits
367 intValue = (int64_t) cast<ConstantUInt>(CPV)->getValue();
368 switch(CPV->getType()->getPrimitiveID())
369 {
370 case Type::UByteTyID: intValue = (int64_t) (int8_t) intValue; break;
371 case Type::UShortTyID: intValue = (int64_t) (short) intValue; break;
372 case Type::UIntTyID: intValue = (int64_t) (int) intValue; break;
373 default: break;
374 }
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000375 }
376
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000377 return ChooseRegOrImmed(intValue, CPV->getType()->isSigned(),
378 opCode, target, canUseImmed,
379 getMachineRegNum, getImmedValue);
Vikram S. Advea1d14f32001-10-10 20:50:43 +0000380}
381
Vikram S. Adve6d353262001-10-17 23:57:50 +0000382
Chris Lattner04120772003-01-15 19:47:53 +0000383
Vikram S. Adve6d353262001-10-17 23:57:50 +0000384//---------------------------------------------------------------------------
385// Function: FixConstantOperandsForInstr
386//
387// Purpose:
388// Special handling for constant operands of a machine instruction
389// -- if the constant is 0, use the hardwired 0 register, if any;
390// -- if the constant fits in the IMMEDIATE field, use that field;
391// -- else create instructions to put the constant into a register, either
392// directly or by loading explicitly from the constant pool.
393//
394// In the first 2 cases, the operand of `minstr' is modified in place.
395// Returns a vector of machine instructions generated for operands that
396// fall under case 3; these must be inserted before `minstr'.
397//---------------------------------------------------------------------------
398
399vector<MachineInstr*>
400FixConstantOperandsForInstr(Instruction* vmInstr,
401 MachineInstr* minstr,
402 TargetMachine& target)
403{
Chris Lattner04120772003-01-15 19:47:53 +0000404 vector<MachineInstr*> MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000405
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000406 MachineOpCode opCode = minstr->getOpCode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000407 const TargetInstrInfo& instrInfo = target.getInstrInfo();
Chris Lattner8f780272002-10-29 17:25:41 +0000408 int resultPos = instrInfo.getResultPos(opCode);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000409 int immedPos = instrInfo.getImmedConstantPos(opCode);
410
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000411 Function *F = vmInstr->getParent()->getParent();
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000412
Vikram S. Adve6d353262001-10-17 23:57:50 +0000413 for (unsigned op=0; op < minstr->getNumOperands(); op++)
414 {
415 const MachineOperand& mop = minstr->getOperand(op);
416
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000417 // Skip the result position, preallocated machine registers, or operands
418 // that cannot be constants (CC regs or PC-relative displacements)
Chris Lattner8f780272002-10-29 17:25:41 +0000419 if (resultPos == (int)op ||
Chris Lattner133f0792002-10-28 04:45:29 +0000420 mop.getType() == MachineOperand::MO_MachineRegister ||
421 mop.getType() == MachineOperand::MO_CCRegister ||
422 mop.getType() == MachineOperand::MO_PCRelativeDisp)
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000423 continue;
424
Vikram S. Adve6d353262001-10-17 23:57:50 +0000425 bool constantThatMustBeLoaded = false;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000426 unsigned int machineRegNum = 0;
427 int64_t immedValue = 0;
428 Value* opValue = NULL;
429 MachineOperand::MachineOperandType opType =
430 MachineOperand::MO_VirtualRegister;
431
432 // Operand may be a virtual register or a compile-time constant
Chris Lattner133f0792002-10-28 04:45:29 +0000433 if (mop.getType() == MachineOperand::MO_VirtualRegister)
Vikram S. Adve42f63202002-03-18 03:33:43 +0000434 {
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000435 assert(mop.getVRegValue() != NULL);
Chris Lattner04120772003-01-15 19:47:53 +0000436 if (Constant *opConst = dyn_cast<Constant>(mop.getVRegValue())) {
437 opType = ChooseRegOrImmed(opConst, opCode, target,
438 (immedPos == (int)op), machineRegNum,
439 immedValue);
440 if (opType == MachineOperand::MO_VirtualRegister)
441 constantThatMustBeLoaded = true;
442 }
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000443 }
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000444 else
445 {
Chris Lattner04120772003-01-15 19:47:53 +0000446 assert(mop.isImmediate());
447 bool isSigned = mop.getType() == MachineOperand::MO_SignExtendedImmed;
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000448
449 // Bit-selection flags indicate an instruction that is extracting
450 // bits from its operand so ignore this even if it is a big constant.
451 if (mop.opHiBits32() || mop.opLoBits32() ||
452 mop.opHiBits64() || mop.opLoBits64())
453 continue;
454
455 opType = ChooseRegOrImmed(mop.getImmedValue(), isSigned,
456 opCode, target, (immedPos == (int)op),
457 machineRegNum, immedValue);
458
Chris Lattner133f0792002-10-28 04:45:29 +0000459 if (opType == mop.getType())
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000460 continue; // no change: this is the most common case
461
462 if (opType == MachineOperand::MO_VirtualRegister)
463 {
464 constantThatMustBeLoaded = true;
465 opValue = isSigned
Chris Lattner82f05d82002-09-17 17:23:09 +0000466 ? (Value*)ConstantSInt::get(Type::LongTy, immedValue)
467 : (Value*)ConstantUInt::get(Type::ULongTy,(uint64_t)immedValue);
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000468 }
469 }
470
471 if (opType == MachineOperand::MO_MachineRegister)
472 minstr->SetMachineOperandReg(op, machineRegNum);
473 else if (opType == MachineOperand::MO_SignExtendedImmed ||
474 opType == MachineOperand::MO_UnextendedImmed)
475 minstr->SetMachineOperandConst(op, opType, immedValue);
476 else if (constantThatMustBeLoaded ||
477 (opValue && isa<GlobalValue>(opValue)))
478 { // opValue is a constant that must be explicitly loaded into a reg
479 assert(opValue);
480 TmpInstruction* tmpReg = InsertCodeToLoadConstant(F, opValue, vmInstr,
Chris Lattner04120772003-01-15 19:47:53 +0000481 MVec, target);
Vikram S. Adve42f63202002-03-18 03:33:43 +0000482 minstr->SetMachineOperandVal(op, MachineOperand::MO_VirtualRegister,
483 tmpReg);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000484 }
485 }
486
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000487 // Also, check for implicit operands used by the machine instruction
488 // (no need to check those defined since they cannot be constants).
489 // These include:
Vikram S. Adve6d353262001-10-17 23:57:50 +0000490 // -- arguments to a Call
491 // -- return value of a Return
492 // Any such operand that is a constant value needs to be fixed also.
493 // The current instructions with implicit refs (viz., Call and Return)
494 // have no immediate fields, so the constant always needs to be loaded
495 // into a register.
496 //
Vikram S. Advefd0ec802002-09-16 15:15:57 +0000497 bool isCall = instrInfo.isCall(opCode);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000498 unsigned lastCallArgNum = 0; // unused if not a call
499 CallArgsDescriptor* argDesc = NULL; // unused if not a call
500 if (isCall)
501 argDesc = CallArgsDescriptor::get(minstr);
502
Vikram S. Adve6d353262001-10-17 23:57:50 +0000503 for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000504 if (isa<Constant>(minstr->getImplicitRef(i)) ||
Vikram S. Adve6d353262001-10-17 23:57:50 +0000505 isa<GlobalValue>(minstr->getImplicitRef(i)))
506 {
Vikram S. Adve94e40ef2001-10-28 21:46:23 +0000507 Value* oldVal = minstr->getImplicitRef(i);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000508 TmpInstruction* tmpReg =
Chris Lattner04120772003-01-15 19:47:53 +0000509 InsertCodeToLoadConstant(F, oldVal, vmInstr, MVec, target);
Vikram S. Adve6d353262001-10-17 23:57:50 +0000510 minstr->setImplicitRef(i, tmpReg);
Vikram S. Adve36f0a9e2002-05-19 15:34:29 +0000511
512 if (isCall)
513 { // find and replace the argument in the CallArgsDescriptor
514 unsigned i=lastCallArgNum;
515 while (argDesc->getArgInfo(i).getArgVal() != oldVal)
516 ++i;
517 assert(i < argDesc->getNumArgs() &&
518 "Constant operands to a call *must* be in the arg list");
519 lastCallArgNum = i;
520 argDesc->getArgInfo(i).replaceArgVal(tmpReg);
521 }
Vikram S. Adve6d353262001-10-17 23:57:50 +0000522 }
523
Chris Lattner04120772003-01-15 19:47:53 +0000524 return MVec;
Vikram S. Adve6d353262001-10-17 23:57:50 +0000525}