blob: ee24333495129794beb50640b62fd48aee7dab30 [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. Adve4ecff5d2003-08-11 18:42:47 +00001421 addRegDef(callInstr.getOperand(1)));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001422 return true;
1423 }
1424
1425 case LLVMIntrinsic::va_end:
1426 return true; // no-op on Sparc
1427
1428 case LLVMIntrinsic::va_copy:
1429 // Simple copy of current va_list (arg2) to new va_list (arg1)
Misha Brukman91aee472003-05-27 22:37:00 +00001430 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001431 addMReg(target.getRegInfo().getZeroRegNum()).
1432 addReg(callInstr.getOperand(2)).
1433 addReg(callInstr.getOperand(1)));
1434 return true;
1435
Chris Lattner72af6b82003-08-18 16:06:09 +00001436 case LLVMIntrinsic::sigsetjmp:
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001437 case LLVMIntrinsic::setjmp: {
1438 // act as if we return 0
1439 unsigned g0 = target.getRegInfo().getZeroRegNum();
1440 mvec.push_back(BuildMI(V9::ORr,3).addMReg(g0).addMReg(g0)
1441 .addReg(&callInstr, MOTy::Def));
1442 return true;
1443 }
1444
Chris Lattner72af6b82003-08-18 16:06:09 +00001445 case LLVMIntrinsic::siglongjmp:
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001446 case LLVMIntrinsic::longjmp: {
1447 // call abort()
1448 Module* M = callInstr.getParent()->getParent()->getParent();
1449 Function *F = M->getNamedFunction("abort");
1450 mvec.push_back(BuildMI(V9::CALL, 1).addReg(F));
1451 return true;
1452 }
1453
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001454 default:
1455 return false;
1456 }
1457}
1458
Vikram S. Advefb361122001-10-22 13:36:31 +00001459//******************* Externally Visible Functions *************************/
1460
Vikram S. Advefb361122001-10-22 13:36:31 +00001461//------------------------------------------------------------------------
1462// External Function: ThisIsAChainRule
1463//
1464// Purpose:
1465// Check if a given BURG rule is a chain rule.
1466//------------------------------------------------------------------------
1467
1468extern bool
1469ThisIsAChainRule(int eruleno)
1470{
1471 switch(eruleno)
1472 {
1473 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001474 case 123:
1475 case 124:
1476 case 125:
1477 case 126:
1478 case 127:
1479 case 128:
1480 case 129:
1481 case 130:
1482 case 131:
1483 case 132:
1484 case 133:
1485 case 155:
1486 case 221:
1487 case 222:
1488 case 241:
1489 case 242:
1490 case 243:
1491 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001492 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001493 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001494 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001495
Vikram S. Advefb361122001-10-22 13:36:31 +00001496 default:
1497 return false; break;
1498 }
1499}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001500
1501
1502//------------------------------------------------------------------------
1503// External Function: GetInstructionsByRule
1504//
1505// Purpose:
1506// Choose machine instructions for the SPARC according to the
1507// patterns chosen by the BURG-generated parser.
1508//------------------------------------------------------------------------
1509
Vikram S. Adve74825322002-03-18 03:15:35 +00001510void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001511GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001512 int ruleForNode,
1513 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001514 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001515 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001516{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001517 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001518 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001519 int nextRule;
1520 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001521 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001522 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001523 unsigned L;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001524 bool foldCase = false;
Vikram S. Adve74825322002-03-18 03:15:35 +00001525
1526 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001527
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001528 // If the code for this instruction was folded into the parent (user),
1529 // then do nothing!
1530 if (subtreeRoot->isFoldedIntoParent())
1531 return;
1532
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001533 //
1534 // Let's check for chain rules outside the switch so that we don't have
1535 // to duplicate the list of chain rule production numbers here again
1536 //
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001537 if (ThisIsAChainRule(ruleForNode))
1538 {
1539 // Chain rules have a single nonterminal on the RHS.
1540 // Get the rule that matches the RHS non-terminal and use that instead.
1541 //
1542 assert(nts[0] && ! nts[1]
1543 && "A chain rule should have only one RHS non-terminal!");
1544 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1545 nts = burm_nts[nextRule];
1546 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
1547 }
1548 else
1549 {
1550 switch(ruleForNode) {
1551 case 1: // stmt: Ret
1552 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001553 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001554 // for moving return value to appropriate register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001555 // Copy the return value to the required return register.
1556 // Mark the return Value as an implicit ref of the RET instr..
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001557 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001558 // Finally put a NOP in the delay slot.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001559 ReturnInst *returnInstr=cast<ReturnInst>(subtreeRoot->getInstruction());
1560 Value* retVal = returnInstr->getReturnValue();
1561 MachineCodeForInstruction& mcfi =
1562 MachineCodeForInstruction::get(returnInstr);
1563
1564 // Create a hidden virtual reg to represent the return address register
1565 // used by the machine instruction but not represented in LLVM.
1566 //
1567 Instruction* returnAddrTmp = new TmpInstruction(mcfi, returnInstr);
1568
1569 MachineInstr* retMI =
1570 BuildMI(V9::JMPLRETi, 3).addReg(returnAddrTmp).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001571 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001572
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001573 // If there is a value to return, we need to:
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001574 // (a) Sign-extend the value if it is smaller than 8 bytes (reg size)
1575 // (b) Insert a copy to copy the return value to the appropriate reg.
1576 // -- For FP values, create a FMOVS or FMOVD instruction
1577 // -- For non-FP values, create an add-with-0 instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001578 //
1579 if (retVal != NULL) {
1580 const UltraSparcRegInfo& regInfo =
1581 (UltraSparcRegInfo&) target.getRegInfo();
1582 const Type* retType = retVal->getType();
1583 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
1584 unsigned retRegNum = (retType->isFloatingPoint()
1585 ? (unsigned) SparcFloatRegClass::f0
1586 : (unsigned) SparcIntRegClass::i0);
1587 retRegNum = regInfo.getUnifiedRegNum(regClassID, retRegNum);
1588
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001589 // () Insert sign-extension instructions for small signed values.
1590 //
1591 Value* retValToUse = retVal;
1592 if (retType->isIntegral() && retType->isSigned()) {
1593 unsigned retSize = target.getTargetData().getTypeSize(retType);
1594 if (retSize <= 4) {
1595 // create a temporary virtual reg. to hold the sign-extension
1596 retValToUse = new TmpInstruction(mcfi, retVal);
1597
1598 // sign-extend retVal and put the result in the temporary reg.
1599 target.getInstrInfo().CreateSignExtensionInstructions
1600 (target, returnInstr->getParent()->getParent(),
1601 retVal, retValToUse, 8*retSize, mvec, mcfi);
1602 }
1603 }
1604
1605 // (b) Now, insert a copy to to the appropriate register:
1606 // -- For FP values, create a FMOVS or FMOVD instruction
1607 // -- For non-FP values, create an add-with-0 instruction
1608 //
1609 // First, create a virtual register to represent the register and
1610 // mark this vreg as being an implicit operand of the ret MI.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001611 TmpInstruction* retVReg =
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001612 new TmpInstruction(mcfi, retValToUse, NULL, "argReg");
1613
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001614 retMI->addImplicitRef(retVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001615
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001616 if (retType->isFloatingPoint())
1617 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001618 .addReg(retValToUse).addReg(retVReg, MOTy::Def));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001619 else
1620 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001621 .addReg(retValToUse).addSImm((int64_t) 0)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001622 .addReg(retVReg, MOTy::Def));
1623
1624 // Mark the operand with the register it should be assigned
1625 M->SetRegForOperand(M->getNumOperands()-1, retRegNum);
1626 retMI->SetRegForImplicitRef(retMI->getNumImplicitRefs()-1, retRegNum);
1627
1628 mvec.push_back(M);
1629 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001630
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001631 // Now insert the RET instruction and a NOP for the delay slot
1632 mvec.push_back(retMI);
Misha Brukmana98cd452003-05-20 20:32:24 +00001633 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001634
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001635 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001636 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001637
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001638 case 3: // stmt: Store(reg,reg)
1639 case 4: // stmt: Store(reg,ptrreg)
1640 SetOperandsForMemInstr(ChooseStoreInstruction(
Chris Lattner54e898e2003-01-15 19:23:34 +00001641 subtreeRoot->leftChild()->getValue()->getType()),
1642 mvec, subtreeRoot, target);
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001643 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001644
1645 case 5: // stmt: BrUncond
1646 {
1647 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1648 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
1649
1650 // delay slot
1651 mvec.push_back(BuildMI(V9::NOP, 0));
1652 break;
1653 }
1654
1655 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001656 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001657 // If the constant is ZERO, we can use the branch-on-integer-register
1658 // instructions and avoid the SUBcc instruction entirely.
1659 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001660 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001661 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1662 assert(constNode &&
1663 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001664 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001665 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001666
Chris Lattner0c4e8862002-09-03 01:08:28 +00001667 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001668 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00001669 && target.getInstrInfo().ConvertConstantToIntType(target,
1670 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001671 && isValidConst)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001672 {
1673 // That constant is a zero after all...
1674 // Use the left child of setCC as the first argument!
1675 // Mark the setCC node so that no code is generated for it.
1676 InstructionNode* setCCNode = (InstructionNode*)
1677 subtreeRoot->leftChild();
1678 assert(setCCNode->getOpLabel() == SetCCOp);
1679 setCCNode->markFoldedIntoParent();
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001680
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001681 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001682
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001683 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1684 .addReg(setCCNode->leftChild()->getValue())
1685 .addPCDisp(brInst->getSuccessor(0));
1686 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001687
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001688 // delay slot
1689 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001690
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001691 // false branch
1692 mvec.push_back(BuildMI(V9::BA, 1)
1693 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001694
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001695 // delay slot
1696 mvec.push_back(BuildMI(V9::NOP, 0));
1697 break;
1698 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001699 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001700 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001701
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001702 case 6: // stmt: BrCond(setCC)
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001703 { // bool => boolean was computed with SetCC.
1704 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001705 // If it is an integer CC, we also need to find the unique
1706 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001707 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001708 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +00001709 const Type* setCCType;
1710 unsigned Opcode = ChooseBccInstruction(subtreeRoot, setCCType);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001711 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1712 brInst->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00001713 setCCType,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001714 MachineCodeForInstruction::get(brInst));
Chris Lattner54e898e2003-01-15 19:23:34 +00001715 M = BuildMI(Opcode, 2).addCCReg(ccValue)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001716 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001717 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001718
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001719 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001720 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001721
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001722 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001723 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001724
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001725 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001726 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001727 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001728 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001729
1730 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001731 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001732 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001733 Constant* constVal =
1734 cast<Constant>(subtreeRoot->leftChild()->getValue());
1735 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001736
Misha Brukmana98cd452003-05-20 20:32:24 +00001737 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001738 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001739 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001740
1741 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001742 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001743 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001744 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001745
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001746 case 8: // stmt: BrCond(boolreg)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001747 { // boolreg => boolean is recorded in an integer register.
1748 // Use branch-on-integer-register instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001749 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001750 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001751 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001752 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001753 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001754
1755 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001756 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001757
1758 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001759 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001760
1761 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001762 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001763 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001764 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001765
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001766 case 9: // stmt: Switch(reg)
1767 assert(0 && "*** SWITCH instruction is not implemented yet.");
1768 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001769
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001770 case 10: // reg: VRegList(reg, reg)
1771 assert(0 && "VRegList should never be the topmost non-chain rule");
1772 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001773
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001774 case 21: // bool: Not(bool,reg): Compute with a conditional-move-on-reg
1775 { // First find the unary operand. It may be left or right, usually right.
1776 Instruction* notI = subtreeRoot->getInstruction();
1777 Value* notArg = BinaryOperator::getNotArgument(
1778 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1779 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1780
1781 // Unconditionally set register to 0
1782 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(notI));
1783
1784 // Now conditionally move 1 into the register.
1785 // Mark the register as a use (as well as a def) because the old
1786 // value will be retained if the condition is false.
1787 mvec.push_back(BuildMI(V9::MOVRZi, 3).addReg(notArg).addZImm(1)
1788 .addReg(notI, MOTy::UseAndDef));
1789
1790 break;
1791 }
1792
1793 case 421: // reg: BNot(reg,reg): Compute as reg = reg XOR-NOT 0
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001794 { // First find the unary operand. It may be left or right, usually right.
1795 Value* notArg = BinaryOperator::getNotArgument(
1796 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001797 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001798 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001799 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001800 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001801 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001802
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001803 case 322: // reg: Not(tobool, reg):
1804 // Fold CAST-TO-BOOL with NOT by inverting the sense of cast-to-bool
1805 foldCase = true;
1806 // Just fall through!
1807
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001808 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001809 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001810 Instruction* castI = subtreeRoot->getInstruction();
1811 Value* opVal = subtreeRoot->leftChild()->getValue();
1812 assert(opVal->getType()->isIntegral() ||
1813 isa<PointerType>(opVal->getType()));
1814
1815 // Unconditionally set register to 0
1816 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(castI));
1817
1818 // Now conditionally move 1 into the register.
1819 // Mark the register as a use (as well as a def) because the old
1820 // value will be retained if the condition is false.
1821 MachineOpCode opCode = foldCase? V9::MOVRZi : V9::MOVRNZi;
1822 mvec.push_back(BuildMI(opCode, 3).addReg(opVal).addZImm(1)
1823 .addReg(castI, MOTy::UseAndDef));
1824
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001825 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001826 }
1827
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001828 case 23: // reg: ToUByteTy(reg)
1829 case 24: // reg: ToSByteTy(reg)
1830 case 25: // reg: ToUShortTy(reg)
1831 case 26: // reg: ToShortTy(reg)
1832 case 27: // reg: ToUIntTy(reg)
1833 case 28: // reg: ToIntTy(reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001834 case 29: // reg: ToULongTy(reg)
1835 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001836 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001837 //======================================================================
1838 // Rules for integer conversions:
1839 //
1840 //--------
1841 // From ISO 1998 C++ Standard, Sec. 4.7:
1842 //
1843 // 2. If the destination type is unsigned, the resulting value is
1844 // the least unsigned integer congruent to the source integer
1845 // (modulo 2n where n is the number of bits used to represent the
1846 // unsigned type). [Note: In a two s complement representation,
1847 // this conversion is conceptual and there is no change in the
1848 // bit pattern (if there is no truncation). ]
1849 //
1850 // 3. If the destination type is signed, the value is unchanged if
1851 // it can be represented in the destination type (and bitfield width);
1852 // otherwise, the value is implementation-defined.
1853 //--------
1854 //
1855 // Since we assume 2s complement representations, this implies:
1856 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001857 // -- If operand is smaller than destination, zero-extend or sign-extend
1858 // according to the signedness of the *operand*: source decides:
1859 // (1) If operand is signed, sign-extend it.
1860 // If dest is unsigned, zero-ext the result!
1861 // (2) If operand is unsigned, our current invariant is that
1862 // it's high bits are correct, so zero-extension is not needed.
Vikram S. Adve94c40812002-09-27 14:33:08 +00001863 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001864 // -- If operand is same size as or larger than destination,
1865 // zero-extend or sign-extend according to the signedness of
1866 // the *destination*: destination decides:
1867 // (1) If destination is signed, sign-extend (truncating if needed)
1868 // This choice is implementation defined. We sign-extend the
1869 // operand, which matches both Sun's cc and gcc3.2.
1870 // (2) If destination is unsigned, zero-extend (truncating if needed)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001871 //======================================================================
1872
Vikram S. Adve242a8082002-05-19 15:25:51 +00001873 Instruction* destI = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001874 Function* currentFunc = destI->getParent()->getParent();
1875 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(destI);
1876
Vikram S. Adve242a8082002-05-19 15:25:51 +00001877 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001878 const Type* opType = opVal->getType();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001879 const Type* destType = destI->getType();
1880 unsigned opSize = target.getTargetData().getTypeSize(opType);
1881 unsigned destSize = target.getTargetData().getTypeSize(destType);
1882
1883 bool isIntegral = opType->isIntegral() || isa<PointerType>(opType);
1884
1885 if (opType == Type::BoolTy ||
1886 opType == destType ||
1887 isIntegral && opSize == destSize && opSize == 8) {
1888 // nothing to do in all these cases
1889 forwardOperandNum = 0; // forward first operand to user
1890
Misha Brukman7b647942003-05-30 20:11:56 +00001891 } else if (opType->isFloatingPoint()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001892
1893 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Advee895a742003-08-06 18:48:40 +00001894 if (destI->getType()->isUnsigned() && destI->getType() !=Type::UIntTy)
Misha Brukman7b647942003-05-30 20:11:56 +00001895 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve1e606692002-07-31 21:01:34 +00001896
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001897 } else if (isIntegral) {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001898
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001899 bool opSigned = opType->isSigned();
1900 bool destSigned = destType->isSigned();
1901 unsigned extSourceInBits = 8 * std::min<unsigned>(opSize, destSize);
1902
1903 assert(! (opSize == destSize && opSigned == destSigned) &&
1904 "How can different int types have same size and signedness?");
1905
1906 bool signExtend = (opSize < destSize && opSigned ||
1907 opSize >= destSize && destSigned);
1908
1909 bool signAndZeroExtend = (opSize < destSize && destSize < 8u &&
1910 opSigned && !destSigned);
1911 assert(!signAndZeroExtend || signExtend);
1912
1913 bool zeroExtendOnly = opSize >= destSize && !destSigned;
1914 assert(!zeroExtendOnly || !signExtend);
1915
1916 if (signExtend) {
1917 Value* signExtDest = (signAndZeroExtend
1918 ? new TmpInstruction(mcfi, destType, opVal)
1919 : destI);
1920
1921 target.getInstrInfo().CreateSignExtensionInstructions
1922 (target, currentFunc,opVal,signExtDest,extSourceInBits,mvec,mcfi);
1923
1924 if (signAndZeroExtend)
1925 target.getInstrInfo().CreateZeroExtensionInstructions
1926 (target, currentFunc, signExtDest, destI, 8*destSize, mvec, mcfi);
1927 }
1928 else if (zeroExtendOnly) {
1929 target.getInstrInfo().CreateZeroExtensionInstructions
1930 (target, currentFunc, opVal, destI, extSourceInBits, mvec, mcfi);
1931 }
1932 else
1933 forwardOperandNum = 0; // forward first operand to user
1934
Misha Brukman7b647942003-05-30 20:11:56 +00001935 } else
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001936 assert(0 && "Unrecognized operand type for convert-to-integer");
1937
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001938 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001939 }
1940
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001941 case 31: // reg: ToFloatTy(reg):
1942 case 32: // reg: ToDoubleTy(reg):
1943 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001944
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001945 // If this instruction has a parent (a user) in the tree
1946 // and the user is translated as an FsMULd instruction,
1947 // then the cast is unnecessary. So check that first.
1948 // In the future, we'll want to do the same for the FdMULq instruction,
1949 // so do the check here instead of only for ToFloatTy(reg).
1950 //
1951 if (subtreeRoot->parent() != NULL) {
1952 const MachineCodeForInstruction& mcfi =
1953 MachineCodeForInstruction::get(
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001954 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001955 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
1956 forwardOperandNum = 0; // forward first operand to user
1957 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001958
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001959 if (forwardOperandNum != 0) { // we do need the cast
1960 Value* leftVal = subtreeRoot->leftChild()->getValue();
1961 const Type* opType = leftVal->getType();
Vikram S. Advee895a742003-08-06 18:48:40 +00001962 MachineOpCode opCode=ChooseConvertToFloatInstr(target,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001963 subtreeRoot->getOpLabel(), opType);
Vikram S. Advee895a742003-08-06 18:48:40 +00001964 if (opCode == V9::NOP) { // no conversion needed
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001965 forwardOperandNum = 0; // forward first operand to user
1966 } else {
1967 // If the source operand is a non-FP type it must be
1968 // first copied from int to float register via memory!
1969 Instruction *dest = subtreeRoot->getInstruction();
1970 Value* srcForCast;
1971 int n = 0;
1972 if (! opType->isFloatingPoint()) {
1973 // Create a temporary to represent the FP register
1974 // into which the integer will be copied via memory.
1975 // The type of this temporary will determine the FP
1976 // register used: single-prec for a 32-bit int or smaller,
1977 // double-prec for a 64-bit int.
1978 //
1979 uint64_t srcSize =
1980 target.getTargetData().getTypeSize(leftVal->getType());
1981 Type* tmpTypeToUse =
1982 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001983 MachineCodeForInstruction &destMCFI =
1984 MachineCodeForInstruction::get(dest);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001985 srcForCast = new TmpInstruction(destMCFI, tmpTypeToUse, dest);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001986
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001987 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001988 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001989 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001990 mvec, destMCFI);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001991 } else
1992 srcForCast = leftVal;
1993
1994 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
1995 mvec.push_back(M);
1996 }
Misha Brukman7b647942003-05-30 20:11:56 +00001997 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001998 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001999
2000 case 19: // reg: ToArrayTy(reg):
2001 case 20: // reg: ToPointerTy(reg):
2002 forwardOperandNum = 0; // forward first operand to user
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002003 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002004
2005 case 233: // reg: Add(reg, Constant)
2006 maskUnsignedResult = true;
2007 M = CreateAddConstInstruction(subtreeRoot);
2008 if (M != NULL) {
2009 mvec.push_back(M);
2010 break;
2011 }
2012 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002013
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002014 case 33: // reg: Add(reg, reg)
2015 maskUnsignedResult = true;
2016 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
2017 break;
2018
2019 case 234: // reg: Sub(reg, Constant)
2020 maskUnsignedResult = true;
2021 M = CreateSubConstInstruction(subtreeRoot);
2022 if (M != NULL) {
2023 mvec.push_back(M);
2024 break;
2025 }
2026 // ELSE FALL THROUGH
2027
2028 case 34: // reg: Sub(reg, reg)
2029 maskUnsignedResult = true;
2030 Add3OperandInstr(ChooseSubInstructionByType(
Chris Lattner54e898e2003-01-15 19:23:34 +00002031 subtreeRoot->getInstruction()->getType()),
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002032 subtreeRoot, mvec);
2033 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002034
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002035 case 135: // reg: Mul(todouble, todouble)
2036 checkCast = true;
2037 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002038
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002039 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002040 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002041 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002042 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002043 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002044 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002045 Instruction* mulInstr = subtreeRoot->getInstruction();
2046 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002047 subtreeRoot->leftChild()->getValue(),
2048 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002049 mulInstr, mvec,
2050 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002051 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002052 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002053 case 335: // reg: Mul(todouble, todoubleConst)
2054 checkCast = true;
2055 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002056
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002057 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00002058 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002059 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002060 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002061 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002062 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002063 Instruction* mulInstr = subtreeRoot->getInstruction();
2064 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002065 subtreeRoot->leftChild()->getValue(),
2066 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002067 mulInstr, mvec,
2068 MachineCodeForInstruction::get(mulInstr),
2069 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002070 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002071 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002072 case 236: // reg: Div(reg, Constant)
2073 maskUnsignedResult = true;
2074 L = mvec.size();
2075 CreateDivConstInstruction(target, subtreeRoot, mvec);
2076 if (mvec.size() > L)
2077 break;
2078 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002079
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002080 case 36: // reg: Div(reg, reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002081 {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002082 maskUnsignedResult = true;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002083
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002084 // If either operand of divide is smaller than 64 bits, we have
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002085 // to make sure the unused top bits are correct because they affect
2086 // the result. These bits are already correct for unsigned values.
2087 // They may be incorrect for signed values, so sign extend to fill in.
2088 Instruction* divI = subtreeRoot->getInstruction();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002089 Value* divOp1 = subtreeRoot->leftChild()->getValue();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002090 Value* divOp2 = subtreeRoot->rightChild()->getValue();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002091 Value* divOp1ToUse = divOp1;
2092 Value* divOp2ToUse = divOp2;
2093 if (divI->getType()->isSigned()) {
2094 unsigned opSize=target.getTargetData().getTypeSize(divI->getType());
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002095 if (opSize < 8) {
2096 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(divI);
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002097 divOp1ToUse = new TmpInstruction(mcfi, divOp1);
2098 divOp2ToUse = new TmpInstruction(mcfi, divOp2);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002099 target.getInstrInfo().
2100 CreateSignExtensionInstructions(target,
2101 divI->getParent()->getParent(),
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002102 divOp1, divOp1ToUse,
2103 8*opSize, mvec, mcfi);
2104 target.getInstrInfo().
2105 CreateSignExtensionInstructions(target,
2106 divI->getParent()->getParent(),
2107 divOp2, divOp2ToUse,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002108 8*opSize, mvec, mcfi);
2109 }
2110 }
2111
2112 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002113 .addReg(divOp1ToUse)
2114 .addReg(divOp2ToUse)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002115 .addRegDef(divI));
2116
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002117 break;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002118 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002119
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002120 case 37: // reg: Rem(reg, reg)
2121 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00002122 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002123 maskUnsignedResult = true;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002124
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002125 Instruction* remI = subtreeRoot->getInstruction();
2126 Value* divOp1 = subtreeRoot->leftChild()->getValue();
2127 Value* divOp2 = subtreeRoot->rightChild()->getValue();
2128
2129 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(remI);
Vikram S. Adve510eec72001-11-04 21:59:14 +00002130
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002131 // If second operand of divide is smaller than 64 bits, we have
2132 // to make sure the unused top bits are correct because they affect
2133 // the result. These bits are already correct for unsigned values.
2134 // They may be incorrect for signed values, so sign extend to fill in.
2135 //
2136 Value* divOpToUse = divOp2;
2137 if (divOp2->getType()->isSigned()) {
2138 unsigned opSize=target.getTargetData().getTypeSize(divOp2->getType());
2139 if (opSize < 8) {
2140 divOpToUse = new TmpInstruction(mcfi, divOp2);
2141 target.getInstrInfo().
2142 CreateSignExtensionInstructions(target,
2143 remI->getParent()->getParent(),
2144 divOp2, divOpToUse,
2145 8*opSize, mvec, mcfi);
2146 }
2147 }
2148
2149 // Now compute: result = rem V1, V2 as:
2150 // result = V1 - (V1 / signExtend(V2)) * signExtend(V2)
2151 //
2152 TmpInstruction* quot = new TmpInstruction(mcfi, divOp1, divOpToUse);
2153 TmpInstruction* prod = new TmpInstruction(mcfi, quot, divOpToUse);
2154
2155 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
2156 .addReg(divOp1).addReg(divOpToUse).addRegDef(quot));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002157
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002158 mvec.push_back(BuildMI(ChooseMulInstructionByType(remI->getType()), 3)
2159 .addReg(quot).addReg(divOpToUse).addRegDef(prod));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002160
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002161 mvec.push_back(BuildMI(ChooseSubInstructionByType(remI->getType()), 3)
2162 .addReg(divOp1).addReg(prod).addRegDef(remI));
2163
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002164 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00002165 }
2166
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002167 case 38: // bool: And(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002168 case 138: // bool: And(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002169 case 238: // bool: And(bool, boolconst)
2170 case 338: // reg : BAnd(reg, reg)
2171 case 538: // reg : BAnd(reg, Constant)
2172 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
2173 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002174
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002175 case 438: // bool: BAnd(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002176 { // Use the argument of NOT as the second argument!
2177 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002178 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002179 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2180 Value* notArg = BinaryOperator::getNotArgument(
2181 cast<BinaryOperator>(notNode->getInstruction()));
2182 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002183 Value *lhs = subtreeRoot->leftChild()->getValue();
2184 Value *dest = subtreeRoot->getValue();
2185 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(lhs).addReg(notArg)
2186 .addReg(dest, MOTy::Def));
2187
2188 if (notArg->getType() == Type::BoolTy)
2189 { // set 1 in result register if result of above is non-zero
2190 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2191 .addReg(dest, MOTy::UseAndDef));
2192 }
2193
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002194 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002195 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002196
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002197 case 39: // bool: Or(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002198 case 139: // bool: Or(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002199 case 239: // bool: Or(bool, boolconst)
2200 case 339: // reg : BOr(reg, reg)
2201 case 539: // reg : BOr(reg, Constant)
2202 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
2203 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002204
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002205 case 439: // bool: BOr(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002206 { // Use the argument of NOT as the second argument!
2207 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002208 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002209 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2210 Value* notArg = BinaryOperator::getNotArgument(
2211 cast<BinaryOperator>(notNode->getInstruction()));
2212 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002213 Value *lhs = subtreeRoot->leftChild()->getValue();
2214 Value *dest = subtreeRoot->getValue();
2215
2216 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(lhs).addReg(notArg)
2217 .addReg(dest, MOTy::Def));
2218
2219 if (notArg->getType() == Type::BoolTy)
2220 { // set 1 in result register if result of above is non-zero
2221 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2222 .addReg(dest, MOTy::UseAndDef));
2223 }
2224
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002225 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002226 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002227
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002228 case 40: // bool: Xor(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002229 case 140: // bool: Xor(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002230 case 240: // bool: Xor(bool, boolconst)
2231 case 340: // reg : BXor(reg, reg)
2232 case 540: // reg : BXor(reg, Constant)
2233 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
2234 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002235
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002236 case 440: // bool: BXor(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002237 { // Use the argument of NOT as the second argument!
2238 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002239 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002240 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2241 Value* notArg = BinaryOperator::getNotArgument(
2242 cast<BinaryOperator>(notNode->getInstruction()));
2243 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002244 Value *lhs = subtreeRoot->leftChild()->getValue();
2245 Value *dest = subtreeRoot->getValue();
2246 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(lhs).addReg(notArg)
2247 .addReg(dest, MOTy::Def));
2248
2249 if (notArg->getType() == Type::BoolTy)
2250 { // set 1 in result register if result of above is non-zero
2251 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2252 .addReg(dest, MOTy::UseAndDef));
2253 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002254 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002255 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002256
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002257 case 41: // setCCconst: SetCC(reg, Constant)
2258 { // Comparison is with a constant:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002259 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002260 // If the bool result must be computed into a register (see below),
2261 // and the constant is int ZERO, we can use the MOVR[op] instructions
2262 // and avoid the SUBcc instruction entirely.
2263 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002264 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002265 // The result of the SetCC must be computed and stored in a register if
2266 // it is used outside the current basic block (so it must be computed
2267 // as a boolreg) or it is used by anything other than a branch.
2268 // We will use a conditional move to do this.
2269 //
2270 Instruction* setCCInstr = subtreeRoot->getInstruction();
2271 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2272 ! AllUsesAreBranches(setCCInstr));
2273
2274 if (computeBoolVal)
2275 {
2276 InstrTreeNode* constNode = subtreeRoot->rightChild();
2277 assert(constNode &&
2278 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
2279 Constant *constVal = cast<Constant>(constNode->getValue());
2280 bool isValidConst;
2281
2282 if ((constVal->getType()->isInteger()
2283 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00002284 && target.getInstrInfo().ConvertConstantToIntType(target,
2285 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002286 && isValidConst)
2287 {
2288 // That constant is an integer zero after all...
2289 // Use a MOVR[op] to compute the boolean result
2290 // Unconditionally set register to 0
2291 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0)
2292 .addRegDef(setCCInstr));
2293
2294 // Now conditionally move 1 into the register.
2295 // Mark the register as a use (as well as a def) because the old
2296 // value will be retained if the condition is false.
2297 MachineOpCode movOpCode = ChooseMovpregiForSetCC(subtreeRoot);
2298 mvec.push_back(BuildMI(movOpCode, 3)
2299 .addReg(subtreeRoot->leftChild()->getValue())
2300 .addZImm(1).addReg(setCCInstr, MOTy::UseAndDef));
2301
2302 break;
2303 }
2304 }
2305 // ELSE FALL THROUGH
2306 }
2307
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002308 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002309 {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002310 // This generates a SUBCC instruction, putting the difference in a
2311 // result reg. if needed, and/or setting a condition code if needed.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002312 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002313 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002314 Value* leftVal = subtreeRoot->leftChild()->getValue();
2315 Value* rightVal = subtreeRoot->rightChild()->getValue();
2316 const Type* opType = leftVal->getType();
2317 bool isFPCompare = opType->isFloatingPoint();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002318
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002319 // If the boolean result of the SetCC is used outside the current basic
2320 // block (so it must be computed as a boolreg) or is used by anything
2321 // other than a branch, the boolean must be computed and stored
2322 // in a result register. We will use a conditional move to do this.
2323 //
2324 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2325 ! AllUsesAreBranches(setCCInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002326
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002327 // A TmpInstruction is created to represent the CC "result".
2328 // Unlike other instances of TmpInstruction, this one is used
2329 // by machine code of multiple LLVM instructions, viz.,
2330 // the SetCC and the branch. Make sure to get the same one!
2331 // Note that we do this even for FP CC registers even though they
2332 // are explicit operands, because the type of the operand
2333 // needs to be a floating point condition code, not an integer
2334 // condition code. Think of this as casting the bool result to
2335 // a FP condition code register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002336 // Later, we mark the 4th operand as being a CC register, and as a def.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002337 //
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002338 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002339 setCCInstr->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00002340 leftVal->getType(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002341 MachineCodeForInstruction::get(setCCInstr));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002342
2343 // If the operands are signed values smaller than 4 bytes, then they
2344 // must be sign-extended in order to do a valid 32-bit comparison
2345 // and get the right result in the 32-bit CC register (%icc).
2346 //
2347 Value* leftOpToUse = leftVal;
2348 Value* rightOpToUse = rightVal;
2349 if (opType->isIntegral() && opType->isSigned()) {
2350 unsigned opSize = target.getTargetData().getTypeSize(opType);
2351 if (opSize < 4) {
2352 MachineCodeForInstruction& mcfi =
2353 MachineCodeForInstruction::get(setCCInstr);
2354
2355 // create temporary virtual regs. to hold the sign-extensions
2356 leftOpToUse = new TmpInstruction(mcfi, leftVal);
2357 rightOpToUse = new TmpInstruction(mcfi, rightVal);
2358
2359 // sign-extend each operand and put the result in the temporary reg.
2360 target.getInstrInfo().CreateSignExtensionInstructions
2361 (target, setCCInstr->getParent()->getParent(),
2362 leftVal, leftOpToUse, 8*opSize, mvec, mcfi);
2363 target.getInstrInfo().CreateSignExtensionInstructions
2364 (target, setCCInstr->getParent()->getParent(),
2365 rightVal, rightOpToUse, 8*opSize, mvec, mcfi);
2366 }
2367 }
2368
Misha Brukman7b647942003-05-30 20:11:56 +00002369 if (! isFPCompare) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002370 // Integer condition: set CC and discard result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002371 mvec.push_back(BuildMI(V9::SUBccr, 4)
2372 .addReg(leftOpToUse)
2373 .addReg(rightOpToUse)
2374 .addMReg(target.getRegInfo().getZeroRegNum(),MOTy::Def)
2375 .addCCReg(tmpForCC, MOTy::Def));
Misha Brukman7b647942003-05-30 20:11:56 +00002376 } else {
2377 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002378 mvec.push_back(BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2379 .addCCReg(tmpForCC, MOTy::Def)
2380 .addReg(leftOpToUse)
2381 .addReg(rightOpToUse));
Misha Brukman7b647942003-05-30 20:11:56 +00002382 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002383
Misha Brukman7b647942003-05-30 20:11:56 +00002384 if (computeBoolVal) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002385 MachineOpCode movOpCode = (isFPCompare
Misha Brukmaneecdb662003-06-02 20:55:14 +00002386 ? ChooseMovFpcciInstruction(subtreeRoot)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002387 : ChooseMovpcciForSetCC(subtreeRoot));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002388
2389 // Unconditionally set register to 0
2390 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
2391 mvec.push_back(M);
2392
2393 // Now conditionally move 1 into the register.
Misha Brukman7b647942003-05-30 20:11:56 +00002394 // Mark the register as a use (as well as a def) because the old
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002395 // value will be retained if the condition is false.
2396 M = (BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(1)
2397 .addReg(setCCInstr, MOTy::UseAndDef));
Misha Brukman7b647942003-05-30 20:11:56 +00002398 mvec.push_back(M);
2399 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002400 break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002401 }
2402
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002403 case 51: // reg: Load(reg)
2404 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002405 SetOperandsForMemInstr(ChooseLoadInstruction(
2406 subtreeRoot->getValue()->getType()),
2407 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002408 break;
2409
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002410 case 55: // reg: GetElemPtr(reg)
2411 case 56: // reg: GetElemPtrIdx(reg,reg)
2412 // If the GetElemPtr was folded into the user (parent), it will be
2413 // caught above. For other cases, we have to compute the address.
2414 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
2415 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002416
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002417 case 57: // reg: Alloca: Implement as 1 instruction:
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002418 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002419 AllocationInst* instr =
2420 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002421 unsigned tsize =
2422 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002423 assert(tsize != 0);
2424 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002425 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002426 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002427
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002428 case 58: // reg: Alloca(reg): Implement as 3 instructions:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002429 // mul num, typeSz -> tmp
2430 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002431 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002432 AllocationInst* instr =
2433 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002434 const Type* eltType = instr->getAllocatedType();
2435
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002436 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002437 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002438 Value* numElementsVal = NULL;
2439 bool isArray = instr->isArrayAllocation();
2440
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002441 if (!isArray || isa<Constant>(numElementsVal = instr->getArraySize())) {
Misha Brukman7b647942003-05-30 20:11:56 +00002442 // total size is constant: generate code for fixed-size alloca
2443 unsigned numElements = isArray?
2444 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2445 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2446 numElements, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002447 } else {
2448 // total size is not constant.
Vikram S. Adve74825322002-03-18 03:15:35 +00002449 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002450 numElementsVal, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002451 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002452 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002453 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002454
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002455 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002456 { // Generate a direct (CALL) or indirect (JMPL) call.
2457 // Mark the return-address register, the indirection
2458 // register (for indirect calls), the operands of the Call,
2459 // and the return value (if any) as implicit operands
2460 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002461 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002462 // If this is a varargs function, floating point arguments
2463 // have to passed in integer registers so insert
2464 // copy-float-to-int instructions for each float operand.
2465 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002466 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002467 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002468 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002469
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002470 // Check if this is an intrinsic function that needs a special code
2471 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002472 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002473 bool specialIntrinsic = false;
2474 LLVMIntrinsic::ID iid;
2475 if (calledFunc && (iid=(LLVMIntrinsic::ID)calledFunc->getIntrinsicID()))
2476 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002477
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002478 // If not, generate the normal call sequence for the function.
2479 // This can also handle any intrinsics that are just function calls.
2480 //
Misha Brukman7b647942003-05-30 20:11:56 +00002481 if (! specialIntrinsic) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002482 Function* currentFunc = callInstr->getParent()->getParent();
2483 MachineFunction& MF = MachineFunction::get(currentFunc);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002484 MachineCodeForInstruction& mcfi =
2485 MachineCodeForInstruction::get(callInstr);
2486 const UltraSparcRegInfo& regInfo =
2487 (UltraSparcRegInfo&) target.getRegInfo();
2488 const TargetFrameInfo& frameInfo = target.getFrameInfo();
2489
Misha Brukman7b647942003-05-30 20:11:56 +00002490 // Create hidden virtual register for return address with type void*
2491 TmpInstruction* retAddrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002492 new TmpInstruction(mcfi, PointerType::get(Type::VoidTy), callInstr);
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002493
Misha Brukman7b647942003-05-30 20:11:56 +00002494 // Generate the machine instruction and its operands.
2495 // Use CALL for direct function calls; this optimistically assumes
2496 // the PC-relative address fits in the CALL address field (22 bits).
2497 // Use JMPL for indirect calls.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002498 // This will be added to mvec later, after operand copies.
Misha Brukman7b647942003-05-30 20:11:56 +00002499 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002500 MachineInstr* callMI;
Misha Brukman7b647942003-05-30 20:11:56 +00002501 if (calledFunc) // direct function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002502 callMI = BuildMI(V9::CALL, 1).addPCDisp(callee);
Misha Brukman7b647942003-05-30 20:11:56 +00002503 else // indirect function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002504 callMI = (BuildMI(V9::JMPLCALLi,3).addReg(callee)
2505 .addSImm((int64_t)0).addRegDef(retAddrReg));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002506
Misha Brukman7b647942003-05-30 20:11:56 +00002507 const FunctionType* funcType =
2508 cast<FunctionType>(cast<PointerType>(callee->getType())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002509 ->getElementType());
Misha Brukman7b647942003-05-30 20:11:56 +00002510 bool isVarArgs = funcType->isVarArg();
2511 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002512
Misha Brukman7b647942003-05-30 20:11:56 +00002513 // Use a descriptor to pass information about call arguments
2514 // to the register allocator. This descriptor will be "owned"
2515 // and freed automatically when the MachineCodeForInstruction
2516 // object for the callInstr goes away.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002517 CallArgsDescriptor* argDesc =
2518 new CallArgsDescriptor(callInstr, retAddrReg,isVarArgs,noPrototype);
Misha Brukman7b647942003-05-30 20:11:56 +00002519 assert(callInstr->getOperand(0) == callee
2520 && "This is assumed in the loop below!");
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002521
2522 // Insert sign-extension instructions for small signed values,
2523 // if this is an unknown function (i.e., called via a funcptr)
2524 // or an external one (i.e., which may not be compiled by llc).
2525 //
2526 if (calledFunc == NULL || calledFunc->isExternal()) {
2527 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
2528 Value* argVal = callInstr->getOperand(i);
2529 const Type* argType = argVal->getType();
2530 if (argType->isIntegral() && argType->isSigned()) {
2531 unsigned argSize = target.getTargetData().getTypeSize(argType);
2532 if (argSize <= 4) {
2533 // create a temporary virtual reg. to hold the sign-extension
2534 TmpInstruction* argExtend = new TmpInstruction(mcfi, argVal);
2535
2536 // sign-extend argVal and put the result in the temporary reg.
2537 target.getInstrInfo().CreateSignExtensionInstructions
2538 (target, currentFunc, argVal, argExtend,
2539 8*argSize, mvec, mcfi);
2540
2541 // replace argVal with argExtend in CallArgsDescriptor
2542 argDesc->getArgInfo(i-1).replaceArgVal(argExtend);
2543 }
2544 }
2545 }
2546 }
2547
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002548 // Insert copy instructions to get all the arguments into
2549 // all the places that they need to be.
2550 //
Misha Brukman7b647942003-05-30 20:11:56 +00002551 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002552 int argNo = i-1;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002553 CallArgInfo& argInfo = argDesc->getArgInfo(argNo);
2554 Value* argVal = argInfo.getArgVal(); // don't use callInstr arg here
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002555 const Type* argType = argVal->getType();
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002556 unsigned regType = regInfo.getRegTypeForDataType(argType);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002557 unsigned argSize = target.getTargetData().getTypeSize(argType);
2558 int regNumForArg = TargetRegInfo::getInvalidRegNum();
2559 unsigned regClassIDOfArgReg;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002560
Misha Brukman7b647942003-05-30 20:11:56 +00002561 // Check for FP arguments to varargs functions.
2562 // Any such argument in the first $K$ args must be passed in an
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002563 // integer register. If there is no prototype, it must also
2564 // be passed as an FP register.
2565 // K = #integer argument registers.
2566 bool isFPArg = argVal->getType()->isFloatingPoint();
2567 if (isVarArgs && isFPArg) {
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002568
2569 if (noPrototype) {
2570 // It is a function with no prototype: pass value
2571 // as an FP value as well as a varargs value. The FP value
2572 // may go in a register or on the stack. The copy instruction
2573 // to the outgoing reg/stack is created by the normal argument
2574 // handling code since this is the "normal" passing mode.
2575 //
2576 regNumForArg = regInfo.regNumForFPArg(regType,
2577 false, false, argNo,
2578 regClassIDOfArgReg);
2579 if (regNumForArg == regInfo.getInvalidRegNum())
2580 argInfo.setUseStackSlot();
2581 else
2582 argInfo.setUseFPArgReg();
2583 }
2584
2585 // If this arg. is in the first $K$ regs, add special copy-
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002586 // float-to-int instructions to pass the value as an int.
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002587 // To check if it is in the first $K$, get the register
2588 // number for the arg #i. These copy instructions are
2589 // generated here because they are extra cases and not needed
2590 // for the normal argument handling (some code reuse is
2591 // possible though -- later).
2592 //
Misha Brukmanea481cc2003-06-03 03:21:58 +00002593 int copyRegNum = regInfo.regNumForIntArg(false, false, argNo,
2594 regClassIDOfArgReg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002595 if (copyRegNum != regInfo.getInvalidRegNum()) {
2596 // Create a virtual register to represent copyReg. Mark
2597 // this vreg as being an implicit operand of the call MI
2598 const Type* loadTy = (argType == Type::FloatTy
2599 ? Type::IntTy : Type::LongTy);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002600 TmpInstruction* argVReg = new TmpInstruction(mcfi, loadTy,
2601 argVal, NULL,
2602 "argRegCopy");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002603 callMI->addImplicitRef(argVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002604
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002605 // Get a temp stack location to use to copy
2606 // float-to-int via the stack.
2607 //
2608 // FIXME: For now, we allocate permanent space because
2609 // the stack frame manager does not allow locals to be
2610 // allocated (e.g., for alloca) after a temp is
2611 // allocated!
2612 //
2613 // int tmpOffset = MF.getInfo()->pushTempValue(argSize);
2614 int tmpOffset = MF.getInfo()->allocateLocalVar(argVReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002615
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002616 // Generate the store from FP reg to stack
Misha Brukmanea481cc2003-06-03 03:21:58 +00002617 unsigned StoreOpcode = ChooseStoreInstruction(argType);
2618 M = BuildMI(convertOpcodeFromRegToImm(StoreOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002619 .addReg(argVal).addMReg(regInfo.getFramePointer())
2620 .addSImm(tmpOffset);
2621 mvec.push_back(M);
2622
2623 // Generate the load from stack to int arg reg
Misha Brukmanea481cc2003-06-03 03:21:58 +00002624 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
2625 M = BuildMI(convertOpcodeFromRegToImm(LoadOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002626 .addMReg(regInfo.getFramePointer()).addSImm(tmpOffset)
2627 .addReg(argVReg, MOTy::Def);
2628
2629 // Mark operand with register it should be assigned
2630 // both for copy and for the callMI
2631 M->SetRegForOperand(M->getNumOperands()-1, copyRegNum);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002632 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2633 copyRegNum);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002634 mvec.push_back(M);
2635
2636 // Add info about the argument to the CallArgsDescriptor
2637 argInfo.setUseIntArgReg();
2638 argInfo.setArgCopy(copyRegNum);
2639 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00002640 // Cannot fit in first $K$ regs so pass arg on stack
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002641 argInfo.setUseStackSlot();
2642 }
2643 } else if (isFPArg) {
2644 // Get the outgoing arg reg to see if there is one.
2645 regNumForArg = regInfo.regNumForFPArg(regType, false, false,
2646 argNo, regClassIDOfArgReg);
2647 if (regNumForArg == regInfo.getInvalidRegNum())
2648 argInfo.setUseStackSlot();
2649 else {
2650 argInfo.setUseFPArgReg();
2651 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2652 regNumForArg);
2653 }
2654 } else {
2655 // Get the outgoing arg reg to see if there is one.
2656 regNumForArg = regInfo.regNumForIntArg(false,false,
2657 argNo, regClassIDOfArgReg);
2658 if (regNumForArg == regInfo.getInvalidRegNum())
2659 argInfo.setUseStackSlot();
2660 else {
2661 argInfo.setUseIntArgReg();
2662 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2663 regNumForArg);
2664 }
2665 }
2666
2667 //
2668 // Now insert copy instructions to stack slot or arg. register
2669 //
2670 if (argInfo.usesStackSlot()) {
2671 // Get the stack offset for this argument slot.
2672 // FP args on stack are right justified so adjust offset!
2673 // int arguments are also right justified but they are
2674 // always loaded as a full double-word so the offset does
2675 // not need to be adjusted.
2676 int argOffset = frameInfo.getOutgoingArgOffset(MF, argNo);
2677 if (argType->isFloatingPoint()) {
2678 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
2679 assert(argSize <= slotSize && "Insufficient slot size!");
2680 argOffset += slotSize - argSize;
2681 }
2682
2683 // Now generate instruction to copy argument to stack
2684 MachineOpCode storeOpCode =
2685 (argType->isFloatingPoint()
2686 ? ((argSize == 4)? V9::STFi : V9::STDFi) : V9::STXi);
2687
2688 M = BuildMI(storeOpCode, 3).addReg(argVal)
2689 .addMReg(regInfo.getStackPointer()).addSImm(argOffset);
2690 mvec.push_back(M);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002691 }
2692 else if (regNumForArg != regInfo.getInvalidRegNum()) {
2693
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002694 // Create a virtual register to represent the arg reg. Mark
2695 // this vreg as being an implicit operand of the call MI.
2696 TmpInstruction* argVReg =
2697 new TmpInstruction(mcfi, argVal, NULL, "argReg");
2698
2699 callMI->addImplicitRef(argVReg);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002700
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002701 // Generate the reg-to-reg copy into the outgoing arg reg.
2702 // -- For FP values, create a FMOVS or FMOVD instruction
2703 // -- For non-FP values, create an add-with-0 instruction
2704 if (argType->isFloatingPoint())
2705 M=(BuildMI(argType==Type::FloatTy? V9::FMOVS :V9::FMOVD,2)
2706 .addReg(argVal).addReg(argVReg, MOTy::Def));
2707 else
2708 M = (BuildMI(ChooseAddInstructionByType(argType), 3)
2709 .addReg(argVal).addSImm((int64_t) 0)
2710 .addReg(argVReg, MOTy::Def));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002711
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002712 // Mark the operand with the register it should be assigned
2713 M->SetRegForOperand(M->getNumOperands()-1, regNumForArg);
2714 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2715 regNumForArg);
2716
2717 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +00002718 }
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002719 else
2720 assert(argInfo.getArgCopy() != regInfo.getInvalidRegNum() &&
2721 "Arg. not in stack slot, primary or secondary register?");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002722 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002723
2724 // add call instruction and delay slot before copying return value
2725 mvec.push_back(callMI);
2726 mvec.push_back(BuildMI(V9::NOP, 0));
2727
Misha Brukman7b647942003-05-30 20:11:56 +00002728 // Add the return value as an implicit ref. The call operands
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002729 // were added above. Also, add code to copy out the return value.
2730 // This is always register-to-register for int or FP return values.
2731 //
2732 if (callInstr->getType() != Type::VoidTy) {
2733 // Get the return value reg.
2734 const Type* retType = callInstr->getType();
2735
2736 int regNum = (retType->isFloatingPoint()
2737 ? (unsigned) SparcFloatRegClass::f0
2738 : (unsigned) SparcIntRegClass::o0);
2739 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
2740 regNum = regInfo.getUnifiedRegNum(regClassID, regNum);
2741
2742 // Create a virtual register to represent it and mark
2743 // this vreg as being an implicit operand of the call MI
2744 TmpInstruction* retVReg =
2745 new TmpInstruction(mcfi, callInstr, NULL, "argReg");
2746
2747 callMI->addImplicitRef(retVReg, /*isDef*/ true);
2748
2749 // Generate the reg-to-reg copy from the return value reg.
2750 // -- For FP values, create a FMOVS or FMOVD instruction
2751 // -- For non-FP values, create an add-with-0 instruction
2752 if (retType->isFloatingPoint())
2753 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
2754 .addReg(retVReg).addReg(callInstr, MOTy::Def));
2755 else
2756 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
2757 .addReg(retVReg).addSImm((int64_t) 0)
2758 .addReg(callInstr, MOTy::Def));
2759
2760 // Mark the operand with the register it should be assigned
2761 // Also mark the implicit ref of the call defining this operand
2762 M->SetRegForOperand(0, regNum);
2763 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,regNum);
2764
2765 mvec.push_back(M);
2766 }
2767
Misha Brukman7b647942003-05-30 20:11:56 +00002768 // For the CALL instruction, the ret. addr. reg. is also implicit
2769 if (isa<Function>(callee))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002770 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
2771
2772 MF.getInfo()->popAllTempValues(); // free temps used for this inst
Misha Brukman7b647942003-05-30 20:11:56 +00002773 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002774
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002775 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002776 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002777
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002778 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002779 {
2780 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2781 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2782 Instruction* shlInstr = subtreeRoot->getInstruction();
2783
2784 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002785 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2786 "Shl unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002787 unsigned opSize = target.getTargetData().getTypeSize(opType);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002788
2789 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Vikram S. Advee895a742003-08-06 18:48:40 +00002790 (opSize > 4)? V9::SLLXr6:V9::SLLr5,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002791 argVal1, argVal2, 0, shlInstr, mvec,
2792 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002793 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002794 }
2795
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002796 case 63: // reg: Shr(reg, reg)
Misha Brukman7b647942003-05-30 20:11:56 +00002797 {
2798 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002799 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2800 "Shr unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002801 unsigned opSize = target.getTargetData().getTypeSize(opType);
Chris Lattner54e898e2003-01-15 19:23:34 +00002802 Add3OperandInstr(opType->isSigned()
Vikram S. Advee895a742003-08-06 18:48:40 +00002803 ? (opSize > 4? V9::SRAXr6 : V9::SRAr5)
2804 : (opSize > 4? V9::SRLXr6 : V9::SRLr5),
Chris Lattner54e898e2003-01-15 19:23:34 +00002805 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002806 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002807 }
2808
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002809 case 64: // reg: Phi(reg,reg)
2810 break; // don't forward the value
Vikram S. Adve74825322002-03-18 03:15:35 +00002811
Vikram S. Adve9d275142003-08-12 03:04:05 +00002812 case 65: // reg: VaArg(reg): the va_arg instruction
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002813 {
2814 // Use value initialized by va_start as pointer to args on the stack.
2815 // Load argument via current pointer value, then increment pointer.
2816 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
2817 Instruction* vaArgI = subtreeRoot->getInstruction();
Vikram S. Adve9d275142003-08-12 03:04:05 +00002818 MachineOpCode loadOp = vaArgI->getType()->isFloatingPoint()? V9::LDDFi
2819 : V9::LDXi;
2820 mvec.push_back(BuildMI(loadOp, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002821 addSImm(0).addRegDef(vaArgI));
Misha Brukman91aee472003-05-27 22:37:00 +00002822 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002823 addSImm(argSize).addRegDef(vaArgI->getOperand(0)));
2824 break;
2825 }
2826
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002827 case 71: // reg: VReg
2828 case 72: // reg: Constant
2829 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002830
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002831 default:
2832 assert(0 && "Unrecognized BURG rule");
2833 break;
2834 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002835 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002836
Misha Brukman7b647942003-05-30 20:11:56 +00002837 if (forwardOperandNum >= 0) {
2838 // We did not generate a machine instruction but need to use operand.
2839 // If user is in the same tree, replace Value in its machine operand.
2840 // If not, insert a copy instruction which should get coalesced away
2841 // by register allocation.
2842 if (subtreeRoot->parent() != NULL)
2843 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2844 else {
2845 std::vector<MachineInstr*> minstrVec;
2846 Instruction* instr = subtreeRoot->getInstruction();
2847 target.getInstrInfo().
2848 CreateCopyInstructionsByType(target,
2849 instr->getParent()->getParent(),
2850 instr->getOperand(forwardOperandNum),
2851 instr, minstrVec,
2852 MachineCodeForInstruction::get(instr));
2853 assert(minstrVec.size() > 0);
2854 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Chris Lattner20b1ea02001-09-14 03:47:57 +00002855 }
Misha Brukman7b647942003-05-30 20:11:56 +00002856 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002857
Misha Brukman7b647942003-05-30 20:11:56 +00002858 if (maskUnsignedResult) {
2859 // If result is unsigned and smaller than int reg size,
2860 // we need to clear high bits of result value.
2861 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2862 Instruction* dest = subtreeRoot->getInstruction();
2863 if (dest->getType()->isUnsigned()) {
2864 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
2865 if (destSize <= 4) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002866 // Mask high 64 - N bits, where N = 4*destSize.
2867
2868 // Use a TmpInstruction to represent the
Misha Brukman7b647942003-05-30 20:11:56 +00002869 // intermediate result before masking. Since those instructions
2870 // have already been generated, go back and substitute tmpI
2871 // for dest in the result position of each one of them.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002872 //
2873 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(dest);
2874 TmpInstruction *tmpI = new TmpInstruction(mcfi, dest->getType(),
2875 dest, NULL, "maskHi");
2876 Value* srlArgToUse = tmpI;
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002877
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002878 unsigned numSubst = 0;
2879 for (unsigned i=0, N=mvec.size(); i < N; ++i) {
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002880
2881 // Make sure we substitute all occurrences of dest in these instrs.
2882 // Otherwise, we will have bogus code.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002883 bool someArgsWereIgnored = false;
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002884
2885 // Make sure not to substitute an upwards-exposed use -- that would
2886 // introduce a use of `tmpI' with no preceding def. Therefore,
2887 // substitute a use or def-and-use operand only if a previous def
2888 // operand has already been substituted (i.e., numSusbt > 0).
2889 //
2890 numSubst += mvec[i]->substituteValue(dest, tmpI,
2891 /*defsOnly*/ numSubst == 0,
2892 /*notDefsAndUses*/ numSubst > 0,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002893 someArgsWereIgnored);
2894 assert(!someArgsWereIgnored &&
2895 "Operand `dest' exists but not replaced: probably bogus!");
2896 }
2897 assert(numSubst > 0 && "Operand `dest' not replaced: probably bogus!");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002898
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002899 // Left shift 32-N if size (N) is less than 32 bits.
2900 // Use another tmp. virtual registe to represent this result.
2901 if (destSize < 4) {
2902 srlArgToUse = new TmpInstruction(mcfi, dest->getType(),
2903 tmpI, NULL, "maskHi2");
2904 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpI)
2905 .addZImm(8*(4-destSize))
2906 .addReg(srlArgToUse, MOTy::Def));
2907 }
2908
2909 // Logical right shift 32-N to get zero extension in top 64-N bits.
2910 mvec.push_back(BuildMI(V9::SRLi5, 3).addReg(srlArgToUse)
2911 .addZImm(8*(4-destSize)).addReg(dest, MOTy::Def));
2912
Misha Brukman7b647942003-05-30 20:11:56 +00002913 } else if (destSize < 8) {
2914 assert(0 && "Unsupported type size: 32 < size < 64 bits");
2915 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002916 }
Misha Brukman7b647942003-05-30 20:11:56 +00002917 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002918}