blob: db49c4ebde230c8f5c0565bd57e48198a40499b7 [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);
480 else if (opType == Type::LongTy || opType == Type::ULongTy)
481 opCode = (vopCode == ToFloatTy? V9::FXTOS : V9::FXTOD);
482 else if (opType == Type::FloatTy)
483 opCode = (vopCode == ToFloatTy? V9::INVALID_OPCODE : V9::FSTOD);
484 else if (opType == Type::DoubleTy)
485 opCode = (vopCode == ToFloatTy? V9::FDTOS : V9::INVALID_OPCODE);
486 else
487 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000488
489 return opCode;
490}
491
492static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000493ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000494{
Misha Brukmana98cd452003-05-20 20:32:24 +0000495 MachineOpCode opCode = V9::INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000496
497 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
498 && "This function should only be called for FLOAT or DOUBLE");
499
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000500 // SPARC does not have a float-to-uint conversion, only a float-to-int.
501 // For converting an FP value to uint32_t, we first need to convert to
502 // uint64_t and then to uint32_t, or we may overflow the signed int
503 // representation even for legal uint32_t values. This expansion is
504 // done by the Preselection pass.
505 //
Misha Brukman81b06862003-05-21 18:48:06 +0000506 if (tid == Type::UIntTyID) {
507 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
508 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
509 } else if (tid == Type::SByteTyID || tid == Type::ShortTyID ||
510 tid == Type::IntTyID || tid == Type::UByteTyID ||
511 tid == Type::UShortTyID) {
512 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
513 } else if (tid == Type::LongTyID || tid == Type::ULongTyID) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000514 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
Misha Brukman81b06862003-05-21 18:48:06 +0000515 } else
516 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000517
Chris Lattner20b1ea02001-09-14 03:47:57 +0000518 return opCode;
519}
520
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000521MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000522CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
523 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000524{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000525 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000526 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000527 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000528}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000529
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000530// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000531// The FP value must be converted to the dest type in an FP register,
532// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000533//
534// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
535// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000536// *only* when converting to an unsigned. (Unsigned byte, short or long
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000537// don't have this problem.)
538// For unsigned int, we therefore have to generate the code sequence:
539//
540// if (V > (float) MAXINT) {
541// unsigned result = (unsigned) (V - (float) MAXINT);
542// result = result + (unsigned) MAXINT;
543// }
544// else
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000545// result = (unsigned) V;
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000546//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000547static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000548CreateCodeToConvertFloatToInt(const TargetMachine& target,
549 Value* opVal,
550 Instruction* destI,
551 std::vector<MachineInstr*>& mvec,
552 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000553{
554 // Create a temporary to represent the FP register into which the
555 // int value will placed after conversion. The type of this temporary
556 // depends on the type of FP register to use: single-prec for a 32-bit
557 // int or smaller; double-prec for a 64-bit int.
558 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000559 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000560 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000561 TmpInstruction* destForCast = new TmpInstruction(mcfi, destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000562
563 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000564 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
565 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000566 mvec.push_back(M);
567
568 // Create the fpreg-to-intreg copy code
569 target.getInstrInfo().
570 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000571 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000572}
573
574
Chris Lattner20b1ea02001-09-14 03:47:57 +0000575static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000576ChooseAddInstruction(const InstructionNode* instrNode)
577{
578 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
579}
580
581
Chris Lattner20b1ea02001-09-14 03:47:57 +0000582static inline MachineInstr*
583CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000584 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000585{
Misha Brukmana98cd452003-05-20 20:32:24 +0000586 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000587 .addReg(instrNode->leftChild()->getValue())
588 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000589}
590
591static inline MachineInstr*
592CreateAddConstInstruction(const InstructionNode* instrNode)
593{
594 MachineInstr* minstr = NULL;
595
596 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000597 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000598
599 // Cases worth optimizing are:
600 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
601 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
602 //
Chris Lattner9b625032002-05-06 16:15:30 +0000603 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000604 double dval = FPC->getValue();
605 if (dval == 0.0)
606 minstr = CreateMovFloatInstruction(instrNode,
607 instrNode->getInstruction()->getType());
608 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000609
610 return minstr;
611}
612
613
614static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000615ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000616{
Misha Brukmana98cd452003-05-20 20:32:24 +0000617 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000618
Misha Brukman81b06862003-05-21 18:48:06 +0000619 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukman91aee472003-05-27 22:37:00 +0000620 opCode = V9::SUBr;
Misha Brukman81b06862003-05-21 18:48:06 +0000621 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000622 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000623 {
624 case Type::FloatTyID: opCode = V9::FSUBS; break;
625 case Type::DoubleTyID: opCode = V9::FSUBD; break;
626 default: assert(0 && "Invalid type for SUB instruction"); break;
627 }
628 }
629
Chris Lattner20b1ea02001-09-14 03:47:57 +0000630 return opCode;
631}
632
633
634static inline MachineInstr*
635CreateSubConstInstruction(const InstructionNode* instrNode)
636{
637 MachineInstr* minstr = NULL;
638
639 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000640 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000641
642 // Cases worth optimizing are:
643 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
644 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
645 //
Chris Lattner9b625032002-05-06 16:15:30 +0000646 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
647 double dval = FPC->getValue();
648 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000649 minstr = CreateMovFloatInstruction(instrNode,
650 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000651 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000652
653 return minstr;
654}
655
656
657static inline MachineOpCode
658ChooseFcmpInstruction(const InstructionNode* instrNode)
659{
Misha Brukmana98cd452003-05-20 20:32:24 +0000660 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000661
662 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
663 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000664 case Type::FloatTyID: opCode = V9::FCMPS; break;
665 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000666 default: assert(0 && "Invalid type for FCMP instruction"); break;
667 }
668
669 return opCode;
670}
671
672
673// Assumes that leftArg and rightArg are both cast instructions.
674//
675static inline bool
676BothFloatToDouble(const InstructionNode* instrNode)
677{
678 InstrTreeNode* leftArg = instrNode->leftChild();
679 InstrTreeNode* rightArg = instrNode->rightChild();
680 InstrTreeNode* leftArgArg = leftArg->leftChild();
681 InstrTreeNode* rightArgArg = rightArg->leftChild();
682 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
683
684 // Check if both arguments are floats cast to double
685 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000686 leftArgArg->getValue()->getType() == Type::FloatTy &&
687 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000688}
689
690
691static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000692ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000693{
Misha Brukmana98cd452003-05-20 20:32:24 +0000694 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000695
Chris Lattner0c4e8862002-09-03 01:08:28 +0000696 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000697 opCode = V9::MULXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000698 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000699 switch(resultType->getPrimitiveID())
Misha Brukman7b647942003-05-30 20:11:56 +0000700 {
701 case Type::FloatTyID: opCode = V9::FMULS; break;
702 case Type::DoubleTyID: opCode = V9::FMULD; break;
703 default: assert(0 && "Invalid type for MUL instruction"); break;
704 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000705
706 return opCode;
707}
708
709
Vikram S. Adve510eec72001-11-04 21:59:14 +0000710
Chris Lattner20b1ea02001-09-14 03:47:57 +0000711static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000712CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000713 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000714{
Misha Brukman91aee472003-05-27 22:37:00 +0000715 return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000716 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000717}
718
719
Vikram S. Adve242a8082002-05-19 15:25:51 +0000720// Create instruction sequence for any shift operation.
721// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
722// requires a second instruction for explicit sign-extension.
723// Note that we only have to worry about a sign-bit appearing in the
724// most significant bit of the operand after shifting (e.g., bit 32 of
725// Int or bit 16 of Short), so we do not have to worry about results
726// that are as large as a normal integer register.
727//
728static inline void
729CreateShiftInstructions(const TargetMachine& target,
730 Function* F,
731 MachineOpCode shiftOpCode,
732 Value* argVal1,
733 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000734 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000735 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000736 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000737 MachineCodeForInstruction& mcfi)
738{
739 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
740 "Large shift sizes unexpected, but can be handled below: "
741 "You need to check whether or not it fits in immed field below");
742
743 // If this is a logical left shift of a type smaller than the standard
744 // integer reg. size, we have to extend the sign-bit into upper bits
745 // of dest, so we need to put the result of the SLL into a temporary.
746 //
747 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000748 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000749
Misha Brukman7b647942003-05-30 20:11:56 +0000750 if ((shiftOpCode == V9::SLLr6 || shiftOpCode == V9::SLLXr6) && opSize < 8) {
751 // put SLL result into a temporary
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000752 shiftDest = new TmpInstruction(mcfi, argVal1, optArgVal2, "sllTmp");
Misha Brukman7b647942003-05-30 20:11:56 +0000753 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000754
755 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000756 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
757 .addReg(shiftDest, MOTy::Def)
758 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
759 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000760 mvec.push_back(M);
761
Misha Brukman7b647942003-05-30 20:11:56 +0000762 if (shiftDest != destVal) {
763 // extend the sign-bit of the result into all upper bits of dest
764 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
765 target.getInstrInfo().
766 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
767 8*opSize, mvec, mcfi);
768 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000769}
770
771
Vikram S. Adve74825322002-03-18 03:15:35 +0000772// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000773// create a cheaper instruction.
774// This returns the approximate cost of the instructions generated,
775// which is used to pick the cheapest when both operands are constant.
Vikram S. Adve645fea32003-05-25 21:59:47 +0000776static unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000777CreateMulConstInstruction(const TargetMachine &target, Function* F,
778 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000779 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000780 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000781{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000782 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukman91aee472003-05-27 22:37:00 +0000783 unsigned cost = target.getInstrInfo().minLatency(V9::MULXr);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000784 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000785
786 Value* constOp = rval;
787 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000788 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000789
790 // Cases worth optimizing are:
791 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
792 // (2) Multiply by 2^x for integer types: replace with Shift
793 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000794 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000795
Misha Brukmana98cd452003-05-20 20:32:24 +0000796 if (resultType->isInteger() || isa<PointerType>(resultType)) {
797 bool isValidConst;
798 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
799 if (isValidConst) {
800 unsigned pow;
801 bool needNeg = false;
802 if (C < 0) {
803 needNeg = true;
804 C = -C;
805 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000806
Misha Brukmana98cd452003-05-20 20:32:24 +0000807 if (C == 0 || C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000808 cost = target.getInstrInfo().minLatency(V9::ADDr);
Misha Brukmana98cd452003-05-20 20:32:24 +0000809 unsigned Zero = target.getRegInfo().getZeroRegNum();
810 MachineInstr* M;
811 if (C == 0)
Misha Brukman91aee472003-05-27 22:37:00 +0000812 M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000813 else
Misha Brukman91aee472003-05-27 22:37:00 +0000814 M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000815 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +0000816 } else if (isPowerOf2(C, pow)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000817 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Misha Brukman91aee472003-05-27 22:37:00 +0000818 MachineOpCode opCode = (opSize <= 32)? V9::SLLr6 : V9::SLLXr6;
Misha Brukmana98cd452003-05-20 20:32:24 +0000819 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
820 destVal, mvec, mcfi);
821 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000822
Misha Brukman7b647942003-05-30 20:11:56 +0000823 if (mvec.size() > 0 && needNeg) {
824 // insert <reg = SUB 0, reg> after the instr to flip the sign
Misha Brukmana98cd452003-05-20 20:32:24 +0000825 MachineInstr* M = CreateIntNegInstruction(target, destVal);
826 mvec.push_back(M);
827 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000828 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000829 } else {
830 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
831 double dval = FPC->getValue();
832 if (fabs(dval) == 1) {
833 MachineOpCode opCode = (dval < 0)
834 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
835 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
836 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
837 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000838 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000839 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000840
Misha Brukmana98cd452003-05-20 20:32:24 +0000841 if (firstNewInstr < mvec.size()) {
842 cost = 0;
843 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
844 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
845 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000846
847 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000848}
849
850
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000851// Does not create any instructions if we cannot exploit constant to
852// create a cheaper instruction.
853//
854static inline void
855CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000856 Function* F,
857 Value* lval, Value* rval,
858 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000859 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000860 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000861{
862 Value* constOp;
Misha Brukman7b647942003-05-30 20:11:56 +0000863 if (isa<Constant>(lval) && isa<Constant>(rval)) {
864 // both operands are constant: evaluate and "set" in dest
865 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
866 cast<Constant>(lval),
867 cast<Constant>(rval));
868 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
869 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000870 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000871 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000872 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000873 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000874
875 // else neither is constant
876 return;
877}
878
Vikram S. Adve74825322002-03-18 03:15:35 +0000879// Return NULL if we cannot exploit constant to create a cheaper instruction
880static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000881CreateMulInstruction(const TargetMachine &target, Function* F,
882 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000883 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000884 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000885 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
886{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000887 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000888 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000889 if (mvec.size() == L) {
890 // no instructions were added so create MUL reg, reg, reg.
891 // Use FSMULD if both operands are actually floats cast to doubles.
892 // Otherwise, use the default opcode for the appropriate type.
893 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
894 ? forceMulOp
895 : ChooseMulInstructionByType(destVal->getType()));
896 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
897 .addRegDef(destVal));
898 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000899}
900
901
Vikram S. Adve510eec72001-11-04 21:59:14 +0000902// Generate a divide instruction for Div or Rem.
903// For Rem, this assumes that the operand type will be signed if the result
904// type is signed. This is correct because they must have the same sign.
905//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000906static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000907ChooseDivInstruction(TargetMachine &target,
908 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000909{
Misha Brukmana98cd452003-05-20 20:32:24 +0000910 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000911
912 const Type* resultType = instrNode->getInstruction()->getType();
913
Chris Lattner0c4e8862002-09-03 01:08:28 +0000914 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000915 opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000916 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000917 switch(resultType->getPrimitiveID())
918 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000919 case Type::FloatTyID: opCode = V9::FDIVS; break;
920 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000921 default: assert(0 && "Invalid type for DIV instruction"); break;
922 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000923
924 return opCode;
925}
926
927
Chris Lattner54e898e2003-01-15 19:23:34 +0000928// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve645fea32003-05-25 21:59:47 +0000929static void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000930CreateDivConstInstruction(TargetMachine &target,
931 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000932 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000933{
Chris Lattner54e898e2003-01-15 19:23:34 +0000934 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000935 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000936 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000937 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000938
Vikram S. Adve645fea32003-05-25 21:59:47 +0000939 Instruction* destVal = instrNode->getInstruction();
Chris Lattner54e898e2003-01-15 19:23:34 +0000940 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000941
942 // Cases worth optimizing are:
943 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
944 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
945 //
946 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000947
Misha Brukman7b647942003-05-30 20:11:56 +0000948 if (resultType->isInteger()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000949 unsigned pow;
950 bool isValidConst;
951 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
952 if (isValidConst) {
953 bool needNeg = false;
954 if (C < 0) {
955 needNeg = true;
956 C = -C;
957 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000958
Misha Brukmana98cd452003-05-20 20:32:24 +0000959 if (C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000960 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000961 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +0000962 } else if (isPowerOf2(C, pow)) {
Vikram S. Adve645fea32003-05-25 21:59:47 +0000963 unsigned opCode;
964 Value* shiftOperand;
965
966 if (resultType->isSigned()) {
967 // The result may be negative and we need to add one before shifting
968 // a negative value. Use:
969 // srl i0, 31, x0; add x0, i0, i1 (if i0 is <= 32 bits)
970 // or
971 // srlx i0, 63, x0; add x0, i0, i1 (if i0 is 64 bits)
972 // to compute i1=i0+1 if i0 < 0 and i1=i0 otherwise.
973 //
974 TmpInstruction *srlTmp, *addTmp;
975 MachineCodeForInstruction& mcfi
976 = MachineCodeForInstruction::get(destVal);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000977 srlTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getSign");
978 addTmp = new TmpInstruction(mcfi, resultType, LHS, srlTmp,"incIfNeg");
Vikram S. Adve645fea32003-05-25 21:59:47 +0000979
980 // Create the SRL or SRLX instruction to get the sign bit
Misha Brukman91aee472003-05-27 22:37:00 +0000981 mvec.push_back(BuildMI((resultType==Type::LongTy) ?
982 V9::SRLXi6 : V9::SRLi6, 3)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000983 .addReg(LHS)
984 .addSImm((resultType==Type::LongTy)? 63 : 31)
985 .addRegDef(srlTmp));
986
987 // Create the ADD instruction to add 1 for negative values
Misha Brukman91aee472003-05-27 22:37:00 +0000988 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000989 .addRegDef(addTmp));
990
991 // Get the shift operand and "right-shift" opcode to do the divide
992 shiftOperand = addTmp;
Misha Brukman91aee472003-05-27 22:37:00 +0000993 opCode = (resultType==Type::LongTy) ? V9::SRAXi6 : V9::SRAi6;
Misha Brukman7b647942003-05-30 20:11:56 +0000994 } else {
Vikram S. Adve645fea32003-05-25 21:59:47 +0000995 // Get the shift operand and "right-shift" opcode to do the divide
996 shiftOperand = LHS;
Misha Brukman91aee472003-05-27 22:37:00 +0000997 opCode = (resultType==Type::LongTy) ? V9::SRLXi6 : V9::SRLi6;
Vikram S. Adve645fea32003-05-25 21:59:47 +0000998 }
999
1000 // Now do the actual shift!
1001 mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow)
1002 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001003 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001004
Misha Brukmana98cd452003-05-20 20:32:24 +00001005 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
1006 // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve645fea32003-05-25 21:59:47 +00001007 mvec.push_back(CreateIntNegInstruction(target, destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001008 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001009 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001010 } else {
1011 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
1012 double dval = FPC->getValue();
1013 if (fabs(dval) == 1) {
1014 unsigned opCode =
1015 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
1016 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001017
Vikram S. Adve645fea32003-05-25 21:59:47 +00001018 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001019 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001020 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001021 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001022}
1023
1024
Vikram S. Adve74825322002-03-18 03:15:35 +00001025static void
1026CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1027 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001028 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001029 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001030 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001031{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001032 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001033 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001034 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001035 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001036
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001037 // Enforce the alignment constraints on the stack pointer at
1038 // compile time if the total size is a known constant.
Misha Brukman7b647942003-05-30 20:11:56 +00001039 if (isa<Constant>(numElementsVal)) {
1040 bool isValid;
1041 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
1042 assert(isValid && "Unexpectedly large array dimension in alloca!");
1043 int64_t total = numElem * tsize;
1044 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1045 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1046 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1047 } else {
1048 // The size is not a constant. Generate code to compute it and
1049 // code to pad the size for stack alignment.
1050 // Create a Value to hold the (constant) element size
1051 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001052
Misha Brukman7b647942003-05-30 20:11:56 +00001053 // Create temporary values to hold the result of MUL, SLL, SRL
1054 // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001055 TmpInstruction* tmpProd = new TmpInstruction(mcfi,numElementsVal, tsizeVal);
1056 TmpInstruction* tmpSLL = new TmpInstruction(mcfi,numElementsVal, tmpProd);
1057 TmpInstruction* tmpSRL = new TmpInstruction(mcfi,numElementsVal, tmpSLL);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001058
Misha Brukman7b647942003-05-30 20:11:56 +00001059 // Instruction 1: mul numElements, typeSize -> tmpProd
1060 // This will optimize the MUL as far as possible.
1061 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec,
1062 mcfi, INVALID_MACHINE_OPCODE);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001063
Misha Brukman7b647942003-05-30 20:11:56 +00001064 assert(0 && "Need to insert padding instructions here!");
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001065
Misha Brukman7b647942003-05-30 20:11:56 +00001066 totalSizeVal = tmpProd;
1067 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001068
1069 // Get the constant offset from SP for dynamically allocated storage
1070 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001071 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001072 bool growUp;
1073 ConstantSInt* dynamicAreaOffset =
1074 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001075 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001076 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1077
Chris Lattner54e898e2003-01-15 19:23:34 +00001078 unsigned SPReg = target.getRegInfo().getStackPointer();
1079
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001080 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukman91aee472003-05-27 22:37:00 +00001081 getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001082 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001083
Vikram S. Adve74825322002-03-18 03:15:35 +00001084 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukman91aee472003-05-27 22:37:00 +00001085 getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset)
Misha Brukmana98cd452003-05-20 20:32:24 +00001086 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001087}
1088
1089
1090static void
1091CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1092 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001093 unsigned tsize,
1094 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001095 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001096{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001097 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001098 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001099 "Result value is not part of a function?");
1100 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001101 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001102
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001103 // Check if the offset would small enough to use as an immediate in
1104 // load/stores (check LDX because all load/stores have the same-size immediate
1105 // field). If not, put the variable in the dynamically sized area of the
1106 // frame.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001107 unsigned paddedSizeIgnored;
1108 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001109 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +00001110 tsize * numElements);
Misha Brukman91aee472003-05-27 22:37:00 +00001111 if (! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001112 CreateCodeForVariableSizeAlloca(target, result, tsize,
1113 ConstantSInt::get(Type::IntTy,numElements),
1114 getMvec);
1115 return;
1116 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001117
1118 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001119 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001120
1121 // Create a temporary Value to hold the constant offset.
1122 // This is needed because it may not fit in the immediate field.
1123 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1124
1125 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001126 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukman91aee472003-05-27 22:37:00 +00001127 getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001128 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001129}
1130
1131
Chris Lattner20b1ea02001-09-14 03:47:57 +00001132//------------------------------------------------------------------------
1133// Function SetOperandsForMemInstr
1134//
1135// Choose addressing mode for the given load or store instruction.
1136// Use [reg+reg] if it is an indexed reference, and the index offset is
1137// not a constant or if it cannot fit in the offset field.
1138// Use [reg+offset] in all other cases.
1139//
1140// This assumes that all array refs are "lowered" to one of these forms:
1141// %x = load (subarray*) ptr, constant ; single constant offset
1142// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1143// Generally, this should happen via strength reduction + LICM.
1144// Also, strength reduction should take care of using the same register for
1145// the loop index variable and an array index, when that is profitable.
1146//------------------------------------------------------------------------
1147
1148static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001149SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001150 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001151 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001152 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001153{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001154 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001155 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001156 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001157 bool allConstantIndices;
1158 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001159
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001160 // Now create the appropriate operands for the machine instruction.
1161 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001162 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001163 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001164 MachineOperand::MachineOperandType offsetOpType =
1165 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001166
Vikram S. Adve74825322002-03-18 03:15:35 +00001167 // Check if there is an index vector and if so, compute the
1168 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001169 //
Misha Brukman7b647942003-05-30 20:11:56 +00001170 if (!idxVec.empty()) {
1171 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001172
Misha Brukman7b647942003-05-30 20:11:56 +00001173 // If all indices are constant, compute the combined offset directly.
1174 if (allConstantIndices) {
1175 // Compute the offset value using the index vector. Create a
1176 // virtual reg. for it since it may not fit in the immed field.
1177 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
1178 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
1179 } else {
1180 // There is at least one non-constant offset. Therefore, this must
1181 // be an array ref, and must have been lowered to a single non-zero
1182 // offset. (An extra leading zero offset, if any, can be ignored.)
1183 // Generate code sequence to compute address from index.
1184 //
1185 bool firstIdxIsZero = IsZero(idxVec[0]);
1186 assert(idxVec.size() == 1U + firstIdxIsZero
1187 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001188
Misha Brukman7b647942003-05-30 20:11:56 +00001189 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001190
Misha Brukman7b647942003-05-30 20:11:56 +00001191 std::vector<MachineInstr*> mulVec;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001192 Instruction* addr =
1193 new TmpInstruction(MachineCodeForInstruction::get(memInst),
1194 Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001195
Misha Brukman7b647942003-05-30 20:11:56 +00001196 // Get the array type indexed by idxVal, and compute its element size.
1197 // The call to getTypeSize() will fail if size is not constant.
1198 const Type* vecType = (firstIdxIsZero
1199 ? GetElementPtrInst::getIndexedType(ptrType,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001200 std::vector<Value*>(1U, idxVec[0]),
1201 /*AllowCompositeLeaf*/ true)
1202 : ptrType);
Misha Brukman7b647942003-05-30 20:11:56 +00001203 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1204 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
1205 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001206
Misha Brukman7b647942003-05-30 20:11:56 +00001207 // CreateMulInstruction() folds constants intelligently enough.
1208 CreateMulInstruction(target, memInst->getParent()->getParent(),
1209 idxVal, /* lval, not likely to be const*/
1210 eltSizeVal, /* rval, likely to be constant */
1211 addr, /* result */
1212 mulVec, MachineCodeForInstruction::get(memInst),
1213 INVALID_MACHINE_OPCODE);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001214
Misha Brukman7b647942003-05-30 20:11:56 +00001215 assert(mulVec.size() > 0 && "No multiply code created?");
1216 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
1217
1218 valueForRegOffset = addr;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001219 }
Misha Brukman7b647942003-05-30 20:11:56 +00001220 } else {
1221 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1222 smallConstOffset = 0;
1223 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001224
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001225 // For STORE:
1226 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1227 // For LOAD or GET_ELEMENT_PTR,
1228 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1229 //
1230 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001231 MachineInstr *MI;
1232 if (memInst->getOpcode() == Instruction::Store) {
Misha Brukman7b647942003-05-30 20:11:56 +00001233 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001234 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1235 .addReg(ptrVal).addReg(valueForRegOffset);
Misha Brukman7b647942003-05-30 20:11:56 +00001236 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001237 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001238 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1239 .addReg(ptrVal).addSImm(smallConstOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001240 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001241 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00001242 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001243 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1244 .addRegDef(memInst);
Misha Brukman7b647942003-05-30 20:11:56 +00001245 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001246 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001247 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1248 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001249 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001250 }
1251 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001252}
1253
1254
Chris Lattner20b1ea02001-09-14 03:47:57 +00001255//
1256// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001257// in place of the use(s) of that instruction in node `parent'.
1258// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001259// Also make sure to skip over a parent who:
1260// (1) is a list node in the Burg tree, or
1261// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001262//
1263static void
1264ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001265 InstrTreeNode* parent,
1266 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001267{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001268 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1269
Chris Lattner20b1ea02001-09-14 03:47:57 +00001270 Instruction* unusedOp = treeNode->getInstruction();
1271 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001272
1273 // The parent itself may be a list node, so find the real parent instruction
1274 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1275 {
1276 parent = parent->parent();
1277 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1278 }
1279 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1280
1281 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001282 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001283
1284 // The parent's mvec would be empty if it was itself forwarded.
1285 // Recursively call ForwardOperand in that case...
1286 //
Misha Brukman7b647942003-05-30 20:11:56 +00001287 if (mvec.size() == 0) {
1288 assert(parent->parent() != NULL &&
1289 "Parent could not have been forwarded, yet has no instructions?");
1290 ForwardOperand(treeNode, parent->parent(), operandNum);
1291 } else {
1292 for (unsigned i=0, N=mvec.size(); i < N; i++) {
1293 MachineInstr* minstr = mvec[i];
1294 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) {
1295 const MachineOperand& mop = minstr->getOperand(i);
1296 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
1297 mop.getVRegValue() == unusedOp)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001298 {
Misha Brukman7b647942003-05-30 20:11:56 +00001299 minstr->SetMachineOperandVal(i, MachineOperand::MO_VirtualRegister,
1300 fwdOp);
1301 }
1302 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001303
Misha Brukman7b647942003-05-30 20:11:56 +00001304 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1305 if (minstr->getImplicitRef(i) == unusedOp) {
1306 minstr->setImplicitRef(i, fwdOp,
1307 minstr->getImplicitOp(i).opIsDefOnly(),
1308 minstr->getImplicitOp(i).opIsDefAndUse());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001309 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001310 }
Misha Brukman7b647942003-05-30 20:11:56 +00001311 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001312}
1313
1314
Vikram S. Adve242a8082002-05-19 15:25:51 +00001315inline bool
1316AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001317{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001318 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1319 UI != UE; ++UI)
1320 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1321 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1322 return false;
1323 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001324}
1325
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001326// Generate code for any intrinsic that needs a special code sequence
1327// instead of a regular call. If not that kind of intrinsic, do nothing.
1328// Returns true if code was generated, otherwise false.
1329//
1330bool CodeGenIntrinsic(LLVMIntrinsic::ID iid, CallInst &callInstr,
1331 TargetMachine &target,
1332 std::vector<MachineInstr*>& mvec)
1333{
1334 switch (iid) {
1335 case LLVMIntrinsic::va_start: {
1336 // Get the address of the first vararg value on stack and copy it to
1337 // the argument of va_start(va_list* ap).
1338 bool ignore;
1339 Function* func = cast<Function>(callInstr.getParent()->getParent());
1340 int numFixedArgs = func->getFunctionType()->getNumParams();
1341 int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum();
1342 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
1343 int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo().
1344 getFirstIncomingArgOffset(MachineFunction::get(func), ignore);
Misha Brukman91aee472003-05-27 22:37:00 +00001345 mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001346 addReg(callInstr.getOperand(1)));
1347 return true;
1348 }
1349
1350 case LLVMIntrinsic::va_end:
1351 return true; // no-op on Sparc
1352
1353 case LLVMIntrinsic::va_copy:
1354 // Simple copy of current va_list (arg2) to new va_list (arg1)
Misha Brukman91aee472003-05-27 22:37:00 +00001355 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001356 addMReg(target.getRegInfo().getZeroRegNum()).
1357 addReg(callInstr.getOperand(2)).
1358 addReg(callInstr.getOperand(1)));
1359 return true;
1360
1361 default:
1362 return false;
1363 }
1364}
1365
Vikram S. Advefb361122001-10-22 13:36:31 +00001366//******************* Externally Visible Functions *************************/
1367
Vikram S. Advefb361122001-10-22 13:36:31 +00001368//------------------------------------------------------------------------
1369// External Function: ThisIsAChainRule
1370//
1371// Purpose:
1372// Check if a given BURG rule is a chain rule.
1373//------------------------------------------------------------------------
1374
1375extern bool
1376ThisIsAChainRule(int eruleno)
1377{
1378 switch(eruleno)
1379 {
1380 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001381 case 123:
1382 case 124:
1383 case 125:
1384 case 126:
1385 case 127:
1386 case 128:
1387 case 129:
1388 case 130:
1389 case 131:
1390 case 132:
1391 case 133:
1392 case 155:
1393 case 221:
1394 case 222:
1395 case 241:
1396 case 242:
1397 case 243:
1398 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001399 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001400 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001401 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001402
Vikram S. Advefb361122001-10-22 13:36:31 +00001403 default:
1404 return false; break;
1405 }
1406}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001407
1408
1409//------------------------------------------------------------------------
1410// External Function: GetInstructionsByRule
1411//
1412// Purpose:
1413// Choose machine instructions for the SPARC according to the
1414// patterns chosen by the BURG-generated parser.
1415//------------------------------------------------------------------------
1416
Vikram S. Adve74825322002-03-18 03:15:35 +00001417void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001418GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001419 int ruleForNode,
1420 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001421 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001422 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001423{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001424 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001425 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001426 int nextRule;
1427 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001428 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001429 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001430 unsigned L;
Vikram S. Adve74825322002-03-18 03:15:35 +00001431
1432 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001433
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001434 // If the code for this instruction was folded into the parent (user),
1435 // then do nothing!
1436 if (subtreeRoot->isFoldedIntoParent())
1437 return;
1438
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001439 //
1440 // Let's check for chain rules outside the switch so that we don't have
1441 // to duplicate the list of chain rule production numbers here again
1442 //
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001443 if (ThisIsAChainRule(ruleForNode))
1444 {
1445 // Chain rules have a single nonterminal on the RHS.
1446 // Get the rule that matches the RHS non-terminal and use that instead.
1447 //
1448 assert(nts[0] && ! nts[1]
1449 && "A chain rule should have only one RHS non-terminal!");
1450 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1451 nts = burm_nts[nextRule];
1452 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
1453 }
1454 else
1455 {
1456 switch(ruleForNode) {
1457 case 1: // stmt: Ret
1458 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001459 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001460 // for moving return value to appropriate register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001461 // Copy the return value to the required return register.
1462 // Mark the return Value as an implicit ref of the RET instr..
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001463 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001464 // Finally put a NOP in the delay slot.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001465 ReturnInst *returnInstr=cast<ReturnInst>(subtreeRoot->getInstruction());
1466 Value* retVal = returnInstr->getReturnValue();
1467 MachineCodeForInstruction& mcfi =
1468 MachineCodeForInstruction::get(returnInstr);
1469
1470 // Create a hidden virtual reg to represent the return address register
1471 // used by the machine instruction but not represented in LLVM.
1472 //
1473 Instruction* returnAddrTmp = new TmpInstruction(mcfi, returnInstr);
1474
1475 MachineInstr* retMI =
1476 BuildMI(V9::JMPLRETi, 3).addReg(returnAddrTmp).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001477 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001478
1479 // Insert a copy to copy the return value to the appropriate register
1480 // -- For FP values, create a FMOVS or FMOVD instruction
1481 // -- For non-FP values, create an add-with-0 instruction
1482 //
1483 if (retVal != NULL) {
1484 const UltraSparcRegInfo& regInfo =
1485 (UltraSparcRegInfo&) target.getRegInfo();
1486 const Type* retType = retVal->getType();
1487 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
1488 unsigned retRegNum = (retType->isFloatingPoint()
1489 ? (unsigned) SparcFloatRegClass::f0
1490 : (unsigned) SparcIntRegClass::i0);
1491 retRegNum = regInfo.getUnifiedRegNum(regClassID, retRegNum);
1492
1493 // Create a virtual register to represent it and mark
1494 // this vreg as being an implicit operand of the ret MI
1495 TmpInstruction* retVReg =
1496 new TmpInstruction(mcfi, retVal, NULL, "argReg");
1497
1498 retMI->addImplicitRef(retVReg);
1499
1500 if (retType->isFloatingPoint())
1501 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
1502 .addReg(retVal).addReg(retVReg, MOTy::Def));
1503 else
1504 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
1505 .addReg(retVal).addSImm((int64_t) 0)
1506 .addReg(retVReg, MOTy::Def));
1507
1508 // Mark the operand with the register it should be assigned
1509 M->SetRegForOperand(M->getNumOperands()-1, retRegNum);
1510 retMI->SetRegForImplicitRef(retMI->getNumImplicitRefs()-1, retRegNum);
1511
1512 mvec.push_back(M);
1513 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001514
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001515 // Now insert the RET instruction and a NOP for the delay slot
1516 mvec.push_back(retMI);
Misha Brukmana98cd452003-05-20 20:32:24 +00001517 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001518
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001519 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001520 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001521
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001522 case 3: // stmt: Store(reg,reg)
1523 case 4: // stmt: Store(reg,ptrreg)
1524 SetOperandsForMemInstr(ChooseStoreInstruction(
Chris Lattner54e898e2003-01-15 19:23:34 +00001525 subtreeRoot->leftChild()->getValue()->getType()),
1526 mvec, subtreeRoot, target);
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001527 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001528
1529 case 5: // stmt: BrUncond
1530 {
1531 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1532 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
1533
1534 // delay slot
1535 mvec.push_back(BuildMI(V9::NOP, 0));
1536 break;
1537 }
1538
1539 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001540 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001541 // If the constant is ZERO, we can use the branch-on-integer-register
1542 // instructions and avoid the SUBcc instruction entirely.
1543 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001544 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001545 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1546 assert(constNode &&
1547 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001548 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001549 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001550
Chris Lattner0c4e8862002-09-03 01:08:28 +00001551 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001552 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001553 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1554 && isValidConst)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001555 {
1556 // That constant is a zero after all...
1557 // Use the left child of setCC as the first argument!
1558 // Mark the setCC node so that no code is generated for it.
1559 InstructionNode* setCCNode = (InstructionNode*)
1560 subtreeRoot->leftChild();
1561 assert(setCCNode->getOpLabel() == SetCCOp);
1562 setCCNode->markFoldedIntoParent();
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001563
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001564 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001565
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001566 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1567 .addReg(setCCNode->leftChild()->getValue())
1568 .addPCDisp(brInst->getSuccessor(0));
1569 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001570
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001571 // delay slot
1572 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001573
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001574 // false branch
1575 mvec.push_back(BuildMI(V9::BA, 1)
1576 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001577
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001578 // delay slot
1579 mvec.push_back(BuildMI(V9::NOP, 0));
1580 break;
1581 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001582 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001583 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001584
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001585 case 6: // stmt: BrCond(setCC)
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001586 { // bool => boolean was computed with SetCC.
1587 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001588 // If it is an integer CC, we also need to find the unique
1589 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001590 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001591 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001592 bool isFPBranch;
Chris Lattner54e898e2003-01-15 19:23:34 +00001593 unsigned Opcode = ChooseBccInstruction(subtreeRoot, isFPBranch);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001594 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1595 brInst->getParent()->getParent(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001596 isFPBranch? Type::FloatTy : Type::IntTy,
1597 MachineCodeForInstruction::get(brInst));
Chris Lattner54e898e2003-01-15 19:23:34 +00001598 M = BuildMI(Opcode, 2).addCCReg(ccValue)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001599 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001600 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001601
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001602 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001603 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001604
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001605 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001606 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001607
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001608 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001609 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001610 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001611 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001612
1613 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001614 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001615 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001616 Constant* constVal =
1617 cast<Constant>(subtreeRoot->leftChild()->getValue());
1618 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001619
Misha Brukmana98cd452003-05-20 20:32:24 +00001620 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001621 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001622 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001623
1624 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001625 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001626 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001627 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001628
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001629 case 8: // stmt: BrCond(boolreg)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001630 { // boolreg => boolean is recorded in an integer register.
1631 // Use branch-on-integer-register instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001632 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001633 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001634 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001635 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001636 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001637
1638 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001639 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001640
1641 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001642 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001643
1644 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001645 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001646 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001647 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001648
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001649 case 9: // stmt: Switch(reg)
1650 assert(0 && "*** SWITCH instruction is not implemented yet.");
1651 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001652
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001653 case 10: // reg: VRegList(reg, reg)
1654 assert(0 && "VRegList should never be the topmost non-chain rule");
1655 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001656
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001657 case 21: // bool: Not(bool,reg): Both these are implemented as:
1658 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001659 { // First find the unary operand. It may be left or right, usually right.
1660 Value* notArg = BinaryOperator::getNotArgument(
1661 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001662 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001663 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001664 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001665 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001666 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001667
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001668 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001669 {
1670 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001671 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001672 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001673 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001674 }
1675
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001676 case 23: // reg: ToUByteTy(reg)
1677 case 24: // reg: ToSByteTy(reg)
1678 case 25: // reg: ToUShortTy(reg)
1679 case 26: // reg: ToShortTy(reg)
1680 case 27: // reg: ToUIntTy(reg)
1681 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001682 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001683 //======================================================================
1684 // Rules for integer conversions:
1685 //
1686 //--------
1687 // From ISO 1998 C++ Standard, Sec. 4.7:
1688 //
1689 // 2. If the destination type is unsigned, the resulting value is
1690 // the least unsigned integer congruent to the source integer
1691 // (modulo 2n where n is the number of bits used to represent the
1692 // unsigned type). [Note: In a two s complement representation,
1693 // this conversion is conceptual and there is no change in the
1694 // bit pattern (if there is no truncation). ]
1695 //
1696 // 3. If the destination type is signed, the value is unchanged if
1697 // it can be represented in the destination type (and bitfield width);
1698 // otherwise, the value is implementation-defined.
1699 //--------
1700 //
1701 // Since we assume 2s complement representations, this implies:
1702 //
1703 // -- if operand is smaller than destination, zero-extend or sign-extend
1704 // according to the signedness of the *operand*: source decides.
1705 // ==> we have to do nothing here!
1706 //
1707 // -- if operand is same size as or larger than destination, and the
1708 // destination is *unsigned*, zero-extend the operand: dest. decides
1709 //
1710 // -- if operand is same size as or larger than destination, and the
1711 // destination is *signed*, the choice is implementation defined:
1712 // we sign-extend the operand: i.e., again dest. decides.
1713 // Note: this matches both Sun's cc and gcc3.2.
1714 //======================================================================
1715
Vikram S. Adve242a8082002-05-19 15:25:51 +00001716 Instruction* destI = subtreeRoot->getInstruction();
1717 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001718 const Type* opType = opVal->getType();
Misha Brukman7b647942003-05-30 20:11:56 +00001719 if (opType->isIntegral() || isa<PointerType>(opType)) {
1720 unsigned opSize = target.getTargetData().getTypeSize(opType);
1721 unsigned destSize =
1722 target.getTargetData().getTypeSize(destI->getType());
1723 if (opSize >= destSize) {
1724 // Operand is same size as or larger than dest:
1725 // zero- or sign-extend, according to the signeddness of
1726 // the destination (see above).
1727 if (destI->getType()->isSigned())
1728 target.getInstrInfo().CreateSignExtensionInstructions(target,
Vikram S. Adve94c40812002-09-27 14:33:08 +00001729 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1730 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001731 else
Misha Brukman7b647942003-05-30 20:11:56 +00001732 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1733 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1734 mvec, MachineCodeForInstruction::get(destI));
1735 } else
1736 forwardOperandNum = 0; // forward first operand to user
1737 } else if (opType->isFloatingPoint()) {
1738 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1739 MachineCodeForInstruction::get(destI));
1740 if (destI->getType()->isUnsigned())
1741 maskUnsignedResult = true; // not handled by fp->int code
1742 } else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001743 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1744
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001745 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001746 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001747
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001748 case 29: // reg: ToULongTy(reg)
1749 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001750 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001751 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001752 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001753 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001754 forwardOperandNum = 0; // forward first operand to user
Misha Brukman7b647942003-05-30 20:11:56 +00001755 else if (opType->isFloatingPoint()) {
1756 Instruction* destI = subtreeRoot->getInstruction();
1757 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1758 MachineCodeForInstruction::get(destI));
1759 } else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001760 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001761 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001762 }
1763
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001764 case 31: // reg: ToFloatTy(reg):
1765 case 32: // reg: ToDoubleTy(reg):
1766 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001767
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001768 // If this instruction has a parent (a user) in the tree
1769 // and the user is translated as an FsMULd instruction,
1770 // then the cast is unnecessary. So check that first.
1771 // In the future, we'll want to do the same for the FdMULq instruction,
1772 // so do the check here instead of only for ToFloatTy(reg).
1773 //
1774 if (subtreeRoot->parent() != NULL) {
1775 const MachineCodeForInstruction& mcfi =
1776 MachineCodeForInstruction::get(
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001777 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001778 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
1779 forwardOperandNum = 0; // forward first operand to user
1780 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001781
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001782 if (forwardOperandNum != 0) { // we do need the cast
1783 Value* leftVal = subtreeRoot->leftChild()->getValue();
1784 const Type* opType = leftVal->getType();
1785 MachineOpCode opCode=ChooseConvertToFloatInstr(
1786 subtreeRoot->getOpLabel(), opType);
1787 if (opCode == V9::INVALID_OPCODE) { // no conversion needed
1788 forwardOperandNum = 0; // forward first operand to user
1789 } else {
1790 // If the source operand is a non-FP type it must be
1791 // first copied from int to float register via memory!
1792 Instruction *dest = subtreeRoot->getInstruction();
1793 Value* srcForCast;
1794 int n = 0;
1795 if (! opType->isFloatingPoint()) {
1796 // Create a temporary to represent the FP register
1797 // into which the integer will be copied via memory.
1798 // The type of this temporary will determine the FP
1799 // register used: single-prec for a 32-bit int or smaller,
1800 // double-prec for a 64-bit int.
1801 //
1802 uint64_t srcSize =
1803 target.getTargetData().getTypeSize(leftVal->getType());
1804 Type* tmpTypeToUse =
1805 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001806 MachineCodeForInstruction &destMCFI =
1807 MachineCodeForInstruction::get(dest);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001808 srcForCast = new TmpInstruction(destMCFI, tmpTypeToUse, dest);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001809
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001810 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001811 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001812 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001813 mvec, destMCFI);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001814 } else
1815 srcForCast = leftVal;
1816
1817 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
1818 mvec.push_back(M);
1819 }
Misha Brukman7b647942003-05-30 20:11:56 +00001820 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001821 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001822
1823 case 19: // reg: ToArrayTy(reg):
1824 case 20: // reg: ToPointerTy(reg):
1825 forwardOperandNum = 0; // forward first operand to user
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001826 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001827
1828 case 233: // reg: Add(reg, Constant)
1829 maskUnsignedResult = true;
1830 M = CreateAddConstInstruction(subtreeRoot);
1831 if (M != NULL) {
1832 mvec.push_back(M);
1833 break;
1834 }
1835 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001836
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001837 case 33: // reg: Add(reg, reg)
1838 maskUnsignedResult = true;
1839 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
1840 break;
1841
1842 case 234: // reg: Sub(reg, Constant)
1843 maskUnsignedResult = true;
1844 M = CreateSubConstInstruction(subtreeRoot);
1845 if (M != NULL) {
1846 mvec.push_back(M);
1847 break;
1848 }
1849 // ELSE FALL THROUGH
1850
1851 case 34: // reg: Sub(reg, reg)
1852 maskUnsignedResult = true;
1853 Add3OperandInstr(ChooseSubInstructionByType(
Chris Lattner54e898e2003-01-15 19:23:34 +00001854 subtreeRoot->getInstruction()->getType()),
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001855 subtreeRoot, mvec);
1856 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001857
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001858 case 135: // reg: Mul(todouble, todouble)
1859 checkCast = true;
1860 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001861
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001862 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001863 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001864 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001865 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00001866 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00001867 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001868 Instruction* mulInstr = subtreeRoot->getInstruction();
1869 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001870 subtreeRoot->leftChild()->getValue(),
1871 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001872 mulInstr, mvec,
1873 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001874 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001875 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001876 case 335: // reg: Mul(todouble, todoubleConst)
1877 checkCast = true;
1878 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001879
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001880 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001881 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001882 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001883 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00001884 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00001885 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001886 Instruction* mulInstr = subtreeRoot->getInstruction();
1887 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001888 subtreeRoot->leftChild()->getValue(),
1889 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001890 mulInstr, mvec,
1891 MachineCodeForInstruction::get(mulInstr),
1892 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001893 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001894 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001895 case 236: // reg: Div(reg, Constant)
1896 maskUnsignedResult = true;
1897 L = mvec.size();
1898 CreateDivConstInstruction(target, subtreeRoot, mvec);
1899 if (mvec.size() > L)
1900 break;
1901 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001902
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001903 case 36: // reg: Div(reg, reg)
1904 maskUnsignedResult = true;
1905 Add3OperandInstr(ChooseDivInstruction(target, subtreeRoot),
1906 subtreeRoot, mvec);
1907 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001908
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001909 case 37: // reg: Rem(reg, reg)
1910 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001911 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001912 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001913 Instruction* remInstr = subtreeRoot->getInstruction();
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001914
1915 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(remInstr);
1916 TmpInstruction* quot = new TmpInstruction(mcfi,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001917 subtreeRoot->leftChild()->getValue(),
1918 subtreeRoot->rightChild()->getValue());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001919 TmpInstruction* prod = new TmpInstruction(mcfi,
Vikram S. Adve510eec72001-11-04 21:59:14 +00001920 quot,
1921 subtreeRoot->rightChild()->getValue());
Vikram S. Adve510eec72001-11-04 21:59:14 +00001922
Chris Lattner54e898e2003-01-15 19:23:34 +00001923 M = BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
1924 .addReg(subtreeRoot->leftChild()->getValue())
1925 .addReg(subtreeRoot->rightChild()->getValue())
1926 .addRegDef(quot);
Vikram S. Adve74825322002-03-18 03:15:35 +00001927 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001928
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001929 unsigned MulOpcode =
1930 ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType());
1931 Value *MulRHS = subtreeRoot->rightChild()->getValue();
1932 M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod,
1933 MOTy::Def);
Vikram S. Adve74825322002-03-18 03:15:35 +00001934 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001935
Chris Lattner54e898e2003-01-15 19:23:34 +00001936 unsigned Opcode = ChooseSubInstructionByType(
1937 subtreeRoot->getInstruction()->getType());
1938 M = BuildMI(Opcode, 3).addReg(subtreeRoot->leftChild()->getValue())
1939 .addReg(prod).addRegDef(subtreeRoot->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001940 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001941 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001942 }
1943
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001944 case 38: // bool: And(bool, bool)
1945 case 238: // bool: And(bool, boolconst)
1946 case 338: // reg : BAnd(reg, reg)
1947 case 538: // reg : BAnd(reg, Constant)
1948 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
1949 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001950
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001951 case 138: // bool: And(bool, not)
1952 case 438: // bool: BAnd(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001953 { // Use the argument of NOT as the second argument!
1954 // Mark the NOT node so that no code is generated for it.
1955 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1956 Value* notArg = BinaryOperator::getNotArgument(
1957 cast<BinaryOperator>(notNode->getInstruction()));
1958 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001959 Value *LHS = subtreeRoot->leftChild()->getValue();
1960 Value *Dest = subtreeRoot->getValue();
Misha Brukman91aee472003-05-27 22:37:00 +00001961 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(LHS).addReg(notArg)
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001962 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001963 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001964 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001965
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001966 case 39: // bool: Or(bool, bool)
1967 case 239: // bool: Or(bool, boolconst)
1968 case 339: // reg : BOr(reg, reg)
1969 case 539: // reg : BOr(reg, Constant)
1970 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
1971 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001972
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001973 case 139: // bool: Or(bool, not)
1974 case 439: // bool: BOr(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001975 { // Use the argument of NOT as the second argument!
1976 // Mark the NOT node so that no code is generated for it.
1977 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1978 Value* notArg = BinaryOperator::getNotArgument(
1979 cast<BinaryOperator>(notNode->getInstruction()));
1980 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001981 Value *LHS = subtreeRoot->leftChild()->getValue();
1982 Value *Dest = subtreeRoot->getValue();
Misha Brukman91aee472003-05-27 22:37:00 +00001983 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(LHS).addReg(notArg)
Misha Brukmana98cd452003-05-20 20:32:24 +00001984 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001985 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001986 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001987
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001988 case 40: // bool: Xor(bool, bool)
1989 case 240: // bool: Xor(bool, boolconst)
1990 case 340: // reg : BXor(reg, reg)
1991 case 540: // reg : BXor(reg, Constant)
1992 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
1993 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001994
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001995 case 140: // bool: Xor(bool, not)
1996 case 440: // bool: BXor(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001997 { // Use the argument of NOT as the second argument!
1998 // Mark the NOT node so that no code is generated for it.
1999 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2000 Value* notArg = BinaryOperator::getNotArgument(
2001 cast<BinaryOperator>(notNode->getInstruction()));
2002 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002003 Value *LHS = subtreeRoot->leftChild()->getValue();
2004 Value *Dest = subtreeRoot->getValue();
Misha Brukman91aee472003-05-27 22:37:00 +00002005 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(LHS).addReg(notArg)
Misha Brukmana98cd452003-05-20 20:32:24 +00002006 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002007 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002008 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002009
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002010 case 41: // boolconst: SetCC(reg, Constant)
2011 //
2012 // If the SetCC was folded into the user (parent), it will be
2013 // caught above. All other cases are the same as case 42,
2014 // so just fall through.
2015 //
2016 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002017 {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002018 // This generates a SUBCC instruction, putting the difference in a
2019 // result reg. if needed, and/or setting a condition code if needed.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002020 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002021 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002022 Value* leftVal = subtreeRoot->leftChild()->getValue();
2023 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002024
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002025 // If the boolean result of the SetCC is used outside the current basic
2026 // block (so it must be computed as a boolreg) or is used by anything
2027 // other than a branch, the boolean must be computed and stored
2028 // in a result register. We will use a conditional move to do this.
2029 //
2030 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2031 ! AllUsesAreBranches(setCCInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002032
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002033 // A TmpInstruction is created to represent the CC "result".
2034 // Unlike other instances of TmpInstruction, this one is used
2035 // by machine code of multiple LLVM instructions, viz.,
2036 // the SetCC and the branch. Make sure to get the same one!
2037 // Note that we do this even for FP CC registers even though they
2038 // are explicit operands, because the type of the operand
2039 // needs to be a floating point condition code, not an integer
2040 // condition code. Think of this as casting the bool result to
2041 // a FP condition code register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002042 // Later, we mark the 4th operand as being a CC register, and as a def.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002043 //
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002044 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002045 setCCInstr->getParent()->getParent(),
2046 isFPCompare ? Type::FloatTy : Type::IntTy,
2047 MachineCodeForInstruction::get(setCCInstr));
Misha Brukman7b647942003-05-30 20:11:56 +00002048 if (! isFPCompare) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002049 // Integer condition: set CC and discard result.
2050 M = BuildMI(V9::SUBccr, 4)
2051 .addReg(subtreeRoot->leftChild()->getValue())
2052 .addReg(subtreeRoot->rightChild()->getValue())
2053 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def)
2054 .addCCReg(tmpForCC, MOTy::Def);
Misha Brukman7b647942003-05-30 20:11:56 +00002055 } else {
2056 // FP condition: dest of FCMP should be some FCCn register
2057 M = BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2058 .addCCReg(tmpForCC, MOTy::Def)
2059 .addReg(subtreeRoot->leftChild()->getValue())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002060 .addReg(subtreeRoot->rightChild()->getValue());
Misha Brukman7b647942003-05-30 20:11:56 +00002061 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002062 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002063
Misha Brukman7b647942003-05-30 20:11:56 +00002064 if (computeBoolVal) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002065 MachineOpCode movOpCode = (isFPCompare
Misha Brukmaneecdb662003-06-02 20:55:14 +00002066 ? ChooseMovFpcciInstruction(subtreeRoot)
2067 : ChooseMovpcciAfterSub(subtreeRoot));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002068
2069 // Unconditionally set register to 0
2070 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
2071 mvec.push_back(M);
2072
2073 // Now conditionally move 1 into the register.
Misha Brukman7b647942003-05-30 20:11:56 +00002074 // Mark the register as a use (as well as a def) because the old
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002075 // value will be retained if the condition is false.
2076 M = (BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(1)
2077 .addReg(setCCInstr, MOTy::UseAndDef));
Misha Brukman7b647942003-05-30 20:11:56 +00002078 mvec.push_back(M);
2079 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002080 break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002081 }
2082
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002083 case 51: // reg: Load(reg)
2084 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002085 SetOperandsForMemInstr(ChooseLoadInstruction(
2086 subtreeRoot->getValue()->getType()),
2087 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002088 break;
2089
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002090 case 55: // reg: GetElemPtr(reg)
2091 case 56: // reg: GetElemPtrIdx(reg,reg)
2092 // If the GetElemPtr was folded into the user (parent), it will be
2093 // caught above. For other cases, we have to compute the address.
2094 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
2095 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002096
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002097 case 57: // reg: Alloca: Implement as 1 instruction:
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002098 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002099 AllocationInst* instr =
2100 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002101 unsigned tsize =
2102 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002103 assert(tsize != 0);
2104 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002105 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002106 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002107
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002108 case 58: // reg: Alloca(reg): Implement as 3 instructions:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002109 // mul num, typeSz -> tmp
2110 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002111 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002112 AllocationInst* instr =
2113 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002114 const Type* eltType = instr->getAllocatedType();
2115
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002116 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002117 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002118 Value* numElementsVal = NULL;
2119 bool isArray = instr->isArrayAllocation();
2120
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002121 if (!isArray || isa<Constant>(numElementsVal = instr->getArraySize())) {
Misha Brukman7b647942003-05-30 20:11:56 +00002122 // total size is constant: generate code for fixed-size alloca
2123 unsigned numElements = isArray?
2124 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2125 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2126 numElements, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002127 } else {
2128 // total size is not constant.
Vikram S. Adve74825322002-03-18 03:15:35 +00002129 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002130 numElementsVal, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002131 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002132 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002133 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002134
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002135 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002136 { // Generate a direct (CALL) or indirect (JMPL) call.
2137 // Mark the return-address register, the indirection
2138 // register (for indirect calls), the operands of the Call,
2139 // and the return value (if any) as implicit operands
2140 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002141 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002142 // If this is a varargs function, floating point arguments
2143 // have to passed in integer registers so insert
2144 // copy-float-to-int instructions for each float operand.
2145 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002146 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002147 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002148 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002149
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002150 // Check if this is an intrinsic function that needs a special code
2151 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002152 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002153 bool specialIntrinsic = false;
2154 LLVMIntrinsic::ID iid;
2155 if (calledFunc && (iid=(LLVMIntrinsic::ID)calledFunc->getIntrinsicID()))
2156 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002157
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002158 // If not, generate the normal call sequence for the function.
2159 // This can also handle any intrinsics that are just function calls.
2160 //
Misha Brukman7b647942003-05-30 20:11:56 +00002161 if (! specialIntrinsic) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002162 MachineFunction& MF =
2163 MachineFunction::get(callInstr->getParent()->getParent());
2164 MachineCodeForInstruction& mcfi =
2165 MachineCodeForInstruction::get(callInstr);
2166 const UltraSparcRegInfo& regInfo =
2167 (UltraSparcRegInfo&) target.getRegInfo();
2168 const TargetFrameInfo& frameInfo = target.getFrameInfo();
2169
Misha Brukman7b647942003-05-30 20:11:56 +00002170 // Create hidden virtual register for return address with type void*
2171 TmpInstruction* retAddrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002172 new TmpInstruction(mcfi, PointerType::get(Type::VoidTy), callInstr);
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002173
Misha Brukman7b647942003-05-30 20:11:56 +00002174 // Generate the machine instruction and its operands.
2175 // Use CALL for direct function calls; this optimistically assumes
2176 // the PC-relative address fits in the CALL address field (22 bits).
2177 // Use JMPL for indirect calls.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002178 // This will be added to mvec later, after operand copies.
Misha Brukman7b647942003-05-30 20:11:56 +00002179 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002180 MachineInstr* callMI;
Misha Brukman7b647942003-05-30 20:11:56 +00002181 if (calledFunc) // direct function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002182 callMI = BuildMI(V9::CALL, 1).addPCDisp(callee);
Misha Brukman7b647942003-05-30 20:11:56 +00002183 else // indirect function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002184 callMI = (BuildMI(V9::JMPLCALLi,3).addReg(callee)
2185 .addSImm((int64_t)0).addRegDef(retAddrReg));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002186
Misha Brukman7b647942003-05-30 20:11:56 +00002187 const FunctionType* funcType =
2188 cast<FunctionType>(cast<PointerType>(callee->getType())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002189 ->getElementType());
Misha Brukman7b647942003-05-30 20:11:56 +00002190 bool isVarArgs = funcType->isVarArg();
2191 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002192
Misha Brukman7b647942003-05-30 20:11:56 +00002193 // Use a descriptor to pass information about call arguments
2194 // to the register allocator. This descriptor will be "owned"
2195 // and freed automatically when the MachineCodeForInstruction
2196 // object for the callInstr goes away.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002197 CallArgsDescriptor* argDesc =
2198 new CallArgsDescriptor(callInstr, retAddrReg,isVarArgs,noPrototype);
Misha Brukman7b647942003-05-30 20:11:56 +00002199 assert(callInstr->getOperand(0) == callee
2200 && "This is assumed in the loop below!");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002201
2202 // Insert copy instructions to get all the arguments into
2203 // all the places that they need to be.
2204 //
Misha Brukman7b647942003-05-30 20:11:56 +00002205 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002206 int argNo = i-1;
Misha Brukman7b647942003-05-30 20:11:56 +00002207 Value* argVal = callInstr->getOperand(i);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002208 const Type* argType = argVal->getType();
2209 unsigned regType = regInfo.getRegType(argType);
2210 unsigned argSize = target.getTargetData().getTypeSize(argType);
2211 int regNumForArg = TargetRegInfo::getInvalidRegNum();
2212 unsigned regClassIDOfArgReg;
2213 CallArgInfo& argInfo = argDesc->getArgInfo(argNo);
2214
Misha Brukman7b647942003-05-30 20:11:56 +00002215 // Check for FP arguments to varargs functions.
2216 // Any such argument in the first $K$ args must be passed in an
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002217 // integer register. If there is no prototype, it must also
2218 // be passed as an FP register.
2219 // K = #integer argument registers.
2220 bool isFPArg = argVal->getType()->isFloatingPoint();
2221 if (isVarArgs && isFPArg) {
Misha Brukman7b647942003-05-30 20:11:56 +00002222 // If it is a function with no prototype, pass value
2223 // as an FP value as well as a varargs value
2224 if (noPrototype)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002225 argInfo.setUseFPArgReg();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002226
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002227 // If this arg. is in the first $K$ regs, add copy-
2228 // float-to-int instructions to pass the value as an int.
2229 // To check if it is in teh first $K$, get the register
2230 // number for the arg #i.
Misha Brukmanea481cc2003-06-03 03:21:58 +00002231 int copyRegNum = regInfo.regNumForIntArg(false, false, argNo,
2232 regClassIDOfArgReg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002233 if (copyRegNum != regInfo.getInvalidRegNum()) {
2234 // Create a virtual register to represent copyReg. Mark
2235 // this vreg as being an implicit operand of the call MI
2236 const Type* loadTy = (argType == Type::FloatTy
2237 ? Type::IntTy : Type::LongTy);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002238 TmpInstruction* argVReg = new TmpInstruction(mcfi, loadTy,
2239 argVal, NULL,
2240 "argRegCopy");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002241 callMI->addImplicitRef(argVReg);
2242
2243 // Get a temp stack location to use to copy
2244 // float-to-int via the stack.
2245 //
2246 // FIXME: For now, we allocate permanent space because
2247 // the stack frame manager does not allow locals to be
2248 // allocated (e.g., for alloca) after a temp is
2249 // allocated!
2250 //
2251 // int tmpOffset = MF.getInfo()->pushTempValue(argSize);
2252 int tmpOffset = MF.getInfo()->allocateLocalVar(argVReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002253
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002254 // Generate the store from FP reg to stack
Misha Brukmanea481cc2003-06-03 03:21:58 +00002255 unsigned StoreOpcode = ChooseStoreInstruction(argType);
2256 M = BuildMI(convertOpcodeFromRegToImm(StoreOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002257 .addReg(argVal).addMReg(regInfo.getFramePointer())
2258 .addSImm(tmpOffset);
2259 mvec.push_back(M);
2260
2261 // Generate the load from stack to int arg reg
Misha Brukmanea481cc2003-06-03 03:21:58 +00002262 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
2263 M = BuildMI(convertOpcodeFromRegToImm(LoadOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002264 .addMReg(regInfo.getFramePointer()).addSImm(tmpOffset)
2265 .addReg(argVReg, MOTy::Def);
2266
2267 // Mark operand with register it should be assigned
2268 // both for copy and for the callMI
2269 M->SetRegForOperand(M->getNumOperands()-1, copyRegNum);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002270 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2271 copyRegNum);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002272 mvec.push_back(M);
2273
2274 // Add info about the argument to the CallArgsDescriptor
2275 argInfo.setUseIntArgReg();
2276 argInfo.setArgCopy(copyRegNum);
2277 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00002278 // Cannot fit in first $K$ regs so pass arg on stack
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002279 argInfo.setUseStackSlot();
2280 }
2281 } else if (isFPArg) {
2282 // Get the outgoing arg reg to see if there is one.
2283 regNumForArg = regInfo.regNumForFPArg(regType, false, false,
2284 argNo, regClassIDOfArgReg);
2285 if (regNumForArg == regInfo.getInvalidRegNum())
2286 argInfo.setUseStackSlot();
2287 else {
2288 argInfo.setUseFPArgReg();
2289 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2290 regNumForArg);
2291 }
2292 } else {
2293 // Get the outgoing arg reg to see if there is one.
2294 regNumForArg = regInfo.regNumForIntArg(false,false,
2295 argNo, regClassIDOfArgReg);
2296 if (regNumForArg == regInfo.getInvalidRegNum())
2297 argInfo.setUseStackSlot();
2298 else {
2299 argInfo.setUseIntArgReg();
2300 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2301 regNumForArg);
2302 }
2303 }
2304
2305 //
2306 // Now insert copy instructions to stack slot or arg. register
2307 //
2308 if (argInfo.usesStackSlot()) {
2309 // Get the stack offset for this argument slot.
2310 // FP args on stack are right justified so adjust offset!
2311 // int arguments are also right justified but they are
2312 // always loaded as a full double-word so the offset does
2313 // not need to be adjusted.
2314 int argOffset = frameInfo.getOutgoingArgOffset(MF, argNo);
2315 if (argType->isFloatingPoint()) {
2316 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
2317 assert(argSize <= slotSize && "Insufficient slot size!");
2318 argOffset += slotSize - argSize;
2319 }
2320
2321 // Now generate instruction to copy argument to stack
2322 MachineOpCode storeOpCode =
2323 (argType->isFloatingPoint()
2324 ? ((argSize == 4)? V9::STFi : V9::STDFi) : V9::STXi);
2325
2326 M = BuildMI(storeOpCode, 3).addReg(argVal)
2327 .addMReg(regInfo.getStackPointer()).addSImm(argOffset);
2328 mvec.push_back(M);
2329 } else {
2330 // Create a virtual register to represent the arg reg. Mark
2331 // this vreg as being an implicit operand of the call MI.
2332 TmpInstruction* argVReg =
2333 new TmpInstruction(mcfi, argVal, NULL, "argReg");
2334
2335 callMI->addImplicitRef(argVReg);
2336
2337 // Generate the reg-to-reg copy into the outgoing arg reg.
2338 // -- For FP values, create a FMOVS or FMOVD instruction
2339 // -- For non-FP values, create an add-with-0 instruction
2340 if (argType->isFloatingPoint())
2341 M=(BuildMI(argType==Type::FloatTy? V9::FMOVS :V9::FMOVD,2)
2342 .addReg(argVal).addReg(argVReg, MOTy::Def));
2343 else
2344 M = (BuildMI(ChooseAddInstructionByType(argType), 3)
2345 .addReg(argVal).addSImm((int64_t) 0)
2346 .addReg(argVReg, MOTy::Def));
2347
2348 // Mark the operand with the register it should be assigned
2349 M->SetRegForOperand(M->getNumOperands()-1, regNumForArg);
2350 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2351 regNumForArg);
2352
2353 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +00002354 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002355 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002356
2357 // add call instruction and delay slot before copying return value
2358 mvec.push_back(callMI);
2359 mvec.push_back(BuildMI(V9::NOP, 0));
2360
Misha Brukman7b647942003-05-30 20:11:56 +00002361 // Add the return value as an implicit ref. The call operands
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002362 // were added above. Also, add code to copy out the return value.
2363 // This is always register-to-register for int or FP return values.
2364 //
2365 if (callInstr->getType() != Type::VoidTy) {
2366 // Get the return value reg.
2367 const Type* retType = callInstr->getType();
2368
2369 int regNum = (retType->isFloatingPoint()
2370 ? (unsigned) SparcFloatRegClass::f0
2371 : (unsigned) SparcIntRegClass::o0);
2372 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
2373 regNum = regInfo.getUnifiedRegNum(regClassID, regNum);
2374
2375 // Create a virtual register to represent it and mark
2376 // this vreg as being an implicit operand of the call MI
2377 TmpInstruction* retVReg =
2378 new TmpInstruction(mcfi, callInstr, NULL, "argReg");
2379
2380 callMI->addImplicitRef(retVReg, /*isDef*/ true);
2381
2382 // Generate the reg-to-reg copy from the return value reg.
2383 // -- For FP values, create a FMOVS or FMOVD instruction
2384 // -- For non-FP values, create an add-with-0 instruction
2385 if (retType->isFloatingPoint())
2386 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
2387 .addReg(retVReg).addReg(callInstr, MOTy::Def));
2388 else
2389 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
2390 .addReg(retVReg).addSImm((int64_t) 0)
2391 .addReg(callInstr, MOTy::Def));
2392
2393 // Mark the operand with the register it should be assigned
2394 // Also mark the implicit ref of the call defining this operand
2395 M->SetRegForOperand(0, regNum);
2396 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,regNum);
2397
2398 mvec.push_back(M);
2399 }
2400
Misha Brukman7b647942003-05-30 20:11:56 +00002401 // For the CALL instruction, the ret. addr. reg. is also implicit
2402 if (isa<Function>(callee))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002403 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
2404
2405 MF.getInfo()->popAllTempValues(); // free temps used for this inst
Misha Brukman7b647942003-05-30 20:11:56 +00002406 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002407
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002408 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002409 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002410
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002411 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002412 {
2413 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2414 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2415 Instruction* shlInstr = subtreeRoot->getInstruction();
2416
2417 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002418 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2419 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002420
2421 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Misha Brukman91aee472003-05-27 22:37:00 +00002422 (opType == Type::LongTy)? V9::SLLXr6:V9::SLLr6,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002423 argVal1, argVal2, 0, shlInstr, mvec,
2424 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002425 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002426 }
2427
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002428 case 63: // reg: Shr(reg, reg)
Misha Brukman7b647942003-05-30 20:11:56 +00002429 {
2430 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002431 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2432 "Shr unsupported for other types");
Chris Lattner54e898e2003-01-15 19:23:34 +00002433 Add3OperandInstr(opType->isSigned()
Misha Brukman91aee472003-05-27 22:37:00 +00002434 ? (opType == Type::LongTy ? V9::SRAXr6 : V9::SRAr6)
2435 : (opType == Type::LongTy ? V9::SRLXr6 : V9::SRLr6),
Chris Lattner54e898e2003-01-15 19:23:34 +00002436 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002437 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002438 }
2439
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002440 case 64: // reg: Phi(reg,reg)
2441 break; // don't forward the value
Vikram S. Adve74825322002-03-18 03:15:35 +00002442
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002443 case 65: // reg: VaArg(reg)
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002444 {
2445 // Use value initialized by va_start as pointer to args on the stack.
2446 // Load argument via current pointer value, then increment pointer.
2447 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
2448 Instruction* vaArgI = subtreeRoot->getInstruction();
Misha Brukman91aee472003-05-27 22:37:00 +00002449 mvec.push_back(BuildMI(V9::LDXi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002450 addSImm(0).addRegDef(vaArgI));
Misha Brukman91aee472003-05-27 22:37:00 +00002451 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002452 addSImm(argSize).addRegDef(vaArgI->getOperand(0)));
2453 break;
2454 }
2455
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002456 case 71: // reg: VReg
2457 case 72: // reg: Constant
2458 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002459
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002460 default:
2461 assert(0 && "Unrecognized BURG rule");
2462 break;
2463 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002464 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002465
Misha Brukman7b647942003-05-30 20:11:56 +00002466 if (forwardOperandNum >= 0) {
2467 // We did not generate a machine instruction but need to use operand.
2468 // If user is in the same tree, replace Value in its machine operand.
2469 // If not, insert a copy instruction which should get coalesced away
2470 // by register allocation.
2471 if (subtreeRoot->parent() != NULL)
2472 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2473 else {
2474 std::vector<MachineInstr*> minstrVec;
2475 Instruction* instr = subtreeRoot->getInstruction();
2476 target.getInstrInfo().
2477 CreateCopyInstructionsByType(target,
2478 instr->getParent()->getParent(),
2479 instr->getOperand(forwardOperandNum),
2480 instr, minstrVec,
2481 MachineCodeForInstruction::get(instr));
2482 assert(minstrVec.size() > 0);
2483 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Chris Lattner20b1ea02001-09-14 03:47:57 +00002484 }
Misha Brukman7b647942003-05-30 20:11:56 +00002485 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002486
Misha Brukman7b647942003-05-30 20:11:56 +00002487 if (maskUnsignedResult) {
2488 // If result is unsigned and smaller than int reg size,
2489 // we need to clear high bits of result value.
2490 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2491 Instruction* dest = subtreeRoot->getInstruction();
2492 if (dest->getType()->isUnsigned()) {
2493 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
2494 if (destSize <= 4) {
2495 // Mask high bits. Use a TmpInstruction to represent the
2496 // intermediate result before masking. Since those instructions
2497 // have already been generated, go back and substitute tmpI
2498 // for dest in the result position of each one of them.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002499 TmpInstruction *tmpI =
2500 new TmpInstruction(MachineCodeForInstruction::get(dest),
2501 dest->getType(), dest, NULL, "maskHi");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002502
Misha Brukman7b647942003-05-30 20:11:56 +00002503 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2504 mvec[i]->substituteValue(dest, tmpI);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002505
Misha Brukman7b647942003-05-30 20:11:56 +00002506 M = BuildMI(V9::SRLi6, 3).addReg(tmpI).addZImm(8*(4-destSize))
2507 .addReg(dest, MOTy::Def);
2508 mvec.push_back(M);
2509 } else if (destSize < 8) {
2510 assert(0 && "Unsupported type size: 32 < size < 64 bits");
2511 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002512 }
Misha Brukman7b647942003-05-30 20:11:56 +00002513 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002514}