blob: 21e884be8e1b418bbe5d502ea0dd8a42efac5893 [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
Vikram S. Adve7fe27872001-10-18 00:26:20 +000014#include "SparcInstrSelectionSupport.h"
Misha Brukman34943292003-10-22 05:09:56 +000015#include "SparcInternals.h"
Vikram S. Adve74825322002-03-18 03:15:35 +000016#include "SparcRegClassInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000018#include "llvm/ConstantHandling.h"
Misha Brukman34943292003-10-22 05:09:56 +000019#include "llvm/DerivedTypes.h"
20#include "llvm/Instructions.h"
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +000021#include "llvm/Intrinsics.h"
Misha Brukman34943292003-10-22 05:09:56 +000022#include "llvm/Module.h"
23#include "llvm/CodeGen/InstrForest.h"
24#include "llvm/CodeGen/InstrSelection.h"
25#include "llvm/CodeGen/InstrSelectionSupport.h"
26#include "llvm/CodeGen/MachineCodeForInstruction.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineFunctionInfo.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000031#include "Support/MathExtras.h"
Vikram S. Adve951df2b2003-07-10 20:07:54 +000032#include <algorithm>
Misha Brukman34943292003-10-22 05:09:56 +000033#include <cmath>
Chris Lattner20b1ea02001-09-14 03:47:57 +000034
Brian Gaeked0fde302003-11-11 22:41:34 +000035namespace llvm {
36
Chris Lattner54e898e2003-01-15 19:23:34 +000037static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
Misha Brukmanee563cb2003-05-21 17:59:06 +000038 std::vector<MachineInstr*>& mvec) {
Chris Lattner54e898e2003-01-15 19:23:34 +000039 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
40 .addReg(Node->rightChild()->getValue())
41 .addRegDef(Node->getValue()));
42}
43
44
Chris Lattner795ba6c2003-01-15 21:36:50 +000045//---------------------------------------------------------------------------
46// Function: FoldGetElemChain
47//
48// Purpose:
49// Fold a chain of GetElementPtr instructions containing only
50// constant offsets into an equivalent (Pointer, IndexVector) pair.
51// Returns the pointer Value, and stores the resulting IndexVector
52// in argument chainIdxVec. This is a helper function for
53// FoldConstantIndices that does the actual folding.
54//---------------------------------------------------------------------------
55
56
57// Check for a constant 0.
58inline bool
59IsZero(Value* idx)
60{
61 return (idx == ConstantSInt::getNullValue(idx->getType()));
62}
63
64static Value*
Misha Brukmanee563cb2003-05-21 17:59:06 +000065FoldGetElemChain(InstrTreeNode* ptrNode, std::vector<Value*>& chainIdxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +000066 bool lastInstHasLeadingNonZero)
67{
68 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
69 GetElementPtrInst* gepInst =
70 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
71
72 // ptr value is not computed in this tree or ptr value does not come from GEP
73 // instruction
74 if (gepInst == NULL)
75 return NULL;
76
77 // Return NULL if we don't fold any instructions in.
78 Value* ptrVal = NULL;
79
80 // Now chase the chain of getElementInstr instructions, if any.
81 // Check for any non-constant indices and stop there.
82 // Also, stop if the first index of child is a non-zero array index
83 // and the last index of the current node is a non-array index:
84 // in that case, a non-array declared type is being accessed as an array
85 // which is not type-safe, but could be legal.
86 //
87 InstructionNode* ptrChild = gepNode;
88 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
89 ptrChild->getOpLabel() == GetElemPtrIdx))
Misha Brukman81b06862003-05-21 18:48:06 +000090 {
91 // Child is a GetElemPtr instruction
92 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
93 User::op_iterator OI, firstIdx = gepInst->idx_begin();
94 User::op_iterator lastIdx = gepInst->idx_end();
95 bool allConstantOffsets = true;
Chris Lattner795ba6c2003-01-15 21:36:50 +000096
Misha Brukman81b06862003-05-21 18:48:06 +000097 // The first index of every GEP must be an array index.
98 assert((*firstIdx)->getType() == Type::LongTy &&
99 "INTERNAL ERROR: Structure index for a pointer type!");
Chris Lattner795ba6c2003-01-15 21:36:50 +0000100
Misha Brukman81b06862003-05-21 18:48:06 +0000101 // If the last instruction had a leading non-zero index, check if the
102 // current one references a sequential (i.e., indexable) type.
103 // If not, the code is not type-safe and we would create an illegal GEP
104 // by folding them, so don't fold any more instructions.
105 //
106 if (lastInstHasLeadingNonZero)
107 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
108 break; // cannot fold in any preceding getElementPtr instrs.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000109
Misha Brukman81b06862003-05-21 18:48:06 +0000110 // Check that all offsets are constant for this instruction
111 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
112 allConstantOffsets = isa<ConstantInt>(*OI);
Chris Lattner795ba6c2003-01-15 21:36:50 +0000113
Misha Brukman81b06862003-05-21 18:48:06 +0000114 if (allConstantOffsets) {
115 // Get pointer value out of ptrChild.
116 ptrVal = gepInst->getPointerOperand();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000117
Misha Brukman81b06862003-05-21 18:48:06 +0000118 // Insert its index vector at the start, skipping any leading [0]
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000119 // Remember the old size to check if anything was inserted.
120 unsigned oldSize = chainIdxVec.size();
121 int firstIsZero = IsZero(*firstIdx);
122 chainIdxVec.insert(chainIdxVec.begin(), firstIdx + firstIsZero, lastIdx);
123
124 // Remember if it has leading zero index: it will be discarded later.
125 if (oldSize < chainIdxVec.size())
126 lastInstHasLeadingNonZero = !firstIsZero;
Chris Lattner795ba6c2003-01-15 21:36:50 +0000127
Misha Brukman81b06862003-05-21 18:48:06 +0000128 // Mark the folded node so no code is generated for it.
129 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
Chris Lattner795ba6c2003-01-15 21:36:50 +0000130
Misha Brukman81b06862003-05-21 18:48:06 +0000131 // Get the previous GEP instruction and continue trying to fold
132 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
133 } else // cannot fold this getElementPtr instr. or any preceding ones
134 break;
135 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000136
137 // If the first getElementPtr instruction had a leading [0], add it back.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000138 // Note that this instruction is the *last* one that was successfully
139 // folded *and* contributed any indices, in the loop above.
140 //
Chris Lattner795ba6c2003-01-15 21:36:50 +0000141 if (ptrVal && ! lastInstHasLeadingNonZero)
142 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
143
144 return ptrVal;
145}
146
147
148//---------------------------------------------------------------------------
149// Function: GetGEPInstArgs
150//
151// Purpose:
152// Helper function for GetMemInstArgs that handles the final getElementPtr
153// instruction used by (or same as) the memory operation.
154// Extracts the indices of the current instruction and tries to fold in
155// preceding ones if all indices of the current one are constant.
156//---------------------------------------------------------------------------
157
158static Value *
159GetGEPInstArgs(InstructionNode* gepNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000160 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000161 bool& allConstantIndices)
162{
163 allConstantIndices = true;
164 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
165
166 // Default pointer is the one from the current instruction.
167 Value* ptrVal = gepI->getPointerOperand();
168 InstrTreeNode* ptrChild = gepNode->leftChild();
169
Misha Brukman452db672003-09-23 17:28:11 +0000170 // Extract the index vector of the GEP instruction.
Chris Lattner795ba6c2003-01-15 21:36:50 +0000171 // If all indices are constant and first index is zero, try to fold
172 // in preceding GEPs with all constant indices.
173 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
174 allConstantIndices && OI != OE; ++OI)
175 if (! isa<Constant>(*OI))
176 allConstantIndices = false; // note: this also terminates loop!
177
178 // If we have only constant indices, fold chains of constant indices
179 // in this and any preceding GetElemPtr instructions.
180 bool foldedGEPs = false;
181 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
182 if (allConstantIndices)
Misha Brukman81b06862003-05-21 18:48:06 +0000183 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx)) {
184 ptrVal = newPtr;
185 foldedGEPs = true;
186 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000187
188 // Append the index vector of the current instruction.
189 // Skip the leading [0] index if preceding GEPs were folded into this.
190 idxVec.insert(idxVec.end(),
191 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
192 gepI->idx_end());
193
194 return ptrVal;
195}
196
197//---------------------------------------------------------------------------
198// Function: GetMemInstArgs
199//
200// Purpose:
201// Get the pointer value and the index vector for a memory operation
202// (GetElementPtr, Load, or Store). If all indices of the given memory
203// operation are constant, fold in constant indices in a chain of
204// preceding GetElementPtr instructions (if any), and return the
205// pointer value of the first instruction in the chain.
206// All folded instructions are marked so no code is generated for them.
207//
208// Return values:
209// Returns the pointer Value to use.
210// Returns the resulting IndexVector in idxVec.
211// Returns true/false in allConstantIndices if all indices are/aren't const.
212//---------------------------------------------------------------------------
213
214static Value*
215GetMemInstArgs(InstructionNode* memInstrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000216 std::vector<Value*>& idxVec,
Chris Lattner795ba6c2003-01-15 21:36:50 +0000217 bool& allConstantIndices)
218{
219 allConstantIndices = false;
220 Instruction* memInst = memInstrNode->getInstruction();
221 assert(idxVec.size() == 0 && "Need empty vector to return indices");
222
223 // If there is a GetElemPtr instruction to fold in to this instr,
224 // it must be in the left child for Load and GetElemPtr, and in the
225 // right child for Store instructions.
226 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
227 ? memInstrNode->rightChild()
228 : memInstrNode->leftChild());
229
230 // Default pointer is the one from the current instruction.
231 Value* ptrVal = ptrChild->getValue();
232
233 // Find the "last" GetElemPtr instruction: this one or the immediate child.
234 // There will be none if this is a load or a store from a scalar pointer.
235 InstructionNode* gepNode = NULL;
236 if (isa<GetElementPtrInst>(memInst))
237 gepNode = memInstrNode;
Misha Brukman81b06862003-05-21 18:48:06 +0000238 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal)) {
239 // Child of load/store is a GEP and memInst is its only use.
240 // Use its indices and mark it as folded.
241 gepNode = cast<InstructionNode>(ptrChild);
242 gepNode->markFoldedIntoParent();
243 }
Chris Lattner795ba6c2003-01-15 21:36:50 +0000244
245 // If there are no indices, return the current pointer.
246 // Else extract the pointer from the GEP and fold the indices.
247 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
248 : ptrVal;
249}
250
Chris Lattner54e898e2003-01-15 19:23:34 +0000251
Chris Lattner20b1ea02001-09-14 03:47:57 +0000252//************************ Internal Functions ******************************/
253
Chris Lattner20b1ea02001-09-14 03:47:57 +0000254
Chris Lattner20b1ea02001-09-14 03:47:57 +0000255static inline MachineOpCode
256ChooseBprInstruction(const InstructionNode* instrNode)
257{
258 MachineOpCode opCode;
259
260 Instruction* setCCInstr =
261 ((InstructionNode*) instrNode->leftChild())->getInstruction();
262
263 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000264 {
265 case Instruction::SetEQ: opCode = V9::BRZ; break;
266 case Instruction::SetNE: opCode = V9::BRNZ; break;
267 case Instruction::SetLE: opCode = V9::BRLEZ; break;
268 case Instruction::SetGE: opCode = V9::BRGEZ; break;
269 case Instruction::SetLT: opCode = V9::BRLZ; break;
270 case Instruction::SetGT: opCode = V9::BRGZ; break;
271 default:
272 assert(0 && "Unrecognized VM instruction!");
273 opCode = V9::INVALID_OPCODE;
274 break;
275 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000276
277 return opCode;
278}
279
280
281static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000282ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000283 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000284{
Misha Brukmana98cd452003-05-20 20:32:24 +0000285 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000286
287 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
288
Misha Brukman81b06862003-05-21 18:48:06 +0000289 if (isSigned) {
290 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000291 {
Misha Brukman81b06862003-05-21 18:48:06 +0000292 case Instruction::SetEQ: opCode = V9::BE; break;
293 case Instruction::SetNE: opCode = V9::BNE; break;
294 case Instruction::SetLE: opCode = V9::BLE; break;
295 case Instruction::SetGE: opCode = V9::BGE; break;
296 case Instruction::SetLT: opCode = V9::BL; break;
297 case Instruction::SetGT: opCode = V9::BG; break;
298 default:
299 assert(0 && "Unrecognized VM instruction!");
300 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000301 }
Misha Brukman81b06862003-05-21 18:48:06 +0000302 } else {
303 switch(setCCInstr->getOpcode())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000304 {
Misha Brukman81b06862003-05-21 18:48:06 +0000305 case Instruction::SetEQ: opCode = V9::BE; break;
306 case Instruction::SetNE: opCode = V9::BNE; break;
307 case Instruction::SetLE: opCode = V9::BLEU; break;
308 case Instruction::SetGE: opCode = V9::BCC; break;
309 case Instruction::SetLT: opCode = V9::BCS; break;
310 case Instruction::SetGT: opCode = V9::BGU; break;
311 default:
312 assert(0 && "Unrecognized VM instruction!");
313 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000314 }
Misha Brukman81b06862003-05-21 18:48:06 +0000315 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000316
317 return opCode;
318}
319
320static inline MachineOpCode
321ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000322 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000323{
Misha Brukmana98cd452003-05-20 20:32:24 +0000324 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000325
326 switch(setCCInstr->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000327 {
328 case Instruction::SetEQ: opCode = V9::FBE; break;
329 case Instruction::SetNE: opCode = V9::FBNE; break;
330 case Instruction::SetLE: opCode = V9::FBLE; break;
331 case Instruction::SetGE: opCode = V9::FBGE; break;
332 case Instruction::SetLT: opCode = V9::FBL; break;
333 case Instruction::SetGT: opCode = V9::FBG; break;
334 default:
335 assert(0 && "Unrecognized VM instruction!");
336 break;
337 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000338
339 return opCode;
340}
341
342
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000343// Create a unique TmpInstruction for a boolean value,
344// representing the CC register used by a branch on that value.
345// For now, hack this using a little static cache of TmpInstructions.
346// Eventually the entire BURG instruction selection should be put
347// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000348// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000349// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000350//
351static TmpInstruction*
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000352GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType,
353 MachineCodeForInstruction& mcfi)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000354{
Chris Lattner09ff1122002-07-24 21:21:32 +0000355 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000356 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000357 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000358
359 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
360
Misha Brukman81b06862003-05-21 18:48:06 +0000361 if (lastFunction != F) {
362 lastFunction = F;
363 boolToTmpCache.clear();
364 }
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000365
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000366 // Look for tmpI and create a new one otherwise. The new value is
367 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000368 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
369 if (tmpI == NULL)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000370 tmpI = new TmpInstruction(mcfi, ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000371
372 return tmpI;
373}
374
375
Chris Lattner20b1ea02001-09-14 03:47:57 +0000376static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000377ChooseBccInstruction(const InstructionNode* instrNode,
Vikram S. Adve786833a2003-07-06 20:13:59 +0000378 const Type*& setCCType)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000379{
380 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000381 assert(setCCNode->getOpLabel() == SetCCOp);
382 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +0000383 setCCType = setCCInstr->getOperand(0)->getType();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000384
Vikram S. Adve786833a2003-07-06 20:13:59 +0000385 if (setCCType->isFloatingPoint())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000386 return ChooseBFpccInstruction(instrNode, setCCInstr);
387 else
388 return ChooseBpccInstruction(instrNode, setCCInstr);
389}
390
391
Misha Brukmaneecdb662003-06-02 20:55:14 +0000392// WARNING: since this function has only one caller, it always returns
393// the opcode that expects an immediate and a register. If this function
394// is ever used in cases where an opcode that takes two registers is required,
395// then modify this function and use convertOpcodeFromRegToImm() where required.
396//
397// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
398// new cases of opcodes.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000399static inline MachineOpCode
Misha Brukmaneecdb662003-06-02 20:55:14 +0000400ChooseMovFpcciInstruction(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000401{
Misha Brukmana98cd452003-05-20 20:32:24 +0000402 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000403
404 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000405 {
Misha Brukmaneecdb662003-06-02 20:55:14 +0000406 case Instruction::SetEQ: opCode = V9::MOVFEi; break;
407 case Instruction::SetNE: opCode = V9::MOVFNEi; break;
408 case Instruction::SetLE: opCode = V9::MOVFLEi; break;
409 case Instruction::SetGE: opCode = V9::MOVFGEi; break;
410 case Instruction::SetLT: opCode = V9::MOVFLi; break;
411 case Instruction::SetGT: opCode = V9::MOVFGi; break;
Misha Brukman81b06862003-05-21 18:48:06 +0000412 default:
413 assert(0 && "Unrecognized VM instruction!");
414 break;
415 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000416
417 return opCode;
418}
419
420
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000421// ChooseMovpcciForSetCC -- Choose a conditional-move instruction
422// based on the type of SetCC operation.
Chris Lattner20b1ea02001-09-14 03:47:57 +0000423//
Misha Brukmaneecdb662003-06-02 20:55:14 +0000424// WARNING: since this function has only one caller, it always returns
425// the opcode that expects an immediate and a register. If this function
426// is ever used in cases where an opcode that takes two registers is required,
427// then modify this function and use convertOpcodeFromRegToImm() where required.
428//
429// It will be necessary to expand convertOpcodeFromRegToImm() to handle the
430// new cases of opcodes.
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000431//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000432static MachineOpCode
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000433ChooseMovpcciForSetCC(const InstructionNode* instrNode)
434{
435 MachineOpCode opCode = V9::INVALID_OPCODE;
436
437 const Type* opType = instrNode->leftChild()->getValue()->getType();
438 assert(opType->isIntegral() || isa<PointerType>(opType));
439 bool noSign = opType->isUnsigned() || isa<PointerType>(opType);
440
441 switch(instrNode->getInstruction()->getOpcode())
442 {
443 case Instruction::SetEQ: opCode = V9::MOVEi; break;
444 case Instruction::SetLE: opCode = noSign? V9::MOVLEUi : V9::MOVLEi; break;
445 case Instruction::SetGE: opCode = noSign? V9::MOVCCi : V9::MOVGEi; break;
446 case Instruction::SetLT: opCode = noSign? V9::MOVCSi : V9::MOVLi; break;
447 case Instruction::SetGT: opCode = noSign? V9::MOVGUi : V9::MOVGi; break;
448 case Instruction::SetNE: opCode = V9::MOVNEi; break;
449 default: assert(0 && "Unrecognized LLVM instr!"); break;
450 }
451
452 return opCode;
453}
454
455
456// ChooseMovpregiForSetCC -- Choose a conditional-move-on-register-value
457// instruction based on the type of SetCC operation. These instructions
458// compare a register with 0 and perform the move is the comparison is true.
459//
460// WARNING: like the previous function, this function it always returns
461// the opcode that expects an immediate and a register. See above.
462//
463static MachineOpCode
464ChooseMovpregiForSetCC(const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000465{
Misha Brukmana98cd452003-05-20 20:32:24 +0000466 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000467
468 switch(instrNode->getInstruction()->getOpcode())
Misha Brukman81b06862003-05-21 18:48:06 +0000469 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000470 case Instruction::SetEQ: opCode = V9::MOVRZi; break;
471 case Instruction::SetLE: opCode = V9::MOVRLEZi; break;
472 case Instruction::SetGE: opCode = V9::MOVRGEZi; break;
473 case Instruction::SetLT: opCode = V9::MOVRLZi; break;
474 case Instruction::SetGT: opCode = V9::MOVRGZi; break;
475 case Instruction::SetNE: opCode = V9::MOVRNZi; break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000476 default: assert(0 && "Unrecognized VM instr!"); break;
Misha Brukman81b06862003-05-21 18:48:06 +0000477 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000478
479 return opCode;
480}
481
Vikram S. Adve951df2b2003-07-10 20:07:54 +0000482
Chris Lattner20b1ea02001-09-14 03:47:57 +0000483static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000484ChooseConvertToFloatInstr(const TargetMachine& target,
485 OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000486{
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000487 assert((vopCode == ToFloatTy || vopCode == ToDoubleTy) &&
488 "Unrecognized convert-to-float opcode!");
Vikram S. Advee895a742003-08-06 18:48:40 +0000489 assert((opType->isIntegral() || opType->isFloatingPoint() ||
490 isa<PointerType>(opType))
491 && "Trying to convert a non-scalar type to FLOAT/DOUBLE?");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000492
Misha Brukmana98cd452003-05-20 20:32:24 +0000493 MachineOpCode opCode = V9::INVALID_OPCODE;
Vikram S. Advee895a742003-08-06 18:48:40 +0000494
495 unsigned opSize = target.getTargetData().getTypeSize(opType);
496
497 if (opType == Type::FloatTy)
498 opCode = (vopCode == ToFloatTy? V9::NOP : V9::FSTOD);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000499 else if (opType == Type::DoubleTy)
Vikram S. Advee895a742003-08-06 18:48:40 +0000500 opCode = (vopCode == ToFloatTy? V9::FDTOS : V9::NOP);
501 else if (opSize <= 4)
502 opCode = (vopCode == ToFloatTy? V9::FITOS : V9::FITOD);
503 else {
504 assert(opSize == 8 && "Unrecognized type size > 4 and < 8!");
505 opCode = (vopCode == ToFloatTy? V9::FXTOS : V9::FXTOD);
506 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000507
508 return opCode;
509}
510
511static inline MachineOpCode
Vikram S. Advee895a742003-08-06 18:48:40 +0000512ChooseConvertFPToIntInstr(const TargetMachine& target,
513 const Type* destType, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000514{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000515 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
516 && "This function should only be called for FLOAT or DOUBLE");
Vikram S. Advee895a742003-08-06 18:48:40 +0000517 assert((destType->isIntegral() || isa<PointerType>(destType))
518 && "Trying to convert FLOAT/DOUBLE to a non-scalar type?");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000519
Vikram S. Advee895a742003-08-06 18:48:40 +0000520 MachineOpCode opCode = V9::INVALID_OPCODE;
521
522 unsigned destSize = target.getTargetData().getTypeSize(destType);
523
524 if (destType == Type::UIntTy)
525 assert(destType != Type::UIntTy && "Expand FP-to-uint beforehand.");
526 else if (destSize <= 4)
Misha Brukman81b06862003-05-21 18:48:06 +0000527 opCode = (opType == Type::FloatTy)? V9::FSTOI : V9::FDTOI;
Vikram S. Advee895a742003-08-06 18:48:40 +0000528 else {
529 assert(destSize == 8 && "Unrecognized type size > 4 and < 8!");
530 opCode = (opType == Type::FloatTy)? V9::FSTOX : V9::FDTOX;
531 }
Vikram S. Adve94c40812002-09-27 14:33:08 +0000532
Chris Lattner20b1ea02001-09-14 03:47:57 +0000533 return opCode;
534}
535
Vikram S. Advee895a742003-08-06 18:48:40 +0000536static MachineInstr*
537CreateConvertFPToIntInstr(const TargetMachine& target,
538 Value* srcVal,
539 Value* destVal,
540 const Type* destType)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000541{
Vikram S. Advee895a742003-08-06 18:48:40 +0000542 MachineOpCode opCode = ChooseConvertFPToIntInstr(target, destType,
543 srcVal->getType());
Misha Brukmana98cd452003-05-20 20:32:24 +0000544 assert(opCode != V9::INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000545 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000546}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000547
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000548// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000549// The FP value must be converted to the dest type in an FP register,
550// and the result is then copied from FP to int register via memory.
Vikram S. Advee895a742003-08-06 18:48:40 +0000551// SPARC does not have a float-to-uint conversion, only a float-to-int (fdtoi).
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000552// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
Vikram S. Advee895a742003-08-06 18:48:40 +0000553// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly.
554// Therefore, for converting an FP value to uint32_t, we first need to convert
555// to uint64_t and then to uint32_t.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000556//
Vikram S. Adve1e606692002-07-31 21:01:34 +0000557static void
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000558CreateCodeToConvertFloatToInt(const TargetMachine& target,
559 Value* opVal,
560 Instruction* destI,
561 std::vector<MachineInstr*>& mvec,
562 MachineCodeForInstruction& mcfi)
Vikram S. Adve1e606692002-07-31 21:01:34 +0000563{
Vikram S. Advee895a742003-08-06 18:48:40 +0000564 Function* F = destI->getParent()->getParent();
565
Vikram S. Adve1e606692002-07-31 21:01:34 +0000566 // Create a temporary to represent the FP register into which the
567 // int value will placed after conversion. The type of this temporary
568 // depends on the type of FP register to use: single-prec for a 32-bit
569 // int or smaller; double-prec for a 64-bit int.
570 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000571 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve1e606692002-07-31 21:01:34 +0000572
Vikram S. Advee895a742003-08-06 18:48:40 +0000573 const Type* castDestType = destI->getType(); // type for the cast instr result
574 const Type* castDestRegType; // type for cast instruction result reg
575 TmpInstruction* destForCast; // dest for cast instruction
576 Instruction* fpToIntCopyDest = destI; // dest for fp-reg-to-int-reg copy instr
577
578 // For converting an FP value to uint32_t, we first need to convert to
579 // uint64_t and then to uint32_t, as explained above.
580 if (destI->getType() == Type::UIntTy) {
581 castDestType = Type::ULongTy; // use this instead of type of destI
582 castDestRegType = Type::DoubleTy; // uint64_t needs 64-bit FP register.
583 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
584 fpToIntCopyDest = new TmpInstruction(mcfi, castDestType, destForCast);
585 }
586 else {
587 castDestRegType = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
588 destForCast = new TmpInstruction(mcfi, castDestRegType, opVal);
589 }
590
591 // Create the fp-to-int conversion instruction (src and dest regs are FP regs)
592 mvec.push_back(CreateConvertFPToIntInstr(target, opVal, destForCast,
593 castDestType));
Vikram S. Adve1e606692002-07-31 21:01:34 +0000594
595 // Create the fpreg-to-intreg copy code
Vikram S. Advee895a742003-08-06 18:48:40 +0000596 target.getInstrInfo().CreateCodeToCopyFloatToInt(target, F, destForCast,
597 fpToIntCopyDest, mvec, mcfi);
598
599 // Create the uint64_t to uint32_t conversion, if needed
600 if (destI->getType() == Type::UIntTy)
601 target.getInstrInfo().
602 CreateZeroExtensionInstructions(target, F, fpToIntCopyDest, destI,
603 /*numLowBits*/ 32, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000604}
605
606
Chris Lattner20b1ea02001-09-14 03:47:57 +0000607static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000608ChooseAddInstruction(const InstructionNode* instrNode)
609{
610 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
611}
612
613
Chris Lattner20b1ea02001-09-14 03:47:57 +0000614static inline MachineInstr*
615CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000616 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000617{
Misha Brukmana98cd452003-05-20 20:32:24 +0000618 return BuildMI((resultType == Type::FloatTy) ? V9::FMOVS : V9::FMOVD, 2)
Chris Lattner00dca912003-01-15 17:47:49 +0000619 .addReg(instrNode->leftChild()->getValue())
620 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000621}
622
623static inline MachineInstr*
624CreateAddConstInstruction(const InstructionNode* instrNode)
625{
626 MachineInstr* minstr = NULL;
627
628 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000629 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000630
631 // Cases worth optimizing are:
632 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
633 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
634 //
Chris Lattner9b625032002-05-06 16:15:30 +0000635 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
Misha Brukman81b06862003-05-21 18:48:06 +0000636 double dval = FPC->getValue();
637 if (dval == 0.0)
638 minstr = CreateMovFloatInstruction(instrNode,
639 instrNode->getInstruction()->getType());
640 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000641
642 return minstr;
643}
644
645
646static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000647ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000648{
Misha Brukmana98cd452003-05-20 20:32:24 +0000649 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000650
Misha Brukman81b06862003-05-21 18:48:06 +0000651 if (resultType->isInteger() || isa<PointerType>(resultType)) {
Misha Brukman91aee472003-05-27 22:37:00 +0000652 opCode = V9::SUBr;
Misha Brukman81b06862003-05-21 18:48:06 +0000653 } else {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000654 switch(resultType->getPrimitiveID())
Misha Brukman81b06862003-05-21 18:48:06 +0000655 {
656 case Type::FloatTyID: opCode = V9::FSUBS; break;
657 case Type::DoubleTyID: opCode = V9::FSUBD; break;
658 default: assert(0 && "Invalid type for SUB instruction"); break;
659 }
660 }
661
Chris Lattner20b1ea02001-09-14 03:47:57 +0000662 return opCode;
663}
664
665
666static inline MachineInstr*
667CreateSubConstInstruction(const InstructionNode* instrNode)
668{
669 MachineInstr* minstr = NULL;
670
671 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000672 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000673
674 // Cases worth optimizing are:
675 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
676 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
677 //
Chris Lattner9b625032002-05-06 16:15:30 +0000678 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
679 double dval = FPC->getValue();
680 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000681 minstr = CreateMovFloatInstruction(instrNode,
682 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000683 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000684
685 return minstr;
686}
687
688
689static inline MachineOpCode
690ChooseFcmpInstruction(const InstructionNode* instrNode)
691{
Misha Brukmana98cd452003-05-20 20:32:24 +0000692 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000693
694 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
695 switch(operand->getType()->getPrimitiveID()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000696 case Type::FloatTyID: opCode = V9::FCMPS; break;
697 case Type::DoubleTyID: opCode = V9::FCMPD; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000698 default: assert(0 && "Invalid type for FCMP instruction"); break;
699 }
700
701 return opCode;
702}
703
704
705// Assumes that leftArg and rightArg are both cast instructions.
706//
707static inline bool
708BothFloatToDouble(const InstructionNode* instrNode)
709{
710 InstrTreeNode* leftArg = instrNode->leftChild();
711 InstrTreeNode* rightArg = instrNode->rightChild();
712 InstrTreeNode* leftArgArg = leftArg->leftChild();
713 InstrTreeNode* rightArgArg = rightArg->leftChild();
714 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
715
716 // Check if both arguments are floats cast to double
717 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000718 leftArgArg->getValue()->getType() == Type::FloatTy &&
719 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000720}
721
722
723static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000724ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000725{
Misha Brukmana98cd452003-05-20 20:32:24 +0000726 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000727
Chris Lattner0c4e8862002-09-03 01:08:28 +0000728 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000729 opCode = V9::MULXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000730 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000731 switch(resultType->getPrimitiveID())
Misha Brukman7b647942003-05-30 20:11:56 +0000732 {
733 case Type::FloatTyID: opCode = V9::FMULS; break;
734 case Type::DoubleTyID: opCode = V9::FMULD; break;
735 default: assert(0 && "Invalid type for MUL instruction"); break;
736 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000737
738 return opCode;
739}
740
741
Vikram S. Adve510eec72001-11-04 21:59:14 +0000742
Chris Lattner20b1ea02001-09-14 03:47:57 +0000743static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000744CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000745 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000746{
Misha Brukman91aee472003-05-27 22:37:00 +0000747 return BuildMI(V9::SUBr, 3).addMReg(target.getRegInfo().getZeroRegNum())
Misha Brukmana98cd452003-05-20 20:32:24 +0000748 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000749}
750
751
Vikram S. Adve242a8082002-05-19 15:25:51 +0000752// Create instruction sequence for any shift operation.
753// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
754// requires a second instruction for explicit sign-extension.
755// Note that we only have to worry about a sign-bit appearing in the
756// most significant bit of the operand after shifting (e.g., bit 32 of
757// Int or bit 16 of Short), so we do not have to worry about results
758// that are as large as a normal integer register.
759//
760static inline void
761CreateShiftInstructions(const TargetMachine& target,
762 Function* F,
763 MachineOpCode shiftOpCode,
764 Value* argVal1,
765 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000766 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000767 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000768 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000769 MachineCodeForInstruction& mcfi)
770{
771 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
772 "Large shift sizes unexpected, but can be handled below: "
773 "You need to check whether or not it fits in immed field below");
774
775 // If this is a logical left shift of a type smaller than the standard
776 // integer reg. size, we have to extend the sign-bit into upper bits
777 // of dest, so we need to put the result of the SLL into a temporary.
778 //
779 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000780 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000781
Misha Brukmand36e30e2003-06-06 09:52:23 +0000782 if ((shiftOpCode == V9::SLLr5 || shiftOpCode == V9::SLLXr6) && opSize < 8) {
Misha Brukman7b647942003-05-30 20:11:56 +0000783 // put SLL result into a temporary
Vikram S. Adved0d06ad2003-05-31 07:32:01 +0000784 shiftDest = new TmpInstruction(mcfi, argVal1, optArgVal2, "sllTmp");
Misha Brukman7b647942003-05-30 20:11:56 +0000785 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000786
787 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000788 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
789 .addReg(shiftDest, MOTy::Def)
790 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
791 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000792 mvec.push_back(M);
793
Misha Brukman7b647942003-05-30 20:11:56 +0000794 if (shiftDest != destVal) {
795 // extend the sign-bit of the result into all upper bits of dest
796 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
797 target.getInstrInfo().
798 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
799 8*opSize, mvec, mcfi);
800 }
Vikram S. Adve242a8082002-05-19 15:25:51 +0000801}
802
803
Vikram S. Adve74825322002-03-18 03:15:35 +0000804// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000805// create a cheaper instruction.
806// This returns the approximate cost of the instructions generated,
807// which is used to pick the cheapest when both operands are constant.
Vikram S. Adve645fea32003-05-25 21:59:47 +0000808static unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000809CreateMulConstInstruction(const TargetMachine &target, Function* F,
810 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000811 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000812 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000813{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000814 /* Use max. multiply cost, viz., cost of MULX */
Misha Brukman91aee472003-05-27 22:37:00 +0000815 unsigned cost = target.getInstrInfo().minLatency(V9::MULXr);
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000816 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000817
818 Value* constOp = rval;
819 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000820 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000821
822 // Cases worth optimizing are:
823 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
824 // (2) Multiply by 2^x for integer types: replace with Shift
825 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000826 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000827
Misha Brukmana98cd452003-05-20 20:32:24 +0000828 if (resultType->isInteger() || isa<PointerType>(resultType)) {
829 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +0000830 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
831 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +0000832 if (isValidConst) {
833 unsigned pow;
834 bool needNeg = false;
835 if (C < 0) {
836 needNeg = true;
837 C = -C;
838 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000839
Misha Brukmana98cd452003-05-20 20:32:24 +0000840 if (C == 0 || C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000841 cost = target.getInstrInfo().minLatency(V9::ADDr);
Misha Brukmana98cd452003-05-20 20:32:24 +0000842 unsigned Zero = target.getRegInfo().getZeroRegNum();
843 MachineInstr* M;
844 if (C == 0)
Misha Brukman91aee472003-05-27 22:37:00 +0000845 M =BuildMI(V9::ADDr,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000846 else
Misha Brukman91aee472003-05-27 22:37:00 +0000847 M = BuildMI(V9::ADDr,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
Misha Brukmana98cd452003-05-20 20:32:24 +0000848 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +0000849 } else if (isPowerOf2(C, pow)) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000850 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Misha Brukmand36e30e2003-06-06 09:52:23 +0000851 MachineOpCode opCode = (opSize <= 32)? V9::SLLr5 : V9::SLLXr6;
Misha Brukmana98cd452003-05-20 20:32:24 +0000852 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
853 destVal, mvec, mcfi);
854 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000855
Misha Brukman7b647942003-05-30 20:11:56 +0000856 if (mvec.size() > 0 && needNeg) {
857 // insert <reg = SUB 0, reg> after the instr to flip the sign
Misha Brukmana98cd452003-05-20 20:32:24 +0000858 MachineInstr* M = CreateIntNegInstruction(target, destVal);
859 mvec.push_back(M);
860 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000861 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000862 } else {
863 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
864 double dval = FPC->getValue();
865 if (fabs(dval) == 1) {
866 MachineOpCode opCode = (dval < 0)
867 ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
868 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
869 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
870 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000871 }
Misha Brukmana98cd452003-05-20 20:32:24 +0000872 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000873
Misha Brukmana98cd452003-05-20 20:32:24 +0000874 if (firstNewInstr < mvec.size()) {
875 cost = 0;
876 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
877 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
878 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000879
880 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000881}
882
883
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000884// Does not create any instructions if we cannot exploit constant to
885// create a cheaper instruction.
886//
887static inline void
888CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000889 Function* F,
890 Value* lval, Value* rval,
891 Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000892 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000893 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000894{
895 Value* constOp;
Misha Brukman7b647942003-05-30 20:11:56 +0000896 if (isa<Constant>(lval) && isa<Constant>(rval)) {
897 // both operands are constant: evaluate and "set" in dest
898 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
899 cast<Constant>(lval),
900 cast<Constant>(rval));
901 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
902 }
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000903 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000904 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000905 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000906 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000907
908 // else neither is constant
909 return;
910}
911
Vikram S. Adve74825322002-03-18 03:15:35 +0000912// Return NULL if we cannot exploit constant to create a cheaper instruction
913static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000914CreateMulInstruction(const TargetMachine &target, Function* F,
915 Value* lval, Value* rval, Instruction* destVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000916 std::vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000917 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000918 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
919{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000920 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000921 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Misha Brukmana98cd452003-05-20 20:32:24 +0000922 if (mvec.size() == L) {
923 // no instructions were added so create MUL reg, reg, reg.
924 // Use FSMULD if both operands are actually floats cast to doubles.
925 // Otherwise, use the default opcode for the appropriate type.
926 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
927 ? forceMulOp
928 : ChooseMulInstructionByType(destVal->getType()));
929 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
930 .addRegDef(destVal));
931 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000932}
933
934
Vikram S. Adve510eec72001-11-04 21:59:14 +0000935// Generate a divide instruction for Div or Rem.
936// For Rem, this assumes that the operand type will be signed if the result
937// type is signed. This is correct because they must have the same sign.
938//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000939static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000940ChooseDivInstruction(TargetMachine &target,
941 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000942{
Misha Brukmana98cd452003-05-20 20:32:24 +0000943 MachineOpCode opCode = V9::INVALID_OPCODE;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000944
945 const Type* resultType = instrNode->getInstruction()->getType();
946
Chris Lattner0c4e8862002-09-03 01:08:28 +0000947 if (resultType->isInteger())
Misha Brukman91aee472003-05-27 22:37:00 +0000948 opCode = resultType->isSigned()? V9::SDIVXr : V9::UDIVXr;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000949 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000950 switch(resultType->getPrimitiveID())
951 {
Misha Brukmana98cd452003-05-20 20:32:24 +0000952 case Type::FloatTyID: opCode = V9::FDIVS; break;
953 case Type::DoubleTyID: opCode = V9::FDIVD; break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000954 default: assert(0 && "Invalid type for DIV instruction"); break;
955 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000956
957 return opCode;
958}
959
960
Chris Lattner54e898e2003-01-15 19:23:34 +0000961// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve645fea32003-05-25 21:59:47 +0000962static void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000963CreateDivConstInstruction(TargetMachine &target,
964 const InstructionNode* instrNode,
Misha Brukmanee563cb2003-05-21 17:59:06 +0000965 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000966{
Chris Lattner54e898e2003-01-15 19:23:34 +0000967 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000968 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000969 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000970 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000971
Vikram S. Adve645fea32003-05-25 21:59:47 +0000972 Instruction* destVal = instrNode->getInstruction();
Chris Lattner54e898e2003-01-15 19:23:34 +0000973 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000974
975 // Cases worth optimizing are:
976 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
977 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
978 //
979 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000980
Misha Brukman7b647942003-05-30 20:11:56 +0000981 if (resultType->isInteger()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000982 unsigned pow;
983 bool isValidConst;
Vikram S. Advee6124d32003-07-29 19:59:23 +0000984 int64_t C = (int64_t) target.getInstrInfo().ConvertConstantToIntType(target,
985 constOp, constOp->getType(), isValidConst);
Misha Brukmana98cd452003-05-20 20:32:24 +0000986 if (isValidConst) {
987 bool needNeg = false;
988 if (C < 0) {
989 needNeg = true;
990 C = -C;
991 }
Vikram S. Advee6124d32003-07-29 19:59:23 +0000992
Misha Brukmana98cd452003-05-20 20:32:24 +0000993 if (C == 1) {
Misha Brukman91aee472003-05-27 22:37:00 +0000994 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addMReg(ZeroReg)
Vikram S. Adve645fea32003-05-25 21:59:47 +0000995 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +0000996 } else if (isPowerOf2(C, pow)) {
Vikram S. Adve645fea32003-05-25 21:59:47 +0000997 unsigned opCode;
998 Value* shiftOperand;
Vikram S. Advee895a742003-08-06 18:48:40 +0000999 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve645fea32003-05-25 21:59:47 +00001000
1001 if (resultType->isSigned()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001002 // For N / 2^k, if the operand N is negative,
1003 // we need to add (2^k - 1) before right-shifting by k, i.e.,
Vikram S. Adve645fea32003-05-25 21:59:47 +00001004 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001005 // (N / 2^k) = N >> k, if N >= 0;
1006 // (N + 2^k - 1) >> k, if N < 0
1007 //
1008 // If N is <= 32 bits, use:
1009 // sra N, 31, t1 // t1 = ~0, if N < 0, 0 else
1010 // srl t1, 32-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1011 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1012 // sra t3, k, result // result = N / 2^k
1013 //
1014 // If N is 64 bits, use:
1015 // srax N, k-1, t1 // t1 = sign bit in high k positions
1016 // srlx t1, 64-k, t2 // t2 = 2^k - 1, if N < 0, 0 else
1017 // add t2, N, t3 // t3 = N + 2^k -1, if N < 0, N else
1018 // sra t3, k, result // result = N / 2^k
1019 //
1020 TmpInstruction *sraTmp, *srlTmp, *addTmp;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001021 MachineCodeForInstruction& mcfi
1022 = MachineCodeForInstruction::get(destVal);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001023 sraTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getSign");
1024 srlTmp = new TmpInstruction(mcfi, resultType, LHS, 0, "getPlus2km1");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001025 addTmp = new TmpInstruction(mcfi, resultType, LHS, srlTmp,"incIfNeg");
Vikram S. Adve645fea32003-05-25 21:59:47 +00001026
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001027 // Create the SRA or SRAX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001028 mvec.push_back(BuildMI((opSize > 4)? V9::SRAXi6 : V9::SRAi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001029 .addReg(LHS)
1030 .addSImm((resultType==Type::LongTy)? pow-1 : 31)
1031 .addRegDef(sraTmp));
1032
Vikram S. Adve645fea32003-05-25 21:59:47 +00001033 // Create the SRL or SRLX instruction to get the sign bit
Vikram S. Advee895a742003-08-06 18:48:40 +00001034 mvec.push_back(BuildMI((opSize > 4)? V9::SRLXi6 : V9::SRLi5, 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001035 .addReg(sraTmp)
1036 .addSImm((resultType==Type::LongTy)? 64-pow : 32-pow)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001037 .addRegDef(srlTmp));
1038
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001039 // Create the ADD instruction to add 2^pow-1 for negative values
Misha Brukman91aee472003-05-27 22:37:00 +00001040 mvec.push_back(BuildMI(V9::ADDr, 3).addReg(LHS).addReg(srlTmp)
Vikram S. Adve645fea32003-05-25 21:59:47 +00001041 .addRegDef(addTmp));
1042
1043 // Get the shift operand and "right-shift" opcode to do the divide
1044 shiftOperand = addTmp;
Vikram S. Advee895a742003-08-06 18:48:40 +00001045 opCode = (opSize > 4)? V9::SRAXi6 : V9::SRAi5;
Misha Brukman7b647942003-05-30 20:11:56 +00001046 } else {
Vikram S. Adve645fea32003-05-25 21:59:47 +00001047 // Get the shift operand and "right-shift" opcode to do the divide
1048 shiftOperand = LHS;
Vikram S. Advee895a742003-08-06 18:48:40 +00001049 opCode = (opSize > 4)? V9::SRLXi6 : V9::SRLi5;
Vikram S. Adve645fea32003-05-25 21:59:47 +00001050 }
1051
1052 // Now do the actual shift!
1053 mvec.push_back(BuildMI(opCode, 3).addReg(shiftOperand).addZImm(pow)
1054 .addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001055 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001056
Misha Brukmana98cd452003-05-20 20:32:24 +00001057 if (needNeg && (C == 1 || isPowerOf2(C, pow))) {
1058 // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve645fea32003-05-25 21:59:47 +00001059 mvec.push_back(CreateIntNegInstruction(target, destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001060 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001061 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001062 } else {
1063 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
1064 double dval = FPC->getValue();
1065 if (fabs(dval) == 1) {
1066 unsigned opCode =
1067 (dval < 0) ? (resultType == Type::FloatTy? V9::FNEGS : V9::FNEGD)
1068 : (resultType == Type::FloatTy? V9::FMOVS : V9::FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001069
Vikram S. Adve645fea32003-05-25 21:59:47 +00001070 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(destVal));
Misha Brukmana98cd452003-05-20 20:32:24 +00001071 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001072 }
Misha Brukmana98cd452003-05-20 20:32:24 +00001073 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001074}
1075
1076
Vikram S. Adve74825322002-03-18 03:15:35 +00001077static void
1078CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1079 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001080 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001081 Value* numElementsVal,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001082 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001083{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001084 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001085 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001086 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001087 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001088
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001089 // Enforce the alignment constraints on the stack pointer at
1090 // compile time if the total size is a known constant.
Misha Brukman7b647942003-05-30 20:11:56 +00001091 if (isa<Constant>(numElementsVal)) {
1092 bool isValid;
Vikram S. Advee6124d32003-07-29 19:59:23 +00001093 int64_t numElem = (int64_t) target.getInstrInfo().
1094 ConvertConstantToIntType(target, numElementsVal,
1095 numElementsVal->getType(), isValid);
Misha Brukman7b647942003-05-30 20:11:56 +00001096 assert(isValid && "Unexpectedly large array dimension in alloca!");
1097 int64_t total = numElem * tsize;
1098 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1099 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1100 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1101 } else {
1102 // The size is not a constant. Generate code to compute it and
1103 // code to pad the size for stack alignment.
1104 // Create a Value to hold the (constant) element size
1105 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001106
Misha Brukman7b647942003-05-30 20:11:56 +00001107 // Create temporary values to hold the result of MUL, SLL, SRL
Vikram S. Adve80544442003-06-23 02:13:57 +00001108 // To pad `size' to next smallest multiple of 16:
1109 // size = (size + 15) & (-16 = 0xfffffffffffffff0)
1110 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001111 TmpInstruction* tmpProd = new TmpInstruction(mcfi,numElementsVal, tsizeVal);
Vikram S. Adve80544442003-06-23 02:13:57 +00001112 TmpInstruction* tmpAdd15= new TmpInstruction(mcfi,numElementsVal, tmpProd);
1113 TmpInstruction* tmpAndf0= new TmpInstruction(mcfi,numElementsVal, tmpAdd15);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001114
Misha Brukman7b647942003-05-30 20:11:56 +00001115 // Instruction 1: mul numElements, typeSize -> tmpProd
1116 // This will optimize the MUL as far as possible.
Vikram S. Adve80544442003-06-23 02:13:57 +00001117 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd, getMvec,
Misha Brukman7b647942003-05-30 20:11:56 +00001118 mcfi, INVALID_MACHINE_OPCODE);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001119
Vikram S. Adve80544442003-06-23 02:13:57 +00001120 // Instruction 2: andn tmpProd, 0x0f -> tmpAndn
1121 getMvec.push_back(BuildMI(V9::ADDi, 3).addReg(tmpProd).addSImm(15)
1122 .addReg(tmpAdd15, MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001123
Vikram S. Adve80544442003-06-23 02:13:57 +00001124 // Instruction 3: add tmpAndn, 0x10 -> tmpAdd16
1125 getMvec.push_back(BuildMI(V9::ANDi, 3).addReg(tmpAdd15).addSImm(-16)
1126 .addReg(tmpAndf0, MOTy::Def));
1127
1128 totalSizeVal = tmpAndf0;
Misha Brukman7b647942003-05-30 20:11:56 +00001129 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001130
1131 // Get the constant offset from SP for dynamically allocated storage
1132 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001133 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001134 bool growUp;
1135 ConstantSInt* dynamicAreaOffset =
1136 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001137 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001138 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1139
Chris Lattner54e898e2003-01-15 19:23:34 +00001140 unsigned SPReg = target.getRegInfo().getStackPointer();
1141
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001142 // Instruction 2: sub %sp, totalSizeVal -> %sp
Misha Brukman91aee472003-05-27 22:37:00 +00001143 getMvec.push_back(BuildMI(V9::SUBr, 3).addMReg(SPReg).addReg(totalSizeVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001144 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001145
Vikram S. Adve74825322002-03-18 03:15:35 +00001146 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Misha Brukman91aee472003-05-27 22:37:00 +00001147 getMvec.push_back(BuildMI(V9::ADDr,3).addMReg(SPReg).addReg(dynamicAreaOffset)
Misha Brukmana98cd452003-05-20 20:32:24 +00001148 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001149}
1150
1151
1152static void
1153CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1154 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001155 unsigned tsize,
1156 unsigned numElements,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001157 std::vector<MachineInstr*>& getMvec)
Vikram S. Adve74825322002-03-18 03:15:35 +00001158{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001159 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001160 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001161 "Result value is not part of a function?");
1162 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001163 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001164
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001165 // Put the variable in the dynamically sized area of the frame if either:
1166 // (a) The offset is too large to use as an immediate in load/stores
1167 // (check LDX because all load/stores have the same-size immed. field).
1168 // (b) The object is "large", so it could cause many other locals,
1169 // spills, and temporaries to have large offsets.
1170 // NOTE: We use LARGE = 8 * argSlotSize = 64 bytes.
1171 // You've gotta love having only 13 bits for constant offset values :-|.
1172 //
1173 unsigned paddedSize;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001174 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Adveea28dd32003-07-02 06:59:22 +00001175 paddedSize,
1176 tsize * numElements);
1177
1178 if (((int)paddedSize) > 8 * target.getFrameInfo().getSizeOfEachArgOnStack() ||
1179 ! target.getInstrInfo().constantFitsInImmedField(V9::LDXi,offsetFromFP)) {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001180 CreateCodeForVariableSizeAlloca(target, result, tsize,
1181 ConstantSInt::get(Type::IntTy,numElements),
1182 getMvec);
1183 return;
1184 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001185
1186 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001187 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001188
1189 // Create a temporary Value to hold the constant offset.
1190 // This is needed because it may not fit in the immediate field.
1191 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1192
1193 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001194 unsigned FPReg = target.getRegInfo().getFramePointer();
Misha Brukman91aee472003-05-27 22:37:00 +00001195 getMvec.push_back(BuildMI(V9::ADDr, 3).addMReg(FPReg).addReg(offsetVal)
Misha Brukmana98cd452003-05-20 20:32:24 +00001196 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001197}
1198
1199
Chris Lattner20b1ea02001-09-14 03:47:57 +00001200//------------------------------------------------------------------------
1201// Function SetOperandsForMemInstr
1202//
1203// Choose addressing mode for the given load or store instruction.
1204// Use [reg+reg] if it is an indexed reference, and the index offset is
1205// not a constant or if it cannot fit in the offset field.
1206// Use [reg+offset] in all other cases.
1207//
1208// This assumes that all array refs are "lowered" to one of these forms:
1209// %x = load (subarray*) ptr, constant ; single constant offset
1210// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1211// Generally, this should happen via strength reduction + LICM.
1212// Also, strength reduction should take care of using the same register for
1213// the loop index variable and an array index, when that is profitable.
1214//------------------------------------------------------------------------
1215
1216static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001217SetOperandsForMemInstr(unsigned Opcode,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001218 std::vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001219 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001220 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001221{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001222 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001223 // Index vector, ptr value, and flag if all indices are const.
Misha Brukmanee563cb2003-05-21 17:59:06 +00001224 std::vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001225 bool allConstantIndices;
1226 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001227
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001228 // Now create the appropriate operands for the machine instruction.
1229 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001230 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001231 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001232 MachineOperand::MachineOperandType offsetOpType =
1233 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001234
Vikram S. Adve74825322002-03-18 03:15:35 +00001235 // Check if there is an index vector and if so, compute the
1236 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001237 //
Misha Brukman7b647942003-05-30 20:11:56 +00001238 if (!idxVec.empty()) {
1239 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001240
Misha Brukman7b647942003-05-30 20:11:56 +00001241 // If all indices are constant, compute the combined offset directly.
1242 if (allConstantIndices) {
1243 // Compute the offset value using the index vector. Create a
1244 // virtual reg. for it since it may not fit in the immed field.
1245 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
1246 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
1247 } else {
1248 // There is at least one non-constant offset. Therefore, this must
1249 // be an array ref, and must have been lowered to a single non-zero
1250 // offset. (An extra leading zero offset, if any, can be ignored.)
1251 // Generate code sequence to compute address from index.
1252 //
1253 bool firstIdxIsZero = IsZero(idxVec[0]);
1254 assert(idxVec.size() == 1U + firstIdxIsZero
1255 && "Array refs must be lowered before Instruction Selection");
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001256
Misha Brukman7b647942003-05-30 20:11:56 +00001257 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001258
Misha Brukman7b647942003-05-30 20:11:56 +00001259 std::vector<MachineInstr*> mulVec;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001260 Instruction* addr =
1261 new TmpInstruction(MachineCodeForInstruction::get(memInst),
1262 Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001263
Misha Brukman7b647942003-05-30 20:11:56 +00001264 // Get the array type indexed by idxVal, and compute its element size.
1265 // The call to getTypeSize() will fail if size is not constant.
1266 const Type* vecType = (firstIdxIsZero
1267 ? GetElementPtrInst::getIndexedType(ptrType,
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001268 std::vector<Value*>(1U, idxVec[0]),
1269 /*AllowCompositeLeaf*/ true)
1270 : ptrType);
Misha Brukman7b647942003-05-30 20:11:56 +00001271 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
1272 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
1273 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001274
Misha Brukman7b647942003-05-30 20:11:56 +00001275 // CreateMulInstruction() folds constants intelligently enough.
1276 CreateMulInstruction(target, memInst->getParent()->getParent(),
1277 idxVal, /* lval, not likely to be const*/
1278 eltSizeVal, /* rval, likely to be constant */
1279 addr, /* result */
1280 mulVec, MachineCodeForInstruction::get(memInst),
1281 INVALID_MACHINE_OPCODE);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001282
Misha Brukman7b647942003-05-30 20:11:56 +00001283 assert(mulVec.size() > 0 && "No multiply code created?");
1284 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
1285
1286 valueForRegOffset = addr;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001287 }
Misha Brukman7b647942003-05-30 20:11:56 +00001288 } else {
1289 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1290 smallConstOffset = 0;
1291 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001292
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001293 // For STORE:
1294 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1295 // For LOAD or GET_ELEMENT_PTR,
1296 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1297 //
1298 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001299 MachineInstr *MI;
1300 if (memInst->getOpcode() == Instruction::Store) {
Misha Brukman7b647942003-05-30 20:11:56 +00001301 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001302 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1303 .addReg(ptrVal).addReg(valueForRegOffset);
Misha Brukman7b647942003-05-30 20:11:56 +00001304 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001305 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001306 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1307 .addReg(ptrVal).addSImm(smallConstOffset);
Misha Brukman91aee472003-05-27 22:37:00 +00001308 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001309 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00001310 if (offsetOpType == MachineOperand::MO_VirtualRegister) {
Chris Lattner54e898e2003-01-15 19:23:34 +00001311 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1312 .addRegDef(memInst);
Misha Brukman7b647942003-05-30 20:11:56 +00001313 } else {
Misha Brukman91aee472003-05-27 22:37:00 +00001314 Opcode = convertOpcodeFromRegToImm(Opcode);
Chris Lattner54e898e2003-01-15 19:23:34 +00001315 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1316 .addRegDef(memInst);
Misha Brukman91aee472003-05-27 22:37:00 +00001317 }
Chris Lattner54e898e2003-01-15 19:23:34 +00001318 }
1319 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001320}
1321
1322
Chris Lattner20b1ea02001-09-14 03:47:57 +00001323//
1324// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001325// in place of the use(s) of that instruction in node `parent'.
1326// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001327// Also make sure to skip over a parent who:
1328// (1) is a list node in the Burg tree, or
1329// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001330//
1331static void
1332ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001333 InstrTreeNode* parent,
1334 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001335{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001336 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1337
Chris Lattner20b1ea02001-09-14 03:47:57 +00001338 Instruction* unusedOp = treeNode->getInstruction();
1339 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001340
1341 // The parent itself may be a list node, so find the real parent instruction
1342 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1343 {
1344 parent = parent->parent();
1345 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1346 }
1347 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1348
1349 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001350 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001351
1352 // The parent's mvec would be empty if it was itself forwarded.
1353 // Recursively call ForwardOperand in that case...
1354 //
Misha Brukman7b647942003-05-30 20:11:56 +00001355 if (mvec.size() == 0) {
1356 assert(parent->parent() != NULL &&
1357 "Parent could not have been forwarded, yet has no instructions?");
1358 ForwardOperand(treeNode, parent->parent(), operandNum);
1359 } else {
1360 for (unsigned i=0, N=mvec.size(); i < N; i++) {
1361 MachineInstr* minstr = mvec[i];
1362 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i) {
1363 const MachineOperand& mop = minstr->getOperand(i);
1364 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
1365 mop.getVRegValue() == unusedOp)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001366 {
Misha Brukman7b647942003-05-30 20:11:56 +00001367 minstr->SetMachineOperandVal(i, MachineOperand::MO_VirtualRegister,
1368 fwdOp);
1369 }
1370 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001371
Misha Brukman7b647942003-05-30 20:11:56 +00001372 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
Chris Lattner907b7dc2003-08-05 16:59:24 +00001373 if (minstr->getImplicitRef(i) == unusedOp)
1374 minstr->setImplicitRef(i, fwdOp);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001375 }
Misha Brukman7b647942003-05-30 20:11:56 +00001376 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001377}
1378
1379
Vikram S. Adve242a8082002-05-19 15:25:51 +00001380inline bool
1381AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001382{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001383 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1384 UI != UE; ++UI)
1385 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1386 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1387 return false;
1388 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001389}
1390
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001391// Generate code for any intrinsic that needs a special code sequence
1392// instead of a regular call. If not that kind of intrinsic, do nothing.
1393// Returns true if code was generated, otherwise false.
1394//
Brian Gaeked0fde302003-11-11 22:41:34 +00001395bool CodeGenIntrinsic(Intrinsic::ID iid, CallInst &callInstr,
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001396 TargetMachine &target,
1397 std::vector<MachineInstr*>& mvec)
1398{
1399 switch (iid) {
Brian Gaeked0fde302003-11-11 22:41:34 +00001400 case Intrinsic::va_start: {
Vikram S. Adve40dee512003-10-21 11:25:09 +00001401 // Get the address of the first incoming vararg argument on the stack
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001402 bool ignore;
1403 Function* func = cast<Function>(callInstr.getParent()->getParent());
1404 int numFixedArgs = func->getFunctionType()->getNumParams();
1405 int fpReg = target.getFrameInfo().getIncomingArgBaseRegNum();
1406 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
1407 int firstVarArgOff = numFixedArgs * argSize + target.getFrameInfo().
1408 getFirstIncomingArgOffset(MachineFunction::get(func), ignore);
Misha Brukman91aee472003-05-27 22:37:00 +00001409 mvec.push_back(BuildMI(V9::ADDi, 3).addMReg(fpReg).addSImm(firstVarArgOff).
Vikram S. Adve40dee512003-10-21 11:25:09 +00001410 addRegDef(&callInstr));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001411 return true;
1412 }
1413
Brian Gaeked0fde302003-11-11 22:41:34 +00001414 case Intrinsic::va_end:
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001415 return true; // no-op on Sparc
1416
Brian Gaeked0fde302003-11-11 22:41:34 +00001417 case Intrinsic::va_copy:
Vikram S. Adve40dee512003-10-21 11:25:09 +00001418 // Simple copy of current va_list (arg1) to new va_list (result)
Misha Brukman91aee472003-05-27 22:37:00 +00001419 mvec.push_back(BuildMI(V9::ORr, 3).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001420 addMReg(target.getRegInfo().getZeroRegNum()).
Vikram S. Adve40dee512003-10-21 11:25:09 +00001421 addReg(callInstr.getOperand(1)).
1422 addRegDef(&callInstr));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001423 return true;
1424
Brian Gaeked0fde302003-11-11 22:41:34 +00001425 case Intrinsic::sigsetjmp:
1426 case Intrinsic::setjmp: {
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001427 // act as if we return 0
1428 unsigned g0 = target.getRegInfo().getZeroRegNum();
1429 mvec.push_back(BuildMI(V9::ORr,3).addMReg(g0).addMReg(g0)
1430 .addReg(&callInstr, MOTy::Def));
1431 return true;
1432 }
1433
Brian Gaeked0fde302003-11-11 22:41:34 +00001434 case Intrinsic::siglongjmp:
1435 case Intrinsic::longjmp: {
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001436 // call abort()
1437 Module* M = callInstr.getParent()->getParent()->getParent();
Vikram S. Adve5be74342003-09-16 05:56:22 +00001438 const FunctionType *voidvoidFuncTy =
1439 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
1440 Function *F = M->getOrInsertFunction("abort", voidvoidFuncTy);
1441 assert(F && "Unable to get or create `abort' function declaration");
1442
1443 // Create hidden virtual register for return address with type void*
1444 TmpInstruction* retAddrReg =
1445 new TmpInstruction(MachineCodeForInstruction::get(&callInstr),
1446 PointerType::get(Type::VoidTy), &callInstr);
1447
1448 // Use a descriptor to pass information about call arguments
1449 // to the register allocator. This descriptor will be "owned"
1450 // and freed automatically when the MachineCodeForInstruction
1451 // object for the callInstr goes away.
1452 CallArgsDescriptor* argDesc =
1453 new CallArgsDescriptor(&callInstr, retAddrReg, false, false);
1454
1455 MachineInstr* callMI = BuildMI(V9::CALL, 1).addPCDisp(F);
1456 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
1457
1458 mvec.push_back(callMI);
1459 mvec.push_back(BuildMI(V9::NOP, 0));
Misha Brukmanb8db66e2003-08-07 15:43:46 +00001460 return true;
1461 }
1462
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00001463 default:
1464 return false;
1465 }
1466}
1467
Vikram S. Advefb361122001-10-22 13:36:31 +00001468//******************* Externally Visible Functions *************************/
1469
Vikram S. Advefb361122001-10-22 13:36:31 +00001470//------------------------------------------------------------------------
1471// External Function: ThisIsAChainRule
1472//
1473// Purpose:
1474// Check if a given BURG rule is a chain rule.
1475//------------------------------------------------------------------------
1476
1477extern bool
1478ThisIsAChainRule(int eruleno)
1479{
1480 switch(eruleno)
1481 {
1482 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001483 case 123:
1484 case 124:
1485 case 125:
1486 case 126:
1487 case 127:
1488 case 128:
1489 case 129:
1490 case 130:
1491 case 131:
1492 case 132:
1493 case 133:
1494 case 155:
1495 case 221:
1496 case 222:
1497 case 241:
1498 case 242:
1499 case 243:
1500 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001501 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001502 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001503 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001504
Vikram S. Advefb361122001-10-22 13:36:31 +00001505 default:
1506 return false; break;
1507 }
1508}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001509
1510
1511//------------------------------------------------------------------------
1512// External Function: GetInstructionsByRule
1513//
1514// Purpose:
1515// Choose machine instructions for the SPARC according to the
1516// patterns chosen by the BURG-generated parser.
1517//------------------------------------------------------------------------
1518
Vikram S. Adve74825322002-03-18 03:15:35 +00001519void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001520GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001521 int ruleForNode,
1522 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001523 TargetMachine &target,
Misha Brukmanee563cb2003-05-21 17:59:06 +00001524 std::vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001525{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001526 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001527 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001528 int nextRule;
1529 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001530 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001531 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001532 unsigned L;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001533 bool foldCase = false;
Vikram S. Adve74825322002-03-18 03:15:35 +00001534
1535 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001536
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001537 // If the code for this instruction was folded into the parent (user),
1538 // then do nothing!
1539 if (subtreeRoot->isFoldedIntoParent())
1540 return;
1541
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001542 //
1543 // Let's check for chain rules outside the switch so that we don't have
1544 // to duplicate the list of chain rule production numbers here again
1545 //
Misha Brukmanb461d372003-10-23 16:48:30 +00001546 if (ThisIsAChainRule(ruleForNode)) {
1547 // Chain rules have a single nonterminal on the RHS.
1548 // Get the rule that matches the RHS non-terminal and use that instead.
1549 //
1550 assert(nts[0] && ! nts[1]
1551 && "A chain rule should have only one RHS non-terminal!");
1552 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1553 nts = burm_nts[nextRule];
1554 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
1555 } else {
1556 switch(ruleForNode) {
1557 case 1: // stmt: Ret
1558 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001559 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001560 // for moving return value to appropriate register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001561 // Copy the return value to the required return register.
1562 // Mark the return Value as an implicit ref of the RET instr..
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001563 // Mark the return-address register as a hidden virtual reg.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001564 // Finally put a NOP in the delay slot.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001565 ReturnInst *returnInstr=cast<ReturnInst>(subtreeRoot->getInstruction());
1566 Value* retVal = returnInstr->getReturnValue();
1567 MachineCodeForInstruction& mcfi =
1568 MachineCodeForInstruction::get(returnInstr);
1569
1570 // Create a hidden virtual reg to represent the return address register
1571 // used by the machine instruction but not represented in LLVM.
1572 //
1573 Instruction* returnAddrTmp = new TmpInstruction(mcfi, returnInstr);
1574
1575 MachineInstr* retMI =
1576 BuildMI(V9::JMPLRETi, 3).addReg(returnAddrTmp).addSImm(8)
Misha Brukmana98cd452003-05-20 20:32:24 +00001577 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001578
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001579 // If there is a value to return, we need to:
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001580 // (a) Sign-extend the value if it is smaller than 8 bytes (reg size)
1581 // (b) Insert a copy to copy the return value to the appropriate reg.
1582 // -- For FP values, create a FMOVS or FMOVD instruction
1583 // -- For non-FP values, create an add-with-0 instruction
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001584 //
1585 if (retVal != NULL) {
1586 const UltraSparcRegInfo& regInfo =
1587 (UltraSparcRegInfo&) target.getRegInfo();
1588 const Type* retType = retVal->getType();
1589 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
1590 unsigned retRegNum = (retType->isFloatingPoint()
1591 ? (unsigned) SparcFloatRegClass::f0
1592 : (unsigned) SparcIntRegClass::i0);
1593 retRegNum = regInfo.getUnifiedRegNum(regClassID, retRegNum);
1594
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001595 // () Insert sign-extension instructions for small signed values.
1596 //
1597 Value* retValToUse = retVal;
1598 if (retType->isIntegral() && retType->isSigned()) {
1599 unsigned retSize = target.getTargetData().getTypeSize(retType);
1600 if (retSize <= 4) {
1601 // create a temporary virtual reg. to hold the sign-extension
1602 retValToUse = new TmpInstruction(mcfi, retVal);
1603
1604 // sign-extend retVal and put the result in the temporary reg.
1605 target.getInstrInfo().CreateSignExtensionInstructions
1606 (target, returnInstr->getParent()->getParent(),
1607 retVal, retValToUse, 8*retSize, mvec, mcfi);
1608 }
1609 }
1610
1611 // (b) Now, insert a copy to to the appropriate register:
1612 // -- For FP values, create a FMOVS or FMOVD instruction
1613 // -- For non-FP values, create an add-with-0 instruction
1614 //
1615 // First, create a virtual register to represent the register and
1616 // mark this vreg as being an implicit operand of the ret MI.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001617 TmpInstruction* retVReg =
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001618 new TmpInstruction(mcfi, retValToUse, NULL, "argReg");
1619
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001620 retMI->addImplicitRef(retVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00001621
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001622 if (retType->isFloatingPoint())
1623 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001624 .addReg(retValToUse).addReg(retVReg, MOTy::Def));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001625 else
1626 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001627 .addReg(retValToUse).addSImm((int64_t) 0)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001628 .addReg(retVReg, MOTy::Def));
1629
1630 // Mark the operand with the register it should be assigned
1631 M->SetRegForOperand(M->getNumOperands()-1, retRegNum);
1632 retMI->SetRegForImplicitRef(retMI->getNumImplicitRefs()-1, retRegNum);
1633
1634 mvec.push_back(M);
1635 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001636
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001637 // Now insert the RET instruction and a NOP for the delay slot
1638 mvec.push_back(retMI);
Misha Brukmana98cd452003-05-20 20:32:24 +00001639 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001640
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001641 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001642 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001643
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001644 case 3: // stmt: Store(reg,reg)
1645 case 4: // stmt: Store(reg,ptrreg)
1646 SetOperandsForMemInstr(ChooseStoreInstruction(
Chris Lattner54e898e2003-01-15 19:23:34 +00001647 subtreeRoot->leftChild()->getValue()->getType()),
1648 mvec, subtreeRoot, target);
Misha Brukmanb3fabe02003-05-31 06:22:37 +00001649 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001650
1651 case 5: // stmt: BrUncond
1652 {
1653 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1654 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(0)));
1655
1656 // delay slot
1657 mvec.push_back(BuildMI(V9::NOP, 0));
1658 break;
1659 }
1660
1661 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001662 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001663 // If the constant is ZERO, we can use the branch-on-integer-register
1664 // instructions and avoid the SUBcc instruction entirely.
1665 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001666 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001667 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1668 assert(constNode &&
1669 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001670 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001671 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001672
Chris Lattner0c4e8862002-09-03 01:08:28 +00001673 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001674 || isa<PointerType>(constVal->getType()))
Vikram S. Advee6124d32003-07-29 19:59:23 +00001675 && target.getInstrInfo().ConvertConstantToIntType(target,
1676 constVal, constVal->getType(), isValidConst) == 0
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001677 && isValidConst)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001678 {
1679 // That constant is a zero after all...
1680 // Use the left child of setCC as the first argument!
1681 // Mark the setCC node so that no code is generated for it.
1682 InstructionNode* setCCNode = (InstructionNode*)
1683 subtreeRoot->leftChild();
1684 assert(setCCNode->getOpLabel() == SetCCOp);
1685 setCCNode->markFoldedIntoParent();
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001686
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001687 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001688
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001689 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1690 .addReg(setCCNode->leftChild()->getValue())
1691 .addPCDisp(brInst->getSuccessor(0));
1692 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001693
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001694 // delay slot
1695 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001696
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001697 // false branch
1698 mvec.push_back(BuildMI(V9::BA, 1)
1699 .addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001700
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001701 // delay slot
1702 mvec.push_back(BuildMI(V9::NOP, 0));
1703 break;
1704 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001705 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001706 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001707
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001708 case 6: // stmt: BrCond(setCC)
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001709 { // bool => boolean was computed with SetCC.
1710 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001711 // If it is an integer CC, we also need to find the unique
1712 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001713 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001714 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve786833a2003-07-06 20:13:59 +00001715 const Type* setCCType;
1716 unsigned Opcode = ChooseBccInstruction(subtreeRoot, setCCType);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001717 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1718 brInst->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00001719 setCCType,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001720 MachineCodeForInstruction::get(brInst));
Chris Lattner54e898e2003-01-15 19:23:34 +00001721 M = BuildMI(Opcode, 2).addCCReg(ccValue)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001722 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001723 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001724
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001725 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001726 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001727
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001728 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001729 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001730
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001731 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001732 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001733 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001734 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001735
1736 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001737 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001738 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001739 Constant* constVal =
1740 cast<Constant>(subtreeRoot->leftChild()->getValue());
1741 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001742
Misha Brukmana98cd452003-05-20 20:32:24 +00001743 M = BuildMI(V9::BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001744 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001745 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001746
1747 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001748 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001749 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001750 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001751
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001752 case 8: // stmt: BrCond(boolreg)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001753 { // boolreg => boolean is recorded in an integer register.
1754 // Use branch-on-integer-register instruction.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001755 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001756 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
Misha Brukmana98cd452003-05-20 20:32:24 +00001757 M = BuildMI(V9::BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
Chris Lattner54e898e2003-01-15 19:23:34 +00001758 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001759 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001760
1761 // delay slot
Misha Brukmana98cd452003-05-20 20:32:24 +00001762 mvec.push_back(BuildMI(V9::NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001763
1764 // false branch
Misha Brukmana98cd452003-05-20 20:32:24 +00001765 mvec.push_back(BuildMI(V9::BA, 1).addPCDisp(BI->getSuccessor(1)));
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 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001771
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001772 case 9: // stmt: Switch(reg)
1773 assert(0 && "*** SWITCH instruction is not implemented yet.");
1774 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001775
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001776 case 10: // reg: VRegList(reg, reg)
1777 assert(0 && "VRegList should never be the topmost non-chain rule");
1778 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001779
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001780 case 21: // bool: Not(bool,reg): Compute with a conditional-move-on-reg
1781 { // First find the unary operand. It may be left or right, usually right.
1782 Instruction* notI = subtreeRoot->getInstruction();
1783 Value* notArg = BinaryOperator::getNotArgument(
1784 cast<BinaryOperator>(subtreeRoot->getInstruction()));
1785 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1786
1787 // Unconditionally set register to 0
1788 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(notI));
1789
1790 // Now conditionally move 1 into the register.
1791 // Mark the register as a use (as well as a def) because the old
1792 // value will be retained if the condition is false.
1793 mvec.push_back(BuildMI(V9::MOVRZi, 3).addReg(notArg).addZImm(1)
1794 .addReg(notI, MOTy::UseAndDef));
1795
1796 break;
1797 }
1798
1799 case 421: // reg: BNot(reg,reg): Compute as reg = reg XOR-NOT 0
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001800 { // First find the unary operand. It may be left or right, usually right.
1801 Value* notArg = BinaryOperator::getNotArgument(
1802 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001803 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Misha Brukman91aee472003-05-27 22:37:00 +00001804 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(notArg).addMReg(ZeroReg)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001805 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001806 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001807 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001808
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001809 case 322: // reg: Not(tobool, reg):
1810 // Fold CAST-TO-BOOL with NOT by inverting the sense of cast-to-bool
1811 foldCase = true;
1812 // Just fall through!
1813
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001814 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001815 {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001816 Instruction* castI = subtreeRoot->getInstruction();
1817 Value* opVal = subtreeRoot->leftChild()->getValue();
1818 assert(opVal->getType()->isIntegral() ||
1819 isa<PointerType>(opVal->getType()));
1820
1821 // Unconditionally set register to 0
1822 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(castI));
1823
1824 // Now conditionally move 1 into the register.
1825 // Mark the register as a use (as well as a def) because the old
1826 // value will be retained if the condition is false.
1827 MachineOpCode opCode = foldCase? V9::MOVRZi : V9::MOVRNZi;
1828 mvec.push_back(BuildMI(opCode, 3).addReg(opVal).addZImm(1)
1829 .addReg(castI, MOTy::UseAndDef));
1830
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001831 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001832 }
1833
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001834 case 23: // reg: ToUByteTy(reg)
1835 case 24: // reg: ToSByteTy(reg)
1836 case 25: // reg: ToUShortTy(reg)
1837 case 26: // reg: ToShortTy(reg)
1838 case 27: // reg: ToUIntTy(reg)
1839 case 28: // reg: ToIntTy(reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001840 case 29: // reg: ToULongTy(reg)
1841 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001842 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001843 //======================================================================
1844 // Rules for integer conversions:
1845 //
1846 //--------
1847 // From ISO 1998 C++ Standard, Sec. 4.7:
1848 //
1849 // 2. If the destination type is unsigned, the resulting value is
1850 // the least unsigned integer congruent to the source integer
1851 // (modulo 2n where n is the number of bits used to represent the
1852 // unsigned type). [Note: In a two s complement representation,
1853 // this conversion is conceptual and there is no change in the
1854 // bit pattern (if there is no truncation). ]
1855 //
1856 // 3. If the destination type is signed, the value is unchanged if
1857 // it can be represented in the destination type (and bitfield width);
1858 // otherwise, the value is implementation-defined.
1859 //--------
1860 //
1861 // Since we assume 2s complement representations, this implies:
1862 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001863 // -- If operand is smaller than destination, zero-extend or sign-extend
1864 // according to the signedness of the *operand*: source decides:
1865 // (1) If operand is signed, sign-extend it.
1866 // If dest is unsigned, zero-ext the result!
1867 // (2) If operand is unsigned, our current invariant is that
1868 // it's high bits are correct, so zero-extension is not needed.
Vikram S. Adve94c40812002-09-27 14:33:08 +00001869 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001870 // -- If operand is same size as or larger than destination,
1871 // zero-extend or sign-extend according to the signedness of
1872 // the *destination*: destination decides:
1873 // (1) If destination is signed, sign-extend (truncating if needed)
1874 // This choice is implementation defined. We sign-extend the
1875 // operand, which matches both Sun's cc and gcc3.2.
1876 // (2) If destination is unsigned, zero-extend (truncating if needed)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001877 //======================================================================
1878
Vikram S. Adve242a8082002-05-19 15:25:51 +00001879 Instruction* destI = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001880 Function* currentFunc = destI->getParent()->getParent();
1881 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(destI);
1882
Vikram S. Adve242a8082002-05-19 15:25:51 +00001883 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001884 const Type* opType = opVal->getType();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001885 const Type* destType = destI->getType();
1886 unsigned opSize = target.getTargetData().getTypeSize(opType);
1887 unsigned destSize = target.getTargetData().getTypeSize(destType);
1888
1889 bool isIntegral = opType->isIntegral() || isa<PointerType>(opType);
1890
1891 if (opType == Type::BoolTy ||
1892 opType == destType ||
1893 isIntegral && opSize == destSize && opSize == 8) {
1894 // nothing to do in all these cases
1895 forwardOperandNum = 0; // forward first operand to user
1896
Misha Brukman7b647942003-05-30 20:11:56 +00001897 } else if (opType->isFloatingPoint()) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001898
1899 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec, mcfi);
Vikram S. Advee895a742003-08-06 18:48:40 +00001900 if (destI->getType()->isUnsigned() && destI->getType() !=Type::UIntTy)
Misha Brukman7b647942003-05-30 20:11:56 +00001901 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve1e606692002-07-31 21:01:34 +00001902
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001903 } else if (isIntegral) {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001904
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001905 bool opSigned = opType->isSigned();
1906 bool destSigned = destType->isSigned();
1907 unsigned extSourceInBits = 8 * std::min<unsigned>(opSize, destSize);
1908
1909 assert(! (opSize == destSize && opSigned == destSigned) &&
1910 "How can different int types have same size and signedness?");
1911
1912 bool signExtend = (opSize < destSize && opSigned ||
1913 opSize >= destSize && destSigned);
1914
1915 bool signAndZeroExtend = (opSize < destSize && destSize < 8u &&
1916 opSigned && !destSigned);
1917 assert(!signAndZeroExtend || signExtend);
1918
1919 bool zeroExtendOnly = opSize >= destSize && !destSigned;
1920 assert(!zeroExtendOnly || !signExtend);
1921
1922 if (signExtend) {
1923 Value* signExtDest = (signAndZeroExtend
1924 ? new TmpInstruction(mcfi, destType, opVal)
1925 : destI);
1926
1927 target.getInstrInfo().CreateSignExtensionInstructions
1928 (target, currentFunc,opVal,signExtDest,extSourceInBits,mvec,mcfi);
1929
1930 if (signAndZeroExtend)
1931 target.getInstrInfo().CreateZeroExtensionInstructions
1932 (target, currentFunc, signExtDest, destI, 8*destSize, mvec, mcfi);
1933 }
1934 else if (zeroExtendOnly) {
1935 target.getInstrInfo().CreateZeroExtensionInstructions
1936 (target, currentFunc, opVal, destI, extSourceInBits, mvec, mcfi);
1937 }
1938 else
1939 forwardOperandNum = 0; // forward first operand to user
1940
Misha Brukman7b647942003-05-30 20:11:56 +00001941 } else
Vikram S. Adve951df2b2003-07-10 20:07:54 +00001942 assert(0 && "Unrecognized operand type for convert-to-integer");
1943
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001944 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001945 }
1946
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001947 case 31: // reg: ToFloatTy(reg):
1948 case 32: // reg: ToDoubleTy(reg):
1949 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001950
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001951 // If this instruction has a parent (a user) in the tree
1952 // and the user is translated as an FsMULd instruction,
1953 // then the cast is unnecessary. So check that first.
1954 // In the future, we'll want to do the same for the FdMULq instruction,
1955 // so do the check here instead of only for ToFloatTy(reg).
1956 //
1957 if (subtreeRoot->parent() != NULL) {
1958 const MachineCodeForInstruction& mcfi =
1959 MachineCodeForInstruction::get(
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001960 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001961 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == V9::FSMULD)
1962 forwardOperandNum = 0; // forward first operand to user
1963 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001964
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001965 if (forwardOperandNum != 0) { // we do need the cast
1966 Value* leftVal = subtreeRoot->leftChild()->getValue();
1967 const Type* opType = leftVal->getType();
Vikram S. Advee895a742003-08-06 18:48:40 +00001968 MachineOpCode opCode=ChooseConvertToFloatInstr(target,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001969 subtreeRoot->getOpLabel(), opType);
Vikram S. Advee895a742003-08-06 18:48:40 +00001970 if (opCode == V9::NOP) { // no conversion needed
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001971 forwardOperandNum = 0; // forward first operand to user
1972 } else {
1973 // If the source operand is a non-FP type it must be
1974 // first copied from int to float register via memory!
1975 Instruction *dest = subtreeRoot->getInstruction();
1976 Value* srcForCast;
1977 int n = 0;
1978 if (! opType->isFloatingPoint()) {
1979 // Create a temporary to represent the FP register
1980 // into which the integer will be copied via memory.
1981 // The type of this temporary will determine the FP
1982 // register used: single-prec for a 32-bit int or smaller,
1983 // double-prec for a 64-bit int.
1984 //
1985 uint64_t srcSize =
1986 target.getTargetData().getTypeSize(leftVal->getType());
1987 Type* tmpTypeToUse =
1988 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001989 MachineCodeForInstruction &destMCFI =
1990 MachineCodeForInstruction::get(dest);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00001991 srcForCast = new TmpInstruction(destMCFI, tmpTypeToUse, dest);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001992
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001993 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001994 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001995 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001996 mvec, destMCFI);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00001997 } else
1998 srcForCast = leftVal;
1999
2000 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
2001 mvec.push_back(M);
2002 }
Misha Brukman7b647942003-05-30 20:11:56 +00002003 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002004 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002005
2006 case 19: // reg: ToArrayTy(reg):
2007 case 20: // reg: ToPointerTy(reg):
2008 forwardOperandNum = 0; // forward first operand to user
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002009 break;
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002010
2011 case 233: // reg: Add(reg, Constant)
2012 maskUnsignedResult = true;
2013 M = CreateAddConstInstruction(subtreeRoot);
2014 if (M != NULL) {
2015 mvec.push_back(M);
2016 break;
2017 }
2018 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002019
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002020 case 33: // reg: Add(reg, reg)
2021 maskUnsignedResult = true;
2022 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
2023 break;
2024
2025 case 234: // reg: Sub(reg, Constant)
2026 maskUnsignedResult = true;
2027 M = CreateSubConstInstruction(subtreeRoot);
2028 if (M != NULL) {
2029 mvec.push_back(M);
2030 break;
2031 }
2032 // ELSE FALL THROUGH
2033
2034 case 34: // reg: Sub(reg, reg)
2035 maskUnsignedResult = true;
2036 Add3OperandInstr(ChooseSubInstructionByType(
Chris Lattner54e898e2003-01-15 19:23:34 +00002037 subtreeRoot->getInstruction()->getType()),
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002038 subtreeRoot, mvec);
2039 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002040
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002041 case 135: // reg: Mul(todouble, todouble)
2042 checkCast = true;
2043 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002044
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002045 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002046 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002047 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002048 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002049 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002050 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002051 Instruction* mulInstr = subtreeRoot->getInstruction();
2052 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002053 subtreeRoot->leftChild()->getValue(),
2054 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002055 mulInstr, mvec,
2056 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002057 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002058 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002059 case 335: // reg: Mul(todouble, todoubleConst)
2060 checkCast = true;
2061 // FALL THROUGH
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002062
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002063 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00002064 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002065 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00002066 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
Misha Brukmana98cd452003-05-20 20:32:24 +00002067 ? V9::FSMULD
Vikram S. Adve74825322002-03-18 03:15:35 +00002068 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002069 Instruction* mulInstr = subtreeRoot->getInstruction();
2070 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00002071 subtreeRoot->leftChild()->getValue(),
2072 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00002073 mulInstr, mvec,
2074 MachineCodeForInstruction::get(mulInstr),
2075 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002076 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00002077 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002078 case 236: // reg: Div(reg, Constant)
2079 maskUnsignedResult = true;
2080 L = mvec.size();
2081 CreateDivConstInstruction(target, subtreeRoot, mvec);
2082 if (mvec.size() > L)
2083 break;
2084 // ELSE FALL THROUGH
Misha Brukmanb3fabe02003-05-31 06:22:37 +00002085
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002086 case 36: // reg: Div(reg, reg)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002087 {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002088 maskUnsignedResult = true;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002089
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002090 // If either operand of divide is smaller than 64 bits, we have
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002091 // to make sure the unused top bits are correct because they affect
2092 // the result. These bits are already correct for unsigned values.
2093 // They may be incorrect for signed values, so sign extend to fill in.
2094 Instruction* divI = subtreeRoot->getInstruction();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002095 Value* divOp1 = subtreeRoot->leftChild()->getValue();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002096 Value* divOp2 = subtreeRoot->rightChild()->getValue();
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002097 Value* divOp1ToUse = divOp1;
2098 Value* divOp2ToUse = divOp2;
2099 if (divI->getType()->isSigned()) {
2100 unsigned opSize=target.getTargetData().getTypeSize(divI->getType());
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002101 if (opSize < 8) {
2102 MachineCodeForInstruction& mcfi=MachineCodeForInstruction::get(divI);
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002103 divOp1ToUse = new TmpInstruction(mcfi, divOp1);
2104 divOp2ToUse = new TmpInstruction(mcfi, divOp2);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002105 target.getInstrInfo().
2106 CreateSignExtensionInstructions(target,
2107 divI->getParent()->getParent(),
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002108 divOp1, divOp1ToUse,
2109 8*opSize, mvec, mcfi);
2110 target.getInstrInfo().
2111 CreateSignExtensionInstructions(target,
2112 divI->getParent()->getParent(),
2113 divOp2, divOp2ToUse,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002114 8*opSize, mvec, mcfi);
2115 }
2116 }
2117
2118 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
Vikram S. Adve97e02eb2003-08-01 15:54:38 +00002119 .addReg(divOp1ToUse)
2120 .addReg(divOp2ToUse)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002121 .addRegDef(divI));
2122
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002123 break;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002124 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002125
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002126 case 37: // reg: Rem(reg, reg)
2127 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00002128 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002129 maskUnsignedResult = true;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002130
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002131 Instruction* remI = subtreeRoot->getInstruction();
2132 Value* divOp1 = subtreeRoot->leftChild()->getValue();
2133 Value* divOp2 = subtreeRoot->rightChild()->getValue();
2134
2135 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(remI);
Vikram S. Adve510eec72001-11-04 21:59:14 +00002136
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002137 // If second operand of divide is smaller than 64 bits, we have
2138 // to make sure the unused top bits are correct because they affect
2139 // the result. These bits are already correct for unsigned values.
2140 // They may be incorrect for signed values, so sign extend to fill in.
2141 //
2142 Value* divOpToUse = divOp2;
2143 if (divOp2->getType()->isSigned()) {
2144 unsigned opSize=target.getTargetData().getTypeSize(divOp2->getType());
2145 if (opSize < 8) {
2146 divOpToUse = new TmpInstruction(mcfi, divOp2);
2147 target.getInstrInfo().
2148 CreateSignExtensionInstructions(target,
2149 remI->getParent()->getParent(),
2150 divOp2, divOpToUse,
2151 8*opSize, mvec, mcfi);
2152 }
2153 }
2154
2155 // Now compute: result = rem V1, V2 as:
2156 // result = V1 - (V1 / signExtend(V2)) * signExtend(V2)
2157 //
2158 TmpInstruction* quot = new TmpInstruction(mcfi, divOp1, divOpToUse);
2159 TmpInstruction* prod = new TmpInstruction(mcfi, quot, divOpToUse);
2160
2161 mvec.push_back(BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
2162 .addReg(divOp1).addReg(divOpToUse).addRegDef(quot));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002163
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002164 mvec.push_back(BuildMI(ChooseMulInstructionByType(remI->getType()), 3)
2165 .addReg(quot).addReg(divOpToUse).addRegDef(prod));
Vikram S. Adve510eec72001-11-04 21:59:14 +00002166
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002167 mvec.push_back(BuildMI(ChooseSubInstructionByType(remI->getType()), 3)
2168 .addReg(divOp1).addReg(prod).addRegDef(remI));
2169
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002170 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00002171 }
2172
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002173 case 38: // bool: And(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002174 case 138: // bool: And(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002175 case 238: // bool: And(bool, boolconst)
2176 case 338: // reg : BAnd(reg, reg)
2177 case 538: // reg : BAnd(reg, Constant)
2178 Add3OperandInstr(V9::ANDr, subtreeRoot, mvec);
2179 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002180
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002181 case 438: // bool: BAnd(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002182 { // Use the argument of NOT as the second argument!
2183 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002184 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002185 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2186 Value* notArg = BinaryOperator::getNotArgument(
2187 cast<BinaryOperator>(notNode->getInstruction()));
2188 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002189 Value *lhs = subtreeRoot->leftChild()->getValue();
2190 Value *dest = subtreeRoot->getValue();
2191 mvec.push_back(BuildMI(V9::ANDNr, 3).addReg(lhs).addReg(notArg)
2192 .addReg(dest, MOTy::Def));
2193
Misha Brukmanb461d372003-10-23 16:48:30 +00002194 if (notArg->getType() == Type::BoolTy) {
2195 // set 1 in result register if result of above is non-zero
2196 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2197 .addReg(dest, MOTy::UseAndDef));
2198 }
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002199
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002200 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002201 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002202
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002203 case 39: // bool: Or(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002204 case 139: // bool: Or(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002205 case 239: // bool: Or(bool, boolconst)
2206 case 339: // reg : BOr(reg, reg)
2207 case 539: // reg : BOr(reg, Constant)
2208 Add3OperandInstr(V9::ORr, subtreeRoot, mvec);
2209 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002210
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002211 case 439: // bool: BOr(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002212 { // Use the argument of NOT as the second argument!
2213 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002214 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002215 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2216 Value* notArg = BinaryOperator::getNotArgument(
2217 cast<BinaryOperator>(notNode->getInstruction()));
2218 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002219 Value *lhs = subtreeRoot->leftChild()->getValue();
2220 Value *dest = subtreeRoot->getValue();
2221
2222 mvec.push_back(BuildMI(V9::ORNr, 3).addReg(lhs).addReg(notArg)
2223 .addReg(dest, MOTy::Def));
2224
Misha Brukmanb461d372003-10-23 16:48:30 +00002225 if (notArg->getType() == Type::BoolTy) {
2226 // set 1 in result register if result of above is non-zero
2227 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2228 .addReg(dest, MOTy::UseAndDef));
2229 }
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002230
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002231 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002232 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002233
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002234 case 40: // bool: Xor(bool, bool)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002235 case 140: // bool: Xor(bool, not)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002236 case 240: // bool: Xor(bool, boolconst)
2237 case 340: // reg : BXor(reg, reg)
2238 case 540: // reg : BXor(reg, Constant)
2239 Add3OperandInstr(V9::XORr, subtreeRoot, mvec);
2240 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002241
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002242 case 440: // bool: BXor(bool, bnot)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002243 { // Use the argument of NOT as the second argument!
2244 // Mark the NOT node so that no code is generated for it.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002245 // If the type is boolean, set 1 or 0 in the result register.
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002246 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
2247 Value* notArg = BinaryOperator::getNotArgument(
2248 cast<BinaryOperator>(notNode->getInstruction()));
2249 notNode->markFoldedIntoParent();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002250 Value *lhs = subtreeRoot->leftChild()->getValue();
2251 Value *dest = subtreeRoot->getValue();
2252 mvec.push_back(BuildMI(V9::XNORr, 3).addReg(lhs).addReg(notArg)
2253 .addReg(dest, MOTy::Def));
2254
Misha Brukmanb461d372003-10-23 16:48:30 +00002255 if (notArg->getType() == Type::BoolTy) {
2256 // set 1 in result register if result of above is non-zero
2257 mvec.push_back(BuildMI(V9::MOVRNZi, 3).addReg(dest).addZImm(1)
2258 .addReg(dest, MOTy::UseAndDef));
2259 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002260 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00002261 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002262
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002263 case 41: // setCCconst: SetCC(reg, Constant)
2264 { // Comparison is with a constant:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002265 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002266 // If the bool result must be computed into a register (see below),
2267 // and the constant is int ZERO, we can use the MOVR[op] instructions
2268 // and avoid the SUBcc instruction entirely.
2269 // Otherwise this is just the same as case 42, so just fall through.
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002270 //
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002271 // The result of the SetCC must be computed and stored in a register if
2272 // it is used outside the current basic block (so it must be computed
2273 // as a boolreg) or it is used by anything other than a branch.
2274 // We will use a conditional move to do this.
2275 //
2276 Instruction* setCCInstr = subtreeRoot->getInstruction();
2277 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2278 ! AllUsesAreBranches(setCCInstr));
2279
Misha Brukmanb461d372003-10-23 16:48:30 +00002280 if (computeBoolVal) {
2281 InstrTreeNode* constNode = subtreeRoot->rightChild();
2282 assert(constNode &&
2283 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
2284 Constant *constVal = cast<Constant>(constNode->getValue());
2285 bool isValidConst;
2286
2287 if ((constVal->getType()->isInteger()
2288 || isa<PointerType>(constVal->getType()))
2289 && target.getInstrInfo().ConvertConstantToIntType(target,
Vikram S. Advee6124d32003-07-29 19:59:23 +00002290 constVal, constVal->getType(), isValidConst) == 0
Misha Brukmanb461d372003-10-23 16:48:30 +00002291 && isValidConst)
2292 {
2293 // That constant is an integer zero after all...
2294 // Use a MOVR[op] to compute the boolean result
2295 // Unconditionally set register to 0
2296 mvec.push_back(BuildMI(V9::SETHI, 2).addZImm(0)
2297 .addRegDef(setCCInstr));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002298
Misha Brukmanb461d372003-10-23 16:48:30 +00002299 // Now conditionally move 1 into the register.
2300 // Mark the register as a use (as well as a def) because the old
2301 // value will be retained if the condition is false.
2302 MachineOpCode movOpCode = ChooseMovpregiForSetCC(subtreeRoot);
2303 mvec.push_back(BuildMI(movOpCode, 3)
2304 .addReg(subtreeRoot->leftChild()->getValue())
2305 .addZImm(1).addReg(setCCInstr, MOTy::UseAndDef));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002306
Misha Brukmanb461d372003-10-23 16:48:30 +00002307 break;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002308 }
Misha Brukmanb461d372003-10-23 16:48:30 +00002309 }
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002310 // ELSE FALL THROUGH
2311 }
2312
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002313 case 42: // bool: SetCC(reg, reg):
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002314 {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002315 // This generates a SUBCC instruction, putting the difference in a
2316 // result reg. if needed, and/or setting a condition code if needed.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002317 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002318 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002319 Value* leftVal = subtreeRoot->leftChild()->getValue();
2320 Value* rightVal = subtreeRoot->rightChild()->getValue();
2321 const Type* opType = leftVal->getType();
2322 bool isFPCompare = opType->isFloatingPoint();
Vikram S. Adve242a8082002-05-19 15:25:51 +00002323
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002324 // If the boolean result of the SetCC is used outside the current basic
2325 // block (so it must be computed as a boolreg) or is used by anything
2326 // other than a branch, the boolean must be computed and stored
2327 // in a result register. We will use a conditional move to do this.
2328 //
2329 bool computeBoolVal = (subtreeRoot->parent() == NULL ||
2330 ! AllUsesAreBranches(setCCInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002331
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002332 // A TmpInstruction is created to represent the CC "result".
2333 // Unlike other instances of TmpInstruction, this one is used
2334 // by machine code of multiple LLVM instructions, viz.,
2335 // the SetCC and the branch. Make sure to get the same one!
2336 // Note that we do this even for FP CC registers even though they
2337 // are explicit operands, because the type of the operand
2338 // needs to be a floating point condition code, not an integer
2339 // condition code. Think of this as casting the bool result to
2340 // a FP condition code register.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002341 // Later, we mark the 4th operand as being a CC register, and as a def.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002342 //
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002343 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002344 setCCInstr->getParent()->getParent(),
Vikram S. Adve786833a2003-07-06 20:13:59 +00002345 leftVal->getType(),
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002346 MachineCodeForInstruction::get(setCCInstr));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002347
2348 // If the operands are signed values smaller than 4 bytes, then they
2349 // must be sign-extended in order to do a valid 32-bit comparison
2350 // and get the right result in the 32-bit CC register (%icc).
2351 //
2352 Value* leftOpToUse = leftVal;
2353 Value* rightOpToUse = rightVal;
2354 if (opType->isIntegral() && opType->isSigned()) {
2355 unsigned opSize = target.getTargetData().getTypeSize(opType);
2356 if (opSize < 4) {
2357 MachineCodeForInstruction& mcfi =
2358 MachineCodeForInstruction::get(setCCInstr);
2359
2360 // create temporary virtual regs. to hold the sign-extensions
2361 leftOpToUse = new TmpInstruction(mcfi, leftVal);
2362 rightOpToUse = new TmpInstruction(mcfi, rightVal);
2363
2364 // sign-extend each operand and put the result in the temporary reg.
2365 target.getInstrInfo().CreateSignExtensionInstructions
2366 (target, setCCInstr->getParent()->getParent(),
2367 leftVal, leftOpToUse, 8*opSize, mvec, mcfi);
2368 target.getInstrInfo().CreateSignExtensionInstructions
2369 (target, setCCInstr->getParent()->getParent(),
2370 rightVal, rightOpToUse, 8*opSize, mvec, mcfi);
2371 }
2372 }
2373
Misha Brukman7b647942003-05-30 20:11:56 +00002374 if (! isFPCompare) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002375 // Integer condition: set CC and discard result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002376 mvec.push_back(BuildMI(V9::SUBccr, 4)
2377 .addReg(leftOpToUse)
2378 .addReg(rightOpToUse)
2379 .addMReg(target.getRegInfo().getZeroRegNum(),MOTy::Def)
2380 .addCCReg(tmpForCC, MOTy::Def));
Misha Brukman7b647942003-05-30 20:11:56 +00002381 } else {
2382 // FP condition: dest of FCMP should be some FCCn register
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002383 mvec.push_back(BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2384 .addCCReg(tmpForCC, MOTy::Def)
2385 .addReg(leftOpToUse)
2386 .addReg(rightOpToUse));
Misha Brukman7b647942003-05-30 20:11:56 +00002387 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002388
Misha Brukman7b647942003-05-30 20:11:56 +00002389 if (computeBoolVal) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002390 MachineOpCode movOpCode = (isFPCompare
Misha Brukmaneecdb662003-06-02 20:55:14 +00002391 ? ChooseMovFpcciInstruction(subtreeRoot)
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002392 : ChooseMovpcciForSetCC(subtreeRoot));
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002393
2394 // Unconditionally set register to 0
2395 M = BuildMI(V9::SETHI, 2).addZImm(0).addRegDef(setCCInstr);
2396 mvec.push_back(M);
2397
2398 // Now conditionally move 1 into the register.
Misha Brukman7b647942003-05-30 20:11:56 +00002399 // Mark the register as a use (as well as a def) because the old
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002400 // value will be retained if the condition is false.
2401 M = (BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(1)
2402 .addReg(setCCInstr, MOTy::UseAndDef));
Misha Brukman7b647942003-05-30 20:11:56 +00002403 mvec.push_back(M);
2404 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002405 break;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002406 }
2407
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002408 case 51: // reg: Load(reg)
2409 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002410 SetOperandsForMemInstr(ChooseLoadInstruction(
2411 subtreeRoot->getValue()->getType()),
2412 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002413 break;
2414
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002415 case 55: // reg: GetElemPtr(reg)
2416 case 56: // reg: GetElemPtrIdx(reg,reg)
2417 // If the GetElemPtr was folded into the user (parent), it will be
2418 // caught above. For other cases, we have to compute the address.
2419 SetOperandsForMemInstr(V9::ADDr, mvec, subtreeRoot, target);
2420 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002421
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002422 case 57: // reg: Alloca: Implement as 1 instruction:
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002423 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002424 AllocationInst* instr =
2425 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002426 unsigned tsize =
2427 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002428 assert(tsize != 0);
2429 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002430 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002431 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002432
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002433 case 58: // reg: Alloca(reg): Implement as 3 instructions:
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002434 // mul num, typeSz -> tmp
2435 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002436 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002437 AllocationInst* instr =
2438 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002439 const Type* eltType = instr->getAllocatedType();
2440
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002441 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002442 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002443 Value* numElementsVal = NULL;
2444 bool isArray = instr->isArrayAllocation();
2445
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002446 if (!isArray || isa<Constant>(numElementsVal = instr->getArraySize())) {
Misha Brukman7b647942003-05-30 20:11:56 +00002447 // total size is constant: generate code for fixed-size alloca
2448 unsigned numElements = isArray?
2449 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2450 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2451 numElements, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002452 } else {
2453 // total size is not constant.
Vikram S. Adve74825322002-03-18 03:15:35 +00002454 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002455 numElementsVal, mvec);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002456 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002457 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002458 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002459
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002460 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002461 { // Generate a direct (CALL) or indirect (JMPL) call.
2462 // Mark the return-address register, the indirection
2463 // register (for indirect calls), the operands of the Call,
2464 // and the return value (if any) as implicit operands
2465 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002466 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002467 // If this is a varargs function, floating point arguments
2468 // have to passed in integer registers so insert
2469 // copy-float-to-int instructions for each float operand.
2470 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002471 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002472 Value *callee = callInstr->getCalledValue();
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002473 Function* calledFunc = dyn_cast<Function>(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002474
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002475 // Check if this is an intrinsic function that needs a special code
2476 // sequence (e.g., va_start). Indirect calls cannot be special.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002477 //
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002478 bool specialIntrinsic = false;
Brian Gaeked0fde302003-11-11 22:41:34 +00002479 Intrinsic::ID iid;
2480 if (calledFunc && (iid=(Intrinsic::ID)calledFunc->getIntrinsicID()))
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002481 specialIntrinsic = CodeGenIntrinsic(iid, *callInstr, target, mvec);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002482
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002483 // If not, generate the normal call sequence for the function.
2484 // This can also handle any intrinsics that are just function calls.
2485 //
Misha Brukman7b647942003-05-30 20:11:56 +00002486 if (! specialIntrinsic) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002487 Function* currentFunc = callInstr->getParent()->getParent();
2488 MachineFunction& MF = MachineFunction::get(currentFunc);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002489 MachineCodeForInstruction& mcfi =
2490 MachineCodeForInstruction::get(callInstr);
2491 const UltraSparcRegInfo& regInfo =
2492 (UltraSparcRegInfo&) target.getRegInfo();
2493 const TargetFrameInfo& frameInfo = target.getFrameInfo();
2494
Misha Brukman7b647942003-05-30 20:11:56 +00002495 // Create hidden virtual register for return address with type void*
2496 TmpInstruction* retAddrReg =
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002497 new TmpInstruction(mcfi, PointerType::get(Type::VoidTy), callInstr);
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002498
Misha Brukman7b647942003-05-30 20:11:56 +00002499 // Generate the machine instruction and its operands.
2500 // Use CALL for direct function calls; this optimistically assumes
2501 // the PC-relative address fits in the CALL address field (22 bits).
2502 // Use JMPL for indirect calls.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002503 // This will be added to mvec later, after operand copies.
Misha Brukman7b647942003-05-30 20:11:56 +00002504 //
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002505 MachineInstr* callMI;
Misha Brukman7b647942003-05-30 20:11:56 +00002506 if (calledFunc) // direct function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002507 callMI = BuildMI(V9::CALL, 1).addPCDisp(callee);
Misha Brukman7b647942003-05-30 20:11:56 +00002508 else // indirect function call
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002509 callMI = (BuildMI(V9::JMPLCALLi,3).addReg(callee)
2510 .addSImm((int64_t)0).addRegDef(retAddrReg));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002511
Misha Brukman7b647942003-05-30 20:11:56 +00002512 const FunctionType* funcType =
2513 cast<FunctionType>(cast<PointerType>(callee->getType())
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002514 ->getElementType());
Misha Brukman7b647942003-05-30 20:11:56 +00002515 bool isVarArgs = funcType->isVarArg();
2516 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002517
Misha Brukman7b647942003-05-30 20:11:56 +00002518 // Use a descriptor to pass information about call arguments
2519 // to the register allocator. This descriptor will be "owned"
2520 // and freed automatically when the MachineCodeForInstruction
2521 // object for the callInstr goes away.
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002522 CallArgsDescriptor* argDesc =
2523 new CallArgsDescriptor(callInstr, retAddrReg,isVarArgs,noPrototype);
Misha Brukman7b647942003-05-30 20:11:56 +00002524 assert(callInstr->getOperand(0) == callee
2525 && "This is assumed in the loop below!");
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002526
2527 // Insert sign-extension instructions for small signed values,
2528 // if this is an unknown function (i.e., called via a funcptr)
2529 // or an external one (i.e., which may not be compiled by llc).
2530 //
2531 if (calledFunc == NULL || calledFunc->isExternal()) {
2532 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
2533 Value* argVal = callInstr->getOperand(i);
2534 const Type* argType = argVal->getType();
2535 if (argType->isIntegral() && argType->isSigned()) {
2536 unsigned argSize = target.getTargetData().getTypeSize(argType);
2537 if (argSize <= 4) {
2538 // create a temporary virtual reg. to hold the sign-extension
2539 TmpInstruction* argExtend = new TmpInstruction(mcfi, argVal);
2540
2541 // sign-extend argVal and put the result in the temporary reg.
2542 target.getInstrInfo().CreateSignExtensionInstructions
2543 (target, currentFunc, argVal, argExtend,
2544 8*argSize, mvec, mcfi);
2545
2546 // replace argVal with argExtend in CallArgsDescriptor
2547 argDesc->getArgInfo(i-1).replaceArgVal(argExtend);
2548 }
2549 }
2550 }
2551 }
2552
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002553 // Insert copy instructions to get all the arguments into
2554 // all the places that they need to be.
2555 //
Misha Brukman7b647942003-05-30 20:11:56 +00002556 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i) {
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002557 int argNo = i-1;
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002558 CallArgInfo& argInfo = argDesc->getArgInfo(argNo);
2559 Value* argVal = argInfo.getArgVal(); // don't use callInstr arg here
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002560 const Type* argType = argVal->getType();
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002561 unsigned regType = regInfo.getRegTypeForDataType(argType);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002562 unsigned argSize = target.getTargetData().getTypeSize(argType);
2563 int regNumForArg = TargetRegInfo::getInvalidRegNum();
2564 unsigned regClassIDOfArgReg;
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002565
Misha Brukman7b647942003-05-30 20:11:56 +00002566 // Check for FP arguments to varargs functions.
2567 // Any such argument in the first $K$ args must be passed in an
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002568 // integer register. If there is no prototype, it must also
2569 // be passed as an FP register.
2570 // K = #integer argument registers.
2571 bool isFPArg = argVal->getType()->isFloatingPoint();
2572 if (isVarArgs && isFPArg) {
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002573
2574 if (noPrototype) {
2575 // It is a function with no prototype: pass value
2576 // as an FP value as well as a varargs value. The FP value
2577 // may go in a register or on the stack. The copy instruction
2578 // to the outgoing reg/stack is created by the normal argument
2579 // handling code since this is the "normal" passing mode.
2580 //
2581 regNumForArg = regInfo.regNumForFPArg(regType,
2582 false, false, argNo,
2583 regClassIDOfArgReg);
2584 if (regNumForArg == regInfo.getInvalidRegNum())
2585 argInfo.setUseStackSlot();
2586 else
2587 argInfo.setUseFPArgReg();
2588 }
2589
2590 // If this arg. is in the first $K$ regs, add special copy-
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002591 // float-to-int instructions to pass the value as an int.
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002592 // To check if it is in the first $K$, get the register
2593 // number for the arg #i. These copy instructions are
2594 // generated here because they are extra cases and not needed
2595 // for the normal argument handling (some code reuse is
2596 // possible though -- later).
2597 //
Misha Brukmanea481cc2003-06-03 03:21:58 +00002598 int copyRegNum = regInfo.regNumForIntArg(false, false, argNo,
2599 regClassIDOfArgReg);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002600 if (copyRegNum != regInfo.getInvalidRegNum()) {
2601 // Create a virtual register to represent copyReg. Mark
2602 // this vreg as being an implicit operand of the call MI
2603 const Type* loadTy = (argType == Type::FloatTy
2604 ? Type::IntTy : Type::LongTy);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002605 TmpInstruction* argVReg = new TmpInstruction(mcfi, loadTy,
2606 argVal, NULL,
2607 "argRegCopy");
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002608 callMI->addImplicitRef(argVReg);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002609
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002610 // Get a temp stack location to use to copy
2611 // float-to-int via the stack.
2612 //
2613 // FIXME: For now, we allocate permanent space because
2614 // the stack frame manager does not allow locals to be
2615 // allocated (e.g., for alloca) after a temp is
2616 // allocated!
2617 //
2618 // int tmpOffset = MF.getInfo()->pushTempValue(argSize);
2619 int tmpOffset = MF.getInfo()->allocateLocalVar(argVReg);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002620
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002621 // Generate the store from FP reg to stack
Misha Brukmanea481cc2003-06-03 03:21:58 +00002622 unsigned StoreOpcode = ChooseStoreInstruction(argType);
2623 M = BuildMI(convertOpcodeFromRegToImm(StoreOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002624 .addReg(argVal).addMReg(regInfo.getFramePointer())
2625 .addSImm(tmpOffset);
2626 mvec.push_back(M);
2627
2628 // Generate the load from stack to int arg reg
Misha Brukmanea481cc2003-06-03 03:21:58 +00002629 unsigned LoadOpcode = ChooseLoadInstruction(loadTy);
2630 M = BuildMI(convertOpcodeFromRegToImm(LoadOpcode), 3)
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002631 .addMReg(regInfo.getFramePointer()).addSImm(tmpOffset)
2632 .addReg(argVReg, MOTy::Def);
2633
2634 // Mark operand with register it should be assigned
2635 // both for copy and for the callMI
2636 M->SetRegForOperand(M->getNumOperands()-1, copyRegNum);
Misha Brukmanea481cc2003-06-03 03:21:58 +00002637 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2638 copyRegNum);
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002639 mvec.push_back(M);
2640
2641 // Add info about the argument to the CallArgsDescriptor
2642 argInfo.setUseIntArgReg();
2643 argInfo.setArgCopy(copyRegNum);
2644 } else {
Misha Brukman7b647942003-05-30 20:11:56 +00002645 // Cannot fit in first $K$ regs so pass arg on stack
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002646 argInfo.setUseStackSlot();
2647 }
2648 } else if (isFPArg) {
2649 // Get the outgoing arg reg to see if there is one.
2650 regNumForArg = regInfo.regNumForFPArg(regType, false, false,
2651 argNo, regClassIDOfArgReg);
2652 if (regNumForArg == regInfo.getInvalidRegNum())
2653 argInfo.setUseStackSlot();
2654 else {
2655 argInfo.setUseFPArgReg();
2656 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2657 regNumForArg);
2658 }
2659 } else {
2660 // Get the outgoing arg reg to see if there is one.
2661 regNumForArg = regInfo.regNumForIntArg(false,false,
2662 argNo, regClassIDOfArgReg);
2663 if (regNumForArg == regInfo.getInvalidRegNum())
2664 argInfo.setUseStackSlot();
2665 else {
2666 argInfo.setUseIntArgReg();
2667 regNumForArg =regInfo.getUnifiedRegNum(regClassIDOfArgReg,
2668 regNumForArg);
2669 }
2670 }
2671
2672 //
2673 // Now insert copy instructions to stack slot or arg. register
2674 //
2675 if (argInfo.usesStackSlot()) {
2676 // Get the stack offset for this argument slot.
2677 // FP args on stack are right justified so adjust offset!
2678 // int arguments are also right justified but they are
2679 // always loaded as a full double-word so the offset does
2680 // not need to be adjusted.
2681 int argOffset = frameInfo.getOutgoingArgOffset(MF, argNo);
2682 if (argType->isFloatingPoint()) {
2683 unsigned slotSize = frameInfo.getSizeOfEachArgOnStack();
2684 assert(argSize <= slotSize && "Insufficient slot size!");
2685 argOffset += slotSize - argSize;
2686 }
2687
2688 // Now generate instruction to copy argument to stack
2689 MachineOpCode storeOpCode =
2690 (argType->isFloatingPoint()
2691 ? ((argSize == 4)? V9::STFi : V9::STDFi) : V9::STXi);
2692
2693 M = BuildMI(storeOpCode, 3).addReg(argVal)
2694 .addMReg(regInfo.getStackPointer()).addSImm(argOffset);
2695 mvec.push_back(M);
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002696 }
2697 else if (regNumForArg != regInfo.getInvalidRegNum()) {
2698
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002699 // Create a virtual register to represent the arg reg. Mark
2700 // this vreg as being an implicit operand of the call MI.
2701 TmpInstruction* argVReg =
2702 new TmpInstruction(mcfi, argVal, NULL, "argReg");
2703
2704 callMI->addImplicitRef(argVReg);
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002705
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002706 // Generate the reg-to-reg copy into the outgoing arg reg.
2707 // -- For FP values, create a FMOVS or FMOVD instruction
2708 // -- For non-FP values, create an add-with-0 instruction
2709 if (argType->isFloatingPoint())
2710 M=(BuildMI(argType==Type::FloatTy? V9::FMOVS :V9::FMOVD,2)
2711 .addReg(argVal).addReg(argVReg, MOTy::Def));
2712 else
2713 M = (BuildMI(ChooseAddInstructionByType(argType), 3)
2714 .addReg(argVal).addSImm((int64_t) 0)
2715 .addReg(argVReg, MOTy::Def));
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002716
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002717 // Mark the operand with the register it should be assigned
2718 M->SetRegForOperand(M->getNumOperands()-1, regNumForArg);
2719 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,
2720 regNumForArg);
2721
2722 mvec.push_back(M);
Misha Brukman7b647942003-05-30 20:11:56 +00002723 }
Vikram S. Advee9a567c2003-07-25 21:08:58 +00002724 else
2725 assert(argInfo.getArgCopy() != regInfo.getInvalidRegNum() &&
2726 "Arg. not in stack slot, primary or secondary register?");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002727 }
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002728
2729 // add call instruction and delay slot before copying return value
2730 mvec.push_back(callMI);
2731 mvec.push_back(BuildMI(V9::NOP, 0));
2732
Misha Brukman7b647942003-05-30 20:11:56 +00002733 // Add the return value as an implicit ref. The call operands
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002734 // were added above. Also, add code to copy out the return value.
2735 // This is always register-to-register for int or FP return values.
2736 //
2737 if (callInstr->getType() != Type::VoidTy) {
2738 // Get the return value reg.
2739 const Type* retType = callInstr->getType();
2740
2741 int regNum = (retType->isFloatingPoint()
2742 ? (unsigned) SparcFloatRegClass::f0
2743 : (unsigned) SparcIntRegClass::o0);
2744 unsigned regClassID = regInfo.getRegClassIDOfType(retType);
2745 regNum = regInfo.getUnifiedRegNum(regClassID, regNum);
2746
2747 // Create a virtual register to represent it and mark
2748 // this vreg as being an implicit operand of the call MI
2749 TmpInstruction* retVReg =
2750 new TmpInstruction(mcfi, callInstr, NULL, "argReg");
2751
2752 callMI->addImplicitRef(retVReg, /*isDef*/ true);
2753
2754 // Generate the reg-to-reg copy from the return value reg.
2755 // -- For FP values, create a FMOVS or FMOVD instruction
2756 // -- For non-FP values, create an add-with-0 instruction
2757 if (retType->isFloatingPoint())
2758 M = (BuildMI(retType==Type::FloatTy? V9::FMOVS : V9::FMOVD, 2)
2759 .addReg(retVReg).addReg(callInstr, MOTy::Def));
2760 else
2761 M = (BuildMI(ChooseAddInstructionByType(retType), 3)
2762 .addReg(retVReg).addSImm((int64_t) 0)
2763 .addReg(callInstr, MOTy::Def));
2764
2765 // Mark the operand with the register it should be assigned
2766 // Also mark the implicit ref of the call defining this operand
2767 M->SetRegForOperand(0, regNum);
2768 callMI->SetRegForImplicitRef(callMI->getNumImplicitRefs()-1,regNum);
2769
2770 mvec.push_back(M);
2771 }
2772
Misha Brukman7b647942003-05-30 20:11:56 +00002773 // For the CALL instruction, the ret. addr. reg. is also implicit
2774 if (isa<Function>(callee))
Vikram S. Adved0d06ad2003-05-31 07:32:01 +00002775 callMI->addImplicitRef(retAddrReg, /*isDef*/ true);
2776
2777 MF.getInfo()->popAllTempValues(); // free temps used for this inst
Misha Brukman7b647942003-05-30 20:11:56 +00002778 }
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002779
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002780 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002781 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002782
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002783 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002784 {
2785 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2786 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2787 Instruction* shlInstr = subtreeRoot->getInstruction();
2788
2789 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002790 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2791 "Shl unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002792 unsigned opSize = target.getTargetData().getTypeSize(opType);
Vikram S. Adve242a8082002-05-19 15:25:51 +00002793
2794 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
Vikram S. Advee895a742003-08-06 18:48:40 +00002795 (opSize > 4)? V9::SLLXr6:V9::SLLr5,
Vikram S. Adve242a8082002-05-19 15:25:51 +00002796 argVal1, argVal2, 0, shlInstr, mvec,
2797 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002798 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002799 }
2800
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002801 case 63: // reg: Shr(reg, reg)
Misha Brukman7b647942003-05-30 20:11:56 +00002802 {
2803 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002804 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2805 "Shr unsupported for other types");
Vikram S. Advee895a742003-08-06 18:48:40 +00002806 unsigned opSize = target.getTargetData().getTypeSize(opType);
Chris Lattner54e898e2003-01-15 19:23:34 +00002807 Add3OperandInstr(opType->isSigned()
Vikram S. Advee895a742003-08-06 18:48:40 +00002808 ? (opSize > 4? V9::SRAXr6 : V9::SRAr5)
2809 : (opSize > 4? V9::SRLXr6 : V9::SRLr5),
Chris Lattner54e898e2003-01-15 19:23:34 +00002810 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002811 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002812 }
2813
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002814 case 64: // reg: Phi(reg,reg)
2815 break; // don't forward the value
Vikram S. Adve74825322002-03-18 03:15:35 +00002816
Vikram S. Adve40dee512003-10-21 11:25:09 +00002817 case 65: // reg: VANext(reg): the va_next(va_list, type) instruction
2818 { // Increment the va_list pointer register according to the type.
2819 // All LLVM argument types are <= 64 bits, so use one doubleword.
2820 Instruction* vaNextI = subtreeRoot->getInstruction();
2821 assert(target.getTargetData().getTypeSize(vaNextI->getType()) <= 8 &&
2822 "We assumed that all LLVM parameter types <= 8 bytes!");
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002823 int argSize = target.getFrameInfo().getSizeOfEachArgOnStack();
Vikram S. Adve40dee512003-10-21 11:25:09 +00002824 mvec.push_back(BuildMI(V9::ADDi, 3).addReg(vaNextI->getOperand(0)).
2825 addSImm(argSize).addRegDef(vaNextI));
Vikram S. Adve472c3042003-10-21 12:28:27 +00002826 break;
Vikram S. Adve40dee512003-10-21 11:25:09 +00002827 }
2828
2829 case 66: // reg: VAArg (reg): the va_arg instruction
2830 { // Load argument from stack using current va_list pointer value.
2831 // Use 64-bit load for all non-FP args, and LDDF or double for FP.
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002832 Instruction* vaArgI = subtreeRoot->getInstruction();
Vikram S. Adve40dee512003-10-21 11:25:09 +00002833 MachineOpCode loadOp = (vaArgI->getType()->isFloatingPoint()
2834 ? (vaArgI->getType() == Type::FloatTy
2835 ? V9::LDFi : V9::LDDFi)
2836 : V9::LDXi);
Vikram S. Adve9d275142003-08-12 03:04:05 +00002837 mvec.push_back(BuildMI(loadOp, 3).addReg(vaArgI->getOperand(0)).
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002838 addSImm(0).addRegDef(vaArgI));
Vikram S. Adve5b1b47b2003-05-25 15:59:47 +00002839 break;
2840 }
2841
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002842 case 71: // reg: VReg
2843 case 72: // reg: Constant
2844 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002845
Vikram S. Adveaf9fd512003-05-31 07:27:17 +00002846 default:
2847 assert(0 && "Unrecognized BURG rule");
2848 break;
2849 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002850 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002851
Misha Brukman7b647942003-05-30 20:11:56 +00002852 if (forwardOperandNum >= 0) {
2853 // We did not generate a machine instruction but need to use operand.
2854 // If user is in the same tree, replace Value in its machine operand.
2855 // If not, insert a copy instruction which should get coalesced away
2856 // by register allocation.
2857 if (subtreeRoot->parent() != NULL)
2858 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
2859 else {
2860 std::vector<MachineInstr*> minstrVec;
2861 Instruction* instr = subtreeRoot->getInstruction();
2862 target.getInstrInfo().
2863 CreateCopyInstructionsByType(target,
2864 instr->getParent()->getParent(),
2865 instr->getOperand(forwardOperandNum),
2866 instr, minstrVec,
2867 MachineCodeForInstruction::get(instr));
2868 assert(minstrVec.size() > 0);
2869 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Chris Lattner20b1ea02001-09-14 03:47:57 +00002870 }
Misha Brukman7b647942003-05-30 20:11:56 +00002871 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002872
Misha Brukman7b647942003-05-30 20:11:56 +00002873 if (maskUnsignedResult) {
2874 // If result is unsigned and smaller than int reg size,
2875 // we need to clear high bits of result value.
2876 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2877 Instruction* dest = subtreeRoot->getInstruction();
2878 if (dest->getType()->isUnsigned()) {
2879 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
2880 if (destSize <= 4) {
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002881 // Mask high 64 - N bits, where N = 4*destSize.
2882
2883 // Use a TmpInstruction to represent the
Misha Brukman7b647942003-05-30 20:11:56 +00002884 // intermediate result before masking. Since those instructions
2885 // have already been generated, go back and substitute tmpI
2886 // for dest in the result position of each one of them.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002887 //
2888 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(dest);
2889 TmpInstruction *tmpI = new TmpInstruction(mcfi, dest->getType(),
2890 dest, NULL, "maskHi");
2891 Value* srlArgToUse = tmpI;
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002892
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002893 unsigned numSubst = 0;
2894 for (unsigned i=0, N=mvec.size(); i < N; ++i) {
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002895
2896 // Make sure we substitute all occurrences of dest in these instrs.
2897 // Otherwise, we will have bogus code.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002898 bool someArgsWereIgnored = false;
Vikram S. Adve97a95bd2003-08-07 15:01:26 +00002899
2900 // Make sure not to substitute an upwards-exposed use -- that would
2901 // introduce a use of `tmpI' with no preceding def. Therefore,
2902 // substitute a use or def-and-use operand only if a previous def
2903 // operand has already been substituted (i.e., numSusbt > 0).
2904 //
2905 numSubst += mvec[i]->substituteValue(dest, tmpI,
2906 /*defsOnly*/ numSubst == 0,
2907 /*notDefsAndUses*/ numSubst > 0,
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002908 someArgsWereIgnored);
2909 assert(!someArgsWereIgnored &&
2910 "Operand `dest' exists but not replaced: probably bogus!");
2911 }
2912 assert(numSubst > 0 && "Operand `dest' not replaced: probably bogus!");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002913
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002914 // Left shift 32-N if size (N) is less than 32 bits.
Misha Brukman452db672003-09-23 17:28:11 +00002915 // Use another tmp. virtual register to represent this result.
Vikram S. Adve951df2b2003-07-10 20:07:54 +00002916 if (destSize < 4) {
2917 srlArgToUse = new TmpInstruction(mcfi, dest->getType(),
2918 tmpI, NULL, "maskHi2");
2919 mvec.push_back(BuildMI(V9::SLLXi6, 3).addReg(tmpI)
2920 .addZImm(8*(4-destSize))
2921 .addReg(srlArgToUse, MOTy::Def));
2922 }
2923
2924 // Logical right shift 32-N to get zero extension in top 64-N bits.
2925 mvec.push_back(BuildMI(V9::SRLi5, 3).addReg(srlArgToUse)
2926 .addZImm(8*(4-destSize)).addReg(dest, MOTy::Def));
2927
Misha Brukman7b647942003-05-30 20:11:56 +00002928 } else if (destSize < 8) {
2929 assert(0 && "Unsupported type size: 32 < size < 64 bits");
2930 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002931 }
Misha Brukman7b647942003-05-30 20:11:56 +00002932 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002933}
Brian Gaeked0fde302003-11-11 22:41:34 +00002934
2935}