blob: 61a804513e26d03517aee65439252798b4412005 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000011#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerea45d7b2002-12-28 20:19:44 +000016#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner9c461082002-02-03 07:50:56 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/iTerminators.h"
20#include "llvm/iMemory.h"
21#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000024#include "llvm/ConstantHandling.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000026#include <math.h>
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 // Remember if it has leading zero index: it will be discarded later.
129 lastInstHasLeadingNonZero = ! IsZero(*firstIdx);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000130
Misha Brukman81b06862003-05-21 18:48:06 +0000131 // Insert its index vector at the start, skipping any leading [0]
132 chainIdxVec.insert(chainIdxVec.begin(),
133 firstIdx + !lastInstHasLeadingNonZero, lastIdx);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000134
Misha Brukman81b06862003-05-21 18:48:06 +0000135 // Mark the folded node so no code is generated for it.
136 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000137
Misha Brukman81b06862003-05-21 18:48:06 +0000138 // Get the previous GEP instruction and continue trying to fold
139 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
140 } else // cannot fold this getElementPtr instr. or any preceding ones
141 break;
142 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000143
144 // If the first getElementPtr instruction had a leading [0], add it back.
145 // Note that this instruction is the *last* one successfully folded above.
146 if (ptrVal && ! lastInstHasLeadingNonZero)
147 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
148
149 return ptrVal;
150}
151
152
153//---------------------------------------------------------------------------
154// Function: GetGEPInstArgs
155//
156// Purpose:
157// Helper function for GetMemInstArgs that handles the final getElementPtr
158// instruction used by (or same as) the memory operation.
159// Extracts the indices of the current instruction and tries to fold in
160// preceding ones if all indices of the current one are constant.
161//---------------------------------------------------------------------------
162
163static Value *
164GetGEPInstArgs(InstructionNode* gepNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000165 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000166 bool& allConstantIndices)
167{
168 allConstantIndices = true;
169 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
170
171 // Default pointer is the one from the current instruction.
172 Value* ptrVal = gepI->getPointerOperand();
173 InstrTreeNode* ptrChild = gepNode->leftChild();
174
175 // Extract the index vector of the GEP instructin.
176 // If all indices are constant and first index is zero, try to fold
177 // in preceding GEPs with all constant indices.
178 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
179 allConstantIndices && OI != OE; ++OI)
180 if (! isa<Constant>(*OI))
181 allConstantIndices = false; // note: this also terminates loop!
182
183 // If we have only constant indices, fold chains of constant indices
184 // in this and any preceding GetElemPtr instructions.
185 bool foldedGEPs = false;
186 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
187 if (allConstantIndices)
Misha Brukman81b06862003-05-21 18:48:06 +0000188 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) {
189 ptrVal = newPtr;
190 foldedGEPs = true;
191 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000192
193 // Append the index vector of the current instruction.
194 // Skip the leading [0] index if preceding GEPs were folded into this.
195 idxVec.insert(idxVec.end(),
196 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
197 gepI->idx_end());
198
199 return ptrVal;
200}
201
202//---------------------------------------------------------------------------
203// Function: GetMemInstArgs
204//
205// Purpose:
206// Get the pointer value and the index vector for a memory operation
207// (GetElementPtr, Load, or Store). If all indices of the given memory
208// operation are constant, fold in constant indices in a chain of
209// preceding GetElementPtr instructions (if any), and return the
210// pointer value of the first instruction in the chain.
211// All folded instructions are marked so no code is generated for them.
212//
213// Return values:
214// Returns the pointer Value to use.
215// Returns the resulting IndexVector in idxVec.
216// Returns true/false in allConstantIndices if all indices are/aren't const.
217//---------------------------------------------------------------------------
218
219static Value*
220GetMemInstArgs(InstructionNode* memInstrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000221 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000222 bool& allConstantIndices)
223{
224 allConstantIndices = false;
225 Instruction* memInst = memInstrNode->getInstruction();
226 assert(idxVec.size() == 0 && "Need empty vector to return indices");
227
228 // If there is a GetElemPtr instruction to fold in to this instr,
229 // it must be in the left child for Load and GetElemPtr, and in the
230 // right child for Store instructions.
231 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
232 ? memInstrNode->rightChild()
233 : memInstrNode->leftChild());
234
235 // Default pointer is the one from the current instruction.
236 Value* ptrVal = ptrChild->getValue();
237
238 // Find the "last" GetElemPtr instruction: this one or the immediate child.
239 // There will be none if this is a load or a store from a scalar pointer.
240 InstructionNode* gepNode = NULL;
241 if (isa<GetElementPtrInst>(memInst))
242 gepNode = memInstrNode;
Misha Brukman81b06862003-05-21 18:48:06 +0000243 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) {
244 // Child of load/store is a GEP and memInst is its only use.
245 // Use its indices and mark it as folded.
246 gepNode = cast<InstructionNode>(ptrChild);
247 gepNode->markFoldedIntoParent();
248 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000249
250 // If there are no indices, return the current pointer.
251 // Else extract the pointer from the GEP and fold the indices.
252 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
253 : ptrVal;
254}
255
Chris Lattner54e898e2003-01-15 19:23:34 +0000256
Chris Lattner20b1ea02001-09-14 03:47:57 +0000257//************************ Internal Functions ******************************/
258
Chris Lattner20b1ea02001-09-14 03:47:57 +0000259
Chris Lattner20b1ea02001-09-14 03:47:57 +0000260static inline MachineOpCode
261ChooseBprInstruction(const InstructionNode* instrNode)
262{
263 MachineOpCode opCode;
264
265 Instruction* setCCInstr =
266 ((InstructionNode*) instrNode->leftChild())->getInstruction();
267
268 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000269 {
270 case Instruction::SetEQ: opCode = V9::BRZ; break;
271 case Instruction::SetNE: opCode = V9::BRNZ; break;
272 case Instruction::SetLE: opCode = V9::BRLEZ; break;
273 case Instruction::SetGE: opCode = V9::BRGEZ; break;
274 case Instruction::SetLT: opCode = V9::BRLZ; break;
275 case Instruction::SetGT: opCode = V9::BRGZ; break;
276 default:
277 assert(0 && "Unrecognized VM instruction!");
278 opCode = V9::INVALID_OPCODE;
279 break;
280 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000281
282 return opCode;
283}
284
285
286static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000287ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000288 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000289{
Misha Brukmana98cd452003-05-20 20:32:24 +0000290 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000291
292 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
293
Misha Brukman81b06862003-05-21 18:48:06 +0000294 if (isSigned) {
295 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000296 {
Misha Brukman81b06862003-05-21 18:48:06 +0000297 case Instruction::SetEQ: opCode = V9::BE; break;
298 case Instruction::SetNE: opCode = V9::BNE; break;
299 case Instruction::SetLE: opCode = V9::BLE; break;
300 case Instruction::SetGE: opCode = V9::BGE; break;
301 case Instruction::SetLT: opCode = V9::BL; break;
302 case Instruction::SetGT: opCode = V9::BG; break;
303 default:
304 assert(0 && "Unrecognized VM instruction!");
305 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000306 }
Misha Brukman81b06862003-05-21 18:48:06 +0000307 } else {
308 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000309 {
Misha Brukman81b06862003-05-21 18:48:06 +0000310 case Instruction::SetEQ: opCode = V9::BE; break;
311 case Instruction::SetNE: opCode = V9::BNE; break;
312 case Instruction::SetLE: opCode = V9::BLEU; break;
313 case Instruction::SetGE: opCode = V9::BCC; break;
314 case Instruction::SetLT: opCode = V9::BCS; break;
315 case Instruction::SetGT: opCode = V9::BGU; break;
316 default:
317 assert(0 && "Unrecognized VM instruction!");
318 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000319 }
Misha Brukman81b06862003-05-21 18:48:06 +0000320 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000321
322 return opCode;
323}
324
325static inline MachineOpCode
326ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000327 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000328{
Misha Brukmana98cd452003-05-20 20:32:24 +0000329 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000330
331 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000332 {
333 case Instruction::SetEQ: opCode = V9::FBE; break;
334 case Instruction::SetNE: opCode = V9::FBNE; break;
335 case Instruction::SetLE: opCode = V9::FBLE; break;
336 case Instruction::SetGE: opCode = V9::FBGE; break;
337 case Instruction::SetLT: opCode = V9::FBL; break;
338 case Instruction::SetGT: opCode = V9::FBG; break;
339 default:
340 assert(0 && "Unrecognized VM instruction!");
341 break;
342 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000343
344 return opCode;
345}
346
347
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000348// Create a unique TmpInstruction for a boolean value,
349// representing the CC register used by a branch on that value.
350// For now, hack this using a little static cache of TmpInstructions.
351// Eventually the entire BURG instruction selection should be put
352// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000353// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000354// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000355//
356static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000357GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000358{
Chris Lattner09ff1122002-07-24 21:21:32 +0000359 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000360 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000361 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000362
363 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
364
Misha Brukman81b06862003-05-21 18:48:06 +0000365 if (lastFunction != F) {
366 lastFunction = F;
367 boolToTmpCache.clear();
368 }
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000369
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000370 // Look for tmpI and create a new one otherwise. The new value is
371 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000372 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
373 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000374 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000375
376 return tmpI;
377}
378
379
Chris Lattner20b1ea02001-09-14 03:47:57 +0000380static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000381ChooseBccInstruction(const InstructionNode* instrNode,
382 bool& isFPBranch)
383{
384 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000385 assert(setCCNode->getOpLabel() == SetCCOp);
386 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000387 const Type* setCCType = setCCInstr->getOperand(0)->getType();
388
Vikram S. Adve242a8082002-05-19 15:25:51 +0000389 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
390
391 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000392 return ChooseBFpccInstruction(instrNode, setCCInstr);
393 else
394 return ChooseBpccInstruction(instrNode, setCCInstr);
395}
396
397
398static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000399ChooseMovFpccInstruction(const InstructionNode* instrNode)
400{
Misha Brukmana98cd452003-05-20 20:32:24 +0000401 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000402
403 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000404 {
405 case Instruction::SetEQ: opCode = V9::MOVFE; break;
406 case Instruction::SetNE: opCode = V9::MOVFNE; break;
407 case Instruction::SetLE: opCode = V9::MOVFLE; break;
408 case Instruction::SetGE: opCode = V9::MOVFGE; break;
409 case Instruction::SetLT: opCode = V9::MOVFL; break;
410 case Instruction::SetGT: opCode = V9::MOVFG; break;
411 default:
412 assert(0 && "Unrecognized VM instruction!");
413 break;
414 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000415
416 return opCode;
417}
418
419
420// Assumes that SUBcc v1, v2 -> v3 has been executed.
421// In most cases, we want to clear v3 and then follow it by instruction
422// MOVcc 1 -> v3.
423// Set mustClearReg=false if v3 need not be cleared before conditional move.
424// Set valueToMove=0 if we want to conditionally move 0 instead of 1
425// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000426// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000427//
428static MachineOpCode
429ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000430 bool& mustClearReg,
431 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000432{
Misha Brukmana98cd452003-05-20 20:32:24 +0000433 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000434 mustClearReg = true;
435 valueToMove = 1;
436
437 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000438 {
439 case Instruction::SetEQ: opCode = V9::MOVE; break;
440 case Instruction::SetLE: opCode = V9::MOVLE; break;
441 case Instruction::SetGE: opCode = V9::MOVGE; break;
442 case Instruction::SetLT: opCode = V9::MOVL; break;
443 case Instruction::SetGT: opCode = V9::MOVG; break;
444 case Instruction::SetNE: assert(0 && "No move required!"); break;
445 default: assert(0 && "Unrecognized VM instr!"); break;
446 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000447
448 return opCode;
449}
450
Chris Lattner20b1ea02001-09-14 03:47:57 +0000451static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000452ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000453{
Misha Brukmana98cd452003-05-20 20:32:24 +0000454 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000455
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000456 switch(vopCode)
Misha Brukman81b06862003-05-21 18:48:06 +0000457 {
458 case ToFloatTy:
459 if (opType == Type::SByteTy || opType == Type::ShortTy ||
460 opType == Type::IntTy)
461 opCode = V9::FITOS;
462 else if (opType == Type::LongTy)
463 opCode = V9::FXTOS;
464 else if (opType == Type::DoubleTy)
465 opCode = V9::FDTOS;
466 else if (opType == Type::FloatTy)
467 ;
468 else
469 assert(0 && "Cannot convert this type to FLOAT on SPARC");
470 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000471
Misha Brukman81b06862003-05-21 18:48:06 +0000472 case ToDoubleTy:
473 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
474 // Both functions should treat the integer as a 32-bit value for types
475 // of 4 bytes or less, and as a 64-bit value otherwise.
476 if (opType == Type::SByteTy || opType == Type::UByteTy ||
477 opType == Type::ShortTy || opType == Type::UShortTy ||
478 opType == Type::IntTy || opType == Type::UIntTy)
479 opCode = V9::FITOD;
480 else if (opType == Type::LongTy || opType == Type::ULongTy)
481 opCode = V9::FXTOD;
482 else if (opType == Type::FloatTy)
483 opCode = V9::FSTOD;
484 else if (opType == Type::DoubleTy)
485 ;
486 else
487 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
488 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000489
Misha Brukman81b06862003-05-21 18:48:06 +0000490 default:
491 break;
492 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000493
494 return opCode;
495}
496
497static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000498ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000499{
Misha Brukmana98cd452003-05-20 20:32:24 +0000500 MachineOpCode opCode = V9::INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000501
502 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
503 && "This function should only be called for FLOAT or DOUBLE");
504
Misha Brukman81b06862003-05-21 18:48:06 +0000505 if (tid == Type::UIntTyID) {
506 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
507 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
508 } else if (tid == Type::SByteTyID || tid == Type::ShortTyID ||
509 tid == Type::IntTyID || tid == Type::UByteTyID ||
510 tid == Type::UShortTyID) {
511 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
512 } else if (tid == Type::LongTyID || tid == Type::ULongTyID) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000513 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
Misha Brukman81b06862003-05-21 18:48:06 +0000514 } else
515 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000516
Chris Lattner20b1ea02001-09-14 03:47:57 +0000517 return opCode;
518}
519
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000520MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000521CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
522 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000523{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000524 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000525 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000526 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000527}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000528
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000529// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000530// The FP value must be converted to the dest type in an FP register,
531// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000532//
533// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
534// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000535// *only* when converting to an unsigned. (Unsigned byte, short or long
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000536// don't have this problem.)
537// For unsigned int, we therefore have to generate the code sequence:
538//
539// if (V > (float) MAXINT) {
540// unsigned result = (unsigned) (V - (float) MAXINT);
541// result = result + (unsigned) MAXINT;
542// }
543// else
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000544// result = (unsigned) V;
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000545//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000546static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000547CreateCodeToConvertFloatToInt(const TargetMachine& target,
548 Value* opVal,
549 Instruction* destI,
550 std::vector<MachineInstr*>& mvec,
551 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000552{
553 // Create a temporary to represent the FP register into which the
554 // int value will placed after conversion. The type of this temporary
555 // depends on the type of FP register to use: single-prec for a 32-bit
556 // int or smaller; double-prec for a 64-bit int.
557 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000558 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000559 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
560 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000561 mcfi.addTemp(destForCast);
562
563 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000564 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
565 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000566 mvec.push_back(M);
567
568 // Create the fpreg-to-intreg copy code
569 target.getInstrInfo().
570 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000571 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000572}
573
574
Chris Lattner20b1ea02001-09-14 03:47:57 +0000575static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000576ChooseAddInstruction(const InstructionNode* instrNode)
577{
578 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
579}
580
581
Chris Lattner20b1ea02001-09-14 03:47:57 +0000582static inline MachineInstr*
583CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000584 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000585{
Misha Brukmana98cd452003-05-20 20:32:24 +0000586 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000587 .addReg(instrNode->leftChild()->getValue())
588 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000589}
590
591static inline MachineInstr*
592CreateAddConstInstruction(const InstructionNode* instrNode)
593{
594 MachineInstr* minstr = NULL;
595
596 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000597 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000598
599 // Cases worth optimizing are:
600 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
601 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
602 //
Chris Lattner9b625032002-05-06 16:15:30 +0000603 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000604 double dval = FPC->getValue();
605 if (dval == 0.0)
606 minstr = CreateMovFloatInstruction(instrNode,
607 instrNode->getInstruction()->getType());
608 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000609
610 return minstr;
611}
612
613
614static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000615ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000616{
Misha Brukmana98cd452003-05-20 20:32:24 +0000617 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000618
Misha Brukman81b06862003-05-21 18:48:06 +0000619 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000620 opCode = V9::SUB;
Misha Brukman81b06862003-05-21 18:48:06 +0000621 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000622 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000623 {
624 case Type::FloatTyID: opCode = V9::FSUBS; break;
625 case Type::DoubleTyID: opCode = V9::FSUBD; break;
626 default: assert(0 && "Invalid type for SUB instruction"); break;
627 }
628 }
629
Chris Lattner20b1ea02001-09-14 03:47:57 +0000630 return opCode;
631}
632
633
634static inline MachineInstr*
635CreateSubConstInstruction(const InstructionNode* instrNode)
636{
637 MachineInstr* minstr = NULL;
638
639 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000640 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000641
642 // Cases worth optimizing are:
643 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
644 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
645 //
Chris Lattner9b625032002-05-06 16:15:30 +0000646 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
647 double dval = FPC->getValue();
648 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000649 minstr = CreateMovFloatInstruction(instrNode,
650 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000651 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000652
653 return minstr;
654}
655
656
657static inline MachineOpCode
658ChooseFcmpInstruction(const InstructionNode* instrNode)
659{
Misha Brukmana98cd452003-05-20 20:32:24 +0000660 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000661
662 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
663 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000664 case Type::FloatTyID: opCode = V9::FCMPS; break;
665 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000666 default: assert(0 && "Invalid type for FCMP instruction"); break;
667 }
668
669 return opCode;
670}
671
672
673// Assumes that leftArg and rightArg are both cast instructions.
674//
675static inline bool
676BothFloatToDouble(const InstructionNode* instrNode)
677{
678 InstrTreeNode* leftArg = instrNode->leftChild();
679 InstrTreeNode* rightArg = instrNode->rightChild();
680 InstrTreeNode* leftArgArg = leftArg->leftChild();
681 InstrTreeNode* rightArgArg = rightArg->leftChild();
682 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
683
684 // Check if both arguments are floats cast to double
685 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000686 leftArgArg->getValue()->getType() == Type::FloatTy &&
687 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000688}
689
690
691static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000692ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000693{
Misha Brukmana98cd452003-05-20 20:32:24 +0000694 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000695
Chris Lattner0c4e8862002-09-03 01:08:28 +0000696 if (resultType->isInteger())
Misha Brukmana98cd452003-05-20 20:32:24 +0000697 opCode = V9::MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000698 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000699 switch(resultType->getPrimitiveID())
700 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000701 case Type::FloatTyID: opCode = V9::FMULS; break;
702 case Type::DoubleTyID: opCode = V9::FMULD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000703 default: assert(0 && "Invalid type for MUL instruction"); break;
704 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000705
706 return opCode;
707}
708
709
Vikram S. Adve510eec72001-11-04 21:59:14 +0000710
Chris Lattner20b1ea02001-09-14 03:47:57 +0000711static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000712CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000713 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000714{
Misha Brukmana98cd452003-05-20 20:32:24 +0000715 return BuildMI(V9::SUB, 3).addMReg(target.getRegInfo().getZeroRegNum())
716 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000717}
718
719
Vikram S. Adve242a8082002-05-19 15:25:51 +0000720// Create instruction sequence for any shift operation.
721// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
722// requires a second instruction for explicit sign-extension.
723// Note that we only have to worry about a sign-bit appearing in the
724// most significant bit of the operand after shifting (e.g., bit 32 of
725// Int or bit 16 of Short), so we do not have to worry about results
726// that are as large as a normal integer register.
727//
728static inline void
729CreateShiftInstructions(const TargetMachine& target,
730 Function* F,
731 MachineOpCode shiftOpCode,
732 Value* argVal1,
733 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000734 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000735 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000736 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000737 MachineCodeForInstruction& mcfi)
738{
739 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
740 "Large shift sizes unexpected, but can be handled below: "
741 "You need to check whether or not it fits in immed field below");
742
743 // If this is a logical left shift of a type smaller than the standard
744 // integer reg. size, we have to extend the sign-bit into upper bits
745 // of dest, so we need to put the result of the SLL into a temporary.
746 //
747 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000748 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000749 if ((shiftOpCode == V9::SLL || shiftOpCode == V9::SLLX) && opSize < 8)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000750 { // put SLL result into a temporary
751 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
752 mcfi.addTemp(shiftDest);
753 }
754
755 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000756 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
757 .addReg(shiftDest, MOTy::Def)
758 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
759 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000760 mvec.push_back(M);
761
762 if (shiftDest != destVal)
763 { // extend the sign-bit of the result into all upper bits of dest
764 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
765 target.getInstrInfo().
Vikram S. Adve94c40812002-09-27 14:33:08 +0000766 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
767 8*opSize, mvec, mcfi);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000768 }
769}
770
771
Vikram S. Adve74825322002-03-18 03:15:35 +0000772// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000773// create a cheaper instruction.
774// This returns the approximate cost of the instructions generated,
775// which is used to pick the cheapest when both operands are constant.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000776static inline unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000777CreateMulConstInstruction(const TargetMachine &target, Function* F,
778 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000779 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000780 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000781{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000782 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukmana98cd452003-05-20 20:32:24 +0000783 unsigned cost = target.getInstrInfo().minLatency(V9::MULX);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000784 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000785
786 Value* constOp = rval;
787 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000788 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000789
790 // Cases worth optimizing are:
791 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
792 // (2) Multiply by 2^x for integer types: replace with Shift
793 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000794 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000795
Misha Brukmana98cd452003-05-20 20:32:24 +0000796 if (resultType->isInteger() || isa<PointerType>(resultType)) {
797 bool isValidConst;
798 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
799 if (isValidConst) {
800 unsigned pow;
801 bool needNeg = false;
802 if (C < 0) {
803 needNeg = true;
804 C = -C;
805 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000806
Misha Brukmana98cd452003-05-20 20:32:24 +0000807 if (C == 0 || C == 1) {
808 cost = target.getInstrInfo().minLatency(V9::ADD);
809 unsigned Zero = target.getRegInfo().getZeroRegNum();
810 MachineInstr* M;
811 if (C == 0)
812 M = BuildMI(V9::ADD,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
813 else
814 M = BuildMI(V9::ADD,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
815 mvec.push_back(M);
816 }
817 else if (isPowerOf2(C, pow)) {
818 unsigned opSize = target.getTargetData().getTypeSize(resultType);
819 MachineOpCode opCode = (opSize <= 32)? V9::SLL : V9::SLLX;
820 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
821 destVal, mvec, mcfi);
822 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000823
Misha Brukmana98cd452003-05-20 20:32:24 +0000824 if (mvec.size() > 0 && needNeg)
825 { // insert <reg = SUB 0, reg> after the instr to flip the sign
826 MachineInstr* M = CreateIntNegInstruction(target, destVal);
827 mvec.push_back(M);
828 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000829 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000830 } else {
831 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
832 double dval = FPC->getValue();
833 if (fabs(dval) == 1) {
834 MachineOpCode opCode = (dval < 0)
835 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
836 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
837 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
838 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000839 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000840 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000841
Misha Brukmana98cd452003-05-20 20:32:24 +0000842 if (firstNewInstr < mvec.size()) {
843 cost = 0;
844 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
845 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
846 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000847
848 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000849}
850
851
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000852// Does not create any instructions if we cannot exploit constant to
853// create a cheaper instruction.
854//
855static inline void
856CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000857 Function* F,
858 Value* lval, Value* rval,
859 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000860 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000861 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000862{
863 Value* constOp;
864 if (isa<Constant>(lval) && isa<Constant>(rval))
Vikram S. Adved3e26482002-10-13 00:18:57 +0000865 { // both operands are constant: evaluate and "set" in dest
866 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
867 cast<Constant>(lval), cast<Constant>(rval));
868 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000869 }
870 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000871 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000872 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000873 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000874
875 // else neither is constant
876 return;
877}
878
Vikram S. Adve74825322002-03-18 03:15:35 +0000879// Return NULL if we cannot exploit constant to create a cheaper instruction
880static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000881CreateMulInstruction(const TargetMachine &target, Function* F,
882 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000883 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000884 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000885 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
886{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000887 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000888 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000889 if (mvec.size() == L) {
890 // no instructions were added so create MUL reg, reg, reg.
891 // Use FSMULD if both operands are actually floats cast to doubles.
892 // Otherwise, use the default opcode for the appropriate type.
893 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
894 ? forceMulOp
895 : ChooseMulInstructionByType(destVal->getType()));
896 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
897 .addRegDef(destVal));
898 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000899}
900
901
Vikram S. Adve510eec72001-11-04 21:59:14 +0000902// Generate a divide instruction for Div or Rem.
903// For Rem, this assumes that the operand type will be signed if the result
904// type is signed. This is correct because they must have the same sign.
905//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000906static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000907ChooseDivInstruction(TargetMachine &target,
908 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000909{
Misha Brukmana98cd452003-05-20 20:32:24 +0000910 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000911
912 const Type* resultType = instrNode->getInstruction()->getType();
913
Chris Lattner0c4e8862002-09-03 01:08:28 +0000914 if (resultType->isInteger())
Misha Brukmana98cd452003-05-20 20:32:24 +0000915 opCode = resultType->isSigned()? V9::SDIVX : V9::UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000916 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000917 switch(resultType->getPrimitiveID())
918 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000919 case Type::FloatTyID: opCode = V9::FDIVS; break;
920 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000921 default: assert(0 && "Invalid type for DIV instruction"); break;
922 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000923
924 return opCode;
925}
926
927
Chris Lattner54e898e2003-01-15 19:23:34 +0000928// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve74825322002-03-18 03:15:35 +0000929static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000930CreateDivConstInstruction(TargetMachine &target,
931 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000932 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000933{
Chris Lattner54e898e2003-01-15 19:23:34 +0000934 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000935 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000936 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000937 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000938
939 Value* DestVal = instrNode->getValue();
940 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000941
942 // Cases worth optimizing are:
943 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
944 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
945 //
946 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000947
Chris Lattner0c4e8862002-09-03 01:08:28 +0000948 if (resultType->isInteger())
Misha Brukmana98cd452003-05-20 20:32:24 +0000949 {
950 unsigned pow;
951 bool isValidConst;
952 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
953 if (isValidConst) {
954 bool needNeg = false;
955 if (C < 0) {
956 needNeg = true;
957 C = -C;
958 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000959
Misha Brukmana98cd452003-05-20 20:32:24 +0000960 if (C == 1) {
961 mvec.push_back(BuildMI(V9::ADD, 3).addReg(LHS).addMReg(ZeroReg)
962 .addRegDef(DestVal));
963 } else if (isPowerOf2(C, pow)) {
964 unsigned opCode= ((resultType->isSigned())
965 ? (resultType==Type::LongTy) ? V9::SRAX : V9::SRA
966 : (resultType==Type::LongTy) ? V9::SRLX : V9::SRL);
967 mvec.push_back(BuildMI(opCode, 3).addReg(LHS).addZImm(pow)
968 .addRegDef(DestVal));
969 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000970
Misha Brukmana98cd452003-05-20 20:32:24 +0000971 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
972 // insert <reg = SUB 0, reg> after the instr to flip the sign
973 mvec.push_back(CreateIntNegInstruction(target, DestVal));
974 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000975 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000976 } else {
977 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
978 double dval = FPC->getValue();
979 if (fabs(dval) == 1) {
980 unsigned opCode =
981 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
982 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000983
Misha Brukmana98cd452003-05-20 20:32:24 +0000984 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(DestVal));
985 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000986 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000987 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000988}
989
990
Vikram S. Adve74825322002-03-18 03:15:35 +0000991static void
992CreateCodeForVariableSizeAlloca(const TargetMachine& target,
993 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000994 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +0000995 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000996 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +0000997{
Vikram S. Adveaabb5952002-10-29 19:37:31 +0000998 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +0000999 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001000 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001001 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001002
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001003 // Enforce the alignment constraints on the stack pointer at
1004 // compile time if the total size is a known constant.
1005 if (isa<Constant>(numElementsVal))
1006 {
1007 bool isValid;
1008 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
1009 assert(isValid && "Unexpectedly large array dimension in alloca!");
1010 int64_t total = numElem * tsize;
1011 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1012 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1013 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1014 }
1015 else
1016 {
1017 // The size is not a constant. Generate code to compute it and
1018 // code to pad the size for stack alignment.
1019 // Create a Value to hold the (constant) element size
1020 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
1021
1022 // Create temporary values to hold the result of MUL, SLL, SRL
1023 // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY.
1024 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
1025 TmpInstruction* tmpSLL = new TmpInstruction(numElementsVal, tmpProd);
1026 TmpInstruction* tmpSRL = new TmpInstruction(numElementsVal, tmpSLL);
1027 mcfi.addTemp(tmpProd);
1028 mcfi.addTemp(tmpSLL);
1029 mcfi.addTemp(tmpSRL);
1030
1031 // Instruction 1: mul numElements, typeSize -> tmpProd
1032 // This will optimize the MUL as far as possible.
1033 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec,
1034 mcfi, INVALID_MACHINE_OPCODE);
1035
1036 assert(0 && "Need to insert padding instructions here!");
1037
1038 totalSizeVal = tmpProd;
1039 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001040
1041 // Get the constant offset from SP for dynamically allocated storage
1042 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001043 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001044 bool growUp;
1045 ConstantSInt* dynamicAreaOffset =
1046 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001047 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001048 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1049
Chris Lattner54e898e2003-01-15 19:23:34 +00001050 unsigned SPReg = target.getRegInfo().getStackPointer();
1051
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001052 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukmana98cd452003-05-20 20:32:24 +00001053 getMvec.push_back(BuildMI(V9::SUB, 3).addMReg(SPReg).addReg(totalSizeVal)
1054 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001055
Vikram S. Adve74825322002-03-18 03:15:35 +00001056 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukmana98cd452003-05-20 20:32:24 +00001057 getMvec.push_back(BuildMI(V9::ADD, 3).addMReg(SPReg).addReg(dynamicAreaOffset)
1058 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001059}
1060
1061
1062static void
1063CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1064 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001065 unsigned tsize,
1066 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001067 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001068{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001069 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001070 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001071 "Result value is not part of a function?");
1072 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001073 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001074
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001075 // Check if the offset would small enough to use as an immediate in
1076 // load/stores (check LDX because all load/stores have the same-size immediate
1077 // field). If not, put the variable in the dynamically sized area of the
1078 // frame.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001079 unsigned paddedSizeIgnored;
1080 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001081 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +00001082 tsize * numElements);
Misha Brukmana98cd452003-05-20 20:32:24 +00001083 if (! target.getInstrInfo().constantFitsInImmedField(V9::LDX, offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001084 CreateCodeForVariableSizeAlloca(target, result, tsize,
1085 ConstantSInt::get(Type::IntTy,numElements),
1086 getMvec);
1087 return;
1088 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001089
1090 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001091 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001092
1093 // Create a temporary Value to hold the constant offset.
1094 // This is needed because it may not fit in the immediate field.
1095 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1096
1097 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001098 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukmana98cd452003-05-20 20:32:24 +00001099 getMvec.push_back(BuildMI(V9::ADD, 3).addMReg(FPReg).addReg(offsetVal)
1100 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001101}
1102
1103
Chris Lattner20b1ea02001-09-14 03:47:57 +00001104//------------------------------------------------------------------------
1105// Function SetOperandsForMemInstr
1106//
1107// Choose addressing mode for the given load or store instruction.
1108// Use [reg+reg] if it is an indexed reference, and the index offset is
1109// not a constant or if it cannot fit in the offset field.
1110// Use [reg+offset] in all other cases.
1111//
1112// This assumes that all array refs are "lowered" to one of these forms:
1113// %x = load (subarray*) ptr, constant ; single constant offset
1114// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1115// Generally, this should happen via strength reduction + LICM.
1116// Also, strength reduction should take care of using the same register for
1117// the loop index variable and an array index, when that is profitable.
1118//------------------------------------------------------------------------
1119
1120static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001121SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001122 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001123 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001124 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001125{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001126 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001127 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001128 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001129 bool allConstantIndices;
1130 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001131
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001132 // Now create the appropriate operands for the machine instruction.
1133 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001134 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001135 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001136 MachineOperand::MachineOperandType offsetOpType =
1137 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001138
Vikram S. Adve74825322002-03-18 03:15:35 +00001139 // Check if there is an index vector and if so, compute the
1140 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001141 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +00001142 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +00001143 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001144 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001145
Vikram S. Adve242a8082002-05-19 15:25:51 +00001146 // If all indices are constant, compute the combined offset directly.
1147 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001148 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001149 // Compute the offset value using the index vector. Create a
1150 // virtual reg. for it since it may not fit in the immed field.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001151 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001152 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001153 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001154 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001155 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001156 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001157 // be an array ref, and must have been lowered to a single non-zero
1158 // offset. (An extra leading zero offset, if any, can be ignored.)
1159 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001160 //
Chris Lattner795ba6c2003-01-15 21:36:50 +00001161 bool firstIdxIsZero = IsZero(idxVec[0]);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001162 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001163 && "Array refs must be lowered before Instruction Selection");
1164
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001165 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001166
Misha Brukmanee563cb2003-05-21 17:59:06 +00001167 std::vector<MachineInstr*> mulVec;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001168 Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001169 MachineCodeForInstruction::get(memInst).addTemp(addr);
1170
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001171 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001172 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001173 const Type* vecType = (firstIdxIsZero
1174 ? GetElementPtrInst::getIndexedType(ptrType,
1175 std::vector<Value*>(1U, idxVec[0]),
1176 /*AllowCompositeLeaf*/ true)
1177 : ptrType);
1178 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +00001179 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001180 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001181
1182 // CreateMulInstruction() folds constants intelligently enough.
Vikram S. Adved3e26482002-10-13 00:18:57 +00001183 CreateMulInstruction(target, memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001184 idxVal, /* lval, not likely to be const*/
1185 eltSizeVal, /* rval, likely to be constant */
1186 addr, /* result */
Vikram S. Adved3e26482002-10-13 00:18:57 +00001187 mulVec, MachineCodeForInstruction::get(memInst),
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001188 INVALID_MACHINE_OPCODE);
1189
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001190 assert(mulVec.size() > 0 && "No multiply code created?");
Chris Lattner54e898e2003-01-15 19:23:34 +00001191 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001192
1193 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001194 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001195 }
1196 else
1197 {
1198 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1199 smallConstOffset = 0;
1200 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001201
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001202 // For STORE:
1203 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1204 // For LOAD or GET_ELEMENT_PTR,
1205 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1206 //
1207 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001208 MachineInstr *MI;
1209 if (memInst->getOpcode() == Instruction::Store) {
1210 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1211 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1212 .addReg(ptrVal).addReg(valueForRegOffset);
1213 else
1214 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1215 .addReg(ptrVal).addSImm(smallConstOffset);
1216 } else {
1217 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1218 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1219 .addRegDef(memInst);
1220 else
1221 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1222 .addRegDef(memInst);
1223 }
1224 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001225}
1226
1227
Chris Lattner20b1ea02001-09-14 03:47:57 +00001228//
1229// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001230// in place of the use(s) of that instruction in node `parent'.
1231// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001232// Also make sure to skip over a parent who:
1233// (1) is a list node in the Burg tree, or
1234// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001235//
1236static void
1237ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001238 InstrTreeNode* parent,
1239 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001240{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001241 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1242
Chris Lattner20b1ea02001-09-14 03:47:57 +00001243 Instruction* unusedOp = treeNode->getInstruction();
1244 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001245
1246 // The parent itself may be a list node, so find the real parent instruction
1247 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1248 {
1249 parent = parent->parent();
1250 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1251 }
1252 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1253
1254 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001255 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001256
1257 // The parent's mvec would be empty if it was itself forwarded.
1258 // Recursively call ForwardOperand in that case...
1259 //
1260 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001261 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001262 assert(parent->parent() != NULL &&
1263 "Parent could not have been forwarded, yet has no instructions?");
1264 ForwardOperand(treeNode, parent->parent(), operandNum);
1265 }
1266 else
1267 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001268 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001269 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001270 MachineInstr* minstr = mvec[i];
1271 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001272 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001273 const MachineOperand& mop = minstr->getOperand(i);
Chris Lattner133f0792002-10-28 04:45:29 +00001274 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
Vikram S. Adve74825322002-03-18 03:15:35 +00001275 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001276 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001277 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001278 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001279
1280 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1281 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001282 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001283 minstr->implicitRefIsDefined(i),
1284 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001285 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001286 }
1287}
1288
1289
Vikram S. Adve242a8082002-05-19 15:25:51 +00001290inline bool
1291AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001292{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001293 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1294 UI != UE; ++UI)
1295 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1296 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1297 return false;
1298 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001299}
1300
Vikram S. Advefb361122001-10-22 13:36:31 +00001301//******************* Externally Visible Functions *************************/
1302
Vikram S. Advefb361122001-10-22 13:36:31 +00001303//------------------------------------------------------------------------
1304// External Function: ThisIsAChainRule
1305//
1306// Purpose:
1307// Check if a given BURG rule is a chain rule.
1308//------------------------------------------------------------------------
1309
1310extern bool
1311ThisIsAChainRule(int eruleno)
1312{
1313 switch(eruleno)
1314 {
1315 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001316 case 123:
1317 case 124:
1318 case 125:
1319 case 126:
1320 case 127:
1321 case 128:
1322 case 129:
1323 case 130:
1324 case 131:
1325 case 132:
1326 case 133:
1327 case 155:
1328 case 221:
1329 case 222:
1330 case 241:
1331 case 242:
1332 case 243:
1333 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001334 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001335 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001336 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001337
Vikram S. Advefb361122001-10-22 13:36:31 +00001338 default:
1339 return false; break;
1340 }
1341}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001342
1343
1344//------------------------------------------------------------------------
1345// External Function: GetInstructionsByRule
1346//
1347// Purpose:
1348// Choose machine instructions for the SPARC according to the
1349// patterns chosen by the BURG-generated parser.
1350//------------------------------------------------------------------------
1351
Vikram S. Adve74825322002-03-18 03:15:35 +00001352void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001353GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001354 int ruleForNode,
1355 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001356 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001357 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001358{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001359 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001360 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001361 int nextRule;
1362 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001363 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001364 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001365 unsigned L;
Vikram S. Adve74825322002-03-18 03:15:35 +00001366
1367 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001368
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001369 // If the code for this instruction was folded into the parent (user),
1370 // then do nothing!
1371 if (subtreeRoot->isFoldedIntoParent())
1372 return;
1373
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001374 //
1375 // Let's check for chain rules outside the switch so that we don't have
1376 // to duplicate the list of chain rule production numbers here again
1377 //
1378 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001379 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001380 // Chain rules have a single nonterminal on the RHS.
1381 // Get the rule that matches the RHS non-terminal and use that instead.
1382 //
1383 assert(nts[0] && ! nts[1]
1384 && "A chain rule should have only one RHS non-terminal!");
1385 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1386 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001387 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001388 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001389 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001390 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001391 switch(ruleForNode) {
1392 case 1: // stmt: Ret
1393 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001394 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001395 // for moving return value to appropriate register.
1396 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001397 // Mark the return value register as an implicit ref of
1398 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001399 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001400 ReturnInst *returnInstr =
1401 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001402 assert(returnInstr->getOpcode() == Instruction::Ret);
1403
Chris Lattner9c461082002-02-03 07:50:56 +00001404 Instruction* returnReg = new TmpInstruction(returnInstr);
1405 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001406
Misha Brukmana98cd452003-05-20 20:32:24 +00001407 M = BuildMI(V9::JMPLRET, 3).addReg(returnReg).addSImm(8)
1408 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001409
Vikram S. Advea995e602001-10-11 04:23:19 +00001410 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001411 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001412
Vikram S. Adve74825322002-03-18 03:15:35 +00001413 mvec.push_back(M);
Misha Brukmana98cd452003-05-20 20:32:24 +00001414 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001415
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001416 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001417 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001418
1419 case 3: // stmt: Store(reg,reg)
1420 case 4: // stmt: Store(reg,ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00001421 SetOperandsForMemInstr(ChooseStoreInstruction(
1422 subtreeRoot->leftChild()->getValue()->getType()),
1423 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001424 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001425
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001426 case 5: // stmt: BrUncond
Chris Lattner54e898e2003-01-15 19:23:34 +00001427 {
1428 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001429 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001430
Chris Lattner54e898e2003-01-15 19:23:34 +00001431 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001432 mvec.push_back(BuildMI(V9::NOP, 0));
Chris Lattner54e898e2003-01-15 19:23:34 +00001433 break;
1434 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001435
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001436 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001437 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001438 // If the constant is ZERO, we can use the branch-on-integer-register
1439 // instructions and avoid the SUBcc instruction entirely.
1440 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001441 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001442 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1443 assert(constNode &&
1444 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001445 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001446 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001447
Chris Lattner0c4e8862002-09-03 01:08:28 +00001448 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001449 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001450 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1451 && isValidConst)
1452 {
1453 // That constant is a zero after all...
1454 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001455 // Mark the setCC node so that no code is generated for it.
1456 InstructionNode* setCCNode = (InstructionNode*)
1457 subtreeRoot->leftChild();
1458 assert(setCCNode->getOpLabel() == SetCCOp);
1459 setCCNode->markFoldedIntoParent();
1460
1461 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1462
Chris Lattner54e898e2003-01-15 19:23:34 +00001463 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1464 .addReg(setCCNode->leftChild()->getValue())
1465 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001466 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001467
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001468 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001469 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001470
1471 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001472 mvec.push_back(BuildMI(V9::BA, 1)
1473 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001474
1475 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001476 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001477 break;
1478 }
1479 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001480 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001481
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001482 case 6: // stmt: BrCond(setCC)
1483 { // bool => boolean was computed with SetCC.
1484 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001485 // If it is an integer CC, we also need to find the unique
1486 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001487 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001488 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001489 bool isFPBranch;
Chris Lattner54e898e2003-01-15 19:23:34 +00001490 unsigned Opcode = ChooseBccInstruction(subtreeRoot, isFPBranch);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001491 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1492 brInst->getParent()->getParent(),
1493 isFPBranch? Type::FloatTy : Type::IntTy);
Chris Lattner54e898e2003-01-15 19:23:34 +00001494 M = BuildMI(Opcode, 2).addCCReg(ccValue)
1495 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001496 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001497
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001498 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001499 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001500
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001501 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001502 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001503
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001504 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001505 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001506 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001507 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001508
1509 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001510 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001511 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001512 Constant* constVal =
1513 cast<Constant>(subtreeRoot->leftChild()->getValue());
1514 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001515
Misha Brukmana98cd452003-05-20 20:32:24 +00001516 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001517 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001518 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001519
1520 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001521 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001522 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001523 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001524
1525 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001526 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001527 // Just use the branch-on-integer-register instruction!
1528 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001529 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001530 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001531 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001532 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001533
1534 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001535 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001536
1537 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001538 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001539
1540 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001541 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001542 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001543 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001544
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001545 case 9: // stmt: Switch(reg)
1546 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001547 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001548
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001549 case 10: // reg: VRegList(reg, reg)
1550 assert(0 && "VRegList should never be the topmost non-chain rule");
1551 break;
1552
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001553 case 21: // bool: Not(bool,reg): Both these are implemented as:
1554 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1555 { // First find the unary operand. It may be left or right, usually right.
1556 Value* notArg = BinaryOperator::getNotArgument(
1557 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001558 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukmana98cd452003-05-20 20:32:24 +00001559 mvec.push_back(BuildMI(V9::XNOR, 3).addReg(notArg).addMReg(ZeroReg)
Chris Lattner00dca912003-01-15 17:47:49 +00001560 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001561 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001562 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001563
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001564 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001565 {
1566 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001567 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001568 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001569 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001570 }
1571
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001572 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001573 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001574 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001575 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001576 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001577 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001578 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001579 //======================================================================
1580 // Rules for integer conversions:
1581 //
1582 //--------
1583 // From ISO 1998 C++ Standard, Sec. 4.7:
1584 //
1585 // 2. If the destination type is unsigned, the resulting value is
1586 // the least unsigned integer congruent to the source integer
1587 // (modulo 2n where n is the number of bits used to represent the
1588 // unsigned type). [Note: In a two s complement representation,
1589 // this conversion is conceptual and there is no change in the
1590 // bit pattern (if there is no truncation). ]
1591 //
1592 // 3. If the destination type is signed, the value is unchanged if
1593 // it can be represented in the destination type (and bitfield width);
1594 // otherwise, the value is implementation-defined.
1595 //--------
1596 //
1597 // Since we assume 2s complement representations, this implies:
1598 //
1599 // -- if operand is smaller than destination, zero-extend or sign-extend
1600 // according to the signedness of the *operand*: source decides.
1601 // ==> we have to do nothing here!
1602 //
1603 // -- if operand is same size as or larger than destination, and the
1604 // destination is *unsigned*, zero-extend the operand: dest. decides
1605 //
1606 // -- if operand is same size as or larger than destination, and the
1607 // destination is *signed*, the choice is implementation defined:
1608 // we sign-extend the operand: i.e., again dest. decides.
1609 // Note: this matches both Sun's cc and gcc3.2.
1610 //======================================================================
1611
Vikram S. Adve242a8082002-05-19 15:25:51 +00001612 Instruction* destI = subtreeRoot->getInstruction();
1613 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001614 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001615 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001616 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001617 unsigned opSize = target.getTargetData().getTypeSize(opType);
1618 unsigned destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001619 if (opSize >= destSize)
1620 { // Operand is same size as or larger than dest:
1621 // zero- or sign-extend, according to the signeddness of
1622 // the destination (see above).
1623 if (destI->getType()->isSigned())
1624 target.getInstrInfo().CreateSignExtensionInstructions(target,
1625 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1626 mvec, MachineCodeForInstruction::get(destI));
1627 else
1628 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1629 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1630 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001631 }
1632 else
1633 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001634 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001635 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001636 {
1637 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1638 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001639 if (destI->getType()->isUnsigned())
1640 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001641 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001642 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001643 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1644
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001645 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001646 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001647
1648 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001649 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001650 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001651 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001652 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001653 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001654 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001655 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001656 {
1657 Instruction* destI = subtreeRoot->getInstruction();
1658 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1659 MachineCodeForInstruction::get(destI));
1660 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001661 else
1662 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001663 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001664 }
1665
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001666 case 31: // reg: ToFloatTy(reg):
1667 case 32: // reg: ToDoubleTy(reg):
1668 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001669
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001670 // If this instruction has a parent (a user) in the tree
1671 // and the user is translated as an FsMULd instruction,
1672 // then the cast is unnecessary. So check that first.
1673 // In the future, we'll want to do the same for the FdMULq instruction,
1674 // so do the check here instead of only for ToFloatTy(reg).
1675 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001676 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001678 const MachineCodeForInstruction& mcfi =
1679 MachineCodeForInstruction::get(
1680 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001681 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001682 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001683 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001684
1685 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001686 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001687 Value* leftVal = subtreeRoot->leftChild()->getValue();
1688 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001689 MachineOpCode opCode=ChooseConvertToFloatInstr(
1690 subtreeRoot->getOpLabel(), opType);
Misha Brukmana98cd452003-05-20 20:32:24 +00001691 if (opCode == V9::INVALID_OPCODE) // no conversion needed
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001692 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001693 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001694 }
1695 else
1696 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001697 // If the source operand is a non-FP type it must be
1698 // first copied from int to float register via memory!
1699 Instruction *dest = subtreeRoot->getInstruction();
1700 Value* srcForCast;
1701 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001702 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001703 {
1704 // Create a temporary to represent the FP register
1705 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001706 // The type of this temporary will determine the FP
1707 // register used: single-prec for a 32-bit int or smaller,
1708 // double-prec for a 64-bit int.
1709 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001710 uint64_t srcSize =
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001711 target.getTargetData().getTypeSize(leftVal->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001712 Type* tmpTypeToUse =
1713 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1714 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001715 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001716 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001717 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001718
Vikram S. Adve242a8082002-05-19 15:25:51 +00001719 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001720 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001721 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001722 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001723 }
1724 else
1725 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001726
Chris Lattner54e898e2003-01-15 19:23:34 +00001727 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
Vikram S. Adve74825322002-03-18 03:15:35 +00001728 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001729 }
1730 }
1731 break;
1732
1733 case 19: // reg: ToArrayTy(reg):
1734 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001735 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001736 break;
1737
1738 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001739 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001740 M = CreateAddConstInstruction(subtreeRoot);
1741 if (M != NULL)
1742 {
1743 mvec.push_back(M);
1744 break;
1745 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001746 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001747
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001748 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001749 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001750 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001751 break;
1752
1753 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001754 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001755 M = CreateSubConstInstruction(subtreeRoot);
1756 if (M != NULL)
1757 {
1758 mvec.push_back(M);
1759 break;
1760 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001761 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001762
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001763 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001764 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001765 Add3OperandInstr(ChooseSubInstructionByType(
1766 subtreeRoot->getInstruction()->getType()),
1767 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001768 break;
1769
1770 case 135: // reg: Mul(todouble, todouble)
1771 checkCast = true;
1772 // FALL THROUGH
1773
1774 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001775 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001776 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001777 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00001778 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00001779 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001780 Instruction* mulInstr = subtreeRoot->getInstruction();
1781 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001782 subtreeRoot->leftChild()->getValue(),
1783 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001784 mulInstr, mvec,
1785 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001786 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001787 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001788 case 335: // reg: Mul(todouble, todoubleConst)
1789 checkCast = true;
1790 // FALL THROUGH
1791
1792 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001793 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001794 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001795 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00001796 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00001797 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001798 Instruction* mulInstr = subtreeRoot->getInstruction();
1799 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001800 subtreeRoot->leftChild()->getValue(),
1801 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001802 mulInstr, mvec,
1803 MachineCodeForInstruction::get(mulInstr),
1804 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001805 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001806 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001807 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001808 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001809 L = mvec.size();
1810 CreateDivConstInstruction(target, subtreeRoot, mvec);
1811 if (mvec.size() > L)
1812 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001813 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001814
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001815 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001816 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001817 Add3OperandInstr(ChooseDivInstruction(target, subtreeRoot),
1818 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001819 break;
1820
1821 case 37: // reg: Rem(reg, reg)
1822 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001823 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001824 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001825 Instruction* remInstr = subtreeRoot->getInstruction();
1826
Chris Lattner9c461082002-02-03 07:50:56 +00001827 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001828 subtreeRoot->leftChild()->getValue(),
1829 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001830 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001831 quot,
1832 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001833 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001834
Chris Lattner54e898e2003-01-15 19:23:34 +00001835 M = BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
1836 .addReg(subtreeRoot->leftChild()->getValue())
1837 .addReg(subtreeRoot->rightChild()->getValue())
1838 .addRegDef(quot);
Vikram S. Adve74825322002-03-18 03:15:35 +00001839 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001840
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001841 unsigned MulOpcode =
1842 ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType());
1843 Value *MulRHS = subtreeRoot->rightChild()->getValue();
1844 M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod,
1845 MOTy::Def);
Vikram S. Adve74825322002-03-18 03:15:35 +00001846 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001847
Chris Lattner54e898e2003-01-15 19:23:34 +00001848 unsigned Opcode = ChooseSubInstructionByType(
1849 subtreeRoot->getInstruction()->getType());
1850 M = BuildMI(Opcode, 3).addReg(subtreeRoot->leftChild()->getValue())
1851 .addReg(prod).addRegDef(subtreeRoot->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001852 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001853 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001854 }
1855
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001856 case 38: // bool: And(bool, bool)
1857 case 238: // bool: And(bool, boolconst)
1858 case 338: // reg : BAnd(reg, reg)
1859 case 538: // reg : BAnd(reg, Constant)
Misha Brukmana98cd452003-05-20 20:32:24 +00001860 Add3OperandInstr(V9::AND, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001861 break;
1862
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001863 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001864 case 438: // bool: BAnd(bool, bnot)
1865 { // Use the argument of NOT as the second argument!
1866 // Mark the NOT node so that no code is generated for it.
1867 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1868 Value* notArg = BinaryOperator::getNotArgument(
1869 cast<BinaryOperator>(notNode->getInstruction()));
1870 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001871 Value *LHS = subtreeRoot->leftChild()->getValue();
1872 Value *Dest = subtreeRoot->getValue();
Misha Brukmana98cd452003-05-20 20:32:24 +00001873 mvec.push_back(BuildMI(V9::ANDN, 3).addReg(LHS).addReg(notArg)
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001874 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001875 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001876 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001877
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001878 case 39: // bool: Or(bool, bool)
1879 case 239: // bool: Or(bool, boolconst)
1880 case 339: // reg : BOr(reg, reg)
1881 case 539: // reg : BOr(reg, Constant)
Misha Brukmana98cd452003-05-20 20:32:24 +00001882 Add3OperandInstr(V9::OR, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001883 break;
1884
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001885 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001886 case 439: // bool: BOr(bool, bnot)
1887 { // Use the argument of NOT as the second argument!
1888 // Mark the NOT node so that no code is generated for it.
1889 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1890 Value* notArg = BinaryOperator::getNotArgument(
1891 cast<BinaryOperator>(notNode->getInstruction()));
1892 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001893 Value *LHS = subtreeRoot->leftChild()->getValue();
1894 Value *Dest = subtreeRoot->getValue();
Misha Brukmana98cd452003-05-20 20:32:24 +00001895 mvec.push_back(BuildMI(V9::ORN, 3).addReg(LHS).addReg(notArg)
1896 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001897 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001898 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001899
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001900 case 40: // bool: Xor(bool, bool)
1901 case 240: // bool: Xor(bool, boolconst)
1902 case 340: // reg : BXor(reg, reg)
1903 case 540: // reg : BXor(reg, Constant)
Misha Brukmana98cd452003-05-20 20:32:24 +00001904 Add3OperandInstr(V9::XOR, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001905 break;
1906
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001907 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001908 case 440: // bool: BXor(bool, bnot)
1909 { // Use the argument of NOT as the second argument!
1910 // Mark the NOT node so that no code is generated for it.
1911 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1912 Value* notArg = BinaryOperator::getNotArgument(
1913 cast<BinaryOperator>(notNode->getInstruction()));
1914 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001915 Value *LHS = subtreeRoot->leftChild()->getValue();
1916 Value *Dest = subtreeRoot->getValue();
Misha Brukmana98cd452003-05-20 20:32:24 +00001917 mvec.push_back(BuildMI(V9::XNOR, 3).addReg(LHS).addReg(notArg)
1918 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001919 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001920 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001921
1922 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001923 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001924 // If the SetCC was folded into the user (parent), it will be
1925 // caught above. All other cases are the same as case 42,
1926 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001927 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001928 case 42: // bool: SetCC(reg, reg):
1929 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001930 // This generates a SUBCC instruction, putting the difference in
1931 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001932 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001933 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001934 // than a branch instruction, or if it is used outside the current
1935 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001936 // computed and stored in the result register. Otherwise, discard
1937 // the difference (by using %g0) and keep only the condition code.
1938 //
1939 // To compute the boolean result in a register we use a conditional
1940 // move, unless the result of the SUBCC instruction can be used as
1941 // the bool! This assumes that zero is FALSE and any non-zero
1942 // integer is TRUE.
1943 //
1944 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1945 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001946
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001947 bool keepBoolVal = parentNode == NULL ||
1948 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001949 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001950 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1951 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1952
1953 bool mustClearReg;
1954 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001955 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001956
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001957 // Mark the 4th operand as being a CC register, and as a def
1958 // A TmpInstruction is created to represent the CC "result".
1959 // Unlike other instances of TmpInstruction, this one is used
1960 // by machine code of multiple LLVM instructions, viz.,
1961 // the SetCC and the branch. Make sure to get the same one!
1962 // Note that we do this even for FP CC registers even though they
1963 // are explicit operands, because the type of the operand
1964 // needs to be a floating point condition code, not an integer
1965 // condition code. Think of this as casting the bool result to
1966 // a FP condition code register.
1967 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001968 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001969 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001970
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001971 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1972 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001973 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001974 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001975
1976 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001977 {
1978 // Integer condition: dest. should be %g0 or an integer register.
1979 // If result must be saved but condition is not SetEQ then we need
1980 // a separate instruction to compute the bool result, so discard
1981 // result of SUBcc instruction anyway.
1982 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001983 if (keepSubVal) {
Misha Brukmana98cd452003-05-20 20:32:24 +00001984 M = BuildMI(V9::SUBcc, 4)
1985 .addReg(subtreeRoot->leftChild()->getValue())
1986 .addReg(subtreeRoot->rightChild()->getValue())
1987 .addRegDef(subtreeRoot->getValue())
1988 .addCCReg(tmpForCC, MOTy::Def);
Chris Lattner54e898e2003-01-15 19:23:34 +00001989 } else {
Misha Brukmana98cd452003-05-20 20:32:24 +00001990 M = BuildMI(V9::SUBcc, 4)
1991 .addReg(subtreeRoot->leftChild()->getValue())
1992 .addReg(subtreeRoot->rightChild()->getValue())
1993 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def)
1994 .addCCReg(tmpForCC, MOTy::Def);
Chris Lattner54e898e2003-01-15 19:23:34 +00001995 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001996 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001997
1998 if (computeBoolVal)
1999 { // recompute bool using the integer condition codes
2000 movOpCode =
2001 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
2002 }
2003 }
2004 else
2005 {
2006 // FP condition: dest of FCMP should be some FCCn register
Chris Lattner54e898e2003-01-15 19:23:34 +00002007 M = BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2008 .addCCReg(tmpForCC, MOTy::Def)
2009 .addReg(subtreeRoot->leftChild()->getValue())
2010 .addRegDef(subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00002011 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002012
2013 if (computeBoolVal)
2014 {// recompute bool using the FP condition codes
2015 mustClearReg = true;
2016 valueToMove = 1;
2017 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
2018 }
2019 }
2020
2021 if (computeBoolVal)
2022 {
2023 if (mustClearReg)
2024 {// Unconditionally set register to 0
Misha Brukmana98cd452003-05-20 20:32:24 +00002025 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00002026 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002027 }
2028
2029 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00002030 // Mark the register as a use (as well as a def) because the old
2031 // value should be retained if the condition is false.
Chris Lattner54e898e2003-01-15 19:23:34 +00002032 M = BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(valueToMove)
2033 .addReg(setCCInstr, MOTy::UseAndDef);
Vikram S. Adve74825322002-03-18 03:15:35 +00002034 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002035 }
2036 break;
2037 }
2038
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002039 case 51: // reg: Load(reg)
2040 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002041 SetOperandsForMemInstr(ChooseLoadInstruction(
2042 subtreeRoot->getValue()->getType()),
2043 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002044 break;
2045
2046 case 55: // reg: GetElemPtr(reg)
2047 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002048 // If the GetElemPtr was folded into the user (parent), it will be
2049 // caught above. For other cases, we have to compute the address.
Misha Brukmana98cd452003-05-20 20:32:24 +00002050 SetOperandsForMemInstr(V9::ADD, mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002051 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002052
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002053 case 57: // reg: Alloca: Implement as 1 instruction:
2054 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002055 AllocationInst* instr =
2056 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002057 unsigned tsize =
2058 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002059 assert(tsize != 0);
2060 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002061 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002062 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002063
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002064 case 58: // reg: Alloca(reg): Implement as 3 instructions:
2065 // mul num, typeSz -> tmp
2066 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002067 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002068 AllocationInst* instr =
2069 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002070 const Type* eltType = instr->getAllocatedType();
2071
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002072 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002073 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002074 Value* numElementsVal = NULL;
2075 bool isArray = instr->isArrayAllocation();
2076
2077 if (!isArray ||
2078 isa<Constant>(numElementsVal = instr->getArraySize()))
2079 { // total size is constant: generate code for fixed-size alloca
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002080 unsigned numElements = isArray?
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002081 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2082 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2083 numElements, mvec);
2084 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002085 else // total size is not constant.
2086 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002087 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002088 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002089 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002090
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002091 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002092 { // Generate a direct (CALL) or indirect (JMPL) call.
2093 // Mark the return-address register, the indirection
2094 // register (for indirect calls), the operands of the Call,
2095 // and the return value (if any) as implicit operands
2096 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002097 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002098 // If this is a varargs function, floating point arguments
2099 // have to passed in integer registers so insert
2100 // copy-float-to-int instructions for each float operand.
2101 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002102 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002103 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002104
2105 // Create hidden virtual register for return address with type void*
Vikram S. Adve242a8082002-05-19 15:25:51 +00002106 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002107 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002108 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002109
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002110 // Generate the machine instruction and its operands.
2111 // Use CALL for direct function calls; this optimistically assumes
2112 // the PC-relative address fits in the CALL address field (22 bits).
2113 // Use JMPL for indirect calls.
2114 //
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002115 if (isa<Function>(callee)) // direct function call
Misha Brukmana98cd452003-05-20 20:32:24 +00002116 M = BuildMI(V9::CALL, 1).addPCDisp(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002117 else // indirect function call
Misha Brukmana98cd452003-05-20 20:32:24 +00002118 M = BuildMI(V9::JMPLCALL, 3).addReg(callee).addSImm((int64_t)0)
2119 .addRegDef(retAddrReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00002120 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002121
Vikram S. Adve242a8082002-05-19 15:25:51 +00002122 const FunctionType* funcType =
2123 cast<FunctionType>(cast<PointerType>(callee->getType())
2124 ->getElementType());
2125 bool isVarArgs = funcType->isVarArg();
2126 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002127
Vikram S. Adveaabb5952002-10-29 19:37:31 +00002128 // Use a descriptor to pass information about call arguments
2129 // to the register allocator. This descriptor will be "owned"
2130 // and freed automatically when the MachineCodeForInstruction
2131 // object for the callInstr goes away.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002132 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2133 retAddrReg, isVarArgs, noPrototype);
Vikram S. Advea995e602001-10-11 04:23:19 +00002134
Vikram S. Adve242a8082002-05-19 15:25:51 +00002135 assert(callInstr->getOperand(0) == callee
2136 && "This is assumed in the loop below!");
2137
2138 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2139 {
2140 Value* argVal = callInstr->getOperand(i);
2141 Instruction* intArgReg = NULL;
2142
2143 // Check for FP arguments to varargs functions.
2144 // Any such argument in the first $K$ args must be passed in an
2145 // integer register, where K = #integer argument registers.
2146 if (isVarArgs && argVal->getType()->isFloatingPoint())
2147 {
2148 // If it is a function with no prototype, pass value
2149 // as an FP value as well as a varargs value
2150 if (noPrototype)
2151 argDesc->getArgInfo(i-1).setUseFPArgReg();
2152
2153 // If this arg. is in the first $K$ regs, add a copy
2154 // float-to-int instruction to pass the value as an integer.
Vikram S. Adved3e26482002-10-13 00:18:57 +00002155 if (i <= target.getRegInfo().GetNumOfIntArgRegs())
Vikram S. Adve242a8082002-05-19 15:25:51 +00002156 {
2157 MachineCodeForInstruction &destMCFI =
2158 MachineCodeForInstruction::get(callInstr);
2159 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2160 destMCFI.addTemp(intArgReg);
2161
Misha Brukmanee563cb2003-05-21 17:59:06 +00002162 std::vector<MachineInstr*> copyMvec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002163 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2164 callInstr->getParent()->getParent(),
2165 argVal, (TmpInstruction*) intArgReg,
2166 copyMvec, destMCFI);
2167 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2168
2169 argDesc->getArgInfo(i-1).setUseIntArgReg();
2170 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2171 }
2172 else
2173 // Cannot fit in first $K$ regs so pass the arg on the stack
2174 argDesc->getArgInfo(i-1).setUseStackSlot();
2175 }
2176
2177 if (intArgReg)
2178 mvec.back()->addImplicitRef(intArgReg);
2179
2180 mvec.back()->addImplicitRef(argVal);
2181 }
2182
2183 // Add the return value as an implicit ref. The call operands
2184 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002185 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002186 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002187
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002188 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002189 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002190 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002191
Vikram S. Adve74825322002-03-18 03:15:35 +00002192 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00002193 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002194 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002195 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002196
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002197 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002198 {
2199 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2200 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2201 Instruction* shlInstr = subtreeRoot->getInstruction();
2202
2203 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002204 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2205 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002206
2207 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Misha Brukmana98cd452003-05-20 20:32:24 +00002208 (opType == Type::LongTy)? V9::SLLX : V9::SLL,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002209 argVal1, argVal2, 0, shlInstr, mvec,
2210 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002211 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002212 }
2213
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002214 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002215 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002216 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2217 "Shr unsupported for other types");
Chris Lattner54e898e2003-01-15 19:23:34 +00002218 Add3OperandInstr(opType->isSigned()
Misha Brukmana98cd452003-05-20 20:32:24 +00002219 ? (opType == Type::LongTy ? V9::SRAX : V9::SRA)
2220 : (opType == Type::LongTy ? V9::SRLX : V9::SRL),
Chris Lattner54e898e2003-01-15 19:23:34 +00002221 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002222 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002223 }
2224
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002225 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002226 break; // don't forward the value
2227
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002228 case 71: // reg: VReg
2229 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002230 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002231
2232 default:
2233 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002234 break;
2235 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002236 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002237
Chris Lattner20b1ea02001-09-14 03:47:57 +00002238 if (forwardOperandNum >= 0)
2239 { // We did not generate a machine instruction but need to use operand.
2240 // If user is in the same tree, replace Value in its machine operand.
2241 // If not, insert a copy instruction which should get coalesced away
2242 // by register allocation.
2243 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002244 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002245 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002246 {
Misha Brukmanee563cb2003-05-21 17:59:06 +00002247 std::vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002248 Instruction* instr = subtreeRoot->getInstruction();
2249 target.getInstrInfo().
2250 CreateCopyInstructionsByType(target,
2251 instr->getParent()->getParent(),
2252 instr->getOperand(forwardOperandNum),
2253 instr, minstrVec,
2254 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002255 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002256 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002257 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002258 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002259
2260 if (maskUnsignedResult)
2261 { // If result is unsigned and smaller than int reg size,
2262 // we need to clear high bits of result value.
2263 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2264 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002265 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002266 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002267 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002268 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002269 { // Mask high bits. Use a TmpInstruction to represent the
2270 // intermediate result before masking. Since those instructions
2271 // have already been generated, go back and substitute tmpI
2272 // for dest in the result position of each one of them.
2273 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2274 NULL, "maskHi");
2275 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2276
2277 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2278 mvec[i]->substituteValue(dest, tmpI);
2279
Misha Brukmana98cd452003-05-20 20:32:24 +00002280 M = BuildMI(V9::SRL, 3).addReg(tmpI).addZImm(8*(4-destSize))
2281 .addReg(dest, MOTy::Def);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002282 mvec.push_back(M);
2283 }
Chris Lattner7a5adc32003-04-26 19:44:35 +00002284 else if (destSize < 8)
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002285 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002286 }
2287 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002288}