blob: 62bc3cb6db60e96472d027376f05862d52f2ae6c [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000011#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerea45d7b2002-12-28 20:19:44 +000016#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner9c461082002-02-03 07:50:56 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/iTerminators.h"
20#include "llvm/iMemory.h"
21#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000024#include "llvm/ConstantHandling.h"
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +000025#include "llvm/Intrinsics.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000027#include <math.h>
Chris Lattner20b1ea02001-09-14 03:47:57 +000028
Chris Lattner54e898e2003-01-15 19:23:34 +000029static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +000030 std::vector<MachineInstr*>& mvec) {
Chris Lattner54e898e2003-01-15 19:23:34 +000031 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
32 .addReg(Node->rightChild()->getValue())
33 .addRegDef(Node->getValue()));
34}
35
36
37
Chris Lattner795ba6c2003-01-15 21:36:50 +000038//---------------------------------------------------------------------------
39// Function: GetMemInstArgs
40//
41// Purpose:
42// Get the pointer value and the index vector for a memory operation
43// (GetElementPtr, Load, or Store). If all indices of the given memory
44// operation are constant, fold in constant indices in a chain of
45// preceding GetElementPtr instructions (if any), and return the
46// pointer value of the first instruction in the chain.
47// All folded instructions are marked so no code is generated for them.
48//
49// Return values:
50// Returns the pointer Value to use.
51// Returns the resulting IndexVector in idxVec.
52// Returns true/false in allConstantIndices if all indices are/aren't const.
53//---------------------------------------------------------------------------
54
55
56//---------------------------------------------------------------------------
57// Function: FoldGetElemChain
58//
59// Purpose:
60// Fold a chain of GetElementPtr instructions containing only
61// constant offsets into an equivalent (Pointer, IndexVector) pair.
62// Returns the pointer Value, and stores the resulting IndexVector
63// in argument chainIdxVec. This is a helper function for
64// FoldConstantIndices that does the actual folding.
65//---------------------------------------------------------------------------
66
67
68// Check for a constant 0.
69inline bool
70IsZero(Value* idx)
71{
72 return (idx == ConstantSInt::getNullValue(idx->getType()));
73}
74
75static Value*
Misha Brukmanee563cb2003-05-21 17:59:06 +000076FoldGetElemChain(InstrTreeNode* ptrNode, std::vector<Value*>& chainIdxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +000077 bool lastInstHasLeadingNonZero)
78{
79 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
80 GetElementPtrInst* gepInst =
81 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
82
83 // ptr value is not computed in this tree or ptr value does not come from GEP
84 // instruction
85 if (gepInst == NULL)
86 return NULL;
87
88 // Return NULL if we don't fold any instructions in.
89 Value* ptrVal = NULL;
90
91 // Now chase the chain of getElementInstr instructions, if any.
92 // Check for any non-constant indices and stop there.
93 // Also, stop if the first index of child is a non-zero array index
94 // and the last index of the current node is a non-array index:
95 // in that case, a non-array declared type is being accessed as an array
96 // which is not type-safe, but could be legal.
97 //
98 InstructionNode* ptrChild = gepNode;
99 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
100 ptrChild->getOpLabel() == GetElemPtrIdx))
Misha Brukman81b06862003-05-21 18:48:06 +0000101 {
102 // Child is a GetElemPtr instruction
103 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
104 User::op_iterator OI, firstIdx = gepInst->idx_begin();
105 User::op_iterator lastIdx = gepInst->idx_end();
106 bool allConstantOffsets = true;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000107
Misha Brukman81b06862003-05-21 18:48:06 +0000108 // The first index of every GEP must be an array index.
109 assert((*firstIdx)->getType() == Type::LongTy &&
110 "INTERNAL ERROR: Structure index for a pointer type!");
Chris Lattner795ba6c2003-01-15 21:36:50 +0000111
Misha Brukman81b06862003-05-21 18:48:06 +0000112 // If the last instruction had a leading non-zero index, check if the
113 // current one references a sequential (i.e., indexable) type.
114 // If not, the code is not type-safe and we would create an illegal GEP
115 // by folding them, so don't fold any more instructions.
116 //
117 if (lastInstHasLeadingNonZero)
118 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
119 break; // cannot fold in any preceding getElementPtr instrs.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000120
Misha Brukman81b06862003-05-21 18:48:06 +0000121 // Check that all offsets are constant for this instruction
122 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
123 allConstantOffsets = isa<ConstantInt>(*OI);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000124
Misha Brukman81b06862003-05-21 18:48:06 +0000125 if (allConstantOffsets) {
126 // Get pointer value out of ptrChild.
127 ptrVal = gepInst->getPointerOperand();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000128
Misha Brukman81b06862003-05-21 18:48:06 +0000129 // Remember if it has leading zero index: it will be discarded later.
130 lastInstHasLeadingNonZero = ! IsZero(*firstIdx);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000131
Misha Brukman81b06862003-05-21 18:48:06 +0000132 // Insert its index vector at the start, skipping any leading [0]
133 chainIdxVec.insert(chainIdxVec.begin(),
134 firstIdx + !lastInstHasLeadingNonZero, lastIdx);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000135
Misha Brukman81b06862003-05-21 18:48:06 +0000136 // Mark the folded node so no code is generated for it.
137 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000138
Misha Brukman81b06862003-05-21 18:48:06 +0000139 // Get the previous GEP instruction and continue trying to fold
140 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
141 } else // cannot fold this getElementPtr instr. or any preceding ones
142 break;
143 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000144
145 // If the first getElementPtr instruction had a leading [0], add it back.
146 // Note that this instruction is the *last* one successfully folded above.
147 if (ptrVal && ! lastInstHasLeadingNonZero)
148 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
149
150 return ptrVal;
151}
152
153
154//---------------------------------------------------------------------------
155// Function: GetGEPInstArgs
156//
157// Purpose:
158// Helper function for GetMemInstArgs that handles the final getElementPtr
159// instruction used by (or same as) the memory operation.
160// Extracts the indices of the current instruction and tries to fold in
161// preceding ones if all indices of the current one are constant.
162//---------------------------------------------------------------------------
163
164static Value *
165GetGEPInstArgs(InstructionNode* gepNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000166 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000167 bool& allConstantIndices)
168{
169 allConstantIndices = true;
170 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
171
172 // Default pointer is the one from the current instruction.
173 Value* ptrVal = gepI->getPointerOperand();
174 InstrTreeNode* ptrChild = gepNode->leftChild();
175
176 // Extract the index vector of the GEP instructin.
177 // If all indices are constant and first index is zero, try to fold
178 // in preceding GEPs with all constant indices.
179 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
180 allConstantIndices && OI != OE; ++OI)
181 if (! isa<Constant>(*OI))
182 allConstantIndices = false; // note: this also terminates loop!
183
184 // If we have only constant indices, fold chains of constant indices
185 // in this and any preceding GetElemPtr instructions.
186 bool foldedGEPs = false;
187 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
188 if (allConstantIndices)
Misha Brukman81b06862003-05-21 18:48:06 +0000189 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) {
190 ptrVal = newPtr;
191 foldedGEPs = true;
192 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000193
194 // Append the index vector of the current instruction.
195 // Skip the leading [0] index if preceding GEPs were folded into this.
196 idxVec.insert(idxVec.end(),
197 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
198 gepI->idx_end());
199
200 return ptrVal;
201}
202
203//---------------------------------------------------------------------------
204// Function: GetMemInstArgs
205//
206// Purpose:
207// Get the pointer value and the index vector for a memory operation
208// (GetElementPtr, Load, or Store). If all indices of the given memory
209// operation are constant, fold in constant indices in a chain of
210// preceding GetElementPtr instructions (if any), and return the
211// pointer value of the first instruction in the chain.
212// All folded instructions are marked so no code is generated for them.
213//
214// Return values:
215// Returns the pointer Value to use.
216// Returns the resulting IndexVector in idxVec.
217// Returns true/false in allConstantIndices if all indices are/aren't const.
218//---------------------------------------------------------------------------
219
220static Value*
221GetMemInstArgs(InstructionNode* memInstrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000222 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000223 bool& allConstantIndices)
224{
225 allConstantIndices = false;
226 Instruction* memInst = memInstrNode->getInstruction();
227 assert(idxVec.size() == 0 && "Need empty vector to return indices");
228
229 // If there is a GetElemPtr instruction to fold in to this instr,
230 // it must be in the left child for Load and GetElemPtr, and in the
231 // right child for Store instructions.
232 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
233 ? memInstrNode->rightChild()
234 : memInstrNode->leftChild());
235
236 // Default pointer is the one from the current instruction.
237 Value* ptrVal = ptrChild->getValue();
238
239 // Find the "last" GetElemPtr instruction: this one or the immediate child.
240 // There will be none if this is a load or a store from a scalar pointer.
241 InstructionNode* gepNode = NULL;
242 if (isa<GetElementPtrInst>(memInst))
243 gepNode = memInstrNode;
Misha Brukman81b06862003-05-21 18:48:06 +0000244 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) {
245 // Child of load/store is a GEP and memInst is its only use.
246 // Use its indices and mark it as folded.
247 gepNode = cast<InstructionNode>(ptrChild);
248 gepNode->markFoldedIntoParent();
249 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000250
251 // If there are no indices, return the current pointer.
252 // Else extract the pointer from the GEP and fold the indices.
253 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
254 : ptrVal;
255}
256
Chris Lattner54e898e2003-01-15 19:23:34 +0000257
Chris Lattner20b1ea02001-09-14 03:47:57 +0000258//************************ Internal Functions ******************************/
259
Chris Lattner20b1ea02001-09-14 03:47:57 +0000260
Chris Lattner20b1ea02001-09-14 03:47:57 +0000261static inline MachineOpCode
262ChooseBprInstruction(const InstructionNode* instrNode)
263{
264 MachineOpCode opCode;
265
266 Instruction* setCCInstr =
267 ((InstructionNode*) instrNode->leftChild())->getInstruction();
268
269 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000270 {
271 case Instruction::SetEQ: opCode = V9::BRZ; break;
272 case Instruction::SetNE: opCode = V9::BRNZ; break;
273 case Instruction::SetLE: opCode = V9::BRLEZ; break;
274 case Instruction::SetGE: opCode = V9::BRGEZ; break;
275 case Instruction::SetLT: opCode = V9::BRLZ; break;
276 case Instruction::SetGT: opCode = V9::BRGZ; break;
277 default:
278 assert(0 && "Unrecognized VM instruction!");
279 opCode = V9::INVALID_OPCODE;
280 break;
281 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000282
283 return opCode;
284}
285
286
287static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000288ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000289 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000290{
Misha Brukmana98cd452003-05-20 20:32:24 +0000291 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000292
293 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
294
Misha Brukman81b06862003-05-21 18:48:06 +0000295 if (isSigned) {
296 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000297 {
Misha Brukman81b06862003-05-21 18:48:06 +0000298 case Instruction::SetEQ: opCode = V9::BE; break;
299 case Instruction::SetNE: opCode = V9::BNE; break;
300 case Instruction::SetLE: opCode = V9::BLE; break;
301 case Instruction::SetGE: opCode = V9::BGE; break;
302 case Instruction::SetLT: opCode = V9::BL; break;
303 case Instruction::SetGT: opCode = V9::BG; break;
304 default:
305 assert(0 && "Unrecognized VM instruction!");
306 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000307 }
Misha Brukman81b06862003-05-21 18:48:06 +0000308 } else {
309 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000310 {
Misha Brukman81b06862003-05-21 18:48:06 +0000311 case Instruction::SetEQ: opCode = V9::BE; break;
312 case Instruction::SetNE: opCode = V9::BNE; break;
313 case Instruction::SetLE: opCode = V9::BLEU; break;
314 case Instruction::SetGE: opCode = V9::BCC; break;
315 case Instruction::SetLT: opCode = V9::BCS; break;
316 case Instruction::SetGT: opCode = V9::BGU; break;
317 default:
318 assert(0 && "Unrecognized VM instruction!");
319 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000320 }
Misha Brukman81b06862003-05-21 18:48:06 +0000321 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000322
323 return opCode;
324}
325
326static inline MachineOpCode
327ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000328 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000329{
Misha Brukmana98cd452003-05-20 20:32:24 +0000330 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000331
332 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000333 {
334 case Instruction::SetEQ: opCode = V9::FBE; break;
335 case Instruction::SetNE: opCode = V9::FBNE; break;
336 case Instruction::SetLE: opCode = V9::FBLE; break;
337 case Instruction::SetGE: opCode = V9::FBGE; break;
338 case Instruction::SetLT: opCode = V9::FBL; break;
339 case Instruction::SetGT: opCode = V9::FBG; break;
340 default:
341 assert(0 && "Unrecognized VM instruction!");
342 break;
343 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000344
345 return opCode;
346}
347
348
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000349// Create a unique TmpInstruction for a boolean value,
350// representing the CC register used by a branch on that value.
351// For now, hack this using a little static cache of TmpInstructions.
352// Eventually the entire BURG instruction selection should be put
353// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000354// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000355// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000356//
357static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000358GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000359{
Chris Lattner09ff1122002-07-24 21:21:32 +0000360 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000361 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000362 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000363
364 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
365
Misha Brukman81b06862003-05-21 18:48:06 +0000366 if (lastFunction != F) {
367 lastFunction = F;
368 boolToTmpCache.clear();
369 }
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000370
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000371 // Look for tmpI and create a new one otherwise. The new value is
372 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000373 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
374 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000375 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000376
377 return tmpI;
378}
379
380
Chris Lattner20b1ea02001-09-14 03:47:57 +0000381static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000382ChooseBccInstruction(const InstructionNode* instrNode,
383 bool& isFPBranch)
384{
385 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000386 assert(setCCNode->getOpLabel() == SetCCOp);
387 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000388 const Type* setCCType = setCCInstr->getOperand(0)->getType();
389
Vikram S. Adve242a8082002-05-19 15:25:51 +0000390 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
391
392 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000393 return ChooseBFpccInstruction(instrNode, setCCInstr);
394 else
395 return ChooseBpccInstruction(instrNode, setCCInstr);
396}
397
398
399static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000400ChooseMovFpccInstruction(const InstructionNode* instrNode)
401{
Misha Brukmana98cd452003-05-20 20:32:24 +0000402 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000403
404 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000405 {
406 case Instruction::SetEQ: opCode = V9::MOVFE; break;
407 case Instruction::SetNE: opCode = V9::MOVFNE; break;
408 case Instruction::SetLE: opCode = V9::MOVFLE; break;
409 case Instruction::SetGE: opCode = V9::MOVFGE; break;
410 case Instruction::SetLT: opCode = V9::MOVFL; break;
411 case Instruction::SetGT: opCode = V9::MOVFG; break;
412 default:
413 assert(0 && "Unrecognized VM instruction!");
414 break;
415 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000416
417 return opCode;
418}
419
420
421// Assumes that SUBcc v1, v2 -> v3 has been executed.
422// In most cases, we want to clear v3 and then follow it by instruction
423// MOVcc 1 -> v3.
424// Set mustClearReg=false if v3 need not be cleared before conditional move.
425// Set valueToMove=0 if we want to conditionally move 0 instead of 1
426// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000427// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000428//
429static MachineOpCode
430ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000431 bool& mustClearReg,
432 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433{
Misha Brukmana98cd452003-05-20 20:32:24 +0000434 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000435 mustClearReg = true;
436 valueToMove = 1;
437
438 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000439 {
440 case Instruction::SetEQ: opCode = V9::MOVE; break;
441 case Instruction::SetLE: opCode = V9::MOVLE; break;
442 case Instruction::SetGE: opCode = V9::MOVGE; break;
443 case Instruction::SetLT: opCode = V9::MOVL; break;
444 case Instruction::SetGT: opCode = V9::MOVG; break;
445 case Instruction::SetNE: assert(0 && "No move required!"); break;
446 default: assert(0 && "Unrecognized VM instr!"); break;
447 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000448
449 return opCode;
450}
451
Chris Lattner20b1ea02001-09-14 03:47:57 +0000452static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000453ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000454{
Misha Brukmana98cd452003-05-20 20:32:24 +0000455 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000456
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000457 switch(vopCode)
Misha Brukman81b06862003-05-21 18:48:06 +0000458 {
459 case ToFloatTy:
460 if (opType == Type::SByteTy || opType == Type::ShortTy ||
461 opType == Type::IntTy)
462 opCode = V9::FITOS;
463 else if (opType == Type::LongTy)
464 opCode = V9::FXTOS;
465 else if (opType == Type::DoubleTy)
466 opCode = V9::FDTOS;
467 else if (opType == Type::FloatTy)
468 ;
469 else
470 assert(0 && "Cannot convert this type to FLOAT on SPARC");
471 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000472
Misha Brukman81b06862003-05-21 18:48:06 +0000473 case ToDoubleTy:
474 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
475 // Both functions should treat the integer as a 32-bit value for types
476 // of 4 bytes or less, and as a 64-bit value otherwise.
477 if (opType == Type::SByteTy || opType == Type::UByteTy ||
478 opType == Type::ShortTy || opType == Type::UShortTy ||
479 opType == Type::IntTy || opType == Type::UIntTy)
480 opCode = V9::FITOD;
481 else if (opType == Type::LongTy || opType == Type::ULongTy)
482 opCode = V9::FXTOD;
483 else if (opType == Type::FloatTy)
484 opCode = V9::FSTOD;
485 else if (opType == Type::DoubleTy)
486 ;
487 else
488 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
489 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000490
Misha Brukman81b06862003-05-21 18:48:06 +0000491 default:
492 break;
493 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000494
495 return opCode;
496}
497
498static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000499ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000500{
Misha Brukmana98cd452003-05-20 20:32:24 +0000501 MachineOpCode opCode = V9::INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000502
503 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
504 && "This function should only be called for FLOAT or DOUBLE");
505
Misha Brukman81b06862003-05-21 18:48:06 +0000506 if (tid == Type::UIntTyID) {
507 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
508 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
509 } else if (tid == Type::SByteTyID || tid == Type::ShortTyID ||
510 tid == Type::IntTyID || tid == Type::UByteTyID ||
511 tid == Type::UShortTyID) {
512 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
513 } else if (tid == Type::LongTyID || tid == Type::ULongTyID) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000514 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
Misha Brukman81b06862003-05-21 18:48:06 +0000515 } else
516 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000517
Chris Lattner20b1ea02001-09-14 03:47:57 +0000518 return opCode;
519}
520
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000521MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000522CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
523 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000524{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000525 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000526 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000527 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000528}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000529
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000530// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000531// The FP value must be converted to the dest type in an FP register,
532// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000533//
534// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
535// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000536// *only* when converting to an unsigned. (Unsigned byte, short or long
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000537// don't have this problem.)
538// For unsigned int, we therefore have to generate the code sequence:
539//
540// if (V > (float) MAXINT) {
541// unsigned result = (unsigned) (V - (float) MAXINT);
542// result = result + (unsigned) MAXINT;
543// }
544// else
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000545// result = (unsigned) V;
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000546//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000547static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000548CreateCodeToConvertFloatToInt(const TargetMachine& target,
549 Value* opVal,
550 Instruction* destI,
551 std::vector<MachineInstr*>& mvec,
552 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000553{
554 // Create a temporary to represent the FP register into which the
555 // int value will placed after conversion. The type of this temporary
556 // depends on the type of FP register to use: single-prec for a 32-bit
557 // int or smaller; double-prec for a 64-bit int.
558 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000559 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000560 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
561 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000562 mcfi.addTemp(destForCast);
563
564 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000565 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
566 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000567 mvec.push_back(M);
568
569 // Create the fpreg-to-intreg copy code
570 target.getInstrInfo().
571 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000572 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000573}
574
575
Chris Lattner20b1ea02001-09-14 03:47:57 +0000576static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000577ChooseAddInstruction(const InstructionNode* instrNode)
578{
579 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
580}
581
582
Chris Lattner20b1ea02001-09-14 03:47:57 +0000583static inline MachineInstr*
584CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000585 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000586{
Misha Brukmana98cd452003-05-20 20:32:24 +0000587 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000588 .addReg(instrNode->leftChild()->getValue())
589 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000590}
591
592static inline MachineInstr*
593CreateAddConstInstruction(const InstructionNode* instrNode)
594{
595 MachineInstr* minstr = NULL;
596
597 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000598 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000599
600 // Cases worth optimizing are:
601 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
602 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
603 //
Chris Lattner9b625032002-05-06 16:15:30 +0000604 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000605 double dval = FPC->getValue();
606 if (dval == 0.0)
607 minstr = CreateMovFloatInstruction(instrNode,
608 instrNode->getInstruction()->getType());
609 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000610
611 return minstr;
612}
613
614
615static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000616ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000617{
Misha Brukmana98cd452003-05-20 20:32:24 +0000618 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000619
Misha Brukman81b06862003-05-21 18:48:06 +0000620 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukman91aee472003-05-27 22:37:00 +0000621 opCode = V9::SUBr;
Misha Brukman81b06862003-05-21 18:48:06 +0000622 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000623 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000624 {
625 case Type::FloatTyID: opCode = V9::FSUBS; break;
626 case Type::DoubleTyID: opCode = V9::FSUBD; break;
627 default: assert(0 && "Invalid type for SUB instruction"); break;
628 }
629 }
630
Chris Lattner20b1ea02001-09-14 03:47:57 +0000631 return opCode;
632}
633
634
635static inline MachineInstr*
636CreateSubConstInstruction(const InstructionNode* instrNode)
637{
638 MachineInstr* minstr = NULL;
639
640 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000641 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000642
643 // Cases worth optimizing are:
644 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
645 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
646 //
Chris Lattner9b625032002-05-06 16:15:30 +0000647 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
648 double dval = FPC->getValue();
649 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000650 minstr = CreateMovFloatInstruction(instrNode,
651 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000652 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000653
654 return minstr;
655}
656
657
658static inline MachineOpCode
659ChooseFcmpInstruction(const InstructionNode* instrNode)
660{
Misha Brukmana98cd452003-05-20 20:32:24 +0000661 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000662
663 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
664 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000665 case Type::FloatTyID: opCode = V9::FCMPS; break;
666 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000667 default: assert(0 && "Invalid type for FCMP instruction"); break;
668 }
669
670 return opCode;
671}
672
673
674// Assumes that leftArg and rightArg are both cast instructions.
675//
676static inline bool
677BothFloatToDouble(const InstructionNode* instrNode)
678{
679 InstrTreeNode* leftArg = instrNode->leftChild();
680 InstrTreeNode* rightArg = instrNode->rightChild();
681 InstrTreeNode* leftArgArg = leftArg->leftChild();
682 InstrTreeNode* rightArgArg = rightArg->leftChild();
683 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
684
685 // Check if both arguments are floats cast to double
686 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000687 leftArgArg->getValue()->getType() == Type::FloatTy &&
688 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000689}
690
691
692static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000693ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000694{
Misha Brukmana98cd452003-05-20 20:32:24 +0000695 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000696
Chris Lattner0c4e8862002-09-03 01:08:28 +0000697 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000698 opCode = V9::MULXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000699 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000700 switch(resultType->getPrimitiveID())
701 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000702 case Type::FloatTyID: opCode = V9::FMULS; break;
703 case Type::DoubleTyID: opCode = V9::FMULD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000704 default: assert(0 && "Invalid type for MUL instruction"); break;
705 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000706
707 return opCode;
708}
709
710
Vikram S. Adve510eec72001-11-04 21:59:14 +0000711
Chris Lattner20b1ea02001-09-14 03:47:57 +0000712static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000713CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000714 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000715{
Misha Brukman91aee472003-05-27 22:37:00 +0000716 return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000717 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000718}
719
720
Vikram S. Adve242a8082002-05-19 15:25:51 +0000721// Create instruction sequence for any shift operation.
722// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
723// requires a second instruction for explicit sign-extension.
724// Note that we only have to worry about a sign-bit appearing in the
725// most significant bit of the operand after shifting (e.g., bit 32 of
726// Int or bit 16 of Short), so we do not have to worry about results
727// that are as large as a normal integer register.
728//
729static inline void
730CreateShiftInstructions(const TargetMachine& target,
731 Function* F,
732 MachineOpCode shiftOpCode,
733 Value* argVal1,
734 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000735 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000736 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000737 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000738 MachineCodeForInstruction& mcfi)
739{
740 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
741 "Large shift sizes unexpected, but can be handled below: "
742 "You need to check whether or not it fits in immed field below");
743
744 // If this is a logical left shift of a type smaller than the standard
745 // integer reg. size, we have to extend the sign-bit into upper bits
746 // of dest, so we need to put the result of the SLL into a temporary.
747 //
748 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000749 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Misha Brukman91aee472003-05-27 22:37:00 +0000750 if ((shiftOpCode == V9::SLLr6 || shiftOpCode == V9::SLLXr6) && opSize < 8)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000751 { // put SLL result into a temporary
752 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
753 mcfi.addTemp(shiftDest);
754 }
755
756 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000757 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
758 .addReg(shiftDest, MOTy::Def)
759 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
760 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000761 mvec.push_back(M);
762
763 if (shiftDest != destVal)
764 { // extend the sign-bit of the result into all upper bits of dest
765 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
766 target.getInstrInfo().
Vikram S. Adve94c40812002-09-27 14:33:08 +0000767 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
768 8*opSize, mvec, mcfi);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000769 }
770}
771
772
Vikram S. Adve74825322002-03-18 03:15:35 +0000773// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000774// create a cheaper instruction.
775// This returns the approximate cost of the instructions generated,
776// which is used to pick the cheapest when both operands are constant.
Vikram S. Adve645fea32003-05-25 21:59:47 +0000777static unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000778CreateMulConstInstruction(const TargetMachine &target, Function* F,
779 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000780 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000781 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000782{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000783 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukman91aee472003-05-27 22:37:00 +0000784 unsigned cost = target.getInstrInfo().minLatency(V9::MULXr);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000785 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000786
787 Value* constOp = rval;
788 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000789 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000790
791 // Cases worth optimizing are:
792 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
793 // (2) Multiply by 2^x for integer types: replace with Shift
794 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000795 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000796
Misha Brukmana98cd452003-05-20 20:32:24 +0000797 if (resultType->isInteger() || isa<PointerType>(resultType)) {
798 bool isValidConst;
799 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
800 if (isValidConst) {
801 unsigned pow;
802 bool needNeg = false;
803 if (C < 0) {
804 needNeg = true;
805 C = -C;
806 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000807
Misha Brukmana98cd452003-05-20 20:32:24 +0000808 if (C == 0 || C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000809 cost = target.getInstrInfo().minLatency(V9::ADDr);
Misha Brukmana98cd452003-05-20 20:32:24 +0000810 unsigned Zero = target.getRegInfo().getZeroRegNum();
811 MachineInstr* M;
812 if (C == 0)
Misha Brukman91aee472003-05-27 22:37:00 +0000813 M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000814 else
Misha Brukman91aee472003-05-27 22:37:00 +0000815 M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000816 mvec.push_back(M);
817 }
818 else if (isPowerOf2(C, pow)) {
819 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Misha Brukman91aee472003-05-27 22:37:00 +0000820 MachineOpCode opCode = (opSize <= 32)? V9::SLLr6 : V9::SLLXr6;
Misha Brukmana98cd452003-05-20 20:32:24 +0000821 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
822 destVal, mvec, mcfi);
823 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000824
Misha Brukmana98cd452003-05-20 20:32:24 +0000825 if (mvec.size() > 0 && needNeg)
826 { // insert <reg = SUB 0, reg> after the instr to flip the sign
827 MachineInstr* M = CreateIntNegInstruction(target, destVal);
828 mvec.push_back(M);
829 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000830 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000831 } else {
832 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
833 double dval = FPC->getValue();
834 if (fabs(dval) == 1) {
835 MachineOpCode opCode = (dval < 0)
836 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
837 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
838 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
839 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000840 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000841 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000842
Misha Brukmana98cd452003-05-20 20:32:24 +0000843 if (firstNewInstr < mvec.size()) {
844 cost = 0;
845 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
846 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
847 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000848
849 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000850}
851
852
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000853// Does not create any instructions if we cannot exploit constant to
854// create a cheaper instruction.
855//
856static inline void
857CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000858 Function* F,
859 Value* lval, Value* rval,
860 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000861 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000862 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000863{
864 Value* constOp;
865 if (isa<Constant>(lval) && isa<Constant>(rval))
Vikram S. Adved3e26482002-10-13 00:18:57 +0000866 { // both operands are constant: evaluate and "set" in dest
867 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
868 cast<Constant>(lval), cast<Constant>(rval));
869 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000870 }
871 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000872 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000873 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000874 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000875
876 // else neither is constant
877 return;
878}
879
Vikram S. Adve74825322002-03-18 03:15:35 +0000880// Return NULL if we cannot exploit constant to create a cheaper instruction
881static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000882CreateMulInstruction(const TargetMachine &target, Function* F,
883 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000884 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000885 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000886 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
887{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000888 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000889 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000890 if (mvec.size() == L) {
891 // no instructions were added so create MUL reg, reg, reg.
892 // Use FSMULD if both operands are actually floats cast to doubles.
893 // Otherwise, use the default opcode for the appropriate type.
894 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
895 ? forceMulOp
896 : ChooseMulInstructionByType(destVal->getType()));
897 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
898 .addRegDef(destVal));
899 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000900}
901
902
Vikram S. Adve510eec72001-11-04 21:59:14 +0000903// Generate a divide instruction for Div or Rem.
904// For Rem, this assumes that the operand type will be signed if the result
905// type is signed. This is correct because they must have the same sign.
906//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000907static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000908ChooseDivInstruction(TargetMachine &target,
909 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000910{
Misha Brukmana98cd452003-05-20 20:32:24 +0000911 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000912
913 const Type* resultType = instrNode->getInstruction()->getType();
914
Chris Lattner0c4e8862002-09-03 01:08:28 +0000915 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000916 opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000917 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000918 switch(resultType->getPrimitiveID())
919 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000920 case Type::FloatTyID: opCode = V9::FDIVS; break;
921 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000922 default: assert(0 && "Invalid type for DIV instruction"); break;
923 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000924
925 return opCode;
926}
927
928
Chris Lattner54e898e2003-01-15 19:23:34 +0000929// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve645fea32003-05-25 21:59:47 +0000930static void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000931CreateDivConstInstruction(TargetMachine &target,
932 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000933 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000934{
Chris Lattner54e898e2003-01-15 19:23:34 +0000935 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000936 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000937 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000938 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000939
Vikram S. Adve645fea32003-05-25 21:59:47 +0000940 Instruction* destVal = instrNode->getInstruction();
Chris Lattner54e898e2003-01-15 19:23:34 +0000941 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000942
943 // Cases worth optimizing are:
944 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
945 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
946 //
947 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000948
Chris Lattner0c4e8862002-09-03 01:08:28 +0000949 if (resultType->isInteger())
Misha Brukmana98cd452003-05-20 20:32:24 +0000950 {
951 unsigned pow;
952 bool isValidConst;
953 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
954 if (isValidConst) {
955 bool needNeg = false;
956 if (C < 0) {
957 needNeg = true;
958 C = -C;
959 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000960
Misha Brukmana98cd452003-05-20 20:32:24 +0000961 if (C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000962 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000963 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +0000964 } else if (isPowerOf2(C, pow)) {
Vikram S. Adve645fea32003-05-25 21:59:47 +0000965 unsigned opCode;
966 Value* shiftOperand;
967
968 if (resultType->isSigned()) {
969 // The result may be negative and we need to add one before shifting
970 // a negative value. Use:
971 // srl i0, 31, x0; add x0, i0, i1 (if i0 is <= 32 bits)
972 // or
973 // srlx i0, 63, x0; add x0, i0, i1 (if i0 is 64 bits)
974 // to compute i1=i0+1 if i0 < 0 and i1=i0 otherwise.
975 //
976 TmpInstruction *srlTmp, *addTmp;
977 MachineCodeForInstruction& mcfi
978 = MachineCodeForInstruction::get(destVal);
979 srlTmp = new TmpInstruction(resultType, LHS, 0, "getSign");
980 addTmp = new TmpInstruction(resultType, LHS, srlTmp, "incIfNeg");
981 mcfi.addTemp(srlTmp);
982 mcfi.addTemp(addTmp);
983
984 // Create the SRL or SRLX instruction to get the sign bit
Misha Brukman91aee472003-05-27 22:37:00 +0000985 mvec.push_back(BuildMI((resultType==Type::LongTy) ?
986 V9::SRLXi6 : V9::SRLi6, 3)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000987 .addReg(LHS)
988 .addSImm((resultType==Type::LongTy)? 63 : 31)
989 .addRegDef(srlTmp));
990
991 // Create the ADD instruction to add 1 for negative values
Misha Brukman91aee472003-05-27 22:37:00 +0000992 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000993 .addRegDef(addTmp));
994
995 // Get the shift operand and "right-shift" opcode to do the divide
996 shiftOperand = addTmp;
Misha Brukman91aee472003-05-27 22:37:00 +0000997 opCode = (resultType==Type::LongTy) ? V9::SRAXi6 : V9::SRAi6;
Vikram S. Adve645fea32003-05-25 21:59:47 +0000998 }
999 else {
1000 // Get the shift operand and "right-shift" opcode to do the divide
1001 shiftOperand = LHS;
Misha Brukman91aee472003-05-27 22:37:00 +00001002 opCode = (resultType==Type::LongTy) ? V9::SRLXi6 : V9::SRLi6;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001003 }
1004
1005 // Now do the actual shift!
1006 mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow)
1007 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001008 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001009
Misha Brukmana98cd452003-05-20 20:32:24 +00001010 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
1011 // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve645fea32003-05-25 21:59:47 +00001012 mvec.push_back(CreateIntNegInstruction(target, destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001013 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001014 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001015 } else {
1016 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
1017 double dval = FPC->getValue();
1018 if (fabs(dval) == 1) {
1019 unsigned opCode =
1020 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
1021 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001022
Vikram S. Adve645fea32003-05-25 21:59:47 +00001023 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001024 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001025 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001026 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001027}
1028
1029
Vikram S. Adve74825322002-03-18 03:15:35 +00001030static void
1031CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1032 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001033 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001034 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001035 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001036{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001037 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001038 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001039 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001040 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001041
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001042 // Enforce the alignment constraints on the stack pointer at
1043 // compile time if the total size is a known constant.
1044 if (isa<Constant>(numElementsVal))
1045 {
1046 bool isValid;
1047 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
1048 assert(isValid && "Unexpectedly large array dimension in alloca!");
1049 int64_t total = numElem * tsize;
1050 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1051 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1052 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1053 }
1054 else
1055 {
1056 // The size is not a constant. Generate code to compute it and
1057 // code to pad the size for stack alignment.
1058 // Create a Value to hold the (constant) element size
1059 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
1060
1061 // Create temporary values to hold the result of MUL, SLL, SRL
1062 // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY.
1063 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
1064 TmpInstruction* tmpSLL = new TmpInstruction(numElementsVal, tmpProd);
1065 TmpInstruction* tmpSRL = new TmpInstruction(numElementsVal, tmpSLL);
1066 mcfi.addTemp(tmpProd);
1067 mcfi.addTemp(tmpSLL);
1068 mcfi.addTemp(tmpSRL);
1069
1070 // Instruction 1: mul numElements, typeSize -> tmpProd
1071 // This will optimize the MUL as far as possible.
1072 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec,
1073 mcfi, INVALID_MACHINE_OPCODE);
1074
1075 assert(0 && "Need to insert padding instructions here!");
1076
1077 totalSizeVal = tmpProd;
1078 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001079
1080 // Get the constant offset from SP for dynamically allocated storage
1081 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001082 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001083 bool growUp;
1084 ConstantSInt* dynamicAreaOffset =
1085 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001086 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001087 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1088
Chris Lattner54e898e2003-01-15 19:23:34 +00001089 unsigned SPReg = target.getRegInfo().getStackPointer();
1090
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001091 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukman91aee472003-05-27 22:37:00 +00001092 getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001093 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001094
Vikram S. Adve74825322002-03-18 03:15:35 +00001095 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukman91aee472003-05-27 22:37:00 +00001096 getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset)
Misha Brukmana98cd452003-05-20 20:32:24 +00001097 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001098}
1099
1100
1101static void
1102CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1103 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001104 unsigned tsize,
1105 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001106 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001107{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001108 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001109 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001110 "Result value is not part of a function?");
1111 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001112 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001113
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001114 // Check if the offset would small enough to use as an immediate in
1115 // load/stores (check LDX because all load/stores have the same-size immediate
1116 // field). If not, put the variable in the dynamically sized area of the
1117 // frame.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001118 unsigned paddedSizeIgnored;
1119 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001120 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +00001121 tsize * numElements);
Misha Brukman91aee472003-05-27 22:37:00 +00001122 if (! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001123 CreateCodeForVariableSizeAlloca(target, result, tsize,
1124 ConstantSInt::get(Type::IntTy,numElements),
1125 getMvec);
1126 return;
1127 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001128
1129 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001130 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001131
1132 // Create a temporary Value to hold the constant offset.
1133 // This is needed because it may not fit in the immediate field.
1134 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1135
1136 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001137 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukman91aee472003-05-27 22:37:00 +00001138 getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001139 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001140}
1141
1142
Misha Brukman91aee472003-05-27 22:37:00 +00001143static unsigned
1144convertOpcodeFromRegToImm(unsigned Opcode) {
1145 switch (Opcode) {
1146 case V9::ADDr: return V9::ADDi;
1147
1148 /* load opcodes */
1149 case V9::LDUBr: return V9::LDUBi;
1150 case V9::LDSBr: return V9::LDSBi;
1151 case V9::LDUHr: return V9::LDUHi;
1152 case V9::LDSHr: return V9::LDSHi;
1153 case V9::LDUWr: return V9::LDUWi;
1154 case V9::LDSWr: return V9::LDSWi;
1155 case V9::LDXr: return V9::LDXi;
1156 case V9::LDFr: return V9::LDFi;
1157 case V9::LDDFr: return V9::LDDFi;
1158
1159 /* store opcodes */
1160 case V9::STBr: return V9::STBi;
1161 case V9::STHr: return V9::STHi;
1162 case V9::STWr: return V9::STWi;
1163 case V9::STXr: return V9::STXi;
1164 case V9::STFr: return V9::STFi;
1165 case V9::STDFr: return V9::STDFi;
1166
1167 default:
1168 std::cerr << "Not handled opcode in convert from reg to imm: " << Opcode
1169 << "\n";
1170 abort();
1171 return 0;
1172 }
1173}
1174
1175
Chris Lattner20b1ea02001-09-14 03:47:57 +00001176//------------------------------------------------------------------------
1177// Function SetOperandsForMemInstr
1178//
1179// Choose addressing mode for the given load or store instruction.
1180// Use [reg+reg] if it is an indexed reference, and the index offset is
1181// not a constant or if it cannot fit in the offset field.
1182// Use [reg+offset] in all other cases.
1183//
1184// This assumes that all array refs are "lowered" to one of these forms:
1185// %x = load (subarray*) ptr, constant ; single constant offset
1186// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1187// Generally, this should happen via strength reduction + LICM.
1188// Also, strength reduction should take care of using the same register for
1189// the loop index variable and an array index, when that is profitable.
1190//------------------------------------------------------------------------
1191
1192static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001193SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001194 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001195 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001196 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001197{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001198 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001199 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001200 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001201 bool allConstantIndices;
1202 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001203
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001204 // Now create the appropriate operands for the machine instruction.
1205 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001206 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001207 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001208 MachineOperand::MachineOperandType offsetOpType =
1209 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001210
Vikram S. Adve74825322002-03-18 03:15:35 +00001211 // Check if there is an index vector and if so, compute the
1212 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001213 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +00001214 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +00001215 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001216 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001217
Vikram S. Adve242a8082002-05-19 15:25:51 +00001218 // If all indices are constant, compute the combined offset directly.
1219 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001220 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001221 // Compute the offset value using the index vector. Create a
1222 // virtual reg. for it since it may not fit in the immed field.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001223 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001224 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001225 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001226 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001227 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001228 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001229 // be an array ref, and must have been lowered to a single non-zero
1230 // offset. (An extra leading zero offset, if any, can be ignored.)
1231 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001232 //
Chris Lattner795ba6c2003-01-15 21:36:50 +00001233 bool firstIdxIsZero = IsZero(idxVec[0]);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001234 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001235 && "Array refs must be lowered before Instruction Selection");
1236
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001237 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001238
Misha Brukmanee563cb2003-05-21 17:59:06 +00001239 std::vector<MachineInstr*> mulVec;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001240 Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001241 MachineCodeForInstruction::get(memInst).addTemp(addr);
1242
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001243 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001244 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001245 const Type* vecType = (firstIdxIsZero
1246 ? GetElementPtrInst::getIndexedType(ptrType,
1247 std::vector<Value*>(1U, idxVec[0]),
1248 /*AllowCompositeLeaf*/ true)
1249 : ptrType);
1250 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +00001251 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001252 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001253
1254 // CreateMulInstruction() folds constants intelligently enough.
Vikram S. Adved3e26482002-10-13 00:18:57 +00001255 CreateMulInstruction(target, memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001256 idxVal, /* lval, not likely to be const*/
1257 eltSizeVal, /* rval, likely to be constant */
1258 addr, /* result */
Vikram S. Adved3e26482002-10-13 00:18:57 +00001259 mulVec, MachineCodeForInstruction::get(memInst),
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001260 INVALID_MACHINE_OPCODE);
1261
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001262 assert(mulVec.size() > 0 && "No multiply code created?");
Chris Lattner54e898e2003-01-15 19:23:34 +00001263 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001264
1265 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001266 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001267 }
1268 else
1269 {
1270 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1271 smallConstOffset = 0;
1272 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001273
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001274 // For STORE:
1275 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1276 // For LOAD or GET_ELEMENT_PTR,
1277 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1278 //
1279 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001280 MachineInstr *MI;
1281 if (memInst->getOpcode() == Instruction::Store) {
1282 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1283 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1284 .addReg(ptrVal).addReg(valueForRegOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001285 else {
1286 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001287 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1288 .addReg(ptrVal).addSImm(smallConstOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001289 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001290 } else {
1291 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1292 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1293 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001294 else {
1295 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001296 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1297 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001298 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001299 }
1300 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001301}
1302
1303
Chris Lattner20b1ea02001-09-14 03:47:57 +00001304//
1305// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001306// in place of the use(s) of that instruction in node `parent'.
1307// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001308// Also make sure to skip over a parent who:
1309// (1) is a list node in the Burg tree, or
1310// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001311//
1312static void
1313ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001314 InstrTreeNode* parent,
1315 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001316{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001317 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1318
Chris Lattner20b1ea02001-09-14 03:47:57 +00001319 Instruction* unusedOp = treeNode->getInstruction();
1320 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001321
1322 // The parent itself may be a list node, so find the real parent instruction
1323 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1324 {
1325 parent = parent->parent();
1326 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1327 }
1328 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1329
1330 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001331 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001332
1333 // The parent's mvec would be empty if it was itself forwarded.
1334 // Recursively call ForwardOperand in that case...
1335 //
1336 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001337 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001338 assert(parent->parent() != NULL &&
1339 "Parent could not have been forwarded, yet has no instructions?");
1340 ForwardOperand(treeNode, parent->parent(), operandNum);
1341 }
1342 else
1343 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001344 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001345 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001346 MachineInstr* minstr = mvec[i];
1347 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001348 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001349 const MachineOperand& mop = minstr->getOperand(i);
Chris Lattner133f0792002-10-28 04:45:29 +00001350 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
Vikram S. Adve74825322002-03-18 03:15:35 +00001351 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001352 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001353 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001354 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001355
1356 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1357 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001358 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve78a4f232003-05-27 00:02:22 +00001359 minstr->getImplicitOp(i).opIsDefOnly(),
1360 minstr->getImplicitOp(i).opIsDefAndUse());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001361 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001362 }
1363}
1364
1365
Vikram S. Adve242a8082002-05-19 15:25:51 +00001366inline bool
1367AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001368{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001369 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1370 UI != UE; ++UI)
1371 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1372 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1373 return false;
1374 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001375}
1376
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001377// Generate code for any intrinsic that needs a special code sequence
1378// instead of a regular call. If not that kind of intrinsic, do nothing.
1379// Returns true if code was generated, otherwise false.
1380//
1381bool CodeGenIntrinsic(LLVMIntrinsic::ID iid, CallInst &callInstr,
1382 TargetMachine &target,
1383 std::vector<MachineInstr*>& mvec)
1384{
1385 switch (iid) {
1386 case LLVMIntrinsic::va_start: {
1387 // Get the address of the first vararg value on stack and copy it to
1388 // the argument of va_start(va_list* ap).
1389 bool ignore;
1390 Function* func = cast<Function>(callInstr.getParent()->getParent());
1391 int numFixedArgs = func->getFunctionType()->getNumParams();
1392 int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum();
1393 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
1394 int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo().
1395 getFirstIncomingArgOffset(MachineFunction::get(func), ignore);
Misha Brukman91aee472003-05-27 22:37:00 +00001396 mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001397 addReg(callInstr.getOperand(1)));
1398 return true;
1399 }
1400
1401 case LLVMIntrinsic::va_end:
1402 return true; // no-op on Sparc
1403
1404 case LLVMIntrinsic::va_copy:
1405 // Simple copy of current va_list (arg2) to new va_list (arg1)
Misha Brukman91aee472003-05-27 22:37:00 +00001406 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001407 addMReg(target.getRegInfo().getZeroRegNum()).
1408 addReg(callInstr.getOperand(2)).
1409 addReg(callInstr.getOperand(1)));
1410 return true;
1411
1412 default:
1413 return false;
1414 }
1415}
1416
Vikram S. Advefb361122001-10-22 13:36:31 +00001417//******************* Externally Visible Functions *************************/
1418
Vikram S. Advefb361122001-10-22 13:36:31 +00001419//------------------------------------------------------------------------
1420// External Function: ThisIsAChainRule
1421//
1422// Purpose:
1423// Check if a given BURG rule is a chain rule.
1424//------------------------------------------------------------------------
1425
1426extern bool
1427ThisIsAChainRule(int eruleno)
1428{
1429 switch(eruleno)
1430 {
1431 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001432 case 123:
1433 case 124:
1434 case 125:
1435 case 126:
1436 case 127:
1437 case 128:
1438 case 129:
1439 case 130:
1440 case 131:
1441 case 132:
1442 case 133:
1443 case 155:
1444 case 221:
1445 case 222:
1446 case 241:
1447 case 242:
1448 case 243:
1449 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001450 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001451 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001452 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001453
Vikram S. Advefb361122001-10-22 13:36:31 +00001454 default:
1455 return false; break;
1456 }
1457}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001458
1459
1460//------------------------------------------------------------------------
1461// External Function: GetInstructionsByRule
1462//
1463// Purpose:
1464// Choose machine instructions for the SPARC according to the
1465// patterns chosen by the BURG-generated parser.
1466//------------------------------------------------------------------------
1467
Vikram S. Adve74825322002-03-18 03:15:35 +00001468void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001469GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001470 int ruleForNode,
1471 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001472 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001473 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001474{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001475 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001476 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001477 int nextRule;
1478 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001479 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001480 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001481 unsigned L;
Vikram S. Adve74825322002-03-18 03:15:35 +00001482
1483 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001484
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001485 // If the code for this instruction was folded into the parent (user),
1486 // then do nothing!
1487 if (subtreeRoot->isFoldedIntoParent())
1488 return;
1489
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001490 //
1491 // Let's check for chain rules outside the switch so that we don't have
1492 // to duplicate the list of chain rule production numbers here again
1493 //
1494 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001495 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001496 // Chain rules have a single nonterminal on the RHS.
1497 // Get the rule that matches the RHS non-terminal and use that instead.
1498 //
1499 assert(nts[0] && ! nts[1]
1500 && "A chain rule should have only one RHS non-terminal!");
1501 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1502 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001503 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001504 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001505 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001506 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001507 switch(ruleForNode) {
1508 case 1: // stmt: Ret
1509 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001510 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001511 // for moving return value to appropriate register.
1512 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001513 // Mark the return value register as an implicit ref of
1514 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001515 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001516 ReturnInst *returnInstr =
1517 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001518 assert(returnInstr->getOpcode() == Instruction::Ret);
1519
Chris Lattner9c461082002-02-03 07:50:56 +00001520 Instruction* returnReg = new TmpInstruction(returnInstr);
1521 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001522
Misha Brukman91aee472003-05-27 22:37:00 +00001523 M = BuildMI(V9::JMPLRETi, 3).addReg(returnReg).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001524 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001525
Vikram S. Advea995e602001-10-11 04:23:19 +00001526 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001527 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001528
Vikram S. Adve74825322002-03-18 03:15:35 +00001529 mvec.push_back(M);
Misha Brukmana98cd452003-05-20 20:32:24 +00001530 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001531
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001532 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001533 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001534
1535 case 3: // stmt: Store(reg,reg)
1536 case 4: // stmt: Store(reg,ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00001537 SetOperandsForMemInstr(ChooseStoreInstruction(
1538 subtreeRoot->leftChild()->getValue()->getType()),
1539 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001540 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001541
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001542 case 5: // stmt: BrUncond
Chris Lattner54e898e2003-01-15 19:23:34 +00001543 {
1544 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001545 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001546
Chris Lattner54e898e2003-01-15 19:23:34 +00001547 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001548 mvec.push_back(BuildMI(V9::NOP, 0));
Chris Lattner54e898e2003-01-15 19:23:34 +00001549 break;
1550 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001551
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001552 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001553 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001554 // If the constant is ZERO, we can use the branch-on-integer-register
1555 // instructions and avoid the SUBcc instruction entirely.
1556 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001557 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001558 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1559 assert(constNode &&
1560 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001561 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001562 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001563
Chris Lattner0c4e8862002-09-03 01:08:28 +00001564 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001565 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001566 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1567 && isValidConst)
1568 {
1569 // That constant is a zero after all...
1570 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001571 // Mark the setCC node so that no code is generated for it.
1572 InstructionNode* setCCNode = (InstructionNode*)
1573 subtreeRoot->leftChild();
1574 assert(setCCNode->getOpLabel() == SetCCOp);
1575 setCCNode->markFoldedIntoParent();
1576
1577 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1578
Chris Lattner54e898e2003-01-15 19:23:34 +00001579 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1580 .addReg(setCCNode->leftChild()->getValue())
1581 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001582 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001583
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001584 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001585 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001586
1587 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001588 mvec.push_back(BuildMI(V9::BA, 1)
1589 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001590
1591 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001592 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001593 break;
1594 }
1595 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001596 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001597
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001598 case 6: // stmt: BrCond(setCC)
1599 { // bool => boolean was computed with SetCC.
1600 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001601 // If it is an integer CC, we also need to find the unique
1602 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001603 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001604 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001605 bool isFPBranch;
Chris Lattner54e898e2003-01-15 19:23:34 +00001606 unsigned Opcode = ChooseBccInstruction(subtreeRoot, isFPBranch);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001607 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1608 brInst->getParent()->getParent(),
1609 isFPBranch? Type::FloatTy : Type::IntTy);
Chris Lattner54e898e2003-01-15 19:23:34 +00001610 M = BuildMI(Opcode, 2).addCCReg(ccValue)
1611 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001612 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001613
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001614 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001615 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001616
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001617 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001618 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001619
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001620 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001621 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001622 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001623 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001624
1625 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001626 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001627 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001628 Constant* constVal =
1629 cast<Constant>(subtreeRoot->leftChild()->getValue());
1630 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001631
Misha Brukmana98cd452003-05-20 20:32:24 +00001632 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001633 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001634 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001635
1636 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001637 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001638 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001639 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001640
1641 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001642 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001643 // Just use the branch-on-integer-register instruction!
1644 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001645 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001646 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001647 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001648 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001649
1650 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001651 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001652
1653 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001654 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001655
1656 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001657 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001658 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001659 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001660
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001661 case 9: // stmt: Switch(reg)
1662 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001663 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001664
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001665 case 10: // reg: VRegList(reg, reg)
1666 assert(0 && "VRegList should never be the topmost non-chain rule");
1667 break;
1668
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001669 case 21: // bool: Not(bool,reg): Both these are implemented as:
1670 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1671 { // First find the unary operand. It may be left or right, usually right.
1672 Value* notArg = BinaryOperator::getNotArgument(
1673 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001674 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001675 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Chris Lattner00dca912003-01-15 17:47:49 +00001676 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001678 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001679
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001680 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001681 {
1682 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001683 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001684 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001685 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001686 }
1687
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001688 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001689 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001690 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001691 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001692 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001693 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001694 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001695 //======================================================================
1696 // Rules for integer conversions:
1697 //
1698 //--------
1699 // From ISO 1998 C++ Standard, Sec. 4.7:
1700 //
1701 // 2. If the destination type is unsigned, the resulting value is
1702 // the least unsigned integer congruent to the source integer
1703 // (modulo 2n where n is the number of bits used to represent the
1704 // unsigned type). [Note: In a two s complement representation,
1705 // this conversion is conceptual and there is no change in the
1706 // bit pattern (if there is no truncation). ]
1707 //
1708 // 3. If the destination type is signed, the value is unchanged if
1709 // it can be represented in the destination type (and bitfield width);
1710 // otherwise, the value is implementation-defined.
1711 //--------
1712 //
1713 // Since we assume 2s complement representations, this implies:
1714 //
1715 // -- if operand is smaller than destination, zero-extend or sign-extend
1716 // according to the signedness of the *operand*: source decides.
1717 // ==> we have to do nothing here!
1718 //
1719 // -- if operand is same size as or larger than destination, and the
1720 // destination is *unsigned*, zero-extend the operand: dest. decides
1721 //
1722 // -- if operand is same size as or larger than destination, and the
1723 // destination is *signed*, the choice is implementation defined:
1724 // we sign-extend the operand: i.e., again dest. decides.
1725 // Note: this matches both Sun's cc and gcc3.2.
1726 //======================================================================
1727
Vikram S. Adve242a8082002-05-19 15:25:51 +00001728 Instruction* destI = subtreeRoot->getInstruction();
1729 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001730 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001731 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001732 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001733 unsigned opSize = target.getTargetData().getTypeSize(opType);
1734 unsigned destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001735 if (opSize >= destSize)
1736 { // Operand is same size as or larger than dest:
1737 // zero- or sign-extend, according to the signeddness of
1738 // the destination (see above).
1739 if (destI->getType()->isSigned())
1740 target.getInstrInfo().CreateSignExtensionInstructions(target,
1741 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1742 mvec, MachineCodeForInstruction::get(destI));
1743 else
1744 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1745 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1746 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001747 }
1748 else
1749 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001750 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001751 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001752 {
1753 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1754 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001755 if (destI->getType()->isUnsigned())
1756 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001757 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001758 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001759 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1760
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001761 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001762 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001763
1764 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001765 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001766 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001767 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001768 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001769 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001770 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001771 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001772 {
1773 Instruction* destI = subtreeRoot->getInstruction();
1774 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1775 MachineCodeForInstruction::get(destI));
1776 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001777 else
1778 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001779 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001780 }
1781
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001782 case 31: // reg: ToFloatTy(reg):
1783 case 32: // reg: ToDoubleTy(reg):
1784 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001785
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001786 // If this instruction has a parent (a user) in the tree
1787 // and the user is translated as an FsMULd instruction,
1788 // then the cast is unnecessary. So check that first.
1789 // In the future, we'll want to do the same for the FdMULq instruction,
1790 // so do the check here instead of only for ToFloatTy(reg).
1791 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001792 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001793 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001794 const MachineCodeForInstruction& mcfi =
1795 MachineCodeForInstruction::get(
1796 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001797 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001798 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001799 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001800
1801 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001802 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001803 Value* leftVal = subtreeRoot->leftChild()->getValue();
1804 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001805 MachineOpCode opCode=ChooseConvertToFloatInstr(
1806 subtreeRoot->getOpLabel(), opType);
Misha Brukmana98cd452003-05-20 20:32:24 +00001807 if (opCode == V9::INVALID_OPCODE) // no conversion needed
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001808 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001809 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001810 }
1811 else
1812 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001813 // If the source operand is a non-FP type it must be
1814 // first copied from int to float register via memory!
1815 Instruction *dest = subtreeRoot->getInstruction();
1816 Value* srcForCast;
1817 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001818 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001819 {
1820 // Create a temporary to represent the FP register
1821 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001822 // The type of this temporary will determine the FP
1823 // register used: single-prec for a 32-bit int or smaller,
1824 // double-prec for a 64-bit int.
1825 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001826 uint64_t srcSize =
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001827 target.getTargetData().getTypeSize(leftVal->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001828 Type* tmpTypeToUse =
1829 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1830 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001831 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001832 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001833 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001834
Vikram S. Adve242a8082002-05-19 15:25:51 +00001835 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001836 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001837 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001838 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001839 }
1840 else
1841 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001842
Chris Lattner54e898e2003-01-15 19:23:34 +00001843 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
Vikram S. Adve74825322002-03-18 03:15:35 +00001844 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001845 }
1846 }
1847 break;
1848
1849 case 19: // reg: ToArrayTy(reg):
1850 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001851 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001852 break;
1853
1854 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001855 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001856 M = CreateAddConstInstruction(subtreeRoot);
1857 if (M != NULL)
1858 {
1859 mvec.push_back(M);
1860 break;
1861 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001862 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001863
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001864 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001865 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001866 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001867 break;
1868
1869 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001870 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001871 M = CreateSubConstInstruction(subtreeRoot);
1872 if (M != NULL)
1873 {
1874 mvec.push_back(M);
1875 break;
1876 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001877 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001878
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001879 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001880 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001881 Add3OperandInstr(ChooseSubInstructionByType(
1882 subtreeRoot->getInstruction()->getType()),
1883 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001884 break;
1885
1886 case 135: // reg: Mul(todouble, todouble)
1887 checkCast = true;
1888 // FALL THROUGH
1889
1890 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001891 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001892 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001893 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00001894 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00001895 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001896 Instruction* mulInstr = subtreeRoot->getInstruction();
1897 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001898 subtreeRoot->leftChild()->getValue(),
1899 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001900 mulInstr, mvec,
1901 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001902 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001903 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001904 case 335: // reg: Mul(todouble, todoubleConst)
1905 checkCast = true;
1906 // FALL THROUGH
1907
1908 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001909 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001910 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001911 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00001912 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00001913 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001914 Instruction* mulInstr = subtreeRoot->getInstruction();
1915 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001916 subtreeRoot->leftChild()->getValue(),
1917 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001918 mulInstr, mvec,
1919 MachineCodeForInstruction::get(mulInstr),
1920 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001921 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001922 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001923 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001924 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001925 L = mvec.size();
1926 CreateDivConstInstruction(target, subtreeRoot, mvec);
1927 if (mvec.size() > L)
1928 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001929 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001930
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001931 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001932 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001933 Add3OperandInstr(ChooseDivInstruction(target, subtreeRoot),
1934 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001935 break;
1936
1937 case 37: // reg: Rem(reg, reg)
1938 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001939 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001940 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001941 Instruction* remInstr = subtreeRoot->getInstruction();
1942
Chris Lattner9c461082002-02-03 07:50:56 +00001943 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001944 subtreeRoot->leftChild()->getValue(),
1945 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001946 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001947 quot,
1948 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001949 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001950
Chris Lattner54e898e2003-01-15 19:23:34 +00001951 M = BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
1952 .addReg(subtreeRoot->leftChild()->getValue())
1953 .addReg(subtreeRoot->rightChild()->getValue())
1954 .addRegDef(quot);
Vikram S. Adve74825322002-03-18 03:15:35 +00001955 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001956
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001957 unsigned MulOpcode =
1958 ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType());
1959 Value *MulRHS = subtreeRoot->rightChild()->getValue();
1960 M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod,
1961 MOTy::Def);
Vikram S. Adve74825322002-03-18 03:15:35 +00001962 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001963
Chris Lattner54e898e2003-01-15 19:23:34 +00001964 unsigned Opcode = ChooseSubInstructionByType(
1965 subtreeRoot->getInstruction()->getType());
1966 M = BuildMI(Opcode, 3).addReg(subtreeRoot->leftChild()->getValue())
1967 .addReg(prod).addRegDef(subtreeRoot->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001968 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001969 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001970 }
1971
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001972 case 38: // bool: And(bool, bool)
1973 case 238: // bool: And(bool, boolconst)
1974 case 338: // reg : BAnd(reg, reg)
1975 case 538: // reg : BAnd(reg, Constant)
Misha Brukman91aee472003-05-27 22:37:00 +00001976 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001977 break;
1978
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001979 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001980 case 438: // bool: BAnd(bool, bnot)
1981 { // Use the argument of NOT as the second argument!
1982 // Mark the NOT node so that no code is generated for it.
1983 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1984 Value* notArg = BinaryOperator::getNotArgument(
1985 cast<BinaryOperator>(notNode->getInstruction()));
1986 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001987 Value *LHS = subtreeRoot->leftChild()->getValue();
1988 Value *Dest = subtreeRoot->getValue();
Misha Brukman91aee472003-05-27 22:37:00 +00001989 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(LHS).addReg(notArg)
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001990 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001991 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001992 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001993
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001994 case 39: // bool: Or(bool, bool)
1995 case 239: // bool: Or(bool, boolconst)
1996 case 339: // reg : BOr(reg, reg)
1997 case 539: // reg : BOr(reg, Constant)
Misha Brukman91aee472003-05-27 22:37:00 +00001998 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001999 break;
2000
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002001 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002002 case 439: // bool: BOr(bool, bnot)
2003 { // Use the argument of NOT as the second argument!
2004 // Mark the NOT node so that no code is generated for it.
2005 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2006 Value* notArg = BinaryOperator::getNotArgument(
2007 cast<BinaryOperator>(notNode->getInstruction()));
2008 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002009 Value *LHS = subtreeRoot->leftChild()->getValue();
2010 Value *Dest = subtreeRoot->getValue();
Misha Brukman91aee472003-05-27 22:37:00 +00002011 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(LHS).addReg(notArg)
Misha Brukmana98cd452003-05-20 20:32:24 +00002012 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002013 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002014 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002015
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002016 case 40: // bool: Xor(bool, bool)
2017 case 240: // bool: Xor(bool, boolconst)
2018 case 340: // reg : BXor(reg, reg)
2019 case 540: // reg : BXor(reg, Constant)
Misha Brukman91aee472003-05-27 22:37:00 +00002020 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002021 break;
2022
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002023 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002024 case 440: // bool: BXor(bool, bnot)
2025 { // Use the argument of NOT as the second argument!
2026 // Mark the NOT node so that no code is generated for it.
2027 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2028 Value* notArg = BinaryOperator::getNotArgument(
2029 cast<BinaryOperator>(notNode->getInstruction()));
2030 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002031 Value *LHS = subtreeRoot->leftChild()->getValue();
2032 Value *Dest = subtreeRoot->getValue();
Misha Brukman91aee472003-05-27 22:37:00 +00002033 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(LHS).addReg(notArg)
Misha Brukmana98cd452003-05-20 20:32:24 +00002034 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002035 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002036 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002037
2038 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002039 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002040 // If the SetCC was folded into the user (parent), it will be
2041 // caught above. All other cases are the same as case 42,
2042 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002043 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002044 case 42: // bool: SetCC(reg, reg):
2045 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002046 // This generates a SUBCC instruction, putting the difference in
2047 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002048 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002049 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00002050 // than a branch instruction, or if it is used outside the current
2051 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002052 // computed and stored in the result register. Otherwise, discard
2053 // the difference (by using %g0) and keep only the condition code.
2054 //
2055 // To compute the boolean result in a register we use a conditional
2056 // move, unless the result of the SUBCC instruction can be used as
2057 // the bool! This assumes that zero is FALSE and any non-zero
2058 // integer is TRUE.
2059 //
2060 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
2061 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002062
Vikram S. Adve6418eac2002-07-08 23:30:14 +00002063 bool keepBoolVal = parentNode == NULL ||
2064 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002065 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002066 bool keepSubVal = keepBoolVal && subValIsBoolVal;
2067 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
2068
2069 bool mustClearReg;
2070 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00002071 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00002072
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002073 // Mark the 4th operand as being a CC register, and as a def
2074 // A TmpInstruction is created to represent the CC "result".
2075 // Unlike other instances of TmpInstruction, this one is used
2076 // by machine code of multiple LLVM instructions, viz.,
2077 // the SetCC and the branch. Make sure to get the same one!
2078 // Note that we do this even for FP CC registers even though they
2079 // are explicit operands, because the type of the operand
2080 // needs to be a floating point condition code, not an integer
2081 // condition code. Think of this as casting the bool result to
2082 // a FP condition code register.
2083 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002084 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00002085 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002086
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002087 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
2088 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00002089 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00002090 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002091
2092 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002093 {
2094 // Integer condition: dest. should be %g0 or an integer register.
2095 // If result must be saved but condition is not SetEQ then we need
2096 // a separate instruction to compute the bool result, so discard
2097 // result of SUBcc instruction anyway.
2098 //
Chris Lattner54e898e2003-01-15 19:23:34 +00002099 if (keepSubVal) {
Misha Brukman91aee472003-05-27 22:37:00 +00002100 M = BuildMI(V9::SUBccr, 4)
Misha Brukmana98cd452003-05-20 20:32:24 +00002101 .addReg(subtreeRoot->leftChild()->getValue())
2102 .addReg(subtreeRoot->rightChild()->getValue())
2103 .addRegDef(subtreeRoot->getValue())
2104 .addCCReg(tmpForCC, MOTy::Def);
Chris Lattner54e898e2003-01-15 19:23:34 +00002105 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00002106 M = BuildMI(V9::SUBccr, 4)
Misha Brukmana98cd452003-05-20 20:32:24 +00002107 .addReg(subtreeRoot->leftChild()->getValue())
2108 .addReg(subtreeRoot->rightChild()->getValue())
2109 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def)
2110 .addCCReg(tmpForCC, MOTy::Def);
Chris Lattner54e898e2003-01-15 19:23:34 +00002111 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002112 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002113
2114 if (computeBoolVal)
2115 { // recompute bool using the integer condition codes
2116 movOpCode =
2117 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
2118 }
2119 }
2120 else
2121 {
2122 // FP condition: dest of FCMP should be some FCCn register
Chris Lattner54e898e2003-01-15 19:23:34 +00002123 M = BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2124 .addCCReg(tmpForCC, MOTy::Def)
2125 .addReg(subtreeRoot->leftChild()->getValue())
2126 .addRegDef(subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00002127 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002128
2129 if (computeBoolVal)
2130 {// recompute bool using the FP condition codes
2131 mustClearReg = true;
2132 valueToMove = 1;
2133 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
2134 }
2135 }
2136
2137 if (computeBoolVal)
2138 {
2139 if (mustClearReg)
2140 {// Unconditionally set register to 0
Misha Brukmana98cd452003-05-20 20:32:24 +00002141 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00002142 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002143 }
2144
2145 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00002146 // Mark the register as a use (as well as a def) because the old
2147 // value should be retained if the condition is false.
Chris Lattner54e898e2003-01-15 19:23:34 +00002148 M = BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(valueToMove)
2149 .addReg(setCCInstr, MOTy::UseAndDef);
Vikram S. Adve74825322002-03-18 03:15:35 +00002150 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002151 }
2152 break;
2153 }
2154
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002155 case 51: // reg: Load(reg)
2156 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002157 SetOperandsForMemInstr(ChooseLoadInstruction(
2158 subtreeRoot->getValue()->getType()),
2159 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002160 break;
2161
2162 case 55: // reg: GetElemPtr(reg)
2163 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002164 // If the GetElemPtr was folded into the user (parent), it will be
2165 // caught above. For other cases, we have to compute the address.
Misha Brukman91aee472003-05-27 22:37:00 +00002166 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002167 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002168
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002169 case 57: // reg: Alloca: Implement as 1 instruction:
2170 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002171 AllocationInst* instr =
2172 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002173 unsigned tsize =
2174 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002175 assert(tsize != 0);
2176 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002177 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002178 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002179
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002180 case 58: // reg: Alloca(reg): Implement as 3 instructions:
2181 // mul num, typeSz -> tmp
2182 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002183 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002184 AllocationInst* instr =
2185 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002186 const Type* eltType = instr->getAllocatedType();
2187
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002188 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002189 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002190 Value* numElementsVal = NULL;
2191 bool isArray = instr->isArrayAllocation();
2192
2193 if (!isArray ||
2194 isa<Constant>(numElementsVal = instr->getArraySize()))
2195 { // total size is constant: generate code for fixed-size alloca
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002196 unsigned numElements = isArray?
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002197 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2198 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2199 numElements, mvec);
2200 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002201 else // total size is not constant.
2202 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002203 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002204 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002205 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002206
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002207 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002208 { // Generate a direct (CALL) or indirect (JMPL) call.
2209 // Mark the return-address register, the indirection
2210 // register (for indirect calls), the operands of the Call,
2211 // and the return value (if any) as implicit operands
2212 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002213 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002214 // If this is a varargs function, floating point arguments
2215 // have to passed in integer registers so insert
2216 // copy-float-to-int instructions for each float operand.
2217 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002218 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002219 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002220 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002221
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002222 // Check if this is an intrinsic function that needs a special code
2223 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002224 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002225 bool specialIntrinsic = false;
2226 LLVMIntrinsic::ID iid;
2227 if (calledFunc && (iid=(LLVMIntrinsic::ID)calledFunc->getIntrinsicID()))
2228 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002229
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002230 // If not, generate the normal call sequence for the function.
2231 // This can also handle any intrinsics that are just function calls.
2232 //
2233 if (! specialIntrinsic)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002234 {
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002235 // Create hidden virtual register for return address with type void*
2236 TmpInstruction* retAddrReg =
2237 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
2238 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
2239
2240 // Generate the machine instruction and its operands.
2241 // Use CALL for direct function calls; this optimistically assumes
2242 // the PC-relative address fits in the CALL address field (22 bits).
2243 // Use JMPL for indirect calls.
2244 //
2245 if (calledFunc) // direct function call
2246 M = BuildMI(V9::CALL, 1).addPCDisp(callee);
2247 else // indirect function call
Misha Brukman91aee472003-05-27 22:37:00 +00002248 M = BuildMI(V9::JMPLCALLi, 3).addReg(callee).addSImm((int64_t)0)
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002249 .addRegDef(retAddrReg);
2250 mvec.push_back(M);
2251
2252 const FunctionType* funcType =
2253 cast<FunctionType>(cast<PointerType>(callee->getType())
2254 ->getElementType());
2255 bool isVarArgs = funcType->isVarArg();
2256 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
2257
2258 // Use a descriptor to pass information about call arguments
2259 // to the register allocator. This descriptor will be "owned"
2260 // and freed automatically when the MachineCodeForInstruction
2261 // object for the callInstr goes away.
2262 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2263 retAddrReg, isVarArgs,noPrototype);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002264
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002265 assert(callInstr->getOperand(0) == callee
2266 && "This is assumed in the loop below!");
2267
2268 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002269 {
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002270 Value* argVal = callInstr->getOperand(i);
2271 Instruction* intArgReg = NULL;
2272
2273 // Check for FP arguments to varargs functions.
2274 // Any such argument in the first $K$ args must be passed in an
2275 // integer register, where K = #integer argument registers.
2276 if (isVarArgs && argVal->getType()->isFloatingPoint())
Vikram S. Adve242a8082002-05-19 15:25:51 +00002277 {
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002278 // If it is a function with no prototype, pass value
2279 // as an FP value as well as a varargs value
2280 if (noPrototype)
2281 argDesc->getArgInfo(i-1).setUseFPArgReg();
2282
2283 // If this arg. is in the first $K$ regs, add a copy
2284 // float-to-int instruction to pass the value as an integer.
2285 if (i <= target.getRegInfo().getNumOfIntArgRegs())
2286 {
2287 MachineCodeForInstruction &destMCFI =
2288 MachineCodeForInstruction::get(callInstr);
2289 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2290 destMCFI.addTemp(intArgReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002291
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002292 std::vector<MachineInstr*> copyMvec;
2293 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2294 callInstr->getParent()->getParent(),
2295 argVal, (TmpInstruction*) intArgReg,
2296 copyMvec, destMCFI);
2297 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
Vikram S. Adve242a8082002-05-19 15:25:51 +00002298
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002299 argDesc->getArgInfo(i-1).setUseIntArgReg();
2300 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2301 }
2302 else
2303 // Cannot fit in first $K$ regs so pass arg on stack
2304 argDesc->getArgInfo(i-1).setUseStackSlot();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002305 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002306
2307 if (intArgReg)
2308 mvec.back()->addImplicitRef(intArgReg);
2309
2310 mvec.back()->addImplicitRef(argVal);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002311 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002312
2313 // Add the return value as an implicit ref. The call operands
2314 // were added above.
2315 if (callInstr->getType() != Type::VoidTy)
2316 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
2317
2318 // For the CALL instruction, the ret. addr. reg. is also implicit
2319 if (isa<Function>(callee))
2320 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
2321
2322 // delay slot
2323 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve242a8082002-05-19 15:25:51 +00002324 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002325
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002326 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002327 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002328
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002329 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002330 {
2331 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2332 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2333 Instruction* shlInstr = subtreeRoot->getInstruction();
2334
2335 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002336 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2337 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002338
2339 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Misha Brukman91aee472003-05-27 22:37:00 +00002340 (opType == Type::LongTy)? V9::SLLXr6:V9::SLLr6,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002341 argVal1, argVal2, 0, shlInstr, mvec,
2342 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002343 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002344 }
2345
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002346 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002347 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002348 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2349 "Shr unsupported for other types");
Chris Lattner54e898e2003-01-15 19:23:34 +00002350 Add3OperandInstr(opType->isSigned()
Misha Brukman91aee472003-05-27 22:37:00 +00002351 ? (opType == Type::LongTy ? V9::SRAXr6 : V9::SRAr6)
2352 : (opType == Type::LongTy ? V9::SRLXr6 : V9::SRLr6),
Chris Lattner54e898e2003-01-15 19:23:34 +00002353 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002354 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002355 }
2356
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002357 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002358 break; // don't forward the value
2359
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002360 case 65: // reg: VaArg(reg)
2361 {
2362 // Use value initialized by va_start as pointer to args on the stack.
2363 // Load argument via current pointer value, then increment pointer.
2364 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
2365 Instruction* vaArgI = subtreeRoot->getInstruction();
Misha Brukman91aee472003-05-27 22:37:00 +00002366 mvec.push_back(BuildMI(V9::LDXi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002367 addSImm(0).addRegDef(vaArgI));
Misha Brukman91aee472003-05-27 22:37:00 +00002368 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002369 addSImm(argSize).addRegDef(vaArgI->getOperand(0)));
2370 break;
2371 }
2372
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002373 case 71: // reg: VReg
2374 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002375 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002376
2377 default:
2378 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002379 break;
2380 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002381 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002382
Chris Lattner20b1ea02001-09-14 03:47:57 +00002383 if (forwardOperandNum >= 0)
2384 { // We did not generate a machine instruction but need to use operand.
2385 // If user is in the same tree, replace Value in its machine operand.
2386 // If not, insert a copy instruction which should get coalesced away
2387 // by register allocation.
2388 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002389 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002390 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002391 {
Misha Brukmanee563cb2003-05-21 17:59:06 +00002392 std::vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002393 Instruction* instr = subtreeRoot->getInstruction();
2394 target.getInstrInfo().
2395 CreateCopyInstructionsByType(target,
2396 instr->getParent()->getParent(),
2397 instr->getOperand(forwardOperandNum),
2398 instr, minstrVec,
2399 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002400 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002401 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002402 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002403 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002404
2405 if (maskUnsignedResult)
2406 { // If result is unsigned and smaller than int reg size,
2407 // we need to clear high bits of result value.
2408 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2409 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002410 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002411 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002412 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002413 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002414 { // Mask high bits. Use a TmpInstruction to represent the
2415 // intermediate result before masking. Since those instructions
2416 // have already been generated, go back and substitute tmpI
2417 // for dest in the result position of each one of them.
2418 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2419 NULL, "maskHi");
2420 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2421
2422 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2423 mvec[i]->substituteValue(dest, tmpI);
2424
Misha Brukman91aee472003-05-27 22:37:00 +00002425 M = BuildMI(V9::SRLi6, 3).addReg(tmpI).addZImm(8*(4-destSize))
Misha Brukmana98cd452003-05-20 20:32:24 +00002426 .addReg(dest, MOTy::Def);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002427 mvec.push_back(M);
2428 }
Chris Lattner7a5adc32003-04-26 19:44:35 +00002429 else if (destSize < 8)
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002430 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002431 }
2432 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002433}