blob: 78fb2f20d982b67e971c03dd6f26368f01878dc0 [file] [log] [blame]
Chris Lattner035dfbe2002-08-09 20:08:06 +00001//===-- SparcInstrSelection.cpp -------------------------------------------===//
2//
3// BURS instruction selection for SPARC V9 architecture.
4//
5//===----------------------------------------------------------------------===//
Chris Lattner20b1ea02001-09-14 03:47:57 +00006
7#include "SparcInternals.h"
Vikram S. Adve7fe27872001-10-18 00:26:20 +00008#include "SparcInstrSelectionSupport.h"
Vikram S. Adve74825322002-03-18 03:15:35 +00009#include "SparcRegClassInfo.h"
Vikram S. Adve8557b222001-10-10 20:56:33 +000010#include "llvm/CodeGen/InstrSelectionSupport.h"
Chris Lattnere5b1ed92003-01-15 00:03:28 +000011#include "llvm/CodeGen/MachineInstrBuilder.h"
Vikram S. Adve242a8082002-05-19 15:25:51 +000012#include "llvm/CodeGen/MachineInstrAnnot.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000013#include "llvm/CodeGen/InstrForest.h"
14#include "llvm/CodeGen/InstrSelection.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerea45d7b2002-12-28 20:19:44 +000016#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner9c461082002-02-03 07:50:56 +000017#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner20b1ea02001-09-14 03:47:57 +000018#include "llvm/DerivedTypes.h"
19#include "llvm/iTerminators.h"
20#include "llvm/iMemory.h"
21#include "llvm/iOther.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000023#include "llvm/Constants.h"
Vikram S. Adved3e26482002-10-13 00:18:57 +000024#include "llvm/ConstantHandling.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include "Support/MathExtras.h"
Chris Lattner749655f2001-10-13 06:54:30 +000026#include <math.h>
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::vector;
Chris Lattner20b1ea02001-09-14 03:47:57 +000028
Chris Lattner54e898e2003-01-15 19:23:34 +000029static inline void Add3OperandInstr(unsigned Opcode, InstructionNode* Node,
30 vector<MachineInstr*>& mvec) {
31 mvec.push_back(BuildMI(Opcode, 3).addReg(Node->leftChild()->getValue())
32 .addReg(Node->rightChild()->getValue())
33 .addRegDef(Node->getValue()));
34}
35
36
37
Chris Lattner795ba6c2003-01-15 21:36:50 +000038//---------------------------------------------------------------------------
39// Function: GetMemInstArgs
40//
41// Purpose:
42// Get the pointer value and the index vector for a memory operation
43// (GetElementPtr, Load, or Store). If all indices of the given memory
44// operation are constant, fold in constant indices in a chain of
45// preceding GetElementPtr instructions (if any), and return the
46// pointer value of the first instruction in the chain.
47// All folded instructions are marked so no code is generated for them.
48//
49// Return values:
50// Returns the pointer Value to use.
51// Returns the resulting IndexVector in idxVec.
52// Returns true/false in allConstantIndices if all indices are/aren't const.
53//---------------------------------------------------------------------------
54
55
56//---------------------------------------------------------------------------
57// Function: FoldGetElemChain
58//
59// Purpose:
60// Fold a chain of GetElementPtr instructions containing only
61// constant offsets into an equivalent (Pointer, IndexVector) pair.
62// Returns the pointer Value, and stores the resulting IndexVector
63// in argument chainIdxVec. This is a helper function for
64// FoldConstantIndices that does the actual folding.
65//---------------------------------------------------------------------------
66
67
68// Check for a constant 0.
69inline bool
70IsZero(Value* idx)
71{
72 return (idx == ConstantSInt::getNullValue(idx->getType()));
73}
74
75static Value*
76FoldGetElemChain(InstrTreeNode* ptrNode, vector<Value*>& chainIdxVec,
77 bool lastInstHasLeadingNonZero)
78{
79 InstructionNode* gepNode = dyn_cast<InstructionNode>(ptrNode);
80 GetElementPtrInst* gepInst =
81 dyn_cast_or_null<GetElementPtrInst>(gepNode ? gepNode->getInstruction() :0);
82
83 // ptr value is not computed in this tree or ptr value does not come from GEP
84 // instruction
85 if (gepInst == NULL)
86 return NULL;
87
88 // Return NULL if we don't fold any instructions in.
89 Value* ptrVal = NULL;
90
91 // Now chase the chain of getElementInstr instructions, if any.
92 // Check for any non-constant indices and stop there.
93 // Also, stop if the first index of child is a non-zero array index
94 // and the last index of the current node is a non-array index:
95 // in that case, a non-array declared type is being accessed as an array
96 // which is not type-safe, but could be legal.
97 //
98 InstructionNode* ptrChild = gepNode;
99 while (ptrChild && (ptrChild->getOpLabel() == Instruction::GetElementPtr ||
100 ptrChild->getOpLabel() == GetElemPtrIdx))
101 {
102 // Child is a GetElemPtr instruction
103 gepInst = cast<GetElementPtrInst>(ptrChild->getValue());
104 User::op_iterator OI, firstIdx = gepInst->idx_begin();
105 User::op_iterator lastIdx = gepInst->idx_end();
106 bool allConstantOffsets = true;
107
108 // The first index of every GEP must be an array index.
109 assert((*firstIdx)->getType() == Type::LongTy &&
110 "INTERNAL ERROR: Structure index for a pointer type!");
111
112 // If the last instruction had a leading non-zero index, check if the
113 // current one references a sequential (i.e., indexable) type.
114 // If not, the code is not type-safe and we would create an illegal GEP
115 // by folding them, so don't fold any more instructions.
116 //
117 if (lastInstHasLeadingNonZero)
118 if (! isa<SequentialType>(gepInst->getType()->getElementType()))
119 break; // cannot fold in any preceding getElementPtr instrs.
120
121 // Check that all offsets are constant for this instruction
122 for (OI = firstIdx; allConstantOffsets && OI != lastIdx; ++OI)
123 allConstantOffsets = isa<ConstantInt>(*OI);
124
125 if (allConstantOffsets)
126 { // Get pointer value out of ptrChild.
127 ptrVal = gepInst->getPointerOperand();
128
129 // Remember if it has leading zero index: it will be discarded later.
130 lastInstHasLeadingNonZero = ! IsZero(*firstIdx);
131
132 // Insert its index vector at the start, skipping any leading [0]
133 chainIdxVec.insert(chainIdxVec.begin(),
134 firstIdx + !lastInstHasLeadingNonZero, lastIdx);
135
136 // Mark the folded node so no code is generated for it.
137 ((InstructionNode*) ptrChild)->markFoldedIntoParent();
138
139 // Get the previous GEP instruction and continue trying to fold
140 ptrChild = dyn_cast<InstructionNode>(ptrChild->leftChild());
141 }
142 else // cannot fold this getElementPtr instr. or any preceding ones
143 break;
144 }
145
146 // If the first getElementPtr instruction had a leading [0], add it back.
147 // Note that this instruction is the *last* one successfully folded above.
148 if (ptrVal && ! lastInstHasLeadingNonZero)
149 chainIdxVec.insert(chainIdxVec.begin(), ConstantSInt::get(Type::LongTy,0));
150
151 return ptrVal;
152}
153
154
155//---------------------------------------------------------------------------
156// Function: GetGEPInstArgs
157//
158// Purpose:
159// Helper function for GetMemInstArgs that handles the final getElementPtr
160// instruction used by (or same as) the memory operation.
161// Extracts the indices of the current instruction and tries to fold in
162// preceding ones if all indices of the current one are constant.
163//---------------------------------------------------------------------------
164
165static Value *
166GetGEPInstArgs(InstructionNode* gepNode,
167 vector<Value*>& idxVec,
168 bool& allConstantIndices)
169{
170 allConstantIndices = true;
171 GetElementPtrInst* gepI = cast<GetElementPtrInst>(gepNode->getInstruction());
172
173 // Default pointer is the one from the current instruction.
174 Value* ptrVal = gepI->getPointerOperand();
175 InstrTreeNode* ptrChild = gepNode->leftChild();
176
177 // Extract the index vector of the GEP instructin.
178 // If all indices are constant and first index is zero, try to fold
179 // in preceding GEPs with all constant indices.
180 for (User::op_iterator OI=gepI->idx_begin(), OE=gepI->idx_end();
181 allConstantIndices && OI != OE; ++OI)
182 if (! isa<Constant>(*OI))
183 allConstantIndices = false; // note: this also terminates loop!
184
185 // If we have only constant indices, fold chains of constant indices
186 // in this and any preceding GetElemPtr instructions.
187 bool foldedGEPs = false;
188 bool leadingNonZeroIdx = gepI && ! IsZero(*gepI->idx_begin());
189 if (allConstantIndices)
190 if (Value* newPtr = FoldGetElemChain(ptrChild, idxVec, leadingNonZeroIdx))
191 {
192 ptrVal = newPtr;
193 foldedGEPs = true;
194 }
195
196 // Append the index vector of the current instruction.
197 // Skip the leading [0] index if preceding GEPs were folded into this.
198 idxVec.insert(idxVec.end(),
199 gepI->idx_begin() + (foldedGEPs && !leadingNonZeroIdx),
200 gepI->idx_end());
201
202 return ptrVal;
203}
204
205//---------------------------------------------------------------------------
206// Function: GetMemInstArgs
207//
208// Purpose:
209// Get the pointer value and the index vector for a memory operation
210// (GetElementPtr, Load, or Store). If all indices of the given memory
211// operation are constant, fold in constant indices in a chain of
212// preceding GetElementPtr instructions (if any), and return the
213// pointer value of the first instruction in the chain.
214// All folded instructions are marked so no code is generated for them.
215//
216// Return values:
217// Returns the pointer Value to use.
218// Returns the resulting IndexVector in idxVec.
219// Returns true/false in allConstantIndices if all indices are/aren't const.
220//---------------------------------------------------------------------------
221
222static Value*
223GetMemInstArgs(InstructionNode* memInstrNode,
224 vector<Value*>& idxVec,
225 bool& allConstantIndices)
226{
227 allConstantIndices = false;
228 Instruction* memInst = memInstrNode->getInstruction();
229 assert(idxVec.size() == 0 && "Need empty vector to return indices");
230
231 // If there is a GetElemPtr instruction to fold in to this instr,
232 // it must be in the left child for Load and GetElemPtr, and in the
233 // right child for Store instructions.
234 InstrTreeNode* ptrChild = (memInst->getOpcode() == Instruction::Store
235 ? memInstrNode->rightChild()
236 : memInstrNode->leftChild());
237
238 // Default pointer is the one from the current instruction.
239 Value* ptrVal = ptrChild->getValue();
240
241 // Find the "last" GetElemPtr instruction: this one or the immediate child.
242 // There will be none if this is a load or a store from a scalar pointer.
243 InstructionNode* gepNode = NULL;
244 if (isa<GetElementPtrInst>(memInst))
245 gepNode = memInstrNode;
246 else if (isa<InstructionNode>(ptrChild) && isa<GetElementPtrInst>(ptrVal))
247 { // Child of load/store is a GEP and memInst is its only use.
248 // Use its indices and mark it as folded.
249 gepNode = cast<InstructionNode>(ptrChild);
250 gepNode->markFoldedIntoParent();
251 }
252
253 // If there are no indices, return the current pointer.
254 // Else extract the pointer from the GEP and fold the indices.
255 return gepNode ? GetGEPInstArgs(gepNode, idxVec, allConstantIndices)
256 : ptrVal;
257}
258
Chris Lattner54e898e2003-01-15 19:23:34 +0000259
Chris Lattner20b1ea02001-09-14 03:47:57 +0000260//************************ Internal Functions ******************************/
261
Chris Lattner20b1ea02001-09-14 03:47:57 +0000262
Chris Lattner20b1ea02001-09-14 03:47:57 +0000263static inline MachineOpCode
264ChooseBprInstruction(const InstructionNode* instrNode)
265{
266 MachineOpCode opCode;
267
268 Instruction* setCCInstr =
269 ((InstructionNode*) instrNode->leftChild())->getInstruction();
270
271 switch(setCCInstr->getOpcode())
272 {
273 case Instruction::SetEQ: opCode = BRZ; break;
274 case Instruction::SetNE: opCode = BRNZ; break;
275 case Instruction::SetLE: opCode = BRLEZ; break;
276 case Instruction::SetGE: opCode = BRGEZ; break;
277 case Instruction::SetLT: opCode = BRLZ; break;
278 case Instruction::SetGT: opCode = BRGZ; break;
279 default:
280 assert(0 && "Unrecognized VM instruction!");
281 opCode = INVALID_OPCODE;
282 break;
283 }
284
285 return opCode;
286}
287
288
289static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000290ChooseBpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000291 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000292{
293 MachineOpCode opCode = INVALID_OPCODE;
294
295 bool isSigned = setCCInstr->getOperand(0)->getType()->isSigned();
296
297 if (isSigned)
298 {
299 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000300 {
301 case Instruction::SetEQ: opCode = BE; break;
302 case Instruction::SetNE: opCode = BNE; break;
303 case Instruction::SetLE: opCode = BLE; break;
304 case Instruction::SetGE: opCode = BGE; break;
305 case Instruction::SetLT: opCode = BL; break;
306 case Instruction::SetGT: opCode = BG; break;
307 default:
308 assert(0 && "Unrecognized VM instruction!");
309 break;
310 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000311 }
312 else
313 {
314 switch(setCCInstr->getOpcode())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000315 {
316 case Instruction::SetEQ: opCode = BE; break;
317 case Instruction::SetNE: opCode = BNE; break;
318 case Instruction::SetLE: opCode = BLEU; break;
319 case Instruction::SetGE: opCode = BCC; break;
320 case Instruction::SetLT: opCode = BCS; break;
321 case Instruction::SetGT: opCode = BGU; break;
322 default:
323 assert(0 && "Unrecognized VM instruction!");
324 break;
325 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000326 }
327
328 return opCode;
329}
330
331static inline MachineOpCode
332ChooseBFpccInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000333 const BinaryOperator* setCCInstr)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000334{
335 MachineOpCode opCode = INVALID_OPCODE;
336
337 switch(setCCInstr->getOpcode())
338 {
339 case Instruction::SetEQ: opCode = FBE; break;
340 case Instruction::SetNE: opCode = FBNE; break;
341 case Instruction::SetLE: opCode = FBLE; break;
342 case Instruction::SetGE: opCode = FBGE; break;
343 case Instruction::SetLT: opCode = FBL; break;
344 case Instruction::SetGT: opCode = FBG; break;
345 default:
346 assert(0 && "Unrecognized VM instruction!");
347 break;
348 }
349
350 return opCode;
351}
352
353
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000354// Create a unique TmpInstruction for a boolean value,
355// representing the CC register used by a branch on that value.
356// For now, hack this using a little static cache of TmpInstructions.
357// Eventually the entire BURG instruction selection should be put
358// into a separate class that can hold such information.
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000359// The static cache is not too bad because the memory for these
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000360// TmpInstructions will be freed along with the rest of the Function anyway.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000361//
362static TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000363GetTmpForCC(Value* boolVal, const Function *F, const Type* ccType)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000364{
Chris Lattner09ff1122002-07-24 21:21:32 +0000365 typedef hash_map<const Value*, TmpInstruction*> BoolTmpCache;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000366 static BoolTmpCache boolToTmpCache; // Map boolVal -> TmpInstruction*
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000367 static const Function *lastFunction = 0;// Use to flush cache between funcs
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000368
369 assert(boolVal->getType() == Type::BoolTy && "Weird but ok! Delete assert");
370
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000371 if (lastFunction != F)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000372 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000373 lastFunction = F;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000374 boolToTmpCache.clear();
375 }
376
Vikram S. Adveff5a09e2001-11-08 05:04:09 +0000377 // Look for tmpI and create a new one otherwise. The new value is
378 // directly written to map using the ref returned by operator[].
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000379 TmpInstruction*& tmpI = boolToTmpCache[boolVal];
380 if (tmpI == NULL)
Chris Lattner9c461082002-02-03 07:50:56 +0000381 tmpI = new TmpInstruction(ccType, boolVal);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +0000382
383 return tmpI;
384}
385
386
Chris Lattner20b1ea02001-09-14 03:47:57 +0000387static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000388ChooseBccInstruction(const InstructionNode* instrNode,
389 bool& isFPBranch)
390{
391 InstructionNode* setCCNode = (InstructionNode*) instrNode->leftChild();
Vikram S. Adve30a6f492002-08-22 02:56:10 +0000392 assert(setCCNode->getOpLabel() == SetCCOp);
393 BinaryOperator* setCCInstr =cast<BinaryOperator>(setCCNode->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000394 const Type* setCCType = setCCInstr->getOperand(0)->getType();
395
Vikram S. Adve242a8082002-05-19 15:25:51 +0000396 isFPBranch = setCCType->isFloatingPoint(); // Return value: don't delete!
397
398 if (isFPBranch)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000399 return ChooseBFpccInstruction(instrNode, setCCInstr);
400 else
401 return ChooseBpccInstruction(instrNode, setCCInstr);
402}
403
404
405static inline MachineOpCode
Chris Lattner20b1ea02001-09-14 03:47:57 +0000406ChooseMovFpccInstruction(const InstructionNode* instrNode)
407{
408 MachineOpCode opCode = INVALID_OPCODE;
409
410 switch(instrNode->getInstruction()->getOpcode())
411 {
412 case Instruction::SetEQ: opCode = MOVFE; break;
413 case Instruction::SetNE: opCode = MOVFNE; break;
414 case Instruction::SetLE: opCode = MOVFLE; break;
415 case Instruction::SetGE: opCode = MOVFGE; break;
416 case Instruction::SetLT: opCode = MOVFL; break;
417 case Instruction::SetGT: opCode = MOVFG; break;
418 default:
419 assert(0 && "Unrecognized VM instruction!");
420 break;
421 }
422
423 return opCode;
424}
425
426
427// Assumes that SUBcc v1, v2 -> v3 has been executed.
428// In most cases, we want to clear v3 and then follow it by instruction
429// MOVcc 1 -> v3.
430// Set mustClearReg=false if v3 need not be cleared before conditional move.
431// Set valueToMove=0 if we want to conditionally move 0 instead of 1
432// (i.e., we want to test inverse of a condition)
Vikram S. Adve243dd452001-09-18 13:03:13 +0000433// (The latter two cases do not seem to arise because SetNE needs nothing.)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000434//
435static MachineOpCode
436ChooseMovpccAfterSub(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000437 bool& mustClearReg,
438 int& valueToMove)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000439{
440 MachineOpCode opCode = INVALID_OPCODE;
441 mustClearReg = true;
442 valueToMove = 1;
443
444 switch(instrNode->getInstruction()->getOpcode())
445 {
Vikram S. Adve243dd452001-09-18 13:03:13 +0000446 case Instruction::SetEQ: opCode = MOVE; break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000447 case Instruction::SetLE: opCode = MOVLE; break;
448 case Instruction::SetGE: opCode = MOVGE; break;
449 case Instruction::SetLT: opCode = MOVL; break;
450 case Instruction::SetGT: opCode = MOVG; break;
Vikram S. Adve243dd452001-09-18 13:03:13 +0000451 case Instruction::SetNE: assert(0 && "No move required!"); break;
452 default: assert(0 && "Unrecognized VM instr!"); break;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000453 }
454
455 return opCode;
456}
457
Chris Lattner20b1ea02001-09-14 03:47:57 +0000458static inline MachineOpCode
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000459ChooseConvertToFloatInstr(OpLabel vopCode, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000460{
461 MachineOpCode opCode = INVALID_OPCODE;
462
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000463 switch(vopCode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000464 {
465 case ToFloatTy:
466 if (opType == Type::SByteTy || opType == Type::ShortTy || opType == Type::IntTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000467 opCode = FITOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000468 else if (opType == Type::LongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000469 opCode = FXTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000470 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000471 opCode = FDTOS;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000472 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000473 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000474 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000475 assert(0 && "Cannot convert this type to FLOAT on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000476 break;
477
478 case ToDoubleTy:
Vikram S. Adve74825322002-03-18 03:15:35 +0000479 // This is usually used in conjunction with CreateCodeToCopyIntToFloat().
480 // Both functions should treat the integer as a 32-bit value for types
481 // of 4 bytes or less, and as a 64-bit value otherwise.
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000482 if (opType == Type::SByteTy || opType == Type::UByteTy ||
483 opType == Type::ShortTy || opType == Type::UShortTy ||
484 opType == Type::IntTy || opType == Type::UIntTy)
Vikram S. Adve74825322002-03-18 03:15:35 +0000485 opCode = FITOD;
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000486 else if (opType == Type::LongTy || opType == Type::ULongTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000487 opCode = FXTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000488 else if (opType == Type::FloatTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000489 opCode = FSTOD;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000490 else if (opType == Type::DoubleTy)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000491 ;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000492 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000493 assert(0 && "Cannot convert this type to DOUBLE on SPARC");
Chris Lattner20b1ea02001-09-14 03:47:57 +0000494 break;
495
496 default:
497 break;
498 }
499
500 return opCode;
501}
502
503static inline MachineOpCode
Vikram S. Adve94c40812002-09-27 14:33:08 +0000504ChooseConvertFPToIntInstr(Type::PrimitiveID tid, const Type* opType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000505{
506 MachineOpCode opCode = INVALID_OPCODE;;
Vikram S. Adve94c40812002-09-27 14:33:08 +0000507
508 assert((opType == Type::FloatTy || opType == Type::DoubleTy)
509 && "This function should only be called for FLOAT or DOUBLE");
510
511 if (tid==Type::UIntTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000512 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000513 assert(tid != Type::UIntTyID && "FP-to-uint conversions must be expanded"
514 " into FP->long->uint for SPARC v9: SO RUN PRESELECTION PASS!");
515 }
516 else if (tid==Type::SByteTyID || tid==Type::ShortTyID || tid==Type::IntTyID ||
517 tid==Type::UByteTyID || tid==Type::UShortTyID)
518 {
519 opCode = (opType == Type::FloatTy)? FSTOI : FDTOI;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000520 }
Vikram S. Adve1e606692002-07-31 21:01:34 +0000521 else if (tid==Type::LongTyID || tid==Type::ULongTyID)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000522 {
Vikram S. Adve94c40812002-09-27 14:33:08 +0000523 opCode = (opType == Type::FloatTy)? FSTOX : FDTOX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000524 }
525 else
526 assert(0 && "Should not get here, Mo!");
Vikram S. Adve94c40812002-09-27 14:33:08 +0000527
Chris Lattner20b1ea02001-09-14 03:47:57 +0000528 return opCode;
529}
530
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000531MachineInstr*
Vikram S. Adve94c40812002-09-27 14:33:08 +0000532CreateConvertFPToIntInstr(Type::PrimitiveID destTID,
533 Value* srcVal, Value* destVal)
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000534{
Vikram S. Adve94c40812002-09-27 14:33:08 +0000535 MachineOpCode opCode = ChooseConvertFPToIntInstr(destTID, srcVal->getType());
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000536 assert(opCode != INVALID_OPCODE && "Expected to need conversion!");
Chris Lattner00dca912003-01-15 17:47:49 +0000537 return BuildMI(opCode, 2).addReg(srcVal).addRegDef(destVal);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +0000538}
Chris Lattner20b1ea02001-09-14 03:47:57 +0000539
Vikram S. Adve8cfffd32002-08-24 20:56:53 +0000540// CreateCodeToConvertFloatToInt: Convert FP value to signed or unsigned integer
Vikram S. Adve1e606692002-07-31 21:01:34 +0000541// The FP value must be converted to the dest type in an FP register,
542// and the result is then copied from FP to int register via memory.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000543//
544// Since fdtoi converts to signed integers, any FP value V between MAXINT+1
545// and MAXUNSIGNED (i.e., 2^31 <= V <= 2^32-1) would be converted incorrectly
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000546// *only* when converting to an unsigned. (Unsigned byte, short or long
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000547// don't have this problem.)
548// For unsigned int, we therefore have to generate the code sequence:
549//
550// if (V > (float) MAXINT) {
551// unsigned result = (unsigned) (V - (float) MAXINT);
552// result = result + (unsigned) MAXINT;
553// }
554// else
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000555// result = (unsigned) V;
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{
564 // Create a temporary to represent the FP register into which the
565 // int value will placed after conversion. The type of this temporary
566 // depends on the type of FP register to use: single-prec for a 32-bit
567 // int or smaller; double-prec for a 64-bit int.
568 //
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000569 size_t destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000570 const Type* destTypeToUse = (destSize > 4)? Type::DoubleTy : Type::FloatTy;
571 TmpInstruction* destForCast = new TmpInstruction(destTypeToUse, opVal);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000572 mcfi.addTemp(destForCast);
573
574 // Create the fp-to-int conversion code
Vikram S. Adve94c40812002-09-27 14:33:08 +0000575 MachineInstr* M =CreateConvertFPToIntInstr(destI->getType()->getPrimitiveID(),
576 opVal, destForCast);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000577 mvec.push_back(M);
578
579 // Create the fpreg-to-intreg copy code
580 target.getInstrInfo().
581 CreateCodeToCopyFloatToInt(target, destI->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +0000582 destForCast, destI, mvec, mcfi);
Vikram S. Adve1e606692002-07-31 21:01:34 +0000583}
584
585
Chris Lattner20b1ea02001-09-14 03:47:57 +0000586static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000587ChooseAddInstruction(const InstructionNode* instrNode)
588{
589 return ChooseAddInstructionByType(instrNode->getInstruction()->getType());
590}
591
592
Chris Lattner20b1ea02001-09-14 03:47:57 +0000593static inline MachineInstr*
594CreateMovFloatInstruction(const InstructionNode* instrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000595 const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000596{
Chris Lattner00dca912003-01-15 17:47:49 +0000597 return BuildMI((resultType == Type::FloatTy) ? FMOVS : FMOVD, 2)
598 .addReg(instrNode->leftChild()->getValue())
599 .addRegDef(instrNode->getValue());
Chris Lattner20b1ea02001-09-14 03:47:57 +0000600}
601
602static inline MachineInstr*
603CreateAddConstInstruction(const InstructionNode* instrNode)
604{
605 MachineInstr* minstr = NULL;
606
607 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000608 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000609
610 // Cases worth optimizing are:
611 // (1) Add with 0 for float or double: use an FMOV of appropriate type,
612 // instead of an FADD (1 vs 3 cycles). There is no integer MOV.
613 //
Chris Lattner9b625032002-05-06 16:15:30 +0000614 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
615 double dval = FPC->getValue();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000616 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000617 minstr = CreateMovFloatInstruction(instrNode,
618 instrNode->getInstruction()->getType());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000619 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000620
621 return minstr;
622}
623
624
625static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000626ChooseSubInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000627{
628 MachineOpCode opCode = INVALID_OPCODE;
629
Chris Lattner0c4e8862002-09-03 01:08:28 +0000630 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000631 {
632 opCode = SUB;
633 }
634 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000635 switch(resultType->getPrimitiveID())
636 {
637 case Type::FloatTyID: opCode = FSUBS; break;
638 case Type::DoubleTyID: opCode = FSUBD; break;
639 default: assert(0 && "Invalid type for SUB instruction"); break;
640 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000641
642 return opCode;
643}
644
645
646static inline MachineInstr*
647CreateSubConstInstruction(const InstructionNode* instrNode)
648{
649 MachineInstr* minstr = NULL;
650
651 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000652 assert(isa<Constant>(constOp));
Chris Lattner20b1ea02001-09-14 03:47:57 +0000653
654 // Cases worth optimizing are:
655 // (1) Sub with 0 for float or double: use an FMOV of appropriate type,
656 // instead of an FSUB (1 vs 3 cycles). There is no integer MOV.
657 //
Chris Lattner9b625032002-05-06 16:15:30 +0000658 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp)) {
659 double dval = FPC->getValue();
660 if (dval == 0.0)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000661 minstr = CreateMovFloatInstruction(instrNode,
662 instrNode->getInstruction()->getType());
Chris Lattner9b625032002-05-06 16:15:30 +0000663 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000664
665 return minstr;
666}
667
668
669static inline MachineOpCode
670ChooseFcmpInstruction(const InstructionNode* instrNode)
671{
672 MachineOpCode opCode = INVALID_OPCODE;
673
674 Value* operand = ((InstrTreeNode*) instrNode->leftChild())->getValue();
675 switch(operand->getType()->getPrimitiveID()) {
676 case Type::FloatTyID: opCode = FCMPS; break;
677 case Type::DoubleTyID: opCode = FCMPD; break;
678 default: assert(0 && "Invalid type for FCMP instruction"); break;
679 }
680
681 return opCode;
682}
683
684
685// Assumes that leftArg and rightArg are both cast instructions.
686//
687static inline bool
688BothFloatToDouble(const InstructionNode* instrNode)
689{
690 InstrTreeNode* leftArg = instrNode->leftChild();
691 InstrTreeNode* rightArg = instrNode->rightChild();
692 InstrTreeNode* leftArgArg = leftArg->leftChild();
693 InstrTreeNode* rightArgArg = rightArg->leftChild();
694 assert(leftArg->getValue()->getType() == rightArg->getValue()->getType());
695
696 // Check if both arguments are floats cast to double
697 return (leftArg->getValue()->getType() == Type::DoubleTy &&
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000698 leftArgArg->getValue()->getType() == Type::FloatTy &&
699 rightArgArg->getValue()->getType() == Type::FloatTy);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000700}
701
702
703static inline MachineOpCode
Vikram S. Adve510eec72001-11-04 21:59:14 +0000704ChooseMulInstructionByType(const Type* resultType)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000705{
706 MachineOpCode opCode = INVALID_OPCODE;
707
Chris Lattner0c4e8862002-09-03 01:08:28 +0000708 if (resultType->isInteger())
Vikram S. Adve510eec72001-11-04 21:59:14 +0000709 opCode = MULX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000710 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000711 switch(resultType->getPrimitiveID())
712 {
713 case Type::FloatTyID: opCode = FMULS; break;
714 case Type::DoubleTyID: opCode = FMULD; break;
715 default: assert(0 && "Invalid type for MUL instruction"); break;
716 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000717
718 return opCode;
719}
720
721
Vikram S. Adve510eec72001-11-04 21:59:14 +0000722
Chris Lattner20b1ea02001-09-14 03:47:57 +0000723static inline MachineInstr*
Vikram S. Adve74825322002-03-18 03:15:35 +0000724CreateIntNegInstruction(const TargetMachine& target,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000725 Value* vreg)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000726{
Chris Lattner00dca912003-01-15 17:47:49 +0000727 return BuildMI(SUB, 3).addMReg(target.getRegInfo().getZeroRegNum())
728 .addReg(vreg).addRegDef(vreg);
Chris Lattner20b1ea02001-09-14 03:47:57 +0000729}
730
731
Vikram S. Adve242a8082002-05-19 15:25:51 +0000732// Create instruction sequence for any shift operation.
733// SLL or SLLX on an operand smaller than the integer reg. size (64bits)
734// requires a second instruction for explicit sign-extension.
735// Note that we only have to worry about a sign-bit appearing in the
736// most significant bit of the operand after shifting (e.g., bit 32 of
737// Int or bit 16 of Short), so we do not have to worry about results
738// that are as large as a normal integer register.
739//
740static inline void
741CreateShiftInstructions(const TargetMachine& target,
742 Function* F,
743 MachineOpCode shiftOpCode,
744 Value* argVal1,
745 Value* optArgVal2, /* Use optArgVal2 if not NULL */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000746 unsigned optShiftNum, /* else use optShiftNum */
Vikram S. Adve242a8082002-05-19 15:25:51 +0000747 Instruction* destVal,
748 vector<MachineInstr*>& mvec,
749 MachineCodeForInstruction& mcfi)
750{
751 assert((optArgVal2 != NULL || optShiftNum <= 64) &&
752 "Large shift sizes unexpected, but can be handled below: "
753 "You need to check whether or not it fits in immed field below");
754
755 // If this is a logical left shift of a type smaller than the standard
756 // integer reg. size, we have to extend the sign-bit into upper bits
757 // of dest, so we need to put the result of the SLL into a temporary.
758 //
759 Value* shiftDest = destVal;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000760 unsigned opSize = target.getTargetData().getTypeSize(argVal1->getType());
Chris Lattner7a5adc32003-04-26 19:44:35 +0000761 if ((shiftOpCode == SLL || shiftOpCode == SLLX) && opSize < 8)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000762 { // put SLL result into a temporary
763 shiftDest = new TmpInstruction(argVal1, optArgVal2, "sllTmp");
764 mcfi.addTemp(shiftDest);
765 }
766
767 MachineInstr* M = (optArgVal2 != NULL)
Chris Lattnere5b1ed92003-01-15 00:03:28 +0000768 ? BuildMI(shiftOpCode, 3).addReg(argVal1).addReg(optArgVal2)
769 .addReg(shiftDest, MOTy::Def)
770 : BuildMI(shiftOpCode, 3).addReg(argVal1).addZImm(optShiftNum)
771 .addReg(shiftDest, MOTy::Def);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000772 mvec.push_back(M);
773
774 if (shiftDest != destVal)
775 { // extend the sign-bit of the result into all upper bits of dest
776 assert(8*opSize <= 32 && "Unexpected type size > 4 and < IntRegSize?");
777 target.getInstrInfo().
Vikram S. Adve94c40812002-09-27 14:33:08 +0000778 CreateSignExtensionInstructions(target, F, shiftDest, destVal,
779 8*opSize, mvec, mcfi);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000780 }
781}
782
783
Vikram S. Adve74825322002-03-18 03:15:35 +0000784// Does not create any instructions if we cannot exploit constant to
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000785// create a cheaper instruction.
786// This returns the approximate cost of the instructions generated,
787// which is used to pick the cheapest when both operands are constant.
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000788static inline unsigned
Vikram S. Adve242a8082002-05-19 15:25:51 +0000789CreateMulConstInstruction(const TargetMachine &target, Function* F,
790 Value* lval, Value* rval, Instruction* destVal,
791 vector<MachineInstr*>& mvec,
792 MachineCodeForInstruction& mcfi)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000793{
Vikram S. Adve242a8082002-05-19 15:25:51 +0000794 /* Use max. multiply cost, viz., cost of MULX */
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000795 unsigned cost = target.getInstrInfo().minLatency(MULX);
796 unsigned firstNewInstr = mvec.size();
Vikram S. Adve74825322002-03-18 03:15:35 +0000797
798 Value* constOp = rval;
799 if (! isa<Constant>(constOp))
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000800 return cost;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000801
802 // Cases worth optimizing are:
803 // (1) Multiply by 0 or 1 for any type: replace with copy (ADD or FMOV)
804 // (2) Multiply by 2^x for integer types: replace with Shift
805 //
Vikram S. Adve74825322002-03-18 03:15:35 +0000806 const Type* resultType = destVal->getType();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000807
Chris Lattner0c4e8862002-09-03 01:08:28 +0000808 if (resultType->isInteger() || isa<PointerType>(resultType))
Chris Lattner20b1ea02001-09-14 03:47:57 +0000809 {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000810 bool isValidConst;
811 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
812 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000813 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000814 unsigned pow;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000815 bool needNeg = false;
816 if (C < 0)
817 {
818 needNeg = true;
819 C = -C;
820 }
821
Chris Lattner00dca912003-01-15 17:47:49 +0000822 if (C == 0 || C == 1) {
823 cost = target.getInstrInfo().minLatency(ADD);
824 unsigned Zero = target.getRegInfo().getZeroRegNum();
825 MachineInstr* M;
826 if (C == 0)
827 M = BuildMI(ADD,3).addMReg(Zero).addMReg(Zero).addRegDef(destVal);
828 else
829 M = BuildMI(ADD,3).addReg(lval).addMReg(Zero).addRegDef(destVal);
830 mvec.push_back(M);
831 }
Chris Lattner36346c72002-05-19 21:20:19 +0000832 else if (isPowerOf2(C, pow))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000833 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000834 unsigned opSize = target.getTargetData().getTypeSize(resultType);
Vikram S. Adve242a8082002-05-19 15:25:51 +0000835 MachineOpCode opCode = (opSize <= 32)? SLL : SLLX;
836 CreateShiftInstructions(target, F, opCode, lval, NULL, pow,
837 destVal, mvec, mcfi);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000838 }
839
Vikram S. Adve242a8082002-05-19 15:25:51 +0000840 if (mvec.size() > 0 && needNeg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000841 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Vikram S. Adve242a8082002-05-19 15:25:51 +0000842 MachineInstr* M = CreateIntNegInstruction(target, destVal);
843 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000844 }
845 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000846 }
847 else
848 {
Chris Lattner9b625032002-05-06 16:15:30 +0000849 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000850 {
Chris Lattner9b625032002-05-06 16:15:30 +0000851 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000852 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000853 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000854 MachineOpCode opCode = (dval < 0)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000855 ? (resultType == Type::FloatTy? FNEGS : FNEGD)
856 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Chris Lattner00dca912003-01-15 17:47:49 +0000857 mvec.push_back(BuildMI(opCode,2).addReg(lval).addRegDef(destVal));
Vikram S. Adve6ad7c552001-11-09 02:18:16 +0000858 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000859 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000860 }
861
Vikram S. Adve242a8082002-05-19 15:25:51 +0000862 if (firstNewInstr < mvec.size())
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000863 {
Vikram S. Adve242a8082002-05-19 15:25:51 +0000864 cost = 0;
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000865 for (unsigned i=firstNewInstr; i < mvec.size(); ++i)
Vikram S. Adve242a8082002-05-19 15:25:51 +0000866 cost += target.getInstrInfo().minLatency(mvec[i]->getOpCode());
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000867 }
868
869 return cost;
Vikram S. Adve74825322002-03-18 03:15:35 +0000870}
871
872
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000873// Does not create any instructions if we cannot exploit constant to
874// create a cheaper instruction.
875//
876static inline void
877CreateCheapestMulConstInstruction(const TargetMachine &target,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000878 Function* F,
879 Value* lval, Value* rval,
880 Instruction* destVal,
881 vector<MachineInstr*>& mvec,
882 MachineCodeForInstruction& mcfi)
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000883{
884 Value* constOp;
885 if (isa<Constant>(lval) && isa<Constant>(rval))
Vikram S. Adved3e26482002-10-13 00:18:57 +0000886 { // both operands are constant: evaluate and "set" in dest
887 Constant* P = ConstantFoldBinaryInstruction(Instruction::Mul,
888 cast<Constant>(lval), cast<Constant>(rval));
889 target.getInstrInfo().CreateCodeToLoadConst(target,F,P,destVal,mvec,mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000890 }
891 else if (isa<Constant>(rval)) // rval is constant, but not lval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000892 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000893 else if (isa<Constant>(lval)) // lval is constant, but not rval
Vikram S. Adve242a8082002-05-19 15:25:51 +0000894 CreateMulConstInstruction(target, F, lval, rval, destVal, mvec, mcfi);
Vikram S. Advefd3900a2002-03-24 03:33:02 +0000895
896 // else neither is constant
897 return;
898}
899
Vikram S. Adve74825322002-03-18 03:15:35 +0000900// Return NULL if we cannot exploit constant to create a cheaper instruction
901static inline void
Vikram S. Adve242a8082002-05-19 15:25:51 +0000902CreateMulInstruction(const TargetMachine &target, Function* F,
903 Value* lval, Value* rval, Instruction* destVal,
Vikram S. Adve74825322002-03-18 03:15:35 +0000904 vector<MachineInstr*>& mvec,
Vikram S. Adve242a8082002-05-19 15:25:51 +0000905 MachineCodeForInstruction& mcfi,
Vikram S. Adve74825322002-03-18 03:15:35 +0000906 MachineOpCode forceMulOp = INVALID_MACHINE_OPCODE)
907{
Chris Lattnerea45d7b2002-12-28 20:19:44 +0000908 unsigned L = mvec.size();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000909 CreateCheapestMulConstInstruction(target,F, lval, rval, destVal, mvec, mcfi);
Vikram S. Adve74825322002-03-18 03:15:35 +0000910 if (mvec.size() == L)
911 { // no instructions were added so create MUL reg, reg, reg.
912 // Use FSMULD if both operands are actually floats cast to doubles.
913 // Otherwise, use the default opcode for the appropriate type.
914 MachineOpCode mulOp = ((forceMulOp != INVALID_MACHINE_OPCODE)
915 ? forceMulOp
916 : ChooseMulInstructionByType(destVal->getType()));
Chris Lattner00dca912003-01-15 17:47:49 +0000917 mvec.push_back(BuildMI(mulOp, 3).addReg(lval).addReg(rval)
918 .addRegDef(destVal));
Vikram S. Adve74825322002-03-18 03:15:35 +0000919 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000920}
921
922
Vikram S. Adve510eec72001-11-04 21:59:14 +0000923// Generate a divide instruction for Div or Rem.
924// For Rem, this assumes that the operand type will be signed if the result
925// type is signed. This is correct because they must have the same sign.
926//
Chris Lattner20b1ea02001-09-14 03:47:57 +0000927static inline MachineOpCode
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000928ChooseDivInstruction(TargetMachine &target,
929 const InstructionNode* instrNode)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000930{
931 MachineOpCode opCode = INVALID_OPCODE;
932
933 const Type* resultType = instrNode->getInstruction()->getType();
934
Chris Lattner0c4e8862002-09-03 01:08:28 +0000935 if (resultType->isInteger())
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000936 opCode = resultType->isSigned()? SDIVX : UDIVX;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000937 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000938 switch(resultType->getPrimitiveID())
939 {
940 case Type::FloatTyID: opCode = FDIVS; break;
941 case Type::DoubleTyID: opCode = FDIVD; break;
942 default: assert(0 && "Invalid type for DIV instruction"); break;
943 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000944
945 return opCode;
946}
947
948
Chris Lattner54e898e2003-01-15 19:23:34 +0000949// Return if we cannot exploit constant to create a cheaper instruction
Vikram S. Adve74825322002-03-18 03:15:35 +0000950static inline void
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000951CreateDivConstInstruction(TargetMachine &target,
952 const InstructionNode* instrNode,
Vikram S. Adve74825322002-03-18 03:15:35 +0000953 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +0000954{
Chris Lattner54e898e2003-01-15 19:23:34 +0000955 Value* LHS = instrNode->leftChild()->getValue();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000956 Value* constOp = ((InstrTreeNode*) instrNode->rightChild())->getValue();
Chris Lattner54e898e2003-01-15 19:23:34 +0000957 if (!isa<Constant>(constOp))
Vikram S. Adve74825322002-03-18 03:15:35 +0000958 return;
Chris Lattner54e898e2003-01-15 19:23:34 +0000959
960 Value* DestVal = instrNode->getValue();
961 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
Chris Lattner20b1ea02001-09-14 03:47:57 +0000962
963 // Cases worth optimizing are:
964 // (1) Divide by 1 for any type: replace with copy (ADD or FMOV)
965 // (2) Divide by 2^x for integer types: replace with SR[L or A]{X}
966 //
967 const Type* resultType = instrNode->getInstruction()->getType();
Chris Lattner54e898e2003-01-15 19:23:34 +0000968
Chris Lattner0c4e8862002-09-03 01:08:28 +0000969 if (resultType->isInteger())
Chris Lattner20b1ea02001-09-14 03:47:57 +0000970 {
971 unsigned pow;
972 bool isValidConst;
973 int64_t C = GetConstantValueAsSignedInt(constOp, isValidConst);
974 if (isValidConst)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000975 {
976 bool needNeg = false;
Chris Lattner54e898e2003-01-15 19:23:34 +0000977 if (C < 0) {
978 needNeg = true;
979 C = -C;
980 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000981
Chris Lattner54e898e2003-01-15 19:23:34 +0000982 if (C == 1) {
983 mvec.push_back(BuildMI(ADD, 3).addReg(LHS).addMReg(ZeroReg)
984 .addRegDef(DestVal));
985 } else if (isPowerOf2(C, pow)) {
986 unsigned opCode= ((resultType->isSigned())
987 ? (resultType==Type::LongTy) ? SRAX : SRA
988 : (resultType==Type::LongTy) ? SRLX : SRL);
989 mvec.push_back(BuildMI(opCode, 3).addReg(LHS).addZImm(pow)
990 .addRegDef(DestVal));
991 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000992
Chris Lattner54e898e2003-01-15 19:23:34 +0000993 if (needNeg && (C == 1 || isPowerOf2(C, pow)))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000994 { // insert <reg = SUB 0, reg> after the instr to flip the sign
Chris Lattner54e898e2003-01-15 19:23:34 +0000995 mvec.push_back(CreateIntNegInstruction(target, DestVal));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +0000996 }
997 }
Chris Lattner20b1ea02001-09-14 03:47:57 +0000998 }
999 else
1000 {
Chris Lattner9b625032002-05-06 16:15:30 +00001001 if (ConstantFP *FPC = dyn_cast<ConstantFP>(constOp))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001002 {
Chris Lattner9b625032002-05-06 16:15:30 +00001003 double dval = FPC->getValue();
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001004 if (fabs(dval) == 1)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001005 {
Chris Lattner54e898e2003-01-15 19:23:34 +00001006 unsigned opCode =
1007 (dval < 0) ? (resultType == Type::FloatTy? FNEGS : FNEGD)
1008 : (resultType == Type::FloatTy? FMOVS : FMOVD);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001009
Chris Lattner54e898e2003-01-15 19:23:34 +00001010 mvec.push_back(BuildMI(opCode, 2).addReg(LHS).addRegDef(DestVal));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001011 }
1012 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001013 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001014}
1015
1016
Vikram S. Adve74825322002-03-18 03:15:35 +00001017static void
1018CreateCodeForVariableSizeAlloca(const TargetMachine& target,
1019 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001020 unsigned tsize,
Vikram S. Adve74825322002-03-18 03:15:35 +00001021 Value* numElementsVal,
1022 vector<MachineInstr*>& getMvec)
1023{
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001024 Value* totalSizeVal;
Vikram S. Adve74825322002-03-18 03:15:35 +00001025 MachineInstr* M;
Vikram S. Adved3e26482002-10-13 00:18:57 +00001026 MachineCodeForInstruction& mcfi = MachineCodeForInstruction::get(result);
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001027 Function *F = result->getParent()->getParent();
Vikram S. Adved3e26482002-10-13 00:18:57 +00001028
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001029 // Enforce the alignment constraints on the stack pointer at
1030 // compile time if the total size is a known constant.
1031 if (isa<Constant>(numElementsVal))
1032 {
1033 bool isValid;
1034 int64_t numElem = GetConstantValueAsSignedInt(numElementsVal, isValid);
1035 assert(isValid && "Unexpectedly large array dimension in alloca!");
1036 int64_t total = numElem * tsize;
1037 if (int extra= total % target.getFrameInfo().getStackFrameSizeAlignment())
1038 total += target.getFrameInfo().getStackFrameSizeAlignment() - extra;
1039 totalSizeVal = ConstantSInt::get(Type::IntTy, total);
1040 }
1041 else
1042 {
1043 // The size is not a constant. Generate code to compute it and
1044 // code to pad the size for stack alignment.
1045 // Create a Value to hold the (constant) element size
1046 Value* tsizeVal = ConstantSInt::get(Type::IntTy, tsize);
1047
1048 // Create temporary values to hold the result of MUL, SLL, SRL
1049 // THIS CASE IS INCOMPLETE AND WILL BE FIXED SHORTLY.
1050 TmpInstruction* tmpProd = new TmpInstruction(numElementsVal, tsizeVal);
1051 TmpInstruction* tmpSLL = new TmpInstruction(numElementsVal, tmpProd);
1052 TmpInstruction* tmpSRL = new TmpInstruction(numElementsVal, tmpSLL);
1053 mcfi.addTemp(tmpProd);
1054 mcfi.addTemp(tmpSLL);
1055 mcfi.addTemp(tmpSRL);
1056
1057 // Instruction 1: mul numElements, typeSize -> tmpProd
1058 // This will optimize the MUL as far as possible.
1059 CreateMulInstruction(target, F, numElementsVal, tsizeVal, tmpProd,getMvec,
1060 mcfi, INVALID_MACHINE_OPCODE);
1061
1062 assert(0 && "Need to insert padding instructions here!");
1063
1064 totalSizeVal = tmpProd;
1065 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001066
1067 // Get the constant offset from SP for dynamically allocated storage
1068 // and create a temporary Value to hold it.
Misha Brukmanfce11432002-10-28 00:28:31 +00001069 MachineFunction& mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001070 bool growUp;
1071 ConstantSInt* dynamicAreaOffset =
1072 ConstantSInt::get(Type::IntTy,
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001073 target.getFrameInfo().getDynamicAreaOffset(mcInfo,growUp));
Vikram S. Adve74825322002-03-18 03:15:35 +00001074 assert(! growUp && "Has SPARC v9 stack frame convention changed?");
1075
Chris Lattner54e898e2003-01-15 19:23:34 +00001076 unsigned SPReg = target.getRegInfo().getStackPointer();
1077
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001078 // Instruction 2: sub %sp, totalSizeVal -> %sp
Chris Lattner54e898e2003-01-15 19:23:34 +00001079 getMvec.push_back(BuildMI(SUB, 3).addMReg(SPReg).addReg(totalSizeVal)
1080 .addMReg(SPReg,MOTy::Def));
Vikram S. Adveaabb5952002-10-29 19:37:31 +00001081
Vikram S. Adve74825322002-03-18 03:15:35 +00001082 // Instruction 3: add %sp, frameSizeBelowDynamicArea -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001083 getMvec.push_back(BuildMI(ADD, 3).addMReg(SPReg).addReg(dynamicAreaOffset)
1084 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001085}
1086
1087
1088static void
1089CreateCodeForFixedSizeAlloca(const TargetMachine& target,
1090 Instruction* result,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001091 unsigned tsize,
1092 unsigned numElements,
Vikram S. Adve74825322002-03-18 03:15:35 +00001093 vector<MachineInstr*>& getMvec)
1094{
Vikram S. Adved3e26482002-10-13 00:18:57 +00001095 assert(tsize > 0 && "Illegal (zero) type size for alloca");
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001096 assert(result && result->getParent() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001097 "Result value is not part of a function?");
1098 Function *F = result->getParent()->getParent();
Misha Brukmanfce11432002-10-28 00:28:31 +00001099 MachineFunction &mcInfo = MachineFunction::get(F);
Vikram S. Adve74825322002-03-18 03:15:35 +00001100
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001101 // Check if the offset would small enough to use as an immediate in
1102 // load/stores (check LDX because all load/stores have the same-size immediate
1103 // field). If not, put the variable in the dynamically sized area of the
1104 // frame.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001105 unsigned paddedSizeIgnored;
1106 int offsetFromFP = mcInfo.getInfo()->computeOffsetforLocalVar(result,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001107 paddedSizeIgnored,
Vikram S. Adve74825322002-03-18 03:15:35 +00001108 tsize * numElements);
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001109 if (! target.getInstrInfo().constantFitsInImmedField(LDX, offsetFromFP)) {
1110 CreateCodeForVariableSizeAlloca(target, result, tsize,
1111 ConstantSInt::get(Type::IntTy,numElements),
1112 getMvec);
1113 return;
1114 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001115
1116 // else offset fits in immediate field so go ahead and allocate it.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001117 offsetFromFP = mcInfo.getInfo()->allocateLocalVar(result, tsize *numElements);
Vikram S. Adve74825322002-03-18 03:15:35 +00001118
1119 // Create a temporary Value to hold the constant offset.
1120 // This is needed because it may not fit in the immediate field.
1121 ConstantSInt* offsetVal = ConstantSInt::get(Type::IntTy, offsetFromFP);
1122
1123 // Instruction 1: add %fp, offsetFromFP -> result
Chris Lattner54e898e2003-01-15 19:23:34 +00001124 unsigned FPReg = target.getRegInfo().getFramePointer();
1125 getMvec.push_back(BuildMI(ADD, 3).addMReg(FPReg).addReg(offsetVal)
1126 .addRegDef(result));
Vikram S. Adve74825322002-03-18 03:15:35 +00001127}
1128
1129
Chris Lattner20b1ea02001-09-14 03:47:57 +00001130//------------------------------------------------------------------------
1131// Function SetOperandsForMemInstr
1132//
1133// Choose addressing mode for the given load or store instruction.
1134// Use [reg+reg] if it is an indexed reference, and the index offset is
1135// not a constant or if it cannot fit in the offset field.
1136// Use [reg+offset] in all other cases.
1137//
1138// This assumes that all array refs are "lowered" to one of these forms:
1139// %x = load (subarray*) ptr, constant ; single constant offset
1140// %x = load (subarray*) ptr, offsetVal ; single non-constant offset
1141// Generally, this should happen via strength reduction + LICM.
1142// Also, strength reduction should take care of using the same register for
1143// the loop index variable and an array index, when that is profitable.
1144//------------------------------------------------------------------------
1145
1146static void
Chris Lattner54e898e2003-01-15 19:23:34 +00001147SetOperandsForMemInstr(unsigned Opcode,
1148 vector<MachineInstr*>& mvec,
Vikram S. Adveefc94332002-10-14 16:32:24 +00001149 InstructionNode* vmInstrNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001150 const TargetMachine& target)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001151{
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001152 Instruction* memInst = vmInstrNode->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001153 // Index vector, ptr value, and flag if all indices are const.
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001154 vector<Value*> idxVec;
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001155 bool allConstantIndices;
1156 Value* ptrVal = GetMemInstArgs(vmInstrNode, idxVec, allConstantIndices);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001157
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001158 // Now create the appropriate operands for the machine instruction.
1159 // First, initialize so we default to storing the offset in a register.
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001160 int64_t smallConstOffset = 0;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001161 Value* valueForRegOffset = NULL;
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001162 MachineOperand::MachineOperandType offsetOpType =
1163 MachineOperand::MO_VirtualRegister;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001164
Vikram S. Adve74825322002-03-18 03:15:35 +00001165 // Check if there is an index vector and if so, compute the
1166 // right offset for structures and for arrays
Chris Lattner20b1ea02001-09-14 03:47:57 +00001167 //
Chris Lattner3bb8ad22002-08-22 23:37:24 +00001168 if (!idxVec.empty())
Chris Lattner20b1ea02001-09-14 03:47:57 +00001169 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001170 const PointerType* ptrType = cast<PointerType>(ptrVal->getType());
Chris Lattner20b1ea02001-09-14 03:47:57 +00001171
Vikram S. Adve242a8082002-05-19 15:25:51 +00001172 // If all indices are constant, compute the combined offset directly.
1173 if (allConstantIndices)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001174 {
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001175 // Compute the offset value using the index vector. Create a
1176 // virtual reg. for it since it may not fit in the immed field.
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001177 uint64_t offset = target.getTargetData().getIndexedOffset(ptrType,idxVec);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001178 valueForRegOffset = ConstantSInt::get(Type::LongTy, offset);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001179 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001180 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001181 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001182 // There is at least one non-constant offset. Therefore, this must
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001183 // be an array ref, and must have been lowered to a single non-zero
1184 // offset. (An extra leading zero offset, if any, can be ignored.)
1185 // Generate code sequence to compute address from index.
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001186 //
Chris Lattner795ba6c2003-01-15 21:36:50 +00001187 bool firstIdxIsZero = IsZero(idxVec[0]);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001188 assert(idxVec.size() == 1U + firstIdxIsZero
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001189 && "Array refs must be lowered before Instruction Selection");
1190
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001191 Value* idxVal = idxVec[firstIdxIsZero];
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001192
1193 vector<MachineInstr*> mulVec;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001194 Instruction* addr = new TmpInstruction(Type::ULongTy, memInst);
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001195 MachineCodeForInstruction::get(memInst).addTemp(addr);
1196
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001197 // Get the array type indexed by idxVal, and compute its element size.
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001198 // The call to getTypeSize() will fail if size is not constant.
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001199 const Type* vecType = (firstIdxIsZero
1200 ? GetElementPtrInst::getIndexedType(ptrType,
1201 std::vector<Value*>(1U, idxVec[0]),
1202 /*AllowCompositeLeaf*/ true)
1203 : ptrType);
1204 const Type* eltType = cast<SequentialType>(vecType)->getElementType();
Vikram S. Advee102a642002-09-16 15:56:45 +00001205 ConstantUInt* eltSizeVal = ConstantUInt::get(Type::ULongTy,
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001206 target.getTargetData().getTypeSize(eltType));
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001207
1208 // CreateMulInstruction() folds constants intelligently enough.
Vikram S. Adved3e26482002-10-13 00:18:57 +00001209 CreateMulInstruction(target, memInst->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001210 idxVal, /* lval, not likely to be const*/
1211 eltSizeVal, /* rval, likely to be constant */
1212 addr, /* result */
Vikram S. Adved3e26482002-10-13 00:18:57 +00001213 mulVec, MachineCodeForInstruction::get(memInst),
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001214 INVALID_MACHINE_OPCODE);
1215
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001216 assert(mulVec.size() > 0 && "No multiply code created?");
Chris Lattner54e898e2003-01-15 19:23:34 +00001217 mvec.insert(mvec.end(), mulVec.begin(), mulVec.end());
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001218
1219 valueForRegOffset = addr;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001220 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001221 }
1222 else
1223 {
1224 offsetOpType = MachineOperand::MO_SignExtendedImmed;
1225 smallConstOffset = 0;
1226 }
Vikram S. Adveed3fefb2002-08-03 13:48:21 +00001227
Vikram S. Advea10d1a72002-03-31 19:07:35 +00001228 // For STORE:
1229 // Operand 0 is value, operand 1 is ptr, operand 2 is offset
1230 // For LOAD or GET_ELEMENT_PTR,
1231 // Operand 0 is ptr, operand 1 is offset, operand 2 is result.
1232 //
1233 unsigned offsetOpNum, ptrOpNum;
Chris Lattner54e898e2003-01-15 19:23:34 +00001234 MachineInstr *MI;
1235 if (memInst->getOpcode() == Instruction::Store) {
1236 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1237 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1238 .addReg(ptrVal).addReg(valueForRegOffset);
1239 else
1240 MI = BuildMI(Opcode, 3).addReg(vmInstrNode->leftChild()->getValue())
1241 .addReg(ptrVal).addSImm(smallConstOffset);
1242 } else {
1243 if (offsetOpType == MachineOperand::MO_VirtualRegister)
1244 MI = BuildMI(Opcode, 3).addReg(ptrVal).addReg(valueForRegOffset)
1245 .addRegDef(memInst);
1246 else
1247 MI = BuildMI(Opcode, 3).addReg(ptrVal).addSImm(smallConstOffset)
1248 .addRegDef(memInst);
1249 }
1250 mvec.push_back(MI);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001251}
1252
1253
Chris Lattner20b1ea02001-09-14 03:47:57 +00001254//
1255// Substitute operand `operandNum' of the instruction in node `treeNode'
Vikram S. Advec025fc12001-10-14 23:28:43 +00001256// in place of the use(s) of that instruction in node `parent'.
1257// Check both explicit and implicit operands!
Vikram S. Adve74825322002-03-18 03:15:35 +00001258// Also make sure to skip over a parent who:
1259// (1) is a list node in the Burg tree, or
1260// (2) itself had its results forwarded to its parent
Chris Lattner20b1ea02001-09-14 03:47:57 +00001261//
1262static void
1263ForwardOperand(InstructionNode* treeNode,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001264 InstrTreeNode* parent,
1265 int operandNum)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001266{
Vikram S. Adve243dd452001-09-18 13:03:13 +00001267 assert(treeNode && parent && "Invalid invocation of ForwardOperand");
1268
Chris Lattner20b1ea02001-09-14 03:47:57 +00001269 Instruction* unusedOp = treeNode->getInstruction();
1270 Value* fwdOp = unusedOp->getOperand(operandNum);
Vikram S. Adve243dd452001-09-18 13:03:13 +00001271
1272 // The parent itself may be a list node, so find the real parent instruction
1273 while (parent->getNodeType() != InstrTreeNode::NTInstructionNode)
1274 {
1275 parent = parent->parent();
1276 assert(parent && "ERROR: Non-instruction node has no parent in tree.");
1277 }
1278 InstructionNode* parentInstrNode = (InstructionNode*) parent;
1279
1280 Instruction* userInstr = parentInstrNode->getInstruction();
Chris Lattner9c461082002-02-03 07:50:56 +00001281 MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(userInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00001282
1283 // The parent's mvec would be empty if it was itself forwarded.
1284 // Recursively call ForwardOperand in that case...
1285 //
1286 if (mvec.size() == 0)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001287 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001288 assert(parent->parent() != NULL &&
1289 "Parent could not have been forwarded, yet has no instructions?");
1290 ForwardOperand(treeNode, parent->parent(), operandNum);
1291 }
1292 else
1293 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001294 for (unsigned i=0, N=mvec.size(); i < N; i++)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001295 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001296 MachineInstr* minstr = mvec[i];
1297 for (unsigned i=0, numOps=minstr->getNumOperands(); i < numOps; ++i)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001298 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001299 const MachineOperand& mop = minstr->getOperand(i);
Chris Lattner133f0792002-10-28 04:45:29 +00001300 if (mop.getType() == MachineOperand::MO_VirtualRegister &&
Vikram S. Adve74825322002-03-18 03:15:35 +00001301 mop.getVRegValue() == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001302 minstr->SetMachineOperandVal(i,
Vikram S. Adve74825322002-03-18 03:15:35 +00001303 MachineOperand::MO_VirtualRegister, fwdOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001304 }
Vikram S. Adve74825322002-03-18 03:15:35 +00001305
1306 for (unsigned i=0,numOps=minstr->getNumImplicitRefs(); i<numOps; ++i)
1307 if (minstr->getImplicitRef(i) == unusedOp)
Vikram S. Adve242a8082002-05-19 15:25:51 +00001308 minstr->setImplicitRef(i, fwdOp,
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001309 minstr->implicitRefIsDefined(i),
1310 minstr->implicitRefIsDefinedAndUsed(i));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001311 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001312 }
1313}
1314
1315
Vikram S. Adve242a8082002-05-19 15:25:51 +00001316inline bool
1317AllUsesAreBranches(const Instruction* setccI)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001318{
Vikram S. Adve242a8082002-05-19 15:25:51 +00001319 for (Value::use_const_iterator UI=setccI->use_begin(), UE=setccI->use_end();
1320 UI != UE; ++UI)
1321 if (! isa<TmpInstruction>(*UI) // ignore tmp instructions here
1322 && cast<Instruction>(*UI)->getOpcode() != Instruction::Br)
1323 return false;
1324 return true;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001325}
1326
Vikram S. Advefb361122001-10-22 13:36:31 +00001327//******************* Externally Visible Functions *************************/
1328
Vikram S. Advefb361122001-10-22 13:36:31 +00001329//------------------------------------------------------------------------
1330// External Function: ThisIsAChainRule
1331//
1332// Purpose:
1333// Check if a given BURG rule is a chain rule.
1334//------------------------------------------------------------------------
1335
1336extern bool
1337ThisIsAChainRule(int eruleno)
1338{
1339 switch(eruleno)
1340 {
1341 case 111: // stmt: reg
Vikram S. Advefb361122001-10-22 13:36:31 +00001342 case 123:
1343 case 124:
1344 case 125:
1345 case 126:
1346 case 127:
1347 case 128:
1348 case 129:
1349 case 130:
1350 case 131:
1351 case 132:
1352 case 133:
1353 case 155:
1354 case 221:
1355 case 222:
1356 case 241:
1357 case 242:
1358 case 243:
1359 case 244:
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001360 case 245:
Vikram S. Adve85e1e9c2002-04-01 20:28:48 +00001361 case 321:
Vikram S. Advefb361122001-10-22 13:36:31 +00001362 return true; break;
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001363
Vikram S. Advefb361122001-10-22 13:36:31 +00001364 default:
1365 return false; break;
1366 }
1367}
Chris Lattner20b1ea02001-09-14 03:47:57 +00001368
1369
1370//------------------------------------------------------------------------
1371// External Function: GetInstructionsByRule
1372//
1373// Purpose:
1374// Choose machine instructions for the SPARC according to the
1375// patterns chosen by the BURG-generated parser.
1376//------------------------------------------------------------------------
1377
Vikram S. Adve74825322002-03-18 03:15:35 +00001378void
Chris Lattner20b1ea02001-09-14 03:47:57 +00001379GetInstructionsByRule(InstructionNode* subtreeRoot,
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001380 int ruleForNode,
1381 short* nts,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001382 TargetMachine &target,
Vikram S. Adve74825322002-03-18 03:15:35 +00001383 vector<MachineInstr*>& mvec)
Chris Lattner20b1ea02001-09-14 03:47:57 +00001384{
Chris Lattner20b1ea02001-09-14 03:47:57 +00001385 bool checkCast = false; // initialize here to use fall-through
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001386 bool maskUnsignedResult = false;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001387 int nextRule;
1388 int forwardOperandNum = -1;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001389 unsigned allocaSize = 0;
Vikram S. Adve74825322002-03-18 03:15:35 +00001390 MachineInstr* M, *M2;
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001391 unsigned L;
Vikram S. Adve74825322002-03-18 03:15:35 +00001392
1393 mvec.clear();
Chris Lattner20b1ea02001-09-14 03:47:57 +00001394
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001395 // If the code for this instruction was folded into the parent (user),
1396 // then do nothing!
1397 if (subtreeRoot->isFoldedIntoParent())
1398 return;
1399
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001400 //
1401 // Let's check for chain rules outside the switch so that we don't have
1402 // to duplicate the list of chain rule production numbers here again
1403 //
1404 if (ThisIsAChainRule(ruleForNode))
Chris Lattner20b1ea02001-09-14 03:47:57 +00001405 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001406 // Chain rules have a single nonterminal on the RHS.
1407 // Get the rule that matches the RHS non-terminal and use that instead.
1408 //
1409 assert(nts[0] && ! nts[1]
1410 && "A chain rule should have only one RHS non-terminal!");
1411 nextRule = burm_rule(subtreeRoot->state, nts[0]);
1412 nts = burm_nts[nextRule];
Vikram S. Adve74825322002-03-18 03:15:35 +00001413 GetInstructionsByRule(subtreeRoot, nextRule, nts, target, mvec);
Chris Lattner20b1ea02001-09-14 03:47:57 +00001414 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001415 else
Chris Lattner20b1ea02001-09-14 03:47:57 +00001416 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001417 switch(ruleForNode) {
1418 case 1: // stmt: Ret
1419 case 2: // stmt: RetValue(reg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001420 { // NOTE: Prepass of register allocation is responsible
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001421 // for moving return value to appropriate register.
1422 // Mark the return-address register as a hidden virtual reg.
Vikram S. Advea995e602001-10-11 04:23:19 +00001423 // Mark the return value register as an implicit ref of
1424 // the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001425 // Finally put a NOP in the delay slot.
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001426 ReturnInst *returnInstr =
1427 cast<ReturnInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001428 assert(returnInstr->getOpcode() == Instruction::Ret);
1429
Chris Lattner9c461082002-02-03 07:50:56 +00001430 Instruction* returnReg = new TmpInstruction(returnInstr);
1431 MachineCodeForInstruction::get(returnInstr).addTemp(returnReg);
Vikram S. Advefb361122001-10-22 13:36:31 +00001432
Chris Lattner4690e6d2003-01-15 18:11:11 +00001433 M = BuildMI(JMPLRET, 3).addReg(returnReg).addSImm(8)
1434 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001435
Vikram S. Advea995e602001-10-11 04:23:19 +00001436 if (returnInstr->getReturnValue() != NULL)
Vikram S. Adve74825322002-03-18 03:15:35 +00001437 M->addImplicitRef(returnInstr->getReturnValue());
Vikram S. Advea995e602001-10-11 04:23:19 +00001438
Vikram S. Adve74825322002-03-18 03:15:35 +00001439 mvec.push_back(M);
Chris Lattner4690e6d2003-01-15 18:11:11 +00001440 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001441
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001442 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001443 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001444
1445 case 3: // stmt: Store(reg,reg)
1446 case 4: // stmt: Store(reg,ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00001447 SetOperandsForMemInstr(ChooseStoreInstruction(
1448 subtreeRoot->leftChild()->getValue()->getType()),
1449 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001450 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001451
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001452 case 5: // stmt: BrUncond
Chris Lattner54e898e2003-01-15 19:23:34 +00001453 {
1454 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1455 mvec.push_back(BuildMI(BA, 1).addPCDisp(BI->getSuccessor(0)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001456
Chris Lattner54e898e2003-01-15 19:23:34 +00001457 // delay slot
1458 mvec.push_back(BuildMI(NOP, 0));
1459 break;
1460 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001461
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001462 case 206: // stmt: BrCond(setCCconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001463 { // setCCconst => boolean was computed with `%b = setCC type reg1 const'
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001464 // If the constant is ZERO, we can use the branch-on-integer-register
1465 // instructions and avoid the SUBcc instruction entirely.
1466 // Otherwise this is just the same as case 5, so just fall through.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001467 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001468 InstrTreeNode* constNode = subtreeRoot->leftChild()->rightChild();
1469 assert(constNode &&
1470 constNode->getNodeType() ==InstrTreeNode::NTConstNode);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001471 Constant *constVal = cast<Constant>(constNode->getValue());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001472 bool isValidConst;
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001473
Chris Lattner0c4e8862002-09-03 01:08:28 +00001474 if ((constVal->getType()->isInteger()
Chris Lattner9b625032002-05-06 16:15:30 +00001475 || isa<PointerType>(constVal->getType()))
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001476 && GetConstantValueAsSignedInt(constVal, isValidConst) == 0
1477 && isValidConst)
1478 {
1479 // That constant is a zero after all...
1480 // Use the left child of setCC as the first argument!
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001481 // Mark the setCC node so that no code is generated for it.
1482 InstructionNode* setCCNode = (InstructionNode*)
1483 subtreeRoot->leftChild();
1484 assert(setCCNode->getOpLabel() == SetCCOp);
1485 setCCNode->markFoldedIntoParent();
1486
1487 BranchInst* brInst=cast<BranchInst>(subtreeRoot->getInstruction());
1488
Chris Lattner54e898e2003-01-15 19:23:34 +00001489 M = BuildMI(ChooseBprInstruction(subtreeRoot), 2)
1490 .addReg(setCCNode->leftChild()->getValue())
1491 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001492 mvec.push_back(M);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001493
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001494 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001495 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001496
1497 // false branch
Chris Lattner54e898e2003-01-15 19:23:34 +00001498 mvec.push_back(BuildMI(BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001499
1500 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001501 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001502 break;
1503 }
1504 // ELSE FALL THROUGH
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001505 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001506
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001507 case 6: // stmt: BrCond(setCC)
1508 { // bool => boolean was computed with SetCC.
1509 // The branch to use depends on whether it is FP, signed, or unsigned.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001510 // If it is an integer CC, we also need to find the unique
1511 // TmpInstruction representing that CC.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001512 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001513 BranchInst* brInst = cast<BranchInst>(subtreeRoot->getInstruction());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001514 bool isFPBranch;
Chris Lattner54e898e2003-01-15 19:23:34 +00001515 unsigned Opcode = ChooseBccInstruction(subtreeRoot, isFPBranch);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001516 Value* ccValue = GetTmpForCC(subtreeRoot->leftChild()->getValue(),
1517 brInst->getParent()->getParent(),
1518 isFPBranch? Type::FloatTy : Type::IntTy);
Chris Lattner54e898e2003-01-15 19:23:34 +00001519 M = BuildMI(Opcode, 2).addCCReg(ccValue)
1520 .addPCDisp(brInst->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001521 mvec.push_back(M);
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001522
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001523 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001524 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001525
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001526 // false branch
Chris Lattner54e898e2003-01-15 19:23:34 +00001527 mvec.push_back(BuildMI(BA, 1).addPCDisp(brInst->getSuccessor(1)));
Vikram S. Adve30a6f492002-08-22 02:56:10 +00001528
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001529 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001530 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001531 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001532 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001533
1534 case 208: // stmt: BrCond(boolconst)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001535 {
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001536 // boolconst => boolean is a constant; use BA to first or second label
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001537 Constant* constVal =
1538 cast<Constant>(subtreeRoot->leftChild()->getValue());
1539 unsigned dest = cast<ConstantBool>(constVal)->getValue()? 0 : 1;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001540
Chris Lattner54e898e2003-01-15 19:23:34 +00001541 M = BuildMI(BA, 1).addPCDisp(
Chris Lattner35504202002-04-27 03:14:39 +00001542 cast<BranchInst>(subtreeRoot->getInstruction())->getSuccessor(dest));
Vikram S. Adve74825322002-03-18 03:15:35 +00001543 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001544
1545 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001546 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001547 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001548 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001549
1550 case 8: // stmt: BrCond(boolreg)
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001551 { // boolreg => boolean is stored in an existing register.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001552 // Just use the branch-on-integer-register instruction!
1553 //
Chris Lattner54e898e2003-01-15 19:23:34 +00001554 BranchInst *BI = cast<BranchInst>(subtreeRoot->getInstruction());
1555 M = BuildMI(BRNZ, 2).addReg(subtreeRoot->leftChild()->getValue())
1556 .addPCDisp(BI->getSuccessor(0));
Vikram S. Adve74825322002-03-18 03:15:35 +00001557 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001558
1559 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001560 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001561
1562 // false branch
Chris Lattner54e898e2003-01-15 19:23:34 +00001563 mvec.push_back(BuildMI(BA, 1).addPCDisp(BI->getSuccessor(1)));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001564
1565 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00001566 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001567 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001568 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00001569
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001570 case 9: // stmt: Switch(reg)
1571 assert(0 && "*** SWITCH instruction is not implemented yet.");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001572 break;
Chris Lattner20b1ea02001-09-14 03:47:57 +00001573
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001574 case 10: // reg: VRegList(reg, reg)
1575 assert(0 && "VRegList should never be the topmost non-chain rule");
1576 break;
1577
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001578 case 21: // bool: Not(bool,reg): Both these are implemented as:
1579 case 421: // reg: BNot(reg,reg): reg = reg XOR-NOT 0
1580 { // First find the unary operand. It may be left or right, usually right.
1581 Value* notArg = BinaryOperator::getNotArgument(
1582 cast<BinaryOperator>(subtreeRoot->getInstruction()));
Chris Lattner00dca912003-01-15 17:47:49 +00001583 unsigned ZeroReg = target.getRegInfo().getZeroRegNum();
1584 mvec.push_back(BuildMI(XNOR, 3).addReg(notArg).addMReg(ZeroReg)
1585 .addRegDef(subtreeRoot->getValue()));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001586 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001587 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001588
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001589 case 22: // reg: ToBoolTy(reg):
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001590 {
1591 const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001592 assert(opType->isIntegral() || isa<PointerType>(opType));
Vikram S. Adve74825322002-03-18 03:15:35 +00001593 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001594 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001595 }
1596
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001597 case 23: // reg: ToUByteTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001598 case 24: // reg: ToSByteTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001599 case 25: // reg: ToUShortTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001600 case 26: // reg: ToShortTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001601 case 27: // reg: ToUIntTy(reg)
Vikram S. Adve94c40812002-09-27 14:33:08 +00001602 case 28: // reg: ToIntTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001603 {
Vikram S. Adve94c40812002-09-27 14:33:08 +00001604 //======================================================================
1605 // Rules for integer conversions:
1606 //
1607 //--------
1608 // From ISO 1998 C++ Standard, Sec. 4.7:
1609 //
1610 // 2. If the destination type is unsigned, the resulting value is
1611 // the least unsigned integer congruent to the source integer
1612 // (modulo 2n where n is the number of bits used to represent the
1613 // unsigned type). [Note: In a two s complement representation,
1614 // this conversion is conceptual and there is no change in the
1615 // bit pattern (if there is no truncation). ]
1616 //
1617 // 3. If the destination type is signed, the value is unchanged if
1618 // it can be represented in the destination type (and bitfield width);
1619 // otherwise, the value is implementation-defined.
1620 //--------
1621 //
1622 // Since we assume 2s complement representations, this implies:
1623 //
1624 // -- if operand is smaller than destination, zero-extend or sign-extend
1625 // according to the signedness of the *operand*: source decides.
1626 // ==> we have to do nothing here!
1627 //
1628 // -- if operand is same size as or larger than destination, and the
1629 // destination is *unsigned*, zero-extend the operand: dest. decides
1630 //
1631 // -- if operand is same size as or larger than destination, and the
1632 // destination is *signed*, the choice is implementation defined:
1633 // we sign-extend the operand: i.e., again dest. decides.
1634 // Note: this matches both Sun's cc and gcc3.2.
1635 //======================================================================
1636
Vikram S. Adve242a8082002-05-19 15:25:51 +00001637 Instruction* destI = subtreeRoot->getInstruction();
1638 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve94c40812002-09-27 14:33:08 +00001639 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001640 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve1e606692002-07-31 21:01:34 +00001641 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001642 unsigned opSize = target.getTargetData().getTypeSize(opType);
1643 unsigned destSize = target.getTargetData().getTypeSize(destI->getType());
Vikram S. Adve94c40812002-09-27 14:33:08 +00001644 if (opSize >= destSize)
1645 { // Operand is same size as or larger than dest:
1646 // zero- or sign-extend, according to the signeddness of
1647 // the destination (see above).
1648 if (destI->getType()->isSigned())
1649 target.getInstrInfo().CreateSignExtensionInstructions(target,
1650 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1651 mvec, MachineCodeForInstruction::get(destI));
1652 else
1653 target.getInstrInfo().CreateZeroExtensionInstructions(target,
1654 destI->getParent()->getParent(), opVal, destI, 8*destSize,
1655 mvec, MachineCodeForInstruction::get(destI));
Vikram S. Adve1e606692002-07-31 21:01:34 +00001656 }
1657 else
1658 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve242a8082002-05-19 15:25:51 +00001659 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001660 else if (opType->isFloatingPoint())
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001661 {
1662 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1663 MachineCodeForInstruction::get(destI));
Vikram S. Adve94c40812002-09-27 14:33:08 +00001664 if (destI->getType()->isUnsigned())
1665 maskUnsignedResult = true; // not handled by fp->int code
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00001666 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00001667 else
Vikram S. Adve1e606692002-07-31 21:01:34 +00001668 assert(0 && "Unrecognized operand type for convert-to-unsigned");
1669
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001670 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001671 }
Vikram S. Adve94c40812002-09-27 14:33:08 +00001672
1673 case 29: // reg: ToULongTy(reg)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001674 case 30: // reg: ToLongTy(reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001675 {
Vikram S. Adve242a8082002-05-19 15:25:51 +00001676 Value* opVal = subtreeRoot->leftChild()->getValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001677 const Type* opType = opVal->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00001678 if (opType->isIntegral() || isa<PointerType>(opType))
Vikram S. Adve94c40812002-09-27 14:33:08 +00001679 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve1e606692002-07-31 21:01:34 +00001680 else if (opType->isFloatingPoint())
Vikram S. Adve94c40812002-09-27 14:33:08 +00001681 {
1682 Instruction* destI = subtreeRoot->getInstruction();
1683 CreateCodeToConvertFloatToInt(target, opVal, destI, mvec,
1684 MachineCodeForInstruction::get(destI));
1685 }
Vikram S. Adve1e606692002-07-31 21:01:34 +00001686 else
1687 assert(0 && "Unrecognized operand type for convert-to-signed");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001688 break;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001689 }
1690
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001691 case 31: // reg: ToFloatTy(reg):
1692 case 32: // reg: ToDoubleTy(reg):
1693 case 232: // reg: ToDoubleTy(Constant):
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001694
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001695 // If this instruction has a parent (a user) in the tree
1696 // and the user is translated as an FsMULd instruction,
1697 // then the cast is unnecessary. So check that first.
1698 // In the future, we'll want to do the same for the FdMULq instruction,
1699 // so do the check here instead of only for ToFloatTy(reg).
1700 //
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001701 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001702 {
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001703 const MachineCodeForInstruction& mcfi =
1704 MachineCodeForInstruction::get(
1705 cast<InstructionNode>(subtreeRoot->parent())->getInstruction());
1706 if (mcfi.size() == 0 || mcfi.front()->getOpCode() == FSMULD)
1707 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001708 }
Vikram S. Adveec7f4822002-09-09 14:54:21 +00001709
1710 if (forwardOperandNum != 0) // we do need the cast
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001711 {
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001712 Value* leftVal = subtreeRoot->leftChild()->getValue();
1713 const Type* opType = leftVal->getType();
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001714 MachineOpCode opCode=ChooseConvertToFloatInstr(
1715 subtreeRoot->getOpLabel(), opType);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001716 if (opCode == INVALID_OPCODE) // no conversion needed
1717 {
Vikram S. Adve74825322002-03-18 03:15:35 +00001718 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001719 }
1720 else
1721 {
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001722 // If the source operand is a non-FP type it must be
1723 // first copied from int to float register via memory!
1724 Instruction *dest = subtreeRoot->getInstruction();
1725 Value* srcForCast;
1726 int n = 0;
Vikram S. Adve242a8082002-05-19 15:25:51 +00001727 if (! opType->isFloatingPoint())
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001728 {
1729 // Create a temporary to represent the FP register
1730 // into which the integer will be copied via memory.
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001731 // The type of this temporary will determine the FP
1732 // register used: single-prec for a 32-bit int or smaller,
1733 // double-prec for a 64-bit int.
1734 //
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001735 uint64_t srcSize =
Chris Lattnerea45d7b2002-12-28 20:19:44 +00001736 target.getTargetData().getTypeSize(leftVal->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001737 Type* tmpTypeToUse =
1738 (srcSize <= 4)? Type::FloatTy : Type::DoubleTy;
1739 srcForCast = new TmpInstruction(tmpTypeToUse, dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001740 MachineCodeForInstruction &destMCFI =
Chris Lattner9c461082002-02-03 07:50:56 +00001741 MachineCodeForInstruction::get(dest);
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00001742 destMCFI.addTemp(srcForCast);
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001743
Vikram S. Adve242a8082002-05-19 15:25:51 +00001744 target.getInstrInfo().CreateCodeToCopyIntToFloat(target,
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001745 dest->getParent()->getParent(),
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00001746 leftVal, cast<Instruction>(srcForCast),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001747 mvec, destMCFI);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001748 }
1749 else
1750 srcForCast = leftVal;
Vikram S. Adve94c40812002-09-27 14:33:08 +00001751
Chris Lattner54e898e2003-01-15 19:23:34 +00001752 M = BuildMI(opCode, 2).addReg(srcForCast).addRegDef(dest);
Vikram S. Adve74825322002-03-18 03:15:35 +00001753 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001754 }
1755 }
1756 break;
1757
1758 case 19: // reg: ToArrayTy(reg):
1759 case 20: // reg: ToPointerTy(reg):
Vikram S. Adve74825322002-03-18 03:15:35 +00001760 forwardOperandNum = 0; // forward first operand to user
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001761 break;
1762
1763 case 233: // reg: Add(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001764 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001765 M = CreateAddConstInstruction(subtreeRoot);
1766 if (M != NULL)
1767 {
1768 mvec.push_back(M);
1769 break;
1770 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001771 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001772
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001773 case 33: // reg: Add(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001774 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001775 Add3OperandInstr(ChooseAddInstruction(subtreeRoot), subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001776 break;
1777
1778 case 234: // reg: Sub(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001779 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001780 M = CreateSubConstInstruction(subtreeRoot);
1781 if (M != NULL)
1782 {
1783 mvec.push_back(M);
1784 break;
1785 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001786 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001787
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001788 case 34: // reg: Sub(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001789 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001790 Add3OperandInstr(ChooseSubInstructionByType(
1791 subtreeRoot->getInstruction()->getType()),
1792 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001793 break;
1794
1795 case 135: // reg: Mul(todouble, todouble)
1796 checkCast = true;
1797 // FALL THROUGH
1798
1799 case 35: // reg: Mul(reg, reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00001800 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001801 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001802 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1803 ? FSMULD
1804 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001805 Instruction* mulInstr = subtreeRoot->getInstruction();
1806 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001807 subtreeRoot->leftChild()->getValue(),
1808 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001809 mulInstr, mvec,
1810 MachineCodeForInstruction::get(mulInstr),forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001811 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001812 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001813 case 335: // reg: Mul(todouble, todoubleConst)
1814 checkCast = true;
1815 // FALL THROUGH
1816
1817 case 235: // reg: Mul(reg, Constant)
Vikram S. Adve74825322002-03-18 03:15:35 +00001818 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001819 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001820 MachineOpCode forceOp = ((checkCast && BothFloatToDouble(subtreeRoot))
1821 ? FSMULD
1822 : INVALID_MACHINE_OPCODE);
Vikram S. Adve242a8082002-05-19 15:25:51 +00001823 Instruction* mulInstr = subtreeRoot->getInstruction();
1824 CreateMulInstruction(target, mulInstr->getParent()->getParent(),
Vikram S. Adve74825322002-03-18 03:15:35 +00001825 subtreeRoot->leftChild()->getValue(),
1826 subtreeRoot->rightChild()->getValue(),
Vikram S. Adve242a8082002-05-19 15:25:51 +00001827 mulInstr, mvec,
1828 MachineCodeForInstruction::get(mulInstr),
1829 forceOp);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001830 break;
Vikram S. Adve74825322002-03-18 03:15:35 +00001831 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001832 case 236: // reg: Div(reg, Constant)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001833 maskUnsignedResult = true;
Vikram S. Adve74825322002-03-18 03:15:35 +00001834 L = mvec.size();
1835 CreateDivConstInstruction(target, subtreeRoot, mvec);
1836 if (mvec.size() > L)
1837 break;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001838 // ELSE FALL THROUGH
Vikram S. Adve74825322002-03-18 03:15:35 +00001839
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001840 case 36: // reg: Div(reg, reg)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001841 maskUnsignedResult = true;
Chris Lattner54e898e2003-01-15 19:23:34 +00001842 Add3OperandInstr(ChooseDivInstruction(target, subtreeRoot),
1843 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001844 break;
1845
1846 case 37: // reg: Rem(reg, reg)
1847 case 237: // reg: Rem(reg, Constant)
Vikram S. Adve510eec72001-11-04 21:59:14 +00001848 {
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00001849 maskUnsignedResult = true;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001850 Instruction* remInstr = subtreeRoot->getInstruction();
1851
Chris Lattner9c461082002-02-03 07:50:56 +00001852 TmpInstruction* quot = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001853 subtreeRoot->leftChild()->getValue(),
1854 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001855 TmpInstruction* prod = new TmpInstruction(
Vikram S. Adve510eec72001-11-04 21:59:14 +00001856 quot,
1857 subtreeRoot->rightChild()->getValue());
Chris Lattner9c461082002-02-03 07:50:56 +00001858 MachineCodeForInstruction::get(remInstr).addTemp(quot).addTemp(prod);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001859
Chris Lattner54e898e2003-01-15 19:23:34 +00001860 M = BuildMI(ChooseDivInstruction(target, subtreeRoot), 3)
1861 .addReg(subtreeRoot->leftChild()->getValue())
1862 .addReg(subtreeRoot->rightChild()->getValue())
1863 .addRegDef(quot);
Vikram S. Adve74825322002-03-18 03:15:35 +00001864 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001865
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001866 unsigned MulOpcode =
1867 ChooseMulInstructionByType(subtreeRoot->getInstruction()->getType());
1868 Value *MulRHS = subtreeRoot->rightChild()->getValue();
1869 M = BuildMI(MulOpcode, 3).addReg(quot).addReg(MulRHS).addReg(prod,
1870 MOTy::Def);
Vikram S. Adve74825322002-03-18 03:15:35 +00001871 mvec.push_back(M);
Vikram S. Adve510eec72001-11-04 21:59:14 +00001872
Chris Lattner54e898e2003-01-15 19:23:34 +00001873 unsigned Opcode = ChooseSubInstructionByType(
1874 subtreeRoot->getInstruction()->getType());
1875 M = BuildMI(Opcode, 3).addReg(subtreeRoot->leftChild()->getValue())
1876 .addReg(prod).addRegDef(subtreeRoot->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00001877 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001878 break;
Vikram S. Adve510eec72001-11-04 21:59:14 +00001879 }
1880
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001881 case 38: // bool: And(bool, bool)
1882 case 238: // bool: And(bool, boolconst)
1883 case 338: // reg : BAnd(reg, reg)
1884 case 538: // reg : BAnd(reg, Constant)
Chris Lattner54e898e2003-01-15 19:23:34 +00001885 Add3OperandInstr(AND, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001886 break;
1887
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001888 case 138: // bool: And(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001889 case 438: // bool: BAnd(bool, bnot)
1890 { // Use the argument of NOT as the second argument!
1891 // Mark the NOT node so that no code is generated for it.
1892 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1893 Value* notArg = BinaryOperator::getNotArgument(
1894 cast<BinaryOperator>(notNode->getInstruction()));
1895 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001896 Value *LHS = subtreeRoot->leftChild()->getValue();
1897 Value *Dest = subtreeRoot->getValue();
1898 mvec.push_back(BuildMI(ANDN, 3).addReg(LHS).addReg(notArg)
1899 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001900 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001901 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001902
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001903 case 39: // bool: Or(bool, bool)
1904 case 239: // bool: Or(bool, boolconst)
1905 case 339: // reg : BOr(reg, reg)
1906 case 539: // reg : BOr(reg, Constant)
Chris Lattner54e898e2003-01-15 19:23:34 +00001907 Add3OperandInstr(OR, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001908 break;
1909
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001910 case 139: // bool: Or(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001911 case 439: // bool: BOr(bool, bnot)
1912 { // Use the argument of NOT as the second argument!
1913 // Mark the NOT node so that no code is generated for it.
1914 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1915 Value* notArg = BinaryOperator::getNotArgument(
1916 cast<BinaryOperator>(notNode->getInstruction()));
1917 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001918 Value *LHS = subtreeRoot->leftChild()->getValue();
1919 Value *Dest = subtreeRoot->getValue();
1920 mvec.push_back(BuildMI(ORN, 3).addReg(LHS).addReg(notArg)
1921 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001922 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001923 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001924
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001925 case 40: // bool: Xor(bool, bool)
1926 case 240: // bool: Xor(bool, boolconst)
1927 case 340: // reg : BXor(reg, reg)
1928 case 540: // reg : BXor(reg, Constant)
Chris Lattner54e898e2003-01-15 19:23:34 +00001929 Add3OperandInstr(XOR, subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001930 break;
1931
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001932 case 140: // bool: Xor(bool, not)
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001933 case 440: // bool: BXor(bool, bnot)
1934 { // Use the argument of NOT as the second argument!
1935 // Mark the NOT node so that no code is generated for it.
1936 InstructionNode* notNode = (InstructionNode*) subtreeRoot->rightChild();
1937 Value* notArg = BinaryOperator::getNotArgument(
1938 cast<BinaryOperator>(notNode->getInstruction()));
1939 notNode->markFoldedIntoParent();
Chris Lattnere5b1ed92003-01-15 00:03:28 +00001940 Value *LHS = subtreeRoot->leftChild()->getValue();
1941 Value *Dest = subtreeRoot->getValue();
1942 mvec.push_back(BuildMI(XNOR, 3).addReg(LHS).addReg(notArg)
1943 .addReg(Dest, MOTy::Def));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001944 break;
Vikram S. Advece08e1d2002-08-15 14:17:37 +00001945 }
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001946
1947 case 41: // boolconst: SetCC(reg, Constant)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001948 //
Vikram S. Advefd3900a2002-03-24 03:33:02 +00001949 // If the SetCC was folded into the user (parent), it will be
1950 // caught above. All other cases are the same as case 42,
1951 // so just fall through.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001952 //
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001953 case 42: // bool: SetCC(reg, reg):
1954 {
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001955 // This generates a SUBCC instruction, putting the difference in
1956 // a result register, and setting a condition code.
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001957 //
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001958 // If the boolean result of the SetCC is used by anything other
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001959 // than a branch instruction, or if it is used outside the current
1960 // basic block, the boolean must be
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001961 // computed and stored in the result register. Otherwise, discard
1962 // the difference (by using %g0) and keep only the condition code.
1963 //
1964 // To compute the boolean result in a register we use a conditional
1965 // move, unless the result of the SUBCC instruction can be used as
1966 // the bool! This assumes that zero is FALSE and any non-zero
1967 // integer is TRUE.
1968 //
1969 InstructionNode* parentNode = (InstructionNode*) subtreeRoot->parent();
1970 Instruction* setCCInstr = subtreeRoot->getInstruction();
Vikram S. Adve242a8082002-05-19 15:25:51 +00001971
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001972 bool keepBoolVal = parentNode == NULL ||
1973 ! AllUsesAreBranches(setCCInstr);
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00001974 bool subValIsBoolVal = setCCInstr->getOpcode() == Instruction::SetNE;
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001975 bool keepSubVal = keepBoolVal && subValIsBoolVal;
1976 bool computeBoolVal = keepBoolVal && ! subValIsBoolVal;
1977
1978 bool mustClearReg;
1979 int valueToMove;
Chris Lattner8e5c0b42001-11-07 14:01:59 +00001980 MachineOpCode movOpCode = 0;
Vikram S. Adve6418eac2002-07-08 23:30:14 +00001981
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001982 // Mark the 4th operand as being a CC register, and as a def
1983 // A TmpInstruction is created to represent the CC "result".
1984 // Unlike other instances of TmpInstruction, this one is used
1985 // by machine code of multiple LLVM instructions, viz.,
1986 // the SetCC and the branch. Make sure to get the same one!
1987 // Note that we do this even for FP CC registers even though they
1988 // are explicit operands, because the type of the operand
1989 // needs to be a floating point condition code, not an integer
1990 // condition code. Think of this as casting the bool result to
1991 // a FP condition code register.
1992 //
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00001993 Value* leftVal = subtreeRoot->leftChild()->getValue();
Chris Lattner9b625032002-05-06 16:15:30 +00001994 bool isFPCompare = leftVal->getType()->isFloatingPoint();
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00001995
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00001996 TmpInstruction* tmpForCC = GetTmpForCC(setCCInstr,
1997 setCCInstr->getParent()->getParent(),
Chris Lattner9b625032002-05-06 16:15:30 +00001998 isFPCompare ? Type::FloatTy : Type::IntTy);
Chris Lattner9c461082002-02-03 07:50:56 +00001999 MachineCodeForInstruction::get(setCCInstr).addTemp(tmpForCC);
Vikram S. Adveff5a09e2001-11-08 05:04:09 +00002000
2001 if (! isFPCompare)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002002 {
2003 // Integer condition: dest. should be %g0 or an integer register.
2004 // If result must be saved but condition is not SetEQ then we need
2005 // a separate instruction to compute the bool result, so discard
2006 // result of SUBcc instruction anyway.
2007 //
Chris Lattner54e898e2003-01-15 19:23:34 +00002008 if (keepSubVal) {
2009 M = BuildMI(SUBcc, 4).addReg(subtreeRoot->leftChild()->getValue())
2010 .addReg(subtreeRoot->rightChild()->getValue())
2011 .addRegDef(subtreeRoot->getValue())
2012 .addCCReg(tmpForCC, MOTy::Def);
2013 } else {
2014 M = BuildMI(SUBcc, 4).addReg(subtreeRoot->leftChild()->getValue())
2015 .addReg(subtreeRoot->rightChild()->getValue())
2016 .addMReg(target.getRegInfo().getZeroRegNum(), MOTy::Def)
2017 .addCCReg(tmpForCC, MOTy::Def);
2018 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002019 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002020
2021 if (computeBoolVal)
2022 { // recompute bool using the integer condition codes
2023 movOpCode =
2024 ChooseMovpccAfterSub(subtreeRoot,mustClearReg,valueToMove);
2025 }
2026 }
2027 else
2028 {
2029 // FP condition: dest of FCMP should be some FCCn register
Chris Lattner54e898e2003-01-15 19:23:34 +00002030 M = BuildMI(ChooseFcmpInstruction(subtreeRoot), 3)
2031 .addCCReg(tmpForCC, MOTy::Def)
2032 .addReg(subtreeRoot->leftChild()->getValue())
2033 .addRegDef(subtreeRoot->rightChild()->getValue());
Vikram S. Adve74825322002-03-18 03:15:35 +00002034 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002035
2036 if (computeBoolVal)
2037 {// recompute bool using the FP condition codes
2038 mustClearReg = true;
2039 valueToMove = 1;
2040 movOpCode = ChooseMovFpccInstruction(subtreeRoot);
2041 }
2042 }
2043
2044 if (computeBoolVal)
2045 {
2046 if (mustClearReg)
2047 {// Unconditionally set register to 0
Chris Lattner54e898e2003-01-15 19:23:34 +00002048 M = BuildMI(SETHI, 2).addZImm(0).addRegDef(setCCInstr);
Vikram S. Adve74825322002-03-18 03:15:35 +00002049 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002050 }
2051
2052 // Now conditionally move `valueToMove' (0 or 1) into the register
Vikram S. Adve6418eac2002-07-08 23:30:14 +00002053 // Mark the register as a use (as well as a def) because the old
2054 // value should be retained if the condition is false.
Chris Lattner54e898e2003-01-15 19:23:34 +00002055 M = BuildMI(movOpCode, 3).addCCReg(tmpForCC).addZImm(valueToMove)
2056 .addReg(setCCInstr, MOTy::UseAndDef);
Vikram S. Adve74825322002-03-18 03:15:35 +00002057 mvec.push_back(M);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002058 }
2059 break;
2060 }
2061
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002062 case 51: // reg: Load(reg)
2063 case 52: // reg: Load(ptrreg)
Chris Lattner54e898e2003-01-15 19:23:34 +00002064 SetOperandsForMemInstr(ChooseLoadInstruction(
2065 subtreeRoot->getValue()->getType()),
2066 mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002067 break;
2068
2069 case 55: // reg: GetElemPtr(reg)
2070 case 56: // reg: GetElemPtrIdx(reg,reg)
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002071 // If the GetElemPtr was folded into the user (parent), it will be
2072 // caught above. For other cases, we have to compute the address.
Chris Lattner54e898e2003-01-15 19:23:34 +00002073 SetOperandsForMemInstr(ADD, mvec, subtreeRoot, target);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002074 break;
Vikram S. Adved3e26482002-10-13 00:18:57 +00002075
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002076 case 57: // reg: Alloca: Implement as 1 instruction:
2077 { // add %fp, offsetFromFP -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002078 AllocationInst* instr =
2079 cast<AllocationInst>(subtreeRoot->getInstruction());
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002080 unsigned tsize =
2081 target.getTargetData().getTypeSize(instr->getAllocatedType());
Vikram S. Adve74825322002-03-18 03:15:35 +00002082 assert(tsize != 0);
2083 CreateCodeForFixedSizeAlloca(target, instr, tsize, 1, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002084 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002085 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002086
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002087 case 58: // reg: Alloca(reg): Implement as 3 instructions:
2088 // mul num, typeSz -> tmp
2089 // sub %sp, tmp -> %sp
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002090 { // add %sp, frameSizeBelowDynamicArea -> result
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002091 AllocationInst* instr =
2092 cast<AllocationInst>(subtreeRoot->getInstruction());
Vikram S. Adve74825322002-03-18 03:15:35 +00002093 const Type* eltType = instr->getAllocatedType();
2094
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002095 // If #elements is constant, use simpler code for fixed-size allocas
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002096 int tsize = (int) target.getTargetData().getTypeSize(eltType);
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002097 Value* numElementsVal = NULL;
2098 bool isArray = instr->isArrayAllocation();
2099
2100 if (!isArray ||
2101 isa<Constant>(numElementsVal = instr->getArraySize()))
2102 { // total size is constant: generate code for fixed-size alloca
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002103 unsigned numElements = isArray?
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002104 cast<ConstantUInt>(numElementsVal)->getValue() : 1;
2105 CreateCodeForFixedSizeAlloca(target, instr, tsize,
2106 numElements, mvec);
2107 }
Vikram S. Adve74825322002-03-18 03:15:35 +00002108 else // total size is not constant.
2109 CreateCodeForVariableSizeAlloca(target, instr, tsize,
Vikram S. Advefd3900a2002-03-24 03:33:02 +00002110 numElementsVal, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002111 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002112 }
Vikram S. Adved3e26482002-10-13 00:18:57 +00002113
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002114 case 61: // reg: Call
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002115 { // Generate a direct (CALL) or indirect (JMPL) call.
2116 // Mark the return-address register, the indirection
2117 // register (for indirect calls), the operands of the Call,
2118 // and the return value (if any) as implicit operands
2119 // of the machine instruction.
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002120 //
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002121 // If this is a varargs function, floating point arguments
2122 // have to passed in integer registers so insert
2123 // copy-float-to-int instructions for each float operand.
2124 //
Chris Lattnerb00c5822001-10-02 03:41:24 +00002125 CallInst *callInstr = cast<CallInst>(subtreeRoot->getInstruction());
Chris Lattner749655f2001-10-13 06:54:30 +00002126 Value *callee = callInstr->getCalledValue();
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002127
2128 // Create hidden virtual register for return address with type void*
Vikram S. Adve242a8082002-05-19 15:25:51 +00002129 TmpInstruction* retAddrReg =
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002130 new TmpInstruction(PointerType::get(Type::VoidTy), callInstr);
Chris Lattner9c461082002-02-03 07:50:56 +00002131 MachineCodeForInstruction::get(callInstr).addTemp(retAddrReg);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002132
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002133 // Generate the machine instruction and its operands.
2134 // Use CALL for direct function calls; this optimistically assumes
2135 // the PC-relative address fits in the CALL address field (22 bits).
2136 // Use JMPL for indirect calls.
2137 //
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002138 if (isa<Function>(callee)) // direct function call
Chris Lattner00dca912003-01-15 17:47:49 +00002139 M = BuildMI(CALL, 1).addPCDisp(callee);
Vikram S. Adve4a8bb2b2002-09-28 16:55:41 +00002140 else // indirect function call
Chris Lattner4690e6d2003-01-15 18:11:11 +00002141 M = BuildMI(JMPLCALL, 3).addReg(callee).addSImm((int64_t)0)
2142 .addRegDef(retAddrReg);
Vikram S. Adve74825322002-03-18 03:15:35 +00002143 mvec.push_back(M);
Vikram S. Advea10d1a72002-03-31 19:07:35 +00002144
Vikram S. Adve242a8082002-05-19 15:25:51 +00002145 const FunctionType* funcType =
2146 cast<FunctionType>(cast<PointerType>(callee->getType())
2147 ->getElementType());
2148 bool isVarArgs = funcType->isVarArg();
2149 bool noPrototype = isVarArgs && funcType->getNumParams() == 0;
Vikram S. Advedbc4fad2002-04-25 04:37:51 +00002150
Vikram S. Adveaabb5952002-10-29 19:37:31 +00002151 // Use a descriptor to pass information about call arguments
2152 // to the register allocator. This descriptor will be "owned"
2153 // and freed automatically when the MachineCodeForInstruction
2154 // object for the callInstr goes away.
Vikram S. Adve242a8082002-05-19 15:25:51 +00002155 CallArgsDescriptor* argDesc = new CallArgsDescriptor(callInstr,
2156 retAddrReg, isVarArgs, noPrototype);
Vikram S. Advea995e602001-10-11 04:23:19 +00002157
Vikram S. Adve242a8082002-05-19 15:25:51 +00002158 assert(callInstr->getOperand(0) == callee
2159 && "This is assumed in the loop below!");
2160
2161 for (unsigned i=1, N=callInstr->getNumOperands(); i < N; ++i)
2162 {
2163 Value* argVal = callInstr->getOperand(i);
2164 Instruction* intArgReg = NULL;
2165
2166 // Check for FP arguments to varargs functions.
2167 // Any such argument in the first $K$ args must be passed in an
2168 // integer register, where K = #integer argument registers.
2169 if (isVarArgs && argVal->getType()->isFloatingPoint())
2170 {
2171 // If it is a function with no prototype, pass value
2172 // as an FP value as well as a varargs value
2173 if (noPrototype)
2174 argDesc->getArgInfo(i-1).setUseFPArgReg();
2175
2176 // If this arg. is in the first $K$ regs, add a copy
2177 // float-to-int instruction to pass the value as an integer.
Vikram S. Adved3e26482002-10-13 00:18:57 +00002178 if (i <= target.getRegInfo().GetNumOfIntArgRegs())
Vikram S. Adve242a8082002-05-19 15:25:51 +00002179 {
2180 MachineCodeForInstruction &destMCFI =
2181 MachineCodeForInstruction::get(callInstr);
2182 intArgReg = new TmpInstruction(Type::IntTy, argVal);
2183 destMCFI.addTemp(intArgReg);
2184
2185 vector<MachineInstr*> copyMvec;
2186 target.getInstrInfo().CreateCodeToCopyFloatToInt(target,
2187 callInstr->getParent()->getParent(),
2188 argVal, (TmpInstruction*) intArgReg,
2189 copyMvec, destMCFI);
2190 mvec.insert(mvec.begin(),copyMvec.begin(),copyMvec.end());
2191
2192 argDesc->getArgInfo(i-1).setUseIntArgReg();
2193 argDesc->getArgInfo(i-1).setArgCopy(intArgReg);
2194 }
2195 else
2196 // Cannot fit in first $K$ regs so pass the arg on the stack
2197 argDesc->getArgInfo(i-1).setUseStackSlot();
2198 }
2199
2200 if (intArgReg)
2201 mvec.back()->addImplicitRef(intArgReg);
2202
2203 mvec.back()->addImplicitRef(argVal);
2204 }
2205
2206 // Add the return value as an implicit ref. The call operands
2207 // were added above.
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002208 if (callInstr->getType() != Type::VoidTy)
Vikram S. Adve74825322002-03-18 03:15:35 +00002209 mvec.back()->addImplicitRef(callInstr, /*isDef*/ true);
Vikram S. Advea995e602001-10-11 04:23:19 +00002210
Vikram S. Adveea21a6c2001-10-20 20:57:06 +00002211 // For the CALL instruction, the ret. addr. reg. is also implicit
Chris Lattnerb0d04722002-03-26 17:58:12 +00002212 if (isa<Function>(callee))
Vikram S. Adve74825322002-03-18 03:15:35 +00002213 mvec.back()->addImplicitRef(retAddrReg, /*isDef*/ true);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002214
Vikram S. Adve74825322002-03-18 03:15:35 +00002215 // delay slot
Chris Lattner54e898e2003-01-15 19:23:34 +00002216 mvec.push_back(BuildMI(NOP, 0));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002217 break;
Vikram S. Adveb7f06f42001-11-04 19:34:49 +00002218 }
Vikram S. Adve242a8082002-05-19 15:25:51 +00002219
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002220 case 62: // reg: Shl(reg, reg)
Vikram S. Adve242a8082002-05-19 15:25:51 +00002221 {
2222 Value* argVal1 = subtreeRoot->leftChild()->getValue();
2223 Value* argVal2 = subtreeRoot->rightChild()->getValue();
2224 Instruction* shlInstr = subtreeRoot->getInstruction();
2225
2226 const Type* opType = argVal1->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002227 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2228 "Shl unsupported for other types");
Vikram S. Adve242a8082002-05-19 15:25:51 +00002229
2230 CreateShiftInstructions(target, shlInstr->getParent()->getParent(),
2231 (opType == Type::LongTy)? SLLX : SLL,
2232 argVal1, argVal2, 0, shlInstr, mvec,
2233 MachineCodeForInstruction::get(shlInstr));
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002234 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002235 }
2236
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002237 case 63: // reg: Shr(reg, reg)
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002238 { const Type* opType = subtreeRoot->leftChild()->getValue()->getType();
Chris Lattner0c4e8862002-09-03 01:08:28 +00002239 assert((opType->isInteger() || isa<PointerType>(opType)) &&
2240 "Shr unsupported for other types");
Chris Lattner54e898e2003-01-15 19:23:34 +00002241 Add3OperandInstr(opType->isSigned()
2242 ? (opType == Type::LongTy ? SRAX : SRA)
2243 : (opType == Type::LongTy ? SRLX : SRL),
2244 subtreeRoot, mvec);
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002245 break;
Vikram S. Adve6ad7c552001-11-09 02:18:16 +00002246 }
2247
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002248 case 64: // reg: Phi(reg,reg)
Vikram S. Adve74825322002-03-18 03:15:35 +00002249 break; // don't forward the value
2250
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002251 case 71: // reg: VReg
2252 case 72: // reg: Constant
Vikram S. Adve74825322002-03-18 03:15:35 +00002253 break; // don't forward the value
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002254
2255 default:
2256 assert(0 && "Unrecognized BURG rule");
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002257 break;
2258 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002259 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002260
Chris Lattner20b1ea02001-09-14 03:47:57 +00002261 if (forwardOperandNum >= 0)
2262 { // We did not generate a machine instruction but need to use operand.
2263 // If user is in the same tree, replace Value in its machine operand.
2264 // If not, insert a copy instruction which should get coalesced away
2265 // by register allocation.
2266 if (subtreeRoot->parent() != NULL)
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002267 ForwardOperand(subtreeRoot, subtreeRoot->parent(), forwardOperandNum);
Chris Lattner20b1ea02001-09-14 03:47:57 +00002268 else
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002269 {
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002270 vector<MachineInstr*> minstrVec;
Vikram S. Adve242a8082002-05-19 15:25:51 +00002271 Instruction* instr = subtreeRoot->getInstruction();
2272 target.getInstrInfo().
2273 CreateCopyInstructionsByType(target,
2274 instr->getParent()->getParent(),
2275 instr->getOperand(forwardOperandNum),
2276 instr, minstrVec,
2277 MachineCodeForInstruction::get(instr));
Vikram S. Adve7fe27872001-10-18 00:26:20 +00002278 assert(minstrVec.size() > 0);
Vikram S. Adve74825322002-03-18 03:15:35 +00002279 mvec.insert(mvec.end(), minstrVec.begin(), minstrVec.end());
Vikram S. Adve4cecdd22001-10-01 00:12:53 +00002280 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002281 }
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002282
2283 if (maskUnsignedResult)
2284 { // If result is unsigned and smaller than int reg size,
2285 // we need to clear high bits of result value.
2286 assert(forwardOperandNum < 0 && "Need mask but no instruction generated");
2287 Instruction* dest = subtreeRoot->getInstruction();
Vikram S. Adve8cfffd32002-08-24 20:56:53 +00002288 if (dest->getType()->isUnsigned())
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002289 {
Chris Lattnerea45d7b2002-12-28 20:19:44 +00002290 unsigned destSize=target.getTargetData().getTypeSize(dest->getType());
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002291 if (destSize <= 4)
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002292 { // Mask high bits. Use a TmpInstruction to represent the
2293 // intermediate result before masking. Since those instructions
2294 // have already been generated, go back and substitute tmpI
2295 // for dest in the result position of each one of them.
2296 TmpInstruction *tmpI = new TmpInstruction(dest->getType(), dest,
2297 NULL, "maskHi");
2298 MachineCodeForInstruction::get(dest).addTemp(tmpI);
2299
2300 for (unsigned i=0, N=mvec.size(); i < N; ++i)
2301 mvec[i]->substituteValue(dest, tmpI);
2302
Chris Lattnere5b1ed92003-01-15 00:03:28 +00002303 M = BuildMI(SRL, 3).addReg(tmpI).addZImm(8*(4-destSize))
2304 .addReg(dest, MOTy::Def);
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002305 mvec.push_back(M);
2306 }
Chris Lattner7a5adc32003-04-26 19:44:35 +00002307 else if (destSize < 8)
Vikram S. Advebabc0fa2002-09-05 18:32:13 +00002308 assert(0 && "Unsupported type size: 32 < size < 64 bits");
Vikram S. Adve65a2dee2002-08-13 17:40:54 +00002309 }
2310 }
Chris Lattner20b1ea02001-09-14 03:47:57 +00002311}