blob: 9c4fcd610b72ab659cb5d1ddf3c385369a57fb5a [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner035dfbe2002-08-09 20:08:06 +00009//
10// BURS instruction selection for SPARC V9 architecture.
11//
12//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +000013
14#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +000015#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +000016#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000017#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000018#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000019#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000020#include "llvm/CodeGen/InstrForest.h"
21#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000022#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerea45d7b2002-12-28 20:19:44 +000023#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner9c461082002-02-03 07:50:56 +000024#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000025#include "llvm/DerivedTypes.h"
Misha Brukmanb8db66e2003-08-07 15:43:46 +000026#include "llvm/Instructions.h"
27#include "llvm/Module.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000028#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000029#include "llvm/ConstantHandling.h"
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +000030#include "llvm/Intrinsics.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000031#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000032#include <math.h>
Vikram S. Adve951df2b2003-07-10 20:07:54 +000033#include <algorithm>
Chris Lattner20b1ea02001-09-14 03:47:57 +000034
Chris Lattner54e898e2003-01-15 19:23:34 +000035static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +000036 std::vector<MachineInstr*>& mvec) {
Chris Lattner54e898e2003-01-15 19:23:34 +000037 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
38 .addReg(Node->rightChild()->getValue())
39 .addRegDef(Node->getValue()));
40}
41
42
43
Chris Lattner795ba6c2003-01-15 21:36:50 +000044//---------------------------------------------------------------------------
45// Function: GetMemInstArgs
46//
47// Purpose:
48// Get the pointer value and the index vector for a memory operation
49// (GetElementPtr, Load, or Store). If all indices of the given memory
50// operation are constant, fold in constant indices in a chain of
51// preceding GetElementPtr instructions (if any), and return the
52// pointer value of the first instruction in the chain.
53// All folded instructions are marked so no code is generated for them.
54//
55// Return values:
56// Returns the pointer Value to use.
57// Returns the resulting IndexVector in idxVec.
58// Returns true/false in allConstantIndices if all indices are/aren't const.
59//---------------------------------------------------------------------------
60
61
62//---------------------------------------------------------------------------
63// Function: FoldGetElemChain
64//
65// Purpose:
66// Fold a chain of GetElementPtr instructions containing only
67// constant offsets into an equivalent (Pointer, IndexVector) pair.
68// Returns the pointer Value, and stores the resulting IndexVector
69// in argument chainIdxVec. This is a helper function for
70// FoldConstantIndices that does the actual folding.
71//---------------------------------------------------------------------------
72
73
74// Check for a constant 0.
75inline bool
76IsZero(Value* idx)
77{
78 return (idx == ConstantSInt::getNullValue(idx->getType()));
79}
80
81static Value*
Misha Brukmanee563cb2003-05-21 17:59:06 +000082FoldGetElemChain(InstrTreeNode* ptrNode, std::vector<Value*>& chainIdxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +000083 bool lastInstHasLeadingNonZero)
84{
85 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
86 GetElementPtrInst* gepInst =
87 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
88
89 // ptr value is not computed in this tree or ptr value does not come from GEP
90 // instruction
91 if (gepInst == NULL)
92 return NULL;
93
94 // Return NULL if we don't fold any instructions in.
95 Value* ptrVal = NULL;
96
97 // Now chase the chain of getElementInstr instructions, if any.
98 // Check for any non-constant indices and stop there.
99 // Also, stop if the first index of child is a non-zero array index
100 // and the last index of the current node is a non-array index:
101 // in that case, a non-array declared type is being accessed as an array
102 // which is not type-safe, but could be legal.
103 //
104 InstructionNode* ptrChild = gepNode;
105 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
106 ptrChild->getOpLabel() == GetElemPtrIdx))
Misha Brukman81b06862003-05-21 18:48:06 +0000107 {
108 // Child is a GetElemPtr instruction
109 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
110 User::op_iterator OI, firstIdx = gepInst->idx_begin();
111 User::op_iterator lastIdx = gepInst->idx_end();
112 bool allConstantOffsets = true;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000113
Misha Brukman81b06862003-05-21 18:48:06 +0000114 // The first index of every GEP must be an array index.
115 assert((*firstIdx)->getType() == Type::LongTy &&
116 "INTERNAL ERROR: Structure index for a pointer type!");
Chris Lattner795ba6c2003-01-15 21:36:50 +0000117
Misha Brukman81b06862003-05-21 18:48:06 +0000118 // If the last instruction had a leading non-zero index, check if the
119 // current one references a sequential (i.e., indexable) type.
120 // If not, the code is not type-safe and we would create an illegal GEP
121 // by folding them, so don't fold any more instructions.
122 //
123 if (lastInstHasLeadingNonZero)
124 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
125 break; // cannot fold in any preceding getElementPtr instrs.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000126
Misha Brukman81b06862003-05-21 18:48:06 +0000127 // Check that all offsets are constant for this instruction
128 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
129 allConstantOffsets = isa<ConstantInt>(*OI);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000130
Misha Brukman81b06862003-05-21 18:48:06 +0000131 if (allConstantOffsets) {
132 // Get pointer value out of ptrChild.
133 ptrVal = gepInst->getPointerOperand();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000134
Misha Brukman81b06862003-05-21 18:48:06 +0000135 // Insert its index vector at the start, skipping any leading [0]
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000136 // Remember the old size to check if anything was inserted.
137 unsigned oldSize = chainIdxVec.size();
138 int firstIsZero = IsZero(*firstIdx);
139 chainIdxVec.insert(chainIdxVec.begin(), firstIdx + firstIsZero, lastIdx);
140
141 // Remember if it has leading zero index: it will be discarded later.
142 if (oldSize < chainIdxVec.size())
143 lastInstHasLeadingNonZero = !firstIsZero;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000144
Misha Brukman81b06862003-05-21 18:48:06 +0000145 // Mark the folded node so no code is generated for it.
146 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000147
Misha Brukman81b06862003-05-21 18:48:06 +0000148 // Get the previous GEP instruction and continue trying to fold
149 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
150 } else // cannot fold this getElementPtr instr. or any preceding ones
151 break;
152 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000153
154 // If the first getElementPtr instruction had a leading [0], add it back.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000155 // Note that this instruction is the *last* one that was successfully
156 // folded *and* contributed any indices, in the loop above.
157 //
Chris Lattner795ba6c2003-01-15 21:36:50 +0000158 if (ptrVal && ! lastInstHasLeadingNonZero)
159 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
160
161 return ptrVal;
162}
163
164
165//---------------------------------------------------------------------------
166// Function: GetGEPInstArgs
167//
168// Purpose:
169// Helper function for GetMemInstArgs that handles the final getElementPtr
170// instruction used by (or same as) the memory operation.
171// Extracts the indices of the current instruction and tries to fold in
172// preceding ones if all indices of the current one are constant.
173//---------------------------------------------------------------------------
174
175static Value *
176GetGEPInstArgs(InstructionNode* gepNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000177 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000178 bool& allConstantIndices)
179{
180 allConstantIndices = true;
181 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
182
183 // Default pointer is the one from the current instruction.
184 Value* ptrVal = gepI->getPointerOperand();
185 InstrTreeNode* ptrChild = gepNode->leftChild();
186
Misha Brukman452db672003-09-23 17:28:11 +0000187 // Extract the index vector of the GEP instruction.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000188 // If all indices are constant and first index is zero, try to fold
189 // in preceding GEPs with all constant indices.
190 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
191 allConstantIndices && OI != OE; ++OI)
192 if (! isa<Constant>(*OI))
193 allConstantIndices = false; // note: this also terminates loop!
194
195 // If we have only constant indices, fold chains of constant indices
196 // in this and any preceding GetElemPtr instructions.
197 bool foldedGEPs = false;
198 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
199 if (allConstantIndices)
Misha Brukman81b06862003-05-21 18:48:06 +0000200 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) {
201 ptrVal = newPtr;
202 foldedGEPs = true;
203 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000204
205 // Append the index vector of the current instruction.
206 // Skip the leading [0] index if preceding GEPs were folded into this.
207 idxVec.insert(idxVec.end(),
208 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
209 gepI->idx_end());
210
211 return ptrVal;
212}
213
214//---------------------------------------------------------------------------
215// Function: GetMemInstArgs
216//
217// Purpose:
218// Get the pointer value and the index vector for a memory operation
219// (GetElementPtr, Load, or Store). If all indices of the given memory
220// operation are constant, fold in constant indices in a chain of
221// preceding GetElementPtr instructions (if any), and return the
222// pointer value of the first instruction in the chain.
223// All folded instructions are marked so no code is generated for them.
224//
225// Return values:
226// Returns the pointer Value to use.
227// Returns the resulting IndexVector in idxVec.
228// Returns true/false in allConstantIndices if all indices are/aren't const.
229//---------------------------------------------------------------------------
230
231static Value*
232GetMemInstArgs(InstructionNode* memInstrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000233 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000234 bool& allConstantIndices)
235{
236 allConstantIndices = false;
237 Instruction* memInst = memInstrNode->getInstruction();
238 assert(idxVec.size() == 0 && "Need empty vector to return indices");
239
240 // If there is a GetElemPtr instruction to fold in to this instr,
241 // it must be in the left child for Load and GetElemPtr, and in the
242 // right child for Store instructions.
243 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
244 ? memInstrNode->rightChild()
245 : memInstrNode->leftChild());
246
247 // Default pointer is the one from the current instruction.
248 Value* ptrVal = ptrChild->getValue();
249
250 // Find the "last" GetElemPtr instruction: this one or the immediate child.
251 // There will be none if this is a load or a store from a scalar pointer.
252 InstructionNode* gepNode = NULL;
253 if (isa<GetElementPtrInst>(memInst))
254 gepNode = memInstrNode;
Misha Brukman81b06862003-05-21 18:48:06 +0000255 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) {
256 // Child of load/store is a GEP and memInst is its only use.
257 // Use its indices and mark it as folded.
258 gepNode = cast<InstructionNode>(ptrChild);
259 gepNode->markFoldedIntoParent();
260 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000261
262 // If there are no indices, return the current pointer.
263 // Else extract the pointer from the GEP and fold the indices.
264 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
265 : ptrVal;
266}
267
Chris Lattner54e898e2003-01-15 19:23:34 +0000268
Chris Lattner20b1ea02001-09-14 03:47:57 +0000269//************************ Internal Functions ******************************/
270
Chris Lattner20b1ea02001-09-14 03:47:57 +0000271
Chris Lattner20b1ea02001-09-14 03:47:57 +0000272static inline MachineOpCode
273ChooseBprInstruction(const InstructionNode* instrNode)
274{
275 MachineOpCode opCode;
276
277 Instruction* setCCInstr =
278 ((InstructionNode*) instrNode->leftChild())->getInstruction();
279
280 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000281 {
282 case Instruction::SetEQ: opCode = V9::BRZ; break;
283 case Instruction::SetNE: opCode = V9::BRNZ; break;
284 case Instruction::SetLE: opCode = V9::BRLEZ; break;
285 case Instruction::SetGE: opCode = V9::BRGEZ; break;
286 case Instruction::SetLT: opCode = V9::BRLZ; break;
287 case Instruction::SetGT: opCode = V9::BRGZ; break;
288 default:
289 assert(0 && "Unrecognized VM instruction!");
290 opCode = V9::INVALID_OPCODE;
291 break;
292 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000293
294 return opCode;
295}
296
297
298static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000299ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000300 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000301{
Misha Brukmana98cd452003-05-20 20:32:24 +0000302 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000303
304 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
305
Misha Brukman81b06862003-05-21 18:48:06 +0000306 if (isSigned) {
307 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000308 {
Misha Brukman81b06862003-05-21 18:48:06 +0000309 case Instruction::SetEQ: opCode = V9::BE; break;
310 case Instruction::SetNE: opCode = V9::BNE; break;
311 case Instruction::SetLE: opCode = V9::BLE; break;
312 case Instruction::SetGE: opCode = V9::BGE; break;
313 case Instruction::SetLT: opCode = V9::BL; break;
314 case Instruction::SetGT: opCode = V9::BG; break;
315 default:
316 assert(0 && "Unrecognized VM instruction!");
317 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000318 }
Misha Brukman81b06862003-05-21 18:48:06 +0000319 } else {
320 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000321 {
Misha Brukman81b06862003-05-21 18:48:06 +0000322 case Instruction::SetEQ: opCode = V9::BE; break;
323 case Instruction::SetNE: opCode = V9::BNE; break;
324 case Instruction::SetLE: opCode = V9::BLEU; break;
325 case Instruction::SetGE: opCode = V9::BCC; break;
326 case Instruction::SetLT: opCode = V9::BCS; break;
327 case Instruction::SetGT: opCode = V9::BGU; break;
328 default:
329 assert(0 && "Unrecognized VM instruction!");
330 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000331 }
Misha Brukman81b06862003-05-21 18:48:06 +0000332 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000333
334 return opCode;
335}
336
337static inline MachineOpCode
338ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000339 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000340{
Misha Brukmana98cd452003-05-20 20:32:24 +0000341 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000342
343 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000344 {
345 case Instruction::SetEQ: opCode = V9::FBE; break;
346 case Instruction::SetNE: opCode = V9::FBNE; break;
347 case Instruction::SetLE: opCode = V9::FBLE; break;
348 case Instruction::SetGE: opCode = V9::FBGE; break;
349 case Instruction::SetLT: opCode = V9::FBL; break;
350 case Instruction::SetGT: opCode = V9::FBG; break;
351 default:
352 assert(0 && "Unrecognized VM instruction!");
353 break;
354 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000355
356 return opCode;
357}
358
359
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000360// Create a unique TmpInstruction for a boolean value,
361// representing the CC register used by a branch on that value.
362// For now, hack this using a little static cache of TmpInstructions.
363// Eventually the entire BURG instruction selection should be put
364// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000365// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000366// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000367//
368static TmpInstruction*
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000369GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType,
370 MachineCodeForInstruction& mcfi)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000371{
Chris Lattner09ff1122002-07-24 21:21:32 +0000372 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000373 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000374 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000375
376 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
377
Misha Brukman81b06862003-05-21 18:48:06 +0000378 if (lastFunction != F) {
379 lastFunction = F;
380 boolToTmpCache.clear();
381 }
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000382
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000383 // Look for tmpI and create a new one otherwise. The new value is
384 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000385 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
386 if (tmpI == NULL)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000387 tmpI = new TmpInstruction(mcfi, ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000388
389 return tmpI;
390}
391
392
Chris Lattner20b1ea02001-09-14 03:47:57 +0000393static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000394ChooseBccInstruction(const InstructionNode* instrNode,
Vikram S. Adve786833a2003-07-06 20:13:59 +0000395 const Type*& setCCType)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000396{
397 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000398 assert(setCCNode->getOpLabel() == SetCCOp);
399 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +0000400 setCCType = setCCInstr->getOperand(0)->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000401
Vikram S. Adve786833a2003-07-06 20:13:59 +0000402 if (setCCType->isFloatingPoint())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000403 return ChooseBFpccInstruction(instrNode, setCCInstr);
404 else
405 return ChooseBpccInstruction(instrNode, setCCInstr);
406}
407
408
Misha Brukmaneecdb662003-06-02 20:55:14 +0000409// WARNING: since this function has only one caller, it always returns
410// the opcode that expects an immediate and a register. If this function
411// is ever used in cases where an opcode that takes two registers is required,
412// then modify this function and use convertOpcodeFromRegToImm() where required.
413//
414// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
415// new cases of opcodes.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000416static inline MachineOpCode
Misha Brukmaneecdb662003-06-02 20:55:14 +0000417ChooseMovFpcciInstruction(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000418{
Misha Brukmana98cd452003-05-20 20:32:24 +0000419 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000420
421 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000422 {
Misha Brukmaneecdb662003-06-02 20:55:14 +0000423 case Instruction::SetEQ: opCode = V9::MOVFEi; break;
424 case Instruction::SetNE: opCode = V9::MOVFNEi; break;
425 case Instruction::SetLE: opCode = V9::MOVFLEi; break;
426 case Instruction::SetGE: opCode = V9::MOVFGEi; break;
427 case Instruction::SetLT: opCode = V9::MOVFLi; break;
428 case Instruction::SetGT: opCode = V9::MOVFGi; break;
Misha Brukman81b06862003-05-21 18:48:06 +0000429 default:
430 assert(0 && "Unrecognized VM instruction!");
431 break;
432 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000433
434 return opCode;
435}
436
437
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000438// ChooseMovpcciForSetCC -- Choose a conditional-move instruction
439// based on the type of SetCC operation.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000440//
Misha Brukmaneecdb662003-06-02 20:55:14 +0000441// WARNING: since this function has only one caller, it always returns
442// the opcode that expects an immediate and a register. If this function
443// is ever used in cases where an opcode that takes two registers is required,
444// then modify this function and use convertOpcodeFromRegToImm() where required.
445//
446// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
447// new cases of opcodes.
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000448//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000449static MachineOpCode
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000450ChooseMovpcciForSetCC(const InstructionNode* instrNode)
451{
452 MachineOpCode opCode = V9::INVALID_OPCODE;
453
454 const Type* opType = instrNode->leftChild()->getValue()->getType();
455 assert(opType->isIntegral() || isa<PointerType>(opType));
456 bool noSign = opType->isUnsigned() || isa<PointerType>(opType);
457
458 switch(instrNode->getInstruction()->getOpcode())
459 {
460 case Instruction::SetEQ: opCode = V9::MOVEi; break;
461 case Instruction::SetLE: opCode = noSign? V9::MOVLEUi : V9::MOVLEi; break;
462 case Instruction::SetGE: opCode = noSign? V9::MOVCCi : V9::MOVGEi; break;
463 case Instruction::SetLT: opCode = noSign? V9::MOVCSi : V9::MOVLi; break;
464 case Instruction::SetGT: opCode = noSign? V9::MOVGUi : V9::MOVGi; break;
465 case Instruction::SetNE: opCode = V9::MOVNEi; break;
466 default: assert(0 && "Unrecognized LLVM instr!"); break;
467 }
468
469 return opCode;
470}
471
472
473// ChooseMovpregiForSetCC -- Choose a conditional-move-on-register-value
474// instruction based on the type of SetCC operation. These instructions
475// compare a register with 0 and perform the move is the comparison is true.
476//
477// WARNING: like the previous function, this function it always returns
478// the opcode that expects an immediate and a register. See above.
479//
480static MachineOpCode
481ChooseMovpregiForSetCC(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000482{
Misha Brukmana98cd452003-05-20 20:32:24 +0000483 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000484
485 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000486 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000487 case Instruction::SetEQ: opCode = V9::MOVRZi; break;
488 case Instruction::SetLE: opCode = V9::MOVRLEZi; break;
489 case Instruction::SetGE: opCode = V9::MOVRGEZi; break;
490 case Instruction::SetLT: opCode = V9::MOVRLZi; break;
491 case Instruction::SetGT: opCode = V9::MOVRGZi; break;
492 case Instruction::SetNE: opCode = V9::MOVRNZi; break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000493 default: assert(0 && "Unrecognized VM instr!"); break;
Misha Brukman81b06862003-05-21 18:48:06 +0000494 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000495
496 return opCode;
497}
498
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000499
Chris Lattner20b1ea02001-09-14 03:47:57 +0000500static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000501ChooseConvertToFloatInstr(const TargetMachine& target,
502 OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000503{
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000504 assert((vopCode == ToFloatTy || vopCode == ToDoubleTy) &&
505 "Unrecognized convert-to-float opcode!");
Vikram S. Advee895a742003-08-06 18:48:40 +0000506 assert((opType->isIntegral() || opType->isFloatingPoint() ||
507 isa<PointerType>(opType))
508 && "Trying to convert a non-scalar type to FLOAT/DOUBLE?");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000509
Misha Brukmana98cd452003-05-20 20:32:24 +0000510 MachineOpCode opCode = V9::INVALID_OPCODE;
Vikram S. Advee895a742003-08-06 18:48:40 +0000511
512 unsigned opSize = target.getTargetData().getTypeSize(opType);
513
514 if (opType == Type::FloatTy)
515 opCode = (vopCode == ToFloatTy? V9::NOP : V9::FSTOD);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000516 else if (opType == Type::DoubleTy)
Vikram S. Advee895a742003-08-06 18:48:40 +0000517 opCode = (vopCode == ToFloatTy? V9::FDTOS : V9::NOP);
518 else if (opSize <= 4)
519 opCode = (vopCode == ToFloatTy? V9::FITOS : V9::FITOD);
520 else {
521 assert(opSize == 8 && "Unrecognized type size > 4 and < 8!");
522 opCode = (vopCode == ToFloatTy? V9::FXTOS : V9::FXTOD);
523 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000524
525 return opCode;
526}
527
528static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000529ChooseConvertFPToIntInstr(const TargetMachine& target,
530 const Type* destType, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000531{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000532 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
533 && "This function should only be called for FLOAT or DOUBLE");
Vikram S. Advee895a742003-08-06 18:48:40 +0000534 assert((destType->isIntegral() || isa<PointerType>(destType))
535 && "Trying to convert FLOAT/DOUBLE to a non-scalar type?");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000536
Vikram S. Advee895a742003-08-06 18:48:40 +0000537 MachineOpCode opCode = V9::INVALID_OPCODE;
538
539 unsigned destSize = target.getTargetData().getTypeSize(destType);
540
541 if (destType == Type::UIntTy)
542 assert(destType != Type::UIntTy && "Expand FP-to-uint beforehand.");
543 else if (destSize <= 4)
Misha Brukman81b06862003-05-21 18:48:06 +0000544 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
Vikram S. Advee895a742003-08-06 18:48:40 +0000545 else {
546 assert(destSize == 8 && "Unrecognized type size > 4 and < 8!");
547 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
548 }
Vikram S. Adve94c40812002-09-27 14:33:08 +0000549
Chris Lattner20b1ea02001-09-14 03:47:57 +0000550 return opCode;
551}
552
Vikram S. Advee895a742003-08-06 18:48:40 +0000553static MachineInstr*
554CreateConvertFPToIntInstr(const TargetMachine& target,
555 Value* srcVal,
556 Value* destVal,
557 const Type* destType)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000558{
Vikram S. Advee895a742003-08-06 18:48:40 +0000559 MachineOpCode opCode = ChooseConvertFPToIntInstr(target, destType,
560 srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000561 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000562 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000563}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000564
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000565// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000566// The FP value must be converted to the dest type in an FP register,
567// and the result is then copied from FP to int register via memory.
Vikram S. Advee895a742003-08-06 18:48:40 +0000568// SPARC does not have a float-to-uint conversion, only a float-to-int (fdtoi).
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000569// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
Vikram S. Advee895a742003-08-06 18:48:40 +0000570// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly.
571// Therefore, for converting an FP value to uint32_t, we first need to convert
572// to uint64_t and then to uint32_t.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000573//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000574static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000575CreateCodeToConvertFloatToInt(const TargetMachine& target,
576 Value* opVal,
577 Instruction* destI,
578 std::vector<MachineInstr*>& mvec,
579 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000580{
Vikram S. Advee895a742003-08-06 18:48:40 +0000581 Function* F = destI->getParent()->getParent();
582
Vikram S. Adve1e606692002-07-31 21:01:34 +0000583 // Create a temporary to represent the FP register into which the
584 // int value will placed after conversion. The type of this temporary
585 // depends on the type of FP register to use: single-prec for a 32-bit
586 // int or smaller; double-prec for a 64-bit int.
587 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000588 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve1e606692002-07-31 21:01:34 +0000589
Vikram S. Advee895a742003-08-06 18:48:40 +0000590 const Type* castDestType = destI->getType(); // type for the cast instr result
591 const Type* castDestRegType; // type for cast instruction result reg
592 TmpInstruction* destForCast; // dest for cast instruction
593 Instruction* fpToIntCopyDest = destI; // dest for fp-reg-to-int-reg copy instr
594
595 // For converting an FP value to uint32_t, we first need to convert to
596 // uint64_t and then to uint32_t, as explained above.
597 if (destI->getType() == Type::UIntTy) {
598 castDestType = Type::ULongTy; // use this instead of type of destI
599 castDestRegType = Type::DoubleTy; // uint64_t needs 64-bit FP register.
600 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
601 fpToIntCopyDest = new TmpInstruction(mcfi, castDestType, destForCast);
602 }
603 else {
604 castDestRegType = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
605 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
606 }
607
608 // Create the fp-to-int conversion instruction (src and dest regs are FP regs)
609 mvec.push_back(CreateConvertFPToIntInstr(target, opVal, destForCast,
610 castDestType));
Vikram S. Adve1e606692002-07-31 21:01:34 +0000611
612 // Create the fpreg-to-intreg copy code
Vikram S. Advee895a742003-08-06 18:48:40 +0000613 target.getInstrInfo().CreateCodeToCopyFloatToInt(target, F, destForCast,
614 fpToIntCopyDest, mvec, mcfi);
615
616 // Create the uint64_t to uint32_t conversion, if needed
617 if (destI->getType() == Type::UIntTy)
618 target.getInstrInfo().
619 CreateZeroExtensionInstructions(target, F, fpToIntCopyDest, destI,
620 /*numLowBits*/ 32, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000621}
622
623
Chris Lattner20b1ea02001-09-14 03:47:57 +0000624static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000625ChooseAddInstruction(const InstructionNode* instrNode)
626{
627 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
628}
629
630
Chris Lattner20b1ea02001-09-14 03:47:57 +0000631static inline MachineInstr*
632CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000633 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000634{
Misha Brukmana98cd452003-05-20 20:32:24 +0000635 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000636 .addReg(instrNode->leftChild()->getValue())
637 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000638}
639
640static inline MachineInstr*
641CreateAddConstInstruction(const InstructionNode* instrNode)
642{
643 MachineInstr* minstr = NULL;
644
645 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000646 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000647
648 // Cases worth optimizing are:
649 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
650 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
651 //
Chris Lattner9b625032002-05-06 16:15:30 +0000652 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000653 double dval = FPC->getValue();
654 if (dval == 0.0)
655 minstr = CreateMovFloatInstruction(instrNode,
656 instrNode->getInstruction()->getType());
657 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000658
659 return minstr;
660}
661
662
663static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000664ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000665{
Misha Brukmana98cd452003-05-20 20:32:24 +0000666 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000667
Misha Brukman81b06862003-05-21 18:48:06 +0000668 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukman91aee472003-05-27 22:37:00 +0000669 opCode = V9::SUBr;
Misha Brukman81b06862003-05-21 18:48:06 +0000670 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000671 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000672 {
673 case Type::FloatTyID: opCode = V9::FSUBS; break;
674 case Type::DoubleTyID: opCode = V9::FSUBD; break;
675 default: assert(0 && "Invalid type for SUB instruction"); break;
676 }
677 }
678
Chris Lattner20b1ea02001-09-14 03:47:57 +0000679 return opCode;
680}
681
682
683static inline MachineInstr*
684CreateSubConstInstruction(const InstructionNode* instrNode)
685{
686 MachineInstr* minstr = NULL;
687
688 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000689 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000690
691 // Cases worth optimizing are:
692 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
693 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
694 //
Chris Lattner9b625032002-05-06 16:15:30 +0000695 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
696 double dval = FPC->getValue();
697 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000698 minstr = CreateMovFloatInstruction(instrNode,
699 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000700 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000701
702 return minstr;
703}
704
705
706static inline MachineOpCode
707ChooseFcmpInstruction(const InstructionNode* instrNode)
708{
Misha Brukmana98cd452003-05-20 20:32:24 +0000709 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000710
711 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
712 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000713 case Type::FloatTyID: opCode = V9::FCMPS; break;
714 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000715 default: assert(0 && "Invalid type for FCMP instruction"); break;
716 }
717
718 return opCode;
719}
720
721
722// Assumes that leftArg and rightArg are both cast instructions.
723//
724static inline bool
725BothFloatToDouble(const InstructionNode* instrNode)
726{
727 InstrTreeNode* leftArg = instrNode->leftChild();
728 InstrTreeNode* rightArg = instrNode->rightChild();
729 InstrTreeNode* leftArgArg = leftArg->leftChild();
730 InstrTreeNode* rightArgArg = rightArg->leftChild();
731 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
732
733 // Check if both arguments are floats cast to double
734 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000735 leftArgArg->getValue()->getType() == Type::FloatTy &&
736 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000737}
738
739
740static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000741ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000742{
Misha Brukmana98cd452003-05-20 20:32:24 +0000743 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000744
Chris Lattner0c4e8862002-09-03 01:08:28 +0000745 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000746 opCode = V9::MULXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000747 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000748 switch(resultType->getPrimitiveID())
Misha Brukman7b647942003-05-30 20:11:56 +0000749 {
750 case Type::FloatTyID: opCode = V9::FMULS; break;
751 case Type::DoubleTyID: opCode = V9::FMULD; break;
752 default: assert(0 && "Invalid type for MUL instruction"); break;
753 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000754
755 return opCode;
756}
757
758
Vikram S. Adve510eec72001-11-04 21:59:14 +0000759
Chris Lattner20b1ea02001-09-14 03:47:57 +0000760static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000761CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000762 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000763{
Misha Brukman91aee472003-05-27 22:37:00 +0000764 return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000765 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000766}
767
768
Vikram S. Adve242a8082002-05-19 15:25:51 +0000769// Create instruction sequence for any shift operation.
770// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
771// requires a second instruction for explicit sign-extension.
772// Note that we only have to worry about a sign-bit appearing in the
773// most significant bit of the operand after shifting (e.g., bit 32 of
774// Int or bit 16 of Short), so we do not have to worry about results
775// that are as large as a normal integer register.
776//
777static inline void
778CreateShiftInstructions(const TargetMachine& target,
779 Function* F,
780 MachineOpCode shiftOpCode,
781 Value* argVal1,
782 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000783 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000784 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000785 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000786 MachineCodeForInstruction& mcfi)
787{
788 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
789 "Large shift sizes unexpected, but can be handled below: "
790 "You need to check whether or not it fits in immed field below");
791
792 // If this is a logical left shift of a type smaller than the standard
793 // integer reg. size, we have to extend the sign-bit into upper bits
794 // of dest, so we need to put the result of the SLL into a temporary.
795 //
796 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000797 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000798
Misha Brukmand36e30e2003-06-06 09:52:23 +0000799 if ((shiftOpCode == V9::SLLr5 || shiftOpCode == V9::SLLXr6) && opSize < 8) {
Misha Brukman7b647942003-05-30 20:11:56 +0000800 // put SLL result into a temporary
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000801 shiftDest = new TmpInstruction(mcfi, argVal1, optArgVal2, "sllTmp");
Misha Brukman7b647942003-05-30 20:11:56 +0000802 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000803
804 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000805 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
806 .addReg(shiftDest, MOTy::Def)
807 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
808 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000809 mvec.push_back(M);
810
Misha Brukman7b647942003-05-30 20:11:56 +0000811 if (shiftDest != destVal) {
812 // extend the sign-bit of the result into all upper bits of dest
813 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
814 target.getInstrInfo().
815 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
816 8*opSize, mvec, mcfi);
817 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000818}
819
820
Vikram S. Adve74825322002-03-18 03:15:35 +0000821// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000822// create a cheaper instruction.
823// This returns the approximate cost of the instructions generated,
824// which is used to pick the cheapest when both operands are constant.
Vikram S. Adve645fea32003-05-25 21:59:47 +0000825static unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000826CreateMulConstInstruction(const TargetMachine &target, Function* F,
827 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000828 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000829 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000830{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000831 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukman91aee472003-05-27 22:37:00 +0000832 unsigned cost = target.getInstrInfo().minLatency(V9::MULXr);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000833 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000834
835 Value* constOp = rval;
836 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000837 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000838
839 // Cases worth optimizing are:
840 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
841 // (2) Multiply by 2^x for integer types: replace with Shift
842 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000843 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000844
Misha Brukmana98cd452003-05-20 20:32:24 +0000845 if (resultType->isInteger() || isa<PointerType>(resultType)) {
846 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +0000847 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
848 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +0000849 if (isValidConst) {
850 unsigned pow;
851 bool needNeg = false;
852 if (C < 0) {
853 needNeg = true;
854 C = -C;
855 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000856
Misha Brukmana98cd452003-05-20 20:32:24 +0000857 if (C == 0 || C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000858 cost = target.getInstrInfo().minLatency(V9::ADDr);
Misha Brukmana98cd452003-05-20 20:32:24 +0000859 unsigned Zero = target.getRegInfo().getZeroRegNum();
860 MachineInstr* M;
861 if (C == 0)
Misha Brukman91aee472003-05-27 22:37:00 +0000862 M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000863 else
Misha Brukman91aee472003-05-27 22:37:00 +0000864 M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000865 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +0000866 } else if (isPowerOf2(C, pow)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000867 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Misha Brukmand36e30e2003-06-06 09:52:23 +0000868 MachineOpCode opCode = (opSize <= 32)? V9::SLLr5 : V9::SLLXr6;
Misha Brukmana98cd452003-05-20 20:32:24 +0000869 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
870 destVal, mvec, mcfi);
871 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000872
Misha Brukman7b647942003-05-30 20:11:56 +0000873 if (mvec.size() > 0 && needNeg) {
874 // insert <reg = SUB 0, reg> after the instr to flip the sign
Misha Brukmana98cd452003-05-20 20:32:24 +0000875 MachineInstr* M = CreateIntNegInstruction(target, destVal);
876 mvec.push_back(M);
877 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000878 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000879 } else {
880 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
881 double dval = FPC->getValue();
882 if (fabs(dval) == 1) {
883 MachineOpCode opCode = (dval < 0)
884 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
885 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
886 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
887 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000888 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000889 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000890
Misha Brukmana98cd452003-05-20 20:32:24 +0000891 if (firstNewInstr < mvec.size()) {
892 cost = 0;
893 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
894 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
895 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000896
897 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000898}
899
900
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000901// Does not create any instructions if we cannot exploit constant to
902// create a cheaper instruction.
903//
904static inline void
905CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000906 Function* F,
907 Value* lval, Value* rval,
908 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000909 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000910 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000911{
912 Value* constOp;
Misha Brukman7b647942003-05-30 20:11:56 +0000913 if (isa<Constant>(lval) && isa<Constant>(rval)) {
914 // both operands are constant: evaluate and "set" in dest
915 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
916 cast<Constant>(lval),
917 cast<Constant>(rval));
918 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
919 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000920 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000921 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000922 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000923 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000924
925 // else neither is constant
926 return;
927}
928
Vikram S. Adve74825322002-03-18 03:15:35 +0000929// Return NULL if we cannot exploit constant to create a cheaper instruction
930static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000931CreateMulInstruction(const TargetMachine &target, Function* F,
932 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000933 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000934 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000935 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
936{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000937 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000938 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000939 if (mvec.size() == L) {
940 // no instructions were added so create MUL reg, reg, reg.
941 // Use FSMULD if both operands are actually floats cast to doubles.
942 // Otherwise, use the default opcode for the appropriate type.
943 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
944 ? forceMulOp
945 : ChooseMulInstructionByType(destVal->getType()));
946 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
947 .addRegDef(destVal));
948 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000949}
950
951
Vikram S. Adve510eec72001-11-04 21:59:14 +0000952// Generate a divide instruction for Div or Rem.
953// For Rem, this assumes that the operand type will be signed if the result
954// type is signed. This is correct because they must have the same sign.
955//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000956static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000957ChooseDivInstruction(TargetMachine &target,
958 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000959{
Misha Brukmana98cd452003-05-20 20:32:24 +0000960 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000961
962 const Type* resultType = instrNode->getInstruction()->getType();
963
Chris Lattner0c4e8862002-09-03 01:08:28 +0000964 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000965 opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000966 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000967 switch(resultType->getPrimitiveID())
968 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000969 case Type::FloatTyID: opCode = V9::FDIVS; break;
970 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000971 default: assert(0 && "Invalid type for DIV instruction"); break;
972 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000973
974 return opCode;
975}
976
977
Chris Lattner54e898e2003-01-15 19:23:34 +0000978// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve645fea32003-05-25 21:59:47 +0000979static void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000980CreateDivConstInstruction(TargetMachine &target,
981 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000982 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000983{
Chris Lattner54e898e2003-01-15 19:23:34 +0000984 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000985 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000986 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000987 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000988
Vikram S. Adve645fea32003-05-25 21:59:47 +0000989 Instruction* destVal = instrNode->getInstruction();
Chris Lattner54e898e2003-01-15 19:23:34 +0000990 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000991
992 // Cases worth optimizing are:
993 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
994 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
995 //
996 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000997
Misha Brukman7b647942003-05-30 20:11:56 +0000998 if (resultType->isInteger()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000999 unsigned pow;
1000 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +00001001 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
1002 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +00001003 if (isValidConst) {
1004 bool needNeg = false;
1005 if (C < 0) {
1006 needNeg = true;
1007 C = -C;
1008 }
Vikram S. Advee6124d32003-07-29 19:59:23 +00001009
Misha Brukmana98cd452003-05-20 20:32:24 +00001010 if (C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +00001011 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001012 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001013 } else if (isPowerOf2(C, pow)) {
Vikram S. Adve645fea32003-05-25 21:59:47 +00001014 unsigned opCode;
1015 Value* shiftOperand;
Vikram S. Advee895a742003-08-06 18:48:40 +00001016 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve645fea32003-05-25 21:59:47 +00001017
1018 if (resultType->isSigned()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001019 // For N / 2^k, if the operand N is negative,
1020 // we need to add (2^k - 1) before right-shifting by k, i.e.,
Vikram S. Adve645fea32003-05-25 21:59:47 +00001021 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001022 // (N / 2^k) = N >> k, if N >= 0;
1023 // (N + 2^k - 1) >> k, if N < 0
1024 //
1025 // If N is <= 32 bits, use:
1026 // sra N, 31, t1 // t1 = ~0, if N < 0, 0 else
1027 // srl t1, 32-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1028 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1029 // sra t3, k, result // result = N / 2^k
1030 //
1031 // If N is 64 bits, use:
1032 // srax N, k-1, t1 // t1 = sign bit in high k positions
1033 // srlx t1, 64-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1034 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1035 // sra t3, k, result // result = N / 2^k
1036 //
1037 TmpInstruction *sraTmp, *srlTmp, *addTmp;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001038 MachineCodeForInstruction& mcfi
1039 = MachineCodeForInstruction::get(destVal);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001040 sraTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getSign");
1041 srlTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getPlus2km1");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001042 addTmp = new TmpInstruction(mcfi, resultType, LHS, srlTmp,"incIfNeg");
Vikram S. Adve645fea32003-05-25 21:59:47 +00001043
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001044 // Create the SRA or SRAX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001045 mvec.push_back(BuildMI((opSize > 4)? V9::SRAXi6 : V9::SRAi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001046 .addReg(LHS)
1047 .addSImm((resultType==Type::LongTy)? pow-1 : 31)
1048 .addRegDef(sraTmp));
1049
Vikram S. Adve645fea32003-05-25 21:59:47 +00001050 // Create the SRL or SRLX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001051 mvec.push_back(BuildMI((opSize > 4)? V9::SRLXi6 : V9::SRLi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001052 .addReg(sraTmp)
1053 .addSImm((resultType==Type::LongTy)? 64-pow : 32-pow)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001054 .addRegDef(srlTmp));
1055
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001056 // Create the ADD instruction to add 2^pow-1 for negative values
Misha Brukman91aee472003-05-27 22:37:00 +00001057 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001058 .addRegDef(addTmp));
1059
1060 // Get the shift operand and "right-shift" opcode to do the divide
1061 shiftOperand = addTmp;
Vikram S. Advee895a742003-08-06 18:48:40 +00001062 opCode = (opSize > 4)? V9::SRAXi6 : V9::SRAi5;
Misha Brukman7b647942003-05-30 20:11:56 +00001063 } else {
Vikram S. Adve645fea32003-05-25 21:59:47 +00001064 // Get the shift operand and "right-shift" opcode to do the divide
1065 shiftOperand = LHS;
Vikram S. Advee895a742003-08-06 18:48:40 +00001066 opCode = (opSize > 4)? V9::SRLXi6 : V9::SRLi5;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001067 }
1068
1069 // Now do the actual shift!
1070 mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow)
1071 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001072 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001073
Misha Brukmana98cd452003-05-20 20:32:24 +00001074 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
1075 // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve645fea32003-05-25 21:59:47 +00001076 mvec.push_back(CreateIntNegInstruction(target, destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001077 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001078 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001079 } else {
1080 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
1081 double dval = FPC->getValue();
1082 if (fabs(dval) == 1) {
1083 unsigned opCode =
1084 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
1085 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001086
Vikram S. Adve645fea32003-05-25 21:59:47 +00001087 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001088 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001089 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001090 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001091}
1092
1093
Vikram S. Adve74825322002-03-18 03:15:35 +00001094static void
1095CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1096 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001097 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001098 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001099 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001100{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001101 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001102 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001103 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001104 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001105
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001106 // Enforce the alignment constraints on the stack pointer at
1107 // compile time if the total size is a known constant.
Misha Brukman7b647942003-05-30 20:11:56 +00001108 if (isa<Constant>(numElementsVal)) {
1109 bool isValid;
Vikram S. Advee6124d32003-07-29 19:59:23 +00001110 int64_t numElem = (int64_t) target.getInstrInfo().
1111 ConvertConstantToIntType(target, numElementsVal,
1112 numElementsVal->getType(), isValid);
Misha Brukman7b647942003-05-30 20:11:56 +00001113 assert(isValid && "Unexpectedly large array dimension in alloca!");
1114 int64_t total = numElem * tsize;
1115 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1116 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1117 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1118 } else {
1119 // The size is not a constant. Generate code to compute it and
1120 // code to pad the size for stack alignment.
1121 // Create a Value to hold the (constant) element size
1122 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001123
Misha Brukman7b647942003-05-30 20:11:56 +00001124 // Create temporary values to hold the result of MUL, SLL, SRL
Vikram S. Adve80544442003-06-23 02:13:57 +00001125 // To pad `size' to next smallest multiple of 16:
1126 // size = (size + 15) & (-16 = 0xfffffffffffffff0)
1127 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001128 TmpInstruction* tmpProd = new TmpInstruction(mcfi,numElementsVal, tsizeVal);
Vikram S. Adve80544442003-06-23 02:13:57 +00001129 TmpInstruction* tmpAdd15= new TmpInstruction(mcfi,numElementsVal, tmpProd);
1130 TmpInstruction* tmpAndf0= new TmpInstruction(mcfi,numElementsVal, tmpAdd15);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001131
Misha Brukman7b647942003-05-30 20:11:56 +00001132 // Instruction 1: mul numElements, typeSize -> tmpProd
1133 // This will optimize the MUL as far as possible.
Vikram S. Adve80544442003-06-23 02:13:57 +00001134 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd, getMvec,
Misha Brukman7b647942003-05-30 20:11:56 +00001135 mcfi, INVALID_MACHINE_OPCODE);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001136
Vikram S. Adve80544442003-06-23 02:13:57 +00001137 // Instruction 2: andn tmpProd, 0x0f -> tmpAndn
1138 getMvec.push_back(BuildMI(V9::ADDi, 3).addReg(tmpProd).addSImm(15)
1139 .addReg(tmpAdd15, MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001140
Vikram S. Adve80544442003-06-23 02:13:57 +00001141 // Instruction 3: add tmpAndn, 0x10 -> tmpAdd16
1142 getMvec.push_back(BuildMI(V9::ANDi, 3).addReg(tmpAdd15).addSImm(-16)
1143 .addReg(tmpAndf0, MOTy::Def));
1144
1145 totalSizeVal = tmpAndf0;
Misha Brukman7b647942003-05-30 20:11:56 +00001146 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001147
1148 // Get the constant offset from SP for dynamically allocated storage
1149 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001150 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001151 bool growUp;
1152 ConstantSInt* dynamicAreaOffset =
1153 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001154 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001155 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1156
Chris Lattner54e898e2003-01-15 19:23:34 +00001157 unsigned SPReg = target.getRegInfo().getStackPointer();
1158
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001159 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukman91aee472003-05-27 22:37:00 +00001160 getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001161 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001162
Vikram S. Adve74825322002-03-18 03:15:35 +00001163 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukman91aee472003-05-27 22:37:00 +00001164 getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset)
Misha Brukmana98cd452003-05-20 20:32:24 +00001165 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001166}
1167
1168
1169static void
1170CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1171 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001172 unsigned tsize,
1173 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001174 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001175{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001176 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001177 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001178 "Result value is not part of a function?");
1179 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001180 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001181
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001182 // Put the variable in the dynamically sized area of the frame if either:
1183 // (a) The offset is too large to use as an immediate in load/stores
1184 // (check LDX because all load/stores have the same-size immed. field).
1185 // (b) The object is "large", so it could cause many other locals,
1186 // spills, and temporaries to have large offsets.
1187 // NOTE: We use LARGE = 8 * argSlotSize = 64 bytes.
1188 // You've gotta love having only 13 bits for constant offset values :-|.
1189 //
1190 unsigned paddedSize;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001191 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001192 paddedSize,
1193 tsize * numElements);
1194
1195 if (((int)paddedSize) > 8 * target.getFrameInfo().getSizeOfEachArgOnStack() ||
1196 ! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001197 CreateCodeForVariableSizeAlloca(target, result, tsize,
1198 ConstantSInt::get(Type::IntTy,numElements),
1199 getMvec);
1200 return;
1201 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001202
1203 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001204 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001205
1206 // Create a temporary Value to hold the constant offset.
1207 // This is needed because it may not fit in the immediate field.
1208 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1209
1210 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001211 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukman91aee472003-05-27 22:37:00 +00001212 getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001213 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001214}
1215
1216
Chris Lattner20b1ea02001-09-14 03:47:57 +00001217//------------------------------------------------------------------------
1218// Function SetOperandsForMemInstr
1219//
1220// Choose addressing mode for the given load or store instruction.
1221// Use [reg+reg] if it is an indexed reference, and the index offset is
1222// not a constant or if it cannot fit in the offset field.
1223// Use [reg+offset] in all other cases.
1224//
1225// This assumes that all array refs are "lowered" to one of these forms:
1226// %x = load (subarray*) ptr, constant ; single constant offset
1227// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1228// Generally, this should happen via strength reduction + LICM.
1229// Also, strength reduction should take care of using the same register for
1230// the loop index variable and an array index, when that is profitable.
1231//------------------------------------------------------------------------
1232
1233static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001234SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001235 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001236 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001237 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001238{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001239 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001240 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001241 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001242 bool allConstantIndices;
1243 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001244
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001245 // Now create the appropriate operands for the machine instruction.
1246 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001247 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001248 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001249 MachineOperand::MachineOperandType offsetOpType =
1250 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001251
Vikram S. Adve74825322002-03-18 03:15:35 +00001252 // Check if there is an index vector and if so, compute the
1253 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001254 //
Misha Brukman7b647942003-05-30 20:11:56 +00001255 if (!idxVec.empty()) {
1256 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001257
Misha Brukman7b647942003-05-30 20:11:56 +00001258 // If all indices are constant, compute the combined offset directly.
1259 if (allConstantIndices) {
1260 // Compute the offset value using the index vector. Create a
1261 // virtual reg. for it since it may not fit in the immed field.
1262 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
1263 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
1264 } else {
1265 // There is at least one non-constant offset. Therefore, this must
1266 // be an array ref, and must have been lowered to a single non-zero
1267 // offset. (An extra leading zero offset, if any, can be ignored.)
1268 // Generate code sequence to compute address from index.
1269 //
1270 bool firstIdxIsZero = IsZero(idxVec[0]);
1271 assert(idxVec.size() == 1U + firstIdxIsZero
1272 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001273
Misha Brukman7b647942003-05-30 20:11:56 +00001274 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001275
Misha Brukman7b647942003-05-30 20:11:56 +00001276 std::vector<MachineInstr*> mulVec;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001277 Instruction* addr =
1278 new TmpInstruction(MachineCodeForInstruction::get(memInst),
1279 Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001280
Misha Brukman7b647942003-05-30 20:11:56 +00001281 // Get the array type indexed by idxVal, and compute its element size.
1282 // The call to getTypeSize() will fail if size is not constant.
1283 const Type* vecType = (firstIdxIsZero
1284 ? GetElementPtrInst::getIndexedType(ptrType,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001285 std::vector<Value*>(1U, idxVec[0]),
1286 /*AllowCompositeLeaf*/ true)
1287 : ptrType);
Misha Brukman7b647942003-05-30 20:11:56 +00001288 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1289 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
1290 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001291
Misha Brukman7b647942003-05-30 20:11:56 +00001292 // CreateMulInstruction() folds constants intelligently enough.
1293 CreateMulInstruction(target, memInst->getParent()->getParent(),
1294 idxVal, /* lval, not likely to be const*/
1295 eltSizeVal, /* rval, likely to be constant */
1296 addr, /* result */
1297 mulVec, MachineCodeForInstruction::get(memInst),
1298 INVALID_MACHINE_OPCODE);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001299
Misha Brukman7b647942003-05-30 20:11:56 +00001300 assert(mulVec.size() > 0 && "No multiply code created?");
1301 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
1302
1303 valueForRegOffset = addr;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001304 }
Misha Brukman7b647942003-05-30 20:11:56 +00001305 } else {
1306 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1307 smallConstOffset = 0;
1308 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001309
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001310 // For STORE:
1311 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1312 // For LOAD or GET_ELEMENT_PTR,
1313 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1314 //
1315 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001316 MachineInstr *MI;
1317 if (memInst->getOpcode() == Instruction::Store) {
Misha Brukman7b647942003-05-30 20:11:56 +00001318 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001319 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1320 .addReg(ptrVal).addReg(valueForRegOffset);
Misha Brukman7b647942003-05-30 20:11:56 +00001321 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001322 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001323 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1324 .addReg(ptrVal).addSImm(smallConstOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001325 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001326 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00001327 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001328 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1329 .addRegDef(memInst);
Misha Brukman7b647942003-05-30 20:11:56 +00001330 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001331 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001332 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1333 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001334 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001335 }
1336 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001337}
1338
1339
Chris Lattner20b1ea02001-09-14 03:47:57 +00001340//
1341// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001342// in place of the use(s) of that instruction in node `parent'.
1343// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001344// Also make sure to skip over a parent who:
1345// (1) is a list node in the Burg tree, or
1346// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001347//
1348static void
1349ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001350 InstrTreeNode* parent,
1351 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001352{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001353 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1354
Chris Lattner20b1ea02001-09-14 03:47:57 +00001355 Instruction* unusedOp = treeNode->getInstruction();
1356 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001357
1358 // The parent itself may be a list node, so find the real parent instruction
1359 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1360 {
1361 parent = parent->parent();
1362 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1363 }
1364 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1365
1366 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001367 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001368
1369 // The parent's mvec would be empty if it was itself forwarded.
1370 // Recursively call ForwardOperand in that case...
1371 //
Misha Brukman7b647942003-05-30 20:11:56 +00001372 if (mvec.size() == 0) {
1373 assert(parent->parent() != NULL &&
1374 "Parent could not have been forwarded, yet has no instructions?");
1375 ForwardOperand(treeNode, parent->parent(), operandNum);
1376 } else {
1377 for (unsigned i=0, N=mvec.size(); i < N; i++) {
1378 MachineInstr* minstr = mvec[i];
1379 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) {
1380 const MachineOperand& mop = minstr->getOperand(i);
1381 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
1382 mop.getVRegValue() == unusedOp)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001383 {
Misha Brukman7b647942003-05-30 20:11:56 +00001384 minstr->SetMachineOperandVal(i, MachineOperand::MO_VirtualRegister,
1385 fwdOp);
1386 }
1387 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001388
Misha Brukman7b647942003-05-30 20:11:56 +00001389 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
Chris Lattner907b7dc2003-08-05 16:59:24 +00001390 if (minstr->getImplicitRef(i) == unusedOp)
1391 minstr->setImplicitRef(i, fwdOp);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001392 }
Misha Brukman7b647942003-05-30 20:11:56 +00001393 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001394}
1395
1396
Vikram S. Adve242a8082002-05-19 15:25:51 +00001397inline bool
1398AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001399{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001400 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1401 UI != UE; ++UI)
1402 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1403 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1404 return false;
1405 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001406}
1407
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001408// Generate code for any intrinsic that needs a special code sequence
1409// instead of a regular call. If not that kind of intrinsic, do nothing.
1410// Returns true if code was generated, otherwise false.
1411//
1412bool CodeGenIntrinsic(LLVMIntrinsic::ID iid, CallInst &callInstr,
1413 TargetMachine &target,
1414 std::vector<MachineInstr*>& mvec)
1415{
1416 switch (iid) {
1417 case LLVMIntrinsic::va_start: {
Vikram S. Adve40dee512003-10-21 11:25:09 +00001418 // Get the address of the first incoming vararg argument on the stack
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001419 bool ignore;
1420 Function* func = cast<Function>(callInstr.getParent()->getParent());
1421 int numFixedArgs = func->getFunctionType()->getNumParams();
1422 int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum();
1423 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
1424 int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo().
1425 getFirstIncomingArgOffset(MachineFunction::get(func), ignore);
Misha Brukman91aee472003-05-27 22:37:00 +00001426 mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff).
Vikram S. Adve40dee512003-10-21 11:25:09 +00001427 addRegDef(&callInstr));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001428 return true;
1429 }
1430
1431 case LLVMIntrinsic::va_end:
1432 return true; // no-op on Sparc
1433
1434 case LLVMIntrinsic::va_copy:
Vikram S. Adve40dee512003-10-21 11:25:09 +00001435 // Simple copy of current va_list (arg1) to new va_list (result)
Misha Brukman91aee472003-05-27 22:37:00 +00001436 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001437 addMReg(target.getRegInfo().getZeroRegNum()).
Vikram S. Adve40dee512003-10-21 11:25:09 +00001438 addReg(callInstr.getOperand(1)).
1439 addRegDef(&callInstr));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001440 return true;
1441
Chris Lattner72af6b82003-08-18 16:06:09 +00001442 case LLVMIntrinsic::sigsetjmp:
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001443 case LLVMIntrinsic::setjmp: {
1444 // act as if we return 0
1445 unsigned g0 = target.getRegInfo().getZeroRegNum();
1446 mvec.push_back(BuildMI(V9::ORr,3).addMReg(g0).addMReg(g0)
1447 .addReg(&callInstr, MOTy::Def));
1448 return true;
1449 }
1450
Chris Lattner72af6b82003-08-18 16:06:09 +00001451 case LLVMIntrinsic::siglongjmp:
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001452 case LLVMIntrinsic::longjmp: {
1453 // call abort()
1454 Module* M = callInstr.getParent()->getParent()->getParent();
Vikram S. Adve5be74342003-09-16 05:56:22 +00001455 const FunctionType *voidvoidFuncTy =
1456 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
1457 Function *F = M->getOrInsertFunction("abort", voidvoidFuncTy);
1458 assert(F && "Unable to get or create `abort' function declaration");
1459
1460 // Create hidden virtual register for return address with type void*
1461 TmpInstruction* retAddrReg =
1462 new TmpInstruction(MachineCodeForInstruction::get(&callInstr),
1463 PointerType::get(Type::VoidTy), &callInstr);
1464
1465 // Use a descriptor to pass information about call arguments
1466 // to the register allocator. This descriptor will be "owned"
1467 // and freed automatically when the MachineCodeForInstruction
1468 // object for the callInstr goes away.
1469 CallArgsDescriptor* argDesc =
1470 new CallArgsDescriptor(&callInstr, retAddrReg, false, false);
1471
1472 MachineInstr* callMI = BuildMI(V9::CALL, 1).addPCDisp(F);
1473 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
1474
1475 mvec.push_back(callMI);
1476 mvec.push_back(BuildMI(V9::NOP, 0));
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001477 return true;
1478 }
1479
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001480 default:
1481 return false;
1482 }
1483}
1484
Vikram S. Advefb361122001-10-22 13:36:31 +00001485//******************* Externally Visible Functions *************************/
1486
Vikram S. Advefb361122001-10-22 13:36:31 +00001487//------------------------------------------------------------------------
1488// External Function: ThisIsAChainRule
1489//
1490// Purpose:
1491// Check if a given BURG rule is a chain rule.
1492//------------------------------------------------------------------------
1493
1494extern bool
1495ThisIsAChainRule(int eruleno)
1496{
1497 switch(eruleno)
1498 {
1499 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001500 case 123:
1501 case 124:
1502 case 125:
1503 case 126:
1504 case 127:
1505 case 128:
1506 case 129:
1507 case 130:
1508 case 131:
1509 case 132:
1510 case 133:
1511 case 155:
1512 case 221:
1513 case 222:
1514 case 241:
1515 case 242:
1516 case 243:
1517 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001518 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001519 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001520 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001521
Vikram S. Advefb361122001-10-22 13:36:31 +00001522 default:
1523 return false; break;
1524 }
1525}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001526
1527
1528//------------------------------------------------------------------------
1529// External Function: GetInstructionsByRule
1530//
1531// Purpose:
1532// Choose machine instructions for the SPARC according to the
1533// patterns chosen by the BURG-generated parser.
1534//------------------------------------------------------------------------
1535
Vikram S. Adve74825322002-03-18 03:15:35 +00001536void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001537GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001538 int ruleForNode,
1539 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001540 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001541 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001542{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001543 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001544 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001545 int nextRule;
1546 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001547 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001548 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001549 unsigned L;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001550 bool foldCase = false;
Vikram S. Adve74825322002-03-18 03:15:35 +00001551
1552 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001553
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001554 // If the code for this instruction was folded into the parent (user),
1555 // then do nothing!
1556 if (subtreeRoot->isFoldedIntoParent())
1557 return;
1558
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001559 //
1560 // Let's check for chain rules outside the switch so that we don't have
1561 // to duplicate the list of chain rule production numbers here again
1562 //
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001563 if (ThisIsAChainRule(ruleForNode))
1564 {
1565 // Chain rules have a single nonterminal on the RHS.
1566 // Get the rule that matches the RHS non-terminal and use that instead.
1567 //
1568 assert(nts[0] && ! nts[1]
1569 && "A chain rule should have only one RHS non-terminal!");
1570 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1571 nts = burm_nts[nextRule];
1572 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
1573 }
1574 else
1575 {
1576 switch(ruleForNode) {
1577 case 1: // stmt: Ret
1578 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001579 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001580 // for moving return value to appropriate register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001581 // Copy the return value to the required return register.
1582 // Mark the return Value as an implicit ref of the RET instr..
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001583 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001584 // Finally put a NOP in the delay slot.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001585 ReturnInst *returnInstr=cast<ReturnInst>(subtreeRoot->getInstruction());
1586 Value* retVal = returnInstr->getReturnValue();
1587 MachineCodeForInstruction& mcfi =
1588 MachineCodeForInstruction::get(returnInstr);
1589
1590 // Create a hidden virtual reg to represent the return address register
1591 // used by the machine instruction but not represented in LLVM.
1592 //
1593 Instruction* returnAddrTmp = new TmpInstruction(mcfi, returnInstr);
1594
1595 MachineInstr* retMI =
1596 BuildMI(V9::JMPLRETi, 3).addReg(returnAddrTmp).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001597 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001598
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001599 // If there is a value to return, we need to:
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001600 // (a) Sign-extend the value if it is smaller than 8 bytes (reg size)
1601 // (b) Insert a copy to copy the return value to the appropriate reg.
1602 // -- For FP values, create a FMOVS or FMOVD instruction
1603 // -- For non-FP values, create an add-with-0 instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001604 //
1605 if (retVal != NULL) {
1606 const UltraSparcRegInfo& regInfo =
1607 (UltraSparcRegInfo&) target.getRegInfo();
1608 const Type* retType = retVal->getType();
1609 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
1610 unsigned retRegNum = (retType->isFloatingPoint()
1611 ? (unsigned) SparcFloatRegClass::f0
1612 : (unsigned) SparcIntRegClass::i0);
1613 retRegNum = regInfo.getUnifiedRegNum(regClassID, retRegNum);
1614
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001615 // () Insert sign-extension instructions for small signed values.
1616 //
1617 Value* retValToUse = retVal;
1618 if (retType->isIntegral() && retType->isSigned()) {
1619 unsigned retSize = target.getTargetData().getTypeSize(retType);
1620 if (retSize <= 4) {
1621 // create a temporary virtual reg. to hold the sign-extension
1622 retValToUse = new TmpInstruction(mcfi, retVal);
1623
1624 // sign-extend retVal and put the result in the temporary reg.
1625 target.getInstrInfo().CreateSignExtensionInstructions
1626 (target, returnInstr->getParent()->getParent(),
1627 retVal, retValToUse, 8*retSize, mvec, mcfi);
1628 }
1629 }
1630
1631 // (b) Now, insert a copy to to the appropriate register:
1632 // -- For FP values, create a FMOVS or FMOVD instruction
1633 // -- For non-FP values, create an add-with-0 instruction
1634 //
1635 // First, create a virtual register to represent the register and
1636 // mark this vreg as being an implicit operand of the ret MI.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001637 TmpInstruction* retVReg =
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001638 new TmpInstruction(mcfi, retValToUse, NULL, "argReg");
1639
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001640 retMI->addImplicitRef(retVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001641
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001642 if (retType->isFloatingPoint())
1643 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001644 .addReg(retValToUse).addReg(retVReg, MOTy::Def));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001645 else
1646 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001647 .addReg(retValToUse).addSImm((int64_t) 0)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001648 .addReg(retVReg, MOTy::Def));
1649
1650 // Mark the operand with the register it should be assigned
1651 M->SetRegForOperand(M->getNumOperands()-1, retRegNum);
1652 retMI->SetRegForImplicitRef(retMI->getNumImplicitRefs()-1, retRegNum);
1653
1654 mvec.push_back(M);
1655 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001656
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001657 // Now insert the RET instruction and a NOP for the delay slot
1658 mvec.push_back(retMI);
Misha Brukmana98cd452003-05-20 20:32:24 +00001659 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001660
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001661 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001662 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001663
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001664 case 3: // stmt: Store(reg,reg)
1665 case 4: // stmt: Store(reg,ptrreg)
1666 SetOperandsForMemInstr(ChooseStoreInstruction(
Chris Lattner54e898e2003-01-15 19:23:34 +00001667 subtreeRoot->leftChild()->getValue()->getType()),
1668 mvec, subtreeRoot, target);
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001669 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001670
1671 case 5: // stmt: BrUncond
1672 {
1673 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1674 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
1675
1676 // delay slot
1677 mvec.push_back(BuildMI(V9::NOP, 0));
1678 break;
1679 }
1680
1681 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001682 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001683 // If the constant is ZERO, we can use the branch-on-integer-register
1684 // instructions and avoid the SUBcc instruction entirely.
1685 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001686 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001687 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1688 assert(constNode &&
1689 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001690 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001691 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001692
Chris Lattner0c4e8862002-09-03 01:08:28 +00001693 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001694 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00001695 && target.getInstrInfo().ConvertConstantToIntType(target,
1696 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001697 && isValidConst)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001698 {
1699 // That constant is a zero after all...
1700 // Use the left child of setCC as the first argument!
1701 // Mark the setCC node so that no code is generated for it.
1702 InstructionNode* setCCNode = (InstructionNode*)
1703 subtreeRoot->leftChild();
1704 assert(setCCNode->getOpLabel() == SetCCOp);
1705 setCCNode->markFoldedIntoParent();
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001706
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001707 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001708
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001709 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1710 .addReg(setCCNode->leftChild()->getValue())
1711 .addPCDisp(brInst->getSuccessor(0));
1712 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001713
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001714 // delay slot
1715 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001716
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001717 // false branch
1718 mvec.push_back(BuildMI(V9::BA, 1)
1719 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001720
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001721 // delay slot
1722 mvec.push_back(BuildMI(V9::NOP, 0));
1723 break;
1724 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001725 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001726 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001727
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001728 case 6: // stmt: BrCond(setCC)
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001729 { // bool => boolean was computed with SetCC.
1730 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001731 // If it is an integer CC, we also need to find the unique
1732 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001733 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001734 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +00001735 const Type* setCCType;
1736 unsigned Opcode = ChooseBccInstruction(subtreeRoot, setCCType);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001737 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1738 brInst->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00001739 setCCType,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001740 MachineCodeForInstruction::get(brInst));
Chris Lattner54e898e2003-01-15 19:23:34 +00001741 M = BuildMI(Opcode, 2).addCCReg(ccValue)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001742 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001743 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001744
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001745 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001746 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001747
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001748 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001749 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001750
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001751 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001752 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001753 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001754 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001755
1756 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001757 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001758 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001759 Constant* constVal =
1760 cast<Constant>(subtreeRoot->leftChild()->getValue());
1761 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001762
Misha Brukmana98cd452003-05-20 20:32:24 +00001763 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001764 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001765 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001766
1767 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001768 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001769 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001770 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001771
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001772 case 8: // stmt: BrCond(boolreg)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001773 { // boolreg => boolean is recorded in an integer register.
1774 // Use branch-on-integer-register instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001775 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001776 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001777 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001778 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001779 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001780
1781 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001782 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001783
1784 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001785 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001786
1787 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001788 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001789 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001790 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001791
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001792 case 9: // stmt: Switch(reg)
1793 assert(0 && "*** SWITCH instruction is not implemented yet.");
1794 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001795
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001796 case 10: // reg: VRegList(reg, reg)
1797 assert(0 && "VRegList should never be the topmost non-chain rule");
1798 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001799
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001800 case 21: // bool: Not(bool,reg): Compute with a conditional-move-on-reg
1801 { // First find the unary operand. It may be left or right, usually right.
1802 Instruction* notI = subtreeRoot->getInstruction();
1803 Value* notArg = BinaryOperator::getNotArgument(
1804 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1805 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1806
1807 // Unconditionally set register to 0
1808 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(notI));
1809
1810 // Now conditionally move 1 into the register.
1811 // Mark the register as a use (as well as a def) because the old
1812 // value will be retained if the condition is false.
1813 mvec.push_back(BuildMI(V9::MOVRZi, 3).addReg(notArg).addZImm(1)
1814 .addReg(notI, MOTy::UseAndDef));
1815
1816 break;
1817 }
1818
1819 case 421: // reg: BNot(reg,reg): Compute as reg = reg XOR-NOT 0
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001820 { // First find the unary operand. It may be left or right, usually right.
1821 Value* notArg = BinaryOperator::getNotArgument(
1822 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001823 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001824 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001825 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001826 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001827 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001828
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001829 case 322: // reg: Not(tobool, reg):
1830 // Fold CAST-TO-BOOL with NOT by inverting the sense of cast-to-bool
1831 foldCase = true;
1832 // Just fall through!
1833
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001834 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001835 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001836 Instruction* castI = subtreeRoot->getInstruction();
1837 Value* opVal = subtreeRoot->leftChild()->getValue();
1838 assert(opVal->getType()->isIntegral() ||
1839 isa<PointerType>(opVal->getType()));
1840
1841 // Unconditionally set register to 0
1842 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(castI));
1843
1844 // Now conditionally move 1 into the register.
1845 // Mark the register as a use (as well as a def) because the old
1846 // value will be retained if the condition is false.
1847 MachineOpCode opCode = foldCase? V9::MOVRZi : V9::MOVRNZi;
1848 mvec.push_back(BuildMI(opCode, 3).addReg(opVal).addZImm(1)
1849 .addReg(castI, MOTy::UseAndDef));
1850
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001851 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001852 }
1853
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001854 case 23: // reg: ToUByteTy(reg)
1855 case 24: // reg: ToSByteTy(reg)
1856 case 25: // reg: ToUShortTy(reg)
1857 case 26: // reg: ToShortTy(reg)
1858 case 27: // reg: ToUIntTy(reg)
1859 case 28: // reg: ToIntTy(reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001860 case 29: // reg: ToULongTy(reg)
1861 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001862 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001863 //======================================================================
1864 // Rules for integer conversions:
1865 //
1866 //--------
1867 // From ISO 1998 C++ Standard, Sec. 4.7:
1868 //
1869 // 2. If the destination type is unsigned, the resulting value is
1870 // the least unsigned integer congruent to the source integer
1871 // (modulo 2n where n is the number of bits used to represent the
1872 // unsigned type). [Note: In a two s complement representation,
1873 // this conversion is conceptual and there is no change in the
1874 // bit pattern (if there is no truncation). ]
1875 //
1876 // 3. If the destination type is signed, the value is unchanged if
1877 // it can be represented in the destination type (and bitfield width);
1878 // otherwise, the value is implementation-defined.
1879 //--------
1880 //
1881 // Since we assume 2s complement representations, this implies:
1882 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001883 // -- If operand is smaller than destination, zero-extend or sign-extend
1884 // according to the signedness of the *operand*: source decides:
1885 // (1) If operand is signed, sign-extend it.
1886 // If dest is unsigned, zero-ext the result!
1887 // (2) If operand is unsigned, our current invariant is that
1888 // it's high bits are correct, so zero-extension is not needed.
Vikram S. Adve94c40812002-09-27 14:33:08 +00001889 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001890 // -- If operand is same size as or larger than destination,
1891 // zero-extend or sign-extend according to the signedness of
1892 // the *destination*: destination decides:
1893 // (1) If destination is signed, sign-extend (truncating if needed)
1894 // This choice is implementation defined. We sign-extend the
1895 // operand, which matches both Sun's cc and gcc3.2.
1896 // (2) If destination is unsigned, zero-extend (truncating if needed)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001897 //======================================================================
1898
Vikram S. Adve242a8082002-05-19 15:25:51 +00001899 Instruction* destI = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001900 Function* currentFunc = destI->getParent()->getParent();
1901 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(destI);
1902
Vikram S. Adve242a8082002-05-19 15:25:51 +00001903 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001904 const Type* opType = opVal->getType();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001905 const Type* destType = destI->getType();
1906 unsigned opSize = target.getTargetData().getTypeSize(opType);
1907 unsigned destSize = target.getTargetData().getTypeSize(destType);
1908
1909 bool isIntegral = opType->isIntegral() || isa<PointerType>(opType);
1910
1911 if (opType == Type::BoolTy ||
1912 opType == destType ||
1913 isIntegral && opSize == destSize && opSize == 8) {
1914 // nothing to do in all these cases
1915 forwardOperandNum = 0; // forward first operand to user
1916
Misha Brukman7b647942003-05-30 20:11:56 +00001917 } else if (opType->isFloatingPoint()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001918
1919 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Advee895a742003-08-06 18:48:40 +00001920 if (destI->getType()->isUnsigned() && destI->getType() !=Type::UIntTy)
Misha Brukman7b647942003-05-30 20:11:56 +00001921 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve1e606692002-07-31 21:01:34 +00001922
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001923 } else if (isIntegral) {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001924
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001925 bool opSigned = opType->isSigned();
1926 bool destSigned = destType->isSigned();
1927 unsigned extSourceInBits = 8 * std::min<unsigned>(opSize, destSize);
1928
1929 assert(! (opSize == destSize && opSigned == destSigned) &&
1930 "How can different int types have same size and signedness?");
1931
1932 bool signExtend = (opSize < destSize && opSigned ||
1933 opSize >= destSize && destSigned);
1934
1935 bool signAndZeroExtend = (opSize < destSize && destSize < 8u &&
1936 opSigned && !destSigned);
1937 assert(!signAndZeroExtend || signExtend);
1938
1939 bool zeroExtendOnly = opSize >= destSize && !destSigned;
1940 assert(!zeroExtendOnly || !signExtend);
1941
1942 if (signExtend) {
1943 Value* signExtDest = (signAndZeroExtend
1944 ? new TmpInstruction(mcfi, destType, opVal)
1945 : destI);
1946
1947 target.getInstrInfo().CreateSignExtensionInstructions
1948 (target, currentFunc,opVal,signExtDest,extSourceInBits,mvec,mcfi);
1949
1950 if (signAndZeroExtend)
1951 target.getInstrInfo().CreateZeroExtensionInstructions
1952 (target, currentFunc, signExtDest, destI, 8*destSize, mvec, mcfi);
1953 }
1954 else if (zeroExtendOnly) {
1955 target.getInstrInfo().CreateZeroExtensionInstructions
1956 (target, currentFunc, opVal, destI, extSourceInBits, mvec, mcfi);
1957 }
1958 else
1959 forwardOperandNum = 0; // forward first operand to user
1960
Misha Brukman7b647942003-05-30 20:11:56 +00001961 } else
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001962 assert(0 && "Unrecognized operand type for convert-to-integer");
1963
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001964 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001965 }
1966
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001967 case 31: // reg: ToFloatTy(reg):
1968 case 32: // reg: ToDoubleTy(reg):
1969 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001970
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001971 // If this instruction has a parent (a user) in the tree
1972 // and the user is translated as an FsMULd instruction,
1973 // then the cast is unnecessary. So check that first.
1974 // In the future, we'll want to do the same for the FdMULq instruction,
1975 // so do the check here instead of only for ToFloatTy(reg).
1976 //
1977 if (subtreeRoot->parent() != NULL) {
1978 const MachineCodeForInstruction& mcfi =
1979 MachineCodeForInstruction::get(
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001980 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001981 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
1982 forwardOperandNum = 0; // forward first operand to user
1983 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001984
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001985 if (forwardOperandNum != 0) { // we do need the cast
1986 Value* leftVal = subtreeRoot->leftChild()->getValue();
1987 const Type* opType = leftVal->getType();
Vikram S. Advee895a742003-08-06 18:48:40 +00001988 MachineOpCode opCode=ChooseConvertToFloatInstr(target,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001989 subtreeRoot->getOpLabel(), opType);
Vikram S. Advee895a742003-08-06 18:48:40 +00001990 if (opCode == V9::NOP) { // no conversion needed
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001991 forwardOperandNum = 0; // forward first operand to user
1992 } else {
1993 // If the source operand is a non-FP type it must be
1994 // first copied from int to float register via memory!
1995 Instruction *dest = subtreeRoot->getInstruction();
1996 Value* srcForCast;
1997 int n = 0;
1998 if (! opType->isFloatingPoint()) {
1999 // Create a temporary to represent the FP register
2000 // into which the integer will be copied via memory.
2001 // The type of this temporary will determine the FP
2002 // register used: single-prec for a 32-bit int or smaller,
2003 // double-prec for a 64-bit int.
2004 //
2005 uint64_t srcSize =
2006 target.getTargetData().getTypeSize(leftVal->getType());
2007 Type* tmpTypeToUse =
2008 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002009 MachineCodeForInstruction &destMCFI =
2010 MachineCodeForInstruction::get(dest);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002011 srcForCast = new TmpInstruction(destMCFI, tmpTypeToUse, dest);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002012
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002013 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002014 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002015 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002016 mvec, destMCFI);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002017 } else
2018 srcForCast = leftVal;
2019
2020 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
2021 mvec.push_back(M);
2022 }
Misha Brukman7b647942003-05-30 20:11:56 +00002023 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002024 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002025
2026 case 19: // reg: ToArrayTy(reg):
2027 case 20: // reg: ToPointerTy(reg):
2028 forwardOperandNum = 0; // forward first operand to user
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002029 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002030
2031 case 233: // reg: Add(reg, Constant)
2032 maskUnsignedResult = true;
2033 M = CreateAddConstInstruction(subtreeRoot);
2034 if (M != NULL) {
2035 mvec.push_back(M);
2036 break;
2037 }
2038 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002039
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002040 case 33: // reg: Add(reg, reg)
2041 maskUnsignedResult = true;
2042 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
2043 break;
2044
2045 case 234: // reg: Sub(reg, Constant)
2046 maskUnsignedResult = true;
2047 M = CreateSubConstInstruction(subtreeRoot);
2048 if (M != NULL) {
2049 mvec.push_back(M);
2050 break;
2051 }
2052 // ELSE FALL THROUGH
2053
2054 case 34: // reg: Sub(reg, reg)
2055 maskUnsignedResult = true;
2056 Add3OperandInstr(ChooseSubInstructionByType(
Chris Lattner54e898e2003-01-15 19:23:34 +00002057 subtreeRoot->getInstruction()->getType()),
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002058 subtreeRoot, mvec);
2059 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002060
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002061 case 135: // reg: Mul(todouble, todouble)
2062 checkCast = true;
2063 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002064
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002065 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002066 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002067 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002068 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002069 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002070 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002071 Instruction* mulInstr = subtreeRoot->getInstruction();
2072 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002073 subtreeRoot->leftChild()->getValue(),
2074 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002075 mulInstr, mvec,
2076 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002077 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002078 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002079 case 335: // reg: Mul(todouble, todoubleConst)
2080 checkCast = true;
2081 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002082
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002083 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00002084 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002085 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002086 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002087 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002088 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002089 Instruction* mulInstr = subtreeRoot->getInstruction();
2090 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002091 subtreeRoot->leftChild()->getValue(),
2092 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002093 mulInstr, mvec,
2094 MachineCodeForInstruction::get(mulInstr),
2095 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002096 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002097 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002098 case 236: // reg: Div(reg, Constant)
2099 maskUnsignedResult = true;
2100 L = mvec.size();
2101 CreateDivConstInstruction(target, subtreeRoot, mvec);
2102 if (mvec.size() > L)
2103 break;
2104 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002105
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002106 case 36: // reg: Div(reg, reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002107 {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002108 maskUnsignedResult = true;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002109
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002110 // If either operand of divide is smaller than 64 bits, we have
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002111 // to make sure the unused top bits are correct because they affect
2112 // the result. These bits are already correct for unsigned values.
2113 // They may be incorrect for signed values, so sign extend to fill in.
2114 Instruction* divI = subtreeRoot->getInstruction();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002115 Value* divOp1 = subtreeRoot->leftChild()->getValue();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002116 Value* divOp2 = subtreeRoot->rightChild()->getValue();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002117 Value* divOp1ToUse = divOp1;
2118 Value* divOp2ToUse = divOp2;
2119 if (divI->getType()->isSigned()) {
2120 unsigned opSize=target.getTargetData().getTypeSize(divI->getType());
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002121 if (opSize < 8) {
2122 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(divI);
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002123 divOp1ToUse = new TmpInstruction(mcfi, divOp1);
2124 divOp2ToUse = new TmpInstruction(mcfi, divOp2);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002125 target.getInstrInfo().
2126 CreateSignExtensionInstructions(target,
2127 divI->getParent()->getParent(),
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002128 divOp1, divOp1ToUse,
2129 8*opSize, mvec, mcfi);
2130 target.getInstrInfo().
2131 CreateSignExtensionInstructions(target,
2132 divI->getParent()->getParent(),
2133 divOp2, divOp2ToUse,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002134 8*opSize, mvec, mcfi);
2135 }
2136 }
2137
2138 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002139 .addReg(divOp1ToUse)
2140 .addReg(divOp2ToUse)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002141 .addRegDef(divI));
2142
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002143 break;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002144 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002145
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002146 case 37: // reg: Rem(reg, reg)
2147 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00002148 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002149 maskUnsignedResult = true;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002150
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002151 Instruction* remI = subtreeRoot->getInstruction();
2152 Value* divOp1 = subtreeRoot->leftChild()->getValue();
2153 Value* divOp2 = subtreeRoot->rightChild()->getValue();
2154
2155 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(remI);
Vikram S. Adve510eec72001-11-04 21:59:14 +00002156
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002157 // If second operand of divide is smaller than 64 bits, we have
2158 // to make sure the unused top bits are correct because they affect
2159 // the result. These bits are already correct for unsigned values.
2160 // They may be incorrect for signed values, so sign extend to fill in.
2161 //
2162 Value* divOpToUse = divOp2;
2163 if (divOp2->getType()->isSigned()) {
2164 unsigned opSize=target.getTargetData().getTypeSize(divOp2->getType());
2165 if (opSize < 8) {
2166 divOpToUse = new TmpInstruction(mcfi, divOp2);
2167 target.getInstrInfo().
2168 CreateSignExtensionInstructions(target,
2169 remI->getParent()->getParent(),
2170 divOp2, divOpToUse,
2171 8*opSize, mvec, mcfi);
2172 }
2173 }
2174
2175 // Now compute: result = rem V1, V2 as:
2176 // result = V1 - (V1 / signExtend(V2)) * signExtend(V2)
2177 //
2178 TmpInstruction* quot = new TmpInstruction(mcfi, divOp1, divOpToUse);
2179 TmpInstruction* prod = new TmpInstruction(mcfi, quot, divOpToUse);
2180
2181 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
2182 .addReg(divOp1).addReg(divOpToUse).addRegDef(quot));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002183
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002184 mvec.push_back(BuildMI(ChooseMulInstructionByType(remI->getType()), 3)
2185 .addReg(quot).addReg(divOpToUse).addRegDef(prod));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002186
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002187 mvec.push_back(BuildMI(ChooseSubInstructionByType(remI->getType()), 3)
2188 .addReg(divOp1).addReg(prod).addRegDef(remI));
2189
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002190 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00002191 }
2192
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002193 case 38: // bool: And(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002194 case 138: // bool: And(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002195 case 238: // bool: And(bool, boolconst)
2196 case 338: // reg : BAnd(reg, reg)
2197 case 538: // reg : BAnd(reg, Constant)
2198 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
2199 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002200
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002201 case 438: // bool: BAnd(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002202 { // Use the argument of NOT as the second argument!
2203 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002204 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002205 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2206 Value* notArg = BinaryOperator::getNotArgument(
2207 cast<BinaryOperator>(notNode->getInstruction()));
2208 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002209 Value *lhs = subtreeRoot->leftChild()->getValue();
2210 Value *dest = subtreeRoot->getValue();
2211 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(lhs).addReg(notArg)
2212 .addReg(dest, MOTy::Def));
2213
2214 if (notArg->getType() == Type::BoolTy)
2215 { // set 1 in result register if result of above is non-zero
2216 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2217 .addReg(dest, MOTy::UseAndDef));
2218 }
2219
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002220 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002221 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002222
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002223 case 39: // bool: Or(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002224 case 139: // bool: Or(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002225 case 239: // bool: Or(bool, boolconst)
2226 case 339: // reg : BOr(reg, reg)
2227 case 539: // reg : BOr(reg, Constant)
2228 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
2229 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002230
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002231 case 439: // bool: BOr(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002232 { // Use the argument of NOT as the second argument!
2233 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002234 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002235 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2236 Value* notArg = BinaryOperator::getNotArgument(
2237 cast<BinaryOperator>(notNode->getInstruction()));
2238 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002239 Value *lhs = subtreeRoot->leftChild()->getValue();
2240 Value *dest = subtreeRoot->getValue();
2241
2242 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(lhs).addReg(notArg)
2243 .addReg(dest, MOTy::Def));
2244
2245 if (notArg->getType() == Type::BoolTy)
2246 { // set 1 in result register if result of above is non-zero
2247 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2248 .addReg(dest, MOTy::UseAndDef));
2249 }
2250
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002251 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002252 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002253
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002254 case 40: // bool: Xor(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002255 case 140: // bool: Xor(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002256 case 240: // bool: Xor(bool, boolconst)
2257 case 340: // reg : BXor(reg, reg)
2258 case 540: // reg : BXor(reg, Constant)
2259 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
2260 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002261
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002262 case 440: // bool: BXor(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002263 { // Use the argument of NOT as the second argument!
2264 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002265 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002266 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2267 Value* notArg = BinaryOperator::getNotArgument(
2268 cast<BinaryOperator>(notNode->getInstruction()));
2269 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002270 Value *lhs = subtreeRoot->leftChild()->getValue();
2271 Value *dest = subtreeRoot->getValue();
2272 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(lhs).addReg(notArg)
2273 .addReg(dest, MOTy::Def));
2274
2275 if (notArg->getType() == Type::BoolTy)
2276 { // set 1 in result register if result of above is non-zero
2277 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2278 .addReg(dest, MOTy::UseAndDef));
2279 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002280 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002281 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002282
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002283 case 41: // setCCconst: SetCC(reg, Constant)
2284 { // Comparison is with a constant:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002285 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002286 // If the bool result must be computed into a register (see below),
2287 // and the constant is int ZERO, we can use the MOVR[op] instructions
2288 // and avoid the SUBcc instruction entirely.
2289 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002290 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002291 // The result of the SetCC must be computed and stored in a register if
2292 // it is used outside the current basic block (so it must be computed
2293 // as a boolreg) or it is used by anything other than a branch.
2294 // We will use a conditional move to do this.
2295 //
2296 Instruction* setCCInstr = subtreeRoot->getInstruction();
2297 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2298 ! AllUsesAreBranches(setCCInstr));
2299
2300 if (computeBoolVal)
2301 {
2302 InstrTreeNode* constNode = subtreeRoot->rightChild();
2303 assert(constNode &&
2304 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
2305 Constant *constVal = cast<Constant>(constNode->getValue());
2306 bool isValidConst;
2307
2308 if ((constVal->getType()->isInteger()
2309 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00002310 && target.getInstrInfo().ConvertConstantToIntType(target,
2311 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002312 && isValidConst)
2313 {
2314 // That constant is an integer zero after all...
2315 // Use a MOVR[op] to compute the boolean result
2316 // Unconditionally set register to 0
2317 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0)
2318 .addRegDef(setCCInstr));
2319
2320 // Now conditionally move 1 into the register.
2321 // Mark the register as a use (as well as a def) because the old
2322 // value will be retained if the condition is false.
2323 MachineOpCode movOpCode = ChooseMovpregiForSetCC(subtreeRoot);
2324 mvec.push_back(BuildMI(movOpCode, 3)
2325 .addReg(subtreeRoot->leftChild()->getValue())
2326 .addZImm(1).addReg(setCCInstr, MOTy::UseAndDef));
2327
2328 break;
2329 }
2330 }
2331 // ELSE FALL THROUGH
2332 }
2333
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002334 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002335 {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002336 // This generates a SUBCC instruction, putting the difference in a
2337 // result reg. if needed, and/or setting a condition code if needed.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002338 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002339 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002340 Value* leftVal = subtreeRoot->leftChild()->getValue();
2341 Value* rightVal = subtreeRoot->rightChild()->getValue();
2342 const Type* opType = leftVal->getType();
2343 bool isFPCompare = opType->isFloatingPoint();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002344
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002345 // If the boolean result of the SetCC is used outside the current basic
2346 // block (so it must be computed as a boolreg) or is used by anything
2347 // other than a branch, the boolean must be computed and stored
2348 // in a result register. We will use a conditional move to do this.
2349 //
2350 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2351 ! AllUsesAreBranches(setCCInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002352
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002353 // A TmpInstruction is created to represent the CC "result".
2354 // Unlike other instances of TmpInstruction, this one is used
2355 // by machine code of multiple LLVM instructions, viz.,
2356 // the SetCC and the branch. Make sure to get the same one!
2357 // Note that we do this even for FP CC registers even though they
2358 // are explicit operands, because the type of the operand
2359 // needs to be a floating point condition code, not an integer
2360 // condition code. Think of this as casting the bool result to
2361 // a FP condition code register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002362 // Later, we mark the 4th operand as being a CC register, and as a def.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002363 //
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002364 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002365 setCCInstr->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00002366 leftVal->getType(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002367 MachineCodeForInstruction::get(setCCInstr));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002368
2369 // If the operands are signed values smaller than 4 bytes, then they
2370 // must be sign-extended in order to do a valid 32-bit comparison
2371 // and get the right result in the 32-bit CC register (%icc).
2372 //
2373 Value* leftOpToUse = leftVal;
2374 Value* rightOpToUse = rightVal;
2375 if (opType->isIntegral() && opType->isSigned()) {
2376 unsigned opSize = target.getTargetData().getTypeSize(opType);
2377 if (opSize < 4) {
2378 MachineCodeForInstruction& mcfi =
2379 MachineCodeForInstruction::get(setCCInstr);
2380
2381 // create temporary virtual regs. to hold the sign-extensions
2382 leftOpToUse = new TmpInstruction(mcfi, leftVal);
2383 rightOpToUse = new TmpInstruction(mcfi, rightVal);
2384
2385 // sign-extend each operand and put the result in the temporary reg.
2386 target.getInstrInfo().CreateSignExtensionInstructions
2387 (target, setCCInstr->getParent()->getParent(),
2388 leftVal, leftOpToUse, 8*opSize, mvec, mcfi);
2389 target.getInstrInfo().CreateSignExtensionInstructions
2390 (target, setCCInstr->getParent()->getParent(),
2391 rightVal, rightOpToUse, 8*opSize, mvec, mcfi);
2392 }
2393 }
2394
Misha Brukman7b647942003-05-30 20:11:56 +00002395 if (! isFPCompare) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002396 // Integer condition: set CC and discard result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002397 mvec.push_back(BuildMI(V9::SUBccr, 4)
2398 .addReg(leftOpToUse)
2399 .addReg(rightOpToUse)
2400 .addMReg(target.getRegInfo().getZeroRegNum(),MOTy::Def)
2401 .addCCReg(tmpForCC, MOTy::Def));
Misha Brukman7b647942003-05-30 20:11:56 +00002402 } else {
2403 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002404 mvec.push_back(BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2405 .addCCReg(tmpForCC, MOTy::Def)
2406 .addReg(leftOpToUse)
2407 .addReg(rightOpToUse));
Misha Brukman7b647942003-05-30 20:11:56 +00002408 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002409
Misha Brukman7b647942003-05-30 20:11:56 +00002410 if (computeBoolVal) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002411 MachineOpCode movOpCode = (isFPCompare
Misha Brukmaneecdb662003-06-02 20:55:14 +00002412 ? ChooseMovFpcciInstruction(subtreeRoot)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002413 : ChooseMovpcciForSetCC(subtreeRoot));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002414
2415 // Unconditionally set register to 0
2416 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
2417 mvec.push_back(M);
2418
2419 // Now conditionally move 1 into the register.
Misha Brukman7b647942003-05-30 20:11:56 +00002420 // Mark the register as a use (as well as a def) because the old
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002421 // value will be retained if the condition is false.
2422 M = (BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(1)
2423 .addReg(setCCInstr, MOTy::UseAndDef));
Misha Brukman7b647942003-05-30 20:11:56 +00002424 mvec.push_back(M);
2425 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002426 break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002427 }
2428
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002429 case 51: // reg: Load(reg)
2430 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002431 SetOperandsForMemInstr(ChooseLoadInstruction(
2432 subtreeRoot->getValue()->getType()),
2433 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002434 break;
2435
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002436 case 55: // reg: GetElemPtr(reg)
2437 case 56: // reg: GetElemPtrIdx(reg,reg)
2438 // If the GetElemPtr was folded into the user (parent), it will be
2439 // caught above. For other cases, we have to compute the address.
2440 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
2441 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002442
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002443 case 57: // reg: Alloca: Implement as 1 instruction:
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002444 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002445 AllocationInst* instr =
2446 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002447 unsigned tsize =
2448 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002449 assert(tsize != 0);
2450 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002451 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002452 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002453
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002454 case 58: // reg: Alloca(reg): Implement as 3 instructions:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002455 // mul num, typeSz -> tmp
2456 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002457 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002458 AllocationInst* instr =
2459 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002460 const Type* eltType = instr->getAllocatedType();
2461
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002462 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002463 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002464 Value* numElementsVal = NULL;
2465 bool isArray = instr->isArrayAllocation();
2466
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002467 if (!isArray || isa<Constant>(numElementsVal = instr->getArraySize())) {
Misha Brukman7b647942003-05-30 20:11:56 +00002468 // total size is constant: generate code for fixed-size alloca
2469 unsigned numElements = isArray?
2470 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2471 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2472 numElements, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002473 } else {
2474 // total size is not constant.
Vikram S. Adve74825322002-03-18 03:15:35 +00002475 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002476 numElementsVal, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002477 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002478 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002479 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002480
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002481 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002482 { // Generate a direct (CALL) or indirect (JMPL) call.
2483 // Mark the return-address register, the indirection
2484 // register (for indirect calls), the operands of the Call,
2485 // and the return value (if any) as implicit operands
2486 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002487 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002488 // If this is a varargs function, floating point arguments
2489 // have to passed in integer registers so insert
2490 // copy-float-to-int instructions for each float operand.
2491 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002492 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002493 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002494 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002495
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002496 // Check if this is an intrinsic function that needs a special code
2497 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002498 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002499 bool specialIntrinsic = false;
2500 LLVMIntrinsic::ID iid;
2501 if (calledFunc && (iid=(LLVMIntrinsic::ID)calledFunc->getIntrinsicID()))
2502 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002503
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002504 // If not, generate the normal call sequence for the function.
2505 // This can also handle any intrinsics that are just function calls.
2506 //
Misha Brukman7b647942003-05-30 20:11:56 +00002507 if (! specialIntrinsic) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002508 Function* currentFunc = callInstr->getParent()->getParent();
2509 MachineFunction& MF = MachineFunction::get(currentFunc);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002510 MachineCodeForInstruction& mcfi =
2511 MachineCodeForInstruction::get(callInstr);
2512 const UltraSparcRegInfo& regInfo =
2513 (UltraSparcRegInfo&) target.getRegInfo();
2514 const TargetFrameInfo& frameInfo = target.getFrameInfo();
2515
Misha Brukman7b647942003-05-30 20:11:56 +00002516 // Create hidden virtual register for return address with type void*
2517 TmpInstruction* retAddrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002518 new TmpInstruction(mcfi, PointerType::get(Type::VoidTy), callInstr);
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002519
Misha Brukman7b647942003-05-30 20:11:56 +00002520 // Generate the machine instruction and its operands.
2521 // Use CALL for direct function calls; this optimistically assumes
2522 // the PC-relative address fits in the CALL address field (22 bits).
2523 // Use JMPL for indirect calls.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002524 // This will be added to mvec later, after operand copies.
Misha Brukman7b647942003-05-30 20:11:56 +00002525 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002526 MachineInstr* callMI;
Misha Brukman7b647942003-05-30 20:11:56 +00002527 if (calledFunc) // direct function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002528 callMI = BuildMI(V9::CALL, 1).addPCDisp(callee);
Misha Brukman7b647942003-05-30 20:11:56 +00002529 else // indirect function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002530 callMI = (BuildMI(V9::JMPLCALLi,3).addReg(callee)
2531 .addSImm((int64_t)0).addRegDef(retAddrReg));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002532
Misha Brukman7b647942003-05-30 20:11:56 +00002533 const FunctionType* funcType =
2534 cast<FunctionType>(cast<PointerType>(callee->getType())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002535 ->getElementType());
Misha Brukman7b647942003-05-30 20:11:56 +00002536 bool isVarArgs = funcType->isVarArg();
2537 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002538
Misha Brukman7b647942003-05-30 20:11:56 +00002539 // Use a descriptor to pass information about call arguments
2540 // to the register allocator. This descriptor will be "owned"
2541 // and freed automatically when the MachineCodeForInstruction
2542 // object for the callInstr goes away.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002543 CallArgsDescriptor* argDesc =
2544 new CallArgsDescriptor(callInstr, retAddrReg,isVarArgs,noPrototype);
Misha Brukman7b647942003-05-30 20:11:56 +00002545 assert(callInstr->getOperand(0) == callee
2546 && "This is assumed in the loop below!");
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002547
2548 // Insert sign-extension instructions for small signed values,
2549 // if this is an unknown function (i.e., called via a funcptr)
2550 // or an external one (i.e., which may not be compiled by llc).
2551 //
2552 if (calledFunc == NULL || calledFunc->isExternal()) {
2553 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
2554 Value* argVal = callInstr->getOperand(i);
2555 const Type* argType = argVal->getType();
2556 if (argType->isIntegral() && argType->isSigned()) {
2557 unsigned argSize = target.getTargetData().getTypeSize(argType);
2558 if (argSize <= 4) {
2559 // create a temporary virtual reg. to hold the sign-extension
2560 TmpInstruction* argExtend = new TmpInstruction(mcfi, argVal);
2561
2562 // sign-extend argVal and put the result in the temporary reg.
2563 target.getInstrInfo().CreateSignExtensionInstructions
2564 (target, currentFunc, argVal, argExtend,
2565 8*argSize, mvec, mcfi);
2566
2567 // replace argVal with argExtend in CallArgsDescriptor
2568 argDesc->getArgInfo(i-1).replaceArgVal(argExtend);
2569 }
2570 }
2571 }
2572 }
2573
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002574 // Insert copy instructions to get all the arguments into
2575 // all the places that they need to be.
2576 //
Misha Brukman7b647942003-05-30 20:11:56 +00002577 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002578 int argNo = i-1;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002579 CallArgInfo& argInfo = argDesc->getArgInfo(argNo);
2580 Value* argVal = argInfo.getArgVal(); // don't use callInstr arg here
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002581 const Type* argType = argVal->getType();
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002582 unsigned regType = regInfo.getRegTypeForDataType(argType);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002583 unsigned argSize = target.getTargetData().getTypeSize(argType);
2584 int regNumForArg = TargetRegInfo::getInvalidRegNum();
2585 unsigned regClassIDOfArgReg;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002586
Misha Brukman7b647942003-05-30 20:11:56 +00002587 // Check for FP arguments to varargs functions.
2588 // Any such argument in the first $K$ args must be passed in an
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002589 // integer register. If there is no prototype, it must also
2590 // be passed as an FP register.
2591 // K = #integer argument registers.
2592 bool isFPArg = argVal->getType()->isFloatingPoint();
2593 if (isVarArgs && isFPArg) {
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002594
2595 if (noPrototype) {
2596 // It is a function with no prototype: pass value
2597 // as an FP value as well as a varargs value. The FP value
2598 // may go in a register or on the stack. The copy instruction
2599 // to the outgoing reg/stack is created by the normal argument
2600 // handling code since this is the "normal" passing mode.
2601 //
2602 regNumForArg = regInfo.regNumForFPArg(regType,
2603 false, false, argNo,
2604 regClassIDOfArgReg);
2605 if (regNumForArg == regInfo.getInvalidRegNum())
2606 argInfo.setUseStackSlot();
2607 else
2608 argInfo.setUseFPArgReg();
2609 }
2610
2611 // If this arg. is in the first $K$ regs, add special copy-
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002612 // float-to-int instructions to pass the value as an int.
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002613 // To check if it is in the first $K$, get the register
2614 // number for the arg #i. These copy instructions are
2615 // generated here because they are extra cases and not needed
2616 // for the normal argument handling (some code reuse is
2617 // possible though -- later).
2618 //
Misha Brukmanea481cc2003-06-03 03:21:58 +00002619 int copyRegNum = regInfo.regNumForIntArg(false, false, argNo,
2620 regClassIDOfArgReg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002621 if (copyRegNum != regInfo.getInvalidRegNum()) {
2622 // Create a virtual register to represent copyReg. Mark
2623 // this vreg as being an implicit operand of the call MI
2624 const Type* loadTy = (argType == Type::FloatTy
2625 ? Type::IntTy : Type::LongTy);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002626 TmpInstruction* argVReg = new TmpInstruction(mcfi, loadTy,
2627 argVal, NULL,
2628 "argRegCopy");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002629 callMI->addImplicitRef(argVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002630
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002631 // Get a temp stack location to use to copy
2632 // float-to-int via the stack.
2633 //
2634 // FIXME: For now, we allocate permanent space because
2635 // the stack frame manager does not allow locals to be
2636 // allocated (e.g., for alloca) after a temp is
2637 // allocated!
2638 //
2639 // int tmpOffset = MF.getInfo()->pushTempValue(argSize);
2640 int tmpOffset = MF.getInfo()->allocateLocalVar(argVReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002641
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002642 // Generate the store from FP reg to stack
Misha Brukmanea481cc2003-06-03 03:21:58 +00002643 unsigned StoreOpcode = ChooseStoreInstruction(argType);
2644 M = BuildMI(convertOpcodeFromRegToImm(StoreOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002645 .addReg(argVal).addMReg(regInfo.getFramePointer())
2646 .addSImm(tmpOffset);
2647 mvec.push_back(M);
2648
2649 // Generate the load from stack to int arg reg
Misha Brukmanea481cc2003-06-03 03:21:58 +00002650 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
2651 M = BuildMI(convertOpcodeFromRegToImm(LoadOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002652 .addMReg(regInfo.getFramePointer()).addSImm(tmpOffset)
2653 .addReg(argVReg, MOTy::Def);
2654
2655 // Mark operand with register it should be assigned
2656 // both for copy and for the callMI
2657 M->SetRegForOperand(M->getNumOperands()-1, copyRegNum);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002658 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2659 copyRegNum);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002660 mvec.push_back(M);
2661
2662 // Add info about the argument to the CallArgsDescriptor
2663 argInfo.setUseIntArgReg();
2664 argInfo.setArgCopy(copyRegNum);
2665 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00002666 // Cannot fit in first $K$ regs so pass arg on stack
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002667 argInfo.setUseStackSlot();
2668 }
2669 } else if (isFPArg) {
2670 // Get the outgoing arg reg to see if there is one.
2671 regNumForArg = regInfo.regNumForFPArg(regType, false, false,
2672 argNo, regClassIDOfArgReg);
2673 if (regNumForArg == regInfo.getInvalidRegNum())
2674 argInfo.setUseStackSlot();
2675 else {
2676 argInfo.setUseFPArgReg();
2677 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2678 regNumForArg);
2679 }
2680 } else {
2681 // Get the outgoing arg reg to see if there is one.
2682 regNumForArg = regInfo.regNumForIntArg(false,false,
2683 argNo, regClassIDOfArgReg);
2684 if (regNumForArg == regInfo.getInvalidRegNum())
2685 argInfo.setUseStackSlot();
2686 else {
2687 argInfo.setUseIntArgReg();
2688 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2689 regNumForArg);
2690 }
2691 }
2692
2693 //
2694 // Now insert copy instructions to stack slot or arg. register
2695 //
2696 if (argInfo.usesStackSlot()) {
2697 // Get the stack offset for this argument slot.
2698 // FP args on stack are right justified so adjust offset!
2699 // int arguments are also right justified but they are
2700 // always loaded as a full double-word so the offset does
2701 // not need to be adjusted.
2702 int argOffset = frameInfo.getOutgoingArgOffset(MF, argNo);
2703 if (argType->isFloatingPoint()) {
2704 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
2705 assert(argSize <= slotSize && "Insufficient slot size!");
2706 argOffset += slotSize - argSize;
2707 }
2708
2709 // Now generate instruction to copy argument to stack
2710 MachineOpCode storeOpCode =
2711 (argType->isFloatingPoint()
2712 ? ((argSize == 4)? V9::STFi : V9::STDFi) : V9::STXi);
2713
2714 M = BuildMI(storeOpCode, 3).addReg(argVal)
2715 .addMReg(regInfo.getStackPointer()).addSImm(argOffset);
2716 mvec.push_back(M);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002717 }
2718 else if (regNumForArg != regInfo.getInvalidRegNum()) {
2719
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002720 // Create a virtual register to represent the arg reg. Mark
2721 // this vreg as being an implicit operand of the call MI.
2722 TmpInstruction* argVReg =
2723 new TmpInstruction(mcfi, argVal, NULL, "argReg");
2724
2725 callMI->addImplicitRef(argVReg);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002726
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002727 // Generate the reg-to-reg copy into the outgoing arg reg.
2728 // -- For FP values, create a FMOVS or FMOVD instruction
2729 // -- For non-FP values, create an add-with-0 instruction
2730 if (argType->isFloatingPoint())
2731 M=(BuildMI(argType==Type::FloatTy? V9::FMOVS :V9::FMOVD,2)
2732 .addReg(argVal).addReg(argVReg, MOTy::Def));
2733 else
2734 M = (BuildMI(ChooseAddInstructionByType(argType), 3)
2735 .addReg(argVal).addSImm((int64_t) 0)
2736 .addReg(argVReg, MOTy::Def));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002737
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002738 // Mark the operand with the register it should be assigned
2739 M->SetRegForOperand(M->getNumOperands()-1, regNumForArg);
2740 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2741 regNumForArg);
2742
2743 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +00002744 }
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002745 else
2746 assert(argInfo.getArgCopy() != regInfo.getInvalidRegNum() &&
2747 "Arg. not in stack slot, primary or secondary register?");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002748 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002749
2750 // add call instruction and delay slot before copying return value
2751 mvec.push_back(callMI);
2752 mvec.push_back(BuildMI(V9::NOP, 0));
2753
Misha Brukman7b647942003-05-30 20:11:56 +00002754 // Add the return value as an implicit ref. The call operands
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002755 // were added above. Also, add code to copy out the return value.
2756 // This is always register-to-register for int or FP return values.
2757 //
2758 if (callInstr->getType() != Type::VoidTy) {
2759 // Get the return value reg.
2760 const Type* retType = callInstr->getType();
2761
2762 int regNum = (retType->isFloatingPoint()
2763 ? (unsigned) SparcFloatRegClass::f0
2764 : (unsigned) SparcIntRegClass::o0);
2765 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
2766 regNum = regInfo.getUnifiedRegNum(regClassID, regNum);
2767
2768 // Create a virtual register to represent it and mark
2769 // this vreg as being an implicit operand of the call MI
2770 TmpInstruction* retVReg =
2771 new TmpInstruction(mcfi, callInstr, NULL, "argReg");
2772
2773 callMI->addImplicitRef(retVReg, /*isDef*/ true);
2774
2775 // Generate the reg-to-reg copy from the return value reg.
2776 // -- For FP values, create a FMOVS or FMOVD instruction
2777 // -- For non-FP values, create an add-with-0 instruction
2778 if (retType->isFloatingPoint())
2779 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
2780 .addReg(retVReg).addReg(callInstr, MOTy::Def));
2781 else
2782 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
2783 .addReg(retVReg).addSImm((int64_t) 0)
2784 .addReg(callInstr, MOTy::Def));
2785
2786 // Mark the operand with the register it should be assigned
2787 // Also mark the implicit ref of the call defining this operand
2788 M->SetRegForOperand(0, regNum);
2789 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,regNum);
2790
2791 mvec.push_back(M);
2792 }
2793
Misha Brukman7b647942003-05-30 20:11:56 +00002794 // For the CALL instruction, the ret. addr. reg. is also implicit
2795 if (isa<Function>(callee))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002796 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
2797
2798 MF.getInfo()->popAllTempValues(); // free temps used for this inst
Misha Brukman7b647942003-05-30 20:11:56 +00002799 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002800
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002801 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002802 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002803
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002804 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002805 {
2806 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2807 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2808 Instruction* shlInstr = subtreeRoot->getInstruction();
2809
2810 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002811 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2812 "Shl unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002813 unsigned opSize = target.getTargetData().getTypeSize(opType);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002814
2815 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Vikram S. Advee895a742003-08-06 18:48:40 +00002816 (opSize > 4)? V9::SLLXr6:V9::SLLr5,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002817 argVal1, argVal2, 0, shlInstr, mvec,
2818 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002819 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002820 }
2821
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002822 case 63: // reg: Shr(reg, reg)
Misha Brukman7b647942003-05-30 20:11:56 +00002823 {
2824 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002825 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2826 "Shr unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002827 unsigned opSize = target.getTargetData().getTypeSize(opType);
Chris Lattner54e898e2003-01-15 19:23:34 +00002828 Add3OperandInstr(opType->isSigned()
Vikram S. Advee895a742003-08-06 18:48:40 +00002829 ? (opSize > 4? V9::SRAXr6 : V9::SRAr5)
2830 : (opSize > 4? V9::SRLXr6 : V9::SRLr5),
Chris Lattner54e898e2003-01-15 19:23:34 +00002831 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002832 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002833 }
2834
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002835 case 64: // reg: Phi(reg,reg)
2836 break; // don't forward the value
Vikram S. Adve74825322002-03-18 03:15:35 +00002837
Vikram S. Adve40dee512003-10-21 11:25:09 +00002838 case 65: // reg: VANext(reg): the va_next(va_list, type) instruction
2839 { // Increment the va_list pointer register according to the type.
2840 // All LLVM argument types are <= 64 bits, so use one doubleword.
2841 Instruction* vaNextI = subtreeRoot->getInstruction();
2842 assert(target.getTargetData().getTypeSize(vaNextI->getType()) <= 8 &&
2843 "We assumed that all LLVM parameter types <= 8 bytes!");
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002844 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
Vikram S. Adve40dee512003-10-21 11:25:09 +00002845 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaNextI->getOperand(0)).
2846 addSImm(argSize).addRegDef(vaNextI));
Vikram S. Adve472c3042003-10-21 12:28:27 +00002847 break;
Vikram S. Adve40dee512003-10-21 11:25:09 +00002848 }
2849
2850 case 66: // reg: VAArg (reg): the va_arg instruction
2851 { // Load argument from stack using current va_list pointer value.
2852 // Use 64-bit load for all non-FP args, and LDDF or double for FP.
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002853 Instruction* vaArgI = subtreeRoot->getInstruction();
Vikram S. Adve40dee512003-10-21 11:25:09 +00002854 MachineOpCode loadOp = (vaArgI->getType()->isFloatingPoint()
2855 ? (vaArgI->getType() == Type::FloatTy
2856 ? V9::LDFi : V9::LDDFi)
2857 : V9::LDXi);
Vikram S. Adve9d275142003-08-12 03:04:05 +00002858 mvec.push_back(BuildMI(loadOp, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002859 addSImm(0).addRegDef(vaArgI));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002860 break;
2861 }
2862
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002863 case 71: // reg: VReg
2864 case 72: // reg: Constant
2865 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002866
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002867 default:
2868 assert(0 && "Unrecognized BURG rule");
2869 break;
2870 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002871 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002872
Misha Brukman7b647942003-05-30 20:11:56 +00002873 if (forwardOperandNum >= 0) {
2874 // We did not generate a machine instruction but need to use operand.
2875 // If user is in the same tree, replace Value in its machine operand.
2876 // If not, insert a copy instruction which should get coalesced away
2877 // by register allocation.
2878 if (subtreeRoot->parent() != NULL)
2879 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2880 else {
2881 std::vector<MachineInstr*> minstrVec;
2882 Instruction* instr = subtreeRoot->getInstruction();
2883 target.getInstrInfo().
2884 CreateCopyInstructionsByType(target,
2885 instr->getParent()->getParent(),
2886 instr->getOperand(forwardOperandNum),
2887 instr, minstrVec,
2888 MachineCodeForInstruction::get(instr));
2889 assert(minstrVec.size() > 0);
2890 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Chris Lattner20b1ea02001-09-14 03:47:57 +00002891 }
Misha Brukman7b647942003-05-30 20:11:56 +00002892 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002893
Misha Brukman7b647942003-05-30 20:11:56 +00002894 if (maskUnsignedResult) {
2895 // If result is unsigned and smaller than int reg size,
2896 // we need to clear high bits of result value.
2897 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2898 Instruction* dest = subtreeRoot->getInstruction();
2899 if (dest->getType()->isUnsigned()) {
2900 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
2901 if (destSize <= 4) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002902 // Mask high 64 - N bits, where N = 4*destSize.
2903
2904 // Use a TmpInstruction to represent the
Misha Brukman7b647942003-05-30 20:11:56 +00002905 // intermediate result before masking. Since those instructions
2906 // have already been generated, go back and substitute tmpI
2907 // for dest in the result position of each one of them.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002908 //
2909 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(dest);
2910 TmpInstruction *tmpI = new TmpInstruction(mcfi, dest->getType(),
2911 dest, NULL, "maskHi");
2912 Value* srlArgToUse = tmpI;
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002913
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002914 unsigned numSubst = 0;
2915 for (unsigned i=0, N=mvec.size(); i < N; ++i) {
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002916
2917 // Make sure we substitute all occurrences of dest in these instrs.
2918 // Otherwise, we will have bogus code.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002919 bool someArgsWereIgnored = false;
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002920
2921 // Make sure not to substitute an upwards-exposed use -- that would
2922 // introduce a use of `tmpI' with no preceding def. Therefore,
2923 // substitute a use or def-and-use operand only if a previous def
2924 // operand has already been substituted (i.e., numSusbt > 0).
2925 //
2926 numSubst += mvec[i]->substituteValue(dest, tmpI,
2927 /*defsOnly*/ numSubst == 0,
2928 /*notDefsAndUses*/ numSubst > 0,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002929 someArgsWereIgnored);
2930 assert(!someArgsWereIgnored &&
2931 "Operand `dest' exists but not replaced: probably bogus!");
2932 }
2933 assert(numSubst > 0 && "Operand `dest' not replaced: probably bogus!");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002934
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002935 // Left shift 32-N if size (N) is less than 32 bits.
Misha Brukman452db672003-09-23 17:28:11 +00002936 // Use another tmp. virtual register to represent this result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002937 if (destSize < 4) {
2938 srlArgToUse = new TmpInstruction(mcfi, dest->getType(),
2939 tmpI, NULL, "maskHi2");
2940 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpI)
2941 .addZImm(8*(4-destSize))
2942 .addReg(srlArgToUse, MOTy::Def));
2943 }
2944
2945 // Logical right shift 32-N to get zero extension in top 64-N bits.
2946 mvec.push_back(BuildMI(V9::SRLi5, 3).addReg(srlArgToUse)
2947 .addZImm(8*(4-destSize)).addReg(dest, MOTy::Def));
2948
Misha Brukman7b647942003-05-30 20:11:56 +00002949 } else if (destSize < 8) {
2950 assert(0 && "Unsupported type size: 32 < size < 64 bits");
2951 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002952 }
Misha Brukman7b647942003-05-30 20:11:56 +00002953 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002954}