blob: 58c15edc29f239ac39de865caf7c02781f253b59 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000011#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerea45d7b2002-12-28 20:19:44 +000016#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner9c461082002-02-03 07:50:56 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/iTerminators.h"
20#include "llvm/iMemory.h"
21#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000024#include "llvm/ConstantHandling.h"
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +000025#include "llvm/Intrinsics.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000027#include <math.h>
Chris Lattner20b1ea02001-09-14 03:47:57 +000028
Chris Lattner54e898e2003-01-15 19:23:34 +000029static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +000030 std::vector<MachineInstr*>& mvec) {
Chris Lattner54e898e2003-01-15 19:23:34 +000031 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
32 .addReg(Node->rightChild()->getValue())
33 .addRegDef(Node->getValue()));
34}
35
36
37
Chris Lattner795ba6c2003-01-15 21:36:50 +000038//---------------------------------------------------------------------------
39// Function: GetMemInstArgs
40//
41// Purpose:
42// Get the pointer value and the index vector for a memory operation
43// (GetElementPtr, Load, or Store). If all indices of the given memory
44// operation are constant, fold in constant indices in a chain of
45// preceding GetElementPtr instructions (if any), and return the
46// pointer value of the first instruction in the chain.
47// All folded instructions are marked so no code is generated for them.
48//
49// Return values:
50// Returns the pointer Value to use.
51// Returns the resulting IndexVector in idxVec.
52// Returns true/false in allConstantIndices if all indices are/aren't const.
53//---------------------------------------------------------------------------
54
55
56//---------------------------------------------------------------------------
57// Function: FoldGetElemChain
58//
59// Purpose:
60// Fold a chain of GetElementPtr instructions containing only
61// constant offsets into an equivalent (Pointer, IndexVector) pair.
62// Returns the pointer Value, and stores the resulting IndexVector
63// in argument chainIdxVec. This is a helper function for
64// FoldConstantIndices that does the actual folding.
65//---------------------------------------------------------------------------
66
67
68// Check for a constant 0.
69inline bool
70IsZero(Value* idx)
71{
72 return (idx == ConstantSInt::getNullValue(idx->getType()));
73}
74
75static Value*
Misha Brukmanee563cb2003-05-21 17:59:06 +000076FoldGetElemChain(InstrTreeNode* ptrNode, std::vector<Value*>& chainIdxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +000077 bool lastInstHasLeadingNonZero)
78{
79 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
80 GetElementPtrInst* gepInst =
81 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
82
83 // ptr value is not computed in this tree or ptr value does not come from GEP
84 // instruction
85 if (gepInst == NULL)
86 return NULL;
87
88 // Return NULL if we don't fold any instructions in.
89 Value* ptrVal = NULL;
90
91 // Now chase the chain of getElementInstr instructions, if any.
92 // Check for any non-constant indices and stop there.
93 // Also, stop if the first index of child is a non-zero array index
94 // and the last index of the current node is a non-array index:
95 // in that case, a non-array declared type is being accessed as an array
96 // which is not type-safe, but could be legal.
97 //
98 InstructionNode* ptrChild = gepNode;
99 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
100 ptrChild->getOpLabel() == GetElemPtrIdx))
Misha Brukman81b06862003-05-21 18:48:06 +0000101 {
102 // Child is a GetElemPtr instruction
103 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
104 User::op_iterator OI, firstIdx = gepInst->idx_begin();
105 User::op_iterator lastIdx = gepInst->idx_end();
106 bool allConstantOffsets = true;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000107
Misha Brukman81b06862003-05-21 18:48:06 +0000108 // The first index of every GEP must be an array index.
109 assert((*firstIdx)->getType() == Type::LongTy &&
110 "INTERNAL ERROR: Structure index for a pointer type!");
Chris Lattner795ba6c2003-01-15 21:36:50 +0000111
Misha Brukman81b06862003-05-21 18:48:06 +0000112 // If the last instruction had a leading non-zero index, check if the
113 // current one references a sequential (i.e., indexable) type.
114 // If not, the code is not type-safe and we would create an illegal GEP
115 // by folding them, so don't fold any more instructions.
116 //
117 if (lastInstHasLeadingNonZero)
118 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
119 break; // cannot fold in any preceding getElementPtr instrs.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000120
Misha Brukman81b06862003-05-21 18:48:06 +0000121 // Check that all offsets are constant for this instruction
122 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
123 allConstantOffsets = isa<ConstantInt>(*OI);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000124
Misha Brukman81b06862003-05-21 18:48:06 +0000125 if (allConstantOffsets) {
126 // Get pointer value out of ptrChild.
127 ptrVal = gepInst->getPointerOperand();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000128
Misha Brukman81b06862003-05-21 18:48:06 +0000129 // Insert its index vector at the start, skipping any leading [0]
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000130 // Remember the old size to check if anything was inserted.
131 unsigned oldSize = chainIdxVec.size();
132 int firstIsZero = IsZero(*firstIdx);
133 chainIdxVec.insert(chainIdxVec.begin(), firstIdx + firstIsZero, lastIdx);
134
135 // Remember if it has leading zero index: it will be discarded later.
136 if (oldSize < chainIdxVec.size())
137 lastInstHasLeadingNonZero = !firstIsZero;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000138
Misha Brukman81b06862003-05-21 18:48:06 +0000139 // Mark the folded node so no code is generated for it.
140 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000141
Misha Brukman81b06862003-05-21 18:48:06 +0000142 // Get the previous GEP instruction and continue trying to fold
143 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
144 } else // cannot fold this getElementPtr instr. or any preceding ones
145 break;
146 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000147
148 // If the first getElementPtr instruction had a leading [0], add it back.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000149 // Note that this instruction is the *last* one that was successfully
150 // folded *and* contributed any indices, in the loop above.
151 //
Chris Lattner795ba6c2003-01-15 21:36:50 +0000152 if (ptrVal && ! lastInstHasLeadingNonZero)
153 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
154
155 return ptrVal;
156}
157
158
159//---------------------------------------------------------------------------
160// Function: GetGEPInstArgs
161//
162// Purpose:
163// Helper function for GetMemInstArgs that handles the final getElementPtr
164// instruction used by (or same as) the memory operation.
165// Extracts the indices of the current instruction and tries to fold in
166// preceding ones if all indices of the current one are constant.
167//---------------------------------------------------------------------------
168
169static Value *
170GetGEPInstArgs(InstructionNode* gepNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000171 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000172 bool& allConstantIndices)
173{
174 allConstantIndices = true;
175 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
176
177 // Default pointer is the one from the current instruction.
178 Value* ptrVal = gepI->getPointerOperand();
179 InstrTreeNode* ptrChild = gepNode->leftChild();
180
181 // Extract the index vector of the GEP instructin.
182 // If all indices are constant and first index is zero, try to fold
183 // in preceding GEPs with all constant indices.
184 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
185 allConstantIndices && OI != OE; ++OI)
186 if (! isa<Constant>(*OI))
187 allConstantIndices = false; // note: this also terminates loop!
188
189 // If we have only constant indices, fold chains of constant indices
190 // in this and any preceding GetElemPtr instructions.
191 bool foldedGEPs = false;
192 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
193 if (allConstantIndices)
Misha Brukman81b06862003-05-21 18:48:06 +0000194 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) {
195 ptrVal = newPtr;
196 foldedGEPs = true;
197 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000198
199 // Append the index vector of the current instruction.
200 // Skip the leading [0] index if preceding GEPs were folded into this.
201 idxVec.insert(idxVec.end(),
202 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
203 gepI->idx_end());
204
205 return ptrVal;
206}
207
208//---------------------------------------------------------------------------
209// Function: GetMemInstArgs
210//
211// Purpose:
212// Get the pointer value and the index vector for a memory operation
213// (GetElementPtr, Load, or Store). If all indices of the given memory
214// operation are constant, fold in constant indices in a chain of
215// preceding GetElementPtr instructions (if any), and return the
216// pointer value of the first instruction in the chain.
217// All folded instructions are marked so no code is generated for them.
218//
219// Return values:
220// Returns the pointer Value to use.
221// Returns the resulting IndexVector in idxVec.
222// Returns true/false in allConstantIndices if all indices are/aren't const.
223//---------------------------------------------------------------------------
224
225static Value*
226GetMemInstArgs(InstructionNode* memInstrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000227 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000228 bool& allConstantIndices)
229{
230 allConstantIndices = false;
231 Instruction* memInst = memInstrNode->getInstruction();
232 assert(idxVec.size() == 0 && "Need empty vector to return indices");
233
234 // If there is a GetElemPtr instruction to fold in to this instr,
235 // it must be in the left child for Load and GetElemPtr, and in the
236 // right child for Store instructions.
237 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
238 ? memInstrNode->rightChild()
239 : memInstrNode->leftChild());
240
241 // Default pointer is the one from the current instruction.
242 Value* ptrVal = ptrChild->getValue();
243
244 // Find the "last" GetElemPtr instruction: this one or the immediate child.
245 // There will be none if this is a load or a store from a scalar pointer.
246 InstructionNode* gepNode = NULL;
247 if (isa<GetElementPtrInst>(memInst))
248 gepNode = memInstrNode;
Misha Brukman81b06862003-05-21 18:48:06 +0000249 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) {
250 // Child of load/store is a GEP and memInst is its only use.
251 // Use its indices and mark it as folded.
252 gepNode = cast<InstructionNode>(ptrChild);
253 gepNode->markFoldedIntoParent();
254 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000255
256 // If there are no indices, return the current pointer.
257 // Else extract the pointer from the GEP and fold the indices.
258 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
259 : ptrVal;
260}
261
Chris Lattner54e898e2003-01-15 19:23:34 +0000262
Chris Lattner20b1ea02001-09-14 03:47:57 +0000263//************************ Internal Functions ******************************/
264
Chris Lattner20b1ea02001-09-14 03:47:57 +0000265
Chris Lattner20b1ea02001-09-14 03:47:57 +0000266static inline MachineOpCode
267ChooseBprInstruction(const InstructionNode* instrNode)
268{
269 MachineOpCode opCode;
270
271 Instruction* setCCInstr =
272 ((InstructionNode*) instrNode->leftChild())->getInstruction();
273
274 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000275 {
276 case Instruction::SetEQ: opCode = V9::BRZ; break;
277 case Instruction::SetNE: opCode = V9::BRNZ; break;
278 case Instruction::SetLE: opCode = V9::BRLEZ; break;
279 case Instruction::SetGE: opCode = V9::BRGEZ; break;
280 case Instruction::SetLT: opCode = V9::BRLZ; break;
281 case Instruction::SetGT: opCode = V9::BRGZ; break;
282 default:
283 assert(0 && "Unrecognized VM instruction!");
284 opCode = V9::INVALID_OPCODE;
285 break;
286 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000287
288 return opCode;
289}
290
291
292static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000293ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000294 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000295{
Misha Brukmana98cd452003-05-20 20:32:24 +0000296 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000297
298 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
299
Misha Brukman81b06862003-05-21 18:48:06 +0000300 if (isSigned) {
301 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000302 {
Misha Brukman81b06862003-05-21 18:48:06 +0000303 case Instruction::SetEQ: opCode = V9::BE; break;
304 case Instruction::SetNE: opCode = V9::BNE; break;
305 case Instruction::SetLE: opCode = V9::BLE; break;
306 case Instruction::SetGE: opCode = V9::BGE; break;
307 case Instruction::SetLT: opCode = V9::BL; break;
308 case Instruction::SetGT: opCode = V9::BG; break;
309 default:
310 assert(0 && "Unrecognized VM instruction!");
311 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000312 }
Misha Brukman81b06862003-05-21 18:48:06 +0000313 } else {
314 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000315 {
Misha Brukman81b06862003-05-21 18:48:06 +0000316 case Instruction::SetEQ: opCode = V9::BE; break;
317 case Instruction::SetNE: opCode = V9::BNE; break;
318 case Instruction::SetLE: opCode = V9::BLEU; break;
319 case Instruction::SetGE: opCode = V9::BCC; break;
320 case Instruction::SetLT: opCode = V9::BCS; break;
321 case Instruction::SetGT: opCode = V9::BGU; break;
322 default:
323 assert(0 && "Unrecognized VM instruction!");
324 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000325 }
Misha Brukman81b06862003-05-21 18:48:06 +0000326 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000327
328 return opCode;
329}
330
331static inline MachineOpCode
332ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000333 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000334{
Misha Brukmana98cd452003-05-20 20:32:24 +0000335 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000336
337 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000338 {
339 case Instruction::SetEQ: opCode = V9::FBE; break;
340 case Instruction::SetNE: opCode = V9::FBNE; break;
341 case Instruction::SetLE: opCode = V9::FBLE; break;
342 case Instruction::SetGE: opCode = V9::FBGE; break;
343 case Instruction::SetLT: opCode = V9::FBL; break;
344 case Instruction::SetGT: opCode = V9::FBG; break;
345 default:
346 assert(0 && "Unrecognized VM instruction!");
347 break;
348 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000349
350 return opCode;
351}
352
353
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000354// Create a unique TmpInstruction for a boolean value,
355// representing the CC register used by a branch on that value.
356// For now, hack this using a little static cache of TmpInstructions.
357// Eventually the entire BURG instruction selection should be put
358// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000359// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000360// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000361//
362static TmpInstruction*
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000363GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType,
364 MachineCodeForInstruction& mcfi)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000365{
Chris Lattner09ff1122002-07-24 21:21:32 +0000366 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000367 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000368 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000369
370 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
371
Misha Brukman81b06862003-05-21 18:48:06 +0000372 if (lastFunction != F) {
373 lastFunction = F;
374 boolToTmpCache.clear();
375 }
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000376
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000377 // Look for tmpI and create a new one otherwise. The new value is
378 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000379 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
380 if (tmpI == NULL)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000381 tmpI = new TmpInstruction(mcfi, ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000382
383 return tmpI;
384}
385
386
Chris Lattner20b1ea02001-09-14 03:47:57 +0000387static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000388ChooseBccInstruction(const InstructionNode* instrNode,
389 bool& isFPBranch)
390{
391 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000392 assert(setCCNode->getOpLabel() == SetCCOp);
393 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000394 const Type* setCCType = setCCInstr->getOperand(0)->getType();
395
Vikram S. Adve242a8082002-05-19 15:25:51 +0000396 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
397
398 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000399 return ChooseBFpccInstruction(instrNode, setCCInstr);
400 else
401 return ChooseBpccInstruction(instrNode, setCCInstr);
402}
403
404
Misha Brukmaneecdb662003-06-02 20:55:14 +0000405// WARNING: since this function has only one caller, it always returns
406// the opcode that expects an immediate and a register. If this function
407// is ever used in cases where an opcode that takes two registers is required,
408// then modify this function and use convertOpcodeFromRegToImm() where required.
409//
410// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
411// new cases of opcodes.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000412static inline MachineOpCode
Misha Brukmaneecdb662003-06-02 20:55:14 +0000413ChooseMovFpcciInstruction(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000414{
Misha Brukmana98cd452003-05-20 20:32:24 +0000415 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000416
417 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000418 {
Misha Brukmaneecdb662003-06-02 20:55:14 +0000419 case Instruction::SetEQ: opCode = V9::MOVFEi; break;
420 case Instruction::SetNE: opCode = V9::MOVFNEi; break;
421 case Instruction::SetLE: opCode = V9::MOVFLEi; break;
422 case Instruction::SetGE: opCode = V9::MOVFGEi; break;
423 case Instruction::SetLT: opCode = V9::MOVFLi; break;
424 case Instruction::SetGT: opCode = V9::MOVFGi; break;
Misha Brukman81b06862003-05-21 18:48:06 +0000425 default:
426 assert(0 && "Unrecognized VM instruction!");
427 break;
428 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000429
430 return opCode;
431}
432
433
434// Assumes that SUBcc v1, v2 -> v3 has been executed.
435// In most cases, we want to clear v3 and then follow it by instruction
436// MOVcc 1 -> v3.
437// Set mustClearReg=false if v3 need not be cleared before conditional move.
438// Set valueToMove=0 if we want to conditionally move 0 instead of 1
439// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000440// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000441//
Misha Brukmaneecdb662003-06-02 20:55:14 +0000442// WARNING: since this function has only one caller, it always returns
443// the opcode that expects an immediate and a register. If this function
444// is ever used in cases where an opcode that takes two registers is required,
445// then modify this function and use convertOpcodeFromRegToImm() where required.
446//
447// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
448// new cases of opcodes.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000449static MachineOpCode
Misha Brukmaneecdb662003-06-02 20:55:14 +0000450ChooseMovpcciAfterSub(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000451{
Misha Brukmana98cd452003-05-20 20:32:24 +0000452 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000453
454 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000455 {
Misha Brukmaneecdb662003-06-02 20:55:14 +0000456 case Instruction::SetEQ: opCode = V9::MOVEi; break;
457 case Instruction::SetLE: opCode = V9::MOVLEi; break;
458 case Instruction::SetGE: opCode = V9::MOVGEi; break;
459 case Instruction::SetLT: opCode = V9::MOVLi; break;
460 case Instruction::SetGT: opCode = V9::MOVGi; break;
461 case Instruction::SetNE: opCode = V9::MOVNEi; break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000462 default: assert(0 && "Unrecognized VM instr!"); break;
Misha Brukman81b06862003-05-21 18:48:06 +0000463 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000464
465 return opCode;
466}
467
Chris Lattner20b1ea02001-09-14 03:47:57 +0000468static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000469ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000470{
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000471 assert((vopCode == ToFloatTy || vopCode == ToDoubleTy) &&
472 "Unrecognized convert-to-float opcode!");
473
Misha Brukmana98cd452003-05-20 20:32:24 +0000474 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000475
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000476 if (opType == Type::SByteTy || opType == Type::UByteTy ||
477 opType == Type::ShortTy || opType == Type::UShortTy ||
478 opType == Type::IntTy || opType == Type::UIntTy)
479 opCode = (vopCode == ToFloatTy? V9::FITOS : V9::FITOD);
Vikram S. Adve784a18b2003-07-02 01:13:57 +0000480 else if (opType == Type::LongTy || opType == Type::ULongTy ||
481 isa<PointerType>(opType))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000482 opCode = (vopCode == ToFloatTy? V9::FXTOS : V9::FXTOD);
483 else if (opType == Type::FloatTy)
484 opCode = (vopCode == ToFloatTy? V9::INVALID_OPCODE : V9::FSTOD);
485 else if (opType == Type::DoubleTy)
486 opCode = (vopCode == ToFloatTy? V9::FDTOS : V9::INVALID_OPCODE);
487 else
Vikram S. Adve784a18b2003-07-02 01:13:57 +0000488 assert(0 && "Trying to convert a non-scalar type to DOUBLE?");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000489
490 return opCode;
491}
492
493static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000494ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000495{
Misha Brukmana98cd452003-05-20 20:32:24 +0000496 MachineOpCode opCode = V9::INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000497
498 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
499 && "This function should only be called for FLOAT or DOUBLE");
500
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000501 // SPARC does not have a float-to-uint conversion, only a float-to-int.
502 // For converting an FP value to uint32_t, we first need to convert to
503 // uint64_t and then to uint32_t, or we may overflow the signed int
504 // representation even for legal uint32_t values. This expansion is
505 // done by the Preselection pass.
506 //
Misha Brukman81b06862003-05-21 18:48:06 +0000507 if (tid == Type::UIntTyID) {
508 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
509 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
510 } else if (tid == Type::SByteTyID || tid == Type::ShortTyID ||
511 tid == Type::IntTyID || tid == Type::UByteTyID ||
512 tid == Type::UShortTyID) {
513 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
514 } else if (tid == Type::LongTyID || tid == Type::ULongTyID) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000515 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
Misha Brukman81b06862003-05-21 18:48:06 +0000516 } else
517 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000518
Chris Lattner20b1ea02001-09-14 03:47:57 +0000519 return opCode;
520}
521
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000522MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000523CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
524 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000525{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000526 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000527 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000528 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000529}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000530
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000531// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000532// The FP value must be converted to the dest type in an FP register,
533// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000534//
535// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
536// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000537// *only* when converting to an unsigned. (Unsigned byte, short or long
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000538// don't have this problem.)
539// For unsigned int, we therefore have to generate the code sequence:
540//
541// if (V > (float) MAXINT) {
542// unsigned result = (unsigned) (V - (float) MAXINT);
543// result = result + (unsigned) MAXINT;
544// }
545// else
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000546// result = (unsigned) V;
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000547//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000548static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000549CreateCodeToConvertFloatToInt(const TargetMachine& target,
550 Value* opVal,
551 Instruction* destI,
552 std::vector<MachineInstr*>& mvec,
553 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000554{
555 // Create a temporary to represent the FP register into which the
556 // int value will placed after conversion. The type of this temporary
557 // depends on the type of FP register to use: single-prec for a 32-bit
558 // int or smaller; double-prec for a 64-bit int.
559 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000560 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000561 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000562 TmpInstruction* destForCast = new TmpInstruction(mcfi, destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000563
564 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000565 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
566 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000567 mvec.push_back(M);
568
569 // Create the fpreg-to-intreg copy code
570 target.getInstrInfo().
571 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000572 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000573}
574
575
Chris Lattner20b1ea02001-09-14 03:47:57 +0000576static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000577ChooseAddInstruction(const InstructionNode* instrNode)
578{
579 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
580}
581
582
Chris Lattner20b1ea02001-09-14 03:47:57 +0000583static inline MachineInstr*
584CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000585 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000586{
Misha Brukmana98cd452003-05-20 20:32:24 +0000587 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000588 .addReg(instrNode->leftChild()->getValue())
589 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000590}
591
592static inline MachineInstr*
593CreateAddConstInstruction(const InstructionNode* instrNode)
594{
595 MachineInstr* minstr = NULL;
596
597 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000598 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000599
600 // Cases worth optimizing are:
601 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
602 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
603 //
Chris Lattner9b625032002-05-06 16:15:30 +0000604 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000605 double dval = FPC->getValue();
606 if (dval == 0.0)
607 minstr = CreateMovFloatInstruction(instrNode,
608 instrNode->getInstruction()->getType());
609 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000610
611 return minstr;
612}
613
614
615static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000616ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000617{
Misha Brukmana98cd452003-05-20 20:32:24 +0000618 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000619
Misha Brukman81b06862003-05-21 18:48:06 +0000620 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukman91aee472003-05-27 22:37:00 +0000621 opCode = V9::SUBr;
Misha Brukman81b06862003-05-21 18:48:06 +0000622 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000623 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000624 {
625 case Type::FloatTyID: opCode = V9::FSUBS; break;
626 case Type::DoubleTyID: opCode = V9::FSUBD; break;
627 default: assert(0 && "Invalid type for SUB instruction"); break;
628 }
629 }
630
Chris Lattner20b1ea02001-09-14 03:47:57 +0000631 return opCode;
632}
633
634
635static inline MachineInstr*
636CreateSubConstInstruction(const InstructionNode* instrNode)
637{
638 MachineInstr* minstr = NULL;
639
640 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000641 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000642
643 // Cases worth optimizing are:
644 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
645 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
646 //
Chris Lattner9b625032002-05-06 16:15:30 +0000647 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
648 double dval = FPC->getValue();
649 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000650 minstr = CreateMovFloatInstruction(instrNode,
651 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000652 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000653
654 return minstr;
655}
656
657
658static inline MachineOpCode
659ChooseFcmpInstruction(const InstructionNode* instrNode)
660{
Misha Brukmana98cd452003-05-20 20:32:24 +0000661 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000662
663 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
664 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000665 case Type::FloatTyID: opCode = V9::FCMPS; break;
666 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000667 default: assert(0 && "Invalid type for FCMP instruction"); break;
668 }
669
670 return opCode;
671}
672
673
674// Assumes that leftArg and rightArg are both cast instructions.
675//
676static inline bool
677BothFloatToDouble(const InstructionNode* instrNode)
678{
679 InstrTreeNode* leftArg = instrNode->leftChild();
680 InstrTreeNode* rightArg = instrNode->rightChild();
681 InstrTreeNode* leftArgArg = leftArg->leftChild();
682 InstrTreeNode* rightArgArg = rightArg->leftChild();
683 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
684
685 // Check if both arguments are floats cast to double
686 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000687 leftArgArg->getValue()->getType() == Type::FloatTy &&
688 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000689}
690
691
692static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000693ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000694{
Misha Brukmana98cd452003-05-20 20:32:24 +0000695 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000696
Chris Lattner0c4e8862002-09-03 01:08:28 +0000697 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000698 opCode = V9::MULXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000699 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000700 switch(resultType->getPrimitiveID())
Misha Brukman7b647942003-05-30 20:11:56 +0000701 {
702 case Type::FloatTyID: opCode = V9::FMULS; break;
703 case Type::DoubleTyID: opCode = V9::FMULD; break;
704 default: assert(0 && "Invalid type for MUL instruction"); break;
705 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000706
707 return opCode;
708}
709
710
Vikram S. Adve510eec72001-11-04 21:59:14 +0000711
Chris Lattner20b1ea02001-09-14 03:47:57 +0000712static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000713CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000714 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000715{
Misha Brukman91aee472003-05-27 22:37:00 +0000716 return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000717 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000718}
719
720
Vikram S. Adve242a8082002-05-19 15:25:51 +0000721// Create instruction sequence for any shift operation.
722// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
723// requires a second instruction for explicit sign-extension.
724// Note that we only have to worry about a sign-bit appearing in the
725// most significant bit of the operand after shifting (e.g., bit 32 of
726// Int or bit 16 of Short), so we do not have to worry about results
727// that are as large as a normal integer register.
728//
729static inline void
730CreateShiftInstructions(const TargetMachine& target,
731 Function* F,
732 MachineOpCode shiftOpCode,
733 Value* argVal1,
734 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000735 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000736 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000737 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000738 MachineCodeForInstruction& mcfi)
739{
740 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
741 "Large shift sizes unexpected, but can be handled below: "
742 "You need to check whether or not it fits in immed field below");
743
744 // If this is a logical left shift of a type smaller than the standard
745 // integer reg. size, we have to extend the sign-bit into upper bits
746 // of dest, so we need to put the result of the SLL into a temporary.
747 //
748 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000749 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000750
Misha Brukmand36e30e2003-06-06 09:52:23 +0000751 if ((shiftOpCode == V9::SLLr5 || shiftOpCode == V9::SLLXr6) && opSize < 8) {
Misha Brukman7b647942003-05-30 20:11:56 +0000752 // put SLL result into a temporary
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000753 shiftDest = new TmpInstruction(mcfi, argVal1, optArgVal2, "sllTmp");
Misha Brukman7b647942003-05-30 20:11:56 +0000754 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000755
756 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000757 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
758 .addReg(shiftDest, MOTy::Def)
759 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
760 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000761 mvec.push_back(M);
762
Misha Brukman7b647942003-05-30 20:11:56 +0000763 if (shiftDest != destVal) {
764 // extend the sign-bit of the result into all upper bits of dest
765 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
766 target.getInstrInfo().
767 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
768 8*opSize, mvec, mcfi);
769 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000770}
771
772
Vikram S. Adve74825322002-03-18 03:15:35 +0000773// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000774// create a cheaper instruction.
775// This returns the approximate cost of the instructions generated,
776// which is used to pick the cheapest when both operands are constant.
Vikram S. Adve645fea32003-05-25 21:59:47 +0000777static unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000778CreateMulConstInstruction(const TargetMachine &target, Function* F,
779 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000780 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000781 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000782{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000783 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukman91aee472003-05-27 22:37:00 +0000784 unsigned cost = target.getInstrInfo().minLatency(V9::MULXr);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000785 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000786
787 Value* constOp = rval;
788 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000789 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000790
791 // Cases worth optimizing are:
792 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
793 // (2) Multiply by 2^x for integer types: replace with Shift
794 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000795 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000796
Misha Brukmana98cd452003-05-20 20:32:24 +0000797 if (resultType->isInteger() || isa<PointerType>(resultType)) {
798 bool isValidConst;
799 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
800 if (isValidConst) {
801 unsigned pow;
802 bool needNeg = false;
803 if (C < 0) {
804 needNeg = true;
805 C = -C;
806 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000807
Misha Brukmana98cd452003-05-20 20:32:24 +0000808 if (C == 0 || C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000809 cost = target.getInstrInfo().minLatency(V9::ADDr);
Misha Brukmana98cd452003-05-20 20:32:24 +0000810 unsigned Zero = target.getRegInfo().getZeroRegNum();
811 MachineInstr* M;
812 if (C == 0)
Misha Brukman91aee472003-05-27 22:37:00 +0000813 M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000814 else
Misha Brukman91aee472003-05-27 22:37:00 +0000815 M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000816 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +0000817 } else if (isPowerOf2(C, pow)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000818 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Misha Brukmand36e30e2003-06-06 09:52:23 +0000819 MachineOpCode opCode = (opSize <= 32)? V9::SLLr5 : V9::SLLXr6;
Misha Brukmana98cd452003-05-20 20:32:24 +0000820 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
821 destVal, mvec, mcfi);
822 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000823
Misha Brukman7b647942003-05-30 20:11:56 +0000824 if (mvec.size() > 0 && needNeg) {
825 // insert <reg = SUB 0, reg> after the instr to flip the sign
Misha Brukmana98cd452003-05-20 20:32:24 +0000826 MachineInstr* M = CreateIntNegInstruction(target, destVal);
827 mvec.push_back(M);
828 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000829 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000830 } else {
831 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
832 double dval = FPC->getValue();
833 if (fabs(dval) == 1) {
834 MachineOpCode opCode = (dval < 0)
835 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
836 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
837 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
838 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000839 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000840 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000841
Misha Brukmana98cd452003-05-20 20:32:24 +0000842 if (firstNewInstr < mvec.size()) {
843 cost = 0;
844 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
845 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
846 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000847
848 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000849}
850
851
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000852// Does not create any instructions if we cannot exploit constant to
853// create a cheaper instruction.
854//
855static inline void
856CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000857 Function* F,
858 Value* lval, Value* rval,
859 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000860 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000861 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000862{
863 Value* constOp;
Misha Brukman7b647942003-05-30 20:11:56 +0000864 if (isa<Constant>(lval) && isa<Constant>(rval)) {
865 // both operands are constant: evaluate and "set" in dest
866 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
867 cast<Constant>(lval),
868 cast<Constant>(rval));
869 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
870 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000871 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000872 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000873 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000874 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000875
876 // else neither is constant
877 return;
878}
879
Vikram S. Adve74825322002-03-18 03:15:35 +0000880// Return NULL if we cannot exploit constant to create a cheaper instruction
881static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000882CreateMulInstruction(const TargetMachine &target, Function* F,
883 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000884 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000885 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000886 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
887{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000888 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000889 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000890 if (mvec.size() == L) {
891 // no instructions were added so create MUL reg, reg, reg.
892 // Use FSMULD if both operands are actually floats cast to doubles.
893 // Otherwise, use the default opcode for the appropriate type.
894 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
895 ? forceMulOp
896 : ChooseMulInstructionByType(destVal->getType()));
897 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
898 .addRegDef(destVal));
899 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000900}
901
902
Vikram S. Adve510eec72001-11-04 21:59:14 +0000903// Generate a divide instruction for Div or Rem.
904// For Rem, this assumes that the operand type will be signed if the result
905// type is signed. This is correct because they must have the same sign.
906//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000907static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000908ChooseDivInstruction(TargetMachine &target,
909 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000910{
Misha Brukmana98cd452003-05-20 20:32:24 +0000911 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000912
913 const Type* resultType = instrNode->getInstruction()->getType();
914
Chris Lattner0c4e8862002-09-03 01:08:28 +0000915 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000916 opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000917 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000918 switch(resultType->getPrimitiveID())
919 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000920 case Type::FloatTyID: opCode = V9::FDIVS; break;
921 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000922 default: assert(0 && "Invalid type for DIV instruction"); break;
923 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000924
925 return opCode;
926}
927
928
Chris Lattner54e898e2003-01-15 19:23:34 +0000929// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve645fea32003-05-25 21:59:47 +0000930static void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000931CreateDivConstInstruction(TargetMachine &target,
932 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000933 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000934{
Chris Lattner54e898e2003-01-15 19:23:34 +0000935 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000936 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000937 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000938 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000939
Vikram S. Adve645fea32003-05-25 21:59:47 +0000940 Instruction* destVal = instrNode->getInstruction();
Chris Lattner54e898e2003-01-15 19:23:34 +0000941 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000942
943 // Cases worth optimizing are:
944 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
945 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
946 //
947 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000948
Misha Brukman7b647942003-05-30 20:11:56 +0000949 if (resultType->isInteger()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000950 unsigned pow;
951 bool isValidConst;
952 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
953 if (isValidConst) {
954 bool needNeg = false;
955 if (C < 0) {
956 needNeg = true;
957 C = -C;
958 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000959
Misha Brukmana98cd452003-05-20 20:32:24 +0000960 if (C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000961 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000962 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +0000963 } else if (isPowerOf2(C, pow)) {
Vikram S. Adve645fea32003-05-25 21:59:47 +0000964 unsigned opCode;
965 Value* shiftOperand;
966
967 if (resultType->isSigned()) {
968 // The result may be negative and we need to add one before shifting
969 // a negative value. Use:
970 // srl i0, 31, x0; add x0, i0, i1 (if i0 is <= 32 bits)
971 // or
972 // srlx i0, 63, x0; add x0, i0, i1 (if i0 is 64 bits)
973 // to compute i1=i0+1 if i0 < 0 and i1=i0 otherwise.
974 //
975 TmpInstruction *srlTmp, *addTmp;
976 MachineCodeForInstruction& mcfi
977 = MachineCodeForInstruction::get(destVal);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000978 srlTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getSign");
979 addTmp = new TmpInstruction(mcfi, resultType, LHS, srlTmp,"incIfNeg");
Vikram S. Adve645fea32003-05-25 21:59:47 +0000980
981 // Create the SRL or SRLX instruction to get the sign bit
Misha Brukman91aee472003-05-27 22:37:00 +0000982 mvec.push_back(BuildMI((resultType==Type::LongTy) ?
Misha Brukmand36e30e2003-06-06 09:52:23 +0000983 V9::SRLXi6 : V9::SRLi5, 3)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000984 .addReg(LHS)
985 .addSImm((resultType==Type::LongTy)? 63 : 31)
986 .addRegDef(srlTmp));
987
988 // Create the ADD instruction to add 1 for negative values
Misha Brukman91aee472003-05-27 22:37:00 +0000989 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000990 .addRegDef(addTmp));
991
992 // Get the shift operand and "right-shift" opcode to do the divide
993 shiftOperand = addTmp;
Misha Brukmand36e30e2003-06-06 09:52:23 +0000994 opCode = (resultType==Type::LongTy) ? V9::SRAXi6 : V9::SRAi5;
Misha Brukman7b647942003-05-30 20:11:56 +0000995 } else {
Vikram S. Adve645fea32003-05-25 21:59:47 +0000996 // Get the shift operand and "right-shift" opcode to do the divide
997 shiftOperand = LHS;
Misha Brukmand36e30e2003-06-06 09:52:23 +0000998 opCode = (resultType==Type::LongTy) ? V9::SRLXi6 : V9::SRLi5;
Vikram S. Adve645fea32003-05-25 21:59:47 +0000999 }
1000
1001 // Now do the actual shift!
1002 mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow)
1003 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001004 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001005
Misha Brukmana98cd452003-05-20 20:32:24 +00001006 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
1007 // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve645fea32003-05-25 21:59:47 +00001008 mvec.push_back(CreateIntNegInstruction(target, destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001009 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001010 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001011 } else {
1012 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
1013 double dval = FPC->getValue();
1014 if (fabs(dval) == 1) {
1015 unsigned opCode =
1016 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
1017 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001018
Vikram S. Adve645fea32003-05-25 21:59:47 +00001019 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001020 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001021 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001022 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001023}
1024
1025
Vikram S. Adve74825322002-03-18 03:15:35 +00001026static void
1027CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1028 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001029 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001030 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001031 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001032{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001033 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001034 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001035 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001036 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001037
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001038 // Enforce the alignment constraints on the stack pointer at
1039 // compile time if the total size is a known constant.
Misha Brukman7b647942003-05-30 20:11:56 +00001040 if (isa<Constant>(numElementsVal)) {
1041 bool isValid;
1042 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
1043 assert(isValid && "Unexpectedly large array dimension in alloca!");
1044 int64_t total = numElem * tsize;
1045 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1046 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1047 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1048 } else {
1049 // The size is not a constant. Generate code to compute it and
1050 // code to pad the size for stack alignment.
1051 // Create a Value to hold the (constant) element size
1052 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001053
Misha Brukman7b647942003-05-30 20:11:56 +00001054 // Create temporary values to hold the result of MUL, SLL, SRL
Vikram S. Adve80544442003-06-23 02:13:57 +00001055 // To pad `size' to next smallest multiple of 16:
1056 // size = (size + 15) & (-16 = 0xfffffffffffffff0)
1057 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001058 TmpInstruction* tmpProd = new TmpInstruction(mcfi,numElementsVal, tsizeVal);
Vikram S. Adve80544442003-06-23 02:13:57 +00001059 TmpInstruction* tmpAdd15= new TmpInstruction(mcfi,numElementsVal, tmpProd);
1060 TmpInstruction* tmpAndf0= new TmpInstruction(mcfi,numElementsVal, tmpAdd15);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001061
Misha Brukman7b647942003-05-30 20:11:56 +00001062 // Instruction 1: mul numElements, typeSize -> tmpProd
1063 // This will optimize the MUL as far as possible.
Vikram S. Adve80544442003-06-23 02:13:57 +00001064 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd, getMvec,
Misha Brukman7b647942003-05-30 20:11:56 +00001065 mcfi, INVALID_MACHINE_OPCODE);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001066
Vikram S. Adve80544442003-06-23 02:13:57 +00001067 // Instruction 2: andn tmpProd, 0x0f -> tmpAndn
1068 getMvec.push_back(BuildMI(V9::ADDi, 3).addReg(tmpProd).addSImm(15)
1069 .addReg(tmpAdd15, MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001070
Vikram S. Adve80544442003-06-23 02:13:57 +00001071 // Instruction 3: add tmpAndn, 0x10 -> tmpAdd16
1072 getMvec.push_back(BuildMI(V9::ANDi, 3).addReg(tmpAdd15).addSImm(-16)
1073 .addReg(tmpAndf0, MOTy::Def));
1074
1075 totalSizeVal = tmpAndf0;
Misha Brukman7b647942003-05-30 20:11:56 +00001076 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001077
1078 // Get the constant offset from SP for dynamically allocated storage
1079 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001080 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001081 bool growUp;
1082 ConstantSInt* dynamicAreaOffset =
1083 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001084 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001085 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1086
Chris Lattner54e898e2003-01-15 19:23:34 +00001087 unsigned SPReg = target.getRegInfo().getStackPointer();
1088
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001089 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukman91aee472003-05-27 22:37:00 +00001090 getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001091 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001092
Vikram S. Adve74825322002-03-18 03:15:35 +00001093 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukman91aee472003-05-27 22:37:00 +00001094 getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset)
Misha Brukmana98cd452003-05-20 20:32:24 +00001095 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001096}
1097
1098
1099static void
1100CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1101 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001102 unsigned tsize,
1103 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001104 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001105{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001106 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001107 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001108 "Result value is not part of a function?");
1109 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001110 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001111
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001112 // Put the variable in the dynamically sized area of the frame if either:
1113 // (a) The offset is too large to use as an immediate in load/stores
1114 // (check LDX because all load/stores have the same-size immed. field).
1115 // (b) The object is "large", so it could cause many other locals,
1116 // spills, and temporaries to have large offsets.
1117 // NOTE: We use LARGE = 8 * argSlotSize = 64 bytes.
1118 // You've gotta love having only 13 bits for constant offset values :-|.
1119 //
1120 unsigned paddedSize;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001121 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001122 paddedSize,
1123 tsize * numElements);
1124
1125 if (((int)paddedSize) > 8 * target.getFrameInfo().getSizeOfEachArgOnStack() ||
1126 ! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001127 CreateCodeForVariableSizeAlloca(target, result, tsize,
1128 ConstantSInt::get(Type::IntTy,numElements),
1129 getMvec);
1130 return;
1131 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001132
1133 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001134 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001135
1136 // Create a temporary Value to hold the constant offset.
1137 // This is needed because it may not fit in the immediate field.
1138 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1139
1140 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001141 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukman91aee472003-05-27 22:37:00 +00001142 getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001143 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001144}
1145
1146
Chris Lattner20b1ea02001-09-14 03:47:57 +00001147//------------------------------------------------------------------------
1148// Function SetOperandsForMemInstr
1149//
1150// Choose addressing mode for the given load or store instruction.
1151// Use [reg+reg] if it is an indexed reference, and the index offset is
1152// not a constant or if it cannot fit in the offset field.
1153// Use [reg+offset] in all other cases.
1154//
1155// This assumes that all array refs are "lowered" to one of these forms:
1156// %x = load (subarray*) ptr, constant ; single constant offset
1157// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1158// Generally, this should happen via strength reduction + LICM.
1159// Also, strength reduction should take care of using the same register for
1160// the loop index variable and an array index, when that is profitable.
1161//------------------------------------------------------------------------
1162
1163static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001164SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001165 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001166 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001167 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001168{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001169 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001170 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001171 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001172 bool allConstantIndices;
1173 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001174
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001175 // Now create the appropriate operands for the machine instruction.
1176 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001177 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001178 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001179 MachineOperand::MachineOperandType offsetOpType =
1180 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001181
Vikram S. Adve74825322002-03-18 03:15:35 +00001182 // Check if there is an index vector and if so, compute the
1183 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001184 //
Misha Brukman7b647942003-05-30 20:11:56 +00001185 if (!idxVec.empty()) {
1186 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001187
Misha Brukman7b647942003-05-30 20:11:56 +00001188 // If all indices are constant, compute the combined offset directly.
1189 if (allConstantIndices) {
1190 // Compute the offset value using the index vector. Create a
1191 // virtual reg. for it since it may not fit in the immed field.
1192 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
1193 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
1194 } else {
1195 // There is at least one non-constant offset. Therefore, this must
1196 // be an array ref, and must have been lowered to a single non-zero
1197 // offset. (An extra leading zero offset, if any, can be ignored.)
1198 // Generate code sequence to compute address from index.
1199 //
1200 bool firstIdxIsZero = IsZero(idxVec[0]);
1201 assert(idxVec.size() == 1U + firstIdxIsZero
1202 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001203
Misha Brukman7b647942003-05-30 20:11:56 +00001204 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001205
Misha Brukman7b647942003-05-30 20:11:56 +00001206 std::vector<MachineInstr*> mulVec;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001207 Instruction* addr =
1208 new TmpInstruction(MachineCodeForInstruction::get(memInst),
1209 Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001210
Misha Brukman7b647942003-05-30 20:11:56 +00001211 // Get the array type indexed by idxVal, and compute its element size.
1212 // The call to getTypeSize() will fail if size is not constant.
1213 const Type* vecType = (firstIdxIsZero
1214 ? GetElementPtrInst::getIndexedType(ptrType,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001215 std::vector<Value*>(1U, idxVec[0]),
1216 /*AllowCompositeLeaf*/ true)
1217 : ptrType);
Misha Brukman7b647942003-05-30 20:11:56 +00001218 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1219 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
1220 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001221
Misha Brukman7b647942003-05-30 20:11:56 +00001222 // CreateMulInstruction() folds constants intelligently enough.
1223 CreateMulInstruction(target, memInst->getParent()->getParent(),
1224 idxVal, /* lval, not likely to be const*/
1225 eltSizeVal, /* rval, likely to be constant */
1226 addr, /* result */
1227 mulVec, MachineCodeForInstruction::get(memInst),
1228 INVALID_MACHINE_OPCODE);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001229
Misha Brukman7b647942003-05-30 20:11:56 +00001230 assert(mulVec.size() > 0 && "No multiply code created?");
1231 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
1232
1233 valueForRegOffset = addr;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001234 }
Misha Brukman7b647942003-05-30 20:11:56 +00001235 } else {
1236 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1237 smallConstOffset = 0;
1238 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001239
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001240 // For STORE:
1241 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1242 // For LOAD or GET_ELEMENT_PTR,
1243 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1244 //
1245 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001246 MachineInstr *MI;
1247 if (memInst->getOpcode() == Instruction::Store) {
Misha Brukman7b647942003-05-30 20:11:56 +00001248 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001249 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1250 .addReg(ptrVal).addReg(valueForRegOffset);
Misha Brukman7b647942003-05-30 20:11:56 +00001251 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001252 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001253 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1254 .addReg(ptrVal).addSImm(smallConstOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001255 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001256 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00001257 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001258 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1259 .addRegDef(memInst);
Misha Brukman7b647942003-05-30 20:11:56 +00001260 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001261 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001262 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1263 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001264 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001265 }
1266 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001267}
1268
1269
Chris Lattner20b1ea02001-09-14 03:47:57 +00001270//
1271// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001272// in place of the use(s) of that instruction in node `parent'.
1273// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001274// Also make sure to skip over a parent who:
1275// (1) is a list node in the Burg tree, or
1276// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001277//
1278static void
1279ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001280 InstrTreeNode* parent,
1281 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001282{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001283 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1284
Chris Lattner20b1ea02001-09-14 03:47:57 +00001285 Instruction* unusedOp = treeNode->getInstruction();
1286 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001287
1288 // The parent itself may be a list node, so find the real parent instruction
1289 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1290 {
1291 parent = parent->parent();
1292 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1293 }
1294 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1295
1296 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001297 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001298
1299 // The parent's mvec would be empty if it was itself forwarded.
1300 // Recursively call ForwardOperand in that case...
1301 //
Misha Brukman7b647942003-05-30 20:11:56 +00001302 if (mvec.size() == 0) {
1303 assert(parent->parent() != NULL &&
1304 "Parent could not have been forwarded, yet has no instructions?");
1305 ForwardOperand(treeNode, parent->parent(), operandNum);
1306 } else {
1307 for (unsigned i=0, N=mvec.size(); i < N; i++) {
1308 MachineInstr* minstr = mvec[i];
1309 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) {
1310 const MachineOperand& mop = minstr->getOperand(i);
1311 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
1312 mop.getVRegValue() == unusedOp)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001313 {
Misha Brukman7b647942003-05-30 20:11:56 +00001314 minstr->SetMachineOperandVal(i, MachineOperand::MO_VirtualRegister,
1315 fwdOp);
1316 }
1317 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001318
Misha Brukman7b647942003-05-30 20:11:56 +00001319 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1320 if (minstr->getImplicitRef(i) == unusedOp) {
1321 minstr->setImplicitRef(i, fwdOp,
1322 minstr->getImplicitOp(i).opIsDefOnly(),
1323 minstr->getImplicitOp(i).opIsDefAndUse());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001324 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001325 }
Misha Brukman7b647942003-05-30 20:11:56 +00001326 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001327}
1328
1329
Vikram S. Adve242a8082002-05-19 15:25:51 +00001330inline bool
1331AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001332{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001333 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1334 UI != UE; ++UI)
1335 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1336 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1337 return false;
1338 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001339}
1340
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001341// Generate code for any intrinsic that needs a special code sequence
1342// instead of a regular call. If not that kind of intrinsic, do nothing.
1343// Returns true if code was generated, otherwise false.
1344//
1345bool CodeGenIntrinsic(LLVMIntrinsic::ID iid, CallInst &callInstr,
1346 TargetMachine &target,
1347 std::vector<MachineInstr*>& mvec)
1348{
1349 switch (iid) {
1350 case LLVMIntrinsic::va_start: {
1351 // Get the address of the first vararg value on stack and copy it to
1352 // the argument of va_start(va_list* ap).
1353 bool ignore;
1354 Function* func = cast<Function>(callInstr.getParent()->getParent());
1355 int numFixedArgs = func->getFunctionType()->getNumParams();
1356 int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum();
1357 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
1358 int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo().
1359 getFirstIncomingArgOffset(MachineFunction::get(func), ignore);
Misha Brukman91aee472003-05-27 22:37:00 +00001360 mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001361 addReg(callInstr.getOperand(1)));
1362 return true;
1363 }
1364
1365 case LLVMIntrinsic::va_end:
1366 return true; // no-op on Sparc
1367
1368 case LLVMIntrinsic::va_copy:
1369 // Simple copy of current va_list (arg2) to new va_list (arg1)
Misha Brukman91aee472003-05-27 22:37:00 +00001370 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001371 addMReg(target.getRegInfo().getZeroRegNum()).
1372 addReg(callInstr.getOperand(2)).
1373 addReg(callInstr.getOperand(1)));
1374 return true;
1375
1376 default:
1377 return false;
1378 }
1379}
1380
Vikram S. Advefb361122001-10-22 13:36:31 +00001381//******************* Externally Visible Functions *************************/
1382
Vikram S. Advefb361122001-10-22 13:36:31 +00001383//------------------------------------------------------------------------
1384// External Function: ThisIsAChainRule
1385//
1386// Purpose:
1387// Check if a given BURG rule is a chain rule.
1388//------------------------------------------------------------------------
1389
1390extern bool
1391ThisIsAChainRule(int eruleno)
1392{
1393 switch(eruleno)
1394 {
1395 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001396 case 123:
1397 case 124:
1398 case 125:
1399 case 126:
1400 case 127:
1401 case 128:
1402 case 129:
1403 case 130:
1404 case 131:
1405 case 132:
1406 case 133:
1407 case 155:
1408 case 221:
1409 case 222:
1410 case 241:
1411 case 242:
1412 case 243:
1413 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001414 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001415 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001416 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001417
Vikram S. Advefb361122001-10-22 13:36:31 +00001418 default:
1419 return false; break;
1420 }
1421}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001422
1423
1424//------------------------------------------------------------------------
1425// External Function: GetInstructionsByRule
1426//
1427// Purpose:
1428// Choose machine instructions for the SPARC according to the
1429// patterns chosen by the BURG-generated parser.
1430//------------------------------------------------------------------------
1431
Vikram S. Adve74825322002-03-18 03:15:35 +00001432void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001433GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001434 int ruleForNode,
1435 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001436 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001437 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001438{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001439 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001440 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001441 int nextRule;
1442 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001443 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001444 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001445 unsigned L;
Vikram S. Adve74825322002-03-18 03:15:35 +00001446
1447 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001448
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001449 // If the code for this instruction was folded into the parent (user),
1450 // then do nothing!
1451 if (subtreeRoot->isFoldedIntoParent())
1452 return;
1453
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001454 //
1455 // Let's check for chain rules outside the switch so that we don't have
1456 // to duplicate the list of chain rule production numbers here again
1457 //
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001458 if (ThisIsAChainRule(ruleForNode))
1459 {
1460 // Chain rules have a single nonterminal on the RHS.
1461 // Get the rule that matches the RHS non-terminal and use that instead.
1462 //
1463 assert(nts[0] && ! nts[1]
1464 && "A chain rule should have only one RHS non-terminal!");
1465 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1466 nts = burm_nts[nextRule];
1467 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
1468 }
1469 else
1470 {
1471 switch(ruleForNode) {
1472 case 1: // stmt: Ret
1473 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001474 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001475 // for moving return value to appropriate register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001476 // Copy the return value to the required return register.
1477 // Mark the return Value as an implicit ref of the RET instr..
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001478 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001479 // Finally put a NOP in the delay slot.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001480 ReturnInst *returnInstr=cast<ReturnInst>(subtreeRoot->getInstruction());
1481 Value* retVal = returnInstr->getReturnValue();
1482 MachineCodeForInstruction& mcfi =
1483 MachineCodeForInstruction::get(returnInstr);
1484
1485 // Create a hidden virtual reg to represent the return address register
1486 // used by the machine instruction but not represented in LLVM.
1487 //
1488 Instruction* returnAddrTmp = new TmpInstruction(mcfi, returnInstr);
1489
1490 MachineInstr* retMI =
1491 BuildMI(V9::JMPLRETi, 3).addReg(returnAddrTmp).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001492 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001493
1494 // Insert a copy to copy the return value to the appropriate register
1495 // -- For FP values, create a FMOVS or FMOVD instruction
1496 // -- For non-FP values, create an add-with-0 instruction
1497 //
1498 if (retVal != NULL) {
1499 const UltraSparcRegInfo& regInfo =
1500 (UltraSparcRegInfo&) target.getRegInfo();
1501 const Type* retType = retVal->getType();
1502 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
1503 unsigned retRegNum = (retType->isFloatingPoint()
1504 ? (unsigned) SparcFloatRegClass::f0
1505 : (unsigned) SparcIntRegClass::i0);
1506 retRegNum = regInfo.getUnifiedRegNum(regClassID, retRegNum);
1507
1508 // Create a virtual register to represent it and mark
1509 // this vreg as being an implicit operand of the ret MI
1510 TmpInstruction* retVReg =
1511 new TmpInstruction(mcfi, retVal, NULL, "argReg");
1512
1513 retMI->addImplicitRef(retVReg);
1514
1515 if (retType->isFloatingPoint())
1516 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
1517 .addReg(retVal).addReg(retVReg, MOTy::Def));
1518 else
1519 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
1520 .addReg(retVal).addSImm((int64_t) 0)
1521 .addReg(retVReg, MOTy::Def));
1522
1523 // Mark the operand with the register it should be assigned
1524 M->SetRegForOperand(M->getNumOperands()-1, retRegNum);
1525 retMI->SetRegForImplicitRef(retMI->getNumImplicitRefs()-1, retRegNum);
1526
1527 mvec.push_back(M);
1528 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001529
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001530 // Now insert the RET instruction and a NOP for the delay slot
1531 mvec.push_back(retMI);
Misha Brukmana98cd452003-05-20 20:32:24 +00001532 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001533
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001534 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001535 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001536
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001537 case 3: // stmt: Store(reg,reg)
1538 case 4: // stmt: Store(reg,ptrreg)
1539 SetOperandsForMemInstr(ChooseStoreInstruction(
Chris Lattner54e898e2003-01-15 19:23:34 +00001540 subtreeRoot->leftChild()->getValue()->getType()),
1541 mvec, subtreeRoot, target);
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001542 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001543
1544 case 5: // stmt: BrUncond
1545 {
1546 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1547 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
1548
1549 // delay slot
1550 mvec.push_back(BuildMI(V9::NOP, 0));
1551 break;
1552 }
1553
1554 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001555 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001556 // If the constant is ZERO, we can use the branch-on-integer-register
1557 // instructions and avoid the SUBcc instruction entirely.
1558 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001559 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001560 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1561 assert(constNode &&
1562 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001563 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001564 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001565
Chris Lattner0c4e8862002-09-03 01:08:28 +00001566 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001567 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001568 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1569 && isValidConst)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001570 {
1571 // That constant is a zero after all...
1572 // Use the left child of setCC as the first argument!
1573 // Mark the setCC node so that no code is generated for it.
1574 InstructionNode* setCCNode = (InstructionNode*)
1575 subtreeRoot->leftChild();
1576 assert(setCCNode->getOpLabel() == SetCCOp);
1577 setCCNode->markFoldedIntoParent();
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001578
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001579 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001580
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001581 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1582 .addReg(setCCNode->leftChild()->getValue())
1583 .addPCDisp(brInst->getSuccessor(0));
1584 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001585
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001586 // delay slot
1587 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001588
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001589 // false branch
1590 mvec.push_back(BuildMI(V9::BA, 1)
1591 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001592
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001593 // delay slot
1594 mvec.push_back(BuildMI(V9::NOP, 0));
1595 break;
1596 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001597 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001598 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001599
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001600 case 6: // stmt: BrCond(setCC)
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001601 { // bool => boolean was computed with SetCC.
1602 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001603 // If it is an integer CC, we also need to find the unique
1604 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001605 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001606 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001607 bool isFPBranch;
Chris Lattner54e898e2003-01-15 19:23:34 +00001608 unsigned Opcode = ChooseBccInstruction(subtreeRoot, isFPBranch);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001609 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1610 brInst->getParent()->getParent(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001611 isFPBranch? Type::FloatTy : Type::IntTy,
1612 MachineCodeForInstruction::get(brInst));
Chris Lattner54e898e2003-01-15 19:23:34 +00001613 M = BuildMI(Opcode, 2).addCCReg(ccValue)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001614 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001615 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001616
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001617 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001618 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001619
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001620 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001621 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001622
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001623 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001624 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001625 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001626 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001627
1628 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001629 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001630 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001631 Constant* constVal =
1632 cast<Constant>(subtreeRoot->leftChild()->getValue());
1633 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001634
Misha Brukmana98cd452003-05-20 20:32:24 +00001635 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001636 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001637 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001638
1639 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001640 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001641 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001642 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001643
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001644 case 8: // stmt: BrCond(boolreg)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001645 { // boolreg => boolean is recorded in an integer register.
1646 // Use branch-on-integer-register instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001647 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001648 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001649 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001650 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001651 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001652
1653 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001654 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001655
1656 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001657 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001658
1659 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001660 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001661 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001662 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001663
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001664 case 9: // stmt: Switch(reg)
1665 assert(0 && "*** SWITCH instruction is not implemented yet.");
1666 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001667
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001668 case 10: // reg: VRegList(reg, reg)
1669 assert(0 && "VRegList should never be the topmost non-chain rule");
1670 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001671
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001672 case 21: // bool: Not(bool,reg): Both these are implemented as:
1673 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001674 { // First find the unary operand. It may be left or right, usually right.
1675 Value* notArg = BinaryOperator::getNotArgument(
1676 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001677 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001678 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001679 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001680 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001681 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001682
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001683 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001684 {
1685 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001686 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001687 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001688 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001689 }
1690
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001691 case 23: // reg: ToUByteTy(reg)
1692 case 24: // reg: ToSByteTy(reg)
1693 case 25: // reg: ToUShortTy(reg)
1694 case 26: // reg: ToShortTy(reg)
1695 case 27: // reg: ToUIntTy(reg)
1696 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001697 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001698 //======================================================================
1699 // Rules for integer conversions:
1700 //
1701 //--------
1702 // From ISO 1998 C++ Standard, Sec. 4.7:
1703 //
1704 // 2. If the destination type is unsigned, the resulting value is
1705 // the least unsigned integer congruent to the source integer
1706 // (modulo 2n where n is the number of bits used to represent the
1707 // unsigned type). [Note: In a two s complement representation,
1708 // this conversion is conceptual and there is no change in the
1709 // bit pattern (if there is no truncation). ]
1710 //
1711 // 3. If the destination type is signed, the value is unchanged if
1712 // it can be represented in the destination type (and bitfield width);
1713 // otherwise, the value is implementation-defined.
1714 //--------
1715 //
1716 // Since we assume 2s complement representations, this implies:
1717 //
1718 // -- if operand is smaller than destination, zero-extend or sign-extend
1719 // according to the signedness of the *operand*: source decides.
1720 // ==> we have to do nothing here!
1721 //
1722 // -- if operand is same size as or larger than destination, and the
1723 // destination is *unsigned*, zero-extend the operand: dest. decides
1724 //
1725 // -- if operand is same size as or larger than destination, and the
1726 // destination is *signed*, the choice is implementation defined:
1727 // we sign-extend the operand: i.e., again dest. decides.
1728 // Note: this matches both Sun's cc and gcc3.2.
1729 //======================================================================
1730
Vikram S. Adve242a8082002-05-19 15:25:51 +00001731 Instruction* destI = subtreeRoot->getInstruction();
1732 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001733 const Type* opType = opVal->getType();
Misha Brukman7b647942003-05-30 20:11:56 +00001734 if (opType->isIntegral() || isa<PointerType>(opType)) {
1735 unsigned opSize = target.getTargetData().getTypeSize(opType);
1736 unsigned destSize =
1737 target.getTargetData().getTypeSize(destI->getType());
1738 if (opSize >= destSize) {
1739 // Operand is same size as or larger than dest:
1740 // zero- or sign-extend, according to the signeddness of
1741 // the destination (see above).
1742 if (destI->getType()->isSigned())
1743 target.getInstrInfo().CreateSignExtensionInstructions(target,
Vikram S. Adve94c40812002-09-27 14:33:08 +00001744 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1745 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001746 else
Misha Brukman7b647942003-05-30 20:11:56 +00001747 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1748 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1749 mvec, MachineCodeForInstruction::get(destI));
1750 } else
1751 forwardOperandNum = 0; // forward first operand to user
1752 } else if (opType->isFloatingPoint()) {
1753 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1754 MachineCodeForInstruction::get(destI));
1755 if (destI->getType()->isUnsigned())
1756 maskUnsignedResult = true; // not handled by fp->int code
1757 } else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001758 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1759
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001760 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001761 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001762
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001763 case 29: // reg: ToULongTy(reg)
1764 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001765 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001766 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001767 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001768 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001769 forwardOperandNum = 0; // forward first operand to user
Misha Brukman7b647942003-05-30 20:11:56 +00001770 else if (opType->isFloatingPoint()) {
1771 Instruction* destI = subtreeRoot->getInstruction();
1772 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1773 MachineCodeForInstruction::get(destI));
1774 } else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001775 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001776 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001777 }
1778
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001779 case 31: // reg: ToFloatTy(reg):
1780 case 32: // reg: ToDoubleTy(reg):
1781 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001782
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001783 // If this instruction has a parent (a user) in the tree
1784 // and the user is translated as an FsMULd instruction,
1785 // then the cast is unnecessary. So check that first.
1786 // In the future, we'll want to do the same for the FdMULq instruction,
1787 // so do the check here instead of only for ToFloatTy(reg).
1788 //
1789 if (subtreeRoot->parent() != NULL) {
1790 const MachineCodeForInstruction& mcfi =
1791 MachineCodeForInstruction::get(
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001792 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001793 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
1794 forwardOperandNum = 0; // forward first operand to user
1795 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001796
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001797 if (forwardOperandNum != 0) { // we do need the cast
1798 Value* leftVal = subtreeRoot->leftChild()->getValue();
1799 const Type* opType = leftVal->getType();
1800 MachineOpCode opCode=ChooseConvertToFloatInstr(
1801 subtreeRoot->getOpLabel(), opType);
1802 if (opCode == V9::INVALID_OPCODE) { // no conversion needed
1803 forwardOperandNum = 0; // forward first operand to user
1804 } else {
1805 // If the source operand is a non-FP type it must be
1806 // first copied from int to float register via memory!
1807 Instruction *dest = subtreeRoot->getInstruction();
1808 Value* srcForCast;
1809 int n = 0;
1810 if (! opType->isFloatingPoint()) {
1811 // Create a temporary to represent the FP register
1812 // into which the integer will be copied via memory.
1813 // The type of this temporary will determine the FP
1814 // register used: single-prec for a 32-bit int or smaller,
1815 // double-prec for a 64-bit int.
1816 //
1817 uint64_t srcSize =
1818 target.getTargetData().getTypeSize(leftVal->getType());
1819 Type* tmpTypeToUse =
1820 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001821 MachineCodeForInstruction &destMCFI =
1822 MachineCodeForInstruction::get(dest);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001823 srcForCast = new TmpInstruction(destMCFI, tmpTypeToUse, dest);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001824
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001825 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001826 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001827 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001828 mvec, destMCFI);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001829 } else
1830 srcForCast = leftVal;
1831
1832 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
1833 mvec.push_back(M);
1834 }
Misha Brukman7b647942003-05-30 20:11:56 +00001835 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001836 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001837
1838 case 19: // reg: ToArrayTy(reg):
1839 case 20: // reg: ToPointerTy(reg):
1840 forwardOperandNum = 0; // forward first operand to user
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001841 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001842
1843 case 233: // reg: Add(reg, Constant)
1844 maskUnsignedResult = true;
1845 M = CreateAddConstInstruction(subtreeRoot);
1846 if (M != NULL) {
1847 mvec.push_back(M);
1848 break;
1849 }
1850 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001851
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001852 case 33: // reg: Add(reg, reg)
1853 maskUnsignedResult = true;
1854 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
1855 break;
1856
1857 case 234: // reg: Sub(reg, Constant)
1858 maskUnsignedResult = true;
1859 M = CreateSubConstInstruction(subtreeRoot);
1860 if (M != NULL) {
1861 mvec.push_back(M);
1862 break;
1863 }
1864 // ELSE FALL THROUGH
1865
1866 case 34: // reg: Sub(reg, reg)
1867 maskUnsignedResult = true;
1868 Add3OperandInstr(ChooseSubInstructionByType(
Chris Lattner54e898e2003-01-15 19:23:34 +00001869 subtreeRoot->getInstruction()->getType()),
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001870 subtreeRoot, mvec);
1871 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001872
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001873 case 135: // reg: Mul(todouble, todouble)
1874 checkCast = true;
1875 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001876
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001877 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001878 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001879 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001880 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00001881 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00001882 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001883 Instruction* mulInstr = subtreeRoot->getInstruction();
1884 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001885 subtreeRoot->leftChild()->getValue(),
1886 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001887 mulInstr, mvec,
1888 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001889 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001890 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001891 case 335: // reg: Mul(todouble, todoubleConst)
1892 checkCast = true;
1893 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001894
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001895 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001896 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001897 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001898 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00001899 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00001900 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001901 Instruction* mulInstr = subtreeRoot->getInstruction();
1902 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001903 subtreeRoot->leftChild()->getValue(),
1904 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001905 mulInstr, mvec,
1906 MachineCodeForInstruction::get(mulInstr),
1907 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001908 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001909 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001910 case 236: // reg: Div(reg, Constant)
1911 maskUnsignedResult = true;
1912 L = mvec.size();
1913 CreateDivConstInstruction(target, subtreeRoot, mvec);
1914 if (mvec.size() > L)
1915 break;
1916 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001917
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001918 case 36: // reg: Div(reg, reg)
1919 maskUnsignedResult = true;
1920 Add3OperandInstr(ChooseDivInstruction(target, subtreeRoot),
1921 subtreeRoot, mvec);
1922 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001923
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001924 case 37: // reg: Rem(reg, reg)
1925 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001926 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001927 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001928 Instruction* remInstr = subtreeRoot->getInstruction();
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001929
1930 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(remInstr);
1931 TmpInstruction* quot = new TmpInstruction(mcfi,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001932 subtreeRoot->leftChild()->getValue(),
1933 subtreeRoot->rightChild()->getValue());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001934 TmpInstruction* prod = new TmpInstruction(mcfi,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001935 quot,
1936 subtreeRoot->rightChild()->getValue());
Vikram S. Adve510eec72001-11-04 21:59:14 +00001937
Chris Lattner54e898e2003-01-15 19:23:34 +00001938 M = BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
1939 .addReg(subtreeRoot->leftChild()->getValue())
1940 .addReg(subtreeRoot->rightChild()->getValue())
1941 .addRegDef(quot);
Vikram S. Adve74825322002-03-18 03:15:35 +00001942 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001943
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001944 unsigned MulOpcode =
1945 ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType());
1946 Value *MulRHS = subtreeRoot->rightChild()->getValue();
1947 M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod,
1948 MOTy::Def);
Vikram S. Adve74825322002-03-18 03:15:35 +00001949 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001950
Chris Lattner54e898e2003-01-15 19:23:34 +00001951 unsigned Opcode = ChooseSubInstructionByType(
1952 subtreeRoot->getInstruction()->getType());
1953 M = BuildMI(Opcode, 3).addReg(subtreeRoot->leftChild()->getValue())
1954 .addReg(prod).addRegDef(subtreeRoot->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001955 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001956 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001957 }
1958
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001959 case 38: // bool: And(bool, bool)
1960 case 238: // bool: And(bool, boolconst)
1961 case 338: // reg : BAnd(reg, reg)
1962 case 538: // reg : BAnd(reg, Constant)
1963 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
1964 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001965
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001966 case 138: // bool: And(bool, not)
1967 case 438: // bool: BAnd(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001968 { // Use the argument of NOT as the second argument!
1969 // Mark the NOT node so that no code is generated for it.
1970 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1971 Value* notArg = BinaryOperator::getNotArgument(
1972 cast<BinaryOperator>(notNode->getInstruction()));
1973 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001974 Value *LHS = subtreeRoot->leftChild()->getValue();
1975 Value *Dest = subtreeRoot->getValue();
Misha Brukman91aee472003-05-27 22:37:00 +00001976 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(LHS).addReg(notArg)
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001977 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001978 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001979 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001980
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001981 case 39: // bool: Or(bool, bool)
1982 case 239: // bool: Or(bool, boolconst)
1983 case 339: // reg : BOr(reg, reg)
1984 case 539: // reg : BOr(reg, Constant)
1985 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
1986 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001987
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001988 case 139: // bool: Or(bool, not)
1989 case 439: // bool: BOr(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001990 { // Use the argument of NOT as the second argument!
1991 // Mark the NOT node so that no code is generated for it.
1992 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1993 Value* notArg = BinaryOperator::getNotArgument(
1994 cast<BinaryOperator>(notNode->getInstruction()));
1995 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001996 Value *LHS = subtreeRoot->leftChild()->getValue();
1997 Value *Dest = subtreeRoot->getValue();
Misha Brukman91aee472003-05-27 22:37:00 +00001998 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(LHS).addReg(notArg)
Misha Brukmana98cd452003-05-20 20:32:24 +00001999 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002000 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002001 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002002
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002003 case 40: // bool: Xor(bool, bool)
2004 case 240: // bool: Xor(bool, boolconst)
2005 case 340: // reg : BXor(reg, reg)
2006 case 540: // reg : BXor(reg, Constant)
2007 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
2008 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002009
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002010 case 140: // bool: Xor(bool, not)
2011 case 440: // bool: BXor(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002012 { // Use the argument of NOT as the second argument!
2013 // Mark the NOT node so that no code is generated for it.
2014 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2015 Value* notArg = BinaryOperator::getNotArgument(
2016 cast<BinaryOperator>(notNode->getInstruction()));
2017 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002018 Value *LHS = subtreeRoot->leftChild()->getValue();
2019 Value *Dest = subtreeRoot->getValue();
Misha Brukman91aee472003-05-27 22:37:00 +00002020 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(LHS).addReg(notArg)
Misha Brukmana98cd452003-05-20 20:32:24 +00002021 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002022 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002023 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002024
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002025 case 41: // boolconst: SetCC(reg, Constant)
2026 //
2027 // If the SetCC was folded into the user (parent), it will be
2028 // caught above. All other cases are the same as case 42,
2029 // so just fall through.
2030 //
2031 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002032 {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002033 // This generates a SUBCC instruction, putting the difference in a
2034 // result reg. if needed, and/or setting a condition code if needed.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002035 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002036 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002037 Value* leftVal = subtreeRoot->leftChild()->getValue();
2038 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002039
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002040 // If the boolean result of the SetCC is used outside the current basic
2041 // block (so it must be computed as a boolreg) or is used by anything
2042 // other than a branch, the boolean must be computed and stored
2043 // in a result register. We will use a conditional move to do this.
2044 //
2045 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2046 ! AllUsesAreBranches(setCCInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002047
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002048 // A TmpInstruction is created to represent the CC "result".
2049 // Unlike other instances of TmpInstruction, this one is used
2050 // by machine code of multiple LLVM instructions, viz.,
2051 // the SetCC and the branch. Make sure to get the same one!
2052 // Note that we do this even for FP CC registers even though they
2053 // are explicit operands, because the type of the operand
2054 // needs to be a floating point condition code, not an integer
2055 // condition code. Think of this as casting the bool result to
2056 // a FP condition code register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002057 // Later, we mark the 4th operand as being a CC register, and as a def.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002058 //
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002059 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002060 setCCInstr->getParent()->getParent(),
2061 isFPCompare ? Type::FloatTy : Type::IntTy,
2062 MachineCodeForInstruction::get(setCCInstr));
Misha Brukman7b647942003-05-30 20:11:56 +00002063 if (! isFPCompare) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002064 // Integer condition: set CC and discard result.
2065 M = BuildMI(V9::SUBccr, 4)
2066 .addReg(subtreeRoot->leftChild()->getValue())
2067 .addReg(subtreeRoot->rightChild()->getValue())
2068 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def)
2069 .addCCReg(tmpForCC, MOTy::Def);
Misha Brukman7b647942003-05-30 20:11:56 +00002070 } else {
2071 // FP condition: dest of FCMP should be some FCCn register
2072 M = BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2073 .addCCReg(tmpForCC, MOTy::Def)
2074 .addReg(subtreeRoot->leftChild()->getValue())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002075 .addReg(subtreeRoot->rightChild()->getValue());
Misha Brukman7b647942003-05-30 20:11:56 +00002076 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002077 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002078
Misha Brukman7b647942003-05-30 20:11:56 +00002079 if (computeBoolVal) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002080 MachineOpCode movOpCode = (isFPCompare
Misha Brukmaneecdb662003-06-02 20:55:14 +00002081 ? ChooseMovFpcciInstruction(subtreeRoot)
2082 : ChooseMovpcciAfterSub(subtreeRoot));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002083
2084 // Unconditionally set register to 0
2085 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
2086 mvec.push_back(M);
2087
2088 // Now conditionally move 1 into the register.
Misha Brukman7b647942003-05-30 20:11:56 +00002089 // Mark the register as a use (as well as a def) because the old
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002090 // value will be retained if the condition is false.
2091 M = (BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(1)
2092 .addReg(setCCInstr, MOTy::UseAndDef));
Misha Brukman7b647942003-05-30 20:11:56 +00002093 mvec.push_back(M);
2094 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002095 break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002096 }
2097
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002098 case 51: // reg: Load(reg)
2099 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002100 SetOperandsForMemInstr(ChooseLoadInstruction(
2101 subtreeRoot->getValue()->getType()),
2102 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002103 break;
2104
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002105 case 55: // reg: GetElemPtr(reg)
2106 case 56: // reg: GetElemPtrIdx(reg,reg)
2107 // If the GetElemPtr was folded into the user (parent), it will be
2108 // caught above. For other cases, we have to compute the address.
2109 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
2110 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002111
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002112 case 57: // reg: Alloca: Implement as 1 instruction:
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002113 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002114 AllocationInst* instr =
2115 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002116 unsigned tsize =
2117 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002118 assert(tsize != 0);
2119 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002120 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002121 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002122
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002123 case 58: // reg: Alloca(reg): Implement as 3 instructions:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002124 // mul num, typeSz -> tmp
2125 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002126 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002127 AllocationInst* instr =
2128 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002129 const Type* eltType = instr->getAllocatedType();
2130
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002131 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002132 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002133 Value* numElementsVal = NULL;
2134 bool isArray = instr->isArrayAllocation();
2135
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002136 if (!isArray || isa<Constant>(numElementsVal = instr->getArraySize())) {
Misha Brukman7b647942003-05-30 20:11:56 +00002137 // total size is constant: generate code for fixed-size alloca
2138 unsigned numElements = isArray?
2139 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2140 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2141 numElements, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002142 } else {
2143 // total size is not constant.
Vikram S. Adve74825322002-03-18 03:15:35 +00002144 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002145 numElementsVal, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002146 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002147 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002148 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002149
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002150 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002151 { // Generate a direct (CALL) or indirect (JMPL) call.
2152 // Mark the return-address register, the indirection
2153 // register (for indirect calls), the operands of the Call,
2154 // and the return value (if any) as implicit operands
2155 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002156 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002157 // If this is a varargs function, floating point arguments
2158 // have to passed in integer registers so insert
2159 // copy-float-to-int instructions for each float operand.
2160 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002161 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002162 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002163 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002164
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002165 // Check if this is an intrinsic function that needs a special code
2166 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002167 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002168 bool specialIntrinsic = false;
2169 LLVMIntrinsic::ID iid;
2170 if (calledFunc && (iid=(LLVMIntrinsic::ID)calledFunc->getIntrinsicID()))
2171 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002172
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002173 // If not, generate the normal call sequence for the function.
2174 // This can also handle any intrinsics that are just function calls.
2175 //
Misha Brukman7b647942003-05-30 20:11:56 +00002176 if (! specialIntrinsic) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002177 MachineFunction& MF =
2178 MachineFunction::get(callInstr->getParent()->getParent());
2179 MachineCodeForInstruction& mcfi =
2180 MachineCodeForInstruction::get(callInstr);
2181 const UltraSparcRegInfo& regInfo =
2182 (UltraSparcRegInfo&) target.getRegInfo();
2183 const TargetFrameInfo& frameInfo = target.getFrameInfo();
2184
Misha Brukman7b647942003-05-30 20:11:56 +00002185 // Create hidden virtual register for return address with type void*
2186 TmpInstruction* retAddrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002187 new TmpInstruction(mcfi, PointerType::get(Type::VoidTy), callInstr);
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002188
Misha Brukman7b647942003-05-30 20:11:56 +00002189 // Generate the machine instruction and its operands.
2190 // Use CALL for direct function calls; this optimistically assumes
2191 // the PC-relative address fits in the CALL address field (22 bits).
2192 // Use JMPL for indirect calls.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002193 // This will be added to mvec later, after operand copies.
Misha Brukman7b647942003-05-30 20:11:56 +00002194 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002195 MachineInstr* callMI;
Misha Brukman7b647942003-05-30 20:11:56 +00002196 if (calledFunc) // direct function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002197 callMI = BuildMI(V9::CALL, 1).addPCDisp(callee);
Misha Brukman7b647942003-05-30 20:11:56 +00002198 else // indirect function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002199 callMI = (BuildMI(V9::JMPLCALLi,3).addReg(callee)
2200 .addSImm((int64_t)0).addRegDef(retAddrReg));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002201
Misha Brukman7b647942003-05-30 20:11:56 +00002202 const FunctionType* funcType =
2203 cast<FunctionType>(cast<PointerType>(callee->getType())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002204 ->getElementType());
Misha Brukman7b647942003-05-30 20:11:56 +00002205 bool isVarArgs = funcType->isVarArg();
2206 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002207
Misha Brukman7b647942003-05-30 20:11:56 +00002208 // Use a descriptor to pass information about call arguments
2209 // to the register allocator. This descriptor will be "owned"
2210 // and freed automatically when the MachineCodeForInstruction
2211 // object for the callInstr goes away.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002212 CallArgsDescriptor* argDesc =
2213 new CallArgsDescriptor(callInstr, retAddrReg,isVarArgs,noPrototype);
Misha Brukman7b647942003-05-30 20:11:56 +00002214 assert(callInstr->getOperand(0) == callee
2215 && "This is assumed in the loop below!");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002216
2217 // Insert copy instructions to get all the arguments into
2218 // all the places that they need to be.
2219 //
Misha Brukman7b647942003-05-30 20:11:56 +00002220 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002221 int argNo = i-1;
Misha Brukman7b647942003-05-30 20:11:56 +00002222 Value* argVal = callInstr->getOperand(i);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002223 const Type* argType = argVal->getType();
2224 unsigned regType = regInfo.getRegType(argType);
2225 unsigned argSize = target.getTargetData().getTypeSize(argType);
2226 int regNumForArg = TargetRegInfo::getInvalidRegNum();
2227 unsigned regClassIDOfArgReg;
2228 CallArgInfo& argInfo = argDesc->getArgInfo(argNo);
2229
Misha Brukman7b647942003-05-30 20:11:56 +00002230 // Check for FP arguments to varargs functions.
2231 // Any such argument in the first $K$ args must be passed in an
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002232 // integer register. If there is no prototype, it must also
2233 // be passed as an FP register.
2234 // K = #integer argument registers.
2235 bool isFPArg = argVal->getType()->isFloatingPoint();
2236 if (isVarArgs && isFPArg) {
Misha Brukman7b647942003-05-30 20:11:56 +00002237 // If it is a function with no prototype, pass value
2238 // as an FP value as well as a varargs value
2239 if (noPrototype)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002240 argInfo.setUseFPArgReg();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002241
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002242 // If this arg. is in the first $K$ regs, add copy-
2243 // float-to-int instructions to pass the value as an int.
2244 // To check if it is in teh first $K$, get the register
2245 // number for the arg #i.
Misha Brukmanea481cc2003-06-03 03:21:58 +00002246 int copyRegNum = regInfo.regNumForIntArg(false, false, argNo,
2247 regClassIDOfArgReg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002248 if (copyRegNum != regInfo.getInvalidRegNum()) {
2249 // Create a virtual register to represent copyReg. Mark
2250 // this vreg as being an implicit operand of the call MI
2251 const Type* loadTy = (argType == Type::FloatTy
2252 ? Type::IntTy : Type::LongTy);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002253 TmpInstruction* argVReg = new TmpInstruction(mcfi, loadTy,
2254 argVal, NULL,
2255 "argRegCopy");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002256 callMI->addImplicitRef(argVReg);
2257
2258 // Get a temp stack location to use to copy
2259 // float-to-int via the stack.
2260 //
2261 // FIXME: For now, we allocate permanent space because
2262 // the stack frame manager does not allow locals to be
2263 // allocated (e.g., for alloca) after a temp is
2264 // allocated!
2265 //
2266 // int tmpOffset = MF.getInfo()->pushTempValue(argSize);
2267 int tmpOffset = MF.getInfo()->allocateLocalVar(argVReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002268
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002269 // Generate the store from FP reg to stack
Misha Brukmanea481cc2003-06-03 03:21:58 +00002270 unsigned StoreOpcode = ChooseStoreInstruction(argType);
2271 M = BuildMI(convertOpcodeFromRegToImm(StoreOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002272 .addReg(argVal).addMReg(regInfo.getFramePointer())
2273 .addSImm(tmpOffset);
2274 mvec.push_back(M);
2275
2276 // Generate the load from stack to int arg reg
Misha Brukmanea481cc2003-06-03 03:21:58 +00002277 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
2278 M = BuildMI(convertOpcodeFromRegToImm(LoadOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002279 .addMReg(regInfo.getFramePointer()).addSImm(tmpOffset)
2280 .addReg(argVReg, MOTy::Def);
2281
2282 // Mark operand with register it should be assigned
2283 // both for copy and for the callMI
2284 M->SetRegForOperand(M->getNumOperands()-1, copyRegNum);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002285 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2286 copyRegNum);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002287 mvec.push_back(M);
2288
2289 // Add info about the argument to the CallArgsDescriptor
2290 argInfo.setUseIntArgReg();
2291 argInfo.setArgCopy(copyRegNum);
2292 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00002293 // Cannot fit in first $K$ regs so pass arg on stack
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002294 argInfo.setUseStackSlot();
2295 }
2296 } else if (isFPArg) {
2297 // Get the outgoing arg reg to see if there is one.
2298 regNumForArg = regInfo.regNumForFPArg(regType, false, false,
2299 argNo, regClassIDOfArgReg);
2300 if (regNumForArg == regInfo.getInvalidRegNum())
2301 argInfo.setUseStackSlot();
2302 else {
2303 argInfo.setUseFPArgReg();
2304 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2305 regNumForArg);
2306 }
2307 } else {
2308 // Get the outgoing arg reg to see if there is one.
2309 regNumForArg = regInfo.regNumForIntArg(false,false,
2310 argNo, regClassIDOfArgReg);
2311 if (regNumForArg == regInfo.getInvalidRegNum())
2312 argInfo.setUseStackSlot();
2313 else {
2314 argInfo.setUseIntArgReg();
2315 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2316 regNumForArg);
2317 }
2318 }
2319
2320 //
2321 // Now insert copy instructions to stack slot or arg. register
2322 //
2323 if (argInfo.usesStackSlot()) {
2324 // Get the stack offset for this argument slot.
2325 // FP args on stack are right justified so adjust offset!
2326 // int arguments are also right justified but they are
2327 // always loaded as a full double-word so the offset does
2328 // not need to be adjusted.
2329 int argOffset = frameInfo.getOutgoingArgOffset(MF, argNo);
2330 if (argType->isFloatingPoint()) {
2331 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
2332 assert(argSize <= slotSize && "Insufficient slot size!");
2333 argOffset += slotSize - argSize;
2334 }
2335
2336 // Now generate instruction to copy argument to stack
2337 MachineOpCode storeOpCode =
2338 (argType->isFloatingPoint()
2339 ? ((argSize == 4)? V9::STFi : V9::STDFi) : V9::STXi);
2340
2341 M = BuildMI(storeOpCode, 3).addReg(argVal)
2342 .addMReg(regInfo.getStackPointer()).addSImm(argOffset);
2343 mvec.push_back(M);
2344 } else {
2345 // Create a virtual register to represent the arg reg. Mark
2346 // this vreg as being an implicit operand of the call MI.
2347 TmpInstruction* argVReg =
2348 new TmpInstruction(mcfi, argVal, NULL, "argReg");
2349
2350 callMI->addImplicitRef(argVReg);
2351
2352 // Generate the reg-to-reg copy into the outgoing arg reg.
2353 // -- For FP values, create a FMOVS or FMOVD instruction
2354 // -- For non-FP values, create an add-with-0 instruction
2355 if (argType->isFloatingPoint())
2356 M=(BuildMI(argType==Type::FloatTy? V9::FMOVS :V9::FMOVD,2)
2357 .addReg(argVal).addReg(argVReg, MOTy::Def));
2358 else
2359 M = (BuildMI(ChooseAddInstructionByType(argType), 3)
2360 .addReg(argVal).addSImm((int64_t) 0)
2361 .addReg(argVReg, MOTy::Def));
2362
2363 // Mark the operand with the register it should be assigned
2364 M->SetRegForOperand(M->getNumOperands()-1, regNumForArg);
2365 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2366 regNumForArg);
2367
2368 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +00002369 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002370 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002371
2372 // add call instruction and delay slot before copying return value
2373 mvec.push_back(callMI);
2374 mvec.push_back(BuildMI(V9::NOP, 0));
2375
Misha Brukman7b647942003-05-30 20:11:56 +00002376 // Add the return value as an implicit ref. The call operands
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002377 // were added above. Also, add code to copy out the return value.
2378 // This is always register-to-register for int or FP return values.
2379 //
2380 if (callInstr->getType() != Type::VoidTy) {
2381 // Get the return value reg.
2382 const Type* retType = callInstr->getType();
2383
2384 int regNum = (retType->isFloatingPoint()
2385 ? (unsigned) SparcFloatRegClass::f0
2386 : (unsigned) SparcIntRegClass::o0);
2387 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
2388 regNum = regInfo.getUnifiedRegNum(regClassID, regNum);
2389
2390 // Create a virtual register to represent it and mark
2391 // this vreg as being an implicit operand of the call MI
2392 TmpInstruction* retVReg =
2393 new TmpInstruction(mcfi, callInstr, NULL, "argReg");
2394
2395 callMI->addImplicitRef(retVReg, /*isDef*/ true);
2396
2397 // Generate the reg-to-reg copy from the return value reg.
2398 // -- For FP values, create a FMOVS or FMOVD instruction
2399 // -- For non-FP values, create an add-with-0 instruction
2400 if (retType->isFloatingPoint())
2401 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
2402 .addReg(retVReg).addReg(callInstr, MOTy::Def));
2403 else
2404 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
2405 .addReg(retVReg).addSImm((int64_t) 0)
2406 .addReg(callInstr, MOTy::Def));
2407
2408 // Mark the operand with the register it should be assigned
2409 // Also mark the implicit ref of the call defining this operand
2410 M->SetRegForOperand(0, regNum);
2411 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,regNum);
2412
2413 mvec.push_back(M);
2414 }
2415
Misha Brukman7b647942003-05-30 20:11:56 +00002416 // For the CALL instruction, the ret. addr. reg. is also implicit
2417 if (isa<Function>(callee))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002418 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
2419
2420 MF.getInfo()->popAllTempValues(); // free temps used for this inst
Misha Brukman7b647942003-05-30 20:11:56 +00002421 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002422
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002423 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002424 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002425
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002426 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002427 {
2428 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2429 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2430 Instruction* shlInstr = subtreeRoot->getInstruction();
2431
2432 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002433 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2434 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002435
2436 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Misha Brukmand36e30e2003-06-06 09:52:23 +00002437 (opType == Type::LongTy)? V9::SLLXr6:V9::SLLr5,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002438 argVal1, argVal2, 0, shlInstr, mvec,
2439 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002440 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002441 }
2442
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002443 case 63: // reg: Shr(reg, reg)
Misha Brukman7b647942003-05-30 20:11:56 +00002444 {
2445 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002446 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2447 "Shr unsupported for other types");
Chris Lattner54e898e2003-01-15 19:23:34 +00002448 Add3OperandInstr(opType->isSigned()
Misha Brukmand36e30e2003-06-06 09:52:23 +00002449 ? (opType == Type::LongTy ? V9::SRAXr6 : V9::SRAr5)
2450 : (opType == Type::LongTy ? V9::SRLXr6 : V9::SRLr5),
Chris Lattner54e898e2003-01-15 19:23:34 +00002451 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002452 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002453 }
2454
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002455 case 64: // reg: Phi(reg,reg)
2456 break; // don't forward the value
Vikram S. Adve74825322002-03-18 03:15:35 +00002457
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002458 case 65: // reg: VaArg(reg)
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002459 {
2460 // Use value initialized by va_start as pointer to args on the stack.
2461 // Load argument via current pointer value, then increment pointer.
2462 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
2463 Instruction* vaArgI = subtreeRoot->getInstruction();
Misha Brukman91aee472003-05-27 22:37:00 +00002464 mvec.push_back(BuildMI(V9::LDXi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002465 addSImm(0).addRegDef(vaArgI));
Misha Brukman91aee472003-05-27 22:37:00 +00002466 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002467 addSImm(argSize).addRegDef(vaArgI->getOperand(0)));
2468 break;
2469 }
2470
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002471 case 71: // reg: VReg
2472 case 72: // reg: Constant
2473 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002474
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002475 default:
2476 assert(0 && "Unrecognized BURG rule");
2477 break;
2478 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002479 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002480
Misha Brukman7b647942003-05-30 20:11:56 +00002481 if (forwardOperandNum >= 0) {
2482 // We did not generate a machine instruction but need to use operand.
2483 // If user is in the same tree, replace Value in its machine operand.
2484 // If not, insert a copy instruction which should get coalesced away
2485 // by register allocation.
2486 if (subtreeRoot->parent() != NULL)
2487 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2488 else {
2489 std::vector<MachineInstr*> minstrVec;
2490 Instruction* instr = subtreeRoot->getInstruction();
2491 target.getInstrInfo().
2492 CreateCopyInstructionsByType(target,
2493 instr->getParent()->getParent(),
2494 instr->getOperand(forwardOperandNum),
2495 instr, minstrVec,
2496 MachineCodeForInstruction::get(instr));
2497 assert(minstrVec.size() > 0);
2498 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Chris Lattner20b1ea02001-09-14 03:47:57 +00002499 }
Misha Brukman7b647942003-05-30 20:11:56 +00002500 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002501
Misha Brukman7b647942003-05-30 20:11:56 +00002502 if (maskUnsignedResult) {
2503 // If result is unsigned and smaller than int reg size,
2504 // we need to clear high bits of result value.
2505 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2506 Instruction* dest = subtreeRoot->getInstruction();
2507 if (dest->getType()->isUnsigned()) {
2508 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
2509 if (destSize <= 4) {
2510 // Mask high bits. Use a TmpInstruction to represent the
2511 // intermediate result before masking. Since those instructions
2512 // have already been generated, go back and substitute tmpI
2513 // for dest in the result position of each one of them.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002514 TmpInstruction *tmpI =
2515 new TmpInstruction(MachineCodeForInstruction::get(dest),
2516 dest->getType(), dest, NULL, "maskHi");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002517
Misha Brukman7b647942003-05-30 20:11:56 +00002518 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2519 mvec[i]->substituteValue(dest, tmpI);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002520
Misha Brukmand36e30e2003-06-06 09:52:23 +00002521 M = BuildMI(V9::SRLi5, 3).addReg(tmpI).addZImm(8*(4-destSize))
Misha Brukman7b647942003-05-30 20:11:56 +00002522 .addReg(dest, MOTy::Def);
2523 mvec.push_back(M);
2524 } else if (destSize < 8) {
2525 assert(0 && "Unsupported type size: 32 < size < 64 bits");
2526 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002527 }
Misha Brukman7b647942003-05-30 20:11:56 +00002528 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002529}