blob: e35024b45dfdd0a4bb54860849f5e0366198ff38 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000011#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerea45d7b2002-12-28 20:19:44 +000016#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner9c461082002-02-03 07:50:56 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000018#include "llvm/DerivedTypes.h"
Misha Brukmanb8db66e2003-08-07 15:43:46 +000019#include "llvm/Instructions.h"
20#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000021#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000022#include "llvm/ConstantHandling.h"
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +000023#include "llvm/Intrinsics.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000025#include <math.h>
Vikram S. Adve951df2b2003-07-10 20:07:54 +000026#include <algorithm>
Chris Lattner20b1ea02001-09-14 03:47:57 +000027
Chris Lattner54e898e2003-01-15 19:23:34 +000028static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +000029 std::vector<MachineInstr*>& mvec) {
Chris Lattner54e898e2003-01-15 19:23:34 +000030 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
31 .addReg(Node->rightChild()->getValue())
32 .addRegDef(Node->getValue()));
33}
34
35
36
Chris Lattner795ba6c2003-01-15 21:36:50 +000037//---------------------------------------------------------------------------
38// Function: GetMemInstArgs
39//
40// Purpose:
41// Get the pointer value and the index vector for a memory operation
42// (GetElementPtr, Load, or Store). If all indices of the given memory
43// operation are constant, fold in constant indices in a chain of
44// preceding GetElementPtr instructions (if any), and return the
45// pointer value of the first instruction in the chain.
46// All folded instructions are marked so no code is generated for them.
47//
48// Return values:
49// Returns the pointer Value to use.
50// Returns the resulting IndexVector in idxVec.
51// Returns true/false in allConstantIndices if all indices are/aren't const.
52//---------------------------------------------------------------------------
53
54
55//---------------------------------------------------------------------------
56// Function: FoldGetElemChain
57//
58// Purpose:
59// Fold a chain of GetElementPtr instructions containing only
60// constant offsets into an equivalent (Pointer, IndexVector) pair.
61// Returns the pointer Value, and stores the resulting IndexVector
62// in argument chainIdxVec. This is a helper function for
63// FoldConstantIndices that does the actual folding.
64//---------------------------------------------------------------------------
65
66
67// Check for a constant 0.
68inline bool
69IsZero(Value* idx)
70{
71 return (idx == ConstantSInt::getNullValue(idx->getType()));
72}
73
74static Value*
Misha Brukmanee563cb2003-05-21 17:59:06 +000075FoldGetElemChain(InstrTreeNode* ptrNode, std::vector<Value*>& chainIdxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +000076 bool lastInstHasLeadingNonZero)
77{
78 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
79 GetElementPtrInst* gepInst =
80 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
81
82 // ptr value is not computed in this tree or ptr value does not come from GEP
83 // instruction
84 if (gepInst == NULL)
85 return NULL;
86
87 // Return NULL if we don't fold any instructions in.
88 Value* ptrVal = NULL;
89
90 // Now chase the chain of getElementInstr instructions, if any.
91 // Check for any non-constant indices and stop there.
92 // Also, stop if the first index of child is a non-zero array index
93 // and the last index of the current node is a non-array index:
94 // in that case, a non-array declared type is being accessed as an array
95 // which is not type-safe, but could be legal.
96 //
97 InstructionNode* ptrChild = gepNode;
98 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
99 ptrChild->getOpLabel() == GetElemPtrIdx))
Misha Brukman81b06862003-05-21 18:48:06 +0000100 {
101 // Child is a GetElemPtr instruction
102 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
103 User::op_iterator OI, firstIdx = gepInst->idx_begin();
104 User::op_iterator lastIdx = gepInst->idx_end();
105 bool allConstantOffsets = true;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000106
Misha Brukman81b06862003-05-21 18:48:06 +0000107 // The first index of every GEP must be an array index.
108 assert((*firstIdx)->getType() == Type::LongTy &&
109 "INTERNAL ERROR: Structure index for a pointer type!");
Chris Lattner795ba6c2003-01-15 21:36:50 +0000110
Misha Brukman81b06862003-05-21 18:48:06 +0000111 // If the last instruction had a leading non-zero index, check if the
112 // current one references a sequential (i.e., indexable) type.
113 // If not, the code is not type-safe and we would create an illegal GEP
114 // by folding them, so don't fold any more instructions.
115 //
116 if (lastInstHasLeadingNonZero)
117 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
118 break; // cannot fold in any preceding getElementPtr instrs.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000119
Misha Brukman81b06862003-05-21 18:48:06 +0000120 // Check that all offsets are constant for this instruction
121 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
122 allConstantOffsets = isa<ConstantInt>(*OI);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000123
Misha Brukman81b06862003-05-21 18:48:06 +0000124 if (allConstantOffsets) {
125 // Get pointer value out of ptrChild.
126 ptrVal = gepInst->getPointerOperand();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000127
Misha Brukman81b06862003-05-21 18:48:06 +0000128 // Insert its index vector at the start, skipping any leading [0]
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000129 // Remember the old size to check if anything was inserted.
130 unsigned oldSize = chainIdxVec.size();
131 int firstIsZero = IsZero(*firstIdx);
132 chainIdxVec.insert(chainIdxVec.begin(), firstIdx + firstIsZero, lastIdx);
133
134 // Remember if it has leading zero index: it will be discarded later.
135 if (oldSize < chainIdxVec.size())
136 lastInstHasLeadingNonZero = !firstIsZero;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000137
Misha Brukman81b06862003-05-21 18:48:06 +0000138 // Mark the folded node so no code is generated for it.
139 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000140
Misha Brukman81b06862003-05-21 18:48:06 +0000141 // Get the previous GEP instruction and continue trying to fold
142 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
143 } else // cannot fold this getElementPtr instr. or any preceding ones
144 break;
145 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000146
147 // If the first getElementPtr instruction had a leading [0], add it back.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000148 // Note that this instruction is the *last* one that was successfully
149 // folded *and* contributed any indices, in the loop above.
150 //
Chris Lattner795ba6c2003-01-15 21:36:50 +0000151 if (ptrVal && ! lastInstHasLeadingNonZero)
152 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
153
154 return ptrVal;
155}
156
157
158//---------------------------------------------------------------------------
159// Function: GetGEPInstArgs
160//
161// Purpose:
162// Helper function for GetMemInstArgs that handles the final getElementPtr
163// instruction used by (or same as) the memory operation.
164// Extracts the indices of the current instruction and tries to fold in
165// preceding ones if all indices of the current one are constant.
166//---------------------------------------------------------------------------
167
168static Value *
169GetGEPInstArgs(InstructionNode* gepNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000170 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000171 bool& allConstantIndices)
172{
173 allConstantIndices = true;
174 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
175
176 // Default pointer is the one from the current instruction.
177 Value* ptrVal = gepI->getPointerOperand();
178 InstrTreeNode* ptrChild = gepNode->leftChild();
179
180 // Extract the index vector of the GEP instructin.
181 // If all indices are constant and first index is zero, try to fold
182 // in preceding GEPs with all constant indices.
183 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
184 allConstantIndices && OI != OE; ++OI)
185 if (! isa<Constant>(*OI))
186 allConstantIndices = false; // note: this also terminates loop!
187
188 // If we have only constant indices, fold chains of constant indices
189 // in this and any preceding GetElemPtr instructions.
190 bool foldedGEPs = false;
191 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
192 if (allConstantIndices)
Misha Brukman81b06862003-05-21 18:48:06 +0000193 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) {
194 ptrVal = newPtr;
195 foldedGEPs = true;
196 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000197
198 // Append the index vector of the current instruction.
199 // Skip the leading [0] index if preceding GEPs were folded into this.
200 idxVec.insert(idxVec.end(),
201 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
202 gepI->idx_end());
203
204 return ptrVal;
205}
206
207//---------------------------------------------------------------------------
208// Function: GetMemInstArgs
209//
210// Purpose:
211// Get the pointer value and the index vector for a memory operation
212// (GetElementPtr, Load, or Store). If all indices of the given memory
213// operation are constant, fold in constant indices in a chain of
214// preceding GetElementPtr instructions (if any), and return the
215// pointer value of the first instruction in the chain.
216// All folded instructions are marked so no code is generated for them.
217//
218// Return values:
219// Returns the pointer Value to use.
220// Returns the resulting IndexVector in idxVec.
221// Returns true/false in allConstantIndices if all indices are/aren't const.
222//---------------------------------------------------------------------------
223
224static Value*
225GetMemInstArgs(InstructionNode* memInstrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000226 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000227 bool& allConstantIndices)
228{
229 allConstantIndices = false;
230 Instruction* memInst = memInstrNode->getInstruction();
231 assert(idxVec.size() == 0 && "Need empty vector to return indices");
232
233 // If there is a GetElemPtr instruction to fold in to this instr,
234 // it must be in the left child for Load and GetElemPtr, and in the
235 // right child for Store instructions.
236 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
237 ? memInstrNode->rightChild()
238 : memInstrNode->leftChild());
239
240 // Default pointer is the one from the current instruction.
241 Value* ptrVal = ptrChild->getValue();
242
243 // Find the "last" GetElemPtr instruction: this one or the immediate child.
244 // There will be none if this is a load or a store from a scalar pointer.
245 InstructionNode* gepNode = NULL;
246 if (isa<GetElementPtrInst>(memInst))
247 gepNode = memInstrNode;
Misha Brukman81b06862003-05-21 18:48:06 +0000248 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) {
249 // Child of load/store is a GEP and memInst is its only use.
250 // Use its indices and mark it as folded.
251 gepNode = cast<InstructionNode>(ptrChild);
252 gepNode->markFoldedIntoParent();
253 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000254
255 // If there are no indices, return the current pointer.
256 // Else extract the pointer from the GEP and fold the indices.
257 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
258 : ptrVal;
259}
260
Chris Lattner54e898e2003-01-15 19:23:34 +0000261
Chris Lattner20b1ea02001-09-14 03:47:57 +0000262//************************ Internal Functions ******************************/
263
Chris Lattner20b1ea02001-09-14 03:47:57 +0000264
Chris Lattner20b1ea02001-09-14 03:47:57 +0000265static inline MachineOpCode
266ChooseBprInstruction(const InstructionNode* instrNode)
267{
268 MachineOpCode opCode;
269
270 Instruction* setCCInstr =
271 ((InstructionNode*) instrNode->leftChild())->getInstruction();
272
273 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000274 {
275 case Instruction::SetEQ: opCode = V9::BRZ; break;
276 case Instruction::SetNE: opCode = V9::BRNZ; break;
277 case Instruction::SetLE: opCode = V9::BRLEZ; break;
278 case Instruction::SetGE: opCode = V9::BRGEZ; break;
279 case Instruction::SetLT: opCode = V9::BRLZ; break;
280 case Instruction::SetGT: opCode = V9::BRGZ; break;
281 default:
282 assert(0 && "Unrecognized VM instruction!");
283 opCode = V9::INVALID_OPCODE;
284 break;
285 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000286
287 return opCode;
288}
289
290
291static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000292ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000293 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000294{
Misha Brukmana98cd452003-05-20 20:32:24 +0000295 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000296
297 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
298
Misha Brukman81b06862003-05-21 18:48:06 +0000299 if (isSigned) {
300 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000301 {
Misha Brukman81b06862003-05-21 18:48:06 +0000302 case Instruction::SetEQ: opCode = V9::BE; break;
303 case Instruction::SetNE: opCode = V9::BNE; break;
304 case Instruction::SetLE: opCode = V9::BLE; break;
305 case Instruction::SetGE: opCode = V9::BGE; break;
306 case Instruction::SetLT: opCode = V9::BL; break;
307 case Instruction::SetGT: opCode = V9::BG; break;
308 default:
309 assert(0 && "Unrecognized VM instruction!");
310 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000311 }
Misha Brukman81b06862003-05-21 18:48:06 +0000312 } else {
313 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000314 {
Misha Brukman81b06862003-05-21 18:48:06 +0000315 case Instruction::SetEQ: opCode = V9::BE; break;
316 case Instruction::SetNE: opCode = V9::BNE; break;
317 case Instruction::SetLE: opCode = V9::BLEU; break;
318 case Instruction::SetGE: opCode = V9::BCC; break;
319 case Instruction::SetLT: opCode = V9::BCS; break;
320 case Instruction::SetGT: opCode = V9::BGU; break;
321 default:
322 assert(0 && "Unrecognized VM instruction!");
323 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000324 }
Misha Brukman81b06862003-05-21 18:48:06 +0000325 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000326
327 return opCode;
328}
329
330static inline MachineOpCode
331ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000332 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000333{
Misha Brukmana98cd452003-05-20 20:32:24 +0000334 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000335
336 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000337 {
338 case Instruction::SetEQ: opCode = V9::FBE; break;
339 case Instruction::SetNE: opCode = V9::FBNE; break;
340 case Instruction::SetLE: opCode = V9::FBLE; break;
341 case Instruction::SetGE: opCode = V9::FBGE; break;
342 case Instruction::SetLT: opCode = V9::FBL; break;
343 case Instruction::SetGT: opCode = V9::FBG; break;
344 default:
345 assert(0 && "Unrecognized VM instruction!");
346 break;
347 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000348
349 return opCode;
350}
351
352
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000353// Create a unique TmpInstruction for a boolean value,
354// representing the CC register used by a branch on that value.
355// For now, hack this using a little static cache of TmpInstructions.
356// Eventually the entire BURG instruction selection should be put
357// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000358// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000359// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000360//
361static TmpInstruction*
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000362GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType,
363 MachineCodeForInstruction& mcfi)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000364{
Chris Lattner09ff1122002-07-24 21:21:32 +0000365 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000366 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000367 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000368
369 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
370
Misha Brukman81b06862003-05-21 18:48:06 +0000371 if (lastFunction != F) {
372 lastFunction = F;
373 boolToTmpCache.clear();
374 }
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000375
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000376 // Look for tmpI and create a new one otherwise. The new value is
377 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000378 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
379 if (tmpI == NULL)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000380 tmpI = new TmpInstruction(mcfi, ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000381
382 return tmpI;
383}
384
385
Chris Lattner20b1ea02001-09-14 03:47:57 +0000386static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000387ChooseBccInstruction(const InstructionNode* instrNode,
Vikram S. Adve786833a2003-07-06 20:13:59 +0000388 const Type*& setCCType)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000389{
390 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000391 assert(setCCNode->getOpLabel() == SetCCOp);
392 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +0000393 setCCType = setCCInstr->getOperand(0)->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000394
Vikram S. Adve786833a2003-07-06 20:13:59 +0000395 if (setCCType->isFloatingPoint())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000396 return ChooseBFpccInstruction(instrNode, setCCInstr);
397 else
398 return ChooseBpccInstruction(instrNode, setCCInstr);
399}
400
401
Misha Brukmaneecdb662003-06-02 20:55:14 +0000402// WARNING: since this function has only one caller, it always returns
403// the opcode that expects an immediate and a register. If this function
404// is ever used in cases where an opcode that takes two registers is required,
405// then modify this function and use convertOpcodeFromRegToImm() where required.
406//
407// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
408// new cases of opcodes.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000409static inline MachineOpCode
Misha Brukmaneecdb662003-06-02 20:55:14 +0000410ChooseMovFpcciInstruction(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000411{
Misha Brukmana98cd452003-05-20 20:32:24 +0000412 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000413
414 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000415 {
Misha Brukmaneecdb662003-06-02 20:55:14 +0000416 case Instruction::SetEQ: opCode = V9::MOVFEi; break;
417 case Instruction::SetNE: opCode = V9::MOVFNEi; break;
418 case Instruction::SetLE: opCode = V9::MOVFLEi; break;
419 case Instruction::SetGE: opCode = V9::MOVFGEi; break;
420 case Instruction::SetLT: opCode = V9::MOVFLi; break;
421 case Instruction::SetGT: opCode = V9::MOVFGi; break;
Misha Brukman81b06862003-05-21 18:48:06 +0000422 default:
423 assert(0 && "Unrecognized VM instruction!");
424 break;
425 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000426
427 return opCode;
428}
429
430
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000431// ChooseMovpcciForSetCC -- Choose a conditional-move instruction
432// based on the type of SetCC operation.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433//
Misha Brukmaneecdb662003-06-02 20:55:14 +0000434// WARNING: since this function has only one caller, it always returns
435// the opcode that expects an immediate and a register. If this function
436// is ever used in cases where an opcode that takes two registers is required,
437// then modify this function and use convertOpcodeFromRegToImm() where required.
438//
439// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
440// new cases of opcodes.
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000441//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000442static MachineOpCode
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000443ChooseMovpcciForSetCC(const InstructionNode* instrNode)
444{
445 MachineOpCode opCode = V9::INVALID_OPCODE;
446
447 const Type* opType = instrNode->leftChild()->getValue()->getType();
448 assert(opType->isIntegral() || isa<PointerType>(opType));
449 bool noSign = opType->isUnsigned() || isa<PointerType>(opType);
450
451 switch(instrNode->getInstruction()->getOpcode())
452 {
453 case Instruction::SetEQ: opCode = V9::MOVEi; break;
454 case Instruction::SetLE: opCode = noSign? V9::MOVLEUi : V9::MOVLEi; break;
455 case Instruction::SetGE: opCode = noSign? V9::MOVCCi : V9::MOVGEi; break;
456 case Instruction::SetLT: opCode = noSign? V9::MOVCSi : V9::MOVLi; break;
457 case Instruction::SetGT: opCode = noSign? V9::MOVGUi : V9::MOVGi; break;
458 case Instruction::SetNE: opCode = V9::MOVNEi; break;
459 default: assert(0 && "Unrecognized LLVM instr!"); break;
460 }
461
462 return opCode;
463}
464
465
466// ChooseMovpregiForSetCC -- Choose a conditional-move-on-register-value
467// instruction based on the type of SetCC operation. These instructions
468// compare a register with 0 and perform the move is the comparison is true.
469//
470// WARNING: like the previous function, this function it always returns
471// the opcode that expects an immediate and a register. See above.
472//
473static MachineOpCode
474ChooseMovpregiForSetCC(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000475{
Misha Brukmana98cd452003-05-20 20:32:24 +0000476 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000477
478 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000479 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000480 case Instruction::SetEQ: opCode = V9::MOVRZi; break;
481 case Instruction::SetLE: opCode = V9::MOVRLEZi; break;
482 case Instruction::SetGE: opCode = V9::MOVRGEZi; break;
483 case Instruction::SetLT: opCode = V9::MOVRLZi; break;
484 case Instruction::SetGT: opCode = V9::MOVRGZi; break;
485 case Instruction::SetNE: opCode = V9::MOVRNZi; break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000486 default: assert(0 && "Unrecognized VM instr!"); break;
Misha Brukman81b06862003-05-21 18:48:06 +0000487 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000488
489 return opCode;
490}
491
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000492
Chris Lattner20b1ea02001-09-14 03:47:57 +0000493static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000494ChooseConvertToFloatInstr(const TargetMachine& target,
495 OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000496{
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000497 assert((vopCode == ToFloatTy || vopCode == ToDoubleTy) &&
498 "Unrecognized convert-to-float opcode!");
Vikram S. Advee895a742003-08-06 18:48:40 +0000499 assert((opType->isIntegral() || opType->isFloatingPoint() ||
500 isa<PointerType>(opType))
501 && "Trying to convert a non-scalar type to FLOAT/DOUBLE?");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000502
Misha Brukmana98cd452003-05-20 20:32:24 +0000503 MachineOpCode opCode = V9::INVALID_OPCODE;
Vikram S. Advee895a742003-08-06 18:48:40 +0000504
505 unsigned opSize = target.getTargetData().getTypeSize(opType);
506
507 if (opType == Type::FloatTy)
508 opCode = (vopCode == ToFloatTy? V9::NOP : V9::FSTOD);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000509 else if (opType == Type::DoubleTy)
Vikram S. Advee895a742003-08-06 18:48:40 +0000510 opCode = (vopCode == ToFloatTy? V9::FDTOS : V9::NOP);
511 else if (opSize <= 4)
512 opCode = (vopCode == ToFloatTy? V9::FITOS : V9::FITOD);
513 else {
514 assert(opSize == 8 && "Unrecognized type size > 4 and < 8!");
515 opCode = (vopCode == ToFloatTy? V9::FXTOS : V9::FXTOD);
516 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000517
518 return opCode;
519}
520
521static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000522ChooseConvertFPToIntInstr(const TargetMachine& target,
523 const Type* destType, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000524{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000525 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
526 && "This function should only be called for FLOAT or DOUBLE");
Vikram S. Advee895a742003-08-06 18:48:40 +0000527 assert((destType->isIntegral() || isa<PointerType>(destType))
528 && "Trying to convert FLOAT/DOUBLE to a non-scalar type?");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000529
Vikram S. Advee895a742003-08-06 18:48:40 +0000530 MachineOpCode opCode = V9::INVALID_OPCODE;
531
532 unsigned destSize = target.getTargetData().getTypeSize(destType);
533
534 if (destType == Type::UIntTy)
535 assert(destType != Type::UIntTy && "Expand FP-to-uint beforehand.");
536 else if (destSize <= 4)
Misha Brukman81b06862003-05-21 18:48:06 +0000537 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
Vikram S. Advee895a742003-08-06 18:48:40 +0000538 else {
539 assert(destSize == 8 && "Unrecognized type size > 4 and < 8!");
540 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
541 }
Vikram S. Adve94c40812002-09-27 14:33:08 +0000542
Chris Lattner20b1ea02001-09-14 03:47:57 +0000543 return opCode;
544}
545
Vikram S. Advee895a742003-08-06 18:48:40 +0000546static MachineInstr*
547CreateConvertFPToIntInstr(const TargetMachine& target,
548 Value* srcVal,
549 Value* destVal,
550 const Type* destType)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000551{
Vikram S. Advee895a742003-08-06 18:48:40 +0000552 MachineOpCode opCode = ChooseConvertFPToIntInstr(target, destType,
553 srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000554 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000555 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000556}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000557
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000558// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000559// The FP value must be converted to the dest type in an FP register,
560// and the result is then copied from FP to int register via memory.
Vikram S. Advee895a742003-08-06 18:48:40 +0000561// SPARC does not have a float-to-uint conversion, only a float-to-int (fdtoi).
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000562// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
Vikram S. Advee895a742003-08-06 18:48:40 +0000563// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly.
564// Therefore, for converting an FP value to uint32_t, we first need to convert
565// to uint64_t and then to uint32_t.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000566//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000567static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000568CreateCodeToConvertFloatToInt(const TargetMachine& target,
569 Value* opVal,
570 Instruction* destI,
571 std::vector<MachineInstr*>& mvec,
572 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000573{
Vikram S. Advee895a742003-08-06 18:48:40 +0000574 Function* F = destI->getParent()->getParent();
575
Vikram S. Adve1e606692002-07-31 21:01:34 +0000576 // Create a temporary to represent the FP register into which the
577 // int value will placed after conversion. The type of this temporary
578 // depends on the type of FP register to use: single-prec for a 32-bit
579 // int or smaller; double-prec for a 64-bit int.
580 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000581 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve1e606692002-07-31 21:01:34 +0000582
Vikram S. Advee895a742003-08-06 18:48:40 +0000583 const Type* castDestType = destI->getType(); // type for the cast instr result
584 const Type* castDestRegType; // type for cast instruction result reg
585 TmpInstruction* destForCast; // dest for cast instruction
586 Instruction* fpToIntCopyDest = destI; // dest for fp-reg-to-int-reg copy instr
587
588 // For converting an FP value to uint32_t, we first need to convert to
589 // uint64_t and then to uint32_t, as explained above.
590 if (destI->getType() == Type::UIntTy) {
591 castDestType = Type::ULongTy; // use this instead of type of destI
592 castDestRegType = Type::DoubleTy; // uint64_t needs 64-bit FP register.
593 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
594 fpToIntCopyDest = new TmpInstruction(mcfi, castDestType, destForCast);
595 }
596 else {
597 castDestRegType = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
598 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
599 }
600
601 // Create the fp-to-int conversion instruction (src and dest regs are FP regs)
602 mvec.push_back(CreateConvertFPToIntInstr(target, opVal, destForCast,
603 castDestType));
Vikram S. Adve1e606692002-07-31 21:01:34 +0000604
605 // Create the fpreg-to-intreg copy code
Vikram S. Advee895a742003-08-06 18:48:40 +0000606 target.getInstrInfo().CreateCodeToCopyFloatToInt(target, F, destForCast,
607 fpToIntCopyDest, mvec, mcfi);
608
609 // Create the uint64_t to uint32_t conversion, if needed
610 if (destI->getType() == Type::UIntTy)
611 target.getInstrInfo().
612 CreateZeroExtensionInstructions(target, F, fpToIntCopyDest, destI,
613 /*numLowBits*/ 32, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000614}
615
616
Chris Lattner20b1ea02001-09-14 03:47:57 +0000617static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000618ChooseAddInstruction(const InstructionNode* instrNode)
619{
620 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
621}
622
623
Chris Lattner20b1ea02001-09-14 03:47:57 +0000624static inline MachineInstr*
625CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000626 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000627{
Misha Brukmana98cd452003-05-20 20:32:24 +0000628 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000629 .addReg(instrNode->leftChild()->getValue())
630 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000631}
632
633static inline MachineInstr*
634CreateAddConstInstruction(const InstructionNode* instrNode)
635{
636 MachineInstr* minstr = NULL;
637
638 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000639 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000640
641 // Cases worth optimizing are:
642 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
643 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
644 //
Chris Lattner9b625032002-05-06 16:15:30 +0000645 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000646 double dval = FPC->getValue();
647 if (dval == 0.0)
648 minstr = CreateMovFloatInstruction(instrNode,
649 instrNode->getInstruction()->getType());
650 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000651
652 return minstr;
653}
654
655
656static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000657ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000658{
Misha Brukmana98cd452003-05-20 20:32:24 +0000659 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000660
Misha Brukman81b06862003-05-21 18:48:06 +0000661 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukman91aee472003-05-27 22:37:00 +0000662 opCode = V9::SUBr;
Misha Brukman81b06862003-05-21 18:48:06 +0000663 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000664 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000665 {
666 case Type::FloatTyID: opCode = V9::FSUBS; break;
667 case Type::DoubleTyID: opCode = V9::FSUBD; break;
668 default: assert(0 && "Invalid type for SUB instruction"); break;
669 }
670 }
671
Chris Lattner20b1ea02001-09-14 03:47:57 +0000672 return opCode;
673}
674
675
676static inline MachineInstr*
677CreateSubConstInstruction(const InstructionNode* instrNode)
678{
679 MachineInstr* minstr = NULL;
680
681 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000682 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000683
684 // Cases worth optimizing are:
685 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
686 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
687 //
Chris Lattner9b625032002-05-06 16:15:30 +0000688 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
689 double dval = FPC->getValue();
690 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000691 minstr = CreateMovFloatInstruction(instrNode,
692 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000693 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000694
695 return minstr;
696}
697
698
699static inline MachineOpCode
700ChooseFcmpInstruction(const InstructionNode* instrNode)
701{
Misha Brukmana98cd452003-05-20 20:32:24 +0000702 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000703
704 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
705 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000706 case Type::FloatTyID: opCode = V9::FCMPS; break;
707 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000708 default: assert(0 && "Invalid type for FCMP instruction"); break;
709 }
710
711 return opCode;
712}
713
714
715// Assumes that leftArg and rightArg are both cast instructions.
716//
717static inline bool
718BothFloatToDouble(const InstructionNode* instrNode)
719{
720 InstrTreeNode* leftArg = instrNode->leftChild();
721 InstrTreeNode* rightArg = instrNode->rightChild();
722 InstrTreeNode* leftArgArg = leftArg->leftChild();
723 InstrTreeNode* rightArgArg = rightArg->leftChild();
724 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
725
726 // Check if both arguments are floats cast to double
727 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000728 leftArgArg->getValue()->getType() == Type::FloatTy &&
729 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000730}
731
732
733static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000734ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000735{
Misha Brukmana98cd452003-05-20 20:32:24 +0000736 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000737
Chris Lattner0c4e8862002-09-03 01:08:28 +0000738 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000739 opCode = V9::MULXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000740 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000741 switch(resultType->getPrimitiveID())
Misha Brukman7b647942003-05-30 20:11:56 +0000742 {
743 case Type::FloatTyID: opCode = V9::FMULS; break;
744 case Type::DoubleTyID: opCode = V9::FMULD; break;
745 default: assert(0 && "Invalid type for MUL instruction"); break;
746 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000747
748 return opCode;
749}
750
751
Vikram S. Adve510eec72001-11-04 21:59:14 +0000752
Chris Lattner20b1ea02001-09-14 03:47:57 +0000753static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000754CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000755 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000756{
Misha Brukman91aee472003-05-27 22:37:00 +0000757 return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000758 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000759}
760
761
Vikram S. Adve242a8082002-05-19 15:25:51 +0000762// Create instruction sequence for any shift operation.
763// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
764// requires a second instruction for explicit sign-extension.
765// Note that we only have to worry about a sign-bit appearing in the
766// most significant bit of the operand after shifting (e.g., bit 32 of
767// Int or bit 16 of Short), so we do not have to worry about results
768// that are as large as a normal integer register.
769//
770static inline void
771CreateShiftInstructions(const TargetMachine& target,
772 Function* F,
773 MachineOpCode shiftOpCode,
774 Value* argVal1,
775 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000776 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000777 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000778 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000779 MachineCodeForInstruction& mcfi)
780{
781 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
782 "Large shift sizes unexpected, but can be handled below: "
783 "You need to check whether or not it fits in immed field below");
784
785 // If this is a logical left shift of a type smaller than the standard
786 // integer reg. size, we have to extend the sign-bit into upper bits
787 // of dest, so we need to put the result of the SLL into a temporary.
788 //
789 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000790 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000791
Misha Brukmand36e30e2003-06-06 09:52:23 +0000792 if ((shiftOpCode == V9::SLLr5 || shiftOpCode == V9::SLLXr6) && opSize < 8) {
Misha Brukman7b647942003-05-30 20:11:56 +0000793 // put SLL result into a temporary
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000794 shiftDest = new TmpInstruction(mcfi, argVal1, optArgVal2, "sllTmp");
Misha Brukman7b647942003-05-30 20:11:56 +0000795 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000796
797 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000798 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
799 .addReg(shiftDest, MOTy::Def)
800 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
801 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000802 mvec.push_back(M);
803
Misha Brukman7b647942003-05-30 20:11:56 +0000804 if (shiftDest != destVal) {
805 // extend the sign-bit of the result into all upper bits of dest
806 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
807 target.getInstrInfo().
808 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
809 8*opSize, mvec, mcfi);
810 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000811}
812
813
Vikram S. Adve74825322002-03-18 03:15:35 +0000814// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000815// create a cheaper instruction.
816// This returns the approximate cost of the instructions generated,
817// which is used to pick the cheapest when both operands are constant.
Vikram S. Adve645fea32003-05-25 21:59:47 +0000818static unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000819CreateMulConstInstruction(const TargetMachine &target, Function* F,
820 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000821 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000822 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000823{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000824 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukman91aee472003-05-27 22:37:00 +0000825 unsigned cost = target.getInstrInfo().minLatency(V9::MULXr);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000826 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000827
828 Value* constOp = rval;
829 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000830 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000831
832 // Cases worth optimizing are:
833 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
834 // (2) Multiply by 2^x for integer types: replace with Shift
835 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000836 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000837
Misha Brukmana98cd452003-05-20 20:32:24 +0000838 if (resultType->isInteger() || isa<PointerType>(resultType)) {
839 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +0000840 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
841 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +0000842 if (isValidConst) {
843 unsigned pow;
844 bool needNeg = false;
845 if (C < 0) {
846 needNeg = true;
847 C = -C;
848 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000849
Misha Brukmana98cd452003-05-20 20:32:24 +0000850 if (C == 0 || C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000851 cost = target.getInstrInfo().minLatency(V9::ADDr);
Misha Brukmana98cd452003-05-20 20:32:24 +0000852 unsigned Zero = target.getRegInfo().getZeroRegNum();
853 MachineInstr* M;
854 if (C == 0)
Misha Brukman91aee472003-05-27 22:37:00 +0000855 M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000856 else
Misha Brukman91aee472003-05-27 22:37:00 +0000857 M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000858 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +0000859 } else if (isPowerOf2(C, pow)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000860 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Misha Brukmand36e30e2003-06-06 09:52:23 +0000861 MachineOpCode opCode = (opSize <= 32)? V9::SLLr5 : V9::SLLXr6;
Misha Brukmana98cd452003-05-20 20:32:24 +0000862 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
863 destVal, mvec, mcfi);
864 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000865
Misha Brukman7b647942003-05-30 20:11:56 +0000866 if (mvec.size() > 0 && needNeg) {
867 // insert <reg = SUB 0, reg> after the instr to flip the sign
Misha Brukmana98cd452003-05-20 20:32:24 +0000868 MachineInstr* M = CreateIntNegInstruction(target, destVal);
869 mvec.push_back(M);
870 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000871 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000872 } else {
873 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
874 double dval = FPC->getValue();
875 if (fabs(dval) == 1) {
876 MachineOpCode opCode = (dval < 0)
877 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
878 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
879 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
880 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000881 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000882 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000883
Misha Brukmana98cd452003-05-20 20:32:24 +0000884 if (firstNewInstr < mvec.size()) {
885 cost = 0;
886 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
887 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
888 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000889
890 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000891}
892
893
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000894// Does not create any instructions if we cannot exploit constant to
895// create a cheaper instruction.
896//
897static inline void
898CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000899 Function* F,
900 Value* lval, Value* rval,
901 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000902 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000903 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000904{
905 Value* constOp;
Misha Brukman7b647942003-05-30 20:11:56 +0000906 if (isa<Constant>(lval) && isa<Constant>(rval)) {
907 // both operands are constant: evaluate and "set" in dest
908 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
909 cast<Constant>(lval),
910 cast<Constant>(rval));
911 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
912 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000913 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000914 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000915 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000916 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000917
918 // else neither is constant
919 return;
920}
921
Vikram S. Adve74825322002-03-18 03:15:35 +0000922// Return NULL if we cannot exploit constant to create a cheaper instruction
923static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000924CreateMulInstruction(const TargetMachine &target, Function* F,
925 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000926 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000927 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000928 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
929{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000930 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000931 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000932 if (mvec.size() == L) {
933 // no instructions were added so create MUL reg, reg, reg.
934 // Use FSMULD if both operands are actually floats cast to doubles.
935 // Otherwise, use the default opcode for the appropriate type.
936 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
937 ? forceMulOp
938 : ChooseMulInstructionByType(destVal->getType()));
939 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
940 .addRegDef(destVal));
941 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000942}
943
944
Vikram S. Adve510eec72001-11-04 21:59:14 +0000945// Generate a divide instruction for Div or Rem.
946// For Rem, this assumes that the operand type will be signed if the result
947// type is signed. This is correct because they must have the same sign.
948//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000949static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000950ChooseDivInstruction(TargetMachine &target,
951 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000952{
Misha Brukmana98cd452003-05-20 20:32:24 +0000953 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000954
955 const Type* resultType = instrNode->getInstruction()->getType();
956
Chris Lattner0c4e8862002-09-03 01:08:28 +0000957 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000958 opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000959 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000960 switch(resultType->getPrimitiveID())
961 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000962 case Type::FloatTyID: opCode = V9::FDIVS; break;
963 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000964 default: assert(0 && "Invalid type for DIV instruction"); break;
965 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000966
967 return opCode;
968}
969
970
Chris Lattner54e898e2003-01-15 19:23:34 +0000971// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve645fea32003-05-25 21:59:47 +0000972static void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000973CreateDivConstInstruction(TargetMachine &target,
974 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000975 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000976{
Chris Lattner54e898e2003-01-15 19:23:34 +0000977 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000978 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000979 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000980 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000981
Vikram S. Adve645fea32003-05-25 21:59:47 +0000982 Instruction* destVal = instrNode->getInstruction();
Chris Lattner54e898e2003-01-15 19:23:34 +0000983 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000984
985 // Cases worth optimizing are:
986 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
987 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
988 //
989 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000990
Misha Brukman7b647942003-05-30 20:11:56 +0000991 if (resultType->isInteger()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000992 unsigned pow;
993 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +0000994 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
995 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +0000996 if (isValidConst) {
997 bool needNeg = false;
998 if (C < 0) {
999 needNeg = true;
1000 C = -C;
1001 }
Vikram S. Advee6124d32003-07-29 19:59:23 +00001002
Misha Brukmana98cd452003-05-20 20:32:24 +00001003 if (C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +00001004 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001005 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001006 } else if (isPowerOf2(C, pow)) {
Vikram S. Adve645fea32003-05-25 21:59:47 +00001007 unsigned opCode;
1008 Value* shiftOperand;
Vikram S. Advee895a742003-08-06 18:48:40 +00001009 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve645fea32003-05-25 21:59:47 +00001010
1011 if (resultType->isSigned()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001012 // For N / 2^k, if the operand N is negative,
1013 // we need to add (2^k - 1) before right-shifting by k, i.e.,
Vikram S. Adve645fea32003-05-25 21:59:47 +00001014 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001015 // (N / 2^k) = N >> k, if N >= 0;
1016 // (N + 2^k - 1) >> k, if N < 0
1017 //
1018 // If N is <= 32 bits, use:
1019 // sra N, 31, t1 // t1 = ~0, if N < 0, 0 else
1020 // srl t1, 32-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1021 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1022 // sra t3, k, result // result = N / 2^k
1023 //
1024 // If N is 64 bits, use:
1025 // srax N, k-1, t1 // t1 = sign bit in high k positions
1026 // srlx t1, 64-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1027 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1028 // sra t3, k, result // result = N / 2^k
1029 //
1030 TmpInstruction *sraTmp, *srlTmp, *addTmp;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001031 MachineCodeForInstruction& mcfi
1032 = MachineCodeForInstruction::get(destVal);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001033 sraTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getSign");
1034 srlTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getPlus2km1");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001035 addTmp = new TmpInstruction(mcfi, resultType, LHS, srlTmp,"incIfNeg");
Vikram S. Adve645fea32003-05-25 21:59:47 +00001036
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001037 // Create the SRA or SRAX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001038 mvec.push_back(BuildMI((opSize > 4)? V9::SRAXi6 : V9::SRAi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001039 .addReg(LHS)
1040 .addSImm((resultType==Type::LongTy)? pow-1 : 31)
1041 .addRegDef(sraTmp));
1042
Vikram S. Adve645fea32003-05-25 21:59:47 +00001043 // Create the SRL or SRLX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001044 mvec.push_back(BuildMI((opSize > 4)? V9::SRLXi6 : V9::SRLi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001045 .addReg(sraTmp)
1046 .addSImm((resultType==Type::LongTy)? 64-pow : 32-pow)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001047 .addRegDef(srlTmp));
1048
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001049 // Create the ADD instruction to add 2^pow-1 for negative values
Misha Brukman91aee472003-05-27 22:37:00 +00001050 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001051 .addRegDef(addTmp));
1052
1053 // Get the shift operand and "right-shift" opcode to do the divide
1054 shiftOperand = addTmp;
Vikram S. Advee895a742003-08-06 18:48:40 +00001055 opCode = (opSize > 4)? V9::SRAXi6 : V9::SRAi5;
Misha Brukman7b647942003-05-30 20:11:56 +00001056 } else {
Vikram S. Adve645fea32003-05-25 21:59:47 +00001057 // Get the shift operand and "right-shift" opcode to do the divide
1058 shiftOperand = LHS;
Vikram S. Advee895a742003-08-06 18:48:40 +00001059 opCode = (opSize > 4)? V9::SRLXi6 : V9::SRLi5;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001060 }
1061
1062 // Now do the actual shift!
1063 mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow)
1064 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001065 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001066
Misha Brukmana98cd452003-05-20 20:32:24 +00001067 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
1068 // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve645fea32003-05-25 21:59:47 +00001069 mvec.push_back(CreateIntNegInstruction(target, destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001070 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001071 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001072 } else {
1073 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
1074 double dval = FPC->getValue();
1075 if (fabs(dval) == 1) {
1076 unsigned opCode =
1077 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
1078 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001079
Vikram S. Adve645fea32003-05-25 21:59:47 +00001080 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001081 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001082 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001083 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001084}
1085
1086
Vikram S. Adve74825322002-03-18 03:15:35 +00001087static void
1088CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1089 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001090 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001091 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001092 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001093{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001094 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001095 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001096 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001097 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001098
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001099 // Enforce the alignment constraints on the stack pointer at
1100 // compile time if the total size is a known constant.
Misha Brukman7b647942003-05-30 20:11:56 +00001101 if (isa<Constant>(numElementsVal)) {
1102 bool isValid;
Vikram S. Advee6124d32003-07-29 19:59:23 +00001103 int64_t numElem = (int64_t) target.getInstrInfo().
1104 ConvertConstantToIntType(target, numElementsVal,
1105 numElementsVal->getType(), isValid);
Misha Brukman7b647942003-05-30 20:11:56 +00001106 assert(isValid && "Unexpectedly large array dimension in alloca!");
1107 int64_t total = numElem * tsize;
1108 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1109 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1110 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1111 } else {
1112 // The size is not a constant. Generate code to compute it and
1113 // code to pad the size for stack alignment.
1114 // Create a Value to hold the (constant) element size
1115 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001116
Misha Brukman7b647942003-05-30 20:11:56 +00001117 // Create temporary values to hold the result of MUL, SLL, SRL
Vikram S. Adve80544442003-06-23 02:13:57 +00001118 // To pad `size' to next smallest multiple of 16:
1119 // size = (size + 15) & (-16 = 0xfffffffffffffff0)
1120 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001121 TmpInstruction* tmpProd = new TmpInstruction(mcfi,numElementsVal, tsizeVal);
Vikram S. Adve80544442003-06-23 02:13:57 +00001122 TmpInstruction* tmpAdd15= new TmpInstruction(mcfi,numElementsVal, tmpProd);
1123 TmpInstruction* tmpAndf0= new TmpInstruction(mcfi,numElementsVal, tmpAdd15);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001124
Misha Brukman7b647942003-05-30 20:11:56 +00001125 // Instruction 1: mul numElements, typeSize -> tmpProd
1126 // This will optimize the MUL as far as possible.
Vikram S. Adve80544442003-06-23 02:13:57 +00001127 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd, getMvec,
Misha Brukman7b647942003-05-30 20:11:56 +00001128 mcfi, INVALID_MACHINE_OPCODE);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001129
Vikram S. Adve80544442003-06-23 02:13:57 +00001130 // Instruction 2: andn tmpProd, 0x0f -> tmpAndn
1131 getMvec.push_back(BuildMI(V9::ADDi, 3).addReg(tmpProd).addSImm(15)
1132 .addReg(tmpAdd15, MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001133
Vikram S. Adve80544442003-06-23 02:13:57 +00001134 // Instruction 3: add tmpAndn, 0x10 -> tmpAdd16
1135 getMvec.push_back(BuildMI(V9::ANDi, 3).addReg(tmpAdd15).addSImm(-16)
1136 .addReg(tmpAndf0, MOTy::Def));
1137
1138 totalSizeVal = tmpAndf0;
Misha Brukman7b647942003-05-30 20:11:56 +00001139 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001140
1141 // Get the constant offset from SP for dynamically allocated storage
1142 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001143 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001144 bool growUp;
1145 ConstantSInt* dynamicAreaOffset =
1146 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001147 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001148 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1149
Chris Lattner54e898e2003-01-15 19:23:34 +00001150 unsigned SPReg = target.getRegInfo().getStackPointer();
1151
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001152 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukman91aee472003-05-27 22:37:00 +00001153 getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001154 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001155
Vikram S. Adve74825322002-03-18 03:15:35 +00001156 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukman91aee472003-05-27 22:37:00 +00001157 getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset)
Misha Brukmana98cd452003-05-20 20:32:24 +00001158 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001159}
1160
1161
1162static void
1163CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1164 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001165 unsigned tsize,
1166 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001167 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001168{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001169 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001170 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001171 "Result value is not part of a function?");
1172 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001173 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001174
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001175 // Put the variable in the dynamically sized area of the frame if either:
1176 // (a) The offset is too large to use as an immediate in load/stores
1177 // (check LDX because all load/stores have the same-size immed. field).
1178 // (b) The object is "large", so it could cause many other locals,
1179 // spills, and temporaries to have large offsets.
1180 // NOTE: We use LARGE = 8 * argSlotSize = 64 bytes.
1181 // You've gotta love having only 13 bits for constant offset values :-|.
1182 //
1183 unsigned paddedSize;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001184 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001185 paddedSize,
1186 tsize * numElements);
1187
1188 if (((int)paddedSize) > 8 * target.getFrameInfo().getSizeOfEachArgOnStack() ||
1189 ! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001190 CreateCodeForVariableSizeAlloca(target, result, tsize,
1191 ConstantSInt::get(Type::IntTy,numElements),
1192 getMvec);
1193 return;
1194 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001195
1196 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001197 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001198
1199 // Create a temporary Value to hold the constant offset.
1200 // This is needed because it may not fit in the immediate field.
1201 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1202
1203 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001204 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukman91aee472003-05-27 22:37:00 +00001205 getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001206 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001207}
1208
1209
Chris Lattner20b1ea02001-09-14 03:47:57 +00001210//------------------------------------------------------------------------
1211// Function SetOperandsForMemInstr
1212//
1213// Choose addressing mode for the given load or store instruction.
1214// Use [reg+reg] if it is an indexed reference, and the index offset is
1215// not a constant or if it cannot fit in the offset field.
1216// Use [reg+offset] in all other cases.
1217//
1218// This assumes that all array refs are "lowered" to one of these forms:
1219// %x = load (subarray*) ptr, constant ; single constant offset
1220// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1221// Generally, this should happen via strength reduction + LICM.
1222// Also, strength reduction should take care of using the same register for
1223// the loop index variable and an array index, when that is profitable.
1224//------------------------------------------------------------------------
1225
1226static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001227SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001228 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001229 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001230 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001231{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001232 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001233 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001234 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001235 bool allConstantIndices;
1236 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001237
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001238 // Now create the appropriate operands for the machine instruction.
1239 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001240 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001241 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001242 MachineOperand::MachineOperandType offsetOpType =
1243 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001244
Vikram S. Adve74825322002-03-18 03:15:35 +00001245 // Check if there is an index vector and if so, compute the
1246 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001247 //
Misha Brukman7b647942003-05-30 20:11:56 +00001248 if (!idxVec.empty()) {
1249 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001250
Misha Brukman7b647942003-05-30 20:11:56 +00001251 // If all indices are constant, compute the combined offset directly.
1252 if (allConstantIndices) {
1253 // Compute the offset value using the index vector. Create a
1254 // virtual reg. for it since it may not fit in the immed field.
1255 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
1256 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
1257 } else {
1258 // There is at least one non-constant offset. Therefore, this must
1259 // be an array ref, and must have been lowered to a single non-zero
1260 // offset. (An extra leading zero offset, if any, can be ignored.)
1261 // Generate code sequence to compute address from index.
1262 //
1263 bool firstIdxIsZero = IsZero(idxVec[0]);
1264 assert(idxVec.size() == 1U + firstIdxIsZero
1265 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001266
Misha Brukman7b647942003-05-30 20:11:56 +00001267 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001268
Misha Brukman7b647942003-05-30 20:11:56 +00001269 std::vector<MachineInstr*> mulVec;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001270 Instruction* addr =
1271 new TmpInstruction(MachineCodeForInstruction::get(memInst),
1272 Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001273
Misha Brukman7b647942003-05-30 20:11:56 +00001274 // Get the array type indexed by idxVal, and compute its element size.
1275 // The call to getTypeSize() will fail if size is not constant.
1276 const Type* vecType = (firstIdxIsZero
1277 ? GetElementPtrInst::getIndexedType(ptrType,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001278 std::vector<Value*>(1U, idxVec[0]),
1279 /*AllowCompositeLeaf*/ true)
1280 : ptrType);
Misha Brukman7b647942003-05-30 20:11:56 +00001281 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1282 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
1283 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001284
Misha Brukman7b647942003-05-30 20:11:56 +00001285 // CreateMulInstruction() folds constants intelligently enough.
1286 CreateMulInstruction(target, memInst->getParent()->getParent(),
1287 idxVal, /* lval, not likely to be const*/
1288 eltSizeVal, /* rval, likely to be constant */
1289 addr, /* result */
1290 mulVec, MachineCodeForInstruction::get(memInst),
1291 INVALID_MACHINE_OPCODE);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001292
Misha Brukman7b647942003-05-30 20:11:56 +00001293 assert(mulVec.size() > 0 && "No multiply code created?");
1294 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
1295
1296 valueForRegOffset = addr;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001297 }
Misha Brukman7b647942003-05-30 20:11:56 +00001298 } else {
1299 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1300 smallConstOffset = 0;
1301 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001302
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001303 // For STORE:
1304 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1305 // For LOAD or GET_ELEMENT_PTR,
1306 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1307 //
1308 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001309 MachineInstr *MI;
1310 if (memInst->getOpcode() == Instruction::Store) {
Misha Brukman7b647942003-05-30 20:11:56 +00001311 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001312 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1313 .addReg(ptrVal).addReg(valueForRegOffset);
Misha Brukman7b647942003-05-30 20:11:56 +00001314 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001315 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001316 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1317 .addReg(ptrVal).addSImm(smallConstOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001318 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001319 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00001320 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001321 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1322 .addRegDef(memInst);
Misha Brukman7b647942003-05-30 20:11:56 +00001323 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001324 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001325 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1326 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001327 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001328 }
1329 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001330}
1331
1332
Chris Lattner20b1ea02001-09-14 03:47:57 +00001333//
1334// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001335// in place of the use(s) of that instruction in node `parent'.
1336// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001337// Also make sure to skip over a parent who:
1338// (1) is a list node in the Burg tree, or
1339// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001340//
1341static void
1342ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001343 InstrTreeNode* parent,
1344 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001345{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001346 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1347
Chris Lattner20b1ea02001-09-14 03:47:57 +00001348 Instruction* unusedOp = treeNode->getInstruction();
1349 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001350
1351 // The parent itself may be a list node, so find the real parent instruction
1352 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1353 {
1354 parent = parent->parent();
1355 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1356 }
1357 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1358
1359 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001360 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001361
1362 // The parent's mvec would be empty if it was itself forwarded.
1363 // Recursively call ForwardOperand in that case...
1364 //
Misha Brukman7b647942003-05-30 20:11:56 +00001365 if (mvec.size() == 0) {
1366 assert(parent->parent() != NULL &&
1367 "Parent could not have been forwarded, yet has no instructions?");
1368 ForwardOperand(treeNode, parent->parent(), operandNum);
1369 } else {
1370 for (unsigned i=0, N=mvec.size(); i < N; i++) {
1371 MachineInstr* minstr = mvec[i];
1372 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) {
1373 const MachineOperand& mop = minstr->getOperand(i);
1374 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
1375 mop.getVRegValue() == unusedOp)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001376 {
Misha Brukman7b647942003-05-30 20:11:56 +00001377 minstr->SetMachineOperandVal(i, MachineOperand::MO_VirtualRegister,
1378 fwdOp);
1379 }
1380 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001381
Misha Brukman7b647942003-05-30 20:11:56 +00001382 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
Chris Lattner907b7dc2003-08-05 16:59:24 +00001383 if (minstr->getImplicitRef(i) == unusedOp)
1384 minstr->setImplicitRef(i, fwdOp);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001385 }
Misha Brukman7b647942003-05-30 20:11:56 +00001386 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001387}
1388
1389
Vikram S. Adve242a8082002-05-19 15:25:51 +00001390inline bool
1391AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001392{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001393 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1394 UI != UE; ++UI)
1395 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1396 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1397 return false;
1398 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399}
1400
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001401// Generate code for any intrinsic that needs a special code sequence
1402// instead of a regular call. If not that kind of intrinsic, do nothing.
1403// Returns true if code was generated, otherwise false.
1404//
1405bool CodeGenIntrinsic(LLVMIntrinsic::ID iid, CallInst &callInstr,
1406 TargetMachine &target,
1407 std::vector<MachineInstr*>& mvec)
1408{
1409 switch (iid) {
1410 case LLVMIntrinsic::va_start: {
1411 // Get the address of the first vararg value on stack and copy it to
1412 // the argument of va_start(va_list* ap).
1413 bool ignore;
1414 Function* func = cast<Function>(callInstr.getParent()->getParent());
1415 int numFixedArgs = func->getFunctionType()->getNumParams();
1416 int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum();
1417 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
1418 int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo().
1419 getFirstIncomingArgOffset(MachineFunction::get(func), ignore);
Misha Brukman91aee472003-05-27 22:37:00 +00001420 mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001421 addReg(callInstr.getOperand(1)));
1422 return true;
1423 }
1424
1425 case LLVMIntrinsic::va_end:
1426 return true; // no-op on Sparc
1427
1428 case LLVMIntrinsic::va_copy:
1429 // Simple copy of current va_list (arg2) to new va_list (arg1)
Misha Brukman91aee472003-05-27 22:37:00 +00001430 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001431 addMReg(target.getRegInfo().getZeroRegNum()).
1432 addReg(callInstr.getOperand(2)).
1433 addReg(callInstr.getOperand(1)));
1434 return true;
1435
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001436 case LLVMIntrinsic::setjmp: {
1437 // act as if we return 0
1438 unsigned g0 = target.getRegInfo().getZeroRegNum();
1439 mvec.push_back(BuildMI(V9::ORr,3).addMReg(g0).addMReg(g0)
1440 .addReg(&callInstr, MOTy::Def));
1441 return true;
1442 }
1443
1444 case LLVMIntrinsic::longjmp: {
1445 // call abort()
1446 Module* M = callInstr.getParent()->getParent()->getParent();
1447 Function *F = M->getNamedFunction("abort");
1448 mvec.push_back(BuildMI(V9::CALL, 1).addReg(F));
1449 return true;
1450 }
1451
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001452 default:
1453 return false;
1454 }
1455}
1456
Vikram S. Advefb361122001-10-22 13:36:31 +00001457//******************* Externally Visible Functions *************************/
1458
Vikram S. Advefb361122001-10-22 13:36:31 +00001459//------------------------------------------------------------------------
1460// External Function: ThisIsAChainRule
1461//
1462// Purpose:
1463// Check if a given BURG rule is a chain rule.
1464//------------------------------------------------------------------------
1465
1466extern bool
1467ThisIsAChainRule(int eruleno)
1468{
1469 switch(eruleno)
1470 {
1471 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001472 case 123:
1473 case 124:
1474 case 125:
1475 case 126:
1476 case 127:
1477 case 128:
1478 case 129:
1479 case 130:
1480 case 131:
1481 case 132:
1482 case 133:
1483 case 155:
1484 case 221:
1485 case 222:
1486 case 241:
1487 case 242:
1488 case 243:
1489 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001490 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001491 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001492 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001493
Vikram S. Advefb361122001-10-22 13:36:31 +00001494 default:
1495 return false; break;
1496 }
1497}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001498
1499
1500//------------------------------------------------------------------------
1501// External Function: GetInstructionsByRule
1502//
1503// Purpose:
1504// Choose machine instructions for the SPARC according to the
1505// patterns chosen by the BURG-generated parser.
1506//------------------------------------------------------------------------
1507
Vikram S. Adve74825322002-03-18 03:15:35 +00001508void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001509GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001510 int ruleForNode,
1511 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001512 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001513 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001514{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001515 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001516 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001517 int nextRule;
1518 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001519 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001520 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001521 unsigned L;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001522 bool foldCase = false;
Vikram S. Adve74825322002-03-18 03:15:35 +00001523
1524 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001525
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001526 // If the code for this instruction was folded into the parent (user),
1527 // then do nothing!
1528 if (subtreeRoot->isFoldedIntoParent())
1529 return;
1530
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001531 //
1532 // Let's check for chain rules outside the switch so that we don't have
1533 // to duplicate the list of chain rule production numbers here again
1534 //
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001535 if (ThisIsAChainRule(ruleForNode))
1536 {
1537 // Chain rules have a single nonterminal on the RHS.
1538 // Get the rule that matches the RHS non-terminal and use that instead.
1539 //
1540 assert(nts[0] && ! nts[1]
1541 && "A chain rule should have only one RHS non-terminal!");
1542 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1543 nts = burm_nts[nextRule];
1544 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
1545 }
1546 else
1547 {
1548 switch(ruleForNode) {
1549 case 1: // stmt: Ret
1550 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001551 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001552 // for moving return value to appropriate register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001553 // Copy the return value to the required return register.
1554 // Mark the return Value as an implicit ref of the RET instr..
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001555 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001556 // Finally put a NOP in the delay slot.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001557 ReturnInst *returnInstr=cast<ReturnInst>(subtreeRoot->getInstruction());
1558 Value* retVal = returnInstr->getReturnValue();
1559 MachineCodeForInstruction& mcfi =
1560 MachineCodeForInstruction::get(returnInstr);
1561
1562 // Create a hidden virtual reg to represent the return address register
1563 // used by the machine instruction but not represented in LLVM.
1564 //
1565 Instruction* returnAddrTmp = new TmpInstruction(mcfi, returnInstr);
1566
1567 MachineInstr* retMI =
1568 BuildMI(V9::JMPLRETi, 3).addReg(returnAddrTmp).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001569 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001570
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001571 // If there is a value to return, we need to:
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001572 // (a) Sign-extend the value if it is smaller than 8 bytes (reg size)
1573 // (b) Insert a copy to copy the return value to the appropriate reg.
1574 // -- For FP values, create a FMOVS or FMOVD instruction
1575 // -- For non-FP values, create an add-with-0 instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001576 //
1577 if (retVal != NULL) {
1578 const UltraSparcRegInfo& regInfo =
1579 (UltraSparcRegInfo&) target.getRegInfo();
1580 const Type* retType = retVal->getType();
1581 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
1582 unsigned retRegNum = (retType->isFloatingPoint()
1583 ? (unsigned) SparcFloatRegClass::f0
1584 : (unsigned) SparcIntRegClass::i0);
1585 retRegNum = regInfo.getUnifiedRegNum(regClassID, retRegNum);
1586
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001587 // () Insert sign-extension instructions for small signed values.
1588 //
1589 Value* retValToUse = retVal;
1590 if (retType->isIntegral() && retType->isSigned()) {
1591 unsigned retSize = target.getTargetData().getTypeSize(retType);
1592 if (retSize <= 4) {
1593 // create a temporary virtual reg. to hold the sign-extension
1594 retValToUse = new TmpInstruction(mcfi, retVal);
1595
1596 // sign-extend retVal and put the result in the temporary reg.
1597 target.getInstrInfo().CreateSignExtensionInstructions
1598 (target, returnInstr->getParent()->getParent(),
1599 retVal, retValToUse, 8*retSize, mvec, mcfi);
1600 }
1601 }
1602
1603 // (b) Now, insert a copy to to the appropriate register:
1604 // -- For FP values, create a FMOVS or FMOVD instruction
1605 // -- For non-FP values, create an add-with-0 instruction
1606 //
1607 // First, create a virtual register to represent the register and
1608 // mark this vreg as being an implicit operand of the ret MI.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001609 TmpInstruction* retVReg =
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001610 new TmpInstruction(mcfi, retValToUse, NULL, "argReg");
1611
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001612 retMI->addImplicitRef(retVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001613
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001614 if (retType->isFloatingPoint())
1615 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001616 .addReg(retValToUse).addReg(retVReg, MOTy::Def));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001617 else
1618 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001619 .addReg(retValToUse).addSImm((int64_t) 0)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001620 .addReg(retVReg, MOTy::Def));
1621
1622 // Mark the operand with the register it should be assigned
1623 M->SetRegForOperand(M->getNumOperands()-1, retRegNum);
1624 retMI->SetRegForImplicitRef(retMI->getNumImplicitRefs()-1, retRegNum);
1625
1626 mvec.push_back(M);
1627 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001628
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001629 // Now insert the RET instruction and a NOP for the delay slot
1630 mvec.push_back(retMI);
Misha Brukmana98cd452003-05-20 20:32:24 +00001631 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001632
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001633 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001634 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001635
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001636 case 3: // stmt: Store(reg,reg)
1637 case 4: // stmt: Store(reg,ptrreg)
1638 SetOperandsForMemInstr(ChooseStoreInstruction(
Chris Lattner54e898e2003-01-15 19:23:34 +00001639 subtreeRoot->leftChild()->getValue()->getType()),
1640 mvec, subtreeRoot, target);
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001641 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001642
1643 case 5: // stmt: BrUncond
1644 {
1645 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1646 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
1647
1648 // delay slot
1649 mvec.push_back(BuildMI(V9::NOP, 0));
1650 break;
1651 }
1652
1653 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001654 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001655 // If the constant is ZERO, we can use the branch-on-integer-register
1656 // instructions and avoid the SUBcc instruction entirely.
1657 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001658 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001659 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1660 assert(constNode &&
1661 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001662 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001663 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001664
Chris Lattner0c4e8862002-09-03 01:08:28 +00001665 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001666 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00001667 && target.getInstrInfo().ConvertConstantToIntType(target,
1668 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001669 && isValidConst)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001670 {
1671 // That constant is a zero after all...
1672 // Use the left child of setCC as the first argument!
1673 // Mark the setCC node so that no code is generated for it.
1674 InstructionNode* setCCNode = (InstructionNode*)
1675 subtreeRoot->leftChild();
1676 assert(setCCNode->getOpLabel() == SetCCOp);
1677 setCCNode->markFoldedIntoParent();
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001678
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001679 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001680
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001681 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1682 .addReg(setCCNode->leftChild()->getValue())
1683 .addPCDisp(brInst->getSuccessor(0));
1684 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001685
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001686 // delay slot
1687 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001688
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001689 // false branch
1690 mvec.push_back(BuildMI(V9::BA, 1)
1691 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001692
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001693 // delay slot
1694 mvec.push_back(BuildMI(V9::NOP, 0));
1695 break;
1696 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001697 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001698 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001699
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001700 case 6: // stmt: BrCond(setCC)
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001701 { // bool => boolean was computed with SetCC.
1702 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001703 // If it is an integer CC, we also need to find the unique
1704 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001705 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001706 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +00001707 const Type* setCCType;
1708 unsigned Opcode = ChooseBccInstruction(subtreeRoot, setCCType);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001709 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1710 brInst->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00001711 setCCType,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001712 MachineCodeForInstruction::get(brInst));
Chris Lattner54e898e2003-01-15 19:23:34 +00001713 M = BuildMI(Opcode, 2).addCCReg(ccValue)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001714 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001715 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001716
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001717 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001718 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001719
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001720 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001721 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001722
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001723 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001724 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001725 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001726 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001727
1728 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001729 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001730 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001731 Constant* constVal =
1732 cast<Constant>(subtreeRoot->leftChild()->getValue());
1733 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001734
Misha Brukmana98cd452003-05-20 20:32:24 +00001735 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001736 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001737 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001738
1739 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001740 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001741 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001742 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001743
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001744 case 8: // stmt: BrCond(boolreg)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001745 { // boolreg => boolean is recorded in an integer register.
1746 // Use branch-on-integer-register instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001747 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001748 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001749 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001750 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001751 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001752
1753 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001754 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001755
1756 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001757 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001758
1759 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001760 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001761 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001762 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001763
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001764 case 9: // stmt: Switch(reg)
1765 assert(0 && "*** SWITCH instruction is not implemented yet.");
1766 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001767
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001768 case 10: // reg: VRegList(reg, reg)
1769 assert(0 && "VRegList should never be the topmost non-chain rule");
1770 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001771
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001772 case 21: // bool: Not(bool,reg): Compute with a conditional-move-on-reg
1773 { // First find the unary operand. It may be left or right, usually right.
1774 Instruction* notI = subtreeRoot->getInstruction();
1775 Value* notArg = BinaryOperator::getNotArgument(
1776 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1777 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1778
1779 // Unconditionally set register to 0
1780 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(notI));
1781
1782 // Now conditionally move 1 into the register.
1783 // Mark the register as a use (as well as a def) because the old
1784 // value will be retained if the condition is false.
1785 mvec.push_back(BuildMI(V9::MOVRZi, 3).addReg(notArg).addZImm(1)
1786 .addReg(notI, MOTy::UseAndDef));
1787
1788 break;
1789 }
1790
1791 case 421: // reg: BNot(reg,reg): Compute as reg = reg XOR-NOT 0
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001792 { // First find the unary operand. It may be left or right, usually right.
1793 Value* notArg = BinaryOperator::getNotArgument(
1794 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001795 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001796 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001797 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001798 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001799 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001800
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001801 case 322: // reg: Not(tobool, reg):
1802 // Fold CAST-TO-BOOL with NOT by inverting the sense of cast-to-bool
1803 foldCase = true;
1804 // Just fall through!
1805
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001806 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001807 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001808 Instruction* castI = subtreeRoot->getInstruction();
1809 Value* opVal = subtreeRoot->leftChild()->getValue();
1810 assert(opVal->getType()->isIntegral() ||
1811 isa<PointerType>(opVal->getType()));
1812
1813 // Unconditionally set register to 0
1814 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(castI));
1815
1816 // Now conditionally move 1 into the register.
1817 // Mark the register as a use (as well as a def) because the old
1818 // value will be retained if the condition is false.
1819 MachineOpCode opCode = foldCase? V9::MOVRZi : V9::MOVRNZi;
1820 mvec.push_back(BuildMI(opCode, 3).addReg(opVal).addZImm(1)
1821 .addReg(castI, MOTy::UseAndDef));
1822
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001823 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001824 }
1825
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001826 case 23: // reg: ToUByteTy(reg)
1827 case 24: // reg: ToSByteTy(reg)
1828 case 25: // reg: ToUShortTy(reg)
1829 case 26: // reg: ToShortTy(reg)
1830 case 27: // reg: ToUIntTy(reg)
1831 case 28: // reg: ToIntTy(reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001832 case 29: // reg: ToULongTy(reg)
1833 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001834 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001835 //======================================================================
1836 // Rules for integer conversions:
1837 //
1838 //--------
1839 // From ISO 1998 C++ Standard, Sec. 4.7:
1840 //
1841 // 2. If the destination type is unsigned, the resulting value is
1842 // the least unsigned integer congruent to the source integer
1843 // (modulo 2n where n is the number of bits used to represent the
1844 // unsigned type). [Note: In a two s complement representation,
1845 // this conversion is conceptual and there is no change in the
1846 // bit pattern (if there is no truncation). ]
1847 //
1848 // 3. If the destination type is signed, the value is unchanged if
1849 // it can be represented in the destination type (and bitfield width);
1850 // otherwise, the value is implementation-defined.
1851 //--------
1852 //
1853 // Since we assume 2s complement representations, this implies:
1854 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001855 // -- If operand is smaller than destination, zero-extend or sign-extend
1856 // according to the signedness of the *operand*: source decides:
1857 // (1) If operand is signed, sign-extend it.
1858 // If dest is unsigned, zero-ext the result!
1859 // (2) If operand is unsigned, our current invariant is that
1860 // it's high bits are correct, so zero-extension is not needed.
Vikram S. Adve94c40812002-09-27 14:33:08 +00001861 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001862 // -- If operand is same size as or larger than destination,
1863 // zero-extend or sign-extend according to the signedness of
1864 // the *destination*: destination decides:
1865 // (1) If destination is signed, sign-extend (truncating if needed)
1866 // This choice is implementation defined. We sign-extend the
1867 // operand, which matches both Sun's cc and gcc3.2.
1868 // (2) If destination is unsigned, zero-extend (truncating if needed)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001869 //======================================================================
1870
Vikram S. Adve242a8082002-05-19 15:25:51 +00001871 Instruction* destI = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001872 Function* currentFunc = destI->getParent()->getParent();
1873 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(destI);
1874
Vikram S. Adve242a8082002-05-19 15:25:51 +00001875 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001876 const Type* opType = opVal->getType();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001877 const Type* destType = destI->getType();
1878 unsigned opSize = target.getTargetData().getTypeSize(opType);
1879 unsigned destSize = target.getTargetData().getTypeSize(destType);
1880
1881 bool isIntegral = opType->isIntegral() || isa<PointerType>(opType);
1882
1883 if (opType == Type::BoolTy ||
1884 opType == destType ||
1885 isIntegral && opSize == destSize && opSize == 8) {
1886 // nothing to do in all these cases
1887 forwardOperandNum = 0; // forward first operand to user
1888
Misha Brukman7b647942003-05-30 20:11:56 +00001889 } else if (opType->isFloatingPoint()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001890
1891 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Advee895a742003-08-06 18:48:40 +00001892 if (destI->getType()->isUnsigned() && destI->getType() !=Type::UIntTy)
Misha Brukman7b647942003-05-30 20:11:56 +00001893 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve1e606692002-07-31 21:01:34 +00001894
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001895 } else if (isIntegral) {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001896
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001897 bool opSigned = opType->isSigned();
1898 bool destSigned = destType->isSigned();
1899 unsigned extSourceInBits = 8 * std::min<unsigned>(opSize, destSize);
1900
1901 assert(! (opSize == destSize && opSigned == destSigned) &&
1902 "How can different int types have same size and signedness?");
1903
1904 bool signExtend = (opSize < destSize && opSigned ||
1905 opSize >= destSize && destSigned);
1906
1907 bool signAndZeroExtend = (opSize < destSize && destSize < 8u &&
1908 opSigned && !destSigned);
1909 assert(!signAndZeroExtend || signExtend);
1910
1911 bool zeroExtendOnly = opSize >= destSize && !destSigned;
1912 assert(!zeroExtendOnly || !signExtend);
1913
1914 if (signExtend) {
1915 Value* signExtDest = (signAndZeroExtend
1916 ? new TmpInstruction(mcfi, destType, opVal)
1917 : destI);
1918
1919 target.getInstrInfo().CreateSignExtensionInstructions
1920 (target, currentFunc,opVal,signExtDest,extSourceInBits,mvec,mcfi);
1921
1922 if (signAndZeroExtend)
1923 target.getInstrInfo().CreateZeroExtensionInstructions
1924 (target, currentFunc, signExtDest, destI, 8*destSize, mvec, mcfi);
1925 }
1926 else if (zeroExtendOnly) {
1927 target.getInstrInfo().CreateZeroExtensionInstructions
1928 (target, currentFunc, opVal, destI, extSourceInBits, mvec, mcfi);
1929 }
1930 else
1931 forwardOperandNum = 0; // forward first operand to user
1932
Misha Brukman7b647942003-05-30 20:11:56 +00001933 } else
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001934 assert(0 && "Unrecognized operand type for convert-to-integer");
1935
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001936 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001937 }
1938
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001939 case 31: // reg: ToFloatTy(reg):
1940 case 32: // reg: ToDoubleTy(reg):
1941 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001942
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001943 // If this instruction has a parent (a user) in the tree
1944 // and the user is translated as an FsMULd instruction,
1945 // then the cast is unnecessary. So check that first.
1946 // In the future, we'll want to do the same for the FdMULq instruction,
1947 // so do the check here instead of only for ToFloatTy(reg).
1948 //
1949 if (subtreeRoot->parent() != NULL) {
1950 const MachineCodeForInstruction& mcfi =
1951 MachineCodeForInstruction::get(
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001952 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001953 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
1954 forwardOperandNum = 0; // forward first operand to user
1955 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001956
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001957 if (forwardOperandNum != 0) { // we do need the cast
1958 Value* leftVal = subtreeRoot->leftChild()->getValue();
1959 const Type* opType = leftVal->getType();
Vikram S. Advee895a742003-08-06 18:48:40 +00001960 MachineOpCode opCode=ChooseConvertToFloatInstr(target,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001961 subtreeRoot->getOpLabel(), opType);
Vikram S. Advee895a742003-08-06 18:48:40 +00001962 if (opCode == V9::NOP) { // no conversion needed
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001963 forwardOperandNum = 0; // forward first operand to user
1964 } else {
1965 // If the source operand is a non-FP type it must be
1966 // first copied from int to float register via memory!
1967 Instruction *dest = subtreeRoot->getInstruction();
1968 Value* srcForCast;
1969 int n = 0;
1970 if (! opType->isFloatingPoint()) {
1971 // Create a temporary to represent the FP register
1972 // into which the integer will be copied via memory.
1973 // The type of this temporary will determine the FP
1974 // register used: single-prec for a 32-bit int or smaller,
1975 // double-prec for a 64-bit int.
1976 //
1977 uint64_t srcSize =
1978 target.getTargetData().getTypeSize(leftVal->getType());
1979 Type* tmpTypeToUse =
1980 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001981 MachineCodeForInstruction &destMCFI =
1982 MachineCodeForInstruction::get(dest);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001983 srcForCast = new TmpInstruction(destMCFI, tmpTypeToUse, dest);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001984
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001985 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001986 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001987 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001988 mvec, destMCFI);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001989 } else
1990 srcForCast = leftVal;
1991
1992 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
1993 mvec.push_back(M);
1994 }
Misha Brukman7b647942003-05-30 20:11:56 +00001995 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001996 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001997
1998 case 19: // reg: ToArrayTy(reg):
1999 case 20: // reg: ToPointerTy(reg):
2000 forwardOperandNum = 0; // forward first operand to user
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002001 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002002
2003 case 233: // reg: Add(reg, Constant)
2004 maskUnsignedResult = true;
2005 M = CreateAddConstInstruction(subtreeRoot);
2006 if (M != NULL) {
2007 mvec.push_back(M);
2008 break;
2009 }
2010 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002011
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002012 case 33: // reg: Add(reg, reg)
2013 maskUnsignedResult = true;
2014 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
2015 break;
2016
2017 case 234: // reg: Sub(reg, Constant)
2018 maskUnsignedResult = true;
2019 M = CreateSubConstInstruction(subtreeRoot);
2020 if (M != NULL) {
2021 mvec.push_back(M);
2022 break;
2023 }
2024 // ELSE FALL THROUGH
2025
2026 case 34: // reg: Sub(reg, reg)
2027 maskUnsignedResult = true;
2028 Add3OperandInstr(ChooseSubInstructionByType(
Chris Lattner54e898e2003-01-15 19:23:34 +00002029 subtreeRoot->getInstruction()->getType()),
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002030 subtreeRoot, mvec);
2031 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002032
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002033 case 135: // reg: Mul(todouble, todouble)
2034 checkCast = true;
2035 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002036
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002037 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002038 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002039 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002040 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002041 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002042 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002043 Instruction* mulInstr = subtreeRoot->getInstruction();
2044 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002045 subtreeRoot->leftChild()->getValue(),
2046 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002047 mulInstr, mvec,
2048 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002049 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002050 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002051 case 335: // reg: Mul(todouble, todoubleConst)
2052 checkCast = true;
2053 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002054
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002055 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00002056 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002057 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002058 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002059 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002060 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002061 Instruction* mulInstr = subtreeRoot->getInstruction();
2062 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002063 subtreeRoot->leftChild()->getValue(),
2064 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002065 mulInstr, mvec,
2066 MachineCodeForInstruction::get(mulInstr),
2067 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002068 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002069 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002070 case 236: // reg: Div(reg, Constant)
2071 maskUnsignedResult = true;
2072 L = mvec.size();
2073 CreateDivConstInstruction(target, subtreeRoot, mvec);
2074 if (mvec.size() > L)
2075 break;
2076 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002077
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002078 case 36: // reg: Div(reg, reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002079 {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002080 maskUnsignedResult = true;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002081
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002082 // If either operand of divide is smaller than 64 bits, we have
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002083 // to make sure the unused top bits are correct because they affect
2084 // the result. These bits are already correct for unsigned values.
2085 // They may be incorrect for signed values, so sign extend to fill in.
2086 Instruction* divI = subtreeRoot->getInstruction();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002087 Value* divOp1 = subtreeRoot->leftChild()->getValue();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002088 Value* divOp2 = subtreeRoot->rightChild()->getValue();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002089 Value* divOp1ToUse = divOp1;
2090 Value* divOp2ToUse = divOp2;
2091 if (divI->getType()->isSigned()) {
2092 unsigned opSize=target.getTargetData().getTypeSize(divI->getType());
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002093 if (opSize < 8) {
2094 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(divI);
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002095 divOp1ToUse = new TmpInstruction(mcfi, divOp1);
2096 divOp2ToUse = new TmpInstruction(mcfi, divOp2);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002097 target.getInstrInfo().
2098 CreateSignExtensionInstructions(target,
2099 divI->getParent()->getParent(),
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002100 divOp1, divOp1ToUse,
2101 8*opSize, mvec, mcfi);
2102 target.getInstrInfo().
2103 CreateSignExtensionInstructions(target,
2104 divI->getParent()->getParent(),
2105 divOp2, divOp2ToUse,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002106 8*opSize, mvec, mcfi);
2107 }
2108 }
2109
2110 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002111 .addReg(divOp1ToUse)
2112 .addReg(divOp2ToUse)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002113 .addRegDef(divI));
2114
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002115 break;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002116 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002117
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002118 case 37: // reg: Rem(reg, reg)
2119 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00002120 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002121 maskUnsignedResult = true;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002122
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002123 Instruction* remI = subtreeRoot->getInstruction();
2124 Value* divOp1 = subtreeRoot->leftChild()->getValue();
2125 Value* divOp2 = subtreeRoot->rightChild()->getValue();
2126
2127 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(remI);
Vikram S. Adve510eec72001-11-04 21:59:14 +00002128
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002129 // If second operand of divide is smaller than 64 bits, we have
2130 // to make sure the unused top bits are correct because they affect
2131 // the result. These bits are already correct for unsigned values.
2132 // They may be incorrect for signed values, so sign extend to fill in.
2133 //
2134 Value* divOpToUse = divOp2;
2135 if (divOp2->getType()->isSigned()) {
2136 unsigned opSize=target.getTargetData().getTypeSize(divOp2->getType());
2137 if (opSize < 8) {
2138 divOpToUse = new TmpInstruction(mcfi, divOp2);
2139 target.getInstrInfo().
2140 CreateSignExtensionInstructions(target,
2141 remI->getParent()->getParent(),
2142 divOp2, divOpToUse,
2143 8*opSize, mvec, mcfi);
2144 }
2145 }
2146
2147 // Now compute: result = rem V1, V2 as:
2148 // result = V1 - (V1 / signExtend(V2)) * signExtend(V2)
2149 //
2150 TmpInstruction* quot = new TmpInstruction(mcfi, divOp1, divOpToUse);
2151 TmpInstruction* prod = new TmpInstruction(mcfi, quot, divOpToUse);
2152
2153 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
2154 .addReg(divOp1).addReg(divOpToUse).addRegDef(quot));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002155
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002156 mvec.push_back(BuildMI(ChooseMulInstructionByType(remI->getType()), 3)
2157 .addReg(quot).addReg(divOpToUse).addRegDef(prod));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002158
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002159 mvec.push_back(BuildMI(ChooseSubInstructionByType(remI->getType()), 3)
2160 .addReg(divOp1).addReg(prod).addRegDef(remI));
2161
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002162 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00002163 }
2164
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002165 case 38: // bool: And(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002166 case 138: // bool: And(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002167 case 238: // bool: And(bool, boolconst)
2168 case 338: // reg : BAnd(reg, reg)
2169 case 538: // reg : BAnd(reg, Constant)
2170 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
2171 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002172
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002173 case 438: // bool: BAnd(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002174 { // Use the argument of NOT as the second argument!
2175 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002176 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002177 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2178 Value* notArg = BinaryOperator::getNotArgument(
2179 cast<BinaryOperator>(notNode->getInstruction()));
2180 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002181 Value *lhs = subtreeRoot->leftChild()->getValue();
2182 Value *dest = subtreeRoot->getValue();
2183 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(lhs).addReg(notArg)
2184 .addReg(dest, MOTy::Def));
2185
2186 if (notArg->getType() == Type::BoolTy)
2187 { // set 1 in result register if result of above is non-zero
2188 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2189 .addReg(dest, MOTy::UseAndDef));
2190 }
2191
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002192 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002193 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002194
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002195 case 39: // bool: Or(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002196 case 139: // bool: Or(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002197 case 239: // bool: Or(bool, boolconst)
2198 case 339: // reg : BOr(reg, reg)
2199 case 539: // reg : BOr(reg, Constant)
2200 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
2201 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002202
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002203 case 439: // bool: BOr(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002204 { // Use the argument of NOT as the second argument!
2205 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002206 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002207 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2208 Value* notArg = BinaryOperator::getNotArgument(
2209 cast<BinaryOperator>(notNode->getInstruction()));
2210 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002211 Value *lhs = subtreeRoot->leftChild()->getValue();
2212 Value *dest = subtreeRoot->getValue();
2213
2214 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(lhs).addReg(notArg)
2215 .addReg(dest, MOTy::Def));
2216
2217 if (notArg->getType() == Type::BoolTy)
2218 { // set 1 in result register if result of above is non-zero
2219 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2220 .addReg(dest, MOTy::UseAndDef));
2221 }
2222
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002223 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002224 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002225
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002226 case 40: // bool: Xor(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002227 case 140: // bool: Xor(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002228 case 240: // bool: Xor(bool, boolconst)
2229 case 340: // reg : BXor(reg, reg)
2230 case 540: // reg : BXor(reg, Constant)
2231 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
2232 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002233
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002234 case 440: // bool: BXor(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002235 { // Use the argument of NOT as the second argument!
2236 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002237 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002238 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2239 Value* notArg = BinaryOperator::getNotArgument(
2240 cast<BinaryOperator>(notNode->getInstruction()));
2241 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002242 Value *lhs = subtreeRoot->leftChild()->getValue();
2243 Value *dest = subtreeRoot->getValue();
2244 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(lhs).addReg(notArg)
2245 .addReg(dest, MOTy::Def));
2246
2247 if (notArg->getType() == Type::BoolTy)
2248 { // set 1 in result register if result of above is non-zero
2249 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2250 .addReg(dest, MOTy::UseAndDef));
2251 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002252 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002253 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002254
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002255 case 41: // setCCconst: SetCC(reg, Constant)
2256 { // Comparison is with a constant:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002257 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002258 // If the bool result must be computed into a register (see below),
2259 // and the constant is int ZERO, we can use the MOVR[op] instructions
2260 // and avoid the SUBcc instruction entirely.
2261 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002262 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002263 // The result of the SetCC must be computed and stored in a register if
2264 // it is used outside the current basic block (so it must be computed
2265 // as a boolreg) or it is used by anything other than a branch.
2266 // We will use a conditional move to do this.
2267 //
2268 Instruction* setCCInstr = subtreeRoot->getInstruction();
2269 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2270 ! AllUsesAreBranches(setCCInstr));
2271
2272 if (computeBoolVal)
2273 {
2274 InstrTreeNode* constNode = subtreeRoot->rightChild();
2275 assert(constNode &&
2276 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
2277 Constant *constVal = cast<Constant>(constNode->getValue());
2278 bool isValidConst;
2279
2280 if ((constVal->getType()->isInteger()
2281 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00002282 && target.getInstrInfo().ConvertConstantToIntType(target,
2283 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002284 && isValidConst)
2285 {
2286 // That constant is an integer zero after all...
2287 // Use a MOVR[op] to compute the boolean result
2288 // Unconditionally set register to 0
2289 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0)
2290 .addRegDef(setCCInstr));
2291
2292 // Now conditionally move 1 into the register.
2293 // Mark the register as a use (as well as a def) because the old
2294 // value will be retained if the condition is false.
2295 MachineOpCode movOpCode = ChooseMovpregiForSetCC(subtreeRoot);
2296 mvec.push_back(BuildMI(movOpCode, 3)
2297 .addReg(subtreeRoot->leftChild()->getValue())
2298 .addZImm(1).addReg(setCCInstr, MOTy::UseAndDef));
2299
2300 break;
2301 }
2302 }
2303 // ELSE FALL THROUGH
2304 }
2305
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002306 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002307 {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002308 // This generates a SUBCC instruction, putting the difference in a
2309 // result reg. if needed, and/or setting a condition code if needed.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002310 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002311 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002312 Value* leftVal = subtreeRoot->leftChild()->getValue();
2313 Value* rightVal = subtreeRoot->rightChild()->getValue();
2314 const Type* opType = leftVal->getType();
2315 bool isFPCompare = opType->isFloatingPoint();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002316
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002317 // If the boolean result of the SetCC is used outside the current basic
2318 // block (so it must be computed as a boolreg) or is used by anything
2319 // other than a branch, the boolean must be computed and stored
2320 // in a result register. We will use a conditional move to do this.
2321 //
2322 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2323 ! AllUsesAreBranches(setCCInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002324
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002325 // A TmpInstruction is created to represent the CC "result".
2326 // Unlike other instances of TmpInstruction, this one is used
2327 // by machine code of multiple LLVM instructions, viz.,
2328 // the SetCC and the branch. Make sure to get the same one!
2329 // Note that we do this even for FP CC registers even though they
2330 // are explicit operands, because the type of the operand
2331 // needs to be a floating point condition code, not an integer
2332 // condition code. Think of this as casting the bool result to
2333 // a FP condition code register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002334 // Later, we mark the 4th operand as being a CC register, and as a def.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002335 //
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002336 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002337 setCCInstr->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00002338 leftVal->getType(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002339 MachineCodeForInstruction::get(setCCInstr));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002340
2341 // If the operands are signed values smaller than 4 bytes, then they
2342 // must be sign-extended in order to do a valid 32-bit comparison
2343 // and get the right result in the 32-bit CC register (%icc).
2344 //
2345 Value* leftOpToUse = leftVal;
2346 Value* rightOpToUse = rightVal;
2347 if (opType->isIntegral() && opType->isSigned()) {
2348 unsigned opSize = target.getTargetData().getTypeSize(opType);
2349 if (opSize < 4) {
2350 MachineCodeForInstruction& mcfi =
2351 MachineCodeForInstruction::get(setCCInstr);
2352
2353 // create temporary virtual regs. to hold the sign-extensions
2354 leftOpToUse = new TmpInstruction(mcfi, leftVal);
2355 rightOpToUse = new TmpInstruction(mcfi, rightVal);
2356
2357 // sign-extend each operand and put the result in the temporary reg.
2358 target.getInstrInfo().CreateSignExtensionInstructions
2359 (target, setCCInstr->getParent()->getParent(),
2360 leftVal, leftOpToUse, 8*opSize, mvec, mcfi);
2361 target.getInstrInfo().CreateSignExtensionInstructions
2362 (target, setCCInstr->getParent()->getParent(),
2363 rightVal, rightOpToUse, 8*opSize, mvec, mcfi);
2364 }
2365 }
2366
Misha Brukman7b647942003-05-30 20:11:56 +00002367 if (! isFPCompare) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002368 // Integer condition: set CC and discard result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002369 mvec.push_back(BuildMI(V9::SUBccr, 4)
2370 .addReg(leftOpToUse)
2371 .addReg(rightOpToUse)
2372 .addMReg(target.getRegInfo().getZeroRegNum(),MOTy::Def)
2373 .addCCReg(tmpForCC, MOTy::Def));
Misha Brukman7b647942003-05-30 20:11:56 +00002374 } else {
2375 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002376 mvec.push_back(BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2377 .addCCReg(tmpForCC, MOTy::Def)
2378 .addReg(leftOpToUse)
2379 .addReg(rightOpToUse));
Misha Brukman7b647942003-05-30 20:11:56 +00002380 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002381
Misha Brukman7b647942003-05-30 20:11:56 +00002382 if (computeBoolVal) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002383 MachineOpCode movOpCode = (isFPCompare
Misha Brukmaneecdb662003-06-02 20:55:14 +00002384 ? ChooseMovFpcciInstruction(subtreeRoot)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002385 : ChooseMovpcciForSetCC(subtreeRoot));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002386
2387 // Unconditionally set register to 0
2388 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
2389 mvec.push_back(M);
2390
2391 // Now conditionally move 1 into the register.
Misha Brukman7b647942003-05-30 20:11:56 +00002392 // Mark the register as a use (as well as a def) because the old
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002393 // value will be retained if the condition is false.
2394 M = (BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(1)
2395 .addReg(setCCInstr, MOTy::UseAndDef));
Misha Brukman7b647942003-05-30 20:11:56 +00002396 mvec.push_back(M);
2397 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002398 break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002399 }
2400
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002401 case 51: // reg: Load(reg)
2402 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002403 SetOperandsForMemInstr(ChooseLoadInstruction(
2404 subtreeRoot->getValue()->getType()),
2405 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002406 break;
2407
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002408 case 55: // reg: GetElemPtr(reg)
2409 case 56: // reg: GetElemPtrIdx(reg,reg)
2410 // If the GetElemPtr was folded into the user (parent), it will be
2411 // caught above. For other cases, we have to compute the address.
2412 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
2413 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002414
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002415 case 57: // reg: Alloca: Implement as 1 instruction:
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002416 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002417 AllocationInst* instr =
2418 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002419 unsigned tsize =
2420 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002421 assert(tsize != 0);
2422 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002423 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002424 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002425
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002426 case 58: // reg: Alloca(reg): Implement as 3 instructions:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002427 // mul num, typeSz -> tmp
2428 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002429 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002430 AllocationInst* instr =
2431 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002432 const Type* eltType = instr->getAllocatedType();
2433
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002434 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002435 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002436 Value* numElementsVal = NULL;
2437 bool isArray = instr->isArrayAllocation();
2438
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002439 if (!isArray || isa<Constant>(numElementsVal = instr->getArraySize())) {
Misha Brukman7b647942003-05-30 20:11:56 +00002440 // total size is constant: generate code for fixed-size alloca
2441 unsigned numElements = isArray?
2442 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2443 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2444 numElements, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002445 } else {
2446 // total size is not constant.
Vikram S. Adve74825322002-03-18 03:15:35 +00002447 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002448 numElementsVal, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002449 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002450 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002451 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002452
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002453 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002454 { // Generate a direct (CALL) or indirect (JMPL) call.
2455 // Mark the return-address register, the indirection
2456 // register (for indirect calls), the operands of the Call,
2457 // and the return value (if any) as implicit operands
2458 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002459 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002460 // If this is a varargs function, floating point arguments
2461 // have to passed in integer registers so insert
2462 // copy-float-to-int instructions for each float operand.
2463 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002464 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002465 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002466 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002467
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002468 // Check if this is an intrinsic function that needs a special code
2469 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002470 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002471 bool specialIntrinsic = false;
2472 LLVMIntrinsic::ID iid;
2473 if (calledFunc && (iid=(LLVMIntrinsic::ID)calledFunc->getIntrinsicID()))
2474 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002475
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002476 // If not, generate the normal call sequence for the function.
2477 // This can also handle any intrinsics that are just function calls.
2478 //
Misha Brukman7b647942003-05-30 20:11:56 +00002479 if (! specialIntrinsic) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002480 Function* currentFunc = callInstr->getParent()->getParent();
2481 MachineFunction& MF = MachineFunction::get(currentFunc);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002482 MachineCodeForInstruction& mcfi =
2483 MachineCodeForInstruction::get(callInstr);
2484 const UltraSparcRegInfo& regInfo =
2485 (UltraSparcRegInfo&) target.getRegInfo();
2486 const TargetFrameInfo& frameInfo = target.getFrameInfo();
2487
Misha Brukman7b647942003-05-30 20:11:56 +00002488 // Create hidden virtual register for return address with type void*
2489 TmpInstruction* retAddrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002490 new TmpInstruction(mcfi, PointerType::get(Type::VoidTy), callInstr);
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002491
Misha Brukman7b647942003-05-30 20:11:56 +00002492 // Generate the machine instruction and its operands.
2493 // Use CALL for direct function calls; this optimistically assumes
2494 // the PC-relative address fits in the CALL address field (22 bits).
2495 // Use JMPL for indirect calls.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002496 // This will be added to mvec later, after operand copies.
Misha Brukman7b647942003-05-30 20:11:56 +00002497 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002498 MachineInstr* callMI;
Misha Brukman7b647942003-05-30 20:11:56 +00002499 if (calledFunc) // direct function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002500 callMI = BuildMI(V9::CALL, 1).addPCDisp(callee);
Misha Brukman7b647942003-05-30 20:11:56 +00002501 else // indirect function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002502 callMI = (BuildMI(V9::JMPLCALLi,3).addReg(callee)
2503 .addSImm((int64_t)0).addRegDef(retAddrReg));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002504
Misha Brukman7b647942003-05-30 20:11:56 +00002505 const FunctionType* funcType =
2506 cast<FunctionType>(cast<PointerType>(callee->getType())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002507 ->getElementType());
Misha Brukman7b647942003-05-30 20:11:56 +00002508 bool isVarArgs = funcType->isVarArg();
2509 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002510
Misha Brukman7b647942003-05-30 20:11:56 +00002511 // Use a descriptor to pass information about call arguments
2512 // to the register allocator. This descriptor will be "owned"
2513 // and freed automatically when the MachineCodeForInstruction
2514 // object for the callInstr goes away.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002515 CallArgsDescriptor* argDesc =
2516 new CallArgsDescriptor(callInstr, retAddrReg,isVarArgs,noPrototype);
Misha Brukman7b647942003-05-30 20:11:56 +00002517 assert(callInstr->getOperand(0) == callee
2518 && "This is assumed in the loop below!");
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002519
2520 // Insert sign-extension instructions for small signed values,
2521 // if this is an unknown function (i.e., called via a funcptr)
2522 // or an external one (i.e., which may not be compiled by llc).
2523 //
2524 if (calledFunc == NULL || calledFunc->isExternal()) {
2525 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
2526 Value* argVal = callInstr->getOperand(i);
2527 const Type* argType = argVal->getType();
2528 if (argType->isIntegral() && argType->isSigned()) {
2529 unsigned argSize = target.getTargetData().getTypeSize(argType);
2530 if (argSize <= 4) {
2531 // create a temporary virtual reg. to hold the sign-extension
2532 TmpInstruction* argExtend = new TmpInstruction(mcfi, argVal);
2533
2534 // sign-extend argVal and put the result in the temporary reg.
2535 target.getInstrInfo().CreateSignExtensionInstructions
2536 (target, currentFunc, argVal, argExtend,
2537 8*argSize, mvec, mcfi);
2538
2539 // replace argVal with argExtend in CallArgsDescriptor
2540 argDesc->getArgInfo(i-1).replaceArgVal(argExtend);
2541 }
2542 }
2543 }
2544 }
2545
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002546 // Insert copy instructions to get all the arguments into
2547 // all the places that they need to be.
2548 //
Misha Brukman7b647942003-05-30 20:11:56 +00002549 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002550 int argNo = i-1;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002551 CallArgInfo& argInfo = argDesc->getArgInfo(argNo);
2552 Value* argVal = argInfo.getArgVal(); // don't use callInstr arg here
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002553 const Type* argType = argVal->getType();
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002554 unsigned regType = regInfo.getRegTypeForDataType(argType);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002555 unsigned argSize = target.getTargetData().getTypeSize(argType);
2556 int regNumForArg = TargetRegInfo::getInvalidRegNum();
2557 unsigned regClassIDOfArgReg;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002558
Misha Brukman7b647942003-05-30 20:11:56 +00002559 // Check for FP arguments to varargs functions.
2560 // Any such argument in the first $K$ args must be passed in an
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002561 // integer register. If there is no prototype, it must also
2562 // be passed as an FP register.
2563 // K = #integer argument registers.
2564 bool isFPArg = argVal->getType()->isFloatingPoint();
2565 if (isVarArgs && isFPArg) {
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002566
2567 if (noPrototype) {
2568 // It is a function with no prototype: pass value
2569 // as an FP value as well as a varargs value. The FP value
2570 // may go in a register or on the stack. The copy instruction
2571 // to the outgoing reg/stack is created by the normal argument
2572 // handling code since this is the "normal" passing mode.
2573 //
2574 regNumForArg = regInfo.regNumForFPArg(regType,
2575 false, false, argNo,
2576 regClassIDOfArgReg);
2577 if (regNumForArg == regInfo.getInvalidRegNum())
2578 argInfo.setUseStackSlot();
2579 else
2580 argInfo.setUseFPArgReg();
2581 }
2582
2583 // If this arg. is in the first $K$ regs, add special copy-
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002584 // float-to-int instructions to pass the value as an int.
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002585 // To check if it is in the first $K$, get the register
2586 // number for the arg #i. These copy instructions are
2587 // generated here because they are extra cases and not needed
2588 // for the normal argument handling (some code reuse is
2589 // possible though -- later).
2590 //
Misha Brukmanea481cc2003-06-03 03:21:58 +00002591 int copyRegNum = regInfo.regNumForIntArg(false, false, argNo,
2592 regClassIDOfArgReg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002593 if (copyRegNum != regInfo.getInvalidRegNum()) {
2594 // Create a virtual register to represent copyReg. Mark
2595 // this vreg as being an implicit operand of the call MI
2596 const Type* loadTy = (argType == Type::FloatTy
2597 ? Type::IntTy : Type::LongTy);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002598 TmpInstruction* argVReg = new TmpInstruction(mcfi, loadTy,
2599 argVal, NULL,
2600 "argRegCopy");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002601 callMI->addImplicitRef(argVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002602
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002603 // Get a temp stack location to use to copy
2604 // float-to-int via the stack.
2605 //
2606 // FIXME: For now, we allocate permanent space because
2607 // the stack frame manager does not allow locals to be
2608 // allocated (e.g., for alloca) after a temp is
2609 // allocated!
2610 //
2611 // int tmpOffset = MF.getInfo()->pushTempValue(argSize);
2612 int tmpOffset = MF.getInfo()->allocateLocalVar(argVReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002613
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002614 // Generate the store from FP reg to stack
Misha Brukmanea481cc2003-06-03 03:21:58 +00002615 unsigned StoreOpcode = ChooseStoreInstruction(argType);
2616 M = BuildMI(convertOpcodeFromRegToImm(StoreOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002617 .addReg(argVal).addMReg(regInfo.getFramePointer())
2618 .addSImm(tmpOffset);
2619 mvec.push_back(M);
2620
2621 // Generate the load from stack to int arg reg
Misha Brukmanea481cc2003-06-03 03:21:58 +00002622 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
2623 M = BuildMI(convertOpcodeFromRegToImm(LoadOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002624 .addMReg(regInfo.getFramePointer()).addSImm(tmpOffset)
2625 .addReg(argVReg, MOTy::Def);
2626
2627 // Mark operand with register it should be assigned
2628 // both for copy and for the callMI
2629 M->SetRegForOperand(M->getNumOperands()-1, copyRegNum);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002630 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2631 copyRegNum);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002632 mvec.push_back(M);
2633
2634 // Add info about the argument to the CallArgsDescriptor
2635 argInfo.setUseIntArgReg();
2636 argInfo.setArgCopy(copyRegNum);
2637 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00002638 // Cannot fit in first $K$ regs so pass arg on stack
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002639 argInfo.setUseStackSlot();
2640 }
2641 } else if (isFPArg) {
2642 // Get the outgoing arg reg to see if there is one.
2643 regNumForArg = regInfo.regNumForFPArg(regType, false, false,
2644 argNo, regClassIDOfArgReg);
2645 if (regNumForArg == regInfo.getInvalidRegNum())
2646 argInfo.setUseStackSlot();
2647 else {
2648 argInfo.setUseFPArgReg();
2649 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2650 regNumForArg);
2651 }
2652 } else {
2653 // Get the outgoing arg reg to see if there is one.
2654 regNumForArg = regInfo.regNumForIntArg(false,false,
2655 argNo, regClassIDOfArgReg);
2656 if (regNumForArg == regInfo.getInvalidRegNum())
2657 argInfo.setUseStackSlot();
2658 else {
2659 argInfo.setUseIntArgReg();
2660 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2661 regNumForArg);
2662 }
2663 }
2664
2665 //
2666 // Now insert copy instructions to stack slot or arg. register
2667 //
2668 if (argInfo.usesStackSlot()) {
2669 // Get the stack offset for this argument slot.
2670 // FP args on stack are right justified so adjust offset!
2671 // int arguments are also right justified but they are
2672 // always loaded as a full double-word so the offset does
2673 // not need to be adjusted.
2674 int argOffset = frameInfo.getOutgoingArgOffset(MF, argNo);
2675 if (argType->isFloatingPoint()) {
2676 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
2677 assert(argSize <= slotSize && "Insufficient slot size!");
2678 argOffset += slotSize - argSize;
2679 }
2680
2681 // Now generate instruction to copy argument to stack
2682 MachineOpCode storeOpCode =
2683 (argType->isFloatingPoint()
2684 ? ((argSize == 4)? V9::STFi : V9::STDFi) : V9::STXi);
2685
2686 M = BuildMI(storeOpCode, 3).addReg(argVal)
2687 .addMReg(regInfo.getStackPointer()).addSImm(argOffset);
2688 mvec.push_back(M);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002689 }
2690 else if (regNumForArg != regInfo.getInvalidRegNum()) {
2691
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002692 // Create a virtual register to represent the arg reg. Mark
2693 // this vreg as being an implicit operand of the call MI.
2694 TmpInstruction* argVReg =
2695 new TmpInstruction(mcfi, argVal, NULL, "argReg");
2696
2697 callMI->addImplicitRef(argVReg);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002698
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002699 // Generate the reg-to-reg copy into the outgoing arg reg.
2700 // -- For FP values, create a FMOVS or FMOVD instruction
2701 // -- For non-FP values, create an add-with-0 instruction
2702 if (argType->isFloatingPoint())
2703 M=(BuildMI(argType==Type::FloatTy? V9::FMOVS :V9::FMOVD,2)
2704 .addReg(argVal).addReg(argVReg, MOTy::Def));
2705 else
2706 M = (BuildMI(ChooseAddInstructionByType(argType), 3)
2707 .addReg(argVal).addSImm((int64_t) 0)
2708 .addReg(argVReg, MOTy::Def));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002709
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002710 // Mark the operand with the register it should be assigned
2711 M->SetRegForOperand(M->getNumOperands()-1, regNumForArg);
2712 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2713 regNumForArg);
2714
2715 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +00002716 }
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002717 else
2718 assert(argInfo.getArgCopy() != regInfo.getInvalidRegNum() &&
2719 "Arg. not in stack slot, primary or secondary register?");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002720 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002721
2722 // add call instruction and delay slot before copying return value
2723 mvec.push_back(callMI);
2724 mvec.push_back(BuildMI(V9::NOP, 0));
2725
Misha Brukman7b647942003-05-30 20:11:56 +00002726 // Add the return value as an implicit ref. The call operands
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002727 // were added above. Also, add code to copy out the return value.
2728 // This is always register-to-register for int or FP return values.
2729 //
2730 if (callInstr->getType() != Type::VoidTy) {
2731 // Get the return value reg.
2732 const Type* retType = callInstr->getType();
2733
2734 int regNum = (retType->isFloatingPoint()
2735 ? (unsigned) SparcFloatRegClass::f0
2736 : (unsigned) SparcIntRegClass::o0);
2737 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
2738 regNum = regInfo.getUnifiedRegNum(regClassID, regNum);
2739
2740 // Create a virtual register to represent it and mark
2741 // this vreg as being an implicit operand of the call MI
2742 TmpInstruction* retVReg =
2743 new TmpInstruction(mcfi, callInstr, NULL, "argReg");
2744
2745 callMI->addImplicitRef(retVReg, /*isDef*/ true);
2746
2747 // Generate the reg-to-reg copy from the return value reg.
2748 // -- For FP values, create a FMOVS or FMOVD instruction
2749 // -- For non-FP values, create an add-with-0 instruction
2750 if (retType->isFloatingPoint())
2751 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
2752 .addReg(retVReg).addReg(callInstr, MOTy::Def));
2753 else
2754 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
2755 .addReg(retVReg).addSImm((int64_t) 0)
2756 .addReg(callInstr, MOTy::Def));
2757
2758 // Mark the operand with the register it should be assigned
2759 // Also mark the implicit ref of the call defining this operand
2760 M->SetRegForOperand(0, regNum);
2761 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,regNum);
2762
2763 mvec.push_back(M);
2764 }
2765
Misha Brukman7b647942003-05-30 20:11:56 +00002766 // For the CALL instruction, the ret. addr. reg. is also implicit
2767 if (isa<Function>(callee))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002768 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
2769
2770 MF.getInfo()->popAllTempValues(); // free temps used for this inst
Misha Brukman7b647942003-05-30 20:11:56 +00002771 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002772
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002773 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002774 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002775
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002776 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002777 {
2778 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2779 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2780 Instruction* shlInstr = subtreeRoot->getInstruction();
2781
2782 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002783 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2784 "Shl unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002785 unsigned opSize = target.getTargetData().getTypeSize(opType);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002786
2787 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Vikram S. Advee895a742003-08-06 18:48:40 +00002788 (opSize > 4)? V9::SLLXr6:V9::SLLr5,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002789 argVal1, argVal2, 0, shlInstr, mvec,
2790 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002791 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002792 }
2793
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002794 case 63: // reg: Shr(reg, reg)
Misha Brukman7b647942003-05-30 20:11:56 +00002795 {
2796 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002797 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2798 "Shr unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002799 unsigned opSize = target.getTargetData().getTypeSize(opType);
Chris Lattner54e898e2003-01-15 19:23:34 +00002800 Add3OperandInstr(opType->isSigned()
Vikram S. Advee895a742003-08-06 18:48:40 +00002801 ? (opSize > 4? V9::SRAXr6 : V9::SRAr5)
2802 : (opSize > 4? V9::SRLXr6 : V9::SRLr5),
Chris Lattner54e898e2003-01-15 19:23:34 +00002803 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002804 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002805 }
2806
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002807 case 64: // reg: Phi(reg,reg)
2808 break; // don't forward the value
Vikram S. Adve74825322002-03-18 03:15:35 +00002809
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002810 case 65: // reg: VaArg(reg)
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002811 {
2812 // Use value initialized by va_start as pointer to args on the stack.
2813 // Load argument via current pointer value, then increment pointer.
2814 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
2815 Instruction* vaArgI = subtreeRoot->getInstruction();
Misha Brukman91aee472003-05-27 22:37:00 +00002816 mvec.push_back(BuildMI(V9::LDXi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002817 addSImm(0).addRegDef(vaArgI));
Misha Brukman91aee472003-05-27 22:37:00 +00002818 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002819 addSImm(argSize).addRegDef(vaArgI->getOperand(0)));
2820 break;
2821 }
2822
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002823 case 71: // reg: VReg
2824 case 72: // reg: Constant
2825 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002826
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002827 default:
2828 assert(0 && "Unrecognized BURG rule");
2829 break;
2830 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002831 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002832
Misha Brukman7b647942003-05-30 20:11:56 +00002833 if (forwardOperandNum >= 0) {
2834 // We did not generate a machine instruction but need to use operand.
2835 // If user is in the same tree, replace Value in its machine operand.
2836 // If not, insert a copy instruction which should get coalesced away
2837 // by register allocation.
2838 if (subtreeRoot->parent() != NULL)
2839 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2840 else {
2841 std::vector<MachineInstr*> minstrVec;
2842 Instruction* instr = subtreeRoot->getInstruction();
2843 target.getInstrInfo().
2844 CreateCopyInstructionsByType(target,
2845 instr->getParent()->getParent(),
2846 instr->getOperand(forwardOperandNum),
2847 instr, minstrVec,
2848 MachineCodeForInstruction::get(instr));
2849 assert(minstrVec.size() > 0);
2850 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Chris Lattner20b1ea02001-09-14 03:47:57 +00002851 }
Misha Brukman7b647942003-05-30 20:11:56 +00002852 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002853
Misha Brukman7b647942003-05-30 20:11:56 +00002854 if (maskUnsignedResult) {
2855 // If result is unsigned and smaller than int reg size,
2856 // we need to clear high bits of result value.
2857 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2858 Instruction* dest = subtreeRoot->getInstruction();
2859 if (dest->getType()->isUnsigned()) {
2860 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
2861 if (destSize <= 4) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002862 // Mask high 64 - N bits, where N = 4*destSize.
2863
2864 // Use a TmpInstruction to represent the
Misha Brukman7b647942003-05-30 20:11:56 +00002865 // intermediate result before masking. Since those instructions
2866 // have already been generated, go back and substitute tmpI
2867 // for dest in the result position of each one of them.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002868 //
2869 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(dest);
2870 TmpInstruction *tmpI = new TmpInstruction(mcfi, dest->getType(),
2871 dest, NULL, "maskHi");
2872 Value* srlArgToUse = tmpI;
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002873
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002874 unsigned numSubst = 0;
2875 for (unsigned i=0, N=mvec.size(); i < N; ++i) {
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002876
2877 // Make sure we substitute all occurrences of dest in these instrs.
2878 // Otherwise, we will have bogus code.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002879 bool someArgsWereIgnored = false;
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002880
2881 // Make sure not to substitute an upwards-exposed use -- that would
2882 // introduce a use of `tmpI' with no preceding def. Therefore,
2883 // substitute a use or def-and-use operand only if a previous def
2884 // operand has already been substituted (i.e., numSusbt > 0).
2885 //
2886 numSubst += mvec[i]->substituteValue(dest, tmpI,
2887 /*defsOnly*/ numSubst == 0,
2888 /*notDefsAndUses*/ numSubst > 0,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002889 someArgsWereIgnored);
2890 assert(!someArgsWereIgnored &&
2891 "Operand `dest' exists but not replaced: probably bogus!");
2892 }
2893 assert(numSubst > 0 && "Operand `dest' not replaced: probably bogus!");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002894
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002895 // Left shift 32-N if size (N) is less than 32 bits.
2896 // Use another tmp. virtual registe to represent this result.
2897 if (destSize < 4) {
2898 srlArgToUse = new TmpInstruction(mcfi, dest->getType(),
2899 tmpI, NULL, "maskHi2");
2900 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpI)
2901 .addZImm(8*(4-destSize))
2902 .addReg(srlArgToUse, MOTy::Def));
2903 }
2904
2905 // Logical right shift 32-N to get zero extension in top 64-N bits.
2906 mvec.push_back(BuildMI(V9::SRLi5, 3).addReg(srlArgToUse)
2907 .addZImm(8*(4-destSize)).addReg(dest, MOTy::Def));
2908
Misha Brukman7b647942003-05-30 20:11:56 +00002909 } else if (destSize < 8) {
2910 assert(0 && "Unsupported type size: 32 < size < 64 bits");
2911 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002912 }
Misha Brukman7b647942003-05-30 20:11:56 +00002913 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002914}