blob: 153bfc354c4a2025898a3bfc69f8c9459b7fd438 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000011#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerea45d7b2002-12-28 20:19:44 +000016#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner9c461082002-02-03 07:50:56 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000018#include "llvm/DerivedTypes.h"
Misha Brukmanb8db66e2003-08-07 15:43:46 +000019#include "llvm/Instructions.h"
20#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000021#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000022#include "llvm/ConstantHandling.h"
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +000023#include "llvm/Intrinsics.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000025#include <math.h>
Vikram S. Adve951df2b2003-07-10 20:07:54 +000026#include <algorithm>
Chris Lattner20b1ea02001-09-14 03:47:57 +000027
Chris Lattner54e898e2003-01-15 19:23:34 +000028static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +000029 std::vector<MachineInstr*>& mvec) {
Chris Lattner54e898e2003-01-15 19:23:34 +000030 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
31 .addReg(Node->rightChild()->getValue())
32 .addRegDef(Node->getValue()));
33}
34
35
36
Chris Lattner795ba6c2003-01-15 21:36:50 +000037//---------------------------------------------------------------------------
38// Function: GetMemInstArgs
39//
40// Purpose:
41// Get the pointer value and the index vector for a memory operation
42// (GetElementPtr, Load, or Store). If all indices of the given memory
43// operation are constant, fold in constant indices in a chain of
44// preceding GetElementPtr instructions (if any), and return the
45// pointer value of the first instruction in the chain.
46// All folded instructions are marked so no code is generated for them.
47//
48// Return values:
49// Returns the pointer Value to use.
50// Returns the resulting IndexVector in idxVec.
51// Returns true/false in allConstantIndices if all indices are/aren't const.
52//---------------------------------------------------------------------------
53
54
55//---------------------------------------------------------------------------
56// Function: FoldGetElemChain
57//
58// Purpose:
59// Fold a chain of GetElementPtr instructions containing only
60// constant offsets into an equivalent (Pointer, IndexVector) pair.
61// Returns the pointer Value, and stores the resulting IndexVector
62// in argument chainIdxVec. This is a helper function for
63// FoldConstantIndices that does the actual folding.
64//---------------------------------------------------------------------------
65
66
67// Check for a constant 0.
68inline bool
69IsZero(Value* idx)
70{
71 return (idx == ConstantSInt::getNullValue(idx->getType()));
72}
73
74static Value*
Misha Brukmanee563cb2003-05-21 17:59:06 +000075FoldGetElemChain(InstrTreeNode* ptrNode, std::vector<Value*>& chainIdxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +000076 bool lastInstHasLeadingNonZero)
77{
78 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
79 GetElementPtrInst* gepInst =
80 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
81
82 // ptr value is not computed in this tree or ptr value does not come from GEP
83 // instruction
84 if (gepInst == NULL)
85 return NULL;
86
87 // Return NULL if we don't fold any instructions in.
88 Value* ptrVal = NULL;
89
90 // Now chase the chain of getElementInstr instructions, if any.
91 // Check for any non-constant indices and stop there.
92 // Also, stop if the first index of child is a non-zero array index
93 // and the last index of the current node is a non-array index:
94 // in that case, a non-array declared type is being accessed as an array
95 // which is not type-safe, but could be legal.
96 //
97 InstructionNode* ptrChild = gepNode;
98 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
99 ptrChild->getOpLabel() == GetElemPtrIdx))
Misha Brukman81b06862003-05-21 18:48:06 +0000100 {
101 // Child is a GetElemPtr instruction
102 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
103 User::op_iterator OI, firstIdx = gepInst->idx_begin();
104 User::op_iterator lastIdx = gepInst->idx_end();
105 bool allConstantOffsets = true;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000106
Misha Brukman81b06862003-05-21 18:48:06 +0000107 // The first index of every GEP must be an array index.
108 assert((*firstIdx)->getType() == Type::LongTy &&
109 "INTERNAL ERROR: Structure index for a pointer type!");
Chris Lattner795ba6c2003-01-15 21:36:50 +0000110
Misha Brukman81b06862003-05-21 18:48:06 +0000111 // If the last instruction had a leading non-zero index, check if the
112 // current one references a sequential (i.e., indexable) type.
113 // If not, the code is not type-safe and we would create an illegal GEP
114 // by folding them, so don't fold any more instructions.
115 //
116 if (lastInstHasLeadingNonZero)
117 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
118 break; // cannot fold in any preceding getElementPtr instrs.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000119
Misha Brukman81b06862003-05-21 18:48:06 +0000120 // Check that all offsets are constant for this instruction
121 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
122 allConstantOffsets = isa<ConstantInt>(*OI);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000123
Misha Brukman81b06862003-05-21 18:48:06 +0000124 if (allConstantOffsets) {
125 // Get pointer value out of ptrChild.
126 ptrVal = gepInst->getPointerOperand();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000127
Misha Brukman81b06862003-05-21 18:48:06 +0000128 // Insert its index vector at the start, skipping any leading [0]
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000129 // Remember the old size to check if anything was inserted.
130 unsigned oldSize = chainIdxVec.size();
131 int firstIsZero = IsZero(*firstIdx);
132 chainIdxVec.insert(chainIdxVec.begin(), firstIdx + firstIsZero, lastIdx);
133
134 // Remember if it has leading zero index: it will be discarded later.
135 if (oldSize < chainIdxVec.size())
136 lastInstHasLeadingNonZero = !firstIsZero;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000137
Misha Brukman81b06862003-05-21 18:48:06 +0000138 // Mark the folded node so no code is generated for it.
139 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000140
Misha Brukman81b06862003-05-21 18:48:06 +0000141 // Get the previous GEP instruction and continue trying to fold
142 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
143 } else // cannot fold this getElementPtr instr. or any preceding ones
144 break;
145 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000146
147 // If the first getElementPtr instruction had a leading [0], add it back.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000148 // Note that this instruction is the *last* one that was successfully
149 // folded *and* contributed any indices, in the loop above.
150 //
Chris Lattner795ba6c2003-01-15 21:36:50 +0000151 if (ptrVal && ! lastInstHasLeadingNonZero)
152 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
153
154 return ptrVal;
155}
156
157
158//---------------------------------------------------------------------------
159// Function: GetGEPInstArgs
160//
161// Purpose:
162// Helper function for GetMemInstArgs that handles the final getElementPtr
163// instruction used by (or same as) the memory operation.
164// Extracts the indices of the current instruction and tries to fold in
165// preceding ones if all indices of the current one are constant.
166//---------------------------------------------------------------------------
167
168static Value *
169GetGEPInstArgs(InstructionNode* gepNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000170 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000171 bool& allConstantIndices)
172{
173 allConstantIndices = true;
174 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
175
176 // Default pointer is the one from the current instruction.
177 Value* ptrVal = gepI->getPointerOperand();
178 InstrTreeNode* ptrChild = gepNode->leftChild();
179
Misha Brukman452db672003-09-23 17:28:11 +0000180 // Extract the index vector of the GEP instruction.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000181 // If all indices are constant and first index is zero, try to fold
182 // in preceding GEPs with all constant indices.
183 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
184 allConstantIndices && OI != OE; ++OI)
185 if (! isa<Constant>(*OI))
186 allConstantIndices = false; // note: this also terminates loop!
187
188 // If we have only constant indices, fold chains of constant indices
189 // in this and any preceding GetElemPtr instructions.
190 bool foldedGEPs = false;
191 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
192 if (allConstantIndices)
Misha Brukman81b06862003-05-21 18:48:06 +0000193 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) {
194 ptrVal = newPtr;
195 foldedGEPs = true;
196 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000197
198 // Append the index vector of the current instruction.
199 // Skip the leading [0] index if preceding GEPs were folded into this.
200 idxVec.insert(idxVec.end(),
201 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
202 gepI->idx_end());
203
204 return ptrVal;
205}
206
207//---------------------------------------------------------------------------
208// Function: GetMemInstArgs
209//
210// Purpose:
211// Get the pointer value and the index vector for a memory operation
212// (GetElementPtr, Load, or Store). If all indices of the given memory
213// operation are constant, fold in constant indices in a chain of
214// preceding GetElementPtr instructions (if any), and return the
215// pointer value of the first instruction in the chain.
216// All folded instructions are marked so no code is generated for them.
217//
218// Return values:
219// Returns the pointer Value to use.
220// Returns the resulting IndexVector in idxVec.
221// Returns true/false in allConstantIndices if all indices are/aren't const.
222//---------------------------------------------------------------------------
223
224static Value*
225GetMemInstArgs(InstructionNode* memInstrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000226 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000227 bool& allConstantIndices)
228{
229 allConstantIndices = false;
230 Instruction* memInst = memInstrNode->getInstruction();
231 assert(idxVec.size() == 0 && "Need empty vector to return indices");
232
233 // If there is a GetElemPtr instruction to fold in to this instr,
234 // it must be in the left child for Load and GetElemPtr, and in the
235 // right child for Store instructions.
236 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
237 ? memInstrNode->rightChild()
238 : memInstrNode->leftChild());
239
240 // Default pointer is the one from the current instruction.
241 Value* ptrVal = ptrChild->getValue();
242
243 // Find the "last" GetElemPtr instruction: this one or the immediate child.
244 // There will be none if this is a load or a store from a scalar pointer.
245 InstructionNode* gepNode = NULL;
246 if (isa<GetElementPtrInst>(memInst))
247 gepNode = memInstrNode;
Misha Brukman81b06862003-05-21 18:48:06 +0000248 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) {
249 // Child of load/store is a GEP and memInst is its only use.
250 // Use its indices and mark it as folded.
251 gepNode = cast<InstructionNode>(ptrChild);
252 gepNode->markFoldedIntoParent();
253 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000254
255 // If there are no indices, return the current pointer.
256 // Else extract the pointer from the GEP and fold the indices.
257 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
258 : ptrVal;
259}
260
Chris Lattner54e898e2003-01-15 19:23:34 +0000261
Chris Lattner20b1ea02001-09-14 03:47:57 +0000262//************************ Internal Functions ******************************/
263
Chris Lattner20b1ea02001-09-14 03:47:57 +0000264
Chris Lattner20b1ea02001-09-14 03:47:57 +0000265static inline MachineOpCode
266ChooseBprInstruction(const InstructionNode* instrNode)
267{
268 MachineOpCode opCode;
269
270 Instruction* setCCInstr =
271 ((InstructionNode*) instrNode->leftChild())->getInstruction();
272
273 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000274 {
275 case Instruction::SetEQ: opCode = V9::BRZ; break;
276 case Instruction::SetNE: opCode = V9::BRNZ; break;
277 case Instruction::SetLE: opCode = V9::BRLEZ; break;
278 case Instruction::SetGE: opCode = V9::BRGEZ; break;
279 case Instruction::SetLT: opCode = V9::BRLZ; break;
280 case Instruction::SetGT: opCode = V9::BRGZ; break;
281 default:
282 assert(0 && "Unrecognized VM instruction!");
283 opCode = V9::INVALID_OPCODE;
284 break;
285 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000286
287 return opCode;
288}
289
290
291static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000292ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000293 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000294{
Misha Brukmana98cd452003-05-20 20:32:24 +0000295 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000296
297 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
298
Misha Brukman81b06862003-05-21 18:48:06 +0000299 if (isSigned) {
300 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000301 {
Misha Brukman81b06862003-05-21 18:48:06 +0000302 case Instruction::SetEQ: opCode = V9::BE; break;
303 case Instruction::SetNE: opCode = V9::BNE; break;
304 case Instruction::SetLE: opCode = V9::BLE; break;
305 case Instruction::SetGE: opCode = V9::BGE; break;
306 case Instruction::SetLT: opCode = V9::BL; break;
307 case Instruction::SetGT: opCode = V9::BG; break;
308 default:
309 assert(0 && "Unrecognized VM instruction!");
310 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000311 }
Misha Brukman81b06862003-05-21 18:48:06 +0000312 } else {
313 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000314 {
Misha Brukman81b06862003-05-21 18:48:06 +0000315 case Instruction::SetEQ: opCode = V9::BE; break;
316 case Instruction::SetNE: opCode = V9::BNE; break;
317 case Instruction::SetLE: opCode = V9::BLEU; break;
318 case Instruction::SetGE: opCode = V9::BCC; break;
319 case Instruction::SetLT: opCode = V9::BCS; break;
320 case Instruction::SetGT: opCode = V9::BGU; break;
321 default:
322 assert(0 && "Unrecognized VM instruction!");
323 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000324 }
Misha Brukman81b06862003-05-21 18:48:06 +0000325 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000326
327 return opCode;
328}
329
330static inline MachineOpCode
331ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000332 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000333{
Misha Brukmana98cd452003-05-20 20:32:24 +0000334 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000335
336 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000337 {
338 case Instruction::SetEQ: opCode = V9::FBE; break;
339 case Instruction::SetNE: opCode = V9::FBNE; break;
340 case Instruction::SetLE: opCode = V9::FBLE; break;
341 case Instruction::SetGE: opCode = V9::FBGE; break;
342 case Instruction::SetLT: opCode = V9::FBL; break;
343 case Instruction::SetGT: opCode = V9::FBG; break;
344 default:
345 assert(0 && "Unrecognized VM instruction!");
346 break;
347 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000348
349 return opCode;
350}
351
352
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000353// Create a unique TmpInstruction for a boolean value,
354// representing the CC register used by a branch on that value.
355// For now, hack this using a little static cache of TmpInstructions.
356// Eventually the entire BURG instruction selection should be put
357// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000358// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000359// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000360//
361static TmpInstruction*
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000362GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType,
363 MachineCodeForInstruction& mcfi)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000364{
Chris Lattner09ff1122002-07-24 21:21:32 +0000365 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000366 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000367 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000368
369 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
370
Misha Brukman81b06862003-05-21 18:48:06 +0000371 if (lastFunction != F) {
372 lastFunction = F;
373 boolToTmpCache.clear();
374 }
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000375
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000376 // Look for tmpI and create a new one otherwise. The new value is
377 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000378 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
379 if (tmpI == NULL)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000380 tmpI = new TmpInstruction(mcfi, ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000381
382 return tmpI;
383}
384
385
Chris Lattner20b1ea02001-09-14 03:47:57 +0000386static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000387ChooseBccInstruction(const InstructionNode* instrNode,
Vikram S. Adve786833a2003-07-06 20:13:59 +0000388 const Type*& setCCType)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000389{
390 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000391 assert(setCCNode->getOpLabel() == SetCCOp);
392 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +0000393 setCCType = setCCInstr->getOperand(0)->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000394
Vikram S. Adve786833a2003-07-06 20:13:59 +0000395 if (setCCType->isFloatingPoint())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000396 return ChooseBFpccInstruction(instrNode, setCCInstr);
397 else
398 return ChooseBpccInstruction(instrNode, setCCInstr);
399}
400
401
Misha Brukmaneecdb662003-06-02 20:55:14 +0000402// WARNING: since this function has only one caller, it always returns
403// the opcode that expects an immediate and a register. If this function
404// is ever used in cases where an opcode that takes two registers is required,
405// then modify this function and use convertOpcodeFromRegToImm() where required.
406//
407// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
408// new cases of opcodes.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000409static inline MachineOpCode
Misha Brukmaneecdb662003-06-02 20:55:14 +0000410ChooseMovFpcciInstruction(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000411{
Misha Brukmana98cd452003-05-20 20:32:24 +0000412 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000413
414 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000415 {
Misha Brukmaneecdb662003-06-02 20:55:14 +0000416 case Instruction::SetEQ: opCode = V9::MOVFEi; break;
417 case Instruction::SetNE: opCode = V9::MOVFNEi; break;
418 case Instruction::SetLE: opCode = V9::MOVFLEi; break;
419 case Instruction::SetGE: opCode = V9::MOVFGEi; break;
420 case Instruction::SetLT: opCode = V9::MOVFLi; break;
421 case Instruction::SetGT: opCode = V9::MOVFGi; break;
Misha Brukman81b06862003-05-21 18:48:06 +0000422 default:
423 assert(0 && "Unrecognized VM instruction!");
424 break;
425 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000426
427 return opCode;
428}
429
430
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000431// ChooseMovpcciForSetCC -- Choose a conditional-move instruction
432// based on the type of SetCC operation.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433//
Misha Brukmaneecdb662003-06-02 20:55:14 +0000434// WARNING: since this function has only one caller, it always returns
435// the opcode that expects an immediate and a register. If this function
436// is ever used in cases where an opcode that takes two registers is required,
437// then modify this function and use convertOpcodeFromRegToImm() where required.
438//
439// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
440// new cases of opcodes.
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000441//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000442static MachineOpCode
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000443ChooseMovpcciForSetCC(const InstructionNode* instrNode)
444{
445 MachineOpCode opCode = V9::INVALID_OPCODE;
446
447 const Type* opType = instrNode->leftChild()->getValue()->getType();
448 assert(opType->isIntegral() || isa<PointerType>(opType));
449 bool noSign = opType->isUnsigned() || isa<PointerType>(opType);
450
451 switch(instrNode->getInstruction()->getOpcode())
452 {
453 case Instruction::SetEQ: opCode = V9::MOVEi; break;
454 case Instruction::SetLE: opCode = noSign? V9::MOVLEUi : V9::MOVLEi; break;
455 case Instruction::SetGE: opCode = noSign? V9::MOVCCi : V9::MOVGEi; break;
456 case Instruction::SetLT: opCode = noSign? V9::MOVCSi : V9::MOVLi; break;
457 case Instruction::SetGT: opCode = noSign? V9::MOVGUi : V9::MOVGi; break;
458 case Instruction::SetNE: opCode = V9::MOVNEi; break;
459 default: assert(0 && "Unrecognized LLVM instr!"); break;
460 }
461
462 return opCode;
463}
464
465
466// ChooseMovpregiForSetCC -- Choose a conditional-move-on-register-value
467// instruction based on the type of SetCC operation. These instructions
468// compare a register with 0 and perform the move is the comparison is true.
469//
470// WARNING: like the previous function, this function it always returns
471// the opcode that expects an immediate and a register. See above.
472//
473static MachineOpCode
474ChooseMovpregiForSetCC(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000475{
Misha Brukmana98cd452003-05-20 20:32:24 +0000476 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000477
478 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000479 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000480 case Instruction::SetEQ: opCode = V9::MOVRZi; break;
481 case Instruction::SetLE: opCode = V9::MOVRLEZi; break;
482 case Instruction::SetGE: opCode = V9::MOVRGEZi; break;
483 case Instruction::SetLT: opCode = V9::MOVRLZi; break;
484 case Instruction::SetGT: opCode = V9::MOVRGZi; break;
485 case Instruction::SetNE: opCode = V9::MOVRNZi; break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000486 default: assert(0 && "Unrecognized VM instr!"); break;
Misha Brukman81b06862003-05-21 18:48:06 +0000487 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000488
489 return opCode;
490}
491
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000492
Chris Lattner20b1ea02001-09-14 03:47:57 +0000493static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000494ChooseConvertToFloatInstr(const TargetMachine& target,
495 OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000496{
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000497 assert((vopCode == ToFloatTy || vopCode == ToDoubleTy) &&
498 "Unrecognized convert-to-float opcode!");
Vikram S. Advee895a742003-08-06 18:48:40 +0000499 assert((opType->isIntegral() || opType->isFloatingPoint() ||
500 isa<PointerType>(opType))
501 && "Trying to convert a non-scalar type to FLOAT/DOUBLE?");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000502
Misha Brukmana98cd452003-05-20 20:32:24 +0000503 MachineOpCode opCode = V9::INVALID_OPCODE;
Vikram S. Advee895a742003-08-06 18:48:40 +0000504
505 unsigned opSize = target.getTargetData().getTypeSize(opType);
506
507 if (opType == Type::FloatTy)
508 opCode = (vopCode == ToFloatTy? V9::NOP : V9::FSTOD);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000509 else if (opType == Type::DoubleTy)
Vikram S. Advee895a742003-08-06 18:48:40 +0000510 opCode = (vopCode == ToFloatTy? V9::FDTOS : V9::NOP);
511 else if (opSize <= 4)
512 opCode = (vopCode == ToFloatTy? V9::FITOS : V9::FITOD);
513 else {
514 assert(opSize == 8 && "Unrecognized type size > 4 and < 8!");
515 opCode = (vopCode == ToFloatTy? V9::FXTOS : V9::FXTOD);
516 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000517
518 return opCode;
519}
520
521static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000522ChooseConvertFPToIntInstr(const TargetMachine& target,
523 const Type* destType, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000524{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000525 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
526 && "This function should only be called for FLOAT or DOUBLE");
Vikram S. Advee895a742003-08-06 18:48:40 +0000527 assert((destType->isIntegral() || isa<PointerType>(destType))
528 && "Trying to convert FLOAT/DOUBLE to a non-scalar type?");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000529
Vikram S. Advee895a742003-08-06 18:48:40 +0000530 MachineOpCode opCode = V9::INVALID_OPCODE;
531
532 unsigned destSize = target.getTargetData().getTypeSize(destType);
533
534 if (destType == Type::UIntTy)
535 assert(destType != Type::UIntTy && "Expand FP-to-uint beforehand.");
536 else if (destSize <= 4)
Misha Brukman81b06862003-05-21 18:48:06 +0000537 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
Vikram S. Advee895a742003-08-06 18:48:40 +0000538 else {
539 assert(destSize == 8 && "Unrecognized type size > 4 and < 8!");
540 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
541 }
Vikram S. Adve94c40812002-09-27 14:33:08 +0000542
Chris Lattner20b1ea02001-09-14 03:47:57 +0000543 return opCode;
544}
545
Vikram S. Advee895a742003-08-06 18:48:40 +0000546static MachineInstr*
547CreateConvertFPToIntInstr(const TargetMachine& target,
548 Value* srcVal,
549 Value* destVal,
550 const Type* destType)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000551{
Vikram S. Advee895a742003-08-06 18:48:40 +0000552 MachineOpCode opCode = ChooseConvertFPToIntInstr(target, destType,
553 srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000554 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000555 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000556}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000557
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000558// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000559// The FP value must be converted to the dest type in an FP register,
560// and the result is then copied from FP to int register via memory.
Vikram S. Advee895a742003-08-06 18:48:40 +0000561// SPARC does not have a float-to-uint conversion, only a float-to-int (fdtoi).
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000562// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
Vikram S. Advee895a742003-08-06 18:48:40 +0000563// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly.
564// Therefore, for converting an FP value to uint32_t, we first need to convert
565// to uint64_t and then to uint32_t.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000566//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000567static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000568CreateCodeToConvertFloatToInt(const TargetMachine& target,
569 Value* opVal,
570 Instruction* destI,
571 std::vector<MachineInstr*>& mvec,
572 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000573{
Vikram S. Advee895a742003-08-06 18:48:40 +0000574 Function* F = destI->getParent()->getParent();
575
Vikram S. Adve1e606692002-07-31 21:01:34 +0000576 // Create a temporary to represent the FP register into which the
577 // int value will placed after conversion. The type of this temporary
578 // depends on the type of FP register to use: single-prec for a 32-bit
579 // int or smaller; double-prec for a 64-bit int.
580 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000581 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve1e606692002-07-31 21:01:34 +0000582
Vikram S. Advee895a742003-08-06 18:48:40 +0000583 const Type* castDestType = destI->getType(); // type for the cast instr result
584 const Type* castDestRegType; // type for cast instruction result reg
585 TmpInstruction* destForCast; // dest for cast instruction
586 Instruction* fpToIntCopyDest = destI; // dest for fp-reg-to-int-reg copy instr
587
588 // For converting an FP value to uint32_t, we first need to convert to
589 // uint64_t and then to uint32_t, as explained above.
590 if (destI->getType() == Type::UIntTy) {
591 castDestType = Type::ULongTy; // use this instead of type of destI
592 castDestRegType = Type::DoubleTy; // uint64_t needs 64-bit FP register.
593 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
594 fpToIntCopyDest = new TmpInstruction(mcfi, castDestType, destForCast);
595 }
596 else {
597 castDestRegType = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
598 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
599 }
600
601 // Create the fp-to-int conversion instruction (src and dest regs are FP regs)
602 mvec.push_back(CreateConvertFPToIntInstr(target, opVal, destForCast,
603 castDestType));
Vikram S. Adve1e606692002-07-31 21:01:34 +0000604
605 // Create the fpreg-to-intreg copy code
Vikram S. Advee895a742003-08-06 18:48:40 +0000606 target.getInstrInfo().CreateCodeToCopyFloatToInt(target, F, destForCast,
607 fpToIntCopyDest, mvec, mcfi);
608
609 // Create the uint64_t to uint32_t conversion, if needed
610 if (destI->getType() == Type::UIntTy)
611 target.getInstrInfo().
612 CreateZeroExtensionInstructions(target, F, fpToIntCopyDest, destI,
613 /*numLowBits*/ 32, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000614}
615
616
Chris Lattner20b1ea02001-09-14 03:47:57 +0000617static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000618ChooseAddInstruction(const InstructionNode* instrNode)
619{
620 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
621}
622
623
Chris Lattner20b1ea02001-09-14 03:47:57 +0000624static inline MachineInstr*
625CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000626 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000627{
Misha Brukmana98cd452003-05-20 20:32:24 +0000628 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000629 .addReg(instrNode->leftChild()->getValue())
630 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000631}
632
633static inline MachineInstr*
634CreateAddConstInstruction(const InstructionNode* instrNode)
635{
636 MachineInstr* minstr = NULL;
637
638 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000639 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000640
641 // Cases worth optimizing are:
642 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
643 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
644 //
Chris Lattner9b625032002-05-06 16:15:30 +0000645 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000646 double dval = FPC->getValue();
647 if (dval == 0.0)
648 minstr = CreateMovFloatInstruction(instrNode,
649 instrNode->getInstruction()->getType());
650 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000651
652 return minstr;
653}
654
655
656static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000657ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000658{
Misha Brukmana98cd452003-05-20 20:32:24 +0000659 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000660
Misha Brukman81b06862003-05-21 18:48:06 +0000661 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukman91aee472003-05-27 22:37:00 +0000662 opCode = V9::SUBr;
Misha Brukman81b06862003-05-21 18:48:06 +0000663 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000664 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000665 {
666 case Type::FloatTyID: opCode = V9::FSUBS; break;
667 case Type::DoubleTyID: opCode = V9::FSUBD; break;
668 default: assert(0 && "Invalid type for SUB instruction"); break;
669 }
670 }
671
Chris Lattner20b1ea02001-09-14 03:47:57 +0000672 return opCode;
673}
674
675
676static inline MachineInstr*
677CreateSubConstInstruction(const InstructionNode* instrNode)
678{
679 MachineInstr* minstr = NULL;
680
681 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000682 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000683
684 // Cases worth optimizing are:
685 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
686 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
687 //
Chris Lattner9b625032002-05-06 16:15:30 +0000688 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
689 double dval = FPC->getValue();
690 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000691 minstr = CreateMovFloatInstruction(instrNode,
692 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000693 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000694
695 return minstr;
696}
697
698
699static inline MachineOpCode
700ChooseFcmpInstruction(const InstructionNode* instrNode)
701{
Misha Brukmana98cd452003-05-20 20:32:24 +0000702 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000703
704 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
705 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000706 case Type::FloatTyID: opCode = V9::FCMPS; break;
707 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000708 default: assert(0 && "Invalid type for FCMP instruction"); break;
709 }
710
711 return opCode;
712}
713
714
715// Assumes that leftArg and rightArg are both cast instructions.
716//
717static inline bool
718BothFloatToDouble(const InstructionNode* instrNode)
719{
720 InstrTreeNode* leftArg = instrNode->leftChild();
721 InstrTreeNode* rightArg = instrNode->rightChild();
722 InstrTreeNode* leftArgArg = leftArg->leftChild();
723 InstrTreeNode* rightArgArg = rightArg->leftChild();
724 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
725
726 // Check if both arguments are floats cast to double
727 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000728 leftArgArg->getValue()->getType() == Type::FloatTy &&
729 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000730}
731
732
733static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000734ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000735{
Misha Brukmana98cd452003-05-20 20:32:24 +0000736 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000737
Chris Lattner0c4e8862002-09-03 01:08:28 +0000738 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000739 opCode = V9::MULXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000740 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000741 switch(resultType->getPrimitiveID())
Misha Brukman7b647942003-05-30 20:11:56 +0000742 {
743 case Type::FloatTyID: opCode = V9::FMULS; break;
744 case Type::DoubleTyID: opCode = V9::FMULD; break;
745 default: assert(0 && "Invalid type for MUL instruction"); break;
746 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000747
748 return opCode;
749}
750
751
Vikram S. Adve510eec72001-11-04 21:59:14 +0000752
Chris Lattner20b1ea02001-09-14 03:47:57 +0000753static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000754CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000755 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000756{
Misha Brukman91aee472003-05-27 22:37:00 +0000757 return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000758 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000759}
760
761
Vikram S. Adve242a8082002-05-19 15:25:51 +0000762// Create instruction sequence for any shift operation.
763// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
764// requires a second instruction for explicit sign-extension.
765// Note that we only have to worry about a sign-bit appearing in the
766// most significant bit of the operand after shifting (e.g., bit 32 of
767// Int or bit 16 of Short), so we do not have to worry about results
768// that are as large as a normal integer register.
769//
770static inline void
771CreateShiftInstructions(const TargetMachine& target,
772 Function* F,
773 MachineOpCode shiftOpCode,
774 Value* argVal1,
775 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000776 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000777 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000778 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000779 MachineCodeForInstruction& mcfi)
780{
781 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
782 "Large shift sizes unexpected, but can be handled below: "
783 "You need to check whether or not it fits in immed field below");
784
785 // If this is a logical left shift of a type smaller than the standard
786 // integer reg. size, we have to extend the sign-bit into upper bits
787 // of dest, so we need to put the result of the SLL into a temporary.
788 //
789 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000790 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000791
Misha Brukmand36e30e2003-06-06 09:52:23 +0000792 if ((shiftOpCode == V9::SLLr5 || shiftOpCode == V9::SLLXr6) && opSize < 8) {
Misha Brukman7b647942003-05-30 20:11:56 +0000793 // put SLL result into a temporary
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000794 shiftDest = new TmpInstruction(mcfi, argVal1, optArgVal2, "sllTmp");
Misha Brukman7b647942003-05-30 20:11:56 +0000795 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000796
797 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000798 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
799 .addReg(shiftDest, MOTy::Def)
800 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
801 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000802 mvec.push_back(M);
803
Misha Brukman7b647942003-05-30 20:11:56 +0000804 if (shiftDest != destVal) {
805 // extend the sign-bit of the result into all upper bits of dest
806 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
807 target.getInstrInfo().
808 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
809 8*opSize, mvec, mcfi);
810 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000811}
812
813
Vikram S. Adve74825322002-03-18 03:15:35 +0000814// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000815// create a cheaper instruction.
816// This returns the approximate cost of the instructions generated,
817// which is used to pick the cheapest when both operands are constant.
Vikram S. Adve645fea32003-05-25 21:59:47 +0000818static unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000819CreateMulConstInstruction(const TargetMachine &target, Function* F,
820 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000821 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000822 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000823{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000824 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukman91aee472003-05-27 22:37:00 +0000825 unsigned cost = target.getInstrInfo().minLatency(V9::MULXr);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000826 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000827
828 Value* constOp = rval;
829 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000830 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000831
832 // Cases worth optimizing are:
833 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
834 // (2) Multiply by 2^x for integer types: replace with Shift
835 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000836 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000837
Misha Brukmana98cd452003-05-20 20:32:24 +0000838 if (resultType->isInteger() || isa<PointerType>(resultType)) {
839 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +0000840 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
841 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +0000842 if (isValidConst) {
843 unsigned pow;
844 bool needNeg = false;
845 if (C < 0) {
846 needNeg = true;
847 C = -C;
848 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000849
Misha Brukmana98cd452003-05-20 20:32:24 +0000850 if (C == 0 || C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000851 cost = target.getInstrInfo().minLatency(V9::ADDr);
Misha Brukmana98cd452003-05-20 20:32:24 +0000852 unsigned Zero = target.getRegInfo().getZeroRegNum();
853 MachineInstr* M;
854 if (C == 0)
Misha Brukman91aee472003-05-27 22:37:00 +0000855 M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000856 else
Misha Brukman91aee472003-05-27 22:37:00 +0000857 M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000858 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +0000859 } else if (isPowerOf2(C, pow)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000860 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Misha Brukmand36e30e2003-06-06 09:52:23 +0000861 MachineOpCode opCode = (opSize <= 32)? V9::SLLr5 : V9::SLLXr6;
Misha Brukmana98cd452003-05-20 20:32:24 +0000862 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
863 destVal, mvec, mcfi);
864 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000865
Misha Brukman7b647942003-05-30 20:11:56 +0000866 if (mvec.size() > 0 && needNeg) {
867 // insert <reg = SUB 0, reg> after the instr to flip the sign
Misha Brukmana98cd452003-05-20 20:32:24 +0000868 MachineInstr* M = CreateIntNegInstruction(target, destVal);
869 mvec.push_back(M);
870 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000871 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000872 } else {
873 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
874 double dval = FPC->getValue();
875 if (fabs(dval) == 1) {
876 MachineOpCode opCode = (dval < 0)
877 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
878 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
879 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
880 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000881 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000882 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000883
Misha Brukmana98cd452003-05-20 20:32:24 +0000884 if (firstNewInstr < mvec.size()) {
885 cost = 0;
886 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
887 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
888 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000889
890 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000891}
892
893
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000894// Does not create any instructions if we cannot exploit constant to
895// create a cheaper instruction.
896//
897static inline void
898CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000899 Function* F,
900 Value* lval, Value* rval,
901 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000902 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000903 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000904{
905 Value* constOp;
Misha Brukman7b647942003-05-30 20:11:56 +0000906 if (isa<Constant>(lval) && isa<Constant>(rval)) {
907 // both operands are constant: evaluate and "set" in dest
908 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
909 cast<Constant>(lval),
910 cast<Constant>(rval));
911 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
912 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000913 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000914 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000915 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000916 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000917
918 // else neither is constant
919 return;
920}
921
Vikram S. Adve74825322002-03-18 03:15:35 +0000922// Return NULL if we cannot exploit constant to create a cheaper instruction
923static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000924CreateMulInstruction(const TargetMachine &target, Function* F,
925 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000926 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000927 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000928 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
929{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000930 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000931 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000932 if (mvec.size() == L) {
933 // no instructions were added so create MUL reg, reg, reg.
934 // Use FSMULD if both operands are actually floats cast to doubles.
935 // Otherwise, use the default opcode for the appropriate type.
936 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
937 ? forceMulOp
938 : ChooseMulInstructionByType(destVal->getType()));
939 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
940 .addRegDef(destVal));
941 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000942}
943
944
Vikram S. Adve510eec72001-11-04 21:59:14 +0000945// Generate a divide instruction for Div or Rem.
946// For Rem, this assumes that the operand type will be signed if the result
947// type is signed. This is correct because they must have the same sign.
948//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000949static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000950ChooseDivInstruction(TargetMachine &target,
951 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000952{
Misha Brukmana98cd452003-05-20 20:32:24 +0000953 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000954
955 const Type* resultType = instrNode->getInstruction()->getType();
956
Chris Lattner0c4e8862002-09-03 01:08:28 +0000957 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000958 opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000959 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000960 switch(resultType->getPrimitiveID())
961 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000962 case Type::FloatTyID: opCode = V9::FDIVS; break;
963 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000964 default: assert(0 && "Invalid type for DIV instruction"); break;
965 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000966
967 return opCode;
968}
969
970
Chris Lattner54e898e2003-01-15 19:23:34 +0000971// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve645fea32003-05-25 21:59:47 +0000972static void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000973CreateDivConstInstruction(TargetMachine &target,
974 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000975 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000976{
Chris Lattner54e898e2003-01-15 19:23:34 +0000977 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000978 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000979 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000980 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000981
Vikram S. Adve645fea32003-05-25 21:59:47 +0000982 Instruction* destVal = instrNode->getInstruction();
Chris Lattner54e898e2003-01-15 19:23:34 +0000983 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000984
985 // Cases worth optimizing are:
986 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
987 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
988 //
989 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000990
Misha Brukman7b647942003-05-30 20:11:56 +0000991 if (resultType->isInteger()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000992 unsigned pow;
993 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +0000994 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
995 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +0000996 if (isValidConst) {
997 bool needNeg = false;
998 if (C < 0) {
999 needNeg = true;
1000 C = -C;
1001 }
Vikram S. Advee6124d32003-07-29 19:59:23 +00001002
Misha Brukmana98cd452003-05-20 20:32:24 +00001003 if (C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +00001004 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001005 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001006 } else if (isPowerOf2(C, pow)) {
Vikram S. Adve645fea32003-05-25 21:59:47 +00001007 unsigned opCode;
1008 Value* shiftOperand;
Vikram S. Advee895a742003-08-06 18:48:40 +00001009 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve645fea32003-05-25 21:59:47 +00001010
1011 if (resultType->isSigned()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001012 // For N / 2^k, if the operand N is negative,
1013 // we need to add (2^k - 1) before right-shifting by k, i.e.,
Vikram S. Adve645fea32003-05-25 21:59:47 +00001014 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001015 // (N / 2^k) = N >> k, if N >= 0;
1016 // (N + 2^k - 1) >> k, if N < 0
1017 //
1018 // If N is <= 32 bits, use:
1019 // sra N, 31, t1 // t1 = ~0, if N < 0, 0 else
1020 // srl t1, 32-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1021 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1022 // sra t3, k, result // result = N / 2^k
1023 //
1024 // If N is 64 bits, use:
1025 // srax N, k-1, t1 // t1 = sign bit in high k positions
1026 // srlx t1, 64-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1027 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1028 // sra t3, k, result // result = N / 2^k
1029 //
1030 TmpInstruction *sraTmp, *srlTmp, *addTmp;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001031 MachineCodeForInstruction& mcfi
1032 = MachineCodeForInstruction::get(destVal);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001033 sraTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getSign");
1034 srlTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getPlus2km1");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001035 addTmp = new TmpInstruction(mcfi, resultType, LHS, srlTmp,"incIfNeg");
Vikram S. Adve645fea32003-05-25 21:59:47 +00001036
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001037 // Create the SRA or SRAX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001038 mvec.push_back(BuildMI((opSize > 4)? V9::SRAXi6 : V9::SRAi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001039 .addReg(LHS)
1040 .addSImm((resultType==Type::LongTy)? pow-1 : 31)
1041 .addRegDef(sraTmp));
1042
Vikram S. Adve645fea32003-05-25 21:59:47 +00001043 // Create the SRL or SRLX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001044 mvec.push_back(BuildMI((opSize > 4)? V9::SRLXi6 : V9::SRLi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001045 .addReg(sraTmp)
1046 .addSImm((resultType==Type::LongTy)? 64-pow : 32-pow)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001047 .addRegDef(srlTmp));
1048
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001049 // Create the ADD instruction to add 2^pow-1 for negative values
Misha Brukman91aee472003-05-27 22:37:00 +00001050 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001051 .addRegDef(addTmp));
1052
1053 // Get the shift operand and "right-shift" opcode to do the divide
1054 shiftOperand = addTmp;
Vikram S. Advee895a742003-08-06 18:48:40 +00001055 opCode = (opSize > 4)? V9::SRAXi6 : V9::SRAi5;
Misha Brukman7b647942003-05-30 20:11:56 +00001056 } else {
Vikram S. Adve645fea32003-05-25 21:59:47 +00001057 // Get the shift operand and "right-shift" opcode to do the divide
1058 shiftOperand = LHS;
Vikram S. Advee895a742003-08-06 18:48:40 +00001059 opCode = (opSize > 4)? V9::SRLXi6 : V9::SRLi5;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001060 }
1061
1062 // Now do the actual shift!
1063 mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow)
1064 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001065 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001066
Misha Brukmana98cd452003-05-20 20:32:24 +00001067 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
1068 // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve645fea32003-05-25 21:59:47 +00001069 mvec.push_back(CreateIntNegInstruction(target, destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001070 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001071 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001072 } else {
1073 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
1074 double dval = FPC->getValue();
1075 if (fabs(dval) == 1) {
1076 unsigned opCode =
1077 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
1078 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001079
Vikram S. Adve645fea32003-05-25 21:59:47 +00001080 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001081 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001082 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001083 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001084}
1085
1086
Vikram S. Adve74825322002-03-18 03:15:35 +00001087static void
1088CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1089 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001090 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001091 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001092 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001093{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001094 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001095 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001096 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001097 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001098
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001099 // Enforce the alignment constraints on the stack pointer at
1100 // compile time if the total size is a known constant.
Misha Brukman7b647942003-05-30 20:11:56 +00001101 if (isa<Constant>(numElementsVal)) {
1102 bool isValid;
Vikram S. Advee6124d32003-07-29 19:59:23 +00001103 int64_t numElem = (int64_t) target.getInstrInfo().
1104 ConvertConstantToIntType(target, numElementsVal,
1105 numElementsVal->getType(), isValid);
Misha Brukman7b647942003-05-30 20:11:56 +00001106 assert(isValid && "Unexpectedly large array dimension in alloca!");
1107 int64_t total = numElem * tsize;
1108 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1109 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1110 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1111 } else {
1112 // The size is not a constant. Generate code to compute it and
1113 // code to pad the size for stack alignment.
1114 // Create a Value to hold the (constant) element size
1115 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001116
Misha Brukman7b647942003-05-30 20:11:56 +00001117 // Create temporary values to hold the result of MUL, SLL, SRL
Vikram S. Adve80544442003-06-23 02:13:57 +00001118 // To pad `size' to next smallest multiple of 16:
1119 // size = (size + 15) & (-16 = 0xfffffffffffffff0)
1120 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001121 TmpInstruction* tmpProd = new TmpInstruction(mcfi,numElementsVal, tsizeVal);
Vikram S. Adve80544442003-06-23 02:13:57 +00001122 TmpInstruction* tmpAdd15= new TmpInstruction(mcfi,numElementsVal, tmpProd);
1123 TmpInstruction* tmpAndf0= new TmpInstruction(mcfi,numElementsVal, tmpAdd15);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001124
Misha Brukman7b647942003-05-30 20:11:56 +00001125 // Instruction 1: mul numElements, typeSize -> tmpProd
1126 // This will optimize the MUL as far as possible.
Vikram S. Adve80544442003-06-23 02:13:57 +00001127 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd, getMvec,
Misha Brukman7b647942003-05-30 20:11:56 +00001128 mcfi, INVALID_MACHINE_OPCODE);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001129
Vikram S. Adve80544442003-06-23 02:13:57 +00001130 // Instruction 2: andn tmpProd, 0x0f -> tmpAndn
1131 getMvec.push_back(BuildMI(V9::ADDi, 3).addReg(tmpProd).addSImm(15)
1132 .addReg(tmpAdd15, MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001133
Vikram S. Adve80544442003-06-23 02:13:57 +00001134 // Instruction 3: add tmpAndn, 0x10 -> tmpAdd16
1135 getMvec.push_back(BuildMI(V9::ANDi, 3).addReg(tmpAdd15).addSImm(-16)
1136 .addReg(tmpAndf0, MOTy::Def));
1137
1138 totalSizeVal = tmpAndf0;
Misha Brukman7b647942003-05-30 20:11:56 +00001139 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001140
1141 // Get the constant offset from SP for dynamically allocated storage
1142 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001143 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001144 bool growUp;
1145 ConstantSInt* dynamicAreaOffset =
1146 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001147 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001148 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1149
Chris Lattner54e898e2003-01-15 19:23:34 +00001150 unsigned SPReg = target.getRegInfo().getStackPointer();
1151
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001152 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukman91aee472003-05-27 22:37:00 +00001153 getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001154 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001155
Vikram S. Adve74825322002-03-18 03:15:35 +00001156 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukman91aee472003-05-27 22:37:00 +00001157 getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset)
Misha Brukmana98cd452003-05-20 20:32:24 +00001158 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001159}
1160
1161
1162static void
1163CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1164 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001165 unsigned tsize,
1166 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001167 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001168{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001169 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001170 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001171 "Result value is not part of a function?");
1172 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001173 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001174
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001175 // Put the variable in the dynamically sized area of the frame if either:
1176 // (a) The offset is too large to use as an immediate in load/stores
1177 // (check LDX because all load/stores have the same-size immed. field).
1178 // (b) The object is "large", so it could cause many other locals,
1179 // spills, and temporaries to have large offsets.
1180 // NOTE: We use LARGE = 8 * argSlotSize = 64 bytes.
1181 // You've gotta love having only 13 bits for constant offset values :-|.
1182 //
1183 unsigned paddedSize;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001184 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001185 paddedSize,
1186 tsize * numElements);
1187
1188 if (((int)paddedSize) > 8 * target.getFrameInfo().getSizeOfEachArgOnStack() ||
1189 ! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001190 CreateCodeForVariableSizeAlloca(target, result, tsize,
1191 ConstantSInt::get(Type::IntTy,numElements),
1192 getMvec);
1193 return;
1194 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001195
1196 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001197 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001198
1199 // Create a temporary Value to hold the constant offset.
1200 // This is needed because it may not fit in the immediate field.
1201 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1202
1203 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001204 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukman91aee472003-05-27 22:37:00 +00001205 getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001206 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001207}
1208
1209
Chris Lattner20b1ea02001-09-14 03:47:57 +00001210//------------------------------------------------------------------------
1211// Function SetOperandsForMemInstr
1212//
1213// Choose addressing mode for the given load or store instruction.
1214// Use [reg+reg] if it is an indexed reference, and the index offset is
1215// not a constant or if it cannot fit in the offset field.
1216// Use [reg+offset] in all other cases.
1217//
1218// This assumes that all array refs are "lowered" to one of these forms:
1219// %x = load (subarray*) ptr, constant ; single constant offset
1220// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1221// Generally, this should happen via strength reduction + LICM.
1222// Also, strength reduction should take care of using the same register for
1223// the loop index variable and an array index, when that is profitable.
1224//------------------------------------------------------------------------
1225
1226static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001227SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001228 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001229 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001230 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001231{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001232 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001233 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001234 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001235 bool allConstantIndices;
1236 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001237
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001238 // Now create the appropriate operands for the machine instruction.
1239 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001240 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001241 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001242 MachineOperand::MachineOperandType offsetOpType =
1243 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001244
Vikram S. Adve74825322002-03-18 03:15:35 +00001245 // Check if there is an index vector and if so, compute the
1246 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001247 //
Misha Brukman7b647942003-05-30 20:11:56 +00001248 if (!idxVec.empty()) {
1249 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001250
Misha Brukman7b647942003-05-30 20:11:56 +00001251 // If all indices are constant, compute the combined offset directly.
1252 if (allConstantIndices) {
1253 // Compute the offset value using the index vector. Create a
1254 // virtual reg. for it since it may not fit in the immed field.
1255 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
1256 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
1257 } else {
1258 // There is at least one non-constant offset. Therefore, this must
1259 // be an array ref, and must have been lowered to a single non-zero
1260 // offset. (An extra leading zero offset, if any, can be ignored.)
1261 // Generate code sequence to compute address from index.
1262 //
1263 bool firstIdxIsZero = IsZero(idxVec[0]);
1264 assert(idxVec.size() == 1U + firstIdxIsZero
1265 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001266
Misha Brukman7b647942003-05-30 20:11:56 +00001267 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001268
Misha Brukman7b647942003-05-30 20:11:56 +00001269 std::vector<MachineInstr*> mulVec;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001270 Instruction* addr =
1271 new TmpInstruction(MachineCodeForInstruction::get(memInst),
1272 Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001273
Misha Brukman7b647942003-05-30 20:11:56 +00001274 // Get the array type indexed by idxVal, and compute its element size.
1275 // The call to getTypeSize() will fail if size is not constant.
1276 const Type* vecType = (firstIdxIsZero
1277 ? GetElementPtrInst::getIndexedType(ptrType,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001278 std::vector<Value*>(1U, idxVec[0]),
1279 /*AllowCompositeLeaf*/ true)
1280 : ptrType);
Misha Brukman7b647942003-05-30 20:11:56 +00001281 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1282 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
1283 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001284
Misha Brukman7b647942003-05-30 20:11:56 +00001285 // CreateMulInstruction() folds constants intelligently enough.
1286 CreateMulInstruction(target, memInst->getParent()->getParent(),
1287 idxVal, /* lval, not likely to be const*/
1288 eltSizeVal, /* rval, likely to be constant */
1289 addr, /* result */
1290 mulVec, MachineCodeForInstruction::get(memInst),
1291 INVALID_MACHINE_OPCODE);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001292
Misha Brukman7b647942003-05-30 20:11:56 +00001293 assert(mulVec.size() > 0 && "No multiply code created?");
1294 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
1295
1296 valueForRegOffset = addr;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001297 }
Misha Brukman7b647942003-05-30 20:11:56 +00001298 } else {
1299 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1300 smallConstOffset = 0;
1301 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001302
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001303 // For STORE:
1304 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1305 // For LOAD or GET_ELEMENT_PTR,
1306 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1307 //
1308 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001309 MachineInstr *MI;
1310 if (memInst->getOpcode() == Instruction::Store) {
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(vmInstrNode->leftChild()->getValue())
1313 .addReg(ptrVal).addReg(valueForRegOffset);
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(vmInstrNode->leftChild()->getValue())
1317 .addReg(ptrVal).addSImm(smallConstOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001318 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001319 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00001320 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001321 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1322 .addRegDef(memInst);
Misha Brukman7b647942003-05-30 20:11:56 +00001323 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001324 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001325 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1326 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001327 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001328 }
1329 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001330}
1331
1332
Chris Lattner20b1ea02001-09-14 03:47:57 +00001333//
1334// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001335// in place of the use(s) of that instruction in node `parent'.
1336// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001337// Also make sure to skip over a parent who:
1338// (1) is a list node in the Burg tree, or
1339// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001340//
1341static void
1342ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001343 InstrTreeNode* parent,
1344 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001345{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001346 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1347
Chris Lattner20b1ea02001-09-14 03:47:57 +00001348 Instruction* unusedOp = treeNode->getInstruction();
1349 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001350
1351 // The parent itself may be a list node, so find the real parent instruction
1352 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1353 {
1354 parent = parent->parent();
1355 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1356 }
1357 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1358
1359 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001360 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001361
1362 // The parent's mvec would be empty if it was itself forwarded.
1363 // Recursively call ForwardOperand in that case...
1364 //
Misha Brukman7b647942003-05-30 20:11:56 +00001365 if (mvec.size() == 0) {
1366 assert(parent->parent() != NULL &&
1367 "Parent could not have been forwarded, yet has no instructions?");
1368 ForwardOperand(treeNode, parent->parent(), operandNum);
1369 } else {
1370 for (unsigned i=0, N=mvec.size(); i < N; i++) {
1371 MachineInstr* minstr = mvec[i];
1372 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) {
1373 const MachineOperand& mop = minstr->getOperand(i);
1374 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
1375 mop.getVRegValue() == unusedOp)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001376 {
Misha Brukman7b647942003-05-30 20:11:56 +00001377 minstr->SetMachineOperandVal(i, MachineOperand::MO_VirtualRegister,
1378 fwdOp);
1379 }
1380 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001381
Misha Brukman7b647942003-05-30 20:11:56 +00001382 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
Chris Lattner907b7dc2003-08-05 16:59:24 +00001383 if (minstr->getImplicitRef(i) == unusedOp)
1384 minstr->setImplicitRef(i, fwdOp);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001385 }
Misha Brukman7b647942003-05-30 20:11:56 +00001386 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001387}
1388
1389
Vikram S. Adve242a8082002-05-19 15:25:51 +00001390inline bool
1391AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001392{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001393 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1394 UI != UE; ++UI)
1395 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1396 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1397 return false;
1398 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399}
1400
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001401// Generate code for any intrinsic that needs a special code sequence
1402// instead of a regular call. If not that kind of intrinsic, do nothing.
1403// Returns true if code was generated, otherwise false.
1404//
1405bool CodeGenIntrinsic(LLVMIntrinsic::ID iid, CallInst &callInstr,
1406 TargetMachine &target,
1407 std::vector<MachineInstr*>& mvec)
1408{
1409 switch (iid) {
1410 case LLVMIntrinsic::va_start: {
1411 // Get the address of the first vararg value on stack and copy it to
1412 // the argument of va_start(va_list* ap).
1413 bool ignore;
1414 Function* func = cast<Function>(callInstr.getParent()->getParent());
1415 int numFixedArgs = func->getFunctionType()->getNumParams();
1416 int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum();
1417 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
1418 int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo().
1419 getFirstIncomingArgOffset(MachineFunction::get(func), ignore);
Misha Brukman91aee472003-05-27 22:37:00 +00001420 mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff).
Vikram S. Adve4ecff5d2003-08-11 18:42:47 +00001421 addRegDef(callInstr.getOperand(1)));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001422 return true;
1423 }
1424
1425 case LLVMIntrinsic::va_end:
1426 return true; // no-op on Sparc
1427
1428 case LLVMIntrinsic::va_copy:
1429 // Simple copy of current va_list (arg2) to new va_list (arg1)
Misha Brukman91aee472003-05-27 22:37:00 +00001430 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001431 addMReg(target.getRegInfo().getZeroRegNum()).
1432 addReg(callInstr.getOperand(2)).
1433 addReg(callInstr.getOperand(1)));
1434 return true;
1435
Chris Lattner72af6b82003-08-18 16:06:09 +00001436 case LLVMIntrinsic::sigsetjmp:
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001437 case LLVMIntrinsic::setjmp: {
1438 // act as if we return 0
1439 unsigned g0 = target.getRegInfo().getZeroRegNum();
1440 mvec.push_back(BuildMI(V9::ORr,3).addMReg(g0).addMReg(g0)
1441 .addReg(&callInstr, MOTy::Def));
1442 return true;
1443 }
1444
Chris Lattner72af6b82003-08-18 16:06:09 +00001445 case LLVMIntrinsic::siglongjmp:
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001446 case LLVMIntrinsic::longjmp: {
1447 // call abort()
1448 Module* M = callInstr.getParent()->getParent()->getParent();
Vikram S. Adve5be74342003-09-16 05:56:22 +00001449 const FunctionType *voidvoidFuncTy =
1450 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
1451 Function *F = M->getOrInsertFunction("abort", voidvoidFuncTy);
1452 assert(F && "Unable to get or create `abort' function declaration");
1453
1454 // Create hidden virtual register for return address with type void*
1455 TmpInstruction* retAddrReg =
1456 new TmpInstruction(MachineCodeForInstruction::get(&callInstr),
1457 PointerType::get(Type::VoidTy), &callInstr);
1458
1459 // Use a descriptor to pass information about call arguments
1460 // to the register allocator. This descriptor will be "owned"
1461 // and freed automatically when the MachineCodeForInstruction
1462 // object for the callInstr goes away.
1463 CallArgsDescriptor* argDesc =
1464 new CallArgsDescriptor(&callInstr, retAddrReg, false, false);
1465
1466 MachineInstr* callMI = BuildMI(V9::CALL, 1).addPCDisp(F);
1467 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
1468
1469 mvec.push_back(callMI);
1470 mvec.push_back(BuildMI(V9::NOP, 0));
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001471 return true;
1472 }
1473
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001474 default:
1475 return false;
1476 }
1477}
1478
Vikram S. Advefb361122001-10-22 13:36:31 +00001479//******************* Externally Visible Functions *************************/
1480
Vikram S. Advefb361122001-10-22 13:36:31 +00001481//------------------------------------------------------------------------
1482// External Function: ThisIsAChainRule
1483//
1484// Purpose:
1485// Check if a given BURG rule is a chain rule.
1486//------------------------------------------------------------------------
1487
1488extern bool
1489ThisIsAChainRule(int eruleno)
1490{
1491 switch(eruleno)
1492 {
1493 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001494 case 123:
1495 case 124:
1496 case 125:
1497 case 126:
1498 case 127:
1499 case 128:
1500 case 129:
1501 case 130:
1502 case 131:
1503 case 132:
1504 case 133:
1505 case 155:
1506 case 221:
1507 case 222:
1508 case 241:
1509 case 242:
1510 case 243:
1511 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001512 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001513 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001514 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001515
Vikram S. Advefb361122001-10-22 13:36:31 +00001516 default:
1517 return false; break;
1518 }
1519}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001520
1521
1522//------------------------------------------------------------------------
1523// External Function: GetInstructionsByRule
1524//
1525// Purpose:
1526// Choose machine instructions for the SPARC according to the
1527// patterns chosen by the BURG-generated parser.
1528//------------------------------------------------------------------------
1529
Vikram S. Adve74825322002-03-18 03:15:35 +00001530void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001531GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001532 int ruleForNode,
1533 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001534 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001535 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001536{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001537 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001538 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001539 int nextRule;
1540 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001541 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001542 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001543 unsigned L;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001544 bool foldCase = false;
Vikram S. Adve74825322002-03-18 03:15:35 +00001545
1546 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001547
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001548 // If the code for this instruction was folded into the parent (user),
1549 // then do nothing!
1550 if (subtreeRoot->isFoldedIntoParent())
1551 return;
1552
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001553 //
1554 // Let's check for chain rules outside the switch so that we don't have
1555 // to duplicate the list of chain rule production numbers here again
1556 //
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001557 if (ThisIsAChainRule(ruleForNode))
1558 {
1559 // Chain rules have a single nonterminal on the RHS.
1560 // Get the rule that matches the RHS non-terminal and use that instead.
1561 //
1562 assert(nts[0] && ! nts[1]
1563 && "A chain rule should have only one RHS non-terminal!");
1564 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1565 nts = burm_nts[nextRule];
1566 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
1567 }
1568 else
1569 {
1570 switch(ruleForNode) {
1571 case 1: // stmt: Ret
1572 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001573 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001574 // for moving return value to appropriate register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001575 // Copy the return value to the required return register.
1576 // Mark the return Value as an implicit ref of the RET instr..
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001577 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001578 // Finally put a NOP in the delay slot.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001579 ReturnInst *returnInstr=cast<ReturnInst>(subtreeRoot->getInstruction());
1580 Value* retVal = returnInstr->getReturnValue();
1581 MachineCodeForInstruction& mcfi =
1582 MachineCodeForInstruction::get(returnInstr);
1583
1584 // Create a hidden virtual reg to represent the return address register
1585 // used by the machine instruction but not represented in LLVM.
1586 //
1587 Instruction* returnAddrTmp = new TmpInstruction(mcfi, returnInstr);
1588
1589 MachineInstr* retMI =
1590 BuildMI(V9::JMPLRETi, 3).addReg(returnAddrTmp).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001591 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001592
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001593 // If there is a value to return, we need to:
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001594 // (a) Sign-extend the value if it is smaller than 8 bytes (reg size)
1595 // (b) Insert a copy to copy the return value to the appropriate reg.
1596 // -- For FP values, create a FMOVS or FMOVD instruction
1597 // -- For non-FP values, create an add-with-0 instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001598 //
1599 if (retVal != NULL) {
1600 const UltraSparcRegInfo& regInfo =
1601 (UltraSparcRegInfo&) target.getRegInfo();
1602 const Type* retType = retVal->getType();
1603 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
1604 unsigned retRegNum = (retType->isFloatingPoint()
1605 ? (unsigned) SparcFloatRegClass::f0
1606 : (unsigned) SparcIntRegClass::i0);
1607 retRegNum = regInfo.getUnifiedRegNum(regClassID, retRegNum);
1608
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001609 // () Insert sign-extension instructions for small signed values.
1610 //
1611 Value* retValToUse = retVal;
1612 if (retType->isIntegral() && retType->isSigned()) {
1613 unsigned retSize = target.getTargetData().getTypeSize(retType);
1614 if (retSize <= 4) {
1615 // create a temporary virtual reg. to hold the sign-extension
1616 retValToUse = new TmpInstruction(mcfi, retVal);
1617
1618 // sign-extend retVal and put the result in the temporary reg.
1619 target.getInstrInfo().CreateSignExtensionInstructions
1620 (target, returnInstr->getParent()->getParent(),
1621 retVal, retValToUse, 8*retSize, mvec, mcfi);
1622 }
1623 }
1624
1625 // (b) Now, insert a copy to to the appropriate register:
1626 // -- For FP values, create a FMOVS or FMOVD instruction
1627 // -- For non-FP values, create an add-with-0 instruction
1628 //
1629 // First, create a virtual register to represent the register and
1630 // mark this vreg as being an implicit operand of the ret MI.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001631 TmpInstruction* retVReg =
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001632 new TmpInstruction(mcfi, retValToUse, NULL, "argReg");
1633
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001634 retMI->addImplicitRef(retVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001635
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001636 if (retType->isFloatingPoint())
1637 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001638 .addReg(retValToUse).addReg(retVReg, MOTy::Def));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001639 else
1640 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001641 .addReg(retValToUse).addSImm((int64_t) 0)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001642 .addReg(retVReg, MOTy::Def));
1643
1644 // Mark the operand with the register it should be assigned
1645 M->SetRegForOperand(M->getNumOperands()-1, retRegNum);
1646 retMI->SetRegForImplicitRef(retMI->getNumImplicitRefs()-1, retRegNum);
1647
1648 mvec.push_back(M);
1649 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001650
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001651 // Now insert the RET instruction and a NOP for the delay slot
1652 mvec.push_back(retMI);
Misha Brukmana98cd452003-05-20 20:32:24 +00001653 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001654
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001655 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001656 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001657
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001658 case 3: // stmt: Store(reg,reg)
1659 case 4: // stmt: Store(reg,ptrreg)
1660 SetOperandsForMemInstr(ChooseStoreInstruction(
Chris Lattner54e898e2003-01-15 19:23:34 +00001661 subtreeRoot->leftChild()->getValue()->getType()),
1662 mvec, subtreeRoot, target);
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001663 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001664
1665 case 5: // stmt: BrUncond
1666 {
1667 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1668 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
1669
1670 // delay slot
1671 mvec.push_back(BuildMI(V9::NOP, 0));
1672 break;
1673 }
1674
1675 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001676 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 // If the constant is ZERO, we can use the branch-on-integer-register
1678 // instructions and avoid the SUBcc instruction entirely.
1679 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001680 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001681 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1682 assert(constNode &&
1683 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001684 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001685 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001686
Chris Lattner0c4e8862002-09-03 01:08:28 +00001687 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001688 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00001689 && target.getInstrInfo().ConvertConstantToIntType(target,
1690 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001691 && isValidConst)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001692 {
1693 // That constant is a zero after all...
1694 // Use the left child of setCC as the first argument!
1695 // Mark the setCC node so that no code is generated for it.
1696 InstructionNode* setCCNode = (InstructionNode*)
1697 subtreeRoot->leftChild();
1698 assert(setCCNode->getOpLabel() == SetCCOp);
1699 setCCNode->markFoldedIntoParent();
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001700
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001701 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001702
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001703 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1704 .addReg(setCCNode->leftChild()->getValue())
1705 .addPCDisp(brInst->getSuccessor(0));
1706 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001707
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001708 // delay slot
1709 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001710
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001711 // false branch
1712 mvec.push_back(BuildMI(V9::BA, 1)
1713 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001714
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001715 // delay slot
1716 mvec.push_back(BuildMI(V9::NOP, 0));
1717 break;
1718 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001719 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001720 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001721
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001722 case 6: // stmt: BrCond(setCC)
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001723 { // bool => boolean was computed with SetCC.
1724 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001725 // If it is an integer CC, we also need to find the unique
1726 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001727 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001728 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +00001729 const Type* setCCType;
1730 unsigned Opcode = ChooseBccInstruction(subtreeRoot, setCCType);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001731 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1732 brInst->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00001733 setCCType,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001734 MachineCodeForInstruction::get(brInst));
Chris Lattner54e898e2003-01-15 19:23:34 +00001735 M = BuildMI(Opcode, 2).addCCReg(ccValue)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001736 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001737 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001738
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001739 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001740 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001741
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001742 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001743 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001744
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001745 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001746 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001747 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001748 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001749
1750 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001751 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001752 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001753 Constant* constVal =
1754 cast<Constant>(subtreeRoot->leftChild()->getValue());
1755 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001756
Misha Brukmana98cd452003-05-20 20:32:24 +00001757 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001758 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001759 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001760
1761 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001762 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001763 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001764 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001765
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001766 case 8: // stmt: BrCond(boolreg)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001767 { // boolreg => boolean is recorded in an integer register.
1768 // Use branch-on-integer-register instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001769 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001770 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001771 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001772 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001773 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001774
1775 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001776 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001777
1778 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001779 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001780
1781 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001782 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001783 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001784 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001785
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001786 case 9: // stmt: Switch(reg)
1787 assert(0 && "*** SWITCH instruction is not implemented yet.");
1788 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001789
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001790 case 10: // reg: VRegList(reg, reg)
1791 assert(0 && "VRegList should never be the topmost non-chain rule");
1792 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001793
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001794 case 21: // bool: Not(bool,reg): Compute with a conditional-move-on-reg
1795 { // First find the unary operand. It may be left or right, usually right.
1796 Instruction* notI = subtreeRoot->getInstruction();
1797 Value* notArg = BinaryOperator::getNotArgument(
1798 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1799 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1800
1801 // Unconditionally set register to 0
1802 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(notI));
1803
1804 // Now conditionally move 1 into the register.
1805 // Mark the register as a use (as well as a def) because the old
1806 // value will be retained if the condition is false.
1807 mvec.push_back(BuildMI(V9::MOVRZi, 3).addReg(notArg).addZImm(1)
1808 .addReg(notI, MOTy::UseAndDef));
1809
1810 break;
1811 }
1812
1813 case 421: // reg: BNot(reg,reg): Compute as reg = reg XOR-NOT 0
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001814 { // First find the unary operand. It may be left or right, usually right.
1815 Value* notArg = BinaryOperator::getNotArgument(
1816 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001817 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001818 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001819 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001820 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001821 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001822
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001823 case 322: // reg: Not(tobool, reg):
1824 // Fold CAST-TO-BOOL with NOT by inverting the sense of cast-to-bool
1825 foldCase = true;
1826 // Just fall through!
1827
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001828 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001829 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001830 Instruction* castI = subtreeRoot->getInstruction();
1831 Value* opVal = subtreeRoot->leftChild()->getValue();
1832 assert(opVal->getType()->isIntegral() ||
1833 isa<PointerType>(opVal->getType()));
1834
1835 // Unconditionally set register to 0
1836 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(castI));
1837
1838 // Now conditionally move 1 into the register.
1839 // Mark the register as a use (as well as a def) because the old
1840 // value will be retained if the condition is false.
1841 MachineOpCode opCode = foldCase? V9::MOVRZi : V9::MOVRNZi;
1842 mvec.push_back(BuildMI(opCode, 3).addReg(opVal).addZImm(1)
1843 .addReg(castI, MOTy::UseAndDef));
1844
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001845 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001846 }
1847
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001848 case 23: // reg: ToUByteTy(reg)
1849 case 24: // reg: ToSByteTy(reg)
1850 case 25: // reg: ToUShortTy(reg)
1851 case 26: // reg: ToShortTy(reg)
1852 case 27: // reg: ToUIntTy(reg)
1853 case 28: // reg: ToIntTy(reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001854 case 29: // reg: ToULongTy(reg)
1855 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001856 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001857 //======================================================================
1858 // Rules for integer conversions:
1859 //
1860 //--------
1861 // From ISO 1998 C++ Standard, Sec. 4.7:
1862 //
1863 // 2. If the destination type is unsigned, the resulting value is
1864 // the least unsigned integer congruent to the source integer
1865 // (modulo 2n where n is the number of bits used to represent the
1866 // unsigned type). [Note: In a two s complement representation,
1867 // this conversion is conceptual and there is no change in the
1868 // bit pattern (if there is no truncation). ]
1869 //
1870 // 3. If the destination type is signed, the value is unchanged if
1871 // it can be represented in the destination type (and bitfield width);
1872 // otherwise, the value is implementation-defined.
1873 //--------
1874 //
1875 // Since we assume 2s complement representations, this implies:
1876 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001877 // -- If operand is smaller than destination, zero-extend or sign-extend
1878 // according to the signedness of the *operand*: source decides:
1879 // (1) If operand is signed, sign-extend it.
1880 // If dest is unsigned, zero-ext the result!
1881 // (2) If operand is unsigned, our current invariant is that
1882 // it's high bits are correct, so zero-extension is not needed.
Vikram S. Adve94c40812002-09-27 14:33:08 +00001883 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001884 // -- If operand is same size as or larger than destination,
1885 // zero-extend or sign-extend according to the signedness of
1886 // the *destination*: destination decides:
1887 // (1) If destination is signed, sign-extend (truncating if needed)
1888 // This choice is implementation defined. We sign-extend the
1889 // operand, which matches both Sun's cc and gcc3.2.
1890 // (2) If destination is unsigned, zero-extend (truncating if needed)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001891 //======================================================================
1892
Vikram S. Adve242a8082002-05-19 15:25:51 +00001893 Instruction* destI = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001894 Function* currentFunc = destI->getParent()->getParent();
1895 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(destI);
1896
Vikram S. Adve242a8082002-05-19 15:25:51 +00001897 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001898 const Type* opType = opVal->getType();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001899 const Type* destType = destI->getType();
1900 unsigned opSize = target.getTargetData().getTypeSize(opType);
1901 unsigned destSize = target.getTargetData().getTypeSize(destType);
1902
1903 bool isIntegral = opType->isIntegral() || isa<PointerType>(opType);
1904
1905 if (opType == Type::BoolTy ||
1906 opType == destType ||
1907 isIntegral && opSize == destSize && opSize == 8) {
1908 // nothing to do in all these cases
1909 forwardOperandNum = 0; // forward first operand to user
1910
Misha Brukman7b647942003-05-30 20:11:56 +00001911 } else if (opType->isFloatingPoint()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001912
1913 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Advee895a742003-08-06 18:48:40 +00001914 if (destI->getType()->isUnsigned() && destI->getType() !=Type::UIntTy)
Misha Brukman7b647942003-05-30 20:11:56 +00001915 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve1e606692002-07-31 21:01:34 +00001916
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001917 } else if (isIntegral) {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001918
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001919 bool opSigned = opType->isSigned();
1920 bool destSigned = destType->isSigned();
1921 unsigned extSourceInBits = 8 * std::min<unsigned>(opSize, destSize);
1922
1923 assert(! (opSize == destSize && opSigned == destSigned) &&
1924 "How can different int types have same size and signedness?");
1925
1926 bool signExtend = (opSize < destSize && opSigned ||
1927 opSize >= destSize && destSigned);
1928
1929 bool signAndZeroExtend = (opSize < destSize && destSize < 8u &&
1930 opSigned && !destSigned);
1931 assert(!signAndZeroExtend || signExtend);
1932
1933 bool zeroExtendOnly = opSize >= destSize && !destSigned;
1934 assert(!zeroExtendOnly || !signExtend);
1935
1936 if (signExtend) {
1937 Value* signExtDest = (signAndZeroExtend
1938 ? new TmpInstruction(mcfi, destType, opVal)
1939 : destI);
1940
1941 target.getInstrInfo().CreateSignExtensionInstructions
1942 (target, currentFunc,opVal,signExtDest,extSourceInBits,mvec,mcfi);
1943
1944 if (signAndZeroExtend)
1945 target.getInstrInfo().CreateZeroExtensionInstructions
1946 (target, currentFunc, signExtDest, destI, 8*destSize, mvec, mcfi);
1947 }
1948 else if (zeroExtendOnly) {
1949 target.getInstrInfo().CreateZeroExtensionInstructions
1950 (target, currentFunc, opVal, destI, extSourceInBits, mvec, mcfi);
1951 }
1952 else
1953 forwardOperandNum = 0; // forward first operand to user
1954
Misha Brukman7b647942003-05-30 20:11:56 +00001955 } else
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001956 assert(0 && "Unrecognized operand type for convert-to-integer");
1957
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001958 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001959 }
1960
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001961 case 31: // reg: ToFloatTy(reg):
1962 case 32: // reg: ToDoubleTy(reg):
1963 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001964
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001965 // If this instruction has a parent (a user) in the tree
1966 // and the user is translated as an FsMULd instruction,
1967 // then the cast is unnecessary. So check that first.
1968 // In the future, we'll want to do the same for the FdMULq instruction,
1969 // so do the check here instead of only for ToFloatTy(reg).
1970 //
1971 if (subtreeRoot->parent() != NULL) {
1972 const MachineCodeForInstruction& mcfi =
1973 MachineCodeForInstruction::get(
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001974 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001975 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
1976 forwardOperandNum = 0; // forward first operand to user
1977 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001978
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001979 if (forwardOperandNum != 0) { // we do need the cast
1980 Value* leftVal = subtreeRoot->leftChild()->getValue();
1981 const Type* opType = leftVal->getType();
Vikram S. Advee895a742003-08-06 18:48:40 +00001982 MachineOpCode opCode=ChooseConvertToFloatInstr(target,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001983 subtreeRoot->getOpLabel(), opType);
Vikram S. Advee895a742003-08-06 18:48:40 +00001984 if (opCode == V9::NOP) { // no conversion needed
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001985 forwardOperandNum = 0; // forward first operand to user
1986 } else {
1987 // If the source operand is a non-FP type it must be
1988 // first copied from int to float register via memory!
1989 Instruction *dest = subtreeRoot->getInstruction();
1990 Value* srcForCast;
1991 int n = 0;
1992 if (! opType->isFloatingPoint()) {
1993 // Create a temporary to represent the FP register
1994 // into which the integer will be copied via memory.
1995 // The type of this temporary will determine the FP
1996 // register used: single-prec for a 32-bit int or smaller,
1997 // double-prec for a 64-bit int.
1998 //
1999 uint64_t srcSize =
2000 target.getTargetData().getTypeSize(leftVal->getType());
2001 Type* tmpTypeToUse =
2002 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002003 MachineCodeForInstruction &destMCFI =
2004 MachineCodeForInstruction::get(dest);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002005 srcForCast = new TmpInstruction(destMCFI, tmpTypeToUse, dest);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002006
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002007 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002008 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002009 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002010 mvec, destMCFI);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002011 } else
2012 srcForCast = leftVal;
2013
2014 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
2015 mvec.push_back(M);
2016 }
Misha Brukman7b647942003-05-30 20:11:56 +00002017 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002018 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002019
2020 case 19: // reg: ToArrayTy(reg):
2021 case 20: // reg: ToPointerTy(reg):
2022 forwardOperandNum = 0; // forward first operand to user
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002023 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002024
2025 case 233: // reg: Add(reg, Constant)
2026 maskUnsignedResult = true;
2027 M = CreateAddConstInstruction(subtreeRoot);
2028 if (M != NULL) {
2029 mvec.push_back(M);
2030 break;
2031 }
2032 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002033
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002034 case 33: // reg: Add(reg, reg)
2035 maskUnsignedResult = true;
2036 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
2037 break;
2038
2039 case 234: // reg: Sub(reg, Constant)
2040 maskUnsignedResult = true;
2041 M = CreateSubConstInstruction(subtreeRoot);
2042 if (M != NULL) {
2043 mvec.push_back(M);
2044 break;
2045 }
2046 // ELSE FALL THROUGH
2047
2048 case 34: // reg: Sub(reg, reg)
2049 maskUnsignedResult = true;
2050 Add3OperandInstr(ChooseSubInstructionByType(
Chris Lattner54e898e2003-01-15 19:23:34 +00002051 subtreeRoot->getInstruction()->getType()),
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002052 subtreeRoot, mvec);
2053 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002054
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002055 case 135: // reg: Mul(todouble, todouble)
2056 checkCast = true;
2057 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002058
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002059 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002060 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002061 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002062 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002063 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002064 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002065 Instruction* mulInstr = subtreeRoot->getInstruction();
2066 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002067 subtreeRoot->leftChild()->getValue(),
2068 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002069 mulInstr, mvec,
2070 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002071 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002072 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002073 case 335: // reg: Mul(todouble, todoubleConst)
2074 checkCast = true;
2075 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002076
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002077 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00002078 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002079 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002080 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002081 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002082 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002083 Instruction* mulInstr = subtreeRoot->getInstruction();
2084 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002085 subtreeRoot->leftChild()->getValue(),
2086 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002087 mulInstr, mvec,
2088 MachineCodeForInstruction::get(mulInstr),
2089 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002090 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002091 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002092 case 236: // reg: Div(reg, Constant)
2093 maskUnsignedResult = true;
2094 L = mvec.size();
2095 CreateDivConstInstruction(target, subtreeRoot, mvec);
2096 if (mvec.size() > L)
2097 break;
2098 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002099
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002100 case 36: // reg: Div(reg, reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002101 {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002102 maskUnsignedResult = true;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002103
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002104 // If either operand of divide is smaller than 64 bits, we have
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002105 // to make sure the unused top bits are correct because they affect
2106 // the result. These bits are already correct for unsigned values.
2107 // They may be incorrect for signed values, so sign extend to fill in.
2108 Instruction* divI = subtreeRoot->getInstruction();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002109 Value* divOp1 = subtreeRoot->leftChild()->getValue();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002110 Value* divOp2 = subtreeRoot->rightChild()->getValue();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002111 Value* divOp1ToUse = divOp1;
2112 Value* divOp2ToUse = divOp2;
2113 if (divI->getType()->isSigned()) {
2114 unsigned opSize=target.getTargetData().getTypeSize(divI->getType());
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002115 if (opSize < 8) {
2116 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(divI);
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002117 divOp1ToUse = new TmpInstruction(mcfi, divOp1);
2118 divOp2ToUse = new TmpInstruction(mcfi, divOp2);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002119 target.getInstrInfo().
2120 CreateSignExtensionInstructions(target,
2121 divI->getParent()->getParent(),
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002122 divOp1, divOp1ToUse,
2123 8*opSize, mvec, mcfi);
2124 target.getInstrInfo().
2125 CreateSignExtensionInstructions(target,
2126 divI->getParent()->getParent(),
2127 divOp2, divOp2ToUse,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002128 8*opSize, mvec, mcfi);
2129 }
2130 }
2131
2132 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002133 .addReg(divOp1ToUse)
2134 .addReg(divOp2ToUse)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002135 .addRegDef(divI));
2136
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002137 break;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002138 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002139
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002140 case 37: // reg: Rem(reg, reg)
2141 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00002142 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002143 maskUnsignedResult = true;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002144
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002145 Instruction* remI = subtreeRoot->getInstruction();
2146 Value* divOp1 = subtreeRoot->leftChild()->getValue();
2147 Value* divOp2 = subtreeRoot->rightChild()->getValue();
2148
2149 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(remI);
Vikram S. Adve510eec72001-11-04 21:59:14 +00002150
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002151 // If second operand of divide is smaller than 64 bits, we have
2152 // to make sure the unused top bits are correct because they affect
2153 // the result. These bits are already correct for unsigned values.
2154 // They may be incorrect for signed values, so sign extend to fill in.
2155 //
2156 Value* divOpToUse = divOp2;
2157 if (divOp2->getType()->isSigned()) {
2158 unsigned opSize=target.getTargetData().getTypeSize(divOp2->getType());
2159 if (opSize < 8) {
2160 divOpToUse = new TmpInstruction(mcfi, divOp2);
2161 target.getInstrInfo().
2162 CreateSignExtensionInstructions(target,
2163 remI->getParent()->getParent(),
2164 divOp2, divOpToUse,
2165 8*opSize, mvec, mcfi);
2166 }
2167 }
2168
2169 // Now compute: result = rem V1, V2 as:
2170 // result = V1 - (V1 / signExtend(V2)) * signExtend(V2)
2171 //
2172 TmpInstruction* quot = new TmpInstruction(mcfi, divOp1, divOpToUse);
2173 TmpInstruction* prod = new TmpInstruction(mcfi, quot, divOpToUse);
2174
2175 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
2176 .addReg(divOp1).addReg(divOpToUse).addRegDef(quot));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002177
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002178 mvec.push_back(BuildMI(ChooseMulInstructionByType(remI->getType()), 3)
2179 .addReg(quot).addReg(divOpToUse).addRegDef(prod));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002180
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002181 mvec.push_back(BuildMI(ChooseSubInstructionByType(remI->getType()), 3)
2182 .addReg(divOp1).addReg(prod).addRegDef(remI));
2183
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002184 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00002185 }
2186
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002187 case 38: // bool: And(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002188 case 138: // bool: And(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002189 case 238: // bool: And(bool, boolconst)
2190 case 338: // reg : BAnd(reg, reg)
2191 case 538: // reg : BAnd(reg, Constant)
2192 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
2193 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002194
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002195 case 438: // bool: BAnd(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002196 { // Use the argument of NOT as the second argument!
2197 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002198 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002199 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2200 Value* notArg = BinaryOperator::getNotArgument(
2201 cast<BinaryOperator>(notNode->getInstruction()));
2202 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002203 Value *lhs = subtreeRoot->leftChild()->getValue();
2204 Value *dest = subtreeRoot->getValue();
2205 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(lhs).addReg(notArg)
2206 .addReg(dest, MOTy::Def));
2207
2208 if (notArg->getType() == Type::BoolTy)
2209 { // set 1 in result register if result of above is non-zero
2210 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2211 .addReg(dest, MOTy::UseAndDef));
2212 }
2213
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002214 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002215 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002216
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002217 case 39: // bool: Or(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002218 case 139: // bool: Or(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002219 case 239: // bool: Or(bool, boolconst)
2220 case 339: // reg : BOr(reg, reg)
2221 case 539: // reg : BOr(reg, Constant)
2222 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
2223 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002224
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002225 case 439: // bool: BOr(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002226 { // Use the argument of NOT as the second argument!
2227 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002228 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002229 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2230 Value* notArg = BinaryOperator::getNotArgument(
2231 cast<BinaryOperator>(notNode->getInstruction()));
2232 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002233 Value *lhs = subtreeRoot->leftChild()->getValue();
2234 Value *dest = subtreeRoot->getValue();
2235
2236 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(lhs).addReg(notArg)
2237 .addReg(dest, MOTy::Def));
2238
2239 if (notArg->getType() == Type::BoolTy)
2240 { // set 1 in result register if result of above is non-zero
2241 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2242 .addReg(dest, MOTy::UseAndDef));
2243 }
2244
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002245 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002246 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002247
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002248 case 40: // bool: Xor(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002249 case 140: // bool: Xor(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002250 case 240: // bool: Xor(bool, boolconst)
2251 case 340: // reg : BXor(reg, reg)
2252 case 540: // reg : BXor(reg, Constant)
2253 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
2254 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002255
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002256 case 440: // bool: BXor(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002257 { // Use the argument of NOT as the second argument!
2258 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002259 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002260 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2261 Value* notArg = BinaryOperator::getNotArgument(
2262 cast<BinaryOperator>(notNode->getInstruction()));
2263 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002264 Value *lhs = subtreeRoot->leftChild()->getValue();
2265 Value *dest = subtreeRoot->getValue();
2266 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(lhs).addReg(notArg)
2267 .addReg(dest, MOTy::Def));
2268
2269 if (notArg->getType() == Type::BoolTy)
2270 { // set 1 in result register if result of above is non-zero
2271 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2272 .addReg(dest, MOTy::UseAndDef));
2273 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002274 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002275 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002276
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002277 case 41: // setCCconst: SetCC(reg, Constant)
2278 { // Comparison is with a constant:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002279 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002280 // If the bool result must be computed into a register (see below),
2281 // and the constant is int ZERO, we can use the MOVR[op] instructions
2282 // and avoid the SUBcc instruction entirely.
2283 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002284 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002285 // The result of the SetCC must be computed and stored in a register if
2286 // it is used outside the current basic block (so it must be computed
2287 // as a boolreg) or it is used by anything other than a branch.
2288 // We will use a conditional move to do this.
2289 //
2290 Instruction* setCCInstr = subtreeRoot->getInstruction();
2291 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2292 ! AllUsesAreBranches(setCCInstr));
2293
2294 if (computeBoolVal)
2295 {
2296 InstrTreeNode* constNode = subtreeRoot->rightChild();
2297 assert(constNode &&
2298 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
2299 Constant *constVal = cast<Constant>(constNode->getValue());
2300 bool isValidConst;
2301
2302 if ((constVal->getType()->isInteger()
2303 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00002304 && target.getInstrInfo().ConvertConstantToIntType(target,
2305 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002306 && isValidConst)
2307 {
2308 // That constant is an integer zero after all...
2309 // Use a MOVR[op] to compute the boolean result
2310 // Unconditionally set register to 0
2311 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0)
2312 .addRegDef(setCCInstr));
2313
2314 // Now conditionally move 1 into the register.
2315 // Mark the register as a use (as well as a def) because the old
2316 // value will be retained if the condition is false.
2317 MachineOpCode movOpCode = ChooseMovpregiForSetCC(subtreeRoot);
2318 mvec.push_back(BuildMI(movOpCode, 3)
2319 .addReg(subtreeRoot->leftChild()->getValue())
2320 .addZImm(1).addReg(setCCInstr, MOTy::UseAndDef));
2321
2322 break;
2323 }
2324 }
2325 // ELSE FALL THROUGH
2326 }
2327
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002328 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002329 {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002330 // This generates a SUBCC instruction, putting the difference in a
2331 // result reg. if needed, and/or setting a condition code if needed.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002332 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002333 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002334 Value* leftVal = subtreeRoot->leftChild()->getValue();
2335 Value* rightVal = subtreeRoot->rightChild()->getValue();
2336 const Type* opType = leftVal->getType();
2337 bool isFPCompare = opType->isFloatingPoint();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002338
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002339 // If the boolean result of the SetCC is used outside the current basic
2340 // block (so it must be computed as a boolreg) or is used by anything
2341 // other than a branch, the boolean must be computed and stored
2342 // in a result register. We will use a conditional move to do this.
2343 //
2344 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2345 ! AllUsesAreBranches(setCCInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002346
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002347 // A TmpInstruction is created to represent the CC "result".
2348 // Unlike other instances of TmpInstruction, this one is used
2349 // by machine code of multiple LLVM instructions, viz.,
2350 // the SetCC and the branch. Make sure to get the same one!
2351 // Note that we do this even for FP CC registers even though they
2352 // are explicit operands, because the type of the operand
2353 // needs to be a floating point condition code, not an integer
2354 // condition code. Think of this as casting the bool result to
2355 // a FP condition code register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002356 // Later, we mark the 4th operand as being a CC register, and as a def.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002357 //
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002358 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002359 setCCInstr->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00002360 leftVal->getType(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002361 MachineCodeForInstruction::get(setCCInstr));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002362
2363 // If the operands are signed values smaller than 4 bytes, then they
2364 // must be sign-extended in order to do a valid 32-bit comparison
2365 // and get the right result in the 32-bit CC register (%icc).
2366 //
2367 Value* leftOpToUse = leftVal;
2368 Value* rightOpToUse = rightVal;
2369 if (opType->isIntegral() && opType->isSigned()) {
2370 unsigned opSize = target.getTargetData().getTypeSize(opType);
2371 if (opSize < 4) {
2372 MachineCodeForInstruction& mcfi =
2373 MachineCodeForInstruction::get(setCCInstr);
2374
2375 // create temporary virtual regs. to hold the sign-extensions
2376 leftOpToUse = new TmpInstruction(mcfi, leftVal);
2377 rightOpToUse = new TmpInstruction(mcfi, rightVal);
2378
2379 // sign-extend each operand and put the result in the temporary reg.
2380 target.getInstrInfo().CreateSignExtensionInstructions
2381 (target, setCCInstr->getParent()->getParent(),
2382 leftVal, leftOpToUse, 8*opSize, mvec, mcfi);
2383 target.getInstrInfo().CreateSignExtensionInstructions
2384 (target, setCCInstr->getParent()->getParent(),
2385 rightVal, rightOpToUse, 8*opSize, mvec, mcfi);
2386 }
2387 }
2388
Misha Brukman7b647942003-05-30 20:11:56 +00002389 if (! isFPCompare) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002390 // Integer condition: set CC and discard result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002391 mvec.push_back(BuildMI(V9::SUBccr, 4)
2392 .addReg(leftOpToUse)
2393 .addReg(rightOpToUse)
2394 .addMReg(target.getRegInfo().getZeroRegNum(),MOTy::Def)
2395 .addCCReg(tmpForCC, MOTy::Def));
Misha Brukman7b647942003-05-30 20:11:56 +00002396 } else {
2397 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002398 mvec.push_back(BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2399 .addCCReg(tmpForCC, MOTy::Def)
2400 .addReg(leftOpToUse)
2401 .addReg(rightOpToUse));
Misha Brukman7b647942003-05-30 20:11:56 +00002402 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002403
Misha Brukman7b647942003-05-30 20:11:56 +00002404 if (computeBoolVal) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002405 MachineOpCode movOpCode = (isFPCompare
Misha Brukmaneecdb662003-06-02 20:55:14 +00002406 ? ChooseMovFpcciInstruction(subtreeRoot)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002407 : ChooseMovpcciForSetCC(subtreeRoot));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002408
2409 // Unconditionally set register to 0
2410 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
2411 mvec.push_back(M);
2412
2413 // Now conditionally move 1 into the register.
Misha Brukman7b647942003-05-30 20:11:56 +00002414 // Mark the register as a use (as well as a def) because the old
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002415 // value will be retained if the condition is false.
2416 M = (BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(1)
2417 .addReg(setCCInstr, MOTy::UseAndDef));
Misha Brukman7b647942003-05-30 20:11:56 +00002418 mvec.push_back(M);
2419 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002420 break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002421 }
2422
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002423 case 51: // reg: Load(reg)
2424 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002425 SetOperandsForMemInstr(ChooseLoadInstruction(
2426 subtreeRoot->getValue()->getType()),
2427 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002428 break;
2429
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002430 case 55: // reg: GetElemPtr(reg)
2431 case 56: // reg: GetElemPtrIdx(reg,reg)
2432 // If the GetElemPtr was folded into the user (parent), it will be
2433 // caught above. For other cases, we have to compute the address.
2434 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
2435 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002436
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002437 case 57: // reg: Alloca: Implement as 1 instruction:
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002438 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002439 AllocationInst* instr =
2440 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002441 unsigned tsize =
2442 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002443 assert(tsize != 0);
2444 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002445 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002446 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002447
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002448 case 58: // reg: Alloca(reg): Implement as 3 instructions:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002449 // mul num, typeSz -> tmp
2450 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002451 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002452 AllocationInst* instr =
2453 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002454 const Type* eltType = instr->getAllocatedType();
2455
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002456 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002457 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002458 Value* numElementsVal = NULL;
2459 bool isArray = instr->isArrayAllocation();
2460
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002461 if (!isArray || isa<Constant>(numElementsVal = instr->getArraySize())) {
Misha Brukman7b647942003-05-30 20:11:56 +00002462 // total size is constant: generate code for fixed-size alloca
2463 unsigned numElements = isArray?
2464 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2465 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2466 numElements, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002467 } else {
2468 // total size is not constant.
Vikram S. Adve74825322002-03-18 03:15:35 +00002469 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002470 numElementsVal, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002471 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002472 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002473 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002474
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002475 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002476 { // Generate a direct (CALL) or indirect (JMPL) call.
2477 // Mark the return-address register, the indirection
2478 // register (for indirect calls), the operands of the Call,
2479 // and the return value (if any) as implicit operands
2480 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002481 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002482 // If this is a varargs function, floating point arguments
2483 // have to passed in integer registers so insert
2484 // copy-float-to-int instructions for each float operand.
2485 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002486 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002487 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002488 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002489
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002490 // Check if this is an intrinsic function that needs a special code
2491 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002492 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002493 bool specialIntrinsic = false;
2494 LLVMIntrinsic::ID iid;
2495 if (calledFunc && (iid=(LLVMIntrinsic::ID)calledFunc->getIntrinsicID()))
2496 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002497
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002498 // If not, generate the normal call sequence for the function.
2499 // This can also handle any intrinsics that are just function calls.
2500 //
Misha Brukman7b647942003-05-30 20:11:56 +00002501 if (! specialIntrinsic) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002502 Function* currentFunc = callInstr->getParent()->getParent();
2503 MachineFunction& MF = MachineFunction::get(currentFunc);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002504 MachineCodeForInstruction& mcfi =
2505 MachineCodeForInstruction::get(callInstr);
2506 const UltraSparcRegInfo& regInfo =
2507 (UltraSparcRegInfo&) target.getRegInfo();
2508 const TargetFrameInfo& frameInfo = target.getFrameInfo();
2509
Misha Brukman7b647942003-05-30 20:11:56 +00002510 // Create hidden virtual register for return address with type void*
2511 TmpInstruction* retAddrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002512 new TmpInstruction(mcfi, PointerType::get(Type::VoidTy), callInstr);
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002513
Misha Brukman7b647942003-05-30 20:11:56 +00002514 // Generate the machine instruction and its operands.
2515 // Use CALL for direct function calls; this optimistically assumes
2516 // the PC-relative address fits in the CALL address field (22 bits).
2517 // Use JMPL for indirect calls.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002518 // This will be added to mvec later, after operand copies.
Misha Brukman7b647942003-05-30 20:11:56 +00002519 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002520 MachineInstr* callMI;
Misha Brukman7b647942003-05-30 20:11:56 +00002521 if (calledFunc) // direct function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002522 callMI = BuildMI(V9::CALL, 1).addPCDisp(callee);
Misha Brukman7b647942003-05-30 20:11:56 +00002523 else // indirect function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002524 callMI = (BuildMI(V9::JMPLCALLi,3).addReg(callee)
2525 .addSImm((int64_t)0).addRegDef(retAddrReg));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002526
Misha Brukman7b647942003-05-30 20:11:56 +00002527 const FunctionType* funcType =
2528 cast<FunctionType>(cast<PointerType>(callee->getType())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002529 ->getElementType());
Misha Brukman7b647942003-05-30 20:11:56 +00002530 bool isVarArgs = funcType->isVarArg();
2531 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002532
Misha Brukman7b647942003-05-30 20:11:56 +00002533 // Use a descriptor to pass information about call arguments
2534 // to the register allocator. This descriptor will be "owned"
2535 // and freed automatically when the MachineCodeForInstruction
2536 // object for the callInstr goes away.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002537 CallArgsDescriptor* argDesc =
2538 new CallArgsDescriptor(callInstr, retAddrReg,isVarArgs,noPrototype);
Misha Brukman7b647942003-05-30 20:11:56 +00002539 assert(callInstr->getOperand(0) == callee
2540 && "This is assumed in the loop below!");
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002541
2542 // Insert sign-extension instructions for small signed values,
2543 // if this is an unknown function (i.e., called via a funcptr)
2544 // or an external one (i.e., which may not be compiled by llc).
2545 //
2546 if (calledFunc == NULL || calledFunc->isExternal()) {
2547 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
2548 Value* argVal = callInstr->getOperand(i);
2549 const Type* argType = argVal->getType();
2550 if (argType->isIntegral() && argType->isSigned()) {
2551 unsigned argSize = target.getTargetData().getTypeSize(argType);
2552 if (argSize <= 4) {
2553 // create a temporary virtual reg. to hold the sign-extension
2554 TmpInstruction* argExtend = new TmpInstruction(mcfi, argVal);
2555
2556 // sign-extend argVal and put the result in the temporary reg.
2557 target.getInstrInfo().CreateSignExtensionInstructions
2558 (target, currentFunc, argVal, argExtend,
2559 8*argSize, mvec, mcfi);
2560
2561 // replace argVal with argExtend in CallArgsDescriptor
2562 argDesc->getArgInfo(i-1).replaceArgVal(argExtend);
2563 }
2564 }
2565 }
2566 }
2567
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002568 // Insert copy instructions to get all the arguments into
2569 // all the places that they need to be.
2570 //
Misha Brukman7b647942003-05-30 20:11:56 +00002571 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002572 int argNo = i-1;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002573 CallArgInfo& argInfo = argDesc->getArgInfo(argNo);
2574 Value* argVal = argInfo.getArgVal(); // don't use callInstr arg here
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002575 const Type* argType = argVal->getType();
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002576 unsigned regType = regInfo.getRegTypeForDataType(argType);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002577 unsigned argSize = target.getTargetData().getTypeSize(argType);
2578 int regNumForArg = TargetRegInfo::getInvalidRegNum();
2579 unsigned regClassIDOfArgReg;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002580
Misha Brukman7b647942003-05-30 20:11:56 +00002581 // Check for FP arguments to varargs functions.
2582 // Any such argument in the first $K$ args must be passed in an
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002583 // integer register. If there is no prototype, it must also
2584 // be passed as an FP register.
2585 // K = #integer argument registers.
2586 bool isFPArg = argVal->getType()->isFloatingPoint();
2587 if (isVarArgs && isFPArg) {
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002588
2589 if (noPrototype) {
2590 // It is a function with no prototype: pass value
2591 // as an FP value as well as a varargs value. The FP value
2592 // may go in a register or on the stack. The copy instruction
2593 // to the outgoing reg/stack is created by the normal argument
2594 // handling code since this is the "normal" passing mode.
2595 //
2596 regNumForArg = regInfo.regNumForFPArg(regType,
2597 false, false, argNo,
2598 regClassIDOfArgReg);
2599 if (regNumForArg == regInfo.getInvalidRegNum())
2600 argInfo.setUseStackSlot();
2601 else
2602 argInfo.setUseFPArgReg();
2603 }
2604
2605 // If this arg. is in the first $K$ regs, add special copy-
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002606 // float-to-int instructions to pass the value as an int.
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002607 // To check if it is in the first $K$, get the register
2608 // number for the arg #i. These copy instructions are
2609 // generated here because they are extra cases and not needed
2610 // for the normal argument handling (some code reuse is
2611 // possible though -- later).
2612 //
Misha Brukmanea481cc2003-06-03 03:21:58 +00002613 int copyRegNum = regInfo.regNumForIntArg(false, false, argNo,
2614 regClassIDOfArgReg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002615 if (copyRegNum != regInfo.getInvalidRegNum()) {
2616 // Create a virtual register to represent copyReg. Mark
2617 // this vreg as being an implicit operand of the call MI
2618 const Type* loadTy = (argType == Type::FloatTy
2619 ? Type::IntTy : Type::LongTy);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002620 TmpInstruction* argVReg = new TmpInstruction(mcfi, loadTy,
2621 argVal, NULL,
2622 "argRegCopy");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002623 callMI->addImplicitRef(argVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002624
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002625 // Get a temp stack location to use to copy
2626 // float-to-int via the stack.
2627 //
2628 // FIXME: For now, we allocate permanent space because
2629 // the stack frame manager does not allow locals to be
2630 // allocated (e.g., for alloca) after a temp is
2631 // allocated!
2632 //
2633 // int tmpOffset = MF.getInfo()->pushTempValue(argSize);
2634 int tmpOffset = MF.getInfo()->allocateLocalVar(argVReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002635
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002636 // Generate the store from FP reg to stack
Misha Brukmanea481cc2003-06-03 03:21:58 +00002637 unsigned StoreOpcode = ChooseStoreInstruction(argType);
2638 M = BuildMI(convertOpcodeFromRegToImm(StoreOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002639 .addReg(argVal).addMReg(regInfo.getFramePointer())
2640 .addSImm(tmpOffset);
2641 mvec.push_back(M);
2642
2643 // Generate the load from stack to int arg reg
Misha Brukmanea481cc2003-06-03 03:21:58 +00002644 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
2645 M = BuildMI(convertOpcodeFromRegToImm(LoadOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002646 .addMReg(regInfo.getFramePointer()).addSImm(tmpOffset)
2647 .addReg(argVReg, MOTy::Def);
2648
2649 // Mark operand with register it should be assigned
2650 // both for copy and for the callMI
2651 M->SetRegForOperand(M->getNumOperands()-1, copyRegNum);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002652 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2653 copyRegNum);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002654 mvec.push_back(M);
2655
2656 // Add info about the argument to the CallArgsDescriptor
2657 argInfo.setUseIntArgReg();
2658 argInfo.setArgCopy(copyRegNum);
2659 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00002660 // Cannot fit in first $K$ regs so pass arg on stack
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002661 argInfo.setUseStackSlot();
2662 }
2663 } else if (isFPArg) {
2664 // Get the outgoing arg reg to see if there is one.
2665 regNumForArg = regInfo.regNumForFPArg(regType, false, false,
2666 argNo, regClassIDOfArgReg);
2667 if (regNumForArg == regInfo.getInvalidRegNum())
2668 argInfo.setUseStackSlot();
2669 else {
2670 argInfo.setUseFPArgReg();
2671 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2672 regNumForArg);
2673 }
2674 } else {
2675 // Get the outgoing arg reg to see if there is one.
2676 regNumForArg = regInfo.regNumForIntArg(false,false,
2677 argNo, regClassIDOfArgReg);
2678 if (regNumForArg == regInfo.getInvalidRegNum())
2679 argInfo.setUseStackSlot();
2680 else {
2681 argInfo.setUseIntArgReg();
2682 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2683 regNumForArg);
2684 }
2685 }
2686
2687 //
2688 // Now insert copy instructions to stack slot or arg. register
2689 //
2690 if (argInfo.usesStackSlot()) {
2691 // Get the stack offset for this argument slot.
2692 // FP args on stack are right justified so adjust offset!
2693 // int arguments are also right justified but they are
2694 // always loaded as a full double-word so the offset does
2695 // not need to be adjusted.
2696 int argOffset = frameInfo.getOutgoingArgOffset(MF, argNo);
2697 if (argType->isFloatingPoint()) {
2698 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
2699 assert(argSize <= slotSize && "Insufficient slot size!");
2700 argOffset += slotSize - argSize;
2701 }
2702
2703 // Now generate instruction to copy argument to stack
2704 MachineOpCode storeOpCode =
2705 (argType->isFloatingPoint()
2706 ? ((argSize == 4)? V9::STFi : V9::STDFi) : V9::STXi);
2707
2708 M = BuildMI(storeOpCode, 3).addReg(argVal)
2709 .addMReg(regInfo.getStackPointer()).addSImm(argOffset);
2710 mvec.push_back(M);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002711 }
2712 else if (regNumForArg != regInfo.getInvalidRegNum()) {
2713
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002714 // Create a virtual register to represent the arg reg. Mark
2715 // this vreg as being an implicit operand of the call MI.
2716 TmpInstruction* argVReg =
2717 new TmpInstruction(mcfi, argVal, NULL, "argReg");
2718
2719 callMI->addImplicitRef(argVReg);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002720
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002721 // Generate the reg-to-reg copy into the outgoing arg reg.
2722 // -- For FP values, create a FMOVS or FMOVD instruction
2723 // -- For non-FP values, create an add-with-0 instruction
2724 if (argType->isFloatingPoint())
2725 M=(BuildMI(argType==Type::FloatTy? V9::FMOVS :V9::FMOVD,2)
2726 .addReg(argVal).addReg(argVReg, MOTy::Def));
2727 else
2728 M = (BuildMI(ChooseAddInstructionByType(argType), 3)
2729 .addReg(argVal).addSImm((int64_t) 0)
2730 .addReg(argVReg, MOTy::Def));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002731
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002732 // Mark the operand with the register it should be assigned
2733 M->SetRegForOperand(M->getNumOperands()-1, regNumForArg);
2734 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2735 regNumForArg);
2736
2737 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +00002738 }
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002739 else
2740 assert(argInfo.getArgCopy() != regInfo.getInvalidRegNum() &&
2741 "Arg. not in stack slot, primary or secondary register?");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002742 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002743
2744 // add call instruction and delay slot before copying return value
2745 mvec.push_back(callMI);
2746 mvec.push_back(BuildMI(V9::NOP, 0));
2747
Misha Brukman7b647942003-05-30 20:11:56 +00002748 // Add the return value as an implicit ref. The call operands
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002749 // were added above. Also, add code to copy out the return value.
2750 // This is always register-to-register for int or FP return values.
2751 //
2752 if (callInstr->getType() != Type::VoidTy) {
2753 // Get the return value reg.
2754 const Type* retType = callInstr->getType();
2755
2756 int regNum = (retType->isFloatingPoint()
2757 ? (unsigned) SparcFloatRegClass::f0
2758 : (unsigned) SparcIntRegClass::o0);
2759 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
2760 regNum = regInfo.getUnifiedRegNum(regClassID, regNum);
2761
2762 // Create a virtual register to represent it and mark
2763 // this vreg as being an implicit operand of the call MI
2764 TmpInstruction* retVReg =
2765 new TmpInstruction(mcfi, callInstr, NULL, "argReg");
2766
2767 callMI->addImplicitRef(retVReg, /*isDef*/ true);
2768
2769 // Generate the reg-to-reg copy from the return value reg.
2770 // -- For FP values, create a FMOVS or FMOVD instruction
2771 // -- For non-FP values, create an add-with-0 instruction
2772 if (retType->isFloatingPoint())
2773 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
2774 .addReg(retVReg).addReg(callInstr, MOTy::Def));
2775 else
2776 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
2777 .addReg(retVReg).addSImm((int64_t) 0)
2778 .addReg(callInstr, MOTy::Def));
2779
2780 // Mark the operand with the register it should be assigned
2781 // Also mark the implicit ref of the call defining this operand
2782 M->SetRegForOperand(0, regNum);
2783 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,regNum);
2784
2785 mvec.push_back(M);
2786 }
2787
Misha Brukman7b647942003-05-30 20:11:56 +00002788 // For the CALL instruction, the ret. addr. reg. is also implicit
2789 if (isa<Function>(callee))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002790 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
2791
2792 MF.getInfo()->popAllTempValues(); // free temps used for this inst
Misha Brukman7b647942003-05-30 20:11:56 +00002793 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002794
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002795 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002796 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002797
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002798 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002799 {
2800 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2801 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2802 Instruction* shlInstr = subtreeRoot->getInstruction();
2803
2804 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002805 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2806 "Shl unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002807 unsigned opSize = target.getTargetData().getTypeSize(opType);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002808
2809 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Vikram S. Advee895a742003-08-06 18:48:40 +00002810 (opSize > 4)? V9::SLLXr6:V9::SLLr5,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002811 argVal1, argVal2, 0, shlInstr, mvec,
2812 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002813 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002814 }
2815
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002816 case 63: // reg: Shr(reg, reg)
Misha Brukman7b647942003-05-30 20:11:56 +00002817 {
2818 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002819 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2820 "Shr unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002821 unsigned opSize = target.getTargetData().getTypeSize(opType);
Chris Lattner54e898e2003-01-15 19:23:34 +00002822 Add3OperandInstr(opType->isSigned()
Vikram S. Advee895a742003-08-06 18:48:40 +00002823 ? (opSize > 4? V9::SRAXr6 : V9::SRAr5)
2824 : (opSize > 4? V9::SRLXr6 : V9::SRLr5),
Chris Lattner54e898e2003-01-15 19:23:34 +00002825 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002826 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002827 }
2828
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002829 case 64: // reg: Phi(reg,reg)
2830 break; // don't forward the value
Vikram S. Adve74825322002-03-18 03:15:35 +00002831
Vikram S. Adve9d275142003-08-12 03:04:05 +00002832 case 65: // reg: VaArg(reg): the va_arg instruction
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002833 {
2834 // Use value initialized by va_start as pointer to args on the stack.
2835 // Load argument via current pointer value, then increment pointer.
2836 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
2837 Instruction* vaArgI = subtreeRoot->getInstruction();
Vikram S. Adve9d275142003-08-12 03:04:05 +00002838 MachineOpCode loadOp = vaArgI->getType()->isFloatingPoint()? V9::LDDFi
2839 : V9::LDXi;
2840 mvec.push_back(BuildMI(loadOp, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002841 addSImm(0).addRegDef(vaArgI));
Misha Brukman91aee472003-05-27 22:37:00 +00002842 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002843 addSImm(argSize).addRegDef(vaArgI->getOperand(0)));
2844 break;
2845 }
2846
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002847 case 71: // reg: VReg
2848 case 72: // reg: Constant
2849 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002850
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002851 default:
2852 assert(0 && "Unrecognized BURG rule");
2853 break;
2854 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002855 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002856
Misha Brukman7b647942003-05-30 20:11:56 +00002857 if (forwardOperandNum >= 0) {
2858 // We did not generate a machine instruction but need to use operand.
2859 // If user is in the same tree, replace Value in its machine operand.
2860 // If not, insert a copy instruction which should get coalesced away
2861 // by register allocation.
2862 if (subtreeRoot->parent() != NULL)
2863 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2864 else {
2865 std::vector<MachineInstr*> minstrVec;
2866 Instruction* instr = subtreeRoot->getInstruction();
2867 target.getInstrInfo().
2868 CreateCopyInstructionsByType(target,
2869 instr->getParent()->getParent(),
2870 instr->getOperand(forwardOperandNum),
2871 instr, minstrVec,
2872 MachineCodeForInstruction::get(instr));
2873 assert(minstrVec.size() > 0);
2874 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Chris Lattner20b1ea02001-09-14 03:47:57 +00002875 }
Misha Brukman7b647942003-05-30 20:11:56 +00002876 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002877
Misha Brukman7b647942003-05-30 20:11:56 +00002878 if (maskUnsignedResult) {
2879 // If result is unsigned and smaller than int reg size,
2880 // we need to clear high bits of result value.
2881 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2882 Instruction* dest = subtreeRoot->getInstruction();
2883 if (dest->getType()->isUnsigned()) {
2884 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
2885 if (destSize <= 4) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002886 // Mask high 64 - N bits, where N = 4*destSize.
2887
2888 // Use a TmpInstruction to represent the
Misha Brukman7b647942003-05-30 20:11:56 +00002889 // intermediate result before masking. Since those instructions
2890 // have already been generated, go back and substitute tmpI
2891 // for dest in the result position of each one of them.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002892 //
2893 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(dest);
2894 TmpInstruction *tmpI = new TmpInstruction(mcfi, dest->getType(),
2895 dest, NULL, "maskHi");
2896 Value* srlArgToUse = tmpI;
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002897
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002898 unsigned numSubst = 0;
2899 for (unsigned i=0, N=mvec.size(); i < N; ++i) {
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002900
2901 // Make sure we substitute all occurrences of dest in these instrs.
2902 // Otherwise, we will have bogus code.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002903 bool someArgsWereIgnored = false;
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002904
2905 // Make sure not to substitute an upwards-exposed use -- that would
2906 // introduce a use of `tmpI' with no preceding def. Therefore,
2907 // substitute a use or def-and-use operand only if a previous def
2908 // operand has already been substituted (i.e., numSusbt > 0).
2909 //
2910 numSubst += mvec[i]->substituteValue(dest, tmpI,
2911 /*defsOnly*/ numSubst == 0,
2912 /*notDefsAndUses*/ numSubst > 0,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002913 someArgsWereIgnored);
2914 assert(!someArgsWereIgnored &&
2915 "Operand `dest' exists but not replaced: probably bogus!");
2916 }
2917 assert(numSubst > 0 && "Operand `dest' not replaced: probably bogus!");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002918
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002919 // Left shift 32-N if size (N) is less than 32 bits.
Misha Brukman452db672003-09-23 17:28:11 +00002920 // Use another tmp. virtual register to represent this result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002921 if (destSize < 4) {
2922 srlArgToUse = new TmpInstruction(mcfi, dest->getType(),
2923 tmpI, NULL, "maskHi2");
2924 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpI)
2925 .addZImm(8*(4-destSize))
2926 .addReg(srlArgToUse, MOTy::Def));
2927 }
2928
2929 // Logical right shift 32-N to get zero extension in top 64-N bits.
2930 mvec.push_back(BuildMI(V9::SRLi5, 3).addReg(srlArgToUse)
2931 .addZImm(8*(4-destSize)).addReg(dest, MOTy::Def));
2932
Misha Brukman7b647942003-05-30 20:11:56 +00002933 } else if (destSize < 8) {
2934 assert(0 && "Unsupported type size: 32 < size < 64 bits");
2935 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002936 }
Misha Brukman7b647942003-05-30 20:11:56 +00002937 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002938}