blob: 669255095c83594b4cc1d27d9f5c01b31b3a7474 [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>
Vikram S. Adve951df2b2003-07-10 20:07:54 +000028#include <algorithm>
Chris Lattner20b1ea02001-09-14 03:47:57 +000029
Chris Lattner54e898e2003-01-15 19:23:34 +000030static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +000031 std::vector<MachineInstr*>& mvec) {
Chris Lattner54e898e2003-01-15 19:23:34 +000032 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
33 .addReg(Node->rightChild()->getValue())
34 .addRegDef(Node->getValue()));
35}
36
37
38
Chris Lattner795ba6c2003-01-15 21:36:50 +000039//---------------------------------------------------------------------------
40// Function: GetMemInstArgs
41//
42// Purpose:
43// Get the pointer value and the index vector for a memory operation
44// (GetElementPtr, Load, or Store). If all indices of the given memory
45// operation are constant, fold in constant indices in a chain of
46// preceding GetElementPtr instructions (if any), and return the
47// pointer value of the first instruction in the chain.
48// All folded instructions are marked so no code is generated for them.
49//
50// Return values:
51// Returns the pointer Value to use.
52// Returns the resulting IndexVector in idxVec.
53// Returns true/false in allConstantIndices if all indices are/aren't const.
54//---------------------------------------------------------------------------
55
56
57//---------------------------------------------------------------------------
58// Function: FoldGetElemChain
59//
60// Purpose:
61// Fold a chain of GetElementPtr instructions containing only
62// constant offsets into an equivalent (Pointer, IndexVector) pair.
63// Returns the pointer Value, and stores the resulting IndexVector
64// in argument chainIdxVec. This is a helper function for
65// FoldConstantIndices that does the actual folding.
66//---------------------------------------------------------------------------
67
68
69// Check for a constant 0.
70inline bool
71IsZero(Value* idx)
72{
73 return (idx == ConstantSInt::getNullValue(idx->getType()));
74}
75
76static Value*
Misha Brukmanee563cb2003-05-21 17:59:06 +000077FoldGetElemChain(InstrTreeNode* ptrNode, std::vector<Value*>& chainIdxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +000078 bool lastInstHasLeadingNonZero)
79{
80 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
81 GetElementPtrInst* gepInst =
82 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
83
84 // ptr value is not computed in this tree or ptr value does not come from GEP
85 // instruction
86 if (gepInst == NULL)
87 return NULL;
88
89 // Return NULL if we don't fold any instructions in.
90 Value* ptrVal = NULL;
91
92 // Now chase the chain of getElementInstr instructions, if any.
93 // Check for any non-constant indices and stop there.
94 // Also, stop if the first index of child is a non-zero array index
95 // and the last index of the current node is a non-array index:
96 // in that case, a non-array declared type is being accessed as an array
97 // which is not type-safe, but could be legal.
98 //
99 InstructionNode* ptrChild = gepNode;
100 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
101 ptrChild->getOpLabel() == GetElemPtrIdx))
Misha Brukman81b06862003-05-21 18:48:06 +0000102 {
103 // Child is a GetElemPtr instruction
104 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
105 User::op_iterator OI, firstIdx = gepInst->idx_begin();
106 User::op_iterator lastIdx = gepInst->idx_end();
107 bool allConstantOffsets = true;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000108
Misha Brukman81b06862003-05-21 18:48:06 +0000109 // The first index of every GEP must be an array index.
110 assert((*firstIdx)->getType() == Type::LongTy &&
111 "INTERNAL ERROR: Structure index for a pointer type!");
Chris Lattner795ba6c2003-01-15 21:36:50 +0000112
Misha Brukman81b06862003-05-21 18:48:06 +0000113 // If the last instruction had a leading non-zero index, check if the
114 // current one references a sequential (i.e., indexable) type.
115 // If not, the code is not type-safe and we would create an illegal GEP
116 // by folding them, so don't fold any more instructions.
117 //
118 if (lastInstHasLeadingNonZero)
119 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
120 break; // cannot fold in any preceding getElementPtr instrs.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000121
Misha Brukman81b06862003-05-21 18:48:06 +0000122 // Check that all offsets are constant for this instruction
123 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
124 allConstantOffsets = isa<ConstantInt>(*OI);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000125
Misha Brukman81b06862003-05-21 18:48:06 +0000126 if (allConstantOffsets) {
127 // Get pointer value out of ptrChild.
128 ptrVal = gepInst->getPointerOperand();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000129
Misha Brukman81b06862003-05-21 18:48:06 +0000130 // Insert its index vector at the start, skipping any leading [0]
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000131 // Remember the old size to check if anything was inserted.
132 unsigned oldSize = chainIdxVec.size();
133 int firstIsZero = IsZero(*firstIdx);
134 chainIdxVec.insert(chainIdxVec.begin(), firstIdx + firstIsZero, lastIdx);
135
136 // Remember if it has leading zero index: it will be discarded later.
137 if (oldSize < chainIdxVec.size())
138 lastInstHasLeadingNonZero = !firstIsZero;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000139
Misha Brukman81b06862003-05-21 18:48:06 +0000140 // Mark the folded node so no code is generated for it.
141 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000142
Misha Brukman81b06862003-05-21 18:48:06 +0000143 // Get the previous GEP instruction and continue trying to fold
144 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
145 } else // cannot fold this getElementPtr instr. or any preceding ones
146 break;
147 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000148
149 // If the first getElementPtr instruction had a leading [0], add it back.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000150 // Note that this instruction is the *last* one that was successfully
151 // folded *and* contributed any indices, in the loop above.
152 //
Chris Lattner795ba6c2003-01-15 21:36:50 +0000153 if (ptrVal && ! lastInstHasLeadingNonZero)
154 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
155
156 return ptrVal;
157}
158
159
160//---------------------------------------------------------------------------
161// Function: GetGEPInstArgs
162//
163// Purpose:
164// Helper function for GetMemInstArgs that handles the final getElementPtr
165// instruction used by (or same as) the memory operation.
166// Extracts the indices of the current instruction and tries to fold in
167// preceding ones if all indices of the current one are constant.
168//---------------------------------------------------------------------------
169
170static Value *
171GetGEPInstArgs(InstructionNode* gepNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000172 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000173 bool& allConstantIndices)
174{
175 allConstantIndices = true;
176 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
177
178 // Default pointer is the one from the current instruction.
179 Value* ptrVal = gepI->getPointerOperand();
180 InstrTreeNode* ptrChild = gepNode->leftChild();
181
182 // Extract the index vector of the GEP instructin.
183 // If all indices are constant and first index is zero, try to fold
184 // in preceding GEPs with all constant indices.
185 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
186 allConstantIndices && OI != OE; ++OI)
187 if (! isa<Constant>(*OI))
188 allConstantIndices = false; // note: this also terminates loop!
189
190 // If we have only constant indices, fold chains of constant indices
191 // in this and any preceding GetElemPtr instructions.
192 bool foldedGEPs = false;
193 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
194 if (allConstantIndices)
Misha Brukman81b06862003-05-21 18:48:06 +0000195 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) {
196 ptrVal = newPtr;
197 foldedGEPs = true;
198 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000199
200 // Append the index vector of the current instruction.
201 // Skip the leading [0] index if preceding GEPs were folded into this.
202 idxVec.insert(idxVec.end(),
203 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
204 gepI->idx_end());
205
206 return ptrVal;
207}
208
209//---------------------------------------------------------------------------
210// Function: GetMemInstArgs
211//
212// Purpose:
213// Get the pointer value and the index vector for a memory operation
214// (GetElementPtr, Load, or Store). If all indices of the given memory
215// operation are constant, fold in constant indices in a chain of
216// preceding GetElementPtr instructions (if any), and return the
217// pointer value of the first instruction in the chain.
218// All folded instructions are marked so no code is generated for them.
219//
220// Return values:
221// Returns the pointer Value to use.
222// Returns the resulting IndexVector in idxVec.
223// Returns true/false in allConstantIndices if all indices are/aren't const.
224//---------------------------------------------------------------------------
225
226static Value*
227GetMemInstArgs(InstructionNode* memInstrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000228 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000229 bool& allConstantIndices)
230{
231 allConstantIndices = false;
232 Instruction* memInst = memInstrNode->getInstruction();
233 assert(idxVec.size() == 0 && "Need empty vector to return indices");
234
235 // If there is a GetElemPtr instruction to fold in to this instr,
236 // it must be in the left child for Load and GetElemPtr, and in the
237 // right child for Store instructions.
238 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
239 ? memInstrNode->rightChild()
240 : memInstrNode->leftChild());
241
242 // Default pointer is the one from the current instruction.
243 Value* ptrVal = ptrChild->getValue();
244
245 // Find the "last" GetElemPtr instruction: this one or the immediate child.
246 // There will be none if this is a load or a store from a scalar pointer.
247 InstructionNode* gepNode = NULL;
248 if (isa<GetElementPtrInst>(memInst))
249 gepNode = memInstrNode;
Misha Brukman81b06862003-05-21 18:48:06 +0000250 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) {
251 // Child of load/store is a GEP and memInst is its only use.
252 // Use its indices and mark it as folded.
253 gepNode = cast<InstructionNode>(ptrChild);
254 gepNode->markFoldedIntoParent();
255 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000256
257 // If there are no indices, return the current pointer.
258 // Else extract the pointer from the GEP and fold the indices.
259 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
260 : ptrVal;
261}
262
Chris Lattner54e898e2003-01-15 19:23:34 +0000263
Chris Lattner20b1ea02001-09-14 03:47:57 +0000264//************************ Internal Functions ******************************/
265
Chris Lattner20b1ea02001-09-14 03:47:57 +0000266
Chris Lattner20b1ea02001-09-14 03:47:57 +0000267static inline MachineOpCode
268ChooseBprInstruction(const InstructionNode* instrNode)
269{
270 MachineOpCode opCode;
271
272 Instruction* setCCInstr =
273 ((InstructionNode*) instrNode->leftChild())->getInstruction();
274
275 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000276 {
277 case Instruction::SetEQ: opCode = V9::BRZ; break;
278 case Instruction::SetNE: opCode = V9::BRNZ; break;
279 case Instruction::SetLE: opCode = V9::BRLEZ; break;
280 case Instruction::SetGE: opCode = V9::BRGEZ; break;
281 case Instruction::SetLT: opCode = V9::BRLZ; break;
282 case Instruction::SetGT: opCode = V9::BRGZ; break;
283 default:
284 assert(0 && "Unrecognized VM instruction!");
285 opCode = V9::INVALID_OPCODE;
286 break;
287 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000288
289 return opCode;
290}
291
292
293static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000294ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000295 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000296{
Misha Brukmana98cd452003-05-20 20:32:24 +0000297 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000298
299 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
300
Misha Brukman81b06862003-05-21 18:48:06 +0000301 if (isSigned) {
302 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000303 {
Misha Brukman81b06862003-05-21 18:48:06 +0000304 case Instruction::SetEQ: opCode = V9::BE; break;
305 case Instruction::SetNE: opCode = V9::BNE; break;
306 case Instruction::SetLE: opCode = V9::BLE; break;
307 case Instruction::SetGE: opCode = V9::BGE; break;
308 case Instruction::SetLT: opCode = V9::BL; break;
309 case Instruction::SetGT: opCode = V9::BG; break;
310 default:
311 assert(0 && "Unrecognized VM instruction!");
312 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000313 }
Misha Brukman81b06862003-05-21 18:48:06 +0000314 } else {
315 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000316 {
Misha Brukman81b06862003-05-21 18:48:06 +0000317 case Instruction::SetEQ: opCode = V9::BE; break;
318 case Instruction::SetNE: opCode = V9::BNE; break;
319 case Instruction::SetLE: opCode = V9::BLEU; break;
320 case Instruction::SetGE: opCode = V9::BCC; break;
321 case Instruction::SetLT: opCode = V9::BCS; break;
322 case Instruction::SetGT: opCode = V9::BGU; break;
323 default:
324 assert(0 && "Unrecognized VM instruction!");
325 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000326 }
Misha Brukman81b06862003-05-21 18:48:06 +0000327 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000328
329 return opCode;
330}
331
332static inline MachineOpCode
333ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000334 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000335{
Misha Brukmana98cd452003-05-20 20:32:24 +0000336 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000337
338 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000339 {
340 case Instruction::SetEQ: opCode = V9::FBE; break;
341 case Instruction::SetNE: opCode = V9::FBNE; break;
342 case Instruction::SetLE: opCode = V9::FBLE; break;
343 case Instruction::SetGE: opCode = V9::FBGE; break;
344 case Instruction::SetLT: opCode = V9::FBL; break;
345 case Instruction::SetGT: opCode = V9::FBG; break;
346 default:
347 assert(0 && "Unrecognized VM instruction!");
348 break;
349 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000350
351 return opCode;
352}
353
354
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000355// Create a unique TmpInstruction for a boolean value,
356// representing the CC register used by a branch on that value.
357// For now, hack this using a little static cache of TmpInstructions.
358// Eventually the entire BURG instruction selection should be put
359// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000360// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000361// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000362//
363static TmpInstruction*
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000364GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType,
365 MachineCodeForInstruction& mcfi)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000366{
Chris Lattner09ff1122002-07-24 21:21:32 +0000367 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000368 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000369 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000370
371 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
372
Misha Brukman81b06862003-05-21 18:48:06 +0000373 if (lastFunction != F) {
374 lastFunction = F;
375 boolToTmpCache.clear();
376 }
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000377
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000378 // Look for tmpI and create a new one otherwise. The new value is
379 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000380 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
381 if (tmpI == NULL)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000382 tmpI = new TmpInstruction(mcfi, ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000383
384 return tmpI;
385}
386
387
Chris Lattner20b1ea02001-09-14 03:47:57 +0000388static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000389ChooseBccInstruction(const InstructionNode* instrNode,
Vikram S. Adve786833a2003-07-06 20:13:59 +0000390 const Type*& setCCType)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000391{
392 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000393 assert(setCCNode->getOpLabel() == SetCCOp);
394 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +0000395 setCCType = setCCInstr->getOperand(0)->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000396
Vikram S. Adve786833a2003-07-06 20:13:59 +0000397 if (setCCType->isFloatingPoint())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000398 return ChooseBFpccInstruction(instrNode, setCCInstr);
399 else
400 return ChooseBpccInstruction(instrNode, setCCInstr);
401}
402
403
Misha Brukmaneecdb662003-06-02 20:55:14 +0000404// WARNING: since this function has only one caller, it always returns
405// the opcode that expects an immediate and a register. If this function
406// is ever used in cases where an opcode that takes two registers is required,
407// then modify this function and use convertOpcodeFromRegToImm() where required.
408//
409// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
410// new cases of opcodes.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000411static inline MachineOpCode
Misha Brukmaneecdb662003-06-02 20:55:14 +0000412ChooseMovFpcciInstruction(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000413{
Misha Brukmana98cd452003-05-20 20:32:24 +0000414 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000415
416 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000417 {
Misha Brukmaneecdb662003-06-02 20:55:14 +0000418 case Instruction::SetEQ: opCode = V9::MOVFEi; break;
419 case Instruction::SetNE: opCode = V9::MOVFNEi; break;
420 case Instruction::SetLE: opCode = V9::MOVFLEi; break;
421 case Instruction::SetGE: opCode = V9::MOVFGEi; break;
422 case Instruction::SetLT: opCode = V9::MOVFLi; break;
423 case Instruction::SetGT: opCode = V9::MOVFGi; break;
Misha Brukman81b06862003-05-21 18:48:06 +0000424 default:
425 assert(0 && "Unrecognized VM instruction!");
426 break;
427 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000428
429 return opCode;
430}
431
432
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000433// ChooseMovpcciForSetCC -- Choose a conditional-move instruction
434// based on the type of SetCC operation.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000435//
Misha Brukmaneecdb662003-06-02 20:55:14 +0000436// WARNING: since this function has only one caller, it always returns
437// the opcode that expects an immediate and a register. If this function
438// is ever used in cases where an opcode that takes two registers is required,
439// then modify this function and use convertOpcodeFromRegToImm() where required.
440//
441// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
442// new cases of opcodes.
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000443//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000444static MachineOpCode
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000445ChooseMovpcciForSetCC(const InstructionNode* instrNode)
446{
447 MachineOpCode opCode = V9::INVALID_OPCODE;
448
449 const Type* opType = instrNode->leftChild()->getValue()->getType();
450 assert(opType->isIntegral() || isa<PointerType>(opType));
451 bool noSign = opType->isUnsigned() || isa<PointerType>(opType);
452
453 switch(instrNode->getInstruction()->getOpcode())
454 {
455 case Instruction::SetEQ: opCode = V9::MOVEi; break;
456 case Instruction::SetLE: opCode = noSign? V9::MOVLEUi : V9::MOVLEi; break;
457 case Instruction::SetGE: opCode = noSign? V9::MOVCCi : V9::MOVGEi; break;
458 case Instruction::SetLT: opCode = noSign? V9::MOVCSi : V9::MOVLi; break;
459 case Instruction::SetGT: opCode = noSign? V9::MOVGUi : V9::MOVGi; break;
460 case Instruction::SetNE: opCode = V9::MOVNEi; break;
461 default: assert(0 && "Unrecognized LLVM instr!"); break;
462 }
463
464 return opCode;
465}
466
467
468// ChooseMovpregiForSetCC -- Choose a conditional-move-on-register-value
469// instruction based on the type of SetCC operation. These instructions
470// compare a register with 0 and perform the move is the comparison is true.
471//
472// WARNING: like the previous function, this function it always returns
473// the opcode that expects an immediate and a register. See above.
474//
475static MachineOpCode
476ChooseMovpregiForSetCC(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000477{
Misha Brukmana98cd452003-05-20 20:32:24 +0000478 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000479
480 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000481 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000482 case Instruction::SetEQ: opCode = V9::MOVRZi; break;
483 case Instruction::SetLE: opCode = V9::MOVRLEZi; break;
484 case Instruction::SetGE: opCode = V9::MOVRGEZi; break;
485 case Instruction::SetLT: opCode = V9::MOVRLZi; break;
486 case Instruction::SetGT: opCode = V9::MOVRGZi; break;
487 case Instruction::SetNE: opCode = V9::MOVRNZi; break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000488 default: assert(0 && "Unrecognized VM instr!"); break;
Misha Brukman81b06862003-05-21 18:48:06 +0000489 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000490
491 return opCode;
492}
493
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000494
Chris Lattner20b1ea02001-09-14 03:47:57 +0000495static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000496ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000497{
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000498 assert((vopCode == ToFloatTy || vopCode == ToDoubleTy) &&
499 "Unrecognized convert-to-float opcode!");
500
Misha Brukmana98cd452003-05-20 20:32:24 +0000501 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000502
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000503 if (opType == Type::SByteTy || opType == Type::UByteTy ||
504 opType == Type::ShortTy || opType == Type::UShortTy ||
505 opType == Type::IntTy || opType == Type::UIntTy)
506 opCode = (vopCode == ToFloatTy? V9::FITOS : V9::FITOD);
Vikram S. Adve784a18b2003-07-02 01:13:57 +0000507 else if (opType == Type::LongTy || opType == Type::ULongTy ||
508 isa<PointerType>(opType))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000509 opCode = (vopCode == ToFloatTy? V9::FXTOS : V9::FXTOD);
510 else if (opType == Type::FloatTy)
511 opCode = (vopCode == ToFloatTy? V9::INVALID_OPCODE : V9::FSTOD);
512 else if (opType == Type::DoubleTy)
513 opCode = (vopCode == ToFloatTy? V9::FDTOS : V9::INVALID_OPCODE);
514 else
Vikram S. Adve784a18b2003-07-02 01:13:57 +0000515 assert(0 && "Trying to convert a non-scalar type to DOUBLE?");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000516
517 return opCode;
518}
519
520static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000521ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000522{
Misha Brukmana98cd452003-05-20 20:32:24 +0000523 MachineOpCode opCode = V9::INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000524
525 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
526 && "This function should only be called for FLOAT or DOUBLE");
527
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000528 // SPARC does not have a float-to-uint conversion, only a float-to-int.
529 // For converting an FP value to uint32_t, we first need to convert to
530 // uint64_t and then to uint32_t, or we may overflow the signed int
531 // representation even for legal uint32_t values. This expansion is
532 // done by the Preselection pass.
533 //
Misha Brukman81b06862003-05-21 18:48:06 +0000534 if (tid == Type::UIntTyID) {
535 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
536 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
537 } else if (tid == Type::SByteTyID || tid == Type::ShortTyID ||
538 tid == Type::IntTyID || tid == Type::UByteTyID ||
539 tid == Type::UShortTyID) {
540 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
541 } else if (tid == Type::LongTyID || tid == Type::ULongTyID) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000542 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
Misha Brukman81b06862003-05-21 18:48:06 +0000543 } else
544 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000545
Chris Lattner20b1ea02001-09-14 03:47:57 +0000546 return opCode;
547}
548
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000549MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000550CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
551 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000552{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000553 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000554 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000555 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000556}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000557
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000558// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000559// The FP value must be converted to the dest type in an FP register,
560// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000561//
562// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
563// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000564// *only* when converting to an unsigned. (Unsigned byte, short or long
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000565// don't have this problem.)
566// For unsigned int, we therefore have to generate the code sequence:
567//
568// if (V > (float) MAXINT) {
569// unsigned result = (unsigned) (V - (float) MAXINT);
570// result = result + (unsigned) MAXINT;
571// }
572// else
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000573// result = (unsigned) V;
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000574//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000575static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000576CreateCodeToConvertFloatToInt(const TargetMachine& target,
577 Value* opVal,
578 Instruction* destI,
579 std::vector<MachineInstr*>& mvec,
580 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000581{
582 // Create a temporary to represent the FP register into which the
583 // int value will placed after conversion. The type of this temporary
584 // depends on the type of FP register to use: single-prec for a 32-bit
585 // int or smaller; double-prec for a 64-bit int.
586 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000587 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000588 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000589 TmpInstruction* destForCast = new TmpInstruction(mcfi, destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000590
591 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000592 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
593 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000594 mvec.push_back(M);
595
596 // Create the fpreg-to-intreg copy code
597 target.getInstrInfo().
598 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000599 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000600}
601
602
Chris Lattner20b1ea02001-09-14 03:47:57 +0000603static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000604ChooseAddInstruction(const InstructionNode* instrNode)
605{
606 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
607}
608
609
Chris Lattner20b1ea02001-09-14 03:47:57 +0000610static inline MachineInstr*
611CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000612 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000613{
Misha Brukmana98cd452003-05-20 20:32:24 +0000614 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000615 .addReg(instrNode->leftChild()->getValue())
616 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000617}
618
619static inline MachineInstr*
620CreateAddConstInstruction(const InstructionNode* instrNode)
621{
622 MachineInstr* minstr = NULL;
623
624 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000625 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000626
627 // Cases worth optimizing are:
628 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
629 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
630 //
Chris Lattner9b625032002-05-06 16:15:30 +0000631 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000632 double dval = FPC->getValue();
633 if (dval == 0.0)
634 minstr = CreateMovFloatInstruction(instrNode,
635 instrNode->getInstruction()->getType());
636 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000637
638 return minstr;
639}
640
641
642static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000643ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000644{
Misha Brukmana98cd452003-05-20 20:32:24 +0000645 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000646
Misha Brukman81b06862003-05-21 18:48:06 +0000647 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukman91aee472003-05-27 22:37:00 +0000648 opCode = V9::SUBr;
Misha Brukman81b06862003-05-21 18:48:06 +0000649 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000650 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000651 {
652 case Type::FloatTyID: opCode = V9::FSUBS; break;
653 case Type::DoubleTyID: opCode = V9::FSUBD; break;
654 default: assert(0 && "Invalid type for SUB instruction"); break;
655 }
656 }
657
Chris Lattner20b1ea02001-09-14 03:47:57 +0000658 return opCode;
659}
660
661
662static inline MachineInstr*
663CreateSubConstInstruction(const InstructionNode* instrNode)
664{
665 MachineInstr* minstr = NULL;
666
667 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000668 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000669
670 // Cases worth optimizing are:
671 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
672 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
673 //
Chris Lattner9b625032002-05-06 16:15:30 +0000674 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
675 double dval = FPC->getValue();
676 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000677 minstr = CreateMovFloatInstruction(instrNode,
678 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000679 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000680
681 return minstr;
682}
683
684
685static inline MachineOpCode
686ChooseFcmpInstruction(const InstructionNode* instrNode)
687{
Misha Brukmana98cd452003-05-20 20:32:24 +0000688 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000689
690 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
691 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000692 case Type::FloatTyID: opCode = V9::FCMPS; break;
693 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000694 default: assert(0 && "Invalid type for FCMP instruction"); break;
695 }
696
697 return opCode;
698}
699
700
701// Assumes that leftArg and rightArg are both cast instructions.
702//
703static inline bool
704BothFloatToDouble(const InstructionNode* instrNode)
705{
706 InstrTreeNode* leftArg = instrNode->leftChild();
707 InstrTreeNode* rightArg = instrNode->rightChild();
708 InstrTreeNode* leftArgArg = leftArg->leftChild();
709 InstrTreeNode* rightArgArg = rightArg->leftChild();
710 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
711
712 // Check if both arguments are floats cast to double
713 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000714 leftArgArg->getValue()->getType() == Type::FloatTy &&
715 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000716}
717
718
719static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000720ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000721{
Misha Brukmana98cd452003-05-20 20:32:24 +0000722 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000723
Chris Lattner0c4e8862002-09-03 01:08:28 +0000724 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000725 opCode = V9::MULXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000726 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000727 switch(resultType->getPrimitiveID())
Misha Brukman7b647942003-05-30 20:11:56 +0000728 {
729 case Type::FloatTyID: opCode = V9::FMULS; break;
730 case Type::DoubleTyID: opCode = V9::FMULD; break;
731 default: assert(0 && "Invalid type for MUL instruction"); break;
732 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000733
734 return opCode;
735}
736
737
Vikram S. Adve510eec72001-11-04 21:59:14 +0000738
Chris Lattner20b1ea02001-09-14 03:47:57 +0000739static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000740CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000741 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000742{
Misha Brukman91aee472003-05-27 22:37:00 +0000743 return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000744 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000745}
746
747
Vikram S. Adve242a8082002-05-19 15:25:51 +0000748// Create instruction sequence for any shift operation.
749// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
750// requires a second instruction for explicit sign-extension.
751// Note that we only have to worry about a sign-bit appearing in the
752// most significant bit of the operand after shifting (e.g., bit 32 of
753// Int or bit 16 of Short), so we do not have to worry about results
754// that are as large as a normal integer register.
755//
756static inline void
757CreateShiftInstructions(const TargetMachine& target,
758 Function* F,
759 MachineOpCode shiftOpCode,
760 Value* argVal1,
761 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000762 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000763 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000764 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000765 MachineCodeForInstruction& mcfi)
766{
767 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
768 "Large shift sizes unexpected, but can be handled below: "
769 "You need to check whether or not it fits in immed field below");
770
771 // If this is a logical left shift of a type smaller than the standard
772 // integer reg. size, we have to extend the sign-bit into upper bits
773 // of dest, so we need to put the result of the SLL into a temporary.
774 //
775 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000776 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000777
Misha Brukmand36e30e2003-06-06 09:52:23 +0000778 if ((shiftOpCode == V9::SLLr5 || shiftOpCode == V9::SLLXr6) && opSize < 8) {
Misha Brukman7b647942003-05-30 20:11:56 +0000779 // put SLL result into a temporary
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000780 shiftDest = new TmpInstruction(mcfi, argVal1, optArgVal2, "sllTmp");
Misha Brukman7b647942003-05-30 20:11:56 +0000781 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000782
783 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000784 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
785 .addReg(shiftDest, MOTy::Def)
786 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
787 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000788 mvec.push_back(M);
789
Misha Brukman7b647942003-05-30 20:11:56 +0000790 if (shiftDest != destVal) {
791 // extend the sign-bit of the result into all upper bits of dest
792 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
793 target.getInstrInfo().
794 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
795 8*opSize, mvec, mcfi);
796 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000797}
798
799
Vikram S. Adve74825322002-03-18 03:15:35 +0000800// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000801// create a cheaper instruction.
802// This returns the approximate cost of the instructions generated,
803// which is used to pick the cheapest when both operands are constant.
Vikram S. Adve645fea32003-05-25 21:59:47 +0000804static unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000805CreateMulConstInstruction(const TargetMachine &target, Function* F,
806 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000807 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000808 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000809{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000810 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukman91aee472003-05-27 22:37:00 +0000811 unsigned cost = target.getInstrInfo().minLatency(V9::MULXr);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000812 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000813
814 Value* constOp = rval;
815 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000816 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000817
818 // Cases worth optimizing are:
819 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
820 // (2) Multiply by 2^x for integer types: replace with Shift
821 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000822 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000823
Misha Brukmana98cd452003-05-20 20:32:24 +0000824 if (resultType->isInteger() || isa<PointerType>(resultType)) {
825 bool isValidConst;
826 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
827 if (isValidConst) {
828 unsigned pow;
829 bool needNeg = false;
830 if (C < 0) {
831 needNeg = true;
832 C = -C;
833 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000834
Misha Brukmana98cd452003-05-20 20:32:24 +0000835 if (C == 0 || C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000836 cost = target.getInstrInfo().minLatency(V9::ADDr);
Misha Brukmana98cd452003-05-20 20:32:24 +0000837 unsigned Zero = target.getRegInfo().getZeroRegNum();
838 MachineInstr* M;
839 if (C == 0)
Misha Brukman91aee472003-05-27 22:37:00 +0000840 M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000841 else
Misha Brukman91aee472003-05-27 22:37:00 +0000842 M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000843 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +0000844 } else if (isPowerOf2(C, pow)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000845 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Misha Brukmand36e30e2003-06-06 09:52:23 +0000846 MachineOpCode opCode = (opSize <= 32)? V9::SLLr5 : V9::SLLXr6;
Misha Brukmana98cd452003-05-20 20:32:24 +0000847 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
848 destVal, mvec, mcfi);
849 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000850
Misha Brukman7b647942003-05-30 20:11:56 +0000851 if (mvec.size() > 0 && needNeg) {
852 // insert <reg = SUB 0, reg> after the instr to flip the sign
Misha Brukmana98cd452003-05-20 20:32:24 +0000853 MachineInstr* M = CreateIntNegInstruction(target, destVal);
854 mvec.push_back(M);
855 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000856 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000857 } else {
858 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
859 double dval = FPC->getValue();
860 if (fabs(dval) == 1) {
861 MachineOpCode opCode = (dval < 0)
862 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
863 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
864 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
865 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000866 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000867 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000868
Misha Brukmana98cd452003-05-20 20:32:24 +0000869 if (firstNewInstr < mvec.size()) {
870 cost = 0;
871 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
872 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
873 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000874
875 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000876}
877
878
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000879// Does not create any instructions if we cannot exploit constant to
880// create a cheaper instruction.
881//
882static inline void
883CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000884 Function* F,
885 Value* lval, Value* rval,
886 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000887 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000888 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000889{
890 Value* constOp;
Misha Brukman7b647942003-05-30 20:11:56 +0000891 if (isa<Constant>(lval) && isa<Constant>(rval)) {
892 // both operands are constant: evaluate and "set" in dest
893 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
894 cast<Constant>(lval),
895 cast<Constant>(rval));
896 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
897 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000898 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000899 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000900 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000901 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000902
903 // else neither is constant
904 return;
905}
906
Vikram S. Adve74825322002-03-18 03:15:35 +0000907// Return NULL if we cannot exploit constant to create a cheaper instruction
908static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000909CreateMulInstruction(const TargetMachine &target, Function* F,
910 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000911 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000912 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000913 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
914{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000915 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000916 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000917 if (mvec.size() == L) {
918 // no instructions were added so create MUL reg, reg, reg.
919 // Use FSMULD if both operands are actually floats cast to doubles.
920 // Otherwise, use the default opcode for the appropriate type.
921 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
922 ? forceMulOp
923 : ChooseMulInstructionByType(destVal->getType()));
924 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
925 .addRegDef(destVal));
926 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000927}
928
929
Vikram S. Adve510eec72001-11-04 21:59:14 +0000930// Generate a divide instruction for Div or Rem.
931// For Rem, this assumes that the operand type will be signed if the result
932// type is signed. This is correct because they must have the same sign.
933//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000934static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000935ChooseDivInstruction(TargetMachine &target,
936 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000937{
Misha Brukmana98cd452003-05-20 20:32:24 +0000938 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000939
940 const Type* resultType = instrNode->getInstruction()->getType();
941
Chris Lattner0c4e8862002-09-03 01:08:28 +0000942 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000943 opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000944 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000945 switch(resultType->getPrimitiveID())
946 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000947 case Type::FloatTyID: opCode = V9::FDIVS; break;
948 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000949 default: assert(0 && "Invalid type for DIV instruction"); break;
950 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000951
952 return opCode;
953}
954
955
Chris Lattner54e898e2003-01-15 19:23:34 +0000956// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve645fea32003-05-25 21:59:47 +0000957static void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000958CreateDivConstInstruction(TargetMachine &target,
959 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000960 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000961{
Chris Lattner54e898e2003-01-15 19:23:34 +0000962 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000963 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000964 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000965 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000966
Vikram S. Adve645fea32003-05-25 21:59:47 +0000967 Instruction* destVal = instrNode->getInstruction();
Chris Lattner54e898e2003-01-15 19:23:34 +0000968 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000969
970 // Cases worth optimizing are:
971 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
972 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
973 //
974 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000975
Misha Brukman7b647942003-05-30 20:11:56 +0000976 if (resultType->isInteger()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000977 unsigned pow;
978 bool isValidConst;
979 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
980 if (isValidConst) {
981 bool needNeg = false;
982 if (C < 0) {
983 needNeg = true;
984 C = -C;
985 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000986
Misha Brukmana98cd452003-05-20 20:32:24 +0000987 if (C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000988 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000989 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +0000990 } else if (isPowerOf2(C, pow)) {
Vikram S. Adve645fea32003-05-25 21:59:47 +0000991 unsigned opCode;
992 Value* shiftOperand;
993
994 if (resultType->isSigned()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000995 // For N / 2^k, if the operand N is negative,
996 // we need to add (2^k - 1) before right-shifting by k, i.e.,
Vikram S. Adve645fea32003-05-25 21:59:47 +0000997 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000998 // (N / 2^k) = N >> k, if N >= 0;
999 // (N + 2^k - 1) >> k, if N < 0
1000 //
1001 // If N is <= 32 bits, use:
1002 // sra N, 31, t1 // t1 = ~0, if N < 0, 0 else
1003 // srl t1, 32-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1004 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1005 // sra t3, k, result // result = N / 2^k
1006 //
1007 // If N is 64 bits, use:
1008 // srax N, k-1, t1 // t1 = sign bit in high k positions
1009 // srlx t1, 64-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1010 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1011 // sra t3, k, result // result = N / 2^k
1012 //
1013 TmpInstruction *sraTmp, *srlTmp, *addTmp;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001014 MachineCodeForInstruction& mcfi
1015 = MachineCodeForInstruction::get(destVal);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001016 sraTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getSign");
1017 srlTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getPlus2km1");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001018 addTmp = new TmpInstruction(mcfi, resultType, LHS, srlTmp,"incIfNeg");
Vikram S. Adve645fea32003-05-25 21:59:47 +00001019
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001020 // Create the SRA or SRAX instruction to get the sign bit
1021 mvec.push_back(BuildMI((resultType==Type::LongTy) ?
1022 V9::SRAXi6 : V9::SRAi5, 3)
1023 .addReg(LHS)
1024 .addSImm((resultType==Type::LongTy)? pow-1 : 31)
1025 .addRegDef(sraTmp));
1026
Vikram S. Adve645fea32003-05-25 21:59:47 +00001027 // Create the SRL or SRLX instruction to get the sign bit
Misha Brukman91aee472003-05-27 22:37:00 +00001028 mvec.push_back(BuildMI((resultType==Type::LongTy) ?
Misha Brukmand36e30e2003-06-06 09:52:23 +00001029 V9::SRLXi6 : V9::SRLi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001030 .addReg(sraTmp)
1031 .addSImm((resultType==Type::LongTy)? 64-pow : 32-pow)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001032 .addRegDef(srlTmp));
1033
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001034 // Create the ADD instruction to add 2^pow-1 for negative values
Misha Brukman91aee472003-05-27 22:37:00 +00001035 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001036 .addRegDef(addTmp));
1037
1038 // Get the shift operand and "right-shift" opcode to do the divide
1039 shiftOperand = addTmp;
Misha Brukmand36e30e2003-06-06 09:52:23 +00001040 opCode = (resultType==Type::LongTy) ? V9::SRAXi6 : V9::SRAi5;
Misha Brukman7b647942003-05-30 20:11:56 +00001041 } else {
Vikram S. Adve645fea32003-05-25 21:59:47 +00001042 // Get the shift operand and "right-shift" opcode to do the divide
1043 shiftOperand = LHS;
Misha Brukmand36e30e2003-06-06 09:52:23 +00001044 opCode = (resultType==Type::LongTy) ? V9::SRLXi6 : V9::SRLi5;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001045 }
1046
1047 // Now do the actual shift!
1048 mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow)
1049 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001050 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001051
Misha Brukmana98cd452003-05-20 20:32:24 +00001052 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
1053 // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve645fea32003-05-25 21:59:47 +00001054 mvec.push_back(CreateIntNegInstruction(target, destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001055 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001056 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001057 } else {
1058 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
1059 double dval = FPC->getValue();
1060 if (fabs(dval) == 1) {
1061 unsigned opCode =
1062 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
1063 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001064
Vikram S. Adve645fea32003-05-25 21:59:47 +00001065 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001066 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001067 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001068 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001069}
1070
1071
Vikram S. Adve74825322002-03-18 03:15:35 +00001072static void
1073CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1074 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001075 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001076 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001077 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001078{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001079 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001080 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001081 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001082 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001083
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001084 // Enforce the alignment constraints on the stack pointer at
1085 // compile time if the total size is a known constant.
Misha Brukman7b647942003-05-30 20:11:56 +00001086 if (isa<Constant>(numElementsVal)) {
1087 bool isValid;
1088 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
1089 assert(isValid && "Unexpectedly large array dimension in alloca!");
1090 int64_t total = numElem * tsize;
1091 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1092 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1093 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1094 } else {
1095 // The size is not a constant. Generate code to compute it and
1096 // code to pad the size for stack alignment.
1097 // Create a Value to hold the (constant) element size
1098 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001099
Misha Brukman7b647942003-05-30 20:11:56 +00001100 // Create temporary values to hold the result of MUL, SLL, SRL
Vikram S. Adve80544442003-06-23 02:13:57 +00001101 // To pad `size' to next smallest multiple of 16:
1102 // size = (size + 15) & (-16 = 0xfffffffffffffff0)
1103 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001104 TmpInstruction* tmpProd = new TmpInstruction(mcfi,numElementsVal, tsizeVal);
Vikram S. Adve80544442003-06-23 02:13:57 +00001105 TmpInstruction* tmpAdd15= new TmpInstruction(mcfi,numElementsVal, tmpProd);
1106 TmpInstruction* tmpAndf0= new TmpInstruction(mcfi,numElementsVal, tmpAdd15);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001107
Misha Brukman7b647942003-05-30 20:11:56 +00001108 // Instruction 1: mul numElements, typeSize -> tmpProd
1109 // This will optimize the MUL as far as possible.
Vikram S. Adve80544442003-06-23 02:13:57 +00001110 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd, getMvec,
Misha Brukman7b647942003-05-30 20:11:56 +00001111 mcfi, INVALID_MACHINE_OPCODE);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001112
Vikram S. Adve80544442003-06-23 02:13:57 +00001113 // Instruction 2: andn tmpProd, 0x0f -> tmpAndn
1114 getMvec.push_back(BuildMI(V9::ADDi, 3).addReg(tmpProd).addSImm(15)
1115 .addReg(tmpAdd15, MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001116
Vikram S. Adve80544442003-06-23 02:13:57 +00001117 // Instruction 3: add tmpAndn, 0x10 -> tmpAdd16
1118 getMvec.push_back(BuildMI(V9::ANDi, 3).addReg(tmpAdd15).addSImm(-16)
1119 .addReg(tmpAndf0, MOTy::Def));
1120
1121 totalSizeVal = tmpAndf0;
Misha Brukman7b647942003-05-30 20:11:56 +00001122 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001123
1124 // Get the constant offset from SP for dynamically allocated storage
1125 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001126 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001127 bool growUp;
1128 ConstantSInt* dynamicAreaOffset =
1129 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001130 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001131 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1132
Chris Lattner54e898e2003-01-15 19:23:34 +00001133 unsigned SPReg = target.getRegInfo().getStackPointer();
1134
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001135 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukman91aee472003-05-27 22:37:00 +00001136 getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001137 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001138
Vikram S. Adve74825322002-03-18 03:15:35 +00001139 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukman91aee472003-05-27 22:37:00 +00001140 getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset)
Misha Brukmana98cd452003-05-20 20:32:24 +00001141 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001142}
1143
1144
1145static void
1146CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1147 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001148 unsigned tsize,
1149 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001150 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001151{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001152 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001153 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001154 "Result value is not part of a function?");
1155 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001156 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001157
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001158 // Put the variable in the dynamically sized area of the frame if either:
1159 // (a) The offset is too large to use as an immediate in load/stores
1160 // (check LDX because all load/stores have the same-size immed. field).
1161 // (b) The object is "large", so it could cause many other locals,
1162 // spills, and temporaries to have large offsets.
1163 // NOTE: We use LARGE = 8 * argSlotSize = 64 bytes.
1164 // You've gotta love having only 13 bits for constant offset values :-|.
1165 //
1166 unsigned paddedSize;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001167 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001168 paddedSize,
1169 tsize * numElements);
1170
1171 if (((int)paddedSize) > 8 * target.getFrameInfo().getSizeOfEachArgOnStack() ||
1172 ! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001173 CreateCodeForVariableSizeAlloca(target, result, tsize,
1174 ConstantSInt::get(Type::IntTy,numElements),
1175 getMvec);
1176 return;
1177 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001178
1179 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001180 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001181
1182 // Create a temporary Value to hold the constant offset.
1183 // This is needed because it may not fit in the immediate field.
1184 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1185
1186 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001187 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukman91aee472003-05-27 22:37:00 +00001188 getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001189 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001190}
1191
1192
Chris Lattner20b1ea02001-09-14 03:47:57 +00001193//------------------------------------------------------------------------
1194// Function SetOperandsForMemInstr
1195//
1196// Choose addressing mode for the given load or store instruction.
1197// Use [reg+reg] if it is an indexed reference, and the index offset is
1198// not a constant or if it cannot fit in the offset field.
1199// Use [reg+offset] in all other cases.
1200//
1201// This assumes that all array refs are "lowered" to one of these forms:
1202// %x = load (subarray*) ptr, constant ; single constant offset
1203// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1204// Generally, this should happen via strength reduction + LICM.
1205// Also, strength reduction should take care of using the same register for
1206// the loop index variable and an array index, when that is profitable.
1207//------------------------------------------------------------------------
1208
1209static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001210SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001211 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001212 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001213 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001214{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001215 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001216 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001217 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001218 bool allConstantIndices;
1219 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001220
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001221 // Now create the appropriate operands for the machine instruction.
1222 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001223 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001224 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001225 MachineOperand::MachineOperandType offsetOpType =
1226 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001227
Vikram S. Adve74825322002-03-18 03:15:35 +00001228 // Check if there is an index vector and if so, compute the
1229 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001230 //
Misha Brukman7b647942003-05-30 20:11:56 +00001231 if (!idxVec.empty()) {
1232 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001233
Misha Brukman7b647942003-05-30 20:11:56 +00001234 // If all indices are constant, compute the combined offset directly.
1235 if (allConstantIndices) {
1236 // Compute the offset value using the index vector. Create a
1237 // virtual reg. for it since it may not fit in the immed field.
1238 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
1239 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
1240 } else {
1241 // There is at least one non-constant offset. Therefore, this must
1242 // be an array ref, and must have been lowered to a single non-zero
1243 // offset. (An extra leading zero offset, if any, can be ignored.)
1244 // Generate code sequence to compute address from index.
1245 //
1246 bool firstIdxIsZero = IsZero(idxVec[0]);
1247 assert(idxVec.size() == 1U + firstIdxIsZero
1248 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001249
Misha Brukman7b647942003-05-30 20:11:56 +00001250 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001251
Misha Brukman7b647942003-05-30 20:11:56 +00001252 std::vector<MachineInstr*> mulVec;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001253 Instruction* addr =
1254 new TmpInstruction(MachineCodeForInstruction::get(memInst),
1255 Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001256
Misha Brukman7b647942003-05-30 20:11:56 +00001257 // Get the array type indexed by idxVal, and compute its element size.
1258 // The call to getTypeSize() will fail if size is not constant.
1259 const Type* vecType = (firstIdxIsZero
1260 ? GetElementPtrInst::getIndexedType(ptrType,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001261 std::vector<Value*>(1U, idxVec[0]),
1262 /*AllowCompositeLeaf*/ true)
1263 : ptrType);
Misha Brukman7b647942003-05-30 20:11:56 +00001264 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1265 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
1266 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001267
Misha Brukman7b647942003-05-30 20:11:56 +00001268 // CreateMulInstruction() folds constants intelligently enough.
1269 CreateMulInstruction(target, memInst->getParent()->getParent(),
1270 idxVal, /* lval, not likely to be const*/
1271 eltSizeVal, /* rval, likely to be constant */
1272 addr, /* result */
1273 mulVec, MachineCodeForInstruction::get(memInst),
1274 INVALID_MACHINE_OPCODE);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001275
Misha Brukman7b647942003-05-30 20:11:56 +00001276 assert(mulVec.size() > 0 && "No multiply code created?");
1277 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
1278
1279 valueForRegOffset = addr;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001280 }
Misha Brukman7b647942003-05-30 20:11:56 +00001281 } else {
1282 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1283 smallConstOffset = 0;
1284 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001285
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001286 // For STORE:
1287 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1288 // For LOAD or GET_ELEMENT_PTR,
1289 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1290 //
1291 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001292 MachineInstr *MI;
1293 if (memInst->getOpcode() == Instruction::Store) {
Misha Brukman7b647942003-05-30 20:11:56 +00001294 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001295 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1296 .addReg(ptrVal).addReg(valueForRegOffset);
Misha Brukman7b647942003-05-30 20:11:56 +00001297 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001298 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001299 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1300 .addReg(ptrVal).addSImm(smallConstOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001301 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001302 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00001303 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001304 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1305 .addRegDef(memInst);
Misha Brukman7b647942003-05-30 20:11:56 +00001306 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001307 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001308 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1309 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001310 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001311 }
1312 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001313}
1314
1315
Chris Lattner20b1ea02001-09-14 03:47:57 +00001316//
1317// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001318// in place of the use(s) of that instruction in node `parent'.
1319// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001320// Also make sure to skip over a parent who:
1321// (1) is a list node in the Burg tree, or
1322// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001323//
1324static void
1325ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001326 InstrTreeNode* parent,
1327 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001328{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001329 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1330
Chris Lattner20b1ea02001-09-14 03:47:57 +00001331 Instruction* unusedOp = treeNode->getInstruction();
1332 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001333
1334 // The parent itself may be a list node, so find the real parent instruction
1335 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1336 {
1337 parent = parent->parent();
1338 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1339 }
1340 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1341
1342 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001343 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001344
1345 // The parent's mvec would be empty if it was itself forwarded.
1346 // Recursively call ForwardOperand in that case...
1347 //
Misha Brukman7b647942003-05-30 20:11:56 +00001348 if (mvec.size() == 0) {
1349 assert(parent->parent() != NULL &&
1350 "Parent could not have been forwarded, yet has no instructions?");
1351 ForwardOperand(treeNode, parent->parent(), operandNum);
1352 } else {
1353 for (unsigned i=0, N=mvec.size(); i < N; i++) {
1354 MachineInstr* minstr = mvec[i];
1355 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) {
1356 const MachineOperand& mop = minstr->getOperand(i);
1357 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
1358 mop.getVRegValue() == unusedOp)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001359 {
Misha Brukman7b647942003-05-30 20:11:56 +00001360 minstr->SetMachineOperandVal(i, MachineOperand::MO_VirtualRegister,
1361 fwdOp);
1362 }
1363 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001364
Misha Brukman7b647942003-05-30 20:11:56 +00001365 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1366 if (minstr->getImplicitRef(i) == unusedOp) {
1367 minstr->setImplicitRef(i, fwdOp,
1368 minstr->getImplicitOp(i).opIsDefOnly(),
1369 minstr->getImplicitOp(i).opIsDefAndUse());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001370 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001371 }
Misha Brukman7b647942003-05-30 20:11:56 +00001372 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001373}
1374
1375
Vikram S. Adve242a8082002-05-19 15:25:51 +00001376inline bool
1377AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001378{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001379 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1380 UI != UE; ++UI)
1381 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1382 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1383 return false;
1384 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001385}
1386
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001387// Generate code for any intrinsic that needs a special code sequence
1388// instead of a regular call. If not that kind of intrinsic, do nothing.
1389// Returns true if code was generated, otherwise false.
1390//
1391bool CodeGenIntrinsic(LLVMIntrinsic::ID iid, CallInst &callInstr,
1392 TargetMachine &target,
1393 std::vector<MachineInstr*>& mvec)
1394{
1395 switch (iid) {
1396 case LLVMIntrinsic::va_start: {
1397 // Get the address of the first vararg value on stack and copy it to
1398 // the argument of va_start(va_list* ap).
1399 bool ignore;
1400 Function* func = cast<Function>(callInstr.getParent()->getParent());
1401 int numFixedArgs = func->getFunctionType()->getNumParams();
1402 int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum();
1403 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
1404 int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo().
1405 getFirstIncomingArgOffset(MachineFunction::get(func), ignore);
Misha Brukman91aee472003-05-27 22:37:00 +00001406 mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001407 addReg(callInstr.getOperand(1)));
1408 return true;
1409 }
1410
1411 case LLVMIntrinsic::va_end:
1412 return true; // no-op on Sparc
1413
1414 case LLVMIntrinsic::va_copy:
1415 // Simple copy of current va_list (arg2) to new va_list (arg1)
Misha Brukman91aee472003-05-27 22:37:00 +00001416 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001417 addMReg(target.getRegInfo().getZeroRegNum()).
1418 addReg(callInstr.getOperand(2)).
1419 addReg(callInstr.getOperand(1)));
1420 return true;
1421
1422 default:
1423 return false;
1424 }
1425}
1426
Vikram S. Advefb361122001-10-22 13:36:31 +00001427//******************* Externally Visible Functions *************************/
1428
Vikram S. Advefb361122001-10-22 13:36:31 +00001429//------------------------------------------------------------------------
1430// External Function: ThisIsAChainRule
1431//
1432// Purpose:
1433// Check if a given BURG rule is a chain rule.
1434//------------------------------------------------------------------------
1435
1436extern bool
1437ThisIsAChainRule(int eruleno)
1438{
1439 switch(eruleno)
1440 {
1441 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001442 case 123:
1443 case 124:
1444 case 125:
1445 case 126:
1446 case 127:
1447 case 128:
1448 case 129:
1449 case 130:
1450 case 131:
1451 case 132:
1452 case 133:
1453 case 155:
1454 case 221:
1455 case 222:
1456 case 241:
1457 case 242:
1458 case 243:
1459 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001460 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001461 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001462 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001463
Vikram S. Advefb361122001-10-22 13:36:31 +00001464 default:
1465 return false; break;
1466 }
1467}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001468
1469
1470//------------------------------------------------------------------------
1471// External Function: GetInstructionsByRule
1472//
1473// Purpose:
1474// Choose machine instructions for the SPARC according to the
1475// patterns chosen by the BURG-generated parser.
1476//------------------------------------------------------------------------
1477
Vikram S. Adve74825322002-03-18 03:15:35 +00001478void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001479GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001480 int ruleForNode,
1481 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001482 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001483 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001484{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001485 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001486 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001487 int nextRule;
1488 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001489 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001490 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001491 unsigned L;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001492 bool foldCase = false;
Vikram S. Adve74825322002-03-18 03:15:35 +00001493
1494 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001495
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001496 // If the code for this instruction was folded into the parent (user),
1497 // then do nothing!
1498 if (subtreeRoot->isFoldedIntoParent())
1499 return;
1500
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001501 //
1502 // Let's check for chain rules outside the switch so that we don't have
1503 // to duplicate the list of chain rule production numbers here again
1504 //
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001505 if (ThisIsAChainRule(ruleForNode))
1506 {
1507 // Chain rules have a single nonterminal on the RHS.
1508 // Get the rule that matches the RHS non-terminal and use that instead.
1509 //
1510 assert(nts[0] && ! nts[1]
1511 && "A chain rule should have only one RHS non-terminal!");
1512 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1513 nts = burm_nts[nextRule];
1514 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
1515 }
1516 else
1517 {
1518 switch(ruleForNode) {
1519 case 1: // stmt: Ret
1520 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001521 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001522 // for moving return value to appropriate register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001523 // Copy the return value to the required return register.
1524 // Mark the return Value as an implicit ref of the RET instr..
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001525 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001526 // Finally put a NOP in the delay slot.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001527 ReturnInst *returnInstr=cast<ReturnInst>(subtreeRoot->getInstruction());
1528 Value* retVal = returnInstr->getReturnValue();
1529 MachineCodeForInstruction& mcfi =
1530 MachineCodeForInstruction::get(returnInstr);
1531
1532 // Create a hidden virtual reg to represent the return address register
1533 // used by the machine instruction but not represented in LLVM.
1534 //
1535 Instruction* returnAddrTmp = new TmpInstruction(mcfi, returnInstr);
1536
1537 MachineInstr* retMI =
1538 BuildMI(V9::JMPLRETi, 3).addReg(returnAddrTmp).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001539 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001540
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001541 // If there is a value to return, we need to:
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001542 // (a) Sign-extend the value if it is smaller than 8 bytes (reg size)
1543 // (b) Insert a copy to copy the return value to the appropriate reg.
1544 // -- For FP values, create a FMOVS or FMOVD instruction
1545 // -- For non-FP values, create an add-with-0 instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001546 //
1547 if (retVal != NULL) {
1548 const UltraSparcRegInfo& regInfo =
1549 (UltraSparcRegInfo&) target.getRegInfo();
1550 const Type* retType = retVal->getType();
1551 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
1552 unsigned retRegNum = (retType->isFloatingPoint()
1553 ? (unsigned) SparcFloatRegClass::f0
1554 : (unsigned) SparcIntRegClass::i0);
1555 retRegNum = regInfo.getUnifiedRegNum(regClassID, retRegNum);
1556
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001557 // () Insert sign-extension instructions for small signed values.
1558 //
1559 Value* retValToUse = retVal;
1560 if (retType->isIntegral() && retType->isSigned()) {
1561 unsigned retSize = target.getTargetData().getTypeSize(retType);
1562 if (retSize <= 4) {
1563 // create a temporary virtual reg. to hold the sign-extension
1564 retValToUse = new TmpInstruction(mcfi, retVal);
1565
1566 // sign-extend retVal and put the result in the temporary reg.
1567 target.getInstrInfo().CreateSignExtensionInstructions
1568 (target, returnInstr->getParent()->getParent(),
1569 retVal, retValToUse, 8*retSize, mvec, mcfi);
1570 }
1571 }
1572
1573 // (b) Now, insert a copy to to the appropriate register:
1574 // -- For FP values, create a FMOVS or FMOVD instruction
1575 // -- For non-FP values, create an add-with-0 instruction
1576 //
1577 // First, create a virtual register to represent the register and
1578 // mark this vreg as being an implicit operand of the ret MI.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001579 TmpInstruction* retVReg =
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001580 new TmpInstruction(mcfi, retValToUse, NULL, "argReg");
1581
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001582 retMI->addImplicitRef(retVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001583
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001584 if (retType->isFloatingPoint())
1585 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001586 .addReg(retValToUse).addReg(retVReg, MOTy::Def));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001587 else
1588 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001589 .addReg(retValToUse).addSImm((int64_t) 0)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001590 .addReg(retVReg, MOTy::Def));
1591
1592 // Mark the operand with the register it should be assigned
1593 M->SetRegForOperand(M->getNumOperands()-1, retRegNum);
1594 retMI->SetRegForImplicitRef(retMI->getNumImplicitRefs()-1, retRegNum);
1595
1596 mvec.push_back(M);
1597 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001598
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001599 // Now insert the RET instruction and a NOP for the delay slot
1600 mvec.push_back(retMI);
Misha Brukmana98cd452003-05-20 20:32:24 +00001601 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001602
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001603 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001604 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001605
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001606 case 3: // stmt: Store(reg,reg)
1607 case 4: // stmt: Store(reg,ptrreg)
1608 SetOperandsForMemInstr(ChooseStoreInstruction(
Chris Lattner54e898e2003-01-15 19:23:34 +00001609 subtreeRoot->leftChild()->getValue()->getType()),
1610 mvec, subtreeRoot, target);
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001611 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001612
1613 case 5: // stmt: BrUncond
1614 {
1615 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1616 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
1617
1618 // delay slot
1619 mvec.push_back(BuildMI(V9::NOP, 0));
1620 break;
1621 }
1622
1623 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001624 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001625 // If the constant is ZERO, we can use the branch-on-integer-register
1626 // instructions and avoid the SUBcc instruction entirely.
1627 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001628 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001629 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1630 assert(constNode &&
1631 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001632 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001633 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001634
Chris Lattner0c4e8862002-09-03 01:08:28 +00001635 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001636 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001637 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1638 && isValidConst)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001639 {
1640 // That constant is a zero after all...
1641 // Use the left child of setCC as the first argument!
1642 // Mark the setCC node so that no code is generated for it.
1643 InstructionNode* setCCNode = (InstructionNode*)
1644 subtreeRoot->leftChild();
1645 assert(setCCNode->getOpLabel() == SetCCOp);
1646 setCCNode->markFoldedIntoParent();
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001647
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001648 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001649
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001650 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1651 .addReg(setCCNode->leftChild()->getValue())
1652 .addPCDisp(brInst->getSuccessor(0));
1653 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001654
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001655 // delay slot
1656 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001657
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001658 // false branch
1659 mvec.push_back(BuildMI(V9::BA, 1)
1660 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001661
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001662 // delay slot
1663 mvec.push_back(BuildMI(V9::NOP, 0));
1664 break;
1665 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001666 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001667 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001668
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001669 case 6: // stmt: BrCond(setCC)
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001670 { // bool => boolean was computed with SetCC.
1671 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001672 // If it is an integer CC, we also need to find the unique
1673 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001674 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001675 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +00001676 const Type* setCCType;
1677 unsigned Opcode = ChooseBccInstruction(subtreeRoot, setCCType);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001678 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1679 brInst->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00001680 setCCType,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001681 MachineCodeForInstruction::get(brInst));
Chris Lattner54e898e2003-01-15 19:23:34 +00001682 M = BuildMI(Opcode, 2).addCCReg(ccValue)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001683 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001684 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001685
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001686 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001687 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001688
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001689 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001690 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001691
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001692 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001693 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001694 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001695 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001696
1697 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001698 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001699 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001700 Constant* constVal =
1701 cast<Constant>(subtreeRoot->leftChild()->getValue());
1702 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001703
Misha Brukmana98cd452003-05-20 20:32:24 +00001704 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001705 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001706 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001707
1708 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001709 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001710 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001711 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001712
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001713 case 8: // stmt: BrCond(boolreg)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001714 { // boolreg => boolean is recorded in an integer register.
1715 // Use branch-on-integer-register instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001716 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001717 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001718 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001719 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001720 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001721
1722 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001723 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001724
1725 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001726 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001727
1728 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001729 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001730 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001731 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001732
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001733 case 9: // stmt: Switch(reg)
1734 assert(0 && "*** SWITCH instruction is not implemented yet.");
1735 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001736
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001737 case 10: // reg: VRegList(reg, reg)
1738 assert(0 && "VRegList should never be the topmost non-chain rule");
1739 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001740
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001741 case 21: // bool: Not(bool,reg): Compute with a conditional-move-on-reg
1742 { // First find the unary operand. It may be left or right, usually right.
1743 Instruction* notI = subtreeRoot->getInstruction();
1744 Value* notArg = BinaryOperator::getNotArgument(
1745 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1746 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1747
1748 // Unconditionally set register to 0
1749 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(notI));
1750
1751 // Now conditionally move 1 into the register.
1752 // Mark the register as a use (as well as a def) because the old
1753 // value will be retained if the condition is false.
1754 mvec.push_back(BuildMI(V9::MOVRZi, 3).addReg(notArg).addZImm(1)
1755 .addReg(notI, MOTy::UseAndDef));
1756
1757 break;
1758 }
1759
1760 case 421: // reg: BNot(reg,reg): Compute as reg = reg XOR-NOT 0
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001761 { // First find the unary operand. It may be left or right, usually right.
1762 Value* notArg = BinaryOperator::getNotArgument(
1763 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001764 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001765 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001766 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001767 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001768 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001769
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001770 case 322: // reg: Not(tobool, reg):
1771 // Fold CAST-TO-BOOL with NOT by inverting the sense of cast-to-bool
1772 foldCase = true;
1773 // Just fall through!
1774
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001775 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001776 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001777 Instruction* castI = subtreeRoot->getInstruction();
1778 Value* opVal = subtreeRoot->leftChild()->getValue();
1779 assert(opVal->getType()->isIntegral() ||
1780 isa<PointerType>(opVal->getType()));
1781
1782 // Unconditionally set register to 0
1783 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(castI));
1784
1785 // Now conditionally move 1 into the register.
1786 // Mark the register as a use (as well as a def) because the old
1787 // value will be retained if the condition is false.
1788 MachineOpCode opCode = foldCase? V9::MOVRZi : V9::MOVRNZi;
1789 mvec.push_back(BuildMI(opCode, 3).addReg(opVal).addZImm(1)
1790 .addReg(castI, MOTy::UseAndDef));
1791
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001792 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001793 }
1794
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001795 case 23: // reg: ToUByteTy(reg)
1796 case 24: // reg: ToSByteTy(reg)
1797 case 25: // reg: ToUShortTy(reg)
1798 case 26: // reg: ToShortTy(reg)
1799 case 27: // reg: ToUIntTy(reg)
1800 case 28: // reg: ToIntTy(reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001801 case 29: // reg: ToULongTy(reg)
1802 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001803 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001804 //======================================================================
1805 // Rules for integer conversions:
1806 //
1807 //--------
1808 // From ISO 1998 C++ Standard, Sec. 4.7:
1809 //
1810 // 2. If the destination type is unsigned, the resulting value is
1811 // the least unsigned integer congruent to the source integer
1812 // (modulo 2n where n is the number of bits used to represent the
1813 // unsigned type). [Note: In a two s complement representation,
1814 // this conversion is conceptual and there is no change in the
1815 // bit pattern (if there is no truncation). ]
1816 //
1817 // 3. If the destination type is signed, the value is unchanged if
1818 // it can be represented in the destination type (and bitfield width);
1819 // otherwise, the value is implementation-defined.
1820 //--------
1821 //
1822 // Since we assume 2s complement representations, this implies:
1823 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001824 // -- If operand is smaller than destination, zero-extend or sign-extend
1825 // according to the signedness of the *operand*: source decides:
1826 // (1) If operand is signed, sign-extend it.
1827 // If dest is unsigned, zero-ext the result!
1828 // (2) If operand is unsigned, our current invariant is that
1829 // it's high bits are correct, so zero-extension is not needed.
Vikram S. Adve94c40812002-09-27 14:33:08 +00001830 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001831 // -- If operand is same size as or larger than destination,
1832 // zero-extend or sign-extend according to the signedness of
1833 // the *destination*: destination decides:
1834 // (1) If destination is signed, sign-extend (truncating if needed)
1835 // This choice is implementation defined. We sign-extend the
1836 // operand, which matches both Sun's cc and gcc3.2.
1837 // (2) If destination is unsigned, zero-extend (truncating if needed)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001838 //======================================================================
1839
Vikram S. Adve242a8082002-05-19 15:25:51 +00001840 Instruction* destI = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001841 Function* currentFunc = destI->getParent()->getParent();
1842 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(destI);
1843
Vikram S. Adve242a8082002-05-19 15:25:51 +00001844 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001845 const Type* opType = opVal->getType();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001846 const Type* destType = destI->getType();
1847 unsigned opSize = target.getTargetData().getTypeSize(opType);
1848 unsigned destSize = target.getTargetData().getTypeSize(destType);
1849
1850 bool isIntegral = opType->isIntegral() || isa<PointerType>(opType);
1851
1852 if (opType == Type::BoolTy ||
1853 opType == destType ||
1854 isIntegral && opSize == destSize && opSize == 8) {
1855 // nothing to do in all these cases
1856 forwardOperandNum = 0; // forward first operand to user
1857
Misha Brukman7b647942003-05-30 20:11:56 +00001858 } else if (opType->isFloatingPoint()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001859
1860 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Misha Brukman7b647942003-05-30 20:11:56 +00001861 if (destI->getType()->isUnsigned())
1862 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve1e606692002-07-31 21:01:34 +00001863
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001864 } else if (isIntegral) {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001865
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001866 bool opSigned = opType->isSigned();
1867 bool destSigned = destType->isSigned();
1868 unsigned extSourceInBits = 8 * std::min<unsigned>(opSize, destSize);
1869
1870 assert(! (opSize == destSize && opSigned == destSigned) &&
1871 "How can different int types have same size and signedness?");
1872
1873 bool signExtend = (opSize < destSize && opSigned ||
1874 opSize >= destSize && destSigned);
1875
1876 bool signAndZeroExtend = (opSize < destSize && destSize < 8u &&
1877 opSigned && !destSigned);
1878 assert(!signAndZeroExtend || signExtend);
1879
1880 bool zeroExtendOnly = opSize >= destSize && !destSigned;
1881 assert(!zeroExtendOnly || !signExtend);
1882
1883 if (signExtend) {
1884 Value* signExtDest = (signAndZeroExtend
1885 ? new TmpInstruction(mcfi, destType, opVal)
1886 : destI);
1887
1888 target.getInstrInfo().CreateSignExtensionInstructions
1889 (target, currentFunc,opVal,signExtDest,extSourceInBits,mvec,mcfi);
1890
1891 if (signAndZeroExtend)
1892 target.getInstrInfo().CreateZeroExtensionInstructions
1893 (target, currentFunc, signExtDest, destI, 8*destSize, mvec, mcfi);
1894 }
1895 else if (zeroExtendOnly) {
1896 target.getInstrInfo().CreateZeroExtensionInstructions
1897 (target, currentFunc, opVal, destI, extSourceInBits, mvec, mcfi);
1898 }
1899 else
1900 forwardOperandNum = 0; // forward first operand to user
1901
Misha Brukman7b647942003-05-30 20:11:56 +00001902 } else
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001903 assert(0 && "Unrecognized operand type for convert-to-integer");
1904
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001905 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001906 }
1907
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001908 case 31: // reg: ToFloatTy(reg):
1909 case 32: // reg: ToDoubleTy(reg):
1910 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001911
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001912 // If this instruction has a parent (a user) in the tree
1913 // and the user is translated as an FsMULd instruction,
1914 // then the cast is unnecessary. So check that first.
1915 // In the future, we'll want to do the same for the FdMULq instruction,
1916 // so do the check here instead of only for ToFloatTy(reg).
1917 //
1918 if (subtreeRoot->parent() != NULL) {
1919 const MachineCodeForInstruction& mcfi =
1920 MachineCodeForInstruction::get(
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001921 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001922 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
1923 forwardOperandNum = 0; // forward first operand to user
1924 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001925
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001926 if (forwardOperandNum != 0) { // we do need the cast
1927 Value* leftVal = subtreeRoot->leftChild()->getValue();
1928 const Type* opType = leftVal->getType();
1929 MachineOpCode opCode=ChooseConvertToFloatInstr(
1930 subtreeRoot->getOpLabel(), opType);
1931 if (opCode == V9::INVALID_OPCODE) { // no conversion needed
1932 forwardOperandNum = 0; // forward first operand to user
1933 } else {
1934 // If the source operand is a non-FP type it must be
1935 // first copied from int to float register via memory!
1936 Instruction *dest = subtreeRoot->getInstruction();
1937 Value* srcForCast;
1938 int n = 0;
1939 if (! opType->isFloatingPoint()) {
1940 // Create a temporary to represent the FP register
1941 // into which the integer will be copied via memory.
1942 // The type of this temporary will determine the FP
1943 // register used: single-prec for a 32-bit int or smaller,
1944 // double-prec for a 64-bit int.
1945 //
1946 uint64_t srcSize =
1947 target.getTargetData().getTypeSize(leftVal->getType());
1948 Type* tmpTypeToUse =
1949 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001950 MachineCodeForInstruction &destMCFI =
1951 MachineCodeForInstruction::get(dest);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001952 srcForCast = new TmpInstruction(destMCFI, tmpTypeToUse, dest);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001953
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001954 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001955 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001956 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001957 mvec, destMCFI);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001958 } else
1959 srcForCast = leftVal;
1960
1961 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
1962 mvec.push_back(M);
1963 }
Misha Brukman7b647942003-05-30 20:11:56 +00001964 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001965 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001966
1967 case 19: // reg: ToArrayTy(reg):
1968 case 20: // reg: ToPointerTy(reg):
1969 forwardOperandNum = 0; // forward first operand to user
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001970 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001971
1972 case 233: // reg: Add(reg, Constant)
1973 maskUnsignedResult = true;
1974 M = CreateAddConstInstruction(subtreeRoot);
1975 if (M != NULL) {
1976 mvec.push_back(M);
1977 break;
1978 }
1979 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001980
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001981 case 33: // reg: Add(reg, reg)
1982 maskUnsignedResult = true;
1983 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
1984 break;
1985
1986 case 234: // reg: Sub(reg, Constant)
1987 maskUnsignedResult = true;
1988 M = CreateSubConstInstruction(subtreeRoot);
1989 if (M != NULL) {
1990 mvec.push_back(M);
1991 break;
1992 }
1993 // ELSE FALL THROUGH
1994
1995 case 34: // reg: Sub(reg, reg)
1996 maskUnsignedResult = true;
1997 Add3OperandInstr(ChooseSubInstructionByType(
Chris Lattner54e898e2003-01-15 19:23:34 +00001998 subtreeRoot->getInstruction()->getType()),
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001999 subtreeRoot, mvec);
2000 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002001
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002002 case 135: // reg: Mul(todouble, todouble)
2003 checkCast = true;
2004 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002005
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002006 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002007 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002008 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002009 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002010 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002011 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002012 Instruction* mulInstr = subtreeRoot->getInstruction();
2013 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002014 subtreeRoot->leftChild()->getValue(),
2015 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002016 mulInstr, mvec,
2017 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002018 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002019 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002020 case 335: // reg: Mul(todouble, todoubleConst)
2021 checkCast = true;
2022 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002023
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002024 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00002025 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002026 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002027 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002028 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002029 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002030 Instruction* mulInstr = subtreeRoot->getInstruction();
2031 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002032 subtreeRoot->leftChild()->getValue(),
2033 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002034 mulInstr, mvec,
2035 MachineCodeForInstruction::get(mulInstr),
2036 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002037 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002038 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002039 case 236: // reg: Div(reg, Constant)
2040 maskUnsignedResult = true;
2041 L = mvec.size();
2042 CreateDivConstInstruction(target, subtreeRoot, mvec);
2043 if (mvec.size() > L)
2044 break;
2045 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002046
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002047 case 36: // reg: Div(reg, reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002048 {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002049 maskUnsignedResult = true;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002050
2051 // If second operand of divide is smaller than 64 bits, we have
2052 // to make sure the unused top bits are correct because they affect
2053 // the result. These bits are already correct for unsigned values.
2054 // They may be incorrect for signed values, so sign extend to fill in.
2055 Instruction* divI = subtreeRoot->getInstruction();
2056 Value* divOp2 = subtreeRoot->rightChild()->getValue();
2057 Value* divOpToUse = divOp2;
2058 if (divOp2->getType()->isSigned()) {
2059 unsigned opSize=target.getTargetData().getTypeSize(divOp2->getType());
2060 if (opSize < 8) {
2061 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(divI);
2062 divOpToUse = new TmpInstruction(mcfi, divOp2);
2063 target.getInstrInfo().
2064 CreateSignExtensionInstructions(target,
2065 divI->getParent()->getParent(),
2066 divOp2, divOpToUse,
2067 8*opSize, mvec, mcfi);
2068 }
2069 }
2070
2071 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
2072 .addReg(subtreeRoot->leftChild()->getValue())
2073 .addReg(divOpToUse)
2074 .addRegDef(divI));
2075
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002076 break;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002077 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002078
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002079 case 37: // reg: Rem(reg, reg)
2080 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00002081 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002082 maskUnsignedResult = true;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002083
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002084 Instruction* remI = subtreeRoot->getInstruction();
2085 Value* divOp1 = subtreeRoot->leftChild()->getValue();
2086 Value* divOp2 = subtreeRoot->rightChild()->getValue();
2087
2088 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(remI);
Vikram S. Adve510eec72001-11-04 21:59:14 +00002089
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002090 // If second operand of divide is smaller than 64 bits, we have
2091 // to make sure the unused top bits are correct because they affect
2092 // the result. These bits are already correct for unsigned values.
2093 // They may be incorrect for signed values, so sign extend to fill in.
2094 //
2095 Value* divOpToUse = divOp2;
2096 if (divOp2->getType()->isSigned()) {
2097 unsigned opSize=target.getTargetData().getTypeSize(divOp2->getType());
2098 if (opSize < 8) {
2099 divOpToUse = new TmpInstruction(mcfi, divOp2);
2100 target.getInstrInfo().
2101 CreateSignExtensionInstructions(target,
2102 remI->getParent()->getParent(),
2103 divOp2, divOpToUse,
2104 8*opSize, mvec, mcfi);
2105 }
2106 }
2107
2108 // Now compute: result = rem V1, V2 as:
2109 // result = V1 - (V1 / signExtend(V2)) * signExtend(V2)
2110 //
2111 TmpInstruction* quot = new TmpInstruction(mcfi, divOp1, divOpToUse);
2112 TmpInstruction* prod = new TmpInstruction(mcfi, quot, divOpToUse);
2113
2114 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
2115 .addReg(divOp1).addReg(divOpToUse).addRegDef(quot));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002116
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002117 mvec.push_back(BuildMI(ChooseMulInstructionByType(remI->getType()), 3)
2118 .addReg(quot).addReg(divOpToUse).addRegDef(prod));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002119
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002120 mvec.push_back(BuildMI(ChooseSubInstructionByType(remI->getType()), 3)
2121 .addReg(divOp1).addReg(prod).addRegDef(remI));
2122
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002123 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00002124 }
2125
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002126 case 38: // bool: And(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002127 case 138: // bool: And(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002128 case 238: // bool: And(bool, boolconst)
2129 case 338: // reg : BAnd(reg, reg)
2130 case 538: // reg : BAnd(reg, Constant)
2131 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
2132 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002133
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002134 case 438: // bool: BAnd(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002135 { // Use the argument of NOT as the second argument!
2136 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002137 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002138 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2139 Value* notArg = BinaryOperator::getNotArgument(
2140 cast<BinaryOperator>(notNode->getInstruction()));
2141 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002142 Value *lhs = subtreeRoot->leftChild()->getValue();
2143 Value *dest = subtreeRoot->getValue();
2144 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(lhs).addReg(notArg)
2145 .addReg(dest, MOTy::Def));
2146
2147 if (notArg->getType() == Type::BoolTy)
2148 { // set 1 in result register if result of above is non-zero
2149 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2150 .addReg(dest, MOTy::UseAndDef));
2151 }
2152
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002153 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002154 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002155
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002156 case 39: // bool: Or(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002157 case 139: // bool: Or(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002158 case 239: // bool: Or(bool, boolconst)
2159 case 339: // reg : BOr(reg, reg)
2160 case 539: // reg : BOr(reg, Constant)
2161 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
2162 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002163
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002164 case 439: // bool: BOr(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002165 { // Use the argument of NOT as the second argument!
2166 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002167 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002168 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2169 Value* notArg = BinaryOperator::getNotArgument(
2170 cast<BinaryOperator>(notNode->getInstruction()));
2171 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002172 Value *lhs = subtreeRoot->leftChild()->getValue();
2173 Value *dest = subtreeRoot->getValue();
2174
2175 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(lhs).addReg(notArg)
2176 .addReg(dest, MOTy::Def));
2177
2178 if (notArg->getType() == Type::BoolTy)
2179 { // set 1 in result register if result of above is non-zero
2180 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2181 .addReg(dest, MOTy::UseAndDef));
2182 }
2183
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002184 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002185 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002186
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002187 case 40: // bool: Xor(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002188 case 140: // bool: Xor(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002189 case 240: // bool: Xor(bool, boolconst)
2190 case 340: // reg : BXor(reg, reg)
2191 case 540: // reg : BXor(reg, Constant)
2192 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
2193 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002194
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002195 case 440: // bool: BXor(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002196 { // Use the argument of NOT as the second argument!
2197 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002198 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002199 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2200 Value* notArg = BinaryOperator::getNotArgument(
2201 cast<BinaryOperator>(notNode->getInstruction()));
2202 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002203 Value *lhs = subtreeRoot->leftChild()->getValue();
2204 Value *dest = subtreeRoot->getValue();
2205 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(lhs).addReg(notArg)
2206 .addReg(dest, MOTy::Def));
2207
2208 if (notArg->getType() == Type::BoolTy)
2209 { // set 1 in result register if result of above is non-zero
2210 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2211 .addReg(dest, MOTy::UseAndDef));
2212 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002213 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002214 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002215
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002216 case 41: // setCCconst: SetCC(reg, Constant)
2217 { // Comparison is with a constant:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002218 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002219 // If the bool result must be computed into a register (see below),
2220 // and the constant is int ZERO, we can use the MOVR[op] instructions
2221 // and avoid the SUBcc instruction entirely.
2222 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002223 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002224 // The result of the SetCC must be computed and stored in a register if
2225 // it is used outside the current basic block (so it must be computed
2226 // as a boolreg) or it is used by anything other than a branch.
2227 // We will use a conditional move to do this.
2228 //
2229 Instruction* setCCInstr = subtreeRoot->getInstruction();
2230 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2231 ! AllUsesAreBranches(setCCInstr));
2232
2233 if (computeBoolVal)
2234 {
2235 InstrTreeNode* constNode = subtreeRoot->rightChild();
2236 assert(constNode &&
2237 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
2238 Constant *constVal = cast<Constant>(constNode->getValue());
2239 bool isValidConst;
2240
2241 if ((constVal->getType()->isInteger()
2242 || isa<PointerType>(constVal->getType()))
2243 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
2244 && isValidConst)
2245 {
2246 // That constant is an integer zero after all...
2247 // Use a MOVR[op] to compute the boolean result
2248 // Unconditionally set register to 0
2249 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0)
2250 .addRegDef(setCCInstr));
2251
2252 // Now conditionally move 1 into the register.
2253 // Mark the register as a use (as well as a def) because the old
2254 // value will be retained if the condition is false.
2255 MachineOpCode movOpCode = ChooseMovpregiForSetCC(subtreeRoot);
2256 mvec.push_back(BuildMI(movOpCode, 3)
2257 .addReg(subtreeRoot->leftChild()->getValue())
2258 .addZImm(1).addReg(setCCInstr, MOTy::UseAndDef));
2259
2260 break;
2261 }
2262 }
2263 // ELSE FALL THROUGH
2264 }
2265
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002266 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002267 {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002268 // This generates a SUBCC instruction, putting the difference in a
2269 // result reg. if needed, and/or setting a condition code if needed.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002270 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002271 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002272 Value* leftVal = subtreeRoot->leftChild()->getValue();
2273 Value* rightVal = subtreeRoot->rightChild()->getValue();
2274 const Type* opType = leftVal->getType();
2275 bool isFPCompare = opType->isFloatingPoint();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002276
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002277 // If the boolean result of the SetCC is used outside the current basic
2278 // block (so it must be computed as a boolreg) or is used by anything
2279 // other than a branch, the boolean must be computed and stored
2280 // in a result register. We will use a conditional move to do this.
2281 //
2282 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2283 ! AllUsesAreBranches(setCCInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002284
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002285 // A TmpInstruction is created to represent the CC "result".
2286 // Unlike other instances of TmpInstruction, this one is used
2287 // by machine code of multiple LLVM instructions, viz.,
2288 // the SetCC and the branch. Make sure to get the same one!
2289 // Note that we do this even for FP CC registers even though they
2290 // are explicit operands, because the type of the operand
2291 // needs to be a floating point condition code, not an integer
2292 // condition code. Think of this as casting the bool result to
2293 // a FP condition code register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002294 // Later, we mark the 4th operand as being a CC register, and as a def.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002295 //
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002296 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002297 setCCInstr->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00002298 leftVal->getType(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002299 MachineCodeForInstruction::get(setCCInstr));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002300
2301 // If the operands are signed values smaller than 4 bytes, then they
2302 // must be sign-extended in order to do a valid 32-bit comparison
2303 // and get the right result in the 32-bit CC register (%icc).
2304 //
2305 Value* leftOpToUse = leftVal;
2306 Value* rightOpToUse = rightVal;
2307 if (opType->isIntegral() && opType->isSigned()) {
2308 unsigned opSize = target.getTargetData().getTypeSize(opType);
2309 if (opSize < 4) {
2310 MachineCodeForInstruction& mcfi =
2311 MachineCodeForInstruction::get(setCCInstr);
2312
2313 // create temporary virtual regs. to hold the sign-extensions
2314 leftOpToUse = new TmpInstruction(mcfi, leftVal);
2315 rightOpToUse = new TmpInstruction(mcfi, rightVal);
2316
2317 // sign-extend each operand and put the result in the temporary reg.
2318 target.getInstrInfo().CreateSignExtensionInstructions
2319 (target, setCCInstr->getParent()->getParent(),
2320 leftVal, leftOpToUse, 8*opSize, mvec, mcfi);
2321 target.getInstrInfo().CreateSignExtensionInstructions
2322 (target, setCCInstr->getParent()->getParent(),
2323 rightVal, rightOpToUse, 8*opSize, mvec, mcfi);
2324 }
2325 }
2326
Misha Brukman7b647942003-05-30 20:11:56 +00002327 if (! isFPCompare) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002328 // Integer condition: set CC and discard result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002329 mvec.push_back(BuildMI(V9::SUBccr, 4)
2330 .addReg(leftOpToUse)
2331 .addReg(rightOpToUse)
2332 .addMReg(target.getRegInfo().getZeroRegNum(),MOTy::Def)
2333 .addCCReg(tmpForCC, MOTy::Def));
Misha Brukman7b647942003-05-30 20:11:56 +00002334 } else {
2335 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002336 mvec.push_back(BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2337 .addCCReg(tmpForCC, MOTy::Def)
2338 .addReg(leftOpToUse)
2339 .addReg(rightOpToUse));
Misha Brukman7b647942003-05-30 20:11:56 +00002340 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002341
Misha Brukman7b647942003-05-30 20:11:56 +00002342 if (computeBoolVal) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002343 MachineOpCode movOpCode = (isFPCompare
Misha Brukmaneecdb662003-06-02 20:55:14 +00002344 ? ChooseMovFpcciInstruction(subtreeRoot)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002345 : ChooseMovpcciForSetCC(subtreeRoot));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002346
2347 // Unconditionally set register to 0
2348 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
2349 mvec.push_back(M);
2350
2351 // Now conditionally move 1 into the register.
Misha Brukman7b647942003-05-30 20:11:56 +00002352 // Mark the register as a use (as well as a def) because the old
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002353 // value will be retained if the condition is false.
2354 M = (BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(1)
2355 .addReg(setCCInstr, MOTy::UseAndDef));
Misha Brukman7b647942003-05-30 20:11:56 +00002356 mvec.push_back(M);
2357 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002358 break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002359 }
2360
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002361 case 51: // reg: Load(reg)
2362 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002363 SetOperandsForMemInstr(ChooseLoadInstruction(
2364 subtreeRoot->getValue()->getType()),
2365 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002366 break;
2367
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002368 case 55: // reg: GetElemPtr(reg)
2369 case 56: // reg: GetElemPtrIdx(reg,reg)
2370 // If the GetElemPtr was folded into the user (parent), it will be
2371 // caught above. For other cases, we have to compute the address.
2372 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
2373 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002374
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002375 case 57: // reg: Alloca: Implement as 1 instruction:
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002376 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002377 AllocationInst* instr =
2378 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002379 unsigned tsize =
2380 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002381 assert(tsize != 0);
2382 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002383 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002384 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002385
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002386 case 58: // reg: Alloca(reg): Implement as 3 instructions:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002387 // mul num, typeSz -> tmp
2388 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002389 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002390 AllocationInst* instr =
2391 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002392 const Type* eltType = instr->getAllocatedType();
2393
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002394 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002395 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002396 Value* numElementsVal = NULL;
2397 bool isArray = instr->isArrayAllocation();
2398
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002399 if (!isArray || isa<Constant>(numElementsVal = instr->getArraySize())) {
Misha Brukman7b647942003-05-30 20:11:56 +00002400 // total size is constant: generate code for fixed-size alloca
2401 unsigned numElements = isArray?
2402 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2403 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2404 numElements, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002405 } else {
2406 // total size is not constant.
Vikram S. Adve74825322002-03-18 03:15:35 +00002407 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002408 numElementsVal, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002409 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002410 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002411 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002412
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002413 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002414 { // Generate a direct (CALL) or indirect (JMPL) call.
2415 // Mark the return-address register, the indirection
2416 // register (for indirect calls), the operands of the Call,
2417 // and the return value (if any) as implicit operands
2418 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002419 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002420 // If this is a varargs function, floating point arguments
2421 // have to passed in integer registers so insert
2422 // copy-float-to-int instructions for each float operand.
2423 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002424 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002425 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002426 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002427
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002428 // Check if this is an intrinsic function that needs a special code
2429 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002430 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002431 bool specialIntrinsic = false;
2432 LLVMIntrinsic::ID iid;
2433 if (calledFunc && (iid=(LLVMIntrinsic::ID)calledFunc->getIntrinsicID()))
2434 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002435
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002436 // If not, generate the normal call sequence for the function.
2437 // This can also handle any intrinsics that are just function calls.
2438 //
Misha Brukman7b647942003-05-30 20:11:56 +00002439 if (! specialIntrinsic) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002440 Function* currentFunc = callInstr->getParent()->getParent();
2441 MachineFunction& MF = MachineFunction::get(currentFunc);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002442 MachineCodeForInstruction& mcfi =
2443 MachineCodeForInstruction::get(callInstr);
2444 const UltraSparcRegInfo& regInfo =
2445 (UltraSparcRegInfo&) target.getRegInfo();
2446 const TargetFrameInfo& frameInfo = target.getFrameInfo();
2447
Misha Brukman7b647942003-05-30 20:11:56 +00002448 // Create hidden virtual register for return address with type void*
2449 TmpInstruction* retAddrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002450 new TmpInstruction(mcfi, PointerType::get(Type::VoidTy), callInstr);
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002451
Misha Brukman7b647942003-05-30 20:11:56 +00002452 // Generate the machine instruction and its operands.
2453 // Use CALL for direct function calls; this optimistically assumes
2454 // the PC-relative address fits in the CALL address field (22 bits).
2455 // Use JMPL for indirect calls.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002456 // This will be added to mvec later, after operand copies.
Misha Brukman7b647942003-05-30 20:11:56 +00002457 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002458 MachineInstr* callMI;
Misha Brukman7b647942003-05-30 20:11:56 +00002459 if (calledFunc) // direct function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002460 callMI = BuildMI(V9::CALL, 1).addPCDisp(callee);
Misha Brukman7b647942003-05-30 20:11:56 +00002461 else // indirect function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002462 callMI = (BuildMI(V9::JMPLCALLi,3).addReg(callee)
2463 .addSImm((int64_t)0).addRegDef(retAddrReg));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002464
Misha Brukman7b647942003-05-30 20:11:56 +00002465 const FunctionType* funcType =
2466 cast<FunctionType>(cast<PointerType>(callee->getType())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002467 ->getElementType());
Misha Brukman7b647942003-05-30 20:11:56 +00002468 bool isVarArgs = funcType->isVarArg();
2469 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002470
Misha Brukman7b647942003-05-30 20:11:56 +00002471 // Use a descriptor to pass information about call arguments
2472 // to the register allocator. This descriptor will be "owned"
2473 // and freed automatically when the MachineCodeForInstruction
2474 // object for the callInstr goes away.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002475 CallArgsDescriptor* argDesc =
2476 new CallArgsDescriptor(callInstr, retAddrReg,isVarArgs,noPrototype);
Misha Brukman7b647942003-05-30 20:11:56 +00002477 assert(callInstr->getOperand(0) == callee
2478 && "This is assumed in the loop below!");
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002479
2480 // Insert sign-extension instructions for small signed values,
2481 // if this is an unknown function (i.e., called via a funcptr)
2482 // or an external one (i.e., which may not be compiled by llc).
2483 //
2484 if (calledFunc == NULL || calledFunc->isExternal()) {
2485 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
2486 Value* argVal = callInstr->getOperand(i);
2487 const Type* argType = argVal->getType();
2488 if (argType->isIntegral() && argType->isSigned()) {
2489 unsigned argSize = target.getTargetData().getTypeSize(argType);
2490 if (argSize <= 4) {
2491 // create a temporary virtual reg. to hold the sign-extension
2492 TmpInstruction* argExtend = new TmpInstruction(mcfi, argVal);
2493
2494 // sign-extend argVal and put the result in the temporary reg.
2495 target.getInstrInfo().CreateSignExtensionInstructions
2496 (target, currentFunc, argVal, argExtend,
2497 8*argSize, mvec, mcfi);
2498
2499 // replace argVal with argExtend in CallArgsDescriptor
2500 argDesc->getArgInfo(i-1).replaceArgVal(argExtend);
2501 }
2502 }
2503 }
2504 }
2505
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002506 // Insert copy instructions to get all the arguments into
2507 // all the places that they need to be.
2508 //
Misha Brukman7b647942003-05-30 20:11:56 +00002509 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002510 int argNo = i-1;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002511 CallArgInfo& argInfo = argDesc->getArgInfo(argNo);
2512 Value* argVal = argInfo.getArgVal(); // don't use callInstr arg here
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002513 const Type* argType = argVal->getType();
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002514 unsigned regType = regInfo.getRegTypeForDataType(argType);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002515 unsigned argSize = target.getTargetData().getTypeSize(argType);
2516 int regNumForArg = TargetRegInfo::getInvalidRegNum();
2517 unsigned regClassIDOfArgReg;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002518
Misha Brukman7b647942003-05-30 20:11:56 +00002519 // Check for FP arguments to varargs functions.
2520 // Any such argument in the first $K$ args must be passed in an
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002521 // integer register. If there is no prototype, it must also
2522 // be passed as an FP register.
2523 // K = #integer argument registers.
2524 bool isFPArg = argVal->getType()->isFloatingPoint();
2525 if (isVarArgs && isFPArg) {
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002526
2527 if (noPrototype) {
2528 // It is a function with no prototype: pass value
2529 // as an FP value as well as a varargs value. The FP value
2530 // may go in a register or on the stack. The copy instruction
2531 // to the outgoing reg/stack is created by the normal argument
2532 // handling code since this is the "normal" passing mode.
2533 //
2534 regNumForArg = regInfo.regNumForFPArg(regType,
2535 false, false, argNo,
2536 regClassIDOfArgReg);
2537 if (regNumForArg == regInfo.getInvalidRegNum())
2538 argInfo.setUseStackSlot();
2539 else
2540 argInfo.setUseFPArgReg();
2541 }
2542
2543 // If this arg. is in the first $K$ regs, add special copy-
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002544 // float-to-int instructions to pass the value as an int.
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002545 // To check if it is in the first $K$, get the register
2546 // number for the arg #i. These copy instructions are
2547 // generated here because they are extra cases and not needed
2548 // for the normal argument handling (some code reuse is
2549 // possible though -- later).
2550 //
Misha Brukmanea481cc2003-06-03 03:21:58 +00002551 int copyRegNum = regInfo.regNumForIntArg(false, false, argNo,
2552 regClassIDOfArgReg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002553 if (copyRegNum != regInfo.getInvalidRegNum()) {
2554 // Create a virtual register to represent copyReg. Mark
2555 // this vreg as being an implicit operand of the call MI
2556 const Type* loadTy = (argType == Type::FloatTy
2557 ? Type::IntTy : Type::LongTy);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002558 TmpInstruction* argVReg = new TmpInstruction(mcfi, loadTy,
2559 argVal, NULL,
2560 "argRegCopy");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002561 callMI->addImplicitRef(argVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002562
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002563 // Get a temp stack location to use to copy
2564 // float-to-int via the stack.
2565 //
2566 // FIXME: For now, we allocate permanent space because
2567 // the stack frame manager does not allow locals to be
2568 // allocated (e.g., for alloca) after a temp is
2569 // allocated!
2570 //
2571 // int tmpOffset = MF.getInfo()->pushTempValue(argSize);
2572 int tmpOffset = MF.getInfo()->allocateLocalVar(argVReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002573
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002574 // Generate the store from FP reg to stack
Misha Brukmanea481cc2003-06-03 03:21:58 +00002575 unsigned StoreOpcode = ChooseStoreInstruction(argType);
2576 M = BuildMI(convertOpcodeFromRegToImm(StoreOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002577 .addReg(argVal).addMReg(regInfo.getFramePointer())
2578 .addSImm(tmpOffset);
2579 mvec.push_back(M);
2580
2581 // Generate the load from stack to int arg reg
Misha Brukmanea481cc2003-06-03 03:21:58 +00002582 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
2583 M = BuildMI(convertOpcodeFromRegToImm(LoadOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002584 .addMReg(regInfo.getFramePointer()).addSImm(tmpOffset)
2585 .addReg(argVReg, MOTy::Def);
2586
2587 // Mark operand with register it should be assigned
2588 // both for copy and for the callMI
2589 M->SetRegForOperand(M->getNumOperands()-1, copyRegNum);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002590 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2591 copyRegNum);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002592 mvec.push_back(M);
2593
2594 // Add info about the argument to the CallArgsDescriptor
2595 argInfo.setUseIntArgReg();
2596 argInfo.setArgCopy(copyRegNum);
2597 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00002598 // Cannot fit in first $K$ regs so pass arg on stack
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002599 argInfo.setUseStackSlot();
2600 }
2601 } else if (isFPArg) {
2602 // Get the outgoing arg reg to see if there is one.
2603 regNumForArg = regInfo.regNumForFPArg(regType, false, false,
2604 argNo, regClassIDOfArgReg);
2605 if (regNumForArg == regInfo.getInvalidRegNum())
2606 argInfo.setUseStackSlot();
2607 else {
2608 argInfo.setUseFPArgReg();
2609 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2610 regNumForArg);
2611 }
2612 } else {
2613 // Get the outgoing arg reg to see if there is one.
2614 regNumForArg = regInfo.regNumForIntArg(false,false,
2615 argNo, regClassIDOfArgReg);
2616 if (regNumForArg == regInfo.getInvalidRegNum())
2617 argInfo.setUseStackSlot();
2618 else {
2619 argInfo.setUseIntArgReg();
2620 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2621 regNumForArg);
2622 }
2623 }
2624
2625 //
2626 // Now insert copy instructions to stack slot or arg. register
2627 //
2628 if (argInfo.usesStackSlot()) {
2629 // Get the stack offset for this argument slot.
2630 // FP args on stack are right justified so adjust offset!
2631 // int arguments are also right justified but they are
2632 // always loaded as a full double-word so the offset does
2633 // not need to be adjusted.
2634 int argOffset = frameInfo.getOutgoingArgOffset(MF, argNo);
2635 if (argType->isFloatingPoint()) {
2636 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
2637 assert(argSize <= slotSize && "Insufficient slot size!");
2638 argOffset += slotSize - argSize;
2639 }
2640
2641 // Now generate instruction to copy argument to stack
2642 MachineOpCode storeOpCode =
2643 (argType->isFloatingPoint()
2644 ? ((argSize == 4)? V9::STFi : V9::STDFi) : V9::STXi);
2645
2646 M = BuildMI(storeOpCode, 3).addReg(argVal)
2647 .addMReg(regInfo.getStackPointer()).addSImm(argOffset);
2648 mvec.push_back(M);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002649 }
2650 else if (regNumForArg != regInfo.getInvalidRegNum()) {
2651
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002652 // Create a virtual register to represent the arg reg. Mark
2653 // this vreg as being an implicit operand of the call MI.
2654 TmpInstruction* argVReg =
2655 new TmpInstruction(mcfi, argVal, NULL, "argReg");
2656
2657 callMI->addImplicitRef(argVReg);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002658
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002659 // Generate the reg-to-reg copy into the outgoing arg reg.
2660 // -- For FP values, create a FMOVS or FMOVD instruction
2661 // -- For non-FP values, create an add-with-0 instruction
2662 if (argType->isFloatingPoint())
2663 M=(BuildMI(argType==Type::FloatTy? V9::FMOVS :V9::FMOVD,2)
2664 .addReg(argVal).addReg(argVReg, MOTy::Def));
2665 else
2666 M = (BuildMI(ChooseAddInstructionByType(argType), 3)
2667 .addReg(argVal).addSImm((int64_t) 0)
2668 .addReg(argVReg, MOTy::Def));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002669
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002670 // Mark the operand with the register it should be assigned
2671 M->SetRegForOperand(M->getNumOperands()-1, regNumForArg);
2672 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2673 regNumForArg);
2674
2675 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +00002676 }
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002677 else
2678 assert(argInfo.getArgCopy() != regInfo.getInvalidRegNum() &&
2679 "Arg. not in stack slot, primary or secondary register?");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002680 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002681
2682 // add call instruction and delay slot before copying return value
2683 mvec.push_back(callMI);
2684 mvec.push_back(BuildMI(V9::NOP, 0));
2685
Misha Brukman7b647942003-05-30 20:11:56 +00002686 // Add the return value as an implicit ref. The call operands
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002687 // were added above. Also, add code to copy out the return value.
2688 // This is always register-to-register for int or FP return values.
2689 //
2690 if (callInstr->getType() != Type::VoidTy) {
2691 // Get the return value reg.
2692 const Type* retType = callInstr->getType();
2693
2694 int regNum = (retType->isFloatingPoint()
2695 ? (unsigned) SparcFloatRegClass::f0
2696 : (unsigned) SparcIntRegClass::o0);
2697 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
2698 regNum = regInfo.getUnifiedRegNum(regClassID, regNum);
2699
2700 // Create a virtual register to represent it and mark
2701 // this vreg as being an implicit operand of the call MI
2702 TmpInstruction* retVReg =
2703 new TmpInstruction(mcfi, callInstr, NULL, "argReg");
2704
2705 callMI->addImplicitRef(retVReg, /*isDef*/ true);
2706
2707 // Generate the reg-to-reg copy from the return value reg.
2708 // -- For FP values, create a FMOVS or FMOVD instruction
2709 // -- For non-FP values, create an add-with-0 instruction
2710 if (retType->isFloatingPoint())
2711 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
2712 .addReg(retVReg).addReg(callInstr, MOTy::Def));
2713 else
2714 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
2715 .addReg(retVReg).addSImm((int64_t) 0)
2716 .addReg(callInstr, MOTy::Def));
2717
2718 // Mark the operand with the register it should be assigned
2719 // Also mark the implicit ref of the call defining this operand
2720 M->SetRegForOperand(0, regNum);
2721 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,regNum);
2722
2723 mvec.push_back(M);
2724 }
2725
Misha Brukman7b647942003-05-30 20:11:56 +00002726 // For the CALL instruction, the ret. addr. reg. is also implicit
2727 if (isa<Function>(callee))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002728 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
2729
2730 MF.getInfo()->popAllTempValues(); // free temps used for this inst
Misha Brukman7b647942003-05-30 20:11:56 +00002731 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002732
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002733 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002734 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002735
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002736 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002737 {
2738 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2739 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2740 Instruction* shlInstr = subtreeRoot->getInstruction();
2741
2742 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002743 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2744 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002745
2746 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Misha Brukmand36e30e2003-06-06 09:52:23 +00002747 (opType == Type::LongTy)? V9::SLLXr6:V9::SLLr5,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002748 argVal1, argVal2, 0, shlInstr, mvec,
2749 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002750 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002751 }
2752
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002753 case 63: // reg: Shr(reg, reg)
Misha Brukman7b647942003-05-30 20:11:56 +00002754 {
2755 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002756 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2757 "Shr unsupported for other types");
Chris Lattner54e898e2003-01-15 19:23:34 +00002758 Add3OperandInstr(opType->isSigned()
Misha Brukmand36e30e2003-06-06 09:52:23 +00002759 ? (opType == Type::LongTy ? V9::SRAXr6 : V9::SRAr5)
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002760 : (opType == Type::ULongTy ? V9::SRLXr6 : V9::SRLr5),
Chris Lattner54e898e2003-01-15 19:23:34 +00002761 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002762 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002763 }
2764
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002765 case 64: // reg: Phi(reg,reg)
2766 break; // don't forward the value
Vikram S. Adve74825322002-03-18 03:15:35 +00002767
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002768 case 65: // reg: VaArg(reg)
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002769 {
2770 // Use value initialized by va_start as pointer to args on the stack.
2771 // Load argument via current pointer value, then increment pointer.
2772 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
2773 Instruction* vaArgI = subtreeRoot->getInstruction();
Misha Brukman91aee472003-05-27 22:37:00 +00002774 mvec.push_back(BuildMI(V9::LDXi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002775 addSImm(0).addRegDef(vaArgI));
Misha Brukman91aee472003-05-27 22:37:00 +00002776 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002777 addSImm(argSize).addRegDef(vaArgI->getOperand(0)));
2778 break;
2779 }
2780
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002781 case 71: // reg: VReg
2782 case 72: // reg: Constant
2783 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002784
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002785 default:
2786 assert(0 && "Unrecognized BURG rule");
2787 break;
2788 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002789 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002790
Misha Brukman7b647942003-05-30 20:11:56 +00002791 if (forwardOperandNum >= 0) {
2792 // We did not generate a machine instruction but need to use operand.
2793 // If user is in the same tree, replace Value in its machine operand.
2794 // If not, insert a copy instruction which should get coalesced away
2795 // by register allocation.
2796 if (subtreeRoot->parent() != NULL)
2797 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2798 else {
2799 std::vector<MachineInstr*> minstrVec;
2800 Instruction* instr = subtreeRoot->getInstruction();
2801 target.getInstrInfo().
2802 CreateCopyInstructionsByType(target,
2803 instr->getParent()->getParent(),
2804 instr->getOperand(forwardOperandNum),
2805 instr, minstrVec,
2806 MachineCodeForInstruction::get(instr));
2807 assert(minstrVec.size() > 0);
2808 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Chris Lattner20b1ea02001-09-14 03:47:57 +00002809 }
Misha Brukman7b647942003-05-30 20:11:56 +00002810 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002811
Misha Brukman7b647942003-05-30 20:11:56 +00002812 if (maskUnsignedResult) {
2813 // If result is unsigned and smaller than int reg size,
2814 // we need to clear high bits of result value.
2815 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2816 Instruction* dest = subtreeRoot->getInstruction();
2817 if (dest->getType()->isUnsigned()) {
2818 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
2819 if (destSize <= 4) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002820 // Mask high 64 - N bits, where N = 4*destSize.
2821
2822 // Use a TmpInstruction to represent the
Misha Brukman7b647942003-05-30 20:11:56 +00002823 // intermediate result before masking. Since those instructions
2824 // have already been generated, go back and substitute tmpI
2825 // for dest in the result position of each one of them.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002826 //
2827 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(dest);
2828 TmpInstruction *tmpI = new TmpInstruction(mcfi, dest->getType(),
2829 dest, NULL, "maskHi");
2830 Value* srlArgToUse = tmpI;
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002831
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002832 unsigned numSubst = 0;
2833 for (unsigned i=0, N=mvec.size(); i < N; ++i) {
2834 bool someArgsWereIgnored = false;
2835 numSubst += mvec[i]->substituteValue(dest, tmpI, /*defsOnly*/ true,
2836 /*defsAndUses*/ false,
2837 someArgsWereIgnored);
2838 assert(!someArgsWereIgnored &&
2839 "Operand `dest' exists but not replaced: probably bogus!");
2840 }
2841 assert(numSubst > 0 && "Operand `dest' not replaced: probably bogus!");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002842
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002843 // Left shift 32-N if size (N) is less than 32 bits.
2844 // Use another tmp. virtual registe to represent this result.
2845 if (destSize < 4) {
2846 srlArgToUse = new TmpInstruction(mcfi, dest->getType(),
2847 tmpI, NULL, "maskHi2");
2848 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpI)
2849 .addZImm(8*(4-destSize))
2850 .addReg(srlArgToUse, MOTy::Def));
2851 }
2852
2853 // Logical right shift 32-N to get zero extension in top 64-N bits.
2854 mvec.push_back(BuildMI(V9::SRLi5, 3).addReg(srlArgToUse)
2855 .addZImm(8*(4-destSize)).addReg(dest, MOTy::Def));
2856
Misha Brukman7b647942003-05-30 20:11:56 +00002857 } else if (destSize < 8) {
2858 assert(0 && "Unsupported type size: 32 < size < 64 bits");
2859 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002860 }
Misha Brukman7b647942003-05-30 20:11:56 +00002861 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002862}