blob: 853da7e8e47a42ca83ae0eb5457cc160b3a91cb8 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner035dfbe2002-08-09 20:08:06 +00009//
10// BURS instruction selection for SPARC V9 architecture.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +000013
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000015#include "llvm/ConstantHandling.h"
Misha Brukman34943292003-10-22 05:09:56 +000016#include "llvm/DerivedTypes.h"
17#include "llvm/Instructions.h"
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +000018#include "llvm/Intrinsics.h"
Misha Brukman34943292003-10-22 05:09:56 +000019#include "llvm/Module.h"
20#include "llvm/CodeGen/InstrForest.h"
21#include "llvm/CodeGen/InstrSelection.h"
22#include "llvm/CodeGen/InstrSelectionSupport.h"
23#include "llvm/CodeGen/MachineCodeForInstruction.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFunctionInfo.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineInstrAnnot.h"
Misha Brukmand71295a2003-12-17 22:04:00 +000028#include "SparcInstrSelectionSupport.h"
29#include "SparcInternals.h"
30#include "SparcRegClassInfo.h"
31#include "SparcRegInfo.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000032#include "Support/MathExtras.h"
Vikram S. Adve951df2b2003-07-10 20:07:54 +000033#include <algorithm>
Misha Brukman34943292003-10-22 05:09:56 +000034#include <cmath>
Chris Lattner20b1ea02001-09-14 03:47:57 +000035
Brian Gaeked0fde302003-11-11 22:41:34 +000036namespace llvm {
37
Chris Lattner54e898e2003-01-15 19:23:34 +000038static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +000039 std::vector<MachineInstr*>& mvec) {
Chris Lattner54e898e2003-01-15 19:23:34 +000040 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
41 .addReg(Node->rightChild()->getValue())
42 .addRegDef(Node->getValue()));
43}
44
45
Chris Lattner795ba6c2003-01-15 21:36:50 +000046//---------------------------------------------------------------------------
47// Function: FoldGetElemChain
48//
49// Purpose:
50// Fold a chain of GetElementPtr instructions containing only
51// constant offsets into an equivalent (Pointer, IndexVector) pair.
52// Returns the pointer Value, and stores the resulting IndexVector
53// in argument chainIdxVec. This is a helper function for
54// FoldConstantIndices that does the actual folding.
55//---------------------------------------------------------------------------
56
57
58// Check for a constant 0.
59inline bool
60IsZero(Value* idx)
61{
62 return (idx == ConstantSInt::getNullValue(idx->getType()));
63}
64
65static Value*
Misha Brukmanee563cb2003-05-21 17:59:06 +000066FoldGetElemChain(InstrTreeNode* ptrNode, std::vector<Value*>& chainIdxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +000067 bool lastInstHasLeadingNonZero)
68{
69 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
70 GetElementPtrInst* gepInst =
71 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
72
73 // ptr value is not computed in this tree or ptr value does not come from GEP
74 // instruction
75 if (gepInst == NULL)
76 return NULL;
77
78 // Return NULL if we don't fold any instructions in.
79 Value* ptrVal = NULL;
80
81 // Now chase the chain of getElementInstr instructions, if any.
82 // Check for any non-constant indices and stop there.
83 // Also, stop if the first index of child is a non-zero array index
84 // and the last index of the current node is a non-array index:
85 // in that case, a non-array declared type is being accessed as an array
86 // which is not type-safe, but could be legal.
87 //
88 InstructionNode* ptrChild = gepNode;
89 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
90 ptrChild->getOpLabel() == GetElemPtrIdx))
Misha Brukman81b06862003-05-21 18:48:06 +000091 {
92 // Child is a GetElemPtr instruction
93 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
94 User::op_iterator OI, firstIdx = gepInst->idx_begin();
95 User::op_iterator lastIdx = gepInst->idx_end();
96 bool allConstantOffsets = true;
Chris Lattner795ba6c2003-01-15 21:36:50 +000097
Misha Brukman81b06862003-05-21 18:48:06 +000098 // The first index of every GEP must be an array index.
99 assert((*firstIdx)->getType() == Type::LongTy &&
100 "INTERNAL ERROR: Structure index for a pointer type!");
Chris Lattner795ba6c2003-01-15 21:36:50 +0000101
Misha Brukman81b06862003-05-21 18:48:06 +0000102 // If the last instruction had a leading non-zero index, check if the
103 // current one references a sequential (i.e., indexable) type.
104 // If not, the code is not type-safe and we would create an illegal GEP
105 // by folding them, so don't fold any more instructions.
106 //
107 if (lastInstHasLeadingNonZero)
108 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
109 break; // cannot fold in any preceding getElementPtr instrs.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000110
Misha Brukman81b06862003-05-21 18:48:06 +0000111 // Check that all offsets are constant for this instruction
112 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
113 allConstantOffsets = isa<ConstantInt>(*OI);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000114
Misha Brukman81b06862003-05-21 18:48:06 +0000115 if (allConstantOffsets) {
116 // Get pointer value out of ptrChild.
117 ptrVal = gepInst->getPointerOperand();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000118
Misha Brukman81b06862003-05-21 18:48:06 +0000119 // Insert its index vector at the start, skipping any leading [0]
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000120 // Remember the old size to check if anything was inserted.
121 unsigned oldSize = chainIdxVec.size();
122 int firstIsZero = IsZero(*firstIdx);
123 chainIdxVec.insert(chainIdxVec.begin(), firstIdx + firstIsZero, lastIdx);
124
125 // Remember if it has leading zero index: it will be discarded later.
126 if (oldSize < chainIdxVec.size())
127 lastInstHasLeadingNonZero = !firstIsZero;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000128
Misha Brukman81b06862003-05-21 18:48:06 +0000129 // Mark the folded node so no code is generated for it.
130 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000131
Misha Brukman81b06862003-05-21 18:48:06 +0000132 // Get the previous GEP instruction and continue trying to fold
133 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
134 } else // cannot fold this getElementPtr instr. or any preceding ones
135 break;
136 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000137
138 // If the first getElementPtr instruction had a leading [0], add it back.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000139 // Note that this instruction is the *last* one that was successfully
140 // folded *and* contributed any indices, in the loop above.
141 //
Chris Lattner795ba6c2003-01-15 21:36:50 +0000142 if (ptrVal && ! lastInstHasLeadingNonZero)
143 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
144
145 return ptrVal;
146}
147
148
149//---------------------------------------------------------------------------
150// Function: GetGEPInstArgs
151//
152// Purpose:
153// Helper function for GetMemInstArgs that handles the final getElementPtr
154// instruction used by (or same as) the memory operation.
155// Extracts the indices of the current instruction and tries to fold in
156// preceding ones if all indices of the current one are constant.
157//---------------------------------------------------------------------------
158
159static Value *
160GetGEPInstArgs(InstructionNode* gepNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000161 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000162 bool& allConstantIndices)
163{
164 allConstantIndices = true;
165 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
166
167 // Default pointer is the one from the current instruction.
168 Value* ptrVal = gepI->getPointerOperand();
169 InstrTreeNode* ptrChild = gepNode->leftChild();
170
Misha Brukman452db672003-09-23 17:28:11 +0000171 // Extract the index vector of the GEP instruction.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000172 // If all indices are constant and first index is zero, try to fold
173 // in preceding GEPs with all constant indices.
174 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
175 allConstantIndices && OI != OE; ++OI)
176 if (! isa<Constant>(*OI))
177 allConstantIndices = false; // note: this also terminates loop!
178
179 // If we have only constant indices, fold chains of constant indices
180 // in this and any preceding GetElemPtr instructions.
181 bool foldedGEPs = false;
182 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
183 if (allConstantIndices)
Misha Brukman81b06862003-05-21 18:48:06 +0000184 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) {
185 ptrVal = newPtr;
186 foldedGEPs = true;
187 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000188
189 // Append the index vector of the current instruction.
190 // Skip the leading [0] index if preceding GEPs were folded into this.
191 idxVec.insert(idxVec.end(),
192 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
193 gepI->idx_end());
194
195 return ptrVal;
196}
197
198//---------------------------------------------------------------------------
199// Function: GetMemInstArgs
200//
201// Purpose:
202// Get the pointer value and the index vector for a memory operation
203// (GetElementPtr, Load, or Store). If all indices of the given memory
204// operation are constant, fold in constant indices in a chain of
205// preceding GetElementPtr instructions (if any), and return the
206// pointer value of the first instruction in the chain.
207// All folded instructions are marked so no code is generated for them.
208//
209// Return values:
210// Returns the pointer Value to use.
211// Returns the resulting IndexVector in idxVec.
212// Returns true/false in allConstantIndices if all indices are/aren't const.
213//---------------------------------------------------------------------------
214
215static Value*
216GetMemInstArgs(InstructionNode* memInstrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000217 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000218 bool& allConstantIndices)
219{
220 allConstantIndices = false;
221 Instruction* memInst = memInstrNode->getInstruction();
222 assert(idxVec.size() == 0 && "Need empty vector to return indices");
223
224 // If there is a GetElemPtr instruction to fold in to this instr,
225 // it must be in the left child for Load and GetElemPtr, and in the
226 // right child for Store instructions.
227 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
228 ? memInstrNode->rightChild()
229 : memInstrNode->leftChild());
230
231 // Default pointer is the one from the current instruction.
232 Value* ptrVal = ptrChild->getValue();
233
234 // Find the "last" GetElemPtr instruction: this one or the immediate child.
235 // There will be none if this is a load or a store from a scalar pointer.
236 InstructionNode* gepNode = NULL;
237 if (isa<GetElementPtrInst>(memInst))
238 gepNode = memInstrNode;
Misha Brukman81b06862003-05-21 18:48:06 +0000239 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) {
240 // Child of load/store is a GEP and memInst is its only use.
241 // Use its indices and mark it as folded.
242 gepNode = cast<InstructionNode>(ptrChild);
243 gepNode->markFoldedIntoParent();
244 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000245
246 // If there are no indices, return the current pointer.
247 // Else extract the pointer from the GEP and fold the indices.
248 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
249 : ptrVal;
250}
251
Chris Lattner54e898e2003-01-15 19:23:34 +0000252
Chris Lattner20b1ea02001-09-14 03:47:57 +0000253//************************ Internal Functions ******************************/
254
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255
Chris Lattner20b1ea02001-09-14 03:47:57 +0000256static inline MachineOpCode
257ChooseBprInstruction(const InstructionNode* instrNode)
258{
259 MachineOpCode opCode;
260
261 Instruction* setCCInstr =
262 ((InstructionNode*) instrNode->leftChild())->getInstruction();
263
264 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000265 {
266 case Instruction::SetEQ: opCode = V9::BRZ; break;
267 case Instruction::SetNE: opCode = V9::BRNZ; break;
268 case Instruction::SetLE: opCode = V9::BRLEZ; break;
269 case Instruction::SetGE: opCode = V9::BRGEZ; break;
270 case Instruction::SetLT: opCode = V9::BRLZ; break;
271 case Instruction::SetGT: opCode = V9::BRGZ; break;
272 default:
273 assert(0 && "Unrecognized VM instruction!");
274 opCode = V9::INVALID_OPCODE;
275 break;
276 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000277
278 return opCode;
279}
280
281
282static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000283ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000284 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000285{
Misha Brukmana98cd452003-05-20 20:32:24 +0000286 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000287
288 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
289
Misha Brukman81b06862003-05-21 18:48:06 +0000290 if (isSigned) {
291 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000292 {
Misha Brukman81b06862003-05-21 18:48:06 +0000293 case Instruction::SetEQ: opCode = V9::BE; break;
294 case Instruction::SetNE: opCode = V9::BNE; break;
295 case Instruction::SetLE: opCode = V9::BLE; break;
296 case Instruction::SetGE: opCode = V9::BGE; break;
297 case Instruction::SetLT: opCode = V9::BL; break;
298 case Instruction::SetGT: opCode = V9::BG; break;
299 default:
300 assert(0 && "Unrecognized VM instruction!");
301 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000302 }
Misha Brukman81b06862003-05-21 18:48:06 +0000303 } else {
304 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000305 {
Misha Brukman81b06862003-05-21 18:48:06 +0000306 case Instruction::SetEQ: opCode = V9::BE; break;
307 case Instruction::SetNE: opCode = V9::BNE; break;
308 case Instruction::SetLE: opCode = V9::BLEU; break;
309 case Instruction::SetGE: opCode = V9::BCC; break;
310 case Instruction::SetLT: opCode = V9::BCS; break;
311 case Instruction::SetGT: opCode = V9::BGU; break;
312 default:
313 assert(0 && "Unrecognized VM instruction!");
314 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000315 }
Misha Brukman81b06862003-05-21 18:48:06 +0000316 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000317
318 return opCode;
319}
320
321static inline MachineOpCode
322ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000323 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000324{
Misha Brukmana98cd452003-05-20 20:32:24 +0000325 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000326
327 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000328 {
329 case Instruction::SetEQ: opCode = V9::FBE; break;
330 case Instruction::SetNE: opCode = V9::FBNE; break;
331 case Instruction::SetLE: opCode = V9::FBLE; break;
332 case Instruction::SetGE: opCode = V9::FBGE; break;
333 case Instruction::SetLT: opCode = V9::FBL; break;
334 case Instruction::SetGT: opCode = V9::FBG; break;
335 default:
336 assert(0 && "Unrecognized VM instruction!");
337 break;
338 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000339
340 return opCode;
341}
342
343
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000344// Create a unique TmpInstruction for a boolean value,
345// representing the CC register used by a branch on that value.
346// For now, hack this using a little static cache of TmpInstructions.
347// Eventually the entire BURG instruction selection should be put
348// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000349// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000350// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000351//
352static TmpInstruction*
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000353GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType,
354 MachineCodeForInstruction& mcfi)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000355{
Chris Lattner09ff1122002-07-24 21:21:32 +0000356 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000357 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000358 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000359
360 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
361
Misha Brukman81b06862003-05-21 18:48:06 +0000362 if (lastFunction != F) {
363 lastFunction = F;
364 boolToTmpCache.clear();
365 }
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000366
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000367 // Look for tmpI and create a new one otherwise. The new value is
368 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000369 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
370 if (tmpI == NULL)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000371 tmpI = new TmpInstruction(mcfi, ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000372
373 return tmpI;
374}
375
376
Chris Lattner20b1ea02001-09-14 03:47:57 +0000377static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000378ChooseBccInstruction(const InstructionNode* instrNode,
Vikram S. Adve786833a2003-07-06 20:13:59 +0000379 const Type*& setCCType)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000380{
381 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000382 assert(setCCNode->getOpLabel() == SetCCOp);
383 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +0000384 setCCType = setCCInstr->getOperand(0)->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000385
Vikram S. Adve786833a2003-07-06 20:13:59 +0000386 if (setCCType->isFloatingPoint())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000387 return ChooseBFpccInstruction(instrNode, setCCInstr);
388 else
389 return ChooseBpccInstruction(instrNode, setCCInstr);
390}
391
392
Misha Brukmaneecdb662003-06-02 20:55:14 +0000393// WARNING: since this function has only one caller, it always returns
394// the opcode that expects an immediate and a register. If this function
395// is ever used in cases where an opcode that takes two registers is required,
396// then modify this function and use convertOpcodeFromRegToImm() where required.
397//
398// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
399// new cases of opcodes.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000400static inline MachineOpCode
Misha Brukmaneecdb662003-06-02 20:55:14 +0000401ChooseMovFpcciInstruction(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000402{
Misha Brukmana98cd452003-05-20 20:32:24 +0000403 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000404
405 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000406 {
Misha Brukmaneecdb662003-06-02 20:55:14 +0000407 case Instruction::SetEQ: opCode = V9::MOVFEi; break;
408 case Instruction::SetNE: opCode = V9::MOVFNEi; break;
409 case Instruction::SetLE: opCode = V9::MOVFLEi; break;
410 case Instruction::SetGE: opCode = V9::MOVFGEi; break;
411 case Instruction::SetLT: opCode = V9::MOVFLi; break;
412 case Instruction::SetGT: opCode = V9::MOVFGi; break;
Misha Brukman81b06862003-05-21 18:48:06 +0000413 default:
414 assert(0 && "Unrecognized VM instruction!");
415 break;
416 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000417
418 return opCode;
419}
420
421
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000422// ChooseMovpcciForSetCC -- Choose a conditional-move instruction
423// based on the type of SetCC operation.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000424//
Misha Brukmaneecdb662003-06-02 20:55:14 +0000425// WARNING: since this function has only one caller, it always returns
426// the opcode that expects an immediate and a register. If this function
427// is ever used in cases where an opcode that takes two registers is required,
428// then modify this function and use convertOpcodeFromRegToImm() where required.
429//
430// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
431// new cases of opcodes.
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000432//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433static MachineOpCode
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000434ChooseMovpcciForSetCC(const InstructionNode* instrNode)
435{
436 MachineOpCode opCode = V9::INVALID_OPCODE;
437
438 const Type* opType = instrNode->leftChild()->getValue()->getType();
439 assert(opType->isIntegral() || isa<PointerType>(opType));
440 bool noSign = opType->isUnsigned() || isa<PointerType>(opType);
441
442 switch(instrNode->getInstruction()->getOpcode())
443 {
444 case Instruction::SetEQ: opCode = V9::MOVEi; break;
445 case Instruction::SetLE: opCode = noSign? V9::MOVLEUi : V9::MOVLEi; break;
446 case Instruction::SetGE: opCode = noSign? V9::MOVCCi : V9::MOVGEi; break;
447 case Instruction::SetLT: opCode = noSign? V9::MOVCSi : V9::MOVLi; break;
448 case Instruction::SetGT: opCode = noSign? V9::MOVGUi : V9::MOVGi; break;
449 case Instruction::SetNE: opCode = V9::MOVNEi; break;
450 default: assert(0 && "Unrecognized LLVM instr!"); break;
451 }
452
453 return opCode;
454}
455
456
457// ChooseMovpregiForSetCC -- Choose a conditional-move-on-register-value
458// instruction based on the type of SetCC operation. These instructions
459// compare a register with 0 and perform the move is the comparison is true.
460//
461// WARNING: like the previous function, this function it always returns
462// the opcode that expects an immediate and a register. See above.
463//
464static MachineOpCode
465ChooseMovpregiForSetCC(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000466{
Misha Brukmana98cd452003-05-20 20:32:24 +0000467 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000468
469 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000470 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000471 case Instruction::SetEQ: opCode = V9::MOVRZi; break;
472 case Instruction::SetLE: opCode = V9::MOVRLEZi; break;
473 case Instruction::SetGE: opCode = V9::MOVRGEZi; break;
474 case Instruction::SetLT: opCode = V9::MOVRLZi; break;
475 case Instruction::SetGT: opCode = V9::MOVRGZi; break;
476 case Instruction::SetNE: opCode = V9::MOVRNZi; break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000477 default: assert(0 && "Unrecognized VM instr!"); break;
Misha Brukman81b06862003-05-21 18:48:06 +0000478 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000479
480 return opCode;
481}
482
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000483
Chris Lattner20b1ea02001-09-14 03:47:57 +0000484static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000485ChooseConvertToFloatInstr(const TargetMachine& target,
486 OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000487{
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000488 assert((vopCode == ToFloatTy || vopCode == ToDoubleTy) &&
489 "Unrecognized convert-to-float opcode!");
Vikram S. Advee895a742003-08-06 18:48:40 +0000490 assert((opType->isIntegral() || opType->isFloatingPoint() ||
491 isa<PointerType>(opType))
492 && "Trying to convert a non-scalar type to FLOAT/DOUBLE?");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000493
Misha Brukmana98cd452003-05-20 20:32:24 +0000494 MachineOpCode opCode = V9::INVALID_OPCODE;
Vikram S. Advee895a742003-08-06 18:48:40 +0000495
496 unsigned opSize = target.getTargetData().getTypeSize(opType);
497
498 if (opType == Type::FloatTy)
499 opCode = (vopCode == ToFloatTy? V9::NOP : V9::FSTOD);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000500 else if (opType == Type::DoubleTy)
Vikram S. Advee895a742003-08-06 18:48:40 +0000501 opCode = (vopCode == ToFloatTy? V9::FDTOS : V9::NOP);
502 else if (opSize <= 4)
503 opCode = (vopCode == ToFloatTy? V9::FITOS : V9::FITOD);
504 else {
505 assert(opSize == 8 && "Unrecognized type size > 4 and < 8!");
506 opCode = (vopCode == ToFloatTy? V9::FXTOS : V9::FXTOD);
507 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000508
509 return opCode;
510}
511
512static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000513ChooseConvertFPToIntInstr(const TargetMachine& target,
514 const Type* destType, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000515{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000516 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
517 && "This function should only be called for FLOAT or DOUBLE");
Vikram S. Advee895a742003-08-06 18:48:40 +0000518 assert((destType->isIntegral() || isa<PointerType>(destType))
519 && "Trying to convert FLOAT/DOUBLE to a non-scalar type?");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000520
Vikram S. Advee895a742003-08-06 18:48:40 +0000521 MachineOpCode opCode = V9::INVALID_OPCODE;
522
523 unsigned destSize = target.getTargetData().getTypeSize(destType);
524
525 if (destType == Type::UIntTy)
526 assert(destType != Type::UIntTy && "Expand FP-to-uint beforehand.");
527 else if (destSize <= 4)
Misha Brukman81b06862003-05-21 18:48:06 +0000528 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
Vikram S. Advee895a742003-08-06 18:48:40 +0000529 else {
530 assert(destSize == 8 && "Unrecognized type size > 4 and < 8!");
531 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
532 }
Vikram S. Adve94c40812002-09-27 14:33:08 +0000533
Chris Lattner20b1ea02001-09-14 03:47:57 +0000534 return opCode;
535}
536
Vikram S. Advee895a742003-08-06 18:48:40 +0000537static MachineInstr*
538CreateConvertFPToIntInstr(const TargetMachine& target,
539 Value* srcVal,
540 Value* destVal,
541 const Type* destType)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000542{
Vikram S. Advee895a742003-08-06 18:48:40 +0000543 MachineOpCode opCode = ChooseConvertFPToIntInstr(target, destType,
544 srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000545 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000546 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000547}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000548
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000549// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000550// The FP value must be converted to the dest type in an FP register,
551// and the result is then copied from FP to int register via memory.
Vikram S. Advee895a742003-08-06 18:48:40 +0000552// SPARC does not have a float-to-uint conversion, only a float-to-int (fdtoi).
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000553// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
Vikram S. Advee895a742003-08-06 18:48:40 +0000554// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly.
555// Therefore, for converting an FP value to uint32_t, we first need to convert
556// to uint64_t and then to uint32_t.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000557//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000558static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000559CreateCodeToConvertFloatToInt(const TargetMachine& target,
560 Value* opVal,
561 Instruction* destI,
562 std::vector<MachineInstr*>& mvec,
563 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000564{
Vikram S. Advee895a742003-08-06 18:48:40 +0000565 Function* F = destI->getParent()->getParent();
566
Vikram S. Adve1e606692002-07-31 21:01:34 +0000567 // Create a temporary to represent the FP register into which the
568 // int value will placed after conversion. The type of this temporary
569 // depends on the type of FP register to use: single-prec for a 32-bit
570 // int or smaller; double-prec for a 64-bit int.
571 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000572 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve1e606692002-07-31 21:01:34 +0000573
Vikram S. Advee895a742003-08-06 18:48:40 +0000574 const Type* castDestType = destI->getType(); // type for the cast instr result
575 const Type* castDestRegType; // type for cast instruction result reg
576 TmpInstruction* destForCast; // dest for cast instruction
577 Instruction* fpToIntCopyDest = destI; // dest for fp-reg-to-int-reg copy instr
578
579 // For converting an FP value to uint32_t, we first need to convert to
580 // uint64_t and then to uint32_t, as explained above.
581 if (destI->getType() == Type::UIntTy) {
582 castDestType = Type::ULongTy; // use this instead of type of destI
583 castDestRegType = Type::DoubleTy; // uint64_t needs 64-bit FP register.
584 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
585 fpToIntCopyDest = new TmpInstruction(mcfi, castDestType, destForCast);
586 }
587 else {
588 castDestRegType = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
589 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
590 }
591
592 // Create the fp-to-int conversion instruction (src and dest regs are FP regs)
593 mvec.push_back(CreateConvertFPToIntInstr(target, opVal, destForCast,
594 castDestType));
Vikram S. Adve1e606692002-07-31 21:01:34 +0000595
596 // Create the fpreg-to-intreg copy code
Vikram S. Advee895a742003-08-06 18:48:40 +0000597 target.getInstrInfo().CreateCodeToCopyFloatToInt(target, F, destForCast,
598 fpToIntCopyDest, mvec, mcfi);
599
600 // Create the uint64_t to uint32_t conversion, if needed
601 if (destI->getType() == Type::UIntTy)
602 target.getInstrInfo().
603 CreateZeroExtensionInstructions(target, F, fpToIntCopyDest, destI,
604 /*numLowBits*/ 32, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000605}
606
607
Chris Lattner20b1ea02001-09-14 03:47:57 +0000608static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000609ChooseAddInstruction(const InstructionNode* instrNode)
610{
611 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
612}
613
614
Chris Lattner20b1ea02001-09-14 03:47:57 +0000615static inline MachineInstr*
616CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000617 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000618{
Misha Brukmana98cd452003-05-20 20:32:24 +0000619 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000620 .addReg(instrNode->leftChild()->getValue())
621 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000622}
623
624static inline MachineInstr*
625CreateAddConstInstruction(const InstructionNode* instrNode)
626{
627 MachineInstr* minstr = NULL;
628
629 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000630 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000631
632 // Cases worth optimizing are:
633 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
634 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
635 //
Chris Lattner9b625032002-05-06 16:15:30 +0000636 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000637 double dval = FPC->getValue();
638 if (dval == 0.0)
639 minstr = CreateMovFloatInstruction(instrNode,
640 instrNode->getInstruction()->getType());
641 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000642
643 return minstr;
644}
645
646
647static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000648ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000649{
Misha Brukmana98cd452003-05-20 20:32:24 +0000650 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000651
Misha Brukman81b06862003-05-21 18:48:06 +0000652 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukman91aee472003-05-27 22:37:00 +0000653 opCode = V9::SUBr;
Misha Brukman81b06862003-05-21 18:48:06 +0000654 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000655 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000656 {
657 case Type::FloatTyID: opCode = V9::FSUBS; break;
658 case Type::DoubleTyID: opCode = V9::FSUBD; break;
659 default: assert(0 && "Invalid type for SUB instruction"); break;
660 }
661 }
662
Chris Lattner20b1ea02001-09-14 03:47:57 +0000663 return opCode;
664}
665
666
667static inline MachineInstr*
668CreateSubConstInstruction(const InstructionNode* instrNode)
669{
670 MachineInstr* minstr = NULL;
671
672 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000673 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000674
675 // Cases worth optimizing are:
676 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
677 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
678 //
Chris Lattner9b625032002-05-06 16:15:30 +0000679 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
680 double dval = FPC->getValue();
681 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000682 minstr = CreateMovFloatInstruction(instrNode,
683 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000684 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000685
686 return minstr;
687}
688
689
690static inline MachineOpCode
691ChooseFcmpInstruction(const InstructionNode* instrNode)
692{
Misha Brukmana98cd452003-05-20 20:32:24 +0000693 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000694
695 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
696 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000697 case Type::FloatTyID: opCode = V9::FCMPS; break;
698 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000699 default: assert(0 && "Invalid type for FCMP instruction"); break;
700 }
701
702 return opCode;
703}
704
705
706// Assumes that leftArg and rightArg are both cast instructions.
707//
708static inline bool
709BothFloatToDouble(const InstructionNode* instrNode)
710{
711 InstrTreeNode* leftArg = instrNode->leftChild();
712 InstrTreeNode* rightArg = instrNode->rightChild();
713 InstrTreeNode* leftArgArg = leftArg->leftChild();
714 InstrTreeNode* rightArgArg = rightArg->leftChild();
715 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
716
717 // Check if both arguments are floats cast to double
718 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000719 leftArgArg->getValue()->getType() == Type::FloatTy &&
720 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000721}
722
723
724static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000725ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000726{
Misha Brukmana98cd452003-05-20 20:32:24 +0000727 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000728
Chris Lattner0c4e8862002-09-03 01:08:28 +0000729 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000730 opCode = V9::MULXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000731 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000732 switch(resultType->getPrimitiveID())
Misha Brukman7b647942003-05-30 20:11:56 +0000733 {
734 case Type::FloatTyID: opCode = V9::FMULS; break;
735 case Type::DoubleTyID: opCode = V9::FMULD; break;
736 default: assert(0 && "Invalid type for MUL instruction"); break;
737 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000738
739 return opCode;
740}
741
742
Vikram S. Adve510eec72001-11-04 21:59:14 +0000743
Chris Lattner20b1ea02001-09-14 03:47:57 +0000744static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000745CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000746 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000747{
Misha Brukman91aee472003-05-27 22:37:00 +0000748 return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000749 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000750}
751
752
Vikram S. Adve242a8082002-05-19 15:25:51 +0000753// Create instruction sequence for any shift operation.
754// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
755// requires a second instruction for explicit sign-extension.
756// Note that we only have to worry about a sign-bit appearing in the
757// most significant bit of the operand after shifting (e.g., bit 32 of
758// Int or bit 16 of Short), so we do not have to worry about results
759// that are as large as a normal integer register.
760//
761static inline void
762CreateShiftInstructions(const TargetMachine& target,
763 Function* F,
764 MachineOpCode shiftOpCode,
765 Value* argVal1,
766 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000767 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000768 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000769 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000770 MachineCodeForInstruction& mcfi)
771{
772 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
773 "Large shift sizes unexpected, but can be handled below: "
774 "You need to check whether or not it fits in immed field below");
775
776 // If this is a logical left shift of a type smaller than the standard
777 // integer reg. size, we have to extend the sign-bit into upper bits
778 // of dest, so we need to put the result of the SLL into a temporary.
779 //
780 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000781 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000782
Misha Brukmand36e30e2003-06-06 09:52:23 +0000783 if ((shiftOpCode == V9::SLLr5 || shiftOpCode == V9::SLLXr6) && opSize < 8) {
Misha Brukman7b647942003-05-30 20:11:56 +0000784 // put SLL result into a temporary
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000785 shiftDest = new TmpInstruction(mcfi, argVal1, optArgVal2, "sllTmp");
Misha Brukman7b647942003-05-30 20:11:56 +0000786 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000787
788 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000789 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
790 .addReg(shiftDest, MOTy::Def)
791 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
792 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000793 mvec.push_back(M);
794
Misha Brukman7b647942003-05-30 20:11:56 +0000795 if (shiftDest != destVal) {
796 // extend the sign-bit of the result into all upper bits of dest
797 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
798 target.getInstrInfo().
799 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
800 8*opSize, mvec, mcfi);
801 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000802}
803
804
Vikram S. Adve74825322002-03-18 03:15:35 +0000805// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000806// create a cheaper instruction.
807// This returns the approximate cost of the instructions generated,
808// which is used to pick the cheapest when both operands are constant.
Vikram S. Adve645fea32003-05-25 21:59:47 +0000809static unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000810CreateMulConstInstruction(const TargetMachine &target, Function* F,
811 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000812 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000813 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000814{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000815 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukman91aee472003-05-27 22:37:00 +0000816 unsigned cost = target.getInstrInfo().minLatency(V9::MULXr);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000817 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000818
819 Value* constOp = rval;
820 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000821 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000822
823 // Cases worth optimizing are:
824 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
825 // (2) Multiply by 2^x for integer types: replace with Shift
826 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000827 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000828
Misha Brukmana98cd452003-05-20 20:32:24 +0000829 if (resultType->isInteger() || isa<PointerType>(resultType)) {
830 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +0000831 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
832 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +0000833 if (isValidConst) {
834 unsigned pow;
835 bool needNeg = false;
836 if (C < 0) {
837 needNeg = true;
838 C = -C;
839 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000840
Misha Brukmana98cd452003-05-20 20:32:24 +0000841 if (C == 0 || C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000842 cost = target.getInstrInfo().minLatency(V9::ADDr);
Misha Brukmana98cd452003-05-20 20:32:24 +0000843 unsigned Zero = target.getRegInfo().getZeroRegNum();
844 MachineInstr* M;
845 if (C == 0)
Misha Brukman91aee472003-05-27 22:37:00 +0000846 M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000847 else
Misha Brukman91aee472003-05-27 22:37:00 +0000848 M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000849 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +0000850 } else if (isPowerOf2(C, pow)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000851 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Misha Brukmand36e30e2003-06-06 09:52:23 +0000852 MachineOpCode opCode = (opSize <= 32)? V9::SLLr5 : V9::SLLXr6;
Misha Brukmana98cd452003-05-20 20:32:24 +0000853 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
854 destVal, mvec, mcfi);
855 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000856
Misha Brukman7b647942003-05-30 20:11:56 +0000857 if (mvec.size() > 0 && needNeg) {
858 // insert <reg = SUB 0, reg> after the instr to flip the sign
Misha Brukmana98cd452003-05-20 20:32:24 +0000859 MachineInstr* M = CreateIntNegInstruction(target, destVal);
860 mvec.push_back(M);
861 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000862 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000863 } else {
864 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
865 double dval = FPC->getValue();
866 if (fabs(dval) == 1) {
867 MachineOpCode opCode = (dval < 0)
868 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
869 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
870 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
871 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000872 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000873 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000874
Misha Brukmana98cd452003-05-20 20:32:24 +0000875 if (firstNewInstr < mvec.size()) {
876 cost = 0;
877 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
878 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
879 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000880
881 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000882}
883
884
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000885// Does not create any instructions if we cannot exploit constant to
886// create a cheaper instruction.
887//
888static inline void
889CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000890 Function* F,
891 Value* lval, Value* rval,
892 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000893 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000894 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000895{
896 Value* constOp;
Misha Brukman7b647942003-05-30 20:11:56 +0000897 if (isa<Constant>(lval) && isa<Constant>(rval)) {
898 // both operands are constant: evaluate and "set" in dest
899 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
900 cast<Constant>(lval),
901 cast<Constant>(rval));
902 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
903 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000904 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000905 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000906 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000907 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000908
909 // else neither is constant
910 return;
911}
912
Vikram S. Adve74825322002-03-18 03:15:35 +0000913// Return NULL if we cannot exploit constant to create a cheaper instruction
914static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000915CreateMulInstruction(const TargetMachine &target, Function* F,
916 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000917 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000918 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000919 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
920{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000921 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000922 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000923 if (mvec.size() == L) {
924 // no instructions were added so create MUL reg, reg, reg.
925 // Use FSMULD if both operands are actually floats cast to doubles.
926 // Otherwise, use the default opcode for the appropriate type.
927 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
928 ? forceMulOp
929 : ChooseMulInstructionByType(destVal->getType()));
930 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
931 .addRegDef(destVal));
932 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000933}
934
935
Vikram S. Adve510eec72001-11-04 21:59:14 +0000936// Generate a divide instruction for Div or Rem.
937// For Rem, this assumes that the operand type will be signed if the result
938// type is signed. This is correct because they must have the same sign.
939//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000940static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000941ChooseDivInstruction(TargetMachine &target,
942 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000943{
Misha Brukmana98cd452003-05-20 20:32:24 +0000944 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000945
946 const Type* resultType = instrNode->getInstruction()->getType();
947
Chris Lattner0c4e8862002-09-03 01:08:28 +0000948 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000949 opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000950 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000951 switch(resultType->getPrimitiveID())
952 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000953 case Type::FloatTyID: opCode = V9::FDIVS; break;
954 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000955 default: assert(0 && "Invalid type for DIV instruction"); break;
956 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000957
958 return opCode;
959}
960
961
Chris Lattner54e898e2003-01-15 19:23:34 +0000962// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve645fea32003-05-25 21:59:47 +0000963static void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000964CreateDivConstInstruction(TargetMachine &target,
965 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000966 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000967{
Chris Lattner54e898e2003-01-15 19:23:34 +0000968 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000969 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000970 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000971 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000972
Vikram S. Adve645fea32003-05-25 21:59:47 +0000973 Instruction* destVal = instrNode->getInstruction();
Chris Lattner54e898e2003-01-15 19:23:34 +0000974 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000975
976 // Cases worth optimizing are:
977 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
978 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
979 //
980 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000981
Misha Brukman7b647942003-05-30 20:11:56 +0000982 if (resultType->isInteger()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000983 unsigned pow;
984 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +0000985 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
986 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +0000987 if (isValidConst) {
988 bool needNeg = false;
989 if (C < 0) {
990 needNeg = true;
991 C = -C;
992 }
Vikram S. Advee6124d32003-07-29 19:59:23 +0000993
Misha Brukmana98cd452003-05-20 20:32:24 +0000994 if (C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000995 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000996 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +0000997 } else if (isPowerOf2(C, pow)) {
Vikram S. Adve645fea32003-05-25 21:59:47 +0000998 unsigned opCode;
999 Value* shiftOperand;
Vikram S. Advee895a742003-08-06 18:48:40 +00001000 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve645fea32003-05-25 21:59:47 +00001001
1002 if (resultType->isSigned()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001003 // For N / 2^k, if the operand N is negative,
1004 // we need to add (2^k - 1) before right-shifting by k, i.e.,
Vikram S. Adve645fea32003-05-25 21:59:47 +00001005 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001006 // (N / 2^k) = N >> k, if N >= 0;
1007 // (N + 2^k - 1) >> k, if N < 0
1008 //
1009 // If N is <= 32 bits, use:
1010 // sra N, 31, t1 // t1 = ~0, if N < 0, 0 else
1011 // srl t1, 32-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1012 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1013 // sra t3, k, result // result = N / 2^k
1014 //
1015 // If N is 64 bits, use:
1016 // srax N, k-1, t1 // t1 = sign bit in high k positions
1017 // srlx t1, 64-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1018 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1019 // sra t3, k, result // result = N / 2^k
1020 //
1021 TmpInstruction *sraTmp, *srlTmp, *addTmp;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001022 MachineCodeForInstruction& mcfi
1023 = MachineCodeForInstruction::get(destVal);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001024 sraTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getSign");
1025 srlTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getPlus2km1");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001026 addTmp = new TmpInstruction(mcfi, resultType, LHS, srlTmp,"incIfNeg");
Vikram S. Adve645fea32003-05-25 21:59:47 +00001027
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001028 // Create the SRA or SRAX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001029 mvec.push_back(BuildMI((opSize > 4)? V9::SRAXi6 : V9::SRAi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001030 .addReg(LHS)
1031 .addSImm((resultType==Type::LongTy)? pow-1 : 31)
1032 .addRegDef(sraTmp));
1033
Vikram S. Adve645fea32003-05-25 21:59:47 +00001034 // Create the SRL or SRLX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001035 mvec.push_back(BuildMI((opSize > 4)? V9::SRLXi6 : V9::SRLi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001036 .addReg(sraTmp)
1037 .addSImm((resultType==Type::LongTy)? 64-pow : 32-pow)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001038 .addRegDef(srlTmp));
1039
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001040 // Create the ADD instruction to add 2^pow-1 for negative values
Misha Brukman91aee472003-05-27 22:37:00 +00001041 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001042 .addRegDef(addTmp));
1043
1044 // Get the shift operand and "right-shift" opcode to do the divide
1045 shiftOperand = addTmp;
Vikram S. Advee895a742003-08-06 18:48:40 +00001046 opCode = (opSize > 4)? V9::SRAXi6 : V9::SRAi5;
Misha Brukman7b647942003-05-30 20:11:56 +00001047 } else {
Vikram S. Adve645fea32003-05-25 21:59:47 +00001048 // Get the shift operand and "right-shift" opcode to do the divide
1049 shiftOperand = LHS;
Vikram S. Advee895a742003-08-06 18:48:40 +00001050 opCode = (opSize > 4)? V9::SRLXi6 : V9::SRLi5;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001051 }
1052
1053 // Now do the actual shift!
1054 mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow)
1055 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001056 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001057
Misha Brukmana98cd452003-05-20 20:32:24 +00001058 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
1059 // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve645fea32003-05-25 21:59:47 +00001060 mvec.push_back(CreateIntNegInstruction(target, destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001061 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001062 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001063 } else {
1064 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
1065 double dval = FPC->getValue();
1066 if (fabs(dval) == 1) {
1067 unsigned opCode =
1068 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
1069 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001070
Vikram S. Adve645fea32003-05-25 21:59:47 +00001071 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001072 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001073 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001074 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001075}
1076
1077
Vikram S. Adve74825322002-03-18 03:15:35 +00001078static void
1079CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1080 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001081 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001082 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001083 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001084{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001085 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001086 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001087 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001088 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001089
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001090 // Enforce the alignment constraints on the stack pointer at
1091 // compile time if the total size is a known constant.
Misha Brukman7b647942003-05-30 20:11:56 +00001092 if (isa<Constant>(numElementsVal)) {
1093 bool isValid;
Vikram S. Advee6124d32003-07-29 19:59:23 +00001094 int64_t numElem = (int64_t) target.getInstrInfo().
1095 ConvertConstantToIntType(target, numElementsVal,
1096 numElementsVal->getType(), isValid);
Misha Brukman7b647942003-05-30 20:11:56 +00001097 assert(isValid && "Unexpectedly large array dimension in alloca!");
1098 int64_t total = numElem * tsize;
1099 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1100 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1101 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1102 } else {
1103 // The size is not a constant. Generate code to compute it and
1104 // code to pad the size for stack alignment.
1105 // Create a Value to hold the (constant) element size
1106 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001107
Misha Brukman7b647942003-05-30 20:11:56 +00001108 // Create temporary values to hold the result of MUL, SLL, SRL
Vikram S. Adve80544442003-06-23 02:13:57 +00001109 // To pad `size' to next smallest multiple of 16:
1110 // size = (size + 15) & (-16 = 0xfffffffffffffff0)
1111 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001112 TmpInstruction* tmpProd = new TmpInstruction(mcfi,numElementsVal, tsizeVal);
Vikram S. Adve80544442003-06-23 02:13:57 +00001113 TmpInstruction* tmpAdd15= new TmpInstruction(mcfi,numElementsVal, tmpProd);
1114 TmpInstruction* tmpAndf0= new TmpInstruction(mcfi,numElementsVal, tmpAdd15);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001115
Misha Brukman7b647942003-05-30 20:11:56 +00001116 // Instruction 1: mul numElements, typeSize -> tmpProd
1117 // This will optimize the MUL as far as possible.
Vikram S. Adve80544442003-06-23 02:13:57 +00001118 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd, getMvec,
Misha Brukman7b647942003-05-30 20:11:56 +00001119 mcfi, INVALID_MACHINE_OPCODE);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001120
Vikram S. Adve80544442003-06-23 02:13:57 +00001121 // Instruction 2: andn tmpProd, 0x0f -> tmpAndn
1122 getMvec.push_back(BuildMI(V9::ADDi, 3).addReg(tmpProd).addSImm(15)
1123 .addReg(tmpAdd15, MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001124
Vikram S. Adve80544442003-06-23 02:13:57 +00001125 // Instruction 3: add tmpAndn, 0x10 -> tmpAdd16
1126 getMvec.push_back(BuildMI(V9::ANDi, 3).addReg(tmpAdd15).addSImm(-16)
1127 .addReg(tmpAndf0, MOTy::Def));
1128
1129 totalSizeVal = tmpAndf0;
Misha Brukman7b647942003-05-30 20:11:56 +00001130 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001131
1132 // Get the constant offset from SP for dynamically allocated storage
1133 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001134 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001135 bool growUp;
1136 ConstantSInt* dynamicAreaOffset =
1137 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001138 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001139 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1140
Chris Lattner54e898e2003-01-15 19:23:34 +00001141 unsigned SPReg = target.getRegInfo().getStackPointer();
1142
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001143 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukman91aee472003-05-27 22:37:00 +00001144 getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001145 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001146
Vikram S. Adve74825322002-03-18 03:15:35 +00001147 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukman91aee472003-05-27 22:37:00 +00001148 getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset)
Misha Brukmana98cd452003-05-20 20:32:24 +00001149 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001150}
1151
1152
1153static void
1154CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1155 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001156 unsigned tsize,
1157 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001158 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001159{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001160 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001161 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001162 "Result value is not part of a function?");
1163 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001164 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001165
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001166 // Put the variable in the dynamically sized area of the frame if either:
1167 // (a) The offset is too large to use as an immediate in load/stores
1168 // (check LDX because all load/stores have the same-size immed. field).
1169 // (b) The object is "large", so it could cause many other locals,
1170 // spills, and temporaries to have large offsets.
1171 // NOTE: We use LARGE = 8 * argSlotSize = 64 bytes.
1172 // You've gotta love having only 13 bits for constant offset values :-|.
1173 //
1174 unsigned paddedSize;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001175 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001176 paddedSize,
1177 tsize * numElements);
1178
1179 if (((int)paddedSize) > 8 * target.getFrameInfo().getSizeOfEachArgOnStack() ||
1180 ! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001181 CreateCodeForVariableSizeAlloca(target, result, tsize,
1182 ConstantSInt::get(Type::IntTy,numElements),
1183 getMvec);
1184 return;
1185 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001186
1187 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001188 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001189
1190 // Create a temporary Value to hold the constant offset.
1191 // This is needed because it may not fit in the immediate field.
1192 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1193
1194 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001195 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukman91aee472003-05-27 22:37:00 +00001196 getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001197 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001198}
1199
1200
Chris Lattner20b1ea02001-09-14 03:47:57 +00001201//------------------------------------------------------------------------
1202// Function SetOperandsForMemInstr
1203//
1204// Choose addressing mode for the given load or store instruction.
1205// Use [reg+reg] if it is an indexed reference, and the index offset is
1206// not a constant or if it cannot fit in the offset field.
1207// Use [reg+offset] in all other cases.
1208//
1209// This assumes that all array refs are "lowered" to one of these forms:
1210// %x = load (subarray*) ptr, constant ; single constant offset
1211// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1212// Generally, this should happen via strength reduction + LICM.
1213// Also, strength reduction should take care of using the same register for
1214// the loop index variable and an array index, when that is profitable.
1215//------------------------------------------------------------------------
1216
1217static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001218SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001219 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001220 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001221 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001222{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001223 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001224 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001225 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001226 bool allConstantIndices;
1227 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001228
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001229 // Now create the appropriate operands for the machine instruction.
1230 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001231 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001232 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001233 MachineOperand::MachineOperandType offsetOpType =
1234 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001235
Vikram S. Adve74825322002-03-18 03:15:35 +00001236 // Check if there is an index vector and if so, compute the
1237 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001238 //
Misha Brukman7b647942003-05-30 20:11:56 +00001239 if (!idxVec.empty()) {
1240 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001241
Misha Brukman7b647942003-05-30 20:11:56 +00001242 // If all indices are constant, compute the combined offset directly.
1243 if (allConstantIndices) {
1244 // Compute the offset value using the index vector. Create a
1245 // virtual reg. for it since it may not fit in the immed field.
1246 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
1247 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
1248 } else {
1249 // There is at least one non-constant offset. Therefore, this must
1250 // be an array ref, and must have been lowered to a single non-zero
1251 // offset. (An extra leading zero offset, if any, can be ignored.)
1252 // Generate code sequence to compute address from index.
1253 //
1254 bool firstIdxIsZero = IsZero(idxVec[0]);
1255 assert(idxVec.size() == 1U + firstIdxIsZero
1256 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001257
Misha Brukman7b647942003-05-30 20:11:56 +00001258 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001259
Misha Brukman7b647942003-05-30 20:11:56 +00001260 std::vector<MachineInstr*> mulVec;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001261 Instruction* addr =
1262 new TmpInstruction(MachineCodeForInstruction::get(memInst),
1263 Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001264
Misha Brukman7b647942003-05-30 20:11:56 +00001265 // Get the array type indexed by idxVal, and compute its element size.
1266 // The call to getTypeSize() will fail if size is not constant.
1267 const Type* vecType = (firstIdxIsZero
1268 ? GetElementPtrInst::getIndexedType(ptrType,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001269 std::vector<Value*>(1U, idxVec[0]),
1270 /*AllowCompositeLeaf*/ true)
1271 : ptrType);
Misha Brukman7b647942003-05-30 20:11:56 +00001272 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1273 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
1274 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001275
Misha Brukman7b647942003-05-30 20:11:56 +00001276 // CreateMulInstruction() folds constants intelligently enough.
1277 CreateMulInstruction(target, memInst->getParent()->getParent(),
1278 idxVal, /* lval, not likely to be const*/
1279 eltSizeVal, /* rval, likely to be constant */
1280 addr, /* result */
1281 mulVec, MachineCodeForInstruction::get(memInst),
1282 INVALID_MACHINE_OPCODE);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001283
Misha Brukman7b647942003-05-30 20:11:56 +00001284 assert(mulVec.size() > 0 && "No multiply code created?");
1285 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
1286
1287 valueForRegOffset = addr;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001288 }
Misha Brukman7b647942003-05-30 20:11:56 +00001289 } else {
1290 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1291 smallConstOffset = 0;
1292 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001293
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001294 // For STORE:
1295 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1296 // For LOAD or GET_ELEMENT_PTR,
1297 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1298 //
1299 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001300 MachineInstr *MI;
1301 if (memInst->getOpcode() == Instruction::Store) {
Misha Brukman7b647942003-05-30 20:11:56 +00001302 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001303 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1304 .addReg(ptrVal).addReg(valueForRegOffset);
Misha Brukman7b647942003-05-30 20:11:56 +00001305 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001306 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001307 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1308 .addReg(ptrVal).addSImm(smallConstOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001309 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001310 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00001311 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001312 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1313 .addRegDef(memInst);
Misha Brukman7b647942003-05-30 20:11:56 +00001314 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001315 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001316 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1317 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001318 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001319 }
1320 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001321}
1322
1323
Chris Lattner20b1ea02001-09-14 03:47:57 +00001324//
1325// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001326// in place of the use(s) of that instruction in node `parent'.
1327// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001328// Also make sure to skip over a parent who:
1329// (1) is a list node in the Burg tree, or
1330// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001331//
1332static void
1333ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001334 InstrTreeNode* parent,
1335 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001336{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001337 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1338
Chris Lattner20b1ea02001-09-14 03:47:57 +00001339 Instruction* unusedOp = treeNode->getInstruction();
1340 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001341
1342 // The parent itself may be a list node, so find the real parent instruction
1343 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1344 {
1345 parent = parent->parent();
1346 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1347 }
1348 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1349
1350 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001351 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001352
1353 // The parent's mvec would be empty if it was itself forwarded.
1354 // Recursively call ForwardOperand in that case...
1355 //
Misha Brukman7b647942003-05-30 20:11:56 +00001356 if (mvec.size() == 0) {
1357 assert(parent->parent() != NULL &&
1358 "Parent could not have been forwarded, yet has no instructions?");
1359 ForwardOperand(treeNode, parent->parent(), operandNum);
1360 } else {
1361 for (unsigned i=0, N=mvec.size(); i < N; i++) {
1362 MachineInstr* minstr = mvec[i];
1363 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) {
1364 const MachineOperand& mop = minstr->getOperand(i);
1365 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
1366 mop.getVRegValue() == unusedOp)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001367 {
Misha Brukman7b647942003-05-30 20:11:56 +00001368 minstr->SetMachineOperandVal(i, MachineOperand::MO_VirtualRegister,
1369 fwdOp);
1370 }
1371 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001372
Misha Brukman7b647942003-05-30 20:11:56 +00001373 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
Chris Lattner907b7dc2003-08-05 16:59:24 +00001374 if (minstr->getImplicitRef(i) == unusedOp)
1375 minstr->setImplicitRef(i, fwdOp);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001376 }
Misha Brukman7b647942003-05-30 20:11:56 +00001377 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001378}
1379
1380
Vikram S. Adve242a8082002-05-19 15:25:51 +00001381inline bool
1382AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001383{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001384 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1385 UI != UE; ++UI)
1386 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1387 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1388 return false;
1389 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001390}
1391
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001392// Generate code for any intrinsic that needs a special code sequence
1393// instead of a regular call. If not that kind of intrinsic, do nothing.
1394// Returns true if code was generated, otherwise false.
1395//
Chris Lattner37b18262003-12-28 09:46:33 +00001396static bool CodeGenIntrinsic(Intrinsic::ID iid, CallInst &callInstr,
1397 TargetMachine &target,
1398 std::vector<MachineInstr*>& mvec) {
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001399 switch (iid) {
Chris Lattner37b18262003-12-28 09:46:33 +00001400 default:
1401 assert(0 && "Unknown intrinsic function call should have been lowered!");
Brian Gaeked0fde302003-11-11 22:41:34 +00001402 case Intrinsic::va_start: {
Vikram S. Adve40dee512003-10-21 11:25:09 +00001403 // Get the address of the first incoming vararg argument on the stack
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001404 bool ignore;
1405 Function* func = cast<Function>(callInstr.getParent()->getParent());
1406 int numFixedArgs = func->getFunctionType()->getNumParams();
1407 int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum();
1408 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
1409 int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo().
1410 getFirstIncomingArgOffset(MachineFunction::get(func), ignore);
Misha Brukman91aee472003-05-27 22:37:00 +00001411 mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff).
Vikram S. Adve40dee512003-10-21 11:25:09 +00001412 addRegDef(&callInstr));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001413 return true;
1414 }
1415
Brian Gaeked0fde302003-11-11 22:41:34 +00001416 case Intrinsic::va_end:
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001417 return true; // no-op on Sparc
1418
Brian Gaeked0fde302003-11-11 22:41:34 +00001419 case Intrinsic::va_copy:
Vikram S. Adve40dee512003-10-21 11:25:09 +00001420 // Simple copy of current va_list (arg1) to new va_list (result)
Misha Brukman91aee472003-05-27 22:37:00 +00001421 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001422 addMReg(target.getRegInfo().getZeroRegNum()).
Vikram S. Adve40dee512003-10-21 11:25:09 +00001423 addReg(callInstr.getOperand(1)).
1424 addRegDef(&callInstr));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001425 return true;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001426 }
1427}
1428
Vikram S. Advefb361122001-10-22 13:36:31 +00001429//******************* Externally Visible Functions *************************/
1430
Vikram S. Advefb361122001-10-22 13:36:31 +00001431//------------------------------------------------------------------------
1432// External Function: ThisIsAChainRule
1433//
1434// Purpose:
1435// Check if a given BURG rule is a chain rule.
1436//------------------------------------------------------------------------
1437
1438extern bool
1439ThisIsAChainRule(int eruleno)
1440{
1441 switch(eruleno)
1442 {
1443 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001444 case 123:
1445 case 124:
1446 case 125:
1447 case 126:
1448 case 127:
1449 case 128:
1450 case 129:
1451 case 130:
1452 case 131:
1453 case 132:
1454 case 133:
1455 case 155:
1456 case 221:
1457 case 222:
1458 case 241:
1459 case 242:
1460 case 243:
1461 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001462 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001463 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001464 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001465
Vikram S. Advefb361122001-10-22 13:36:31 +00001466 default:
1467 return false; break;
1468 }
1469}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001470
1471
1472//------------------------------------------------------------------------
1473// External Function: GetInstructionsByRule
1474//
1475// Purpose:
1476// Choose machine instructions for the SPARC according to the
1477// patterns chosen by the BURG-generated parser.
1478//------------------------------------------------------------------------
1479
Vikram S. Adve74825322002-03-18 03:15:35 +00001480void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001481GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001482 int ruleForNode,
1483 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001484 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001485 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001486{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001487 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001488 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001489 int nextRule;
1490 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001491 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001492 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001493 unsigned L;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001494 bool foldCase = false;
Vikram S. Adve74825322002-03-18 03:15:35 +00001495
1496 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001497
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001498 // If the code for this instruction was folded into the parent (user),
1499 // then do nothing!
1500 if (subtreeRoot->isFoldedIntoParent())
1501 return;
1502
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001503 //
1504 // Let's check for chain rules outside the switch so that we don't have
1505 // to duplicate the list of chain rule production numbers here again
1506 //
Misha Brukmanb461d372003-10-23 16:48:30 +00001507 if (ThisIsAChainRule(ruleForNode)) {
1508 // Chain rules have a single nonterminal on the RHS.
1509 // Get the rule that matches the RHS non-terminal and use that instead.
1510 //
1511 assert(nts[0] && ! nts[1]
1512 && "A chain rule should have only one RHS non-terminal!");
1513 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1514 nts = burm_nts[nextRule];
1515 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
1516 } else {
1517 switch(ruleForNode) {
1518 case 1: // stmt: Ret
1519 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001520 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001521 // for moving return value to appropriate register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001522 // Copy the return value to the required return register.
1523 // Mark the return Value as an implicit ref of the RET instr..
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001524 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001525 // Finally put a NOP in the delay slot.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001526 ReturnInst *returnInstr=cast<ReturnInst>(subtreeRoot->getInstruction());
1527 Value* retVal = returnInstr->getReturnValue();
1528 MachineCodeForInstruction& mcfi =
1529 MachineCodeForInstruction::get(returnInstr);
1530
1531 // Create a hidden virtual reg to represent the return address register
1532 // used by the machine instruction but not represented in LLVM.
1533 //
1534 Instruction* returnAddrTmp = new TmpInstruction(mcfi, returnInstr);
1535
1536 MachineInstr* retMI =
1537 BuildMI(V9::JMPLRETi, 3).addReg(returnAddrTmp).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001538 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001539
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001540 // If there is a value to return, we need to:
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001541 // (a) Sign-extend the value if it is smaller than 8 bytes (reg size)
1542 // (b) Insert a copy to copy the return value to the appropriate reg.
1543 // -- For FP values, create a FMOVS or FMOVD instruction
1544 // -- For non-FP values, create an add-with-0 instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001545 //
1546 if (retVal != NULL) {
Misha Brukmand71295a2003-12-17 22:04:00 +00001547 const SparcRegInfo& regInfo =
1548 (SparcRegInfo&) target.getRegInfo();
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001549 const Type* retType = retVal->getType();
1550 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
1551 unsigned retRegNum = (retType->isFloatingPoint()
1552 ? (unsigned) SparcFloatRegClass::f0
1553 : (unsigned) SparcIntRegClass::i0);
1554 retRegNum = regInfo.getUnifiedRegNum(regClassID, retRegNum);
1555
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001556 // () Insert sign-extension instructions for small signed values.
1557 //
1558 Value* retValToUse = retVal;
1559 if (retType->isIntegral() && retType->isSigned()) {
1560 unsigned retSize = target.getTargetData().getTypeSize(retType);
1561 if (retSize <= 4) {
1562 // create a temporary virtual reg. to hold the sign-extension
1563 retValToUse = new TmpInstruction(mcfi, retVal);
1564
1565 // sign-extend retVal and put the result in the temporary reg.
1566 target.getInstrInfo().CreateSignExtensionInstructions
1567 (target, returnInstr->getParent()->getParent(),
1568 retVal, retValToUse, 8*retSize, mvec, mcfi);
1569 }
1570 }
1571
1572 // (b) Now, insert a copy to to the appropriate register:
1573 // -- For FP values, create a FMOVS or FMOVD instruction
1574 // -- For non-FP values, create an add-with-0 instruction
1575 //
1576 // First, create a virtual register to represent the register and
1577 // mark this vreg as being an implicit operand of the ret MI.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001578 TmpInstruction* retVReg =
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001579 new TmpInstruction(mcfi, retValToUse, NULL, "argReg");
1580
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001581 retMI->addImplicitRef(retVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001582
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001583 if (retType->isFloatingPoint())
1584 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001585 .addReg(retValToUse).addReg(retVReg, MOTy::Def));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001586 else
1587 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001588 .addReg(retValToUse).addSImm((int64_t) 0)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001589 .addReg(retVReg, MOTy::Def));
1590
1591 // Mark the operand with the register it should be assigned
1592 M->SetRegForOperand(M->getNumOperands()-1, retRegNum);
1593 retMI->SetRegForImplicitRef(retMI->getNumImplicitRefs()-1, retRegNum);
1594
1595 mvec.push_back(M);
1596 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001597
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001598 // Now insert the RET instruction and a NOP for the delay slot
1599 mvec.push_back(retMI);
Misha Brukmana98cd452003-05-20 20:32:24 +00001600 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001601
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001602 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001603 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001604
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001605 case 3: // stmt: Store(reg,reg)
1606 case 4: // stmt: Store(reg,ptrreg)
1607 SetOperandsForMemInstr(ChooseStoreInstruction(
Chris Lattner54e898e2003-01-15 19:23:34 +00001608 subtreeRoot->leftChild()->getValue()->getType()),
1609 mvec, subtreeRoot, target);
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001610 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001611
1612 case 5: // stmt: BrUncond
1613 {
1614 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1615 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
1616
1617 // delay slot
1618 mvec.push_back(BuildMI(V9::NOP, 0));
1619 break;
1620 }
1621
1622 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001623 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001624 // If the constant is ZERO, we can use the branch-on-integer-register
1625 // instructions and avoid the SUBcc instruction entirely.
1626 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001627 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001628 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1629 assert(constNode &&
1630 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001631 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001632 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001633
Chris Lattner0c4e8862002-09-03 01:08:28 +00001634 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001635 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00001636 && target.getInstrInfo().ConvertConstantToIntType(target,
1637 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001638 && isValidConst)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001639 {
1640 // That constant is a zero after all...
1641 // Use the left child of setCC as the first argument!
1642 // Mark the setCC node so that no code is generated for it.
1643 InstructionNode* setCCNode = (InstructionNode*)
1644 subtreeRoot->leftChild();
1645 assert(setCCNode->getOpLabel() == SetCCOp);
1646 setCCNode->markFoldedIntoParent();
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001647
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001648 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001649
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001650 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1651 .addReg(setCCNode->leftChild()->getValue())
1652 .addPCDisp(brInst->getSuccessor(0));
1653 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001654
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001655 // delay slot
1656 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001657
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001658 // false branch
1659 mvec.push_back(BuildMI(V9::BA, 1)
1660 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001661
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001662 // delay slot
1663 mvec.push_back(BuildMI(V9::NOP, 0));
1664 break;
1665 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001666 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001667 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001668
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001669 case 6: // stmt: BrCond(setCC)
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001670 { // bool => boolean was computed with SetCC.
1671 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001672 // If it is an integer CC, we also need to find the unique
1673 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001674 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001675 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +00001676 const Type* setCCType;
1677 unsigned Opcode = ChooseBccInstruction(subtreeRoot, setCCType);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001678 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1679 brInst->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00001680 setCCType,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001681 MachineCodeForInstruction::get(brInst));
Chris Lattner54e898e2003-01-15 19:23:34 +00001682 M = BuildMI(Opcode, 2).addCCReg(ccValue)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001683 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001684 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001685
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001686 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001687 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001688
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001689 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001690 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001691
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001692 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001693 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001694 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001695 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001696
1697 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001698 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001699 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001700 Constant* constVal =
1701 cast<Constant>(subtreeRoot->leftChild()->getValue());
1702 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001703
Misha Brukmana98cd452003-05-20 20:32:24 +00001704 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001705 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001706 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001707
1708 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001709 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001710 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001711 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001712
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001713 case 8: // stmt: BrCond(boolreg)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001714 { // boolreg => boolean is recorded in an integer register.
1715 // Use branch-on-integer-register instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001716 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001717 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001718 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001719 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001720 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001721
1722 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001723 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001724
1725 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001726 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001727
1728 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001729 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001730 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001731 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001732
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001733 case 9: // stmt: Switch(reg)
1734 assert(0 && "*** SWITCH instruction is not implemented yet.");
1735 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001736
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001737 case 10: // reg: VRegList(reg, reg)
1738 assert(0 && "VRegList should never be the topmost non-chain rule");
1739 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001740
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001741 case 21: // bool: Not(bool,reg): Compute with a conditional-move-on-reg
1742 { // First find the unary operand. It may be left or right, usually right.
1743 Instruction* notI = subtreeRoot->getInstruction();
1744 Value* notArg = BinaryOperator::getNotArgument(
1745 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1746 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1747
1748 // Unconditionally set register to 0
1749 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(notI));
1750
1751 // Now conditionally move 1 into the register.
1752 // Mark the register as a use (as well as a def) because the old
1753 // value will be retained if the condition is false.
1754 mvec.push_back(BuildMI(V9::MOVRZi, 3).addReg(notArg).addZImm(1)
1755 .addReg(notI, MOTy::UseAndDef));
1756
1757 break;
1758 }
1759
1760 case 421: // reg: BNot(reg,reg): Compute as reg = reg XOR-NOT 0
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001761 { // First find the unary operand. It may be left or right, usually right.
1762 Value* notArg = BinaryOperator::getNotArgument(
1763 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001764 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001765 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001766 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001767 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001768 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001769
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001770 case 322: // reg: Not(tobool, reg):
1771 // Fold CAST-TO-BOOL with NOT by inverting the sense of cast-to-bool
1772 foldCase = true;
1773 // Just fall through!
1774
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001775 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001776 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001777 Instruction* castI = subtreeRoot->getInstruction();
1778 Value* opVal = subtreeRoot->leftChild()->getValue();
1779 assert(opVal->getType()->isIntegral() ||
1780 isa<PointerType>(opVal->getType()));
1781
1782 // Unconditionally set register to 0
1783 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(castI));
1784
1785 // Now conditionally move 1 into the register.
1786 // Mark the register as a use (as well as a def) because the old
1787 // value will be retained if the condition is false.
1788 MachineOpCode opCode = foldCase? V9::MOVRZi : V9::MOVRNZi;
1789 mvec.push_back(BuildMI(opCode, 3).addReg(opVal).addZImm(1)
1790 .addReg(castI, MOTy::UseAndDef));
1791
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001792 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001793 }
1794
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001795 case 23: // reg: ToUByteTy(reg)
1796 case 24: // reg: ToSByteTy(reg)
1797 case 25: // reg: ToUShortTy(reg)
1798 case 26: // reg: ToShortTy(reg)
1799 case 27: // reg: ToUIntTy(reg)
1800 case 28: // reg: ToIntTy(reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001801 case 29: // reg: ToULongTy(reg)
1802 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001803 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001804 //======================================================================
1805 // Rules for integer conversions:
1806 //
1807 //--------
1808 // From ISO 1998 C++ Standard, Sec. 4.7:
1809 //
1810 // 2. If the destination type is unsigned, the resulting value is
1811 // the least unsigned integer congruent to the source integer
1812 // (modulo 2n where n is the number of bits used to represent the
1813 // unsigned type). [Note: In a two s complement representation,
1814 // this conversion is conceptual and there is no change in the
1815 // bit pattern (if there is no truncation). ]
1816 //
1817 // 3. If the destination type is signed, the value is unchanged if
1818 // it can be represented in the destination type (and bitfield width);
1819 // otherwise, the value is implementation-defined.
1820 //--------
1821 //
1822 // Since we assume 2s complement representations, this implies:
1823 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001824 // -- If operand is smaller than destination, zero-extend or sign-extend
1825 // according to the signedness of the *operand*: source decides:
1826 // (1) If operand is signed, sign-extend it.
1827 // If dest is unsigned, zero-ext the result!
1828 // (2) If operand is unsigned, our current invariant is that
1829 // it's high bits are correct, so zero-extension is not needed.
Vikram S. Adve94c40812002-09-27 14:33:08 +00001830 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001831 // -- If operand is same size as or larger than destination,
1832 // zero-extend or sign-extend according to the signedness of
1833 // the *destination*: destination decides:
1834 // (1) If destination is signed, sign-extend (truncating if needed)
1835 // This choice is implementation defined. We sign-extend the
1836 // operand, which matches both Sun's cc and gcc3.2.
1837 // (2) If destination is unsigned, zero-extend (truncating if needed)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001838 //======================================================================
1839
Vikram S. Adve242a8082002-05-19 15:25:51 +00001840 Instruction* destI = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001841 Function* currentFunc = destI->getParent()->getParent();
1842 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(destI);
1843
Vikram S. Adve242a8082002-05-19 15:25:51 +00001844 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001845 const Type* opType = opVal->getType();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001846 const Type* destType = destI->getType();
1847 unsigned opSize = target.getTargetData().getTypeSize(opType);
1848 unsigned destSize = target.getTargetData().getTypeSize(destType);
1849
1850 bool isIntegral = opType->isIntegral() || isa<PointerType>(opType);
1851
1852 if (opType == Type::BoolTy ||
1853 opType == destType ||
1854 isIntegral && opSize == destSize && opSize == 8) {
1855 // nothing to do in all these cases
1856 forwardOperandNum = 0; // forward first operand to user
1857
Misha Brukman7b647942003-05-30 20:11:56 +00001858 } else if (opType->isFloatingPoint()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001859
1860 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Advee895a742003-08-06 18:48:40 +00001861 if (destI->getType()->isUnsigned() && destI->getType() !=Type::UIntTy)
Misha Brukman7b647942003-05-30 20:11:56 +00001862 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve1e606692002-07-31 21:01:34 +00001863
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001864 } else if (isIntegral) {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001865
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001866 bool opSigned = opType->isSigned();
1867 bool destSigned = destType->isSigned();
1868 unsigned extSourceInBits = 8 * std::min<unsigned>(opSize, destSize);
1869
1870 assert(! (opSize == destSize && opSigned == destSigned) &&
1871 "How can different int types have same size and signedness?");
1872
1873 bool signExtend = (opSize < destSize && opSigned ||
1874 opSize >= destSize && destSigned);
1875
1876 bool signAndZeroExtend = (opSize < destSize && destSize < 8u &&
1877 opSigned && !destSigned);
1878 assert(!signAndZeroExtend || signExtend);
1879
1880 bool zeroExtendOnly = opSize >= destSize && !destSigned;
1881 assert(!zeroExtendOnly || !signExtend);
1882
1883 if (signExtend) {
1884 Value* signExtDest = (signAndZeroExtend
1885 ? new TmpInstruction(mcfi, destType, opVal)
1886 : destI);
1887
1888 target.getInstrInfo().CreateSignExtensionInstructions
1889 (target, currentFunc,opVal,signExtDest,extSourceInBits,mvec,mcfi);
1890
1891 if (signAndZeroExtend)
1892 target.getInstrInfo().CreateZeroExtensionInstructions
1893 (target, currentFunc, signExtDest, destI, 8*destSize, mvec, mcfi);
1894 }
1895 else if (zeroExtendOnly) {
1896 target.getInstrInfo().CreateZeroExtensionInstructions
1897 (target, currentFunc, opVal, destI, extSourceInBits, mvec, mcfi);
1898 }
1899 else
1900 forwardOperandNum = 0; // forward first operand to user
1901
Misha Brukman7b647942003-05-30 20:11:56 +00001902 } else
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001903 assert(0 && "Unrecognized operand type for convert-to-integer");
1904
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001905 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001906 }
1907
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001908 case 31: // reg: ToFloatTy(reg):
1909 case 32: // reg: ToDoubleTy(reg):
1910 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001911
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001912 // If this instruction has a parent (a user) in the tree
1913 // and the user is translated as an FsMULd instruction,
1914 // then the cast is unnecessary. So check that first.
1915 // In the future, we'll want to do the same for the FdMULq instruction,
1916 // so do the check here instead of only for ToFloatTy(reg).
1917 //
1918 if (subtreeRoot->parent() != NULL) {
1919 const MachineCodeForInstruction& mcfi =
1920 MachineCodeForInstruction::get(
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001921 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001922 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
1923 forwardOperandNum = 0; // forward first operand to user
1924 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001925
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001926 if (forwardOperandNum != 0) { // we do need the cast
1927 Value* leftVal = subtreeRoot->leftChild()->getValue();
1928 const Type* opType = leftVal->getType();
Vikram S. Advee895a742003-08-06 18:48:40 +00001929 MachineOpCode opCode=ChooseConvertToFloatInstr(target,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001930 subtreeRoot->getOpLabel(), opType);
Vikram S. Advee895a742003-08-06 18:48:40 +00001931 if (opCode == V9::NOP) { // no conversion needed
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001932 forwardOperandNum = 0; // forward first operand to user
1933 } else {
1934 // If the source operand is a non-FP type it must be
1935 // first copied from int to float register via memory!
1936 Instruction *dest = subtreeRoot->getInstruction();
1937 Value* srcForCast;
1938 int n = 0;
1939 if (! opType->isFloatingPoint()) {
1940 // Create a temporary to represent the FP register
1941 // into which the integer will be copied via memory.
1942 // The type of this temporary will determine the FP
1943 // register used: single-prec for a 32-bit int or smaller,
1944 // double-prec for a 64-bit int.
1945 //
1946 uint64_t srcSize =
1947 target.getTargetData().getTypeSize(leftVal->getType());
1948 Type* tmpTypeToUse =
1949 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001950 MachineCodeForInstruction &destMCFI =
1951 MachineCodeForInstruction::get(dest);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001952 srcForCast = new TmpInstruction(destMCFI, tmpTypeToUse, dest);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001953
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001954 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001955 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001956 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001957 mvec, destMCFI);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001958 } else
1959 srcForCast = leftVal;
1960
1961 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
1962 mvec.push_back(M);
1963 }
Misha Brukman7b647942003-05-30 20:11:56 +00001964 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001965 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001966
1967 case 19: // reg: ToArrayTy(reg):
1968 case 20: // reg: ToPointerTy(reg):
1969 forwardOperandNum = 0; // forward first operand to user
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001970 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001971
1972 case 233: // reg: Add(reg, Constant)
1973 maskUnsignedResult = true;
1974 M = CreateAddConstInstruction(subtreeRoot);
1975 if (M != NULL) {
1976 mvec.push_back(M);
1977 break;
1978 }
1979 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001980
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001981 case 33: // reg: Add(reg, reg)
1982 maskUnsignedResult = true;
1983 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
1984 break;
1985
1986 case 234: // reg: Sub(reg, Constant)
1987 maskUnsignedResult = true;
1988 M = CreateSubConstInstruction(subtreeRoot);
1989 if (M != NULL) {
1990 mvec.push_back(M);
1991 break;
1992 }
1993 // ELSE FALL THROUGH
1994
1995 case 34: // reg: Sub(reg, reg)
1996 maskUnsignedResult = true;
1997 Add3OperandInstr(ChooseSubInstructionByType(
Chris Lattner54e898e2003-01-15 19:23:34 +00001998 subtreeRoot->getInstruction()->getType()),
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001999 subtreeRoot, mvec);
2000 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002001
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002002 case 135: // reg: Mul(todouble, todouble)
2003 checkCast = true;
2004 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002005
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002006 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002007 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002008 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002009 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002010 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002011 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002012 Instruction* mulInstr = subtreeRoot->getInstruction();
2013 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002014 subtreeRoot->leftChild()->getValue(),
2015 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002016 mulInstr, mvec,
2017 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002018 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002019 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002020 case 335: // reg: Mul(todouble, todoubleConst)
2021 checkCast = true;
2022 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002023
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002024 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00002025 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002026 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002027 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002028 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002029 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002030 Instruction* mulInstr = subtreeRoot->getInstruction();
2031 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002032 subtreeRoot->leftChild()->getValue(),
2033 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002034 mulInstr, mvec,
2035 MachineCodeForInstruction::get(mulInstr),
2036 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002037 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002038 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002039 case 236: // reg: Div(reg, Constant)
2040 maskUnsignedResult = true;
2041 L = mvec.size();
2042 CreateDivConstInstruction(target, subtreeRoot, mvec);
2043 if (mvec.size() > L)
2044 break;
2045 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002046
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002047 case 36: // reg: Div(reg, reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002048 {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002049 maskUnsignedResult = true;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002050
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002051 // If either operand of divide is smaller than 64 bits, we have
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002052 // to make sure the unused top bits are correct because they affect
2053 // the result. These bits are already correct for unsigned values.
2054 // They may be incorrect for signed values, so sign extend to fill in.
2055 Instruction* divI = subtreeRoot->getInstruction();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002056 Value* divOp1 = subtreeRoot->leftChild()->getValue();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002057 Value* divOp2 = subtreeRoot->rightChild()->getValue();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002058 Value* divOp1ToUse = divOp1;
2059 Value* divOp2ToUse = divOp2;
2060 if (divI->getType()->isSigned()) {
2061 unsigned opSize=target.getTargetData().getTypeSize(divI->getType());
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002062 if (opSize < 8) {
2063 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(divI);
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002064 divOp1ToUse = new TmpInstruction(mcfi, divOp1);
2065 divOp2ToUse = new TmpInstruction(mcfi, divOp2);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002066 target.getInstrInfo().
2067 CreateSignExtensionInstructions(target,
2068 divI->getParent()->getParent(),
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002069 divOp1, divOp1ToUse,
2070 8*opSize, mvec, mcfi);
2071 target.getInstrInfo().
2072 CreateSignExtensionInstructions(target,
2073 divI->getParent()->getParent(),
2074 divOp2, divOp2ToUse,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002075 8*opSize, mvec, mcfi);
2076 }
2077 }
2078
2079 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002080 .addReg(divOp1ToUse)
2081 .addReg(divOp2ToUse)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002082 .addRegDef(divI));
2083
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002084 break;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002085 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002086
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002087 case 37: // reg: Rem(reg, reg)
2088 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00002089 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002090 maskUnsignedResult = true;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002091
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002092 Instruction* remI = subtreeRoot->getInstruction();
2093 Value* divOp1 = subtreeRoot->leftChild()->getValue();
2094 Value* divOp2 = subtreeRoot->rightChild()->getValue();
2095
2096 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(remI);
Vikram S. Adve510eec72001-11-04 21:59:14 +00002097
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002098 // If second operand of divide is smaller than 64 bits, we have
2099 // to make sure the unused top bits are correct because they affect
2100 // the result. These bits are already correct for unsigned values.
2101 // They may be incorrect for signed values, so sign extend to fill in.
2102 //
2103 Value* divOpToUse = divOp2;
2104 if (divOp2->getType()->isSigned()) {
2105 unsigned opSize=target.getTargetData().getTypeSize(divOp2->getType());
2106 if (opSize < 8) {
2107 divOpToUse = new TmpInstruction(mcfi, divOp2);
2108 target.getInstrInfo().
2109 CreateSignExtensionInstructions(target,
2110 remI->getParent()->getParent(),
2111 divOp2, divOpToUse,
2112 8*opSize, mvec, mcfi);
2113 }
2114 }
2115
2116 // Now compute: result = rem V1, V2 as:
2117 // result = V1 - (V1 / signExtend(V2)) * signExtend(V2)
2118 //
2119 TmpInstruction* quot = new TmpInstruction(mcfi, divOp1, divOpToUse);
2120 TmpInstruction* prod = new TmpInstruction(mcfi, quot, divOpToUse);
2121
2122 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
2123 .addReg(divOp1).addReg(divOpToUse).addRegDef(quot));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002124
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002125 mvec.push_back(BuildMI(ChooseMulInstructionByType(remI->getType()), 3)
2126 .addReg(quot).addReg(divOpToUse).addRegDef(prod));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002127
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002128 mvec.push_back(BuildMI(ChooseSubInstructionByType(remI->getType()), 3)
2129 .addReg(divOp1).addReg(prod).addRegDef(remI));
2130
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002131 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00002132 }
2133
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002134 case 38: // bool: And(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002135 case 138: // bool: And(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002136 case 238: // bool: And(bool, boolconst)
2137 case 338: // reg : BAnd(reg, reg)
2138 case 538: // reg : BAnd(reg, Constant)
2139 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
2140 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002141
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002142 case 438: // bool: BAnd(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002143 { // Use the argument of NOT as the second argument!
2144 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002145 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002146 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2147 Value* notArg = BinaryOperator::getNotArgument(
2148 cast<BinaryOperator>(notNode->getInstruction()));
2149 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002150 Value *lhs = subtreeRoot->leftChild()->getValue();
2151 Value *dest = subtreeRoot->getValue();
2152 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(lhs).addReg(notArg)
2153 .addReg(dest, MOTy::Def));
2154
Misha Brukmanb461d372003-10-23 16:48:30 +00002155 if (notArg->getType() == Type::BoolTy) {
2156 // set 1 in result register if result of above is non-zero
2157 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2158 .addReg(dest, MOTy::UseAndDef));
2159 }
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002160
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002161 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002162 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002163
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002164 case 39: // bool: Or(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002165 case 139: // bool: Or(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002166 case 239: // bool: Or(bool, boolconst)
2167 case 339: // reg : BOr(reg, reg)
2168 case 539: // reg : BOr(reg, Constant)
2169 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
2170 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002171
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002172 case 439: // bool: BOr(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002173 { // Use the argument of NOT as the second argument!
2174 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002175 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002176 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2177 Value* notArg = BinaryOperator::getNotArgument(
2178 cast<BinaryOperator>(notNode->getInstruction()));
2179 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002180 Value *lhs = subtreeRoot->leftChild()->getValue();
2181 Value *dest = subtreeRoot->getValue();
2182
2183 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(lhs).addReg(notArg)
2184 .addReg(dest, MOTy::Def));
2185
Misha Brukmanb461d372003-10-23 16:48:30 +00002186 if (notArg->getType() == Type::BoolTy) {
2187 // set 1 in result register if result of above is non-zero
2188 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2189 .addReg(dest, MOTy::UseAndDef));
2190 }
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002191
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002192 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002193 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002194
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002195 case 40: // bool: Xor(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002196 case 140: // bool: Xor(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002197 case 240: // bool: Xor(bool, boolconst)
2198 case 340: // reg : BXor(reg, reg)
2199 case 540: // reg : BXor(reg, Constant)
2200 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
2201 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002202
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002203 case 440: // bool: BXor(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002204 { // Use the argument of NOT as the second argument!
2205 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002206 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002207 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2208 Value* notArg = BinaryOperator::getNotArgument(
2209 cast<BinaryOperator>(notNode->getInstruction()));
2210 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002211 Value *lhs = subtreeRoot->leftChild()->getValue();
2212 Value *dest = subtreeRoot->getValue();
2213 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(lhs).addReg(notArg)
2214 .addReg(dest, MOTy::Def));
2215
Misha Brukmanb461d372003-10-23 16:48:30 +00002216 if (notArg->getType() == Type::BoolTy) {
2217 // set 1 in result register if result of above is non-zero
2218 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2219 .addReg(dest, MOTy::UseAndDef));
2220 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002221 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002222 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002223
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002224 case 41: // setCCconst: SetCC(reg, Constant)
2225 { // Comparison is with a constant:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002226 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002227 // If the bool result must be computed into a register (see below),
2228 // and the constant is int ZERO, we can use the MOVR[op] instructions
2229 // and avoid the SUBcc instruction entirely.
2230 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002231 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002232 // The result of the SetCC must be computed and stored in a register if
2233 // it is used outside the current basic block (so it must be computed
2234 // as a boolreg) or it is used by anything other than a branch.
2235 // We will use a conditional move to do this.
2236 //
2237 Instruction* setCCInstr = subtreeRoot->getInstruction();
2238 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2239 ! AllUsesAreBranches(setCCInstr));
2240
Misha Brukmanb461d372003-10-23 16:48:30 +00002241 if (computeBoolVal) {
2242 InstrTreeNode* constNode = subtreeRoot->rightChild();
2243 assert(constNode &&
2244 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
2245 Constant *constVal = cast<Constant>(constNode->getValue());
2246 bool isValidConst;
2247
2248 if ((constVal->getType()->isInteger()
2249 || isa<PointerType>(constVal->getType()))
2250 && target.getInstrInfo().ConvertConstantToIntType(target,
Vikram S. Advee6124d32003-07-29 19:59:23 +00002251 constVal, constVal->getType(), isValidConst) == 0
Misha Brukmanb461d372003-10-23 16:48:30 +00002252 && isValidConst)
2253 {
2254 // That constant is an integer zero after all...
2255 // Use a MOVR[op] to compute the boolean result
2256 // Unconditionally set register to 0
2257 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0)
2258 .addRegDef(setCCInstr));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002259
Misha Brukmanb461d372003-10-23 16:48:30 +00002260 // Now conditionally move 1 into the register.
2261 // Mark the register as a use (as well as a def) because the old
2262 // value will be retained if the condition is false.
2263 MachineOpCode movOpCode = ChooseMovpregiForSetCC(subtreeRoot);
2264 mvec.push_back(BuildMI(movOpCode, 3)
2265 .addReg(subtreeRoot->leftChild()->getValue())
2266 .addZImm(1).addReg(setCCInstr, MOTy::UseAndDef));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002267
Misha Brukmanb461d372003-10-23 16:48:30 +00002268 break;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002269 }
Misha Brukmanb461d372003-10-23 16:48:30 +00002270 }
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002271 // ELSE FALL THROUGH
2272 }
2273
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002274 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002275 {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002276 // This generates a SUBCC instruction, putting the difference in a
2277 // result reg. if needed, and/or setting a condition code if needed.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002278 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002279 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002280 Value* leftVal = subtreeRoot->leftChild()->getValue();
2281 Value* rightVal = subtreeRoot->rightChild()->getValue();
2282 const Type* opType = leftVal->getType();
2283 bool isFPCompare = opType->isFloatingPoint();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002284
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002285 // If the boolean result of the SetCC is used outside the current basic
2286 // block (so it must be computed as a boolreg) or is used by anything
2287 // other than a branch, the boolean must be computed and stored
2288 // in a result register. We will use a conditional move to do this.
2289 //
2290 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2291 ! AllUsesAreBranches(setCCInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002292
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002293 // A TmpInstruction is created to represent the CC "result".
2294 // Unlike other instances of TmpInstruction, this one is used
2295 // by machine code of multiple LLVM instructions, viz.,
2296 // the SetCC and the branch. Make sure to get the same one!
2297 // Note that we do this even for FP CC registers even though they
2298 // are explicit operands, because the type of the operand
2299 // needs to be a floating point condition code, not an integer
2300 // condition code. Think of this as casting the bool result to
2301 // a FP condition code register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002302 // Later, we mark the 4th operand as being a CC register, and as a def.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002303 //
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002304 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002305 setCCInstr->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00002306 leftVal->getType(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002307 MachineCodeForInstruction::get(setCCInstr));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002308
2309 // If the operands are signed values smaller than 4 bytes, then they
2310 // must be sign-extended in order to do a valid 32-bit comparison
2311 // and get the right result in the 32-bit CC register (%icc).
2312 //
2313 Value* leftOpToUse = leftVal;
2314 Value* rightOpToUse = rightVal;
2315 if (opType->isIntegral() && opType->isSigned()) {
2316 unsigned opSize = target.getTargetData().getTypeSize(opType);
2317 if (opSize < 4) {
2318 MachineCodeForInstruction& mcfi =
2319 MachineCodeForInstruction::get(setCCInstr);
2320
2321 // create temporary virtual regs. to hold the sign-extensions
2322 leftOpToUse = new TmpInstruction(mcfi, leftVal);
2323 rightOpToUse = new TmpInstruction(mcfi, rightVal);
2324
2325 // sign-extend each operand and put the result in the temporary reg.
2326 target.getInstrInfo().CreateSignExtensionInstructions
2327 (target, setCCInstr->getParent()->getParent(),
2328 leftVal, leftOpToUse, 8*opSize, mvec, mcfi);
2329 target.getInstrInfo().CreateSignExtensionInstructions
2330 (target, setCCInstr->getParent()->getParent(),
2331 rightVal, rightOpToUse, 8*opSize, mvec, mcfi);
2332 }
2333 }
2334
Misha Brukman7b647942003-05-30 20:11:56 +00002335 if (! isFPCompare) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002336 // Integer condition: set CC and discard result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002337 mvec.push_back(BuildMI(V9::SUBccr, 4)
2338 .addReg(leftOpToUse)
2339 .addReg(rightOpToUse)
2340 .addMReg(target.getRegInfo().getZeroRegNum(),MOTy::Def)
2341 .addCCReg(tmpForCC, MOTy::Def));
Misha Brukman7b647942003-05-30 20:11:56 +00002342 } else {
2343 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002344 mvec.push_back(BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2345 .addCCReg(tmpForCC, MOTy::Def)
2346 .addReg(leftOpToUse)
2347 .addReg(rightOpToUse));
Misha Brukman7b647942003-05-30 20:11:56 +00002348 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002349
Misha Brukman7b647942003-05-30 20:11:56 +00002350 if (computeBoolVal) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002351 MachineOpCode movOpCode = (isFPCompare
Misha Brukmaneecdb662003-06-02 20:55:14 +00002352 ? ChooseMovFpcciInstruction(subtreeRoot)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002353 : ChooseMovpcciForSetCC(subtreeRoot));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002354
2355 // Unconditionally set register to 0
2356 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
2357 mvec.push_back(M);
2358
2359 // Now conditionally move 1 into the register.
Misha Brukman7b647942003-05-30 20:11:56 +00002360 // Mark the register as a use (as well as a def) because the old
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002361 // value will be retained if the condition is false.
2362 M = (BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(1)
2363 .addReg(setCCInstr, MOTy::UseAndDef));
Misha Brukman7b647942003-05-30 20:11:56 +00002364 mvec.push_back(M);
2365 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002366 break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002367 }
2368
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002369 case 51: // reg: Load(reg)
2370 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002371 SetOperandsForMemInstr(ChooseLoadInstruction(
2372 subtreeRoot->getValue()->getType()),
2373 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002374 break;
2375
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002376 case 55: // reg: GetElemPtr(reg)
2377 case 56: // reg: GetElemPtrIdx(reg,reg)
2378 // If the GetElemPtr was folded into the user (parent), it will be
2379 // caught above. For other cases, we have to compute the address.
2380 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
2381 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002382
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002383 case 57: // reg: Alloca: Implement as 1 instruction:
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002384 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002385 AllocationInst* instr =
2386 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002387 unsigned tsize =
2388 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002389 assert(tsize != 0);
2390 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002391 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002392 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002393
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002394 case 58: // reg: Alloca(reg): Implement as 3 instructions:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002395 // mul num, typeSz -> tmp
2396 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002397 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002398 AllocationInst* instr =
2399 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002400 const Type* eltType = instr->getAllocatedType();
2401
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002402 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002403 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002404 Value* numElementsVal = NULL;
2405 bool isArray = instr->isArrayAllocation();
2406
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002407 if (!isArray || isa<Constant>(numElementsVal = instr->getArraySize())) {
Misha Brukman7b647942003-05-30 20:11:56 +00002408 // total size is constant: generate code for fixed-size alloca
2409 unsigned numElements = isArray?
2410 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2411 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2412 numElements, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002413 } else {
2414 // total size is not constant.
Vikram S. Adve74825322002-03-18 03:15:35 +00002415 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002416 numElementsVal, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002417 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002418 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002419 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002420
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002421 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002422 { // Generate a direct (CALL) or indirect (JMPL) call.
2423 // Mark the return-address register, the indirection
2424 // register (for indirect calls), the operands of the Call,
2425 // and the return value (if any) as implicit operands
2426 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002427 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002428 // If this is a varargs function, floating point arguments
2429 // have to passed in integer registers so insert
2430 // copy-float-to-int instructions for each float operand.
2431 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002432 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002433 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002434 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002435
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002436 // Check if this is an intrinsic function that needs a special code
2437 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002438 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002439 bool specialIntrinsic = false;
Brian Gaeked0fde302003-11-11 22:41:34 +00002440 Intrinsic::ID iid;
2441 if (calledFunc && (iid=(Intrinsic::ID)calledFunc->getIntrinsicID()))
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002442 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002443
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002444 // If not, generate the normal call sequence for the function.
2445 // This can also handle any intrinsics that are just function calls.
2446 //
Misha Brukman7b647942003-05-30 20:11:56 +00002447 if (! specialIntrinsic) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002448 Function* currentFunc = callInstr->getParent()->getParent();
2449 MachineFunction& MF = MachineFunction::get(currentFunc);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002450 MachineCodeForInstruction& mcfi =
2451 MachineCodeForInstruction::get(callInstr);
Misha Brukmand71295a2003-12-17 22:04:00 +00002452 const SparcRegInfo& regInfo =
2453 (SparcRegInfo&) target.getRegInfo();
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002454 const TargetFrameInfo& frameInfo = target.getFrameInfo();
2455
Misha Brukman7b647942003-05-30 20:11:56 +00002456 // Create hidden virtual register for return address with type void*
2457 TmpInstruction* retAddrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002458 new TmpInstruction(mcfi, PointerType::get(Type::VoidTy), callInstr);
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002459
Misha Brukman7b647942003-05-30 20:11:56 +00002460 // Generate the machine instruction and its operands.
2461 // Use CALL for direct function calls; this optimistically assumes
2462 // the PC-relative address fits in the CALL address field (22 bits).
2463 // Use JMPL for indirect calls.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002464 // This will be added to mvec later, after operand copies.
Misha Brukman7b647942003-05-30 20:11:56 +00002465 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002466 MachineInstr* callMI;
Misha Brukman7b647942003-05-30 20:11:56 +00002467 if (calledFunc) // direct function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002468 callMI = BuildMI(V9::CALL, 1).addPCDisp(callee);
Misha Brukman7b647942003-05-30 20:11:56 +00002469 else // indirect function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002470 callMI = (BuildMI(V9::JMPLCALLi,3).addReg(callee)
2471 .addSImm((int64_t)0).addRegDef(retAddrReg));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002472
Misha Brukman7b647942003-05-30 20:11:56 +00002473 const FunctionType* funcType =
2474 cast<FunctionType>(cast<PointerType>(callee->getType())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002475 ->getElementType());
Misha Brukman7b647942003-05-30 20:11:56 +00002476 bool isVarArgs = funcType->isVarArg();
2477 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002478
Misha Brukman7b647942003-05-30 20:11:56 +00002479 // Use a descriptor to pass information about call arguments
2480 // to the register allocator. This descriptor will be "owned"
2481 // and freed automatically when the MachineCodeForInstruction
2482 // object for the callInstr goes away.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002483 CallArgsDescriptor* argDesc =
2484 new CallArgsDescriptor(callInstr, retAddrReg,isVarArgs,noPrototype);
Misha Brukman7b647942003-05-30 20:11:56 +00002485 assert(callInstr->getOperand(0) == callee
2486 && "This is assumed in the loop below!");
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002487
2488 // Insert sign-extension instructions for small signed values,
2489 // if this is an unknown function (i.e., called via a funcptr)
2490 // or an external one (i.e., which may not be compiled by llc).
2491 //
2492 if (calledFunc == NULL || calledFunc->isExternal()) {
2493 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
2494 Value* argVal = callInstr->getOperand(i);
2495 const Type* argType = argVal->getType();
2496 if (argType->isIntegral() && argType->isSigned()) {
2497 unsigned argSize = target.getTargetData().getTypeSize(argType);
2498 if (argSize <= 4) {
2499 // create a temporary virtual reg. to hold the sign-extension
2500 TmpInstruction* argExtend = new TmpInstruction(mcfi, argVal);
2501
2502 // sign-extend argVal and put the result in the temporary reg.
2503 target.getInstrInfo().CreateSignExtensionInstructions
2504 (target, currentFunc, argVal, argExtend,
2505 8*argSize, mvec, mcfi);
2506
2507 // replace argVal with argExtend in CallArgsDescriptor
2508 argDesc->getArgInfo(i-1).replaceArgVal(argExtend);
2509 }
2510 }
2511 }
2512 }
2513
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002514 // Insert copy instructions to get all the arguments into
2515 // all the places that they need to be.
2516 //
Misha Brukman7b647942003-05-30 20:11:56 +00002517 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002518 int argNo = i-1;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002519 CallArgInfo& argInfo = argDesc->getArgInfo(argNo);
2520 Value* argVal = argInfo.getArgVal(); // don't use callInstr arg here
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002521 const Type* argType = argVal->getType();
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002522 unsigned regType = regInfo.getRegTypeForDataType(argType);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002523 unsigned argSize = target.getTargetData().getTypeSize(argType);
2524 int regNumForArg = TargetRegInfo::getInvalidRegNum();
2525 unsigned regClassIDOfArgReg;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002526
Misha Brukman7b647942003-05-30 20:11:56 +00002527 // Check for FP arguments to varargs functions.
2528 // Any such argument in the first $K$ args must be passed in an
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002529 // integer register. If there is no prototype, it must also
2530 // be passed as an FP register.
2531 // K = #integer argument registers.
2532 bool isFPArg = argVal->getType()->isFloatingPoint();
2533 if (isVarArgs && isFPArg) {
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002534
2535 if (noPrototype) {
2536 // It is a function with no prototype: pass value
2537 // as an FP value as well as a varargs value. The FP value
2538 // may go in a register or on the stack. The copy instruction
2539 // to the outgoing reg/stack is created by the normal argument
2540 // handling code since this is the "normal" passing mode.
2541 //
2542 regNumForArg = regInfo.regNumForFPArg(regType,
2543 false, false, argNo,
2544 regClassIDOfArgReg);
2545 if (regNumForArg == regInfo.getInvalidRegNum())
2546 argInfo.setUseStackSlot();
2547 else
2548 argInfo.setUseFPArgReg();
2549 }
2550
2551 // If this arg. is in the first $K$ regs, add special copy-
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002552 // float-to-int instructions to pass the value as an int.
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002553 // To check if it is in the first $K$, get the register
2554 // number for the arg #i. These copy instructions are
2555 // generated here because they are extra cases and not needed
2556 // for the normal argument handling (some code reuse is
2557 // possible though -- later).
2558 //
Misha Brukmanea481cc2003-06-03 03:21:58 +00002559 int copyRegNum = regInfo.regNumForIntArg(false, false, argNo,
2560 regClassIDOfArgReg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002561 if (copyRegNum != regInfo.getInvalidRegNum()) {
2562 // Create a virtual register to represent copyReg. Mark
2563 // this vreg as being an implicit operand of the call MI
2564 const Type* loadTy = (argType == Type::FloatTy
2565 ? Type::IntTy : Type::LongTy);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002566 TmpInstruction* argVReg = new TmpInstruction(mcfi, loadTy,
2567 argVal, NULL,
2568 "argRegCopy");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002569 callMI->addImplicitRef(argVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002570
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002571 // Get a temp stack location to use to copy
2572 // float-to-int via the stack.
2573 //
2574 // FIXME: For now, we allocate permanent space because
2575 // the stack frame manager does not allow locals to be
2576 // allocated (e.g., for alloca) after a temp is
2577 // allocated!
2578 //
2579 // int tmpOffset = MF.getInfo()->pushTempValue(argSize);
2580 int tmpOffset = MF.getInfo()->allocateLocalVar(argVReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002581
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002582 // Generate the store from FP reg to stack
Misha Brukmanea481cc2003-06-03 03:21:58 +00002583 unsigned StoreOpcode = ChooseStoreInstruction(argType);
2584 M = BuildMI(convertOpcodeFromRegToImm(StoreOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002585 .addReg(argVal).addMReg(regInfo.getFramePointer())
2586 .addSImm(tmpOffset);
2587 mvec.push_back(M);
2588
2589 // Generate the load from stack to int arg reg
Misha Brukmanea481cc2003-06-03 03:21:58 +00002590 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
2591 M = BuildMI(convertOpcodeFromRegToImm(LoadOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002592 .addMReg(regInfo.getFramePointer()).addSImm(tmpOffset)
2593 .addReg(argVReg, MOTy::Def);
2594
2595 // Mark operand with register it should be assigned
2596 // both for copy and for the callMI
2597 M->SetRegForOperand(M->getNumOperands()-1, copyRegNum);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002598 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2599 copyRegNum);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002600 mvec.push_back(M);
2601
2602 // Add info about the argument to the CallArgsDescriptor
2603 argInfo.setUseIntArgReg();
2604 argInfo.setArgCopy(copyRegNum);
2605 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00002606 // Cannot fit in first $K$ regs so pass arg on stack
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002607 argInfo.setUseStackSlot();
2608 }
2609 } else if (isFPArg) {
2610 // Get the outgoing arg reg to see if there is one.
2611 regNumForArg = regInfo.regNumForFPArg(regType, false, false,
2612 argNo, regClassIDOfArgReg);
2613 if (regNumForArg == regInfo.getInvalidRegNum())
2614 argInfo.setUseStackSlot();
2615 else {
2616 argInfo.setUseFPArgReg();
2617 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2618 regNumForArg);
2619 }
2620 } else {
2621 // Get the outgoing arg reg to see if there is one.
2622 regNumForArg = regInfo.regNumForIntArg(false,false,
2623 argNo, regClassIDOfArgReg);
2624 if (regNumForArg == regInfo.getInvalidRegNum())
2625 argInfo.setUseStackSlot();
2626 else {
2627 argInfo.setUseIntArgReg();
2628 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2629 regNumForArg);
2630 }
2631 }
2632
2633 //
2634 // Now insert copy instructions to stack slot or arg. register
2635 //
2636 if (argInfo.usesStackSlot()) {
2637 // Get the stack offset for this argument slot.
2638 // FP args on stack are right justified so adjust offset!
2639 // int arguments are also right justified but they are
2640 // always loaded as a full double-word so the offset does
2641 // not need to be adjusted.
2642 int argOffset = frameInfo.getOutgoingArgOffset(MF, argNo);
2643 if (argType->isFloatingPoint()) {
2644 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
2645 assert(argSize <= slotSize && "Insufficient slot size!");
2646 argOffset += slotSize - argSize;
2647 }
2648
2649 // Now generate instruction to copy argument to stack
2650 MachineOpCode storeOpCode =
2651 (argType->isFloatingPoint()
2652 ? ((argSize == 4)? V9::STFi : V9::STDFi) : V9::STXi);
2653
2654 M = BuildMI(storeOpCode, 3).addReg(argVal)
2655 .addMReg(regInfo.getStackPointer()).addSImm(argOffset);
2656 mvec.push_back(M);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002657 }
2658 else if (regNumForArg != regInfo.getInvalidRegNum()) {
2659
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002660 // Create a virtual register to represent the arg reg. Mark
2661 // this vreg as being an implicit operand of the call MI.
2662 TmpInstruction* argVReg =
2663 new TmpInstruction(mcfi, argVal, NULL, "argReg");
2664
2665 callMI->addImplicitRef(argVReg);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002666
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002667 // Generate the reg-to-reg copy into the outgoing arg reg.
2668 // -- For FP values, create a FMOVS or FMOVD instruction
2669 // -- For non-FP values, create an add-with-0 instruction
2670 if (argType->isFloatingPoint())
2671 M=(BuildMI(argType==Type::FloatTy? V9::FMOVS :V9::FMOVD,2)
2672 .addReg(argVal).addReg(argVReg, MOTy::Def));
2673 else
2674 M = (BuildMI(ChooseAddInstructionByType(argType), 3)
2675 .addReg(argVal).addSImm((int64_t) 0)
2676 .addReg(argVReg, MOTy::Def));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002677
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002678 // Mark the operand with the register it should be assigned
2679 M->SetRegForOperand(M->getNumOperands()-1, regNumForArg);
2680 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2681 regNumForArg);
2682
2683 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +00002684 }
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002685 else
2686 assert(argInfo.getArgCopy() != regInfo.getInvalidRegNum() &&
2687 "Arg. not in stack slot, primary or secondary register?");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002688 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002689
2690 // add call instruction and delay slot before copying return value
2691 mvec.push_back(callMI);
2692 mvec.push_back(BuildMI(V9::NOP, 0));
2693
Misha Brukman7b647942003-05-30 20:11:56 +00002694 // Add the return value as an implicit ref. The call operands
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002695 // were added above. Also, add code to copy out the return value.
2696 // This is always register-to-register for int or FP return values.
2697 //
2698 if (callInstr->getType() != Type::VoidTy) {
2699 // Get the return value reg.
2700 const Type* retType = callInstr->getType();
2701
2702 int regNum = (retType->isFloatingPoint()
2703 ? (unsigned) SparcFloatRegClass::f0
2704 : (unsigned) SparcIntRegClass::o0);
2705 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
2706 regNum = regInfo.getUnifiedRegNum(regClassID, regNum);
2707
2708 // Create a virtual register to represent it and mark
2709 // this vreg as being an implicit operand of the call MI
2710 TmpInstruction* retVReg =
2711 new TmpInstruction(mcfi, callInstr, NULL, "argReg");
2712
2713 callMI->addImplicitRef(retVReg, /*isDef*/ true);
2714
2715 // Generate the reg-to-reg copy from the return value reg.
2716 // -- For FP values, create a FMOVS or FMOVD instruction
2717 // -- For non-FP values, create an add-with-0 instruction
2718 if (retType->isFloatingPoint())
2719 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
2720 .addReg(retVReg).addReg(callInstr, MOTy::Def));
2721 else
2722 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
2723 .addReg(retVReg).addSImm((int64_t) 0)
2724 .addReg(callInstr, MOTy::Def));
2725
2726 // Mark the operand with the register it should be assigned
2727 // Also mark the implicit ref of the call defining this operand
2728 M->SetRegForOperand(0, regNum);
2729 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,regNum);
2730
2731 mvec.push_back(M);
2732 }
2733
Misha Brukman7b647942003-05-30 20:11:56 +00002734 // For the CALL instruction, the ret. addr. reg. is also implicit
2735 if (isa<Function>(callee))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002736 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
2737
2738 MF.getInfo()->popAllTempValues(); // free temps used for this inst
Misha Brukman7b647942003-05-30 20:11:56 +00002739 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002740
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002741 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002742 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002743
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002744 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002745 {
2746 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2747 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2748 Instruction* shlInstr = subtreeRoot->getInstruction();
2749
2750 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002751 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2752 "Shl unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002753 unsigned opSize = target.getTargetData().getTypeSize(opType);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002754
2755 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Vikram S. Advee895a742003-08-06 18:48:40 +00002756 (opSize > 4)? V9::SLLXr6:V9::SLLr5,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002757 argVal1, argVal2, 0, shlInstr, mvec,
2758 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002759 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002760 }
2761
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002762 case 63: // reg: Shr(reg, reg)
Misha Brukman7b647942003-05-30 20:11:56 +00002763 {
2764 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002765 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2766 "Shr unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002767 unsigned opSize = target.getTargetData().getTypeSize(opType);
Chris Lattner54e898e2003-01-15 19:23:34 +00002768 Add3OperandInstr(opType->isSigned()
Vikram S. Advee895a742003-08-06 18:48:40 +00002769 ? (opSize > 4? V9::SRAXr6 : V9::SRAr5)
2770 : (opSize > 4? V9::SRLXr6 : V9::SRLr5),
Chris Lattner54e898e2003-01-15 19:23:34 +00002771 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002772 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002773 }
2774
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002775 case 64: // reg: Phi(reg,reg)
2776 break; // don't forward the value
Vikram S. Adve74825322002-03-18 03:15:35 +00002777
Vikram S. Adve40dee512003-10-21 11:25:09 +00002778 case 65: // reg: VANext(reg): the va_next(va_list, type) instruction
2779 { // Increment the va_list pointer register according to the type.
2780 // All LLVM argument types are <= 64 bits, so use one doubleword.
2781 Instruction* vaNextI = subtreeRoot->getInstruction();
2782 assert(target.getTargetData().getTypeSize(vaNextI->getType()) <= 8 &&
2783 "We assumed that all LLVM parameter types <= 8 bytes!");
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002784 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
Vikram S. Adve40dee512003-10-21 11:25:09 +00002785 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaNextI->getOperand(0)).
2786 addSImm(argSize).addRegDef(vaNextI));
Vikram S. Adve472c3042003-10-21 12:28:27 +00002787 break;
Vikram S. Adve40dee512003-10-21 11:25:09 +00002788 }
2789
2790 case 66: // reg: VAArg (reg): the va_arg instruction
2791 { // Load argument from stack using current va_list pointer value.
2792 // Use 64-bit load for all non-FP args, and LDDF or double for FP.
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002793 Instruction* vaArgI = subtreeRoot->getInstruction();
Vikram S. Adve40dee512003-10-21 11:25:09 +00002794 MachineOpCode loadOp = (vaArgI->getType()->isFloatingPoint()
2795 ? (vaArgI->getType() == Type::FloatTy
2796 ? V9::LDFi : V9::LDDFi)
2797 : V9::LDXi);
Vikram S. Adve9d275142003-08-12 03:04:05 +00002798 mvec.push_back(BuildMI(loadOp, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002799 addSImm(0).addRegDef(vaArgI));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002800 break;
2801 }
2802
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002803 case 71: // reg: VReg
2804 case 72: // reg: Constant
2805 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002806
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002807 default:
2808 assert(0 && "Unrecognized BURG rule");
2809 break;
2810 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002811 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002812
Misha Brukman7b647942003-05-30 20:11:56 +00002813 if (forwardOperandNum >= 0) {
2814 // We did not generate a machine instruction but need to use operand.
2815 // If user is in the same tree, replace Value in its machine operand.
2816 // If not, insert a copy instruction which should get coalesced away
2817 // by register allocation.
2818 if (subtreeRoot->parent() != NULL)
2819 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2820 else {
2821 std::vector<MachineInstr*> minstrVec;
2822 Instruction* instr = subtreeRoot->getInstruction();
2823 target.getInstrInfo().
2824 CreateCopyInstructionsByType(target,
2825 instr->getParent()->getParent(),
2826 instr->getOperand(forwardOperandNum),
2827 instr, minstrVec,
2828 MachineCodeForInstruction::get(instr));
2829 assert(minstrVec.size() > 0);
2830 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Chris Lattner20b1ea02001-09-14 03:47:57 +00002831 }
Misha Brukman7b647942003-05-30 20:11:56 +00002832 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002833
Misha Brukman7b647942003-05-30 20:11:56 +00002834 if (maskUnsignedResult) {
2835 // If result is unsigned and smaller than int reg size,
2836 // we need to clear high bits of result value.
2837 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2838 Instruction* dest = subtreeRoot->getInstruction();
2839 if (dest->getType()->isUnsigned()) {
2840 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
2841 if (destSize <= 4) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002842 // Mask high 64 - N bits, where N = 4*destSize.
2843
2844 // Use a TmpInstruction to represent the
Misha Brukman7b647942003-05-30 20:11:56 +00002845 // intermediate result before masking. Since those instructions
2846 // have already been generated, go back and substitute tmpI
2847 // for dest in the result position of each one of them.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002848 //
2849 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(dest);
2850 TmpInstruction *tmpI = new TmpInstruction(mcfi, dest->getType(),
2851 dest, NULL, "maskHi");
2852 Value* srlArgToUse = tmpI;
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002853
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002854 unsigned numSubst = 0;
2855 for (unsigned i=0, N=mvec.size(); i < N; ++i) {
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002856
2857 // Make sure we substitute all occurrences of dest in these instrs.
2858 // Otherwise, we will have bogus code.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002859 bool someArgsWereIgnored = false;
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002860
2861 // Make sure not to substitute an upwards-exposed use -- that would
2862 // introduce a use of `tmpI' with no preceding def. Therefore,
2863 // substitute a use or def-and-use operand only if a previous def
2864 // operand has already been substituted (i.e., numSusbt > 0).
2865 //
2866 numSubst += mvec[i]->substituteValue(dest, tmpI,
2867 /*defsOnly*/ numSubst == 0,
2868 /*notDefsAndUses*/ numSubst > 0,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002869 someArgsWereIgnored);
2870 assert(!someArgsWereIgnored &&
2871 "Operand `dest' exists but not replaced: probably bogus!");
2872 }
2873 assert(numSubst > 0 && "Operand `dest' not replaced: probably bogus!");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002874
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002875 // Left shift 32-N if size (N) is less than 32 bits.
Misha Brukman452db672003-09-23 17:28:11 +00002876 // Use another tmp. virtual register to represent this result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002877 if (destSize < 4) {
2878 srlArgToUse = new TmpInstruction(mcfi, dest->getType(),
2879 tmpI, NULL, "maskHi2");
2880 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpI)
2881 .addZImm(8*(4-destSize))
2882 .addReg(srlArgToUse, MOTy::Def));
2883 }
2884
2885 // Logical right shift 32-N to get zero extension in top 64-N bits.
2886 mvec.push_back(BuildMI(V9::SRLi5, 3).addReg(srlArgToUse)
2887 .addZImm(8*(4-destSize)).addReg(dest, MOTy::Def));
2888
Misha Brukman7b647942003-05-30 20:11:56 +00002889 } else if (destSize < 8) {
2890 assert(0 && "Unsupported type size: 32 < size < 64 bits");
2891 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002892 }
Misha Brukman7b647942003-05-30 20:11:56 +00002893 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002894}
Brian Gaeked0fde302003-11-11 22:41:34 +00002895
2896}